diff options
author | Chris Lattner <sabre@nondot.org> | 2009-02-18 18:34:12 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-02-18 18:34:12 +0000 |
commit | f638b97fe06a36e3b6de5ce1705ac4181cbf6f9b (patch) | |
tree | a9d8bb2756d003a572a2fb119ebe6371e25c53a7 | |
parent | df18c6a9af34550404058f80f9d2e74b42bb4a74 (diff) | |
download | bcm5719-llvm-f638b97fe06a36e3b6de5ce1705ac4181cbf6f9b.tar.gz bcm5719-llvm-f638b97fe06a36e3b6de5ce1705ac4181cbf6f9b.zip |
use the full spelling of a string literal token so that trigraphs
and escaped newlines don't throw off the offset computation.
On this testcase:
printf("abc\
def"
"%*d", (unsigned) 1, 1);
Before:
t.m:5:5: warning: field width should have type 'int', but argument has type 'unsigned int'
def"
^
after:
t.m:6:12: warning: field width should have type 'int', but argument has type 'unsigned int'
"%*d", (unsigned) 1, 1);
^ ~~~~~~~~~~~~
llvm-svn: 64930
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 9 | ||||
-rw-r--r-- | clang/test/Sema/format-strings.c | 9 |
2 files changed, 15 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index f469684e503..6ffca1b7b87 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -31,6 +31,8 @@ SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, unsigned ByteNo) const { assert(!SL->isWide() && "This doesn't work for wide strings yet"); + llvm::SmallString<32> SpellingBuffer; + // Loop over all of the tokens in this string until we find the one that // contains the byte we're looking for. unsigned TokNo = 0; @@ -61,8 +63,13 @@ SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, Token TheTok; TheLexer.LexFromRawLexer(TheTok); + // Get the spelling of the token to remove trigraphs and escaped newlines. + SpellingBuffer.resize(TheTok.getLength()); + const char *SpellingPtr = &SpellingBuffer[0]; + unsigned TokLen = PP.getSpelling(TheTok, SpellingPtr); + // The length of the string is the token length minus the two quotes. - unsigned TokNumBytes = TheTok.getLength()-2; + unsigned TokNumBytes = TokLen-2; // If we found the token we're looking for, return the location. // FIXME: This should consider character escapes! diff --git a/clang/test/Sema/format-strings.c b/clang/test/Sema/format-strings.c index 024e06ab591..5007bb0fa40 100644 --- a/clang/test/Sema/format-strings.c +++ b/clang/test/Sema/format-strings.c @@ -30,8 +30,13 @@ void check_string_literal( FILE* fp, const char* s, char *buf, ... ) { __builtin___vsnprintf_chk(buf,2,0,-1,s,ap); // no-warning __builtin___vsnprintf_chk(buf,2,0,-1,global_fmt,ap); // expected-warning {{format string is not a string literal}} - printf("abc" - "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}} + // rdar://6079877 + printf("abc" + "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}} + printf("abc\ +def" + "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}} + } void check_conditional_literal(const char* s, int i) { |