From 8f8b650525b0c48938b44c29dfd3df1699851d82 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 18 May 2017 19:34:55 +0000 Subject: [PATCH] When we enter a module within a linkage specification, switch the linkage specification and the TU to the new module. This is necessary to get the module ownership correct for entities that we temporarily hang off the TranslationUnitDecl, such as template parameters and function parameters. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303373 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 16 +++++++++++----- test/Modules/extern_cxx.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 test/Modules/extern_cxx.cpp diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index ba1e97b33d..5e937aa699 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -16048,8 +16048,10 @@ void Sema::ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod) { // FIXME: Consider creating a child DeclContext to hold the entities // lexically within the module. if (getLangOpts().trackLocalOwningModule()) { - cast(CurContext)->setHidden(true); - cast(CurContext)->setLocalOwningModule(Mod); + for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { + cast(DC)->setHidden(true); + cast(DC)->setLocalOwningModule(Mod); + } } } @@ -16082,9 +16084,13 @@ void Sema::ActOnModuleEnd(SourceLocation EomLoc, Module *Mod) { // Any further declarations are in whatever module we returned to. if (getLangOpts().trackLocalOwningModule()) { - cast(CurContext)->setLocalOwningModule(getCurrentModule()); - if (!getCurrentModule()) - cast(CurContext)->setHidden(false); + // The parser guarantees that this is the same context that we entered + // the module within. + for (auto *DC = CurContext; DC; DC = DC->getLexicalParent()) { + cast(DC)->setLocalOwningModule(getCurrentModule()); + if (!getCurrentModule()) + cast(DC)->setHidden(false); + } } } diff --git a/test/Modules/extern_cxx.cpp b/test/Modules/extern_cxx.cpp new file mode 100644 index 0000000000..97ec726de2 --- /dev/null +++ b/test/Modules/extern_cxx.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -x c++-module-map -fmodule-name=A -verify %s -fmodules-local-submodule-visibility +module A { module B {} module C {} } + +#pragma clang module contents + +#pragma clang module begin A.B +extern "C++" { + #pragma clang module begin A.C + template void f(T t); + #pragma clang module end + + void g() { f(0); } // ok +} + +extern "C++" { + #pragma clang module begin A.C + } // expected-error {{extraneous closing brace}} + #pragma clang module end + + #pragma clang module begin A.C + extern "C++" { // expected-note {{to match this '{'}} + #pragma clang module end // expected-error {{expected '}' at end of module}} +} + +#pragma clang module end -- 2.40.0