diff options
author | Chris Lattner <sabre@nondot.org> | 2009-02-18 19:21:10 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-02-18 19:21:10 +0000 |
commit | ddb7191920f657397c27bc345037e9a92a232e6d (patch) | |
tree | ec3e48d58f5bc48bab4cef592aa4a941e683c19c /clang/lib/Lex/LiteralSupport.cpp | |
parent | 57a09cfcbcf789a342d68ffa538f240d47bde4ab (diff) | |
download | bcm5719-llvm-ddb7191920f657397c27bc345037e9a92a232e6d.tar.gz bcm5719-llvm-ddb7191920f657397c27bc345037e9a92a232e6d.zip |
Next step toward making string diagnostics correct: handle
escapes in the string for subtoken positioning. This gives
us working examples like:
t.m:5:16: warning: field width should have type 'int', but argument has type 'unsigned int'
printf("\n\n%*d", (unsigned) 1, 1);
^ ~~~~~~~~~~~~
where before the caret pointed two spaces to the left.
llvm-svn: 64940
Diffstat (limited to 'clang/lib/Lex/LiteralSupport.cpp')
-rw-r--r-- | clang/lib/Lex/LiteralSupport.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp index 93b653aae6f..9815f9b91e7 100644 --- a/clang/lib/Lex/LiteralSupport.cpp +++ b/clang/lib/Lex/LiteralSupport.cpp @@ -798,3 +798,49 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks, return; } } + + +/// getOffsetOfStringByte - This function returns the offset of the +/// specified byte of the string data represented by Token. This handles +/// advancing over escape sequences in the string. +unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, + unsigned ByteNo, + Preprocessor &PP) { + // Get the spelling of the token. + llvm::SmallString<16> SpellingBuffer; + SpellingBuffer.resize(Tok.getLength()); + + const char *SpellingPtr = &SpellingBuffer[0]; + unsigned TokLen = PP.getSpelling(Tok, SpellingPtr); + + assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet"); + + + const char *SpellingStart = SpellingPtr; + const char *SpellingEnd = SpellingPtr+TokLen; + + // Skip over the leading quote. + assert(SpellingPtr[0] == '"' && "Should be a string literal!"); + ++SpellingPtr; + + // Skip over bytes until we find the offset we're looking for. + while (ByteNo) { + assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); + + // Step over non-escapes simply. + if (*SpellingPtr != '\\') { + ++SpellingPtr; + --ByteNo; + continue; + } + + // Otherwise, this is an escape character. Advance over it. + bool HadError = false; + ProcessCharEscape(SpellingPtr, SpellingEnd, HadError, + Tok.getLocation(), false, PP); + assert(!HadError && "This method isn't valid on erroneous strings"); + --ByteNo; + } + + return SpellingPtr-SpellingStart; +} |