[cfe-commits] r38701 - in /cfe/cfe/trunk: Lex/Lexer.cpp Lex/Pragma.cpp Lex/Preprocessor.cpp include/clang/Lex/Lexer.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:56 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:56 2007
New Revision: 38701

URL: http://llvm.org/viewvc/llvm-project?rev=38701&view=rev
Log:
Change Preprocessor::SkippingContents into Lexer::LexingRawMode.  Raw mode
is an intra-lexer property, not a inter-lexer property, so it makes sense
for it to be define here.  It also makes no sense for macros, and allows us
to define it more carefully in the header.

While I'm at it, improve comments and structuring in Lexer.h

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

Modified: cfe/cfe/trunk/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Lexer.cpp?rev=38701&r1=38700&r2=38701&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Lexer.cpp (original)
+++ cfe/cfe/trunk/Lex/Lexer.cpp Wed Jul 11 11:23:56 2007
@@ -58,6 +58,12 @@
 
   // We are not after parsing #include.
   ParsingFilename = false;
+
+  // We are not in raw mode.  Raw mode disables diagnostics and interpretation
+  // of tokens (e.g. identifiers, thus disabling macro expansion).  It is used
+  // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
+  // or otherwise skipping over tokens.
+  LexingRawMode = false;
 }
 
 /// Stringify - Convert the specified string into a C string, with surrounding
@@ -896,10 +902,10 @@
     return;
   }        
 
