<div dir="ltr"><div dir="ltr">On Fri, 26 Jun 2020 at 12:53, Louis Dionne via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div><blockquote type="cite"><div>On Jun 26, 2020, at 12:22, Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:</div><br><div><div dir="auto" 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;text-decoration:none"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 25 Jun 2020, 21:05 Louis Dionne via cfe-dev, <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br><br>The C++ Standard Library has multiple headers of the form <cfoobar>, which are<br>basically importing declarations from the corresponding <foobar.h> header into<br>namespace std. For example, <cstdio> basically imports all the declarations from<br><stdio.h> into namespace std:<br><br> <span> </span>namespace std {<br> <span> </span>using ::FILE;<br> <span> </span>using ::fpos_t;<br> <span> </span>using ::size_t;<br> <span> </span>...<br> <span> </span>}<br><br>When porting libc++ to new platforms, especially more embedded-ish platforms,<span> </span><br>it is very common that some declarations are not provided by the underlying<br>C Standard Library. This leads to having to detect what platform we're on,<br>and then to conditionally exclude some declarations:<br><br> <span> </span>namespace std {<br> <span> </span>#if !defined(SOME_WEIRD_PLATFORM)<br> <span> </span>using ::FILE;<br> <span> </span>using ::fpos_t;<br> <span> </span>#endif<br> <span> </span>using ::size_t;<br> <span> </span>}<br><br>Unfortunately, different platforms often offer slightly different subsets, so<br>these #ifs quickly become difficult to maintain. Trying to find common themes<br>for excluding declarations (e.g. #if !defined(_LIBCPP_HAS_NO_FILE)) is vain,<br>because the subset provided by a platform is often arbitrary. For example, I've<br>seen platforms where malloc() and free() were not provided, however operator new<br>was -- so trying to carve out something like _LIBCPP_HAS_NO_ALLOCATION wouldn't<br>really make sense.<br><br>Given the difficulty of manually excluding such using declarations, I came to<br>the conclusion that what we wanted was often something like (pseudocode):<br><br> <span> </span>namespace std {<br> <span> </span>#if __has_declaration(::FILE)<br> <span> </span>using ::FILE;<br> <span> </span>#endif<br><br> <span> </span>#if __has_declaration(::fpos_t)<br> <span> </span>using ::fpos_t;<br> <span> </span>#endif<br><br> <span> </span>#if __has_declaration(::size_t)<br> <span> </span>using ::size_t;<br> <span> </span>#endif<br><br> <span> </span>...<br> <span> </span>}<br><br>Basically, we want to import each declaration into namespace std only if the<br>global namespace has such a declaration.<br><br>Now, I understand this raises several questions. Of course, this couldn't be<br>done at the preprocessor level because I don't think we can know whether a<span> </span><br>declaration exists at that time. However, I was curious to ask on this list<br>whether someone could think of a reasonable way to solve this problem.<br><br>Having some mechanism for doing this would be a huge life improvement for libc++.<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Do you always want to guard a namespace & scope using-declaration? We could probably support an attribute on such declarations that would result in them having no effect if no names are found -- or perhaps being invalid to reference if no names are found. (Otherwise, Clang already has some support for MSVC's __if_exists, but it's got some deep semantic problems, and I don't think we should recommend its use.)</div></div></div></blockquote><div><br></div><div>Yes, as far as I can tell, I only want to apply this to using declarations inside namespace std. It's a pretty constrained use case.</div><div><br></div><div>I think the better semantics would be to have no effect if no names are found, since that's what's closest in effect to having an #ifdef in front that removes it.</div></div></div></blockquote><div><br></div><div>That semantic model may be a better fit for your use case, but it's not really a great fit for C++ more broadly. Consider a case such as:</div><div><br></div><div>template<typename T> int f() {</div><div> [[clang::its_ok_if_this_doesnt_exist]] using T::x;</div><div> return x;</div><div>}</div><div><br></div><div>During the phase 1 name lookup for 'x' in the return-statement, we find the local (but dependent) using-declaration and bind to that. During template instantiation, it would be challenging to pretend that that 'x' doesn't exist and fall back on an enclosing name 'x'. The semantic model that fits better for this case would be to say that name lookup stops at the using-declaration (and doesn't consider enclosing scopes), but then the result is an error because the name 'x' didn't resolve to anything. (Roughly as if a failed using-declaration with the attribute in question behaves as if it were =delete'd or 'requires false'd in some sense.)</div><div><br></div><div>If we're going to add an extension for this, I'd prefer that it's something that can be applied soundly in general, rather than introducing another situation like the one for `__if_exists` where we support it only partially and only in certain contexts.</div><div><br></div><div>It's not clear to me whether this approach would actually be bad for your use case, or merely different from what you get with #ifdefs. An error on use of `std::fopen` that says "can't refer to std::fopen because there is no declaration named 'fopen' in the global namespace" or similar seems like it might actually result in a better user experience than one that says "no name 'fopen' in namespace 'std'" or similar.</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="overflow-wrap: break-word;"><div><div>The resulting error is also quite straightforward, i.e. the compiler just says "oops, no such member in namespace std". We could potentially augment the diagnostic to say "there's no such member in namespace std BECAUSE we tried importing it and it wasn't in the global namespace".</div><div><br></div><div>Louis</div><br><blockquote type="cite"><div><div dir="auto" 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;text-decoration:none"><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Cheers,<br>Louis<br><br>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" rel="noreferrer" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-</a></blockquote></div></div></div></div></blockquote></div><br></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>