[cfe-commits] r67163 - in /cfe/trunk: lib/Lex/TokenConcatenation.cpp test/Lexer/token-concat.c

Daniel Dunbar daniel at zuster.org
Tue Mar 17 20:32:31 PDT 2009


Author: ddunbar
Date: Tue Mar 17 22:32:24 2009
New Revision: 67163

URL: http://llvm.org/viewvc/llvm-project?rev=67163&view=rev
Log:
Fix -E mismatch; an identifier followed by a numeric constant does not
require a space (to avoid concatenation) if the numeric constant had a
leading period.
 - PR3819.

Added:
    cfe/trunk/test/Lexer/token-concat.c
Modified:
    cfe/trunk/lib/Lex/TokenConcatenation.cpp

Modified: cfe/trunk/lib/Lex/TokenConcatenation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/TokenConcatenation.cpp?rev=67163&r1=67162&r2=67163&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/TokenConcatenation.cpp (original)
+++ cfe/trunk/lib/Lex/TokenConcatenation.cpp Tue Mar 17 22:32:24 2009
@@ -90,6 +90,29 @@
   TokenInfo[tok::equal       ] |= aci_avoid_equal;           // ==
 }
 
+/// GetFirstChar - Get the first character of the token \arg Tok,
+/// avoiding calls to getSpelling where possible.
+static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
+  if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
+    // Avoid spelling identifiers, the most common form of token.
+    return II->getName()[0];
+  } else if (!Tok.needsCleaning()) {
+    if (Tok.isLiteral() && Tok.getLiteralData()) {
+      return *Tok.getLiteralData();
+    } else {
+      SourceManager &SM = PP.getSourceManager();
+      return *SM.getCharacterData(SM.getSpellingLoc(Tok.getLocation()));
+    }
+  } else if (Tok.getLength() < 256) {
+    char Buffer[256];
+    const char *TokPtr = Buffer;
+    PP.getSpelling(Tok, TokPtr);
+    return TokPtr[0];
+  } else {
+    return PP.getSpelling(Tok)[0];
+  }
+}
+
 /// 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
@@ -103,8 +126,6 @@
 /// don't want to track enough to tell "x.." from "...".
 bool TokenConcatenation::AvoidConcat(const Token &PrevTok,
                                      const Token &Tok) const {
-  char Buffer[256];
-  
   tok::TokenKind PrevKind = PrevTok.getKind();
   if (PrevTok.getIdentifierInfo())  // Language keyword or named operator.
     PrevKind = tok::identifier;
@@ -130,30 +151,18 @@
   char FirstChar = 0;
   if (ConcatInfo & aci_custom) {
     // If the token does not need to know the first character, don't get it.
-  } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
-    // Avoid spelling identifiers, the most common form of token.
-    FirstChar = II->getName()[0];
-  } else if (!Tok.needsCleaning()) {
-    if (Tok.isLiteral() && Tok.getLiteralData()) {
-      FirstChar = *Tok.getLiteralData();
-    } else {
-      SourceManager &SrcMgr = PP.getSourceManager();
-      FirstChar =
-      *SrcMgr.getCharacterData(SrcMgr.getSpellingLoc(Tok.getLocation()));
-    }
-  } else if (Tok.getLength() < 256) {
-    const char *TokPtr = Buffer;
-    PP.getSpelling(Tok, TokPtr);
-    FirstChar = TokPtr[0];
   } else {
-    FirstChar = PP.getSpelling(Tok)[0];
+    FirstChar = GetFirstChar(PP, Tok);
   }
-  
+    
   switch (PrevKind) {
   default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
-  case tok::identifier:   // id+id or id+number or id+L"foo".
-    if (Tok.is(tok::numeric_constant) || Tok.getIdentifierInfo() ||
-        Tok.is(tok::wide_string_literal) /* ||
+  case tok::identifier:   // id+id or id+number or id+L"foo".    
+    // id+'.'... will not append.
+    if (Tok.is(tok::numeric_constant))
+      return GetFirstChar(PP, Tok) != '.';
+
+    if (Tok.getIdentifierInfo() || Tok.is(tok::wide_string_literal) /* ||
      Tok.is(tok::wide_char_literal)*/)
       return true;
     

Added: cfe/trunk/test/Lexer/token-concat.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/token-concat.c?rev=67163&view=auto

==============================================================================
--- cfe/trunk/test/Lexer/token-concat.c (added)
+++ cfe/trunk/test/Lexer/token-concat.c Tue Mar 17 22:32:24 2009
@@ -0,0 +1,4 @@
+// RUN: clang -E -x c -o %t %s &&
+// RUN: grep 'IDENT.2' %t
+
+IDENT.2





More information about the cfe-commits mailing list