From 979b9b12342582b024d45baf4cba86b92585d151 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 13 Oct 2015 00:39:40 +0000 Subject: [PATCH] [modules] Allow the error on importing a C++ module within an extern "C" context (but otherwise at the top level) to be disabled, to support use of C++ standard library implementations that (legitimately) mark their headers as being C++ headers from C libraries that wrap things in 'extern "C"' a bit too enthusiastically. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@250137 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 5 +++-- lib/Sema/SemaDecl.cpp | 15 +++++++++------ test/Modules/extern_c.cpp | 14 ++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index ab4cf0e542..2cc863f4a1 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7803,9 +7803,10 @@ def err_module_unimported_use : Error< def err_module_unimported_use_multiple : Error< "%select{declaration|definition|default argument}0 of %1 must be imported " "from one of the following modules before it is required:%2">; -def err_module_import_in_extern_c : Error< +def ext_module_import_in_extern_c : ExtWarn< "import of C++ module '%0' appears within extern \"C\" language linkage " - "specification">; + "specification">, DefaultError, + InGroup>; def note_module_import_in_extern_c : Note< "extern \"C\" language linkage specification begins here">; def err_module_import_not_at_top_level_fatal : Error< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index a12ff3a02e..174b9cf50f 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -14424,15 +14424,13 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, static void checkModuleImportContext(Sema &S, Module *M, SourceLocation ImportLoc, DeclContext *DC) { + SourceLocation ExternCLoc; + if (auto *LSD = dyn_cast(DC)) { switch (LSD->getLanguage()) { case LinkageSpecDecl::lang_c: - if (!M->IsExternC) { - S.Diag(ImportLoc, diag::err_module_import_in_extern_c) - << M->getFullModuleName(); - S.Diag(LSD->getLocStart(), diag::note_module_import_in_extern_c); - return; - } + if (ExternCLoc.isInvalid()) + ExternCLoc = LSD->getLocStart(); break; case LinkageSpecDecl::lang_cxx: break; @@ -14442,11 +14440,16 @@ static void checkModuleImportContext(Sema &S, Module *M, while (isa(DC)) DC = DC->getParent(); + if (!isa(DC)) { S.Diag(ImportLoc, diag::err_module_import_not_at_top_level_fatal) << M->getFullModuleName() << DC; S.Diag(cast(DC)->getLocStart(), diag::note_module_import_not_at_top_level) << DC; + } else if (!M->IsExternC && ExternCLoc.isValid()) { + S.Diag(ImportLoc, diag::ext_module_import_in_extern_c) + << M->getFullModuleName(); + S.Diag(ExternCLoc, diag::note_module_import_in_extern_c); } } diff --git a/test/Modules/extern_c.cpp b/test/Modules/extern_c.cpp index c9b4b8435f..c072b7e75a 100644 --- a/test/Modules/extern_c.cpp +++ b/test/Modules/extern_c.cpp @@ -9,6 +9,8 @@ // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -DCXX_HEADER -DEXTERN_CXX // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -DCXX_HEADER -DEXTERN_C -DEXTERN_CXX // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -DCXX_HEADER -DEXTERN_C -DNAMESPACE +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -DCXX_HEADER -DEXTERN_C -DNO_EXTERN_C_ERROR -Wno-module-import-in-extern-c +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs %s -DCXX_HEADER -DEXTERN_C -DNAMESPACE -DNO_EXTERN_C_ERROR -Wno-module-import-in-extern-c // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs -x c %s // RUN: %clang_cc1 -fmodules -fimplicit-module-maps -verify -fmodules-cache-path=%t -I %S/Inputs/elsewhere -I %S/Inputs %s -DEXTERN_C -DINDIRECT @@ -36,12 +38,12 @@ extern "C++" { #include HEADER -#if defined(EXTERN_C) && !defined(EXTERN_CXX) && defined(CXX_HEADER) -// expected-error@-3 {{import of C++ module 'cxx_library' appears within extern "C" language linkage specification}} -// expected-note@-17 {{extern "C" language linkage specification begins here}} -#elif defined(NAMESPACE) -// expected-error-re@-6 {{import of module '{{c_library.inner|cxx_library}}' appears within namespace 'M'}} -// expected-note@-24 {{namespace 'M' begins here}} +#if defined(NAMESPACE) +// expected-error-re@-3 {{import of module '{{c_library.inner|cxx_library}}' appears within namespace 'M'}} +// expected-note@-21 {{namespace 'M' begins here}} +#elif defined(EXTERN_C) && !defined(EXTERN_CXX) && defined(CXX_HEADER) && !defined(NO_EXTERN_C_ERROR) +// expected-error@-6 {{import of C++ module 'cxx_library' appears within extern "C" language linkage specification}} +// expected-note@-20 {{extern "C" language linkage specification begins here}} #endif #ifdef EXTERN_CXX -- 2.40.0