<div dir="ltr">On Tue, Mar 26, 2013 at 6:25 PM, Argyrios Kyrtzidis <span dir="ltr"><<a href="mailto:akyrtzi@gmail.com" target="_blank">akyrtzi@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: akirtzidis<br>
Date: Tue Mar 26 20:25:19 2013<br>
New Revision: 178105<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=178105&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=178105&view=rev</a><br>
Log:<br>
[modules] Re-enable the "ambiguous expansion of macro" warning.<br>
<br>
Also update "test/Modules/macros.c" to test modified semantics:<br>
-When there is an ambiguous macro, expand using the latest introduced version, not the first one.<br>
-#undefs in submodules cause the macro to not be exported by that submodule, it doesn't cause<br>
 undefining of macros in the translation unit that imported that submodule.<br>
 This reduces macro namespace interference across modules.<br>
<br>
Modified:<br>
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp<br>
    cfe/trunk/test/Modules/macros.c<br>
<br>
Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=178105&r1=178104&r2=178105&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=178105&r1=178104&r2=178105&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)<br>
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Tue Mar 26 20:25:19 2013<br>
@@ -211,7 +211,9 @@ bool Preprocessor::isNextPPTokenLParen()<br>
 /// expanded as a macro, handle it and return the next token as 'Identifier'.<br>
 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,<br>
                                                  MacroDirective *MD) {<br>
-  MacroInfo *MI = MD->getMacroInfo();<br>
+  MacroDirective::DefInfo Def = MD->getDefinition();<br>
+  assert(Def.isValid());<br>
+  MacroInfo *MI = Def.getMacroInfo();<br>
<br>
   // If this is a macro expansion in the "#if !defined(x)" line for the file,<br>
   // then the macro could expand to different things in other contexts, we need<br>
@@ -286,25 +288,22 @@ bool Preprocessor::HandleMacroExpandedId<br>
     }<br>
   }<br>
<br>
-  // FIXME: Temporarily disable this warning that is currently bogus with a PCH<br>
-  // that redefined a macro without undef'ing it first (test/PCH/macro-redef.c).<br>
-#if 0<br>
   // If the macro definition is ambiguous, complain.<br>
-  if (MI->isAmbiguous()) {<br>
+  if (Def.getDirective()->isAmbiguous()) {<br>
     Diag(Identifier, diag::warn_pp_ambiguous_macro)<br>
       << Identifier.getIdentifierInfo();<br>
     Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)<br>
       << Identifier.getIdentifierInfo();<br>
-    for (MacroInfo *PrevMI = MI->getPreviousDefinition();<br>
-         PrevMI && PrevMI->isDefined();<br>
-         PrevMI = PrevMI->getPreviousDefinition()) {<br>
-      if (PrevMI->isAmbiguous()) {<br>
-        Diag(PrevMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)<br>
+    for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();<br>
+         PrevDef && !PrevDef.isUndefined();<br>
+         PrevDef = PrevDef.getPreviousDefinition()) {<br>
+      if (PrevDef.getDirective()->isAmbiguous()) {<br>
+        Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),<br>
+             diag::note_pp_ambiguous_macro_other)<br>
           << Identifier.getIdentifierInfo();<br>
       }<br>
     }<br>
   }<br>
-#endif<br>
<br>
   // If we started lexing a macro, enter the macro expansion body.<br>
<br>
<br>
Modified: cfe/trunk/test/Modules/macros.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/macros.c?rev=178105&r1=178104&r2=178105&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/macros.c?rev=178105&r1=178104&r2=178105&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/test/Modules/macros.c (original)<br>
+++ cfe/trunk/test/Modules/macros.c Tue Mar 26 20:25:19 2013<br>
@@ -1,4 +1,3 @@<br>
-// XFAIL: *<br>
 // RUN: rm -rf %t<br>
 // RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros_top %S/Inputs/module.map<br>
 // RUN: %clang_cc1 -fmodules -x objective-c -emit-module -fmodules-cache-path=%t -fmodule-name=macros_left %S/Inputs/module.map<br>
@@ -10,12 +9,12 @@<br>
 // These notes come from headers in modules, and are bogus.<br>
<br>
 // FIXME: expected-note{{previous definition is here}}<br>
+// FIXME: expected-note{{previous definition is here}} expected-note{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}}<br>
+// expected-note{{other definition of 'TOP_RIGHT_REDEF'}} expected-note{{expanding this definition of 'LEFT_RIGHT_DIFFERENT2'}}<br>
 // expected-note{{other definition of 'LEFT_RIGHT_DIFFERENT'}}<br>
-// expected-note{{expanding this definition of 'TOP_RIGHT_REDEF'}}<br>
-// FIXME: expected-note{{previous definition is here}} \<br>
-// expected-note{{expanding this definition of 'LEFT_RIGHT_DIFFERENT'}}<br>
<br>
-// expected-note{{other definition of 'TOP_RIGHT_REDEF'}}<br>
+<br>
+// expected-note{{expanding this definition of 'TOP_RIGHT_REDEF'}}<br>
<br>
 @import macros;<br>
<br>
@@ -80,8 +79,8 @@ void f() {<br>
 #  error TOP should be visible<br>
 #endif<br>
<br>
-#ifdef TOP_LEFT_UNDEF<br>
-#  error TOP_LEFT_UNDEF should not be visible<br>
+#ifndef TOP_LEFT_UNDEF<br>
+#  error TOP_LEFT_UNDEF should still be defined<br></blockquote><div><br></div><div>This seems completely broken to me, and it breaks libc++.</div><div><br></div><div>glibc has something like:</div><div><br></div><div>  // stdio.h<br>
</div><div>  int getc(FILE*);</div><div><div>  #define foo_getc(x) /* ... */</div></div><div>  #define getc(x) foo_getc(x)<br></div><div><br></div><div>libc++ has:</div><div><br></div><div>  // cstdio</div><div>  #include <stdio.h></div>
<div><br></div><div>  #ifdef getc</div><div>  #undef getc<br></div><div>  #endif<br></div><div><br></div><div>  namespace std { using ::getc; }</div><div><br></div><div>This results in any program which includes <cstdio> being unable to use std::getc:</div>
<div><br></div><div>  // main.cc</div><div>  #include <cstdio></div><div>  int k = std::getc(stdin); // error, no foo_getc in namespace std</div><div><br></div><div>Can you explain a bit about the effect you were trying to achieve here? Here's another case:</div>
<div><br></div><div><div>  #include <stdio.h></div><div>  #include <something></div><div>  int k = getc(stdin); // still uses the macro from <stdio.h></div></div><div><br></div><div>I think we might want to ignore an #undef in <something> if <something> comes from a module that doesn't depend on the module containing <stdio.h>. (If it's just using the macro for its own internal purposes, or more generally if rearranging the order in which we chose to load the modules would result in a consistent macro definition chain.)</div>
<div><br></div><div>I think the behavior we want here is:</div><div><br></div><div>* If a module M1 depends on a module M2, macro definitions/undefs in M1 and M2 are never ambiguous (M1 is always ordered after M2).</div><div>
* If there are multiple ambiguous 'most recent' definitions of a macro (excluding chains that end in an undef), and the macro is used, warn.</div><div><br></div><div>Put another way: consider the subgraph of the imported portion of the module dependency graph in which the macro is or was defined. All roots of that subgraph for which the macro is defined must define it to the same value, or we warn on ambiguity. If there are any such roots, the value of the macro is the value at such a root. Otherwise, the macro is not defined.</div>
<div><br></div><div>Does that sound right to you?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

 #endif<br>
<br>
 void test1() {<br>
@@ -89,7 +88,8 @@ void test1() {<br>
   TOP_RIGHT_REDEF *ip = &i;<br>
 }<br>
<br>
-#define LEFT_RIGHT_DIFFERENT2 double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT2' macro redefined}}<br>
+#define LEFT_RIGHT_DIFFERENT2 double // FIXME: expected-warning{{'LEFT_RIGHT_DIFFERENT2' macro redefined}} \<br>
+                                     // expected-note{{other definition of 'LEFT_RIGHT_DIFFERENT2'}}<br>
<br>
 // Import right module (which also imports top)<br>
 @import macros_right;<br>
@@ -112,11 +112,11 @@ void test2() {<br>
   int i;<br>
   float f;<br>
   double d;<br>
-  TOP_RIGHT_REDEF *ip = &i; // expected-warning{{ambiguous expansion of macro 'TOP_RIGHT_REDEF'}}<br>
+  TOP_RIGHT_REDEF *fp = &f; // expected-warning{{ambiguous expansion of macro 'TOP_RIGHT_REDEF'}}<br>
<br>
-  LEFT_RIGHT_IDENTICAL *ip2 = &i;<br>
-  LEFT_RIGHT_DIFFERENT *fp = &f; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT'}}<br>
-  LEFT_RIGHT_DIFFERENT2 *dp = &d;<br>
+  LEFT_RIGHT_IDENTICAL *ip = &i;<br>
+  LEFT_RIGHT_DIFFERENT *ip2 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT'}}<br>
+  LEFT_RIGHT_DIFFERENT2 *ip3 = &i; // expected-warning{{ambiguous expansion of macro 'LEFT_RIGHT_DIFFERENT2}}<br>
   int LEFT_RIGHT_DIFFERENT3;<br>
 }<br>
<br>
@@ -133,6 +133,6 @@ void test3() {<br>
<br>
 @import macros_right.undef;<br>
<br>
-#ifdef TOP_RIGHT_UNDEF<br>
-# error TOP_RIGHT_UNDEF should not be defined<br>
+#ifndef TOP_RIGHT_UNDEF<br>
+# error TOP_RIGHT_UNDEF should still be defined<br>
 #endif<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>