<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Hey Jordan,</div><div class="gmail_quote"><br></div><div class="gmail_quote">I wrote up an answer, then realised what you're probably seeing is what happens when -fmodules-local-submodule-visibility is turned off. In that mode, macros from, say, ConflictingA.h will leak into ConflictingB.h (as if ConflictingB.h started by performing a private import of ConflictingA.h), so ConflictingB.h's macro *does* override ConflictingA.h's macro, and from clang's perspective the Conflicting and Redefined modules would then have the same behavior -- if you import A and B, you get the macro from B.</div><div class="gmail_quote"><br></div><div class="gmail_quote">So then I think the question is, what is the difference between the two cases that you want to detect? (The #undef? The import of A into B prior to the redefinition? Something else?) I think we retain sufficient information to do this, but it's not necessarily convenient, and some of it isn't ever read from the PCM file currently.</div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">If you <i>are</i> using -fmodules-local-submodule-visibility, then:<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On 6 October 2017 at 13:28, Jordan Rose via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Hi, Richard. Jordan's back with another annoying ModuleMacro question for Swift. Let's say I have these two modules:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>module Conflicting {</div><div>  explicit module A { header "ConflictingA.h" }</div><div>  explicit module B { header "ConflictingB.h" }</div><div>}</div><div><br></div><div>module Redefined {</div><div>  module A { header "RedefinedA.h" }</div><div>  module B { header "RedefinedB.h" }</div><div>}</div></blockquote><div><br></div><div>Both headers in 'Conflicting' define a macro CONFLICTING, but with different values; a client is only supposed to import one of them. 'Redefined' is a little different: RedefinedB.h includes RedefinedA.h before defining the new value, and the client is probably going to import the entire top-level module. Let's say RedefinedB.h is even polite enough to use #undef.*</div><div><br></div><div>* If these examples sound bad, well, I agree, but the former is what the OpenGL framework on macOS has been doing for years, and the latter is how Clang's limits.h deals with a system limits.h.</div><div><br></div><div>The question: using ModuleMacro, <b>how can I distinguish these two cases</b> while building a PCM?</div></div></blockquote><div><br></div><div>When you say "while building a PCM", do you mean before you get to the end of one of the headers / submodules, or after? Depending on when you ask, you'd need to look at either the set of overridden module macros in the preprocessor (while we're still lexing within the overriding module, before we create a ModuleMacro) or the list of overridden module macros on the ModuleMacro (after it's created).</div><div><br></div><div>We build a chain of MacroDirectives while we parse, and then convert them to ModuleMacros at the end of each submodule. So, taking the example of Redefined.B:</div><div><br></div><div>// start of RedefinedB.h, preprocessor's MacroState for CONFLICTING will be that the ModuleMacro from Redefined.A is active and there is no MacroDirective</div><div>#undef CONFLICTING</div><div>// now the MacroState will have Redefined.A overridden, with the UndefMacroDirective being the current MacroDirective</div><div>#define CONFLICTING something_else</div><div>// now the MacroState will have Redefined.A overridden, with the DefMacroDirective being the current MacroDirective</div><div><br></div><div>At the end of Redefined.A, we convert the DefMacroDirective to a ModuleMacro for Redefined.B that overrides Redefined.A's macro.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div>The obvious correct Clang answer is "don't bother, visibility will handle everything when the modules get imported", but unfortunately that doesn't fly for Swift, because of this third example:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>module CrossModuleRedefinedCore {</div><div>  header "CrossModuleRedefinedCore.h"</div><div>}</div><div>module CrossModuleRedefined {</div><div>  // imports CrossModuleRedefinedCore</div><div>  header "CrossModuleRedefined.h"</div><div>}</div></blockquote><br><div>In Swift, I'm allowed to say `CrossModuleRedefinedCore.<wbr>REDEFINED` to get the old value, and `CrossModuleRedefined.<wbr>REDEFINED` to get the new value. I'm not <i>hugely</i> concerned about this use case if I can get the other two to work well, but I haven't given up on it yet.</div><div><br></div><div>Any ideas? I already tried looking at the overrides, but that seems like it's represented the same in both cases.</div></div></blockquote><div><br></div><div>That sounds wrong. Can you write a file that imports the modules and then does:</div><div><br></div><div>#pragma clang __debug macro REDEFINED</div></div></div></div>