modocache added a comment. Thanks @rsmith for the guidance here! I appreciate it very much. One snag I ran into after following your suggestion, though, is that when I modify `ASTDeclReader::findExisting` to return Sema's existing implicit std namespace, I run into an assertion later on, when the decl chain is being marked as incomplete. That is, the following patch (debug output included):
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 32bd82d077..9d447952e1 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -47,6 +47,7 @@ #include "clang/Basic/SourceLocation.h" #include "clang/Basic/Specifiers.h" #include "clang/Sema/IdentifierResolver.h" +#include "clang/Sema/Sema.h" #include "clang/Serialization/ASTBitCodes.h" #include "clang/Serialization/ASTReader.h" #include "clang/Serialization/ContinuousRangeMap.h" @@ -3401,6 +3402,22 @@ ASTDeclReader::FindExistingResult ASTDeclReader::findExisting(NamedDecl *D) { return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, TypedefNameForLinkage); } + if (isa<NamespaceDecl>(D) && D->getName() == "std") { + llvm::outs() << "Found std namespace: "; + D->dump(); + llvm::outs() << "Merging into Sema std namespace: "; + Sema *S = Reader.getSema(); + S->getStdNamespace()->dump(); + NamedDecl *Existing = + getDeclForMerging(S->getStdNamespace(), TypedefNameForLinkage); + llvm::outs() << "Found Existing: "; + Existing->dump(); + if (isSameEntity(Existing, D)) { + llvm::outs() << "isSameEntity is true\n"; + return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, + TypedefNameForLinkage); + } + } } else { // Not in a mergeable context. return FindExistingResult(Reader); Results in the expected output, along with an unexpected assert: Found std namespace: NamespaceDecl 0x55a391530910 <mod-virtual-destructor-bug/a.h:1:1, col:16> col:11 imported in a.h std Merging into Sema std namespace: NamespaceDecl 0x55a391501ec8 <<invalid sloc>> <invalid sloc> implicit std Found Existing: NamespaceDecl 0x55a391501ec8 <<invalid sloc>> <invalid sloc> implicit std isSameEntity is true clang-9: ../include/llvm/ADT/PointerUnion.h:135: T llvm::PointerUnion<PT1, PT2>::get() const [with T = clang::LazyGenerationalUpdatePtr<const clang::Decl*, clang::Decl*, &clang::ExternalASTSource::CompleteRedeclChain>; PT1 = llvm::PointerUnion<clang::Decl*, const void*>; PT2 = clang::LazyGenerationalUpdatePtr<const clang::Decl*, clang::Decl*, &clang::ExternalASTSource::CompleteRedeclChain>]: Assertion `is<T>() && "Invalid accessor called"' failed. Stack dump: 0. Program arguments: /home/modocache/Source/llvm/git/dev/llvm/build/bin/clang-9 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name mod-virtual-destructor-bug.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /home/modocache/Source/llvm/git/dev/llvm/build/lib/clang/9.0.0 -I mod-virtual-destructor-bug -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/local/include -internal-isystem /home/modocache/Source/llvm/git/dev/llvm/build/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/modocache/Source/tmp/mod -ferror-limit 19 -fmessage-length 195 -fmodules -fimplicit-module-maps -fmodules-cache-path=mod-virtual-destructor-cache -fmodules-validate-system-headers -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/mod-virtual-destructor-bug-0da92f.o -x c++ mod-virtual-destructor-bug.cpp -faddrsig 1. mod-virtual-destructor-bug.cpp:10:17: current parser token 'class' #0 0x000055a388bfaaa5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/modocache/Source/llvm/git/dev/llvm/build/../lib/Support/Unix/Signals.inc:494:22 #1 0x000055a388bfab38 PrintStackTraceSignalHandler(void*) /home/modocache/Source/llvm/git/dev/llvm/build/../lib/Support/Unix/Signals.inc:558:1 #2 0x000055a388bf8b32 llvm::sys::RunSignalHandlers() /home/modocache/Source/llvm/git/dev/llvm/build/../lib/Support/Signals.cpp:68:20 #3 0x000055a388bfa4fb SignalHandler(int) /home/modocache/Source/llvm/git/dev/llvm/build/../lib/Support/Unix/Signals.inc:357:1 #4 0x00007f909ab86dd0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12dd0) #5 0x00007f909a249077 raise /build/glibc-B9XfQf/glibc-2.28/signal/../sysdeps/unix/sysv/linux/raise.c:51:1 #6 0x00007f909a22a535 abort /build/glibc-B9XfQf/glibc-2.28/stdlib/abort.c:81:7 #7 0x00007f909a22a40f get_sysdep_segment_value /build/glibc-B9XfQf/glibc-2.28/intl/loadmsgcat.c:509:8 #8 0x00007f909a22a40f _nl_load_domain /build/glibc-B9XfQf/glibc-2.28/intl/loadmsgcat.c:970:34 #9 0x00007f909a23a142 (/lib/x86_64-linux-gnu/libc.so.6+0x32142) #10 0x000055a388ec8ee7 clang::LazyGenerationalUpdatePtr<clang::Decl const*, clang::Decl*, &(clang::ExternalASTSource::CompleteRedeclChain(clang::Decl const*))> llvm::PointerUnion<llvm::PointerUnion<clang::Decl*, void const*>, clang::LazyGenerationalUpdatePtr<clang::Decl const*, clang::Decl*, &(clang::ExternalASTSource::CompleteRedeclChain(clang::Decl const*))> >::get<clang::LazyGenerationalUpdatePtr<clang::Decl const*, clang::Decl*, &(clang::ExternalASTSource::CompleteRedeclChain(clang::Decl const*))> >() const /home/modocache/Source/llvm/git/dev/llvm/build/../include/llvm/ADT/PointerUnion.h:135:5 #11 0x000055a38986268d clang::Redeclarable<clang::NamespaceDecl>::DeclLink::markIncomplete() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/include/clang/AST/Redeclarable.h:156:50 #12 0x000055a38985907a void clang::ASTDeclReader::markIncompleteDeclChainImpl<clang::NamespaceDecl>(clang::Redeclarable<clang::NamespaceDecl>*) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Serialization/ASTReaderDecl.cpp:3651:1 #13 0x000055a38983e243 clang::ASTReader::markIncompleteDeclChain(clang::Decl*) /home/modocache/Source/llvm/git/dev/llvm/build/tools/clang/include/clang/AST/DeclNodes.inc:105:1 #14 0x000055a389795c25 clang::ASTReader::finishPendingActions() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Serialization/ASTReader.cpp:9309:5 #15 0x000055a3897a27df clang::ASTReader::FinishedDeserializing() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Serialization/ASTReader.cpp:11563:5 #16 0x000055a3897ad297 clang::ExternalASTSource::Deserializing::~Deserializing() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/include/clang/AST/ExternalASTSource.h:90:5 #17 0x000055a38978d994 clang::ASTReader::FindExternalVisibleDeclsByName(clang::DeclContext const*, clang::DeclarationName) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Serialization/ASTReader.cpp:7561:35 #18 0x000055a38c4c0fa8 clang::DeclContext::lookup(clang::DeclarationName) const (.localalias.2) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/AST/DeclBase.cpp:1635:60 #19 0x000055a38bb9dd35 LookupDirect(clang::Sema&, clang::LookupResult&, clang::DeclContext const*) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Sema/SemaLookup.cpp:845:23 #20 0x000055a38bba142f clang::Sema::LookupQualifiedName(clang::LookupResult&, clang::DeclContext*, bool) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Sema/SemaLookup.cpp:2055:3 #21 0x000055a38b86d705 clang::Sema::ActOnStartNamespaceDef(clang::Scope*, clang::SourceLocation, clang::SourceLocation, clang::SourceLocation, clang::IdentifierInfo*,clang::SourceLocation, clang::ParsedAttributesView const&, clang::UsingDirectiveDecl*&) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Sema/SemaDeclCXX.cpp:8915:25 #22 0x000055a38b41cbae clang::Parser::ParseNamespace(clang::DeclaratorContext, clang::SourceLocation&, clang::SourceLocation) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Parse/ParseDeclCXX.cpp:215:53 #23 0x000055a38b3fe8a5 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Parse/ParseDecl.cpp:1714:43 #24 0x000055a38b3e11e6 clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (.localalias.1) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Parse/Parser.cpp:856:77 #25 0x000055a38b3e0861 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, bool) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Parse/Parser.cpp:674:42 #26 0x000055a38b3dc6dc clang::ParseAST(clang::Sema&, bool, bool) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Parse/ParseAST.cpp:158:37 #27 0x000055a3895f86ef clang::ASTFrontendAction::ExecuteAction() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Frontend/FrontendAction.cpp:1035:11 #28 0x000055a389e277f9 clang::CodeGenAction::ExecuteAction() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/CodeGen/CodeGenAction.cpp:1057:1 #29 0x000055a3895f80de clang::FrontendAction::Execute() /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Frontend/FrontendAction.cpp:938:38 #30 0x000055a3895966d6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/Frontend/CompilerInstance.cpp:945:24 #31 0x000055a38974e262 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:271:38 #32 0x000055a387732e03 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/tools/driver/cc1_main.cpp:225:40 #33 0x000055a38772871d ExecuteCC1Tool(llvm::ArrayRef<char const*>, llvm::StringRef) /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/tools/driver/driver.cpp:309:64 #34 0x000055a387728dfb main /home/modocache/Source/llvm/git/dev/llvm/build/../tools/clang/tools/driver/driver.cpp:381:26 #35 0x00007f909a22c09b __libc_start_main /build/glibc-B9XfQf/glibc-2.28/csu/../csu/libc-start.c:342:3 #36 0x000055a387726eea _start (/home/modocache/Source/llvm/git/dev/llvm/build/bin/clang-9+0x2070eea) fish: “"/home/modocache/Source/llvm/gi…” terminated by signal SIGABRT (Abort) I'm looking into how to avoid this assertion, but for the time being I thought I'd post an update to let everyone know this patch isn't abandoned :) Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D58920/new/ https://reviews.llvm.org/D58920 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits