diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/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" @@ -3195,6 +3196,9 @@ // Add the declaration to its redeclaration context so later merging // lookups will find it. MergeDC->makeDeclVisibleInContextImpl(New, /*Internal*/true); + if (isa(New) && Name.getAsString() == "std") + if (!Reader.getSema()->StdNamespace) + Reader.getSema()->StdNamespace = New; } } @@ -3358,6 +3362,14 @@ return FindExistingResult(Reader, D, Existing, AnonymousDeclNumber, TypedefNameForLinkage); } + if (isa(D) && D->getName() == "std") { + auto StdPtr = Reader.getSema()->StdNamespace; + if (StdPtr.isValid() && !StdPtr.isOffset()) + if (auto *Std = cast_or_null(StdPtr.get(nullptr))) + if (isSameEntity(Std, D)) + return FindExistingResult(Reader, D, Std, AnonymousDeclNumber, + TypedefNameForLinkage); + } } else { // Not in a mergeable context. return FindExistingResult(Reader); diff --git a/clang/test/Modules/Inputs/pr39287-2/a.h b/clang/test/Modules/Inputs/pr39287-2/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/pr39287-2/a.h @@ -0,0 +1 @@ +namespace std { class type_info; } diff --git a/clang/test/Modules/Inputs/pr39287-2/module.modulemap b/clang/test/Modules/Inputs/pr39287-2/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/pr39287-2/module.modulemap @@ -0,0 +1,3 @@ +module "a.h" { + header "a.h" +} diff --git a/clang/test/Modules/Inputs/pr39287/a.h b/clang/test/Modules/Inputs/pr39287/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/pr39287/a.h @@ -0,0 +1 @@ +namespace std {} diff --git a/clang/test/Modules/Inputs/pr39287/module.modulemap b/clang/test/Modules/Inputs/pr39287/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/pr39287/module.modulemap @@ -0,0 +1,3 @@ +module "a.h" { + header "a.h" +} diff --git a/clang/test/Modules/pr39287-2.cpp b/clang/test/Modules/pr39287-2.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/pr39287-2.cpp @@ -0,0 +1,12 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -std=c++17 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs/pr39287-2 %s -verify + +class A { + virtual ~A() {} +}; + +#include "a.h" + +void foo() { + typeid(foo); // expected-warning {{expression result unused}} +} diff --git a/clang/test/Modules/pr39287.cpp b/clang/test/Modules/pr39287.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/pr39287.cpp @@ -0,0 +1,14 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -std=c++17 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs/pr39287 %s -verify + +class A { + virtual ~A() {} +}; + +#include "a.h" + +namespace std { class type_info; } + +void foo() { + typeid(foo); // expected-warning {{expression result unused}} +}