[cfe-commits] r38760 - in /cfe/cfe/trunk: Lex/Preprocessor.cpp include/clang/Lex/LexerToken.h

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


Author: sabre
Date: Wed Jul 11 11:24:24 2007
New Revision: 38760

URL: http://llvm.org/viewvc/llvm-project?rev=38760&view=rev
Log:
Implement C99 6.10.3.4p2, testcase here: Preprocessor/macro_disable3.c.

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

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

==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:24:24 2007
@@ -147,6 +147,8 @@
     std::cerr << " [StartOfLine]";
   if (Tok.hasLeadingSpace())
     std::cerr << " [LeadingSpace]";
+  if (Tok.isExpandDisabled())
+    std::cerr << " [ExpandDisabled]";
   if (Tok.needsCleaning()) {
     const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
     std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
@@ -675,6 +677,11 @@
       SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
     Identifier.SetLocation(Loc);
     
+    // If this is #define X X, we must mark the result as unexpandible.
+    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
+      if (NewII->getMacroInfo() == MI)
+        Identifier.SetFlag(LexerToken::DisableExpand);
+    
     // Since this is not an identifier token, it can't be macro expanded, so
     // we're done.
     ++NumFastMacroExpanded;
@@ -998,9 +1005,17 @@
   
   // If this is a macro to be expanded, do it.
   if (MacroInfo *MI = II.getMacroInfo())
-    if (MI->isEnabled() && !DisableMacroExpansion)
-      if (!HandleMacroExpandedIdentifier(Identifier, MI))
-        return;
+    if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
+      if (MI->isEnabled()) {
+        if (!HandleMacroExpandedIdentifier(Identifier, MI))
+          return;
+      } else {
+        // C99 6.10.3.4p2 says that a disabled macro may never again be
+        // expanded, even if it's in a context where it could be expanded in the
+        // future.
+        Identifier.SetFlag(LexerToken::DisableExpand);
+      }
+    }
 
   // Change the kind of this identifier to the appropriate token kind, e.g.
   // turning "for" into a keyword.

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/LexerToken.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/LexerToken.h Wed Jul 11 11:24:24 2007
@@ -47,7 +47,8 @@
   enum TokenFlags {
     StartOfLine   = 0x01,  // At start of line or only after whitespace.
     LeadingSpace  = 0x02,  // Whitespace exists before this token.
-    NeedsCleaning = 0x04   // Contained an escaped newline or trigraph.
+    DisableExpand = 0x04,  // This identifier may never be macro expanded.
+    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
   };
 
   tok::TokenKind getKind() const { return Kind; }
@@ -100,6 +101,10 @@
   ///
   bool hasLeadingSpace() const { return Flags & LeadingSpace; }
   
+  /// isExpandDisabled - Return true if this identifier token should never
+  /// be expanded in the future, due to C99 6.10.3.4p2.
+  bool isExpandDisabled() const { return Flags & DisableExpand; }
+    
   /// needsCleaning - Return true if this token has trigraphs or escaped
   /// newlines in it.
   ///





More information about the cfe-commits mailing list