[cfe-commits] r64930 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/Sema/format-strings.c

Chris Lattner sabre at nondot.org
Wed Feb 18 10:34:13 PST 2009


Author: lattner
Date: Wed Feb 18 12:34:12 2009
New Revision: 64930

URL: http://llvm.org/viewvc/llvm-project?rev=64930&view=rev
Log:
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);
           ^    ~~~~~~~~~~~~


Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=64930&r1=64929&r2=64930&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Feb 18 12:34:12 2009
@@ -31,6 +31,8 @@
                                                     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 @@
     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!

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=64930&r1=64929&r2=64930&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Wed Feb 18 12:34:12 2009
@@ -30,8 +30,13 @@
   __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) {





More information about the cfe-commits mailing list