From: Richard Smith Date: Tue, 17 Nov 2015 23:32:01 +0000 (+0000) Subject: [modules] When a #include is mapped to a module import and appears somewhere X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=41a6e73085bd3a4ea29f06d20cb48d1776774cee;p=clang [modules] When a #include is mapped to a module import and appears somewhere other than the top level, we issue an error. This breaks a fair amount of C++ code wrapping C libraries, where the C library is #included within a namespace / extern "C" combination, because the C library (probably) includes C++ standard library headers which may be within modules. Without modules, this setup is harmless if (and *only* if) the corresponding standard library module was already included outside the namespace, so downgrade the error to a default-error extension in that case, so that it can be selectively disabled for such misbehaving libraries. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253398 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 058d807203..3a24282187 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7930,6 +7930,9 @@ 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< "import of module '%0' appears within %1">, DefaultFatal; +def ext_module_import_not_at_top_level_noop : ExtWarn< + "redundant #include of module '%0' appears within %1">, DefaultError, + InGroup>; def note_module_import_not_at_top_level : Note<"%0 begins here">; def err_module_self_import : Error< "import of module '%0' appears within same top-level module '%1'">; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 0c6e8bd453..817970dd07 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -3589,8 +3589,8 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc, SmallVector FieldDecls; // While we still have something to read, read the declarations in the struct. - while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof) && - !tryParseMisplacedModuleImport()) { + while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && + Tok.isNot(tok::eof)) { // Each iteration of this loop reads one struct-declaration. // Check for extraneous top-level semicolon. diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 05a75d26ec..049aa776e9 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -210,8 +210,8 @@ void Parser::ParseInnerNamespace(std::vector &IdentLoc, ParsedAttributes &attrs, BalancedDelimiterTracker &Tracker) { if (index == Ident.size()) { - while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof) && - !tryParseMisplacedModuleImport()) { + while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && + Tok.isNot(tok::eof)) { ParsedAttributesWithRange attrs(AttrFactory); MaybeParseCXX11Attributes(attrs); MaybeParseMicrosoftAttributes(attrs); @@ -3064,8 +3064,8 @@ void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, if (TagDecl) { // While we still have something to read, read the member-declarations. - while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof) && - !tryParseMisplacedModuleImport()) { + while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && + Tok.isNot(tok::eof)) { // Each iteration of this loop reads one member-declaration. ParseCXXClassMemberDeclarationWithPragmas( CurAS, AccessAttrs, static_cast(TagType), TagDecl); diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index 35cb0fef78..3f8f94b6fc 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -948,8 +948,8 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { Stmts.push_back(R.get()); } - while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof) && - !tryParseMisplacedModuleImport()) { + while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && + Tok.isNot(tok::eof)) { if (Tok.is(tok::annot_pragma_unused)) { HandlePragmaUnused(); continue; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index e4e48a69ab..4e2616ebe2 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -14500,8 +14500,8 @@ Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, } static void checkModuleImportContext(Sema &S, Module *M, - SourceLocation ImportLoc, - DeclContext *DC) { + SourceLocation ImportLoc, DeclContext *DC, + bool FromInclude = false) { SourceLocation ExternCLoc; if (auto *LSD = dyn_cast(DC)) { @@ -14520,7 +14520,9 @@ static void checkModuleImportContext(Sema &S, Module *M, DC = DC->getParent(); if (!isa(DC)) { - S.Diag(ImportLoc, diag::err_module_import_not_at_top_level_fatal) + S.Diag(ImportLoc, (FromInclude && S.isModuleVisible(M)) + ? diag::ext_module_import_not_at_top_level_noop + : 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; @@ -14579,7 +14581,7 @@ DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc, } void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { - checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext); + checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); // Determine whether we're in the #include buffer for a module. The #includes // in that buffer do not qualify as module imports; they're just an diff --git a/test/Modules/auto-module-import.m b/test/Modules/auto-module-import.m index 76441fcfed..9a34c92eab 100644 --- a/test/Modules/auto-module-import.m +++ b/test/Modules/auto-module-import.m @@ -1,6 +1,7 @@ // RUN: rm -rf %t // RUN: %clang_cc1 -Wauto-import -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -F %S/Inputs %s -verify -DERRORS // RUN: %clang_cc1 -Wauto-import -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -F %S/Inputs %s -verify +// RUN: %clang_cc1 -Wauto-import -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -F %S/Inputs -xobjective-c++ %s -verify // // Test both with and without the declarations that refer to unimported // entities. For error recovery, those cases implicitly trigger an import. @@ -85,5 +86,16 @@ int getNotInModule() { void includeNotAtTopLevel() { // expected-note {{function 'includeNotAtTopLevel' begins here}} #include // expected-warning {{treating #include as an import}} \ - expected-error {{import of module 'NoUmbrella.A' appears within function 'includeNotAtTopLevel'}} + expected-error {{redundant #include of module 'NoUmbrella.A' appears within function 'includeNotAtTopLevel'}} } + +#ifdef __cplusplus +namespace NS { // expected-note {{begins here}} +#include // expected-warning {{treating #include as an import}} \ + expected-error {{redundant #include of module 'NoUmbrella.A' appears within namespace 'NS'}} +} +extern "C" { // expected-note {{begins here}} +#include // expected-warning {{treating #include as an import}} \ + expected-error {{import of C++ module 'NoUmbrella.A' appears within extern "C"}} +} +#endif