[cfe-commits] r46000 - in /cfe/trunk: Driver/PrintPreprocessedOutput.cpp test/Preprocessor/output_paste_avoid.c

Chris Lattner sabre at nondot.org
Mon Jan 14 21:22:14 PST 2008


Author: lattner
Date: Mon Jan 14 23:22:14 2008
New Revision: 46000

URL: http://llvm.org/viewvc/llvm-project?rev=46000&view=rev
Log:
avoid pasting L + "foo" into L"foo".

Modified:
    cfe/trunk/Driver/PrintPreprocessedOutput.cpp
    cfe/trunk/test/Preprocessor/output_paste_avoid.c

Modified: cfe/trunk/Driver/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/PrintPreprocessedOutput.cpp?rev=46000&r1=45999&r2=46000&view=diff

==============================================================================
--- cfe/trunk/Driver/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/Driver/PrintPreprocessedOutput.cpp Mon Jan 14 23:22:14 2008
@@ -420,8 +420,8 @@
   TokenInfo[tok::equal       ] |= aci_avoid_equal;           // ==
 }
 
+/// StartsWithL - Return true if the spelling of this token starts with 'L'.
 static bool StartsWithL(const Token &Tok, Preprocessor &PP) {
-  char Buffer[256];
   if (!Tok.needsCleaning()) {
     SourceManager &SrcMgr = PP.getSourceManager();
     return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
@@ -429,6 +429,7 @@
   }
   
   if (Tok.getLength() < 256) {
+    char Buffer[256];
     const char *TokPtr = Buffer;
     PP.getSpelling(Tok, TokPtr);
     return TokPtr[0] == 'L';
@@ -437,6 +438,28 @@
   return PP.getSpelling(Tok)[0] == 'L';
 }
 
+/// IsIdentifierL - Return true if the spelling of this token is literally 'L'.
+static bool IsIdentifierL(const Token &Tok, Preprocessor &PP) {
+  if (!Tok.needsCleaning()) {
+    if (Tok.getLength() != 1)
+      return false;
+    SourceManager &SrcMgr = PP.getSourceManager();
+    return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
+               == 'L';
+  }
+  
+  if (Tok.getLength() < 256) {
+    char Buffer[256];
+    const char *TokPtr = Buffer;
+    if (PP.getSpelling(Tok, TokPtr) != 1) 
+      return false;
+    return TokPtr[0] == 'L';
+  }
+  
+  return PP.getSpelling(Tok) == "L";
+}
+
+
 /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
 /// the two individual tokens to be lexed as a single token, return true (which
 /// causes a space to be printed between them).  This allows the output of -E
@@ -513,7 +536,9 @@
     if (StartsWithL(Tok, PP))
       return true;
 
-    return false;
+    // Otherwise, this is a narrow character or string.  If the *identifier* is
+    // a literal 'L', avoid pasting L "foo" -> L"foo".
+    return IsIdentifierL(PrevTok, PP);
   case tok::numeric_constant:
     return isalnum(FirstChar) || Tok.is(tok::numeric_constant) ||
            FirstChar == '+' || FirstChar == '-' || FirstChar == '.';

Modified: cfe/trunk/test/Preprocessor/output_paste_avoid.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/output_paste_avoid.c?rev=46000&r1=45999&r2=46000&view=diff

==============================================================================
--- cfe/trunk/test/Preprocessor/output_paste_avoid.c (original)
+++ cfe/trunk/test/Preprocessor/output_paste_avoid.c Mon Jan 14 23:22:14 2008
@@ -1,5 +1,6 @@
 // RUN: clang -E %s | grep '+ + - - + + = = =' &&
 // RUN: clang -E %s | not grep -F '...'
+// RUN: clang -E %s | not grep -F 'L"str"'
 
 // This should print as ".. ." to avoid turning into ...
 #define y(a) ..a
@@ -10,3 +11,8 @@
 #define f(x) =x=
 +PLUS -EMPTY- PLUS+ f(=)
 
+
+// Should expand to L "str" not L"str"
+#define test(x) L#x
+test(str)
+





More information about the cfe-commits mailing list