-  // If we aren't skipping, issue diagnostics.  If we are skipping, let the
-  // skipping code do this: there are multiple possible reasons for skipping,
-  // and not all want these diagnostics.
-  if (!PP.isSkipping()) {
+  // If we aren't in raw mode, issue diagnostics. If we are in raw mode, let the
+  // code that put us into raw mode do this: there are multiple possible reasons
+  // for raw mode, and not all want these diagnostics.
+  if (!LexingRawMode) {
     // If we are in a #if directive, emit an error.
     while (!ConditionalStack.empty()) {
       PP.Diag(ConditionalStack.back().IfLoc,
@@ -1176,7 +1182,7 @@
         // it's actually the start of a preprocessing directive.  Callback to
         // the preprocessor to handle it.
         // FIXME: -fpreprocessed mode??
-        if (Result.isAtStartOfLine() && !PP.isSkipping()) {
+        if (Result.isAtStartOfLine() && !LexingRawMode) {
           BufferPtr = CurPtr;
           PP.HandleDirective(Result);
           
@@ -1321,7 +1327,7 @@
       // it's actually the start of a preprocessing directive.  Callback to
       // the preprocessor to handle it.
       // FIXME: -fpreprocessed mode??
-      if (Result.isAtStartOfLine() && !PP.isSkipping()) {
+      if (Result.isAtStartOfLine() && !LexingRawMode) {
         BufferPtr = CurPtr;
         PP.HandleDirective(Result);
         
@@ -1357,7 +1363,7 @@
       return LexIdentifier(Result, CurPtr);
     }
     
-    if (!PP.isSkipping()) Diag(CurPtr-1, diag::err_stray_character);
+    if (!LexingRawMode) Diag(CurPtr-1, diag::err_stray_character);
     BufferPtr = CurPtr;
     goto LexNextToken;   // GCC isn't tail call eliminating.
   }

Modified: cfe/cfe/trunk/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Pragma.cpp?rev=38701&r1=38700&r2=38701&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Pragma.cpp (original)
+++ cfe/cfe/trunk/Lex/Pragma.cpp Wed Jul 11 11:23:56 2007
@@ -184,16 +184,16 @@
 ///
 void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) {
   LexerToken Tok;
-  assert(!SkippingContents && "Why are we handling pragmas while skipping?");
+  assert(!isSkipping() && "Why are we handling pragmas while skipping?");
   while (1) {
     // Read the next token to poison.  While doing this, pretend that we are
     // skipping while reading the identifier to poison.
     // This avoids errors on code like:
     //   #pragma GCC poison X
     //   #pragma GCC poison X
-    SkippingContents = true;
+    if (CurLexer) CurLexer->LexingRawMode = true;
     LexUnexpandedToken(Tok);
-    SkippingContents = false;
+    if (CurLexer) CurLexer->LexingRawMode = false;
     
     // If we reached the end of line, we're done.
     if (Tok.getKind() == tok::eom) return;

Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38701&r1=38700&r2=38701&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:56 2007
@@ -57,7 +57,6 @@
     
   // Macro expansion is enabled.
   DisableMacroExpansion = false;
-  SkippingContents = false;
   InMacroFormalArgs = false;
 
   // There is no file-change handler yet.
@@ -512,12 +511,13 @@
 /// else and 2 if there are no more tokens in the buffer controlled by the
 /// lexer.
 unsigned Preprocessor::isNextPPTokenLParen(Lexer *L) {
-  assert(!isSkipping() && "How can we expand a macro from a skipping buffer?");
+  assert(!L->LexingRawMode &&
+         "How can we expand a macro from a skipping buffer?");
   
   // Set the lexer to 'skipping' mode.  This will ensure that we can lex a token
   // without emitting diagnostics, disables macro expansion, and will cause EOF
   // to return an EOF token instead of popping the include stack.
-  SkippingContents = true;
+  L->LexingRawMode = true;
   
   // Save state that can be changed while lexing so that we can restore it.
   const char *BufferPtr = L->BufferPtr;
@@ -530,7 +530,7 @@
   L->BufferPtr = BufferPtr;
   
   // Restore the lexer back to non-skipping mode.
-  SkippingContents = false;
+  L->LexingRawMode = false;
   
   if (Tok.getKind() == tok::eof)
     return 2;
@@ -1172,16 +1172,9 @@
   CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
                                  FoundNonSkipPortion, FoundElse);
   
-  // Know that we are going to be skipping tokens.  Set this flag to indicate
-  // this, which has a couple of effects:
-  //  1. If EOF of the current lexer is found, the include stack isn't popped.
-  //  2. Identifier information is not looked up for identifier tokens.  As an
-  //     effect of this, implicit macro expansion is naturally disabled.
-  //  3. "#" tokens at the start of a line are treated as normal tokens, not
-  //     implicitly transformed by the lexer.
-  //  4. All notes, warnings, and extension messages are disabled.
-  //
-  SkippingContents = true;
+  // Enter raw mode to disable identifier lookup (and thus macro expansion),
+  // disabling warnings, etc.
+  CurLexer->LexingRawMode = true;
   LexerToken Tok;
   while (1) {
     CurLexer->Lex(Tok);
@@ -1305,13 +1298,13 @@
           DiscardUntilEndOfDirective();
           ShouldEnter = false;
         } else {
-          // Restore the value of SkippingContents so that identifiers are
+          // Restore the value of LexingRawMode so that identifiers are
           // looked up, etc, inside the #elif expression.
-          assert(SkippingContents && "We have to be skipping here!");
-          SkippingContents = false;
+          assert(CurLexer->LexingRawMode && "We have to be skipping here!");
+          CurLexer->LexingRawMode = false;
           IdentifierInfo *IfNDefMacro = 0;
           ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
-          SkippingContents = true;
+          CurLexer->LexingRawMode = true;
         }
         
         // If this is a #elif with a #else before it, report the error.
@@ -1331,7 +1324,7 @@
   // Finally, if we are out of the conditional (saw an #endif or ran off the end
   // of the file, just stop skipping and return to lexing whatever came after
   // the #if block.
-  SkippingContents = false;
+  CurLexer->LexingRawMode = false;
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/cfe/trunk/include/clang/Lex/Lexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Lexer.h?rev=38701&r1=38700&r2=38701&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Lexer.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Lexer.h Wed Jul 11 11:23:56 2007
@@ -52,7 +52,8 @@
 /// or buffering/seeking of tokens, only forward lexing is supported.  It relies
 /// on the specified Preprocessor object to handle preprocessor directives, etc.
 class Lexer {
-  const char *BufferPtr;         // Current pointer into the buffer.
+  //===--------------------------------------------------------------------===//
+  // Constant configuration values for this lexer.
   const char * const BufferEnd;  // End of the buffer.
   const SourceBuffer *InputFile; // The file we are reading from.
   unsigned CurFileID;            // FileID for the current input file.
@@ -61,14 +62,40 @@
   bool Is_PragmaLexer;           // True if lexer for _Pragma handling.
   bool IsMainFile;               // True if top-level file.
   
-  // Context-specific lexing flags.
-  bool ParsingPreprocessorDirective; // True if parsing #XXX
-  bool ParsingFilename;          // True after #include: turn <xx> into string.
+  //===--------------------------------------------------------------------===//
+  // Context-specific lexing flags set by the preprocessor.
+  //
   
+  /// ParsingPreprocessorDirective - This is true when parsing #XXX.  This turns
+  /// '\n' into a tok::eom token.
+  bool ParsingPreprocessorDirective;
+  
+  /// ParsingFilename - True after #include: this turns <xx> into a
+  /// tok::angle_string_literal token.
+  bool ParsingFilename;
+  
+  /// LexingRawMode - True if in raw mode:  This flag disables interpretation of
+  /// tokens and is a far faster mode to lex in than non-raw-mode.  This flag:
+  ///  1. If EOF of the current lexer is found, the include stack isn't popped.
+  ///  2. Identifier information is not looked up for identifier tokens.  As an
+  ///     effect of this, implicit macro expansion is naturally disabled.
+  ///  3. "#" tokens at the start of a line are treated as normal tokens, not
+  ///     implicitly transformed by the lexer.
+  ///  4. All notes, warnings, and extension messages are disabled.
+  ///
+  bool LexingRawMode;
+  
+  //===--------------------------------------------------------------------===//
   // Context that changes as the file is lexed.  NOTE: any state that mutates as
   // the file is lexed should be added to Preprocessor::isNextPPTokenLParen.
 
-  bool IsAtStartOfLine;          // True if sitting at start of line.
+  // BufferPtr - Current pointer into the buffer.  This is the next character
+  // to be lexed.
+  const char *BufferPtr;
+
+  // IsAtStartOfLine - True if the next lexed token should get the "start of
+  // line" flag set on it.
+  bool IsAtStartOfLine;
 
   /// MIOpt - This is a state machine that detects the #ifndef-wrapping a file 
   /// idiom for the multiple-include optimization.

Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38701&r1=38700&r2=38701&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:23:56 2007
@@ -129,7 +129,6 @@
 
   // State that changes while the preprocessor runs:
   bool DisableMacroExpansion;    // True if macro expansion is disabled.
-  bool SkippingContents;         // True if in a #if 0 block.
   bool InMacroFormalArgs;        // True if parsing fn macro invocation args.
 
   /// Identifiers - This is mapping/lookup information for all identifiers in
@@ -217,7 +216,7 @@
 
   /// isSkipping - Return true if we're lexing a '#if 0' block.  This causes
   /// lexer errors/warnings to get ignored.
-  bool isSkipping() const { return SkippingContents; }
+  bool isSkipping() const { return CurLexer && CurLexer->LexingRawMode; }
   
   /// isCurrentLexer - Return true if we are lexing directly from the specified
   /// lexer.
@@ -267,7 +266,7 @@
   IdentifierInfo *getIdentifierInfo(const char *NameStart,
                                     const char *NameEnd) {
     // If we are in a "#if 0" block, don't bother lookup up identifiers.
-    if (SkippingContents) return 0;
+    if (isSkipping()) return 0;
     return &Identifiers.get(NameStart, NameEnd);
   }
   IdentifierInfo *getIdentifierInfo(const char *NameStr) {





More information about the cfe-commits mailing list