]> granicus.if.org Git - clang/commitdiff
make the token paste avoidance logic turn "..." into ".. ." instead of ". . ."
authorChris Lattner <sabre@nondot.org>
Wed, 14 Apr 2010 03:57:19 +0000 (03:57 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 14 Apr 2010 03:57:19 +0000 (03:57 +0000)
when avoiding paste.  Patch by David Peixotto!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101218 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/TokenConcatenation.h
lib/Frontend/PrintPreprocessedOutput.cpp
lib/Lex/TokenConcatenation.cpp
lib/Rewrite/HTMLRewrite.cpp
test/Preprocessor/output_paste_avoid.c

index d759e47e57fcd4017738ece4cb919283a5e1d5ad..094990a6e317a460d48b68c33ba02de2a6765721 100644 (file)
@@ -58,7 +58,9 @@ namespace clang {
   public:
     TokenConcatenation(Preprocessor &PP);
 
-    bool AvoidConcat(const Token &PrevTok, const Token &Tok) const;
+    bool AvoidConcat(const Token &PrevPrevTok, 
+                     const Token &PrevTok, 
+                     const Token &Tok) const;
 
   private:
     /// StartsWithL - Return true if the spelling of this token starts with 'L'.
index a64f6e7c0c694b783ac1a76cd88109661c4920a0..b4718e9ad499cabf8a37c0eff571c381cb38bfd7 100644 (file)
@@ -125,8 +125,9 @@ public:
   }
   bool MoveToLine(unsigned LineNo);
 
-  bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
-    return ConcatInfo.AvoidConcat(PrevTok, Tok);
+  bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 
+                   const Token &Tok) {
+    return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
   }
   void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
 
@@ -396,6 +397,7 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
                                     PrintPPOutputPPCallbacks *Callbacks,
                                     llvm::raw_ostream &OS) {
   char Buffer[256];
+  Token PrevPrevTok;
   Token PrevTok;
   while (1) {
 
@@ -407,7 +409,7 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
                // useful to look at and no concatenation could happen anyway.
                (Callbacks->hasEmittedTokensOnThisLine() &&
                 // Don't print "-" next to "-", it would form "--".
-                Callbacks->AvoidConcat(PrevTok, Tok))) {
+                Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
       OS << ' ';
     }
 
@@ -438,6 +440,7 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
 
     if (Tok.is(tok::eof)) break;
 
+    PrevPrevTok = PrevTok;
     PrevTok = Tok;
     PP.Lex(Tok);
   }
index 51d2e2326fcaadf45fbce37678b945030804c237..fc6db2151a3ac0aa0e062143a3096327cce34cb9 100644 (file)
@@ -124,7 +124,8 @@ static char GetFirstChar(Preprocessor &PP, const Token &Tok) {
 /// but the resulting output won't have incorrect concatenations going on.
 /// Examples include "..", which we print with a space between, because we
 /// don't want to track enough to tell "x.." from "...".
-bool TokenConcatenation::AvoidConcat(const Token &PrevTok,
+bool TokenConcatenation::AvoidConcat(const Token &PrevPrevTok,
+                                     const Token &PrevTok,
                                      const Token &Tok) const {
   // First, check to see if the tokens were directly adjacent in the original
   // source.  If they were, it must be okay to stick them together: if there
@@ -192,7 +193,8 @@ bool TokenConcatenation::AvoidConcat(const Token &PrevTok,
     return isalnum(FirstChar) || Tok.is(tok::numeric_constant) ||
     FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
   case tok::period:          // ..., .*, .1234
-    return FirstChar == '.' || isdigit(FirstChar) ||
+    return (FirstChar == '.' && PrevPrevTok.is(tok::period)) ||
+    isdigit(FirstChar) ||
     (PP.getLangOptions().CPlusPlus && FirstChar == '*');
   case tok::amp:             // &&
     return FirstChar == '&';
index 7b78070a27f81356d7733baf4ed1f4659b9af9ec..5fe064990e9636721e9fc6b33b66cc070c0f4e06 100644 (file)
@@ -533,6 +533,7 @@ void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
     std::string Expansion = EscapeText(TmpPP.getSpelling(Tok));
     unsigned LineLen = Expansion.size();
 
+    Token PrevPrevTok;
     Token PrevTok = Tok;
     // Okay, eat this token, getting the next one.
     TmpPP.Lex(Tok);
@@ -553,13 +554,14 @@ void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
       // If the tokens were already space separated, or if they must be to avoid
       // them being implicitly pasted, add a space between them.
       if (Tok.hasLeadingSpace() ||
-          ConcatInfo.AvoidConcat(PrevTok, Tok))
+          ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
         Expansion += ' ';
 
       // Escape any special characters in the token text.
       Expansion += EscapeText(TmpPP.getSpelling(Tok));
       LineLen += Expansion.size();
 
+      PrevPrevTok = PrevTok;
       PrevTok = Tok;
       TmpPP.Lex(Tok);
     }
index 8c6173a78d1d0fbc5832c2446d16769be3b24c32..8e4f3a4bf4d960e0aee65a72b5eb0160764927c7 100644 (file)
@@ -4,8 +4,11 @@
 #define y(a) ..a
 A: y(.)
 // This should print as ".. ." to avoid turning into ...
-// CHECK: A: . . .
+// CHECK: A: .. .
 
+#define X 0 .. 1
+B: X
+// CHECK: B: 0 .. 1
 
 #define DOT .
 C: ..DOT