[cfe-commits] r39209 - in /cfe/cfe/trunk: Driver/PrintPreprocessedOutput.cpp Lex/Lexer.cpp Lex/Preprocessor.cpp include/clang/Basic/LangOptions.h include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:41:14 PDT 2007


Author: sabre
Date: Wed Jul 11 11:41:14 2007
New Revision: 39209

URL: http://llvm.org/viewvc/llvm-project?rev=39209&view=rev
Log:
Change KeepComments/KeepMacroComments modes to be facets of the preprocessor
state, not aspects of the language standard being parsed.

Modified:
    cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp
    cfe/cfe/trunk/Lex/Lexer.cpp
    cfe/cfe/trunk/Lex/Preprocessor.cpp
    cfe/cfe/trunk/include/clang/Basic/LangOptions.h
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

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

==============================================================================
--- cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp (original)
+++ cfe/cfe/trunk/Driver/PrintPreprocessedOutput.cpp Wed Jul 11 11:41:14 2007
@@ -387,11 +387,9 @@
 ///
 void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
                                      LangOptions &Options) {
-  // FIXME: should not mutate LangOptions!!
-  if (EnableCommentOutput)          // -C specified?
-    Options.KeepComments = 1;
-  if (EnableMacroCommentOutput)     // -CC specified?
-    Options.KeepComments = Options.KeepMacroComments = 1;
+  // Inform the preprocessor whether we want it to retain comments or not, due
+  // to -C or -CC.
+  PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
   
   InitOutputBuffer();
   

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

==============================================================================
--- cfe/cfe/trunk/Lex/Lexer.cpp (original)
+++ cfe/cfe/trunk/Lex/Lexer.cpp Wed Jul 11 11:41:14 2007
@@ -67,7 +67,7 @@
   LexingRawMode = false;
   
   // Default to keeping comments if requested.
-  KeepCommentMode = Features.KeepComments;
+  KeepCommentMode = PP.getCommentRetentionState();
 }
 
 /// Stringify - Convert the specified string into a C string, with surrounding
@@ -970,7 +970,7 @@
     FormTokenWithChars(Result, CurPtr);
     
     // Restore comment saving mode, in case it was disabled for directive.
-    KeepCommentMode = Features.KeepComments;
+    KeepCommentMode = PP.getCommentRetentionState();
     return true;  // Have a token.
   }        
 
@@ -1087,7 +1087,7 @@
       ParsingPreprocessorDirective = false;
       
       // Restore comment saving mode, in case it was disabled for directive.
-      KeepCommentMode = Features.KeepComments;
+      KeepCommentMode = PP.getCommentRetentionState();
       
       // Since we consumed a newline, we are back at the start of a line.
       IsAtStartOfLine = true;

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:41:14 2007
@@ -58,7 +58,11 @@
   NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
   MaxIncludeStackDepth = 0; 
   NumSkipped = 0;
-    
+
+  // Default to discarding comments.
+  KeepComments = false;
+  KeepMacroComments = false;
+  
   // Macro expansion is enabled.
   DisableMacroExpansion = false;
   InMacroArgs = false;
@@ -686,7 +690,7 @@
           return 0;
         }
         // Otherwise, continue to add the tokens to this variable argument.
-      } else if (Tok.getKind() == tok::comment && !Features.KeepMacroComments) {
+      } else if (Tok.getKind() == tok::comment && !KeepMacroComments) {
         // If this is a comment token in the argument list and we're just in
         // -C mode (not -CC mode), discard the comment.
         continue;
@@ -1175,7 +1179,7 @@
     if (Tok.getKind() != tok::identifier) {
       CurLexer->ParsingPreprocessorDirective = false;
       // Restore comment saving mode.
-      CurLexer->KeepCommentMode = Features.KeepComments;
+      CurLexer->KeepCommentMode = KeepComments;
       continue;
     }
 
@@ -1190,7 +1194,7 @@
         FirstChar != 'i' && FirstChar != 'e') {
       CurLexer->ParsingPreprocessorDirective = false;
       // Restore comment saving mode.
-      CurLexer->KeepCommentMode = Features.KeepComments;
+      CurLexer->KeepCommentMode = KeepComments;
       continue;
     }
     
@@ -1211,7 +1215,7 @@
       if (IdLen >= 20) {
         CurLexer->ParsingPreprocessorDirective = false;
         // Restore comment saving mode.
-        CurLexer->KeepCommentMode = Features.KeepComments;
+        CurLexer->KeepCommentMode = KeepComments;
         continue;
       }
       memcpy(Directive, &DirectiveStr[0], IdLen);
@@ -1292,7 +1296,7 @@
     
     CurLexer->ParsingPreprocessorDirective = false;
     // Restore comment saving mode.
-    CurLexer->KeepCommentMode = Features.KeepComments;
+    CurLexer->KeepCommentMode = KeepComments;
   }
 
   // Finally, if we are out of the conditional (saw an #endif or ran off the end
@@ -1710,7 +1714,7 @@
   
   // If we are supposed to keep comments in #defines, reenable comment saving
   // mode.
-  CurLexer->KeepCommentMode = Features.KeepMacroComments;
+  CurLexer->KeepCommentMode = KeepMacroComments;
   
   // Create the new macro.
   MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());

Modified: cfe/cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/LangOptions.h?rev=39209&r1=39208&r2=39209&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/LangOptions.h Wed Jul 11 11:41:14 2007
@@ -33,13 +33,9 @@
   unsigned ObjC1             : 1;  // Objective C 1 support enabled.
   unsigned ObjC2             : 1;  // Objective C 2 support enabled.
   
-  unsigned KeepComments      : 1;  // Keep comments ("-C") mode.
-  unsigned KeepMacroComments : 1;  // Keep macro-exp comments ("-CC") mode.
-  
   LangOptions() {
     Trigraphs = BCPLComment = DollarIdents = Digraphs = ObjC1 = ObjC2 = 0;
     C99 = Microsoft = CPlusPlus = NoExtensions = 0;
-    KeepComments = KeepMacroComments = 0;
   }
 };
 

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=39209&r1=39208&r2=39209&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:41:14 2007
@@ -62,6 +62,10 @@
     MaxAllowedIncludeStackDepth = 200
   };
 
+  // State that is set before the preprocessor begins.
+  bool KeepComments : 1;
+  bool KeepMacroComments : 1;
+  
   // State that changes while the preprocessor runs:
   bool DisableMacroExpansion : 1;  // True if macro expansion is disabled.
   bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
@@ -125,6 +129,15 @@
 
   IdentifierTable &getIdentifierTable() { return Identifiers; }
 
+  /// SetCommentRetentionState - Control whether or not the preprocessor retains
+  /// comments in output.
+  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
+    this->KeepComments = KeepComments | KeepMacroComments;
+    this->KeepMacroComments = KeepMacroComments;
+  }
+  
+  bool getCommentRetentionState() const { return KeepComments; }
+  
   /// isCurrentLexer - Return true if we are lexing directly from the specified
   /// lexer.
   bool isCurrentLexer(const Lexer *L) const {





More information about the cfe-commits mailing list