<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 6 October 2017 at 15:23, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><span class=""><blockquote type="cite"><div>On Oct 6, 2017, at 15:06, Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:</div><br class="m_3319083616672955788Apple-interchange-newline"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><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-<wbr>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></div></div></blockquote><div><br></div></span><div>Hm, interesting. Yes, we're not using -fmodules-local-submodule-<wbr>visibility; apart from Apple's SDK not yet being vetted with this option, it seems to break simple things like</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>module X {</div></div><div><div>  header "A.h"</div></div><div><div>  header "B.h"</div><div>  header "C.h"</div><div>  export *</div></div><div><div>}</div></div></blockquote><div><br></div><div>with declarations from B.h not being visible to Swift at all. I suspect we'll have to rework our lookup logic in some way to do whatever's needed for this mode, but it's not an option for us at the moment.</div><div><br></div><div>I think the import of A into B would be a fair thing to look for. Another alternative that's not 100% correct but is likely to work in practice is to see if the macros are in different explicit submodules.</div><div><br></div><div>Any suggestions?</div></div></blockquote><div><br></div><div>You can query whether B is imported into A with Module::isModuleVisible. That doesn't give you the import location, but might be good enough if you don't care whether the import was before the redefinition of the macro.</div><div><br></div><div>You could extend the ASTReader to (conditionally) load the macro definition chains for identifies even when the AST file is for a module. Once you have the chain, you could traverse it to see if the prior macro was #undef'd before the new one was #defined, if you want to base the outcome on that.</div><div><br></div><div>I have some other ideas below, but I don't think I have a good enough idea of what behavior you want in the various cases to give you particularly useful advice.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>Or is no-local-submodule-visibility just on its way out, and not something you're interested in spending time on?</div></div></blockquote><div><br></div><div>I don't think that mode has a long-term future, and I'd encourage you to find a way off it, but I expect it to stick around and keep working until you do :)</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>Thanks, this has already been helpful.</div><span class="HOEnZb"><font color="#888888"><div>Jordan</div></font></span><div><div class="h5"><div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">If you<span class="m_3319083616672955788Apple-converted-space"> </span><i>are</i><span class="m_3319083616672955788Apple-converted-space"> </span>using -fmodules-local-submodule-<wbr>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 class="m_3319083616672955788Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.<wbr>org</a>></span><span class="m_3319083616672955788Apple-converted-space"> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color: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> <span class="m_3319083616672955788Apple-converted-space"> </span>explicit module A { header "ConflictingA.h" }</div><div> <span class="m_3319083616672955788Apple-converted-space"> </span>explicit module B { header "ConflictingB.h" }</div><div>}</div><div><br></div><div>module Redefined {</div><div> <span class="m_3319083616672955788Apple-converted-space"> </span>module A { header "RedefinedA.h" }</div><div> <span class="m_3319083616672955788Apple-converted-space"> </span>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,<span class="m_3319083616672955788Apple-converted-space"> </span><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-width:1px;border-left-style:solid;border-left-color: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> <span class="m_3319083616672955788Apple-converted-space"> </span>header "CrossModuleRedefinedCore.h"</div><div>}</div><div>module CrossModuleRedefined {</div><div> <span class="m_3319083616672955788Apple-converted-space"> </span>// imports CrossModuleRedefinedCore</div><div> <span class="m_3319083616672955788Apple-converted-space"> </span>header "CrossModuleRedefined.h"</div><div>}</div></blockquote><br><div>In Swift, I'm allowed to say `CrossModuleRedefinedCore.REDE<wbr>FINED` to get the old value, and `CrossModuleRedefined.REDEFINE<wbr>D` to get the new value. I'm not<span class="m_3319083616672955788Apple-converted-space"> </span><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></blockquote></div></div></div></div></blockquote></div></div></div></div></blockquote><div><br></div><div>What should happen if CrossModuleRedefined didn't redefine the macro, assuming CrossModuleRedefined includes an "export *"? Would CrossModuleRedefined.REDEFINED be an error or would it refer to the macro from CrossModuleRedefinedCore?</div><div><br></div><div>(In the former case, you could search the ModuleMacro graph for REDEFINED, looking for a ModuleMacro definition in the nominated module. The latter case seems trickier, but you could perhaps figure out the right module visibility state for having CrossModuleRedefined and all of its transitively exported modules visible, and then query the preprocessor for the visible ModuleMacro in that state.)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div class="h5"><div><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><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-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><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></div></blockquote></div><br></div></div></div><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>