[cfe-commits] r126221 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPDirectives.cpp lib/Lex/Pragma.cpp

Peter Collingbourne peter at pcc.me.uk
Tue Feb 22 05:49:07 PST 2011


Author: pcc
Date: Tue Feb 22 07:49:06 2011
New Revision: 126221

URL: http://llvm.org/viewvc/llvm-project?rev=126221&view=rev
Log:
Reimplement __pragma support using a TokenLexer

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/lib/Lex/Pragma.cpp

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=126221&r1=126220&r2=126221&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Feb 22 07:49:06 2011
@@ -942,9 +942,6 @@
   /// is not enclosed within a string literal.
   void HandleMicrosoft__pragma(Token &Tok);
 
-  void Handle_Pragma(unsigned Introducer, const std::string &StrVal, 
-                     SourceLocation PragmaLoc, SourceLocation RParenLoc);
-
   /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
   /// start lexing tokens from it instead of the current buffer.
   void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=126221&r1=126220&r2=126221&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Tue Feb 22 07:49:06 2011
@@ -86,6 +86,7 @@
   Token Tmp;
   do {
     LexUnexpandedToken(Tmp);
+    assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
   } while (Tmp.isNot(tok::eom));
 }
 

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Pragma.cpp?rev=126221&r1=126220&r2=126221&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Tue Feb 22 07:49:06 2011
@@ -175,7 +175,22 @@
     }
   }
   
-  Handle_Pragma(PIK__Pragma, StrVal, PragmaLoc, RParenLoc);
+  // Plop the string (including the newline and trailing null) into a buffer
+  // where we can lex it.
+  Token TmpTok;
+  TmpTok.startToken();
+  CreateString(&StrVal[0], StrVal.size(), TmpTok);
+  SourceLocation TokLoc = TmpTok.getLocation();
+
+  // Make and enter a lexer object so that we lex and expand the tokens just
+  // like any others.
+  Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
+                                        StrVal.size(), *this);
+
+  EnterSourceFileWithLexer(TL, 0);
+
+  // With everything set up, lex this as a #pragma directive.
+  HandlePragmaDirective(PIK__Pragma);
 
   // Finally, return whatever came after the pragma directive.
   return Lex(Tok);
@@ -194,16 +209,16 @@
     return;
   }
 
-  // Get the tokens enclosed within the __pragma().
+  // Get the tokens enclosed within the __pragma(), as well as the final ')'.
   llvm::SmallVector<Token, 32> PragmaToks;
   int NumParens = 0;
   Lex(Tok);
   while (Tok.isNot(tok::eof)) {
+    PragmaToks.push_back(Tok);
     if (Tok.is(tok::l_paren))
       NumParens++;
     else if (Tok.is(tok::r_paren) && NumParens-- == 0)
       break;
-    PragmaToks.push_back(Tok);
     Lex(Tok);
   }
 
@@ -212,45 +227,23 @@
     return;
   }
 
-  // Build the pragma string.
-  std::string StrVal = " ";
-  for (llvm::SmallVector<Token, 32>::iterator I =
-       PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) {
-    StrVal += getSpelling(*I);
-  }
-  
-  SourceLocation RParenLoc = Tok.getLocation();
+  PragmaToks.front().setFlag(Token::LeadingSpace);
 
-  Handle_Pragma(PIK___pragma, StrVal, PragmaLoc, RParenLoc);
+  // Replace the ')' with an EOM to mark the end of the pragma.
+  PragmaToks.back().setKind(tok::eom);
 
-  // Finally, return whatever came after the pragma directive.
-  return Lex(Tok);
-}
-
-void Preprocessor::Handle_Pragma(unsigned Introducer,
-                                 const std::string &StrVal,
-                                 SourceLocation PragmaLoc,
-                                 SourceLocation RParenLoc) {
-
-  // Plop the string (including the newline and trailing null) into a buffer
-  // where we can lex it.
-  Token TmpTok;
-  TmpTok.startToken();
-  CreateString(&StrVal[0], StrVal.size(), TmpTok);
-  SourceLocation TokLoc = TmpTok.getLocation();
-
-  // Make and enter a lexer object so that we lex and expand the tokens just
-  // like any others.
-  Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
-                                        StrVal.size(), *this);
+  Token *TokArray = new Token[PragmaToks.size()];
+  std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
 
-  EnterSourceFileWithLexer(TL, 0);
+  // Push the tokens onto the stack.
+  EnterTokenStream(TokArray, PragmaToks.size(), true, true);
 
   // With everything set up, lex this as a #pragma directive.
-  HandlePragmaDirective(Introducer);
-}
-
+  HandlePragmaDirective(PIK___pragma);
 
+  // Finally, return whatever came after the pragma directive.
+  return Lex(Tok);
+}
 
 /// HandlePragmaOnce - Handle #pragma once.  OnceTok is the 'once'.
 ///





More information about the cfe-commits mailing list