[cfe-commits] r91266 - in /cfe/trunk: lib/Lex/PPExpressions.cpp test/Preprocessor/macro_fn_disable_expand.c

Chris Lattner sabre at nondot.org
Sun Dec 13 21:00:18 PST 2009


Author: lattner
Date: Sun Dec 13 23:00:18 2009
New Revision: 91266

URL: http://llvm.org/viewvc/llvm-project?rev=91266&view=rev
Log:
fix rdar://7466570 - Be more bug compatible with GCC when it comes to 
expanding directives withing macro expansions.  This is undefined behavior
according to 6.10.3p11, so we might as well be undefined in ways similar to
GCC.

Modified:
    cfe/trunk/lib/Lex/PPExpressions.cpp
    cfe/trunk/test/Preprocessor/macro_fn_disable_expand.c

Modified: cfe/trunk/lib/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPExpressions.cpp?rev=91266&r1=91265&r2=91266&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPExpressions.cpp (original)
+++ cfe/trunk/lib/Lex/PPExpressions.cpp Sun Dec 13 23:00:18 2009
@@ -72,8 +72,8 @@
 };
 
 /// EvaluateDefined - Process a 'defined(sym)' expression.
-static bool EvaluateDefined(PPValue &Result, Token &PeekTok,
-        DefinedTracker &DT, bool ValueLive, Preprocessor &PP) {
+static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
+                            bool ValueLive, Preprocessor &PP) {
   IdentifierInfo *II;
   Result.setBegin(PeekTok.getLocation());
 
@@ -676,6 +676,15 @@
 /// to "!defined(X)" return X in IfNDefMacro.
 bool Preprocessor::
 EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
+  // Save the current state of 'DisableMacroExpansion' and reset it to false. If
+  // 'DisableMacroExpansion' is true, then we must be in a macro argument list
+  // in which case a directive is undefined behavior.  We want macros to be able
+  // to recursively expand in order to get more gcc-list behavior, so we force
+  // DisableMacroExpansion to false and restore it when we're done parsing the
+  // expression.
+  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
+  DisableMacroExpansion = false;
+  
   // Peek ahead one token.
   Token Tok;
   Lex(Tok);
@@ -689,6 +698,9 @@
     // Parse error, skip the rest of the macro line.
     if (Tok.isNot(tok::eom))
       DiscardUntilEndOfDirective();
+    
+    // Restore 'DisableMacroExpansion'.
+    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
     return false;
   }
 
@@ -701,6 +713,8 @@
     if (DT.State == DefinedTracker::NotDefinedMacro)
       IfNDefMacro = DT.TheMacro;
 
+    // Restore 'DisableMacroExpansion'.
+    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
     return ResVal.Val != 0;
   }
 
@@ -711,6 +725,9 @@
     // Parse error, skip the rest of the macro line.
     if (Tok.isNot(tok::eom))
       DiscardUntilEndOfDirective();
+    
+    // Restore 'DisableMacroExpansion'.
+    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
     return false;
   }
 
@@ -721,6 +738,8 @@
     DiscardUntilEndOfDirective();
   }
 
+  // Restore 'DisableMacroExpansion'.
+  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
   return ResVal.Val != 0;
 }
 

Modified: cfe/trunk/test/Preprocessor/macro_fn_disable_expand.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/macro_fn_disable_expand.c?rev=91266&r1=91265&r2=91266&view=diff

==============================================================================
--- cfe/trunk/test/Preprocessor/macro_fn_disable_expand.c (original)
+++ cfe/trunk/test/Preprocessor/macro_fn_disable_expand.c Sun Dec 13 23:00:18 2009
@@ -8,3 +8,23 @@
 #define w ABCD
 m(m)
 // CHECK: m(ABCD)
+
+
+
+// rdar://7466570
+
+// We should get '42' in the argument list for gcc compatibility.
+#define A 1
+#define B 2
+#define C(x) (x + 1)
+
+X: C(
+#ifdef A
+#if A == 1
+#if B
+    42
+#endif
+#endif
+#endif
+    )
+// CHECK: X: (42 + 1)





More information about the cfe-commits mailing list