From: Bruno Cardoso Lopes Date: Wed, 27 Jun 2018 20:29:36 +0000 (+0000) Subject: [Modules][ObjC] Warn on the use of '@import' in framework headers X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4dc93bb1e15647d01cec2ef51f6bd53d20e7ea82;p=clang [Modules][ObjC] Warn on the use of '@import' in framework headers Using @import in framework headers inhibit the use of such headers when not using modules, this is specially bad for headers that end up in the SDK (or any other system framework). Add a warning to give users some indication that this is discouraged. rdar://problem/39192894 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@335780 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 56384db124..a7fe5032df 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -34,6 +34,7 @@ def AutoImport : DiagGroup<"auto-import">; def FrameworkHdrQuotedInclude : DiagGroup<"quoted-include-in-framework-header">; def FrameworkIncludePrivateFromPublic : DiagGroup<"framework-include-private-from-public">; +def FrameworkHdrAtImport : DiagGroup<"atimport-in-framework-header">; def CXX14BinaryLiteral : DiagGroup<"c++14-binary-literal">; def CXXPre14CompatBinaryLiteral : DiagGroup<"c++98-c++11-compat-binary-literal">; def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">; diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index ee9ca7118b..c2a1f095f1 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -248,6 +248,11 @@ def err_unexpected_at : Error<"unexpected '@' in program">; def err_atimport : Error< "use of '@import' when modules are disabled">; +def warn_atimport_in_framework_header : Warning< + "use of '@import' in framework header is discouraged, " + "including this header requires -fmodules">, + InGroup; + def err_invalid_reference_qualifier_application : Error< "'%0' qualifier may not be applied to a reference">; def err_illegal_decl_reference_to_reference : Error< diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp index d9255109d7..d1c35352ae 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -20,6 +20,7 @@ #include "clang/Sema/DeclSpec.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" +#include "llvm/Support/Path.h" using namespace clang; @@ -2123,6 +2124,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc) { assert((AtLoc.isInvalid() ? Tok.is(tok::kw_import) : Tok.isObjCAtKeyword(tok::objc_import)) && "Improper start to module import"); + bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import); SourceLocation ImportLoc = ConsumeToken(); SourceLocation StartLoc = AtLoc.isInvalid() ? ImportLoc : AtLoc; @@ -2146,6 +2148,16 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc) { if (Import.isInvalid()) return nullptr; + // Using '@import' in framework headers requires modules to be enabled so that + // the header is parseable. Emit a warning to make the user aware. + if (IsObjCAtImport && AtLoc.isValid()) { + auto &SrcMgr = PP.getSourceManager(); + auto *FE = SrcMgr.getFileEntryForID(SrcMgr.getFileID(AtLoc)); + if (FE && llvm::sys::path::parent_path(FE->getDir()->getName()) + .endswith(".framework")) + Diags.Report(AtLoc, diag::warn_atimport_in_framework_header); + } + return Import.get(); } diff --git a/test/Modules/Inputs/at-import-in-framework-header/A.framework/Headers/A.h b/test/Modules/Inputs/at-import-in-framework-header/A.framework/Headers/A.h new file mode 100644 index 0000000000..6949a872fc --- /dev/null +++ b/test/Modules/Inputs/at-import-in-framework-header/A.framework/Headers/A.h @@ -0,0 +1,2 @@ +@import B; +int foo(); diff --git a/test/Modules/Inputs/at-import-in-framework-header/A.framework/Modules/module.modulemap b/test/Modules/Inputs/at-import-in-framework-header/A.framework/Modules/module.modulemap new file mode 100644 index 0000000000..126cf26ec9 --- /dev/null +++ b/test/Modules/Inputs/at-import-in-framework-header/A.framework/Modules/module.modulemap @@ -0,0 +1,4 @@ +// at-import-in-framework-header/A.framework/Modules/module.modulemap +framework module A { + header "A.h" +} diff --git a/test/Modules/Inputs/at-import-in-framework-header/module.modulemap b/test/Modules/Inputs/at-import-in-framework-header/module.modulemap new file mode 100644 index 0000000000..a38d66863e --- /dev/null +++ b/test/Modules/Inputs/at-import-in-framework-header/module.modulemap @@ -0,0 +1,2 @@ +// at-import-in-framework-header/module.modulemap +module B {} diff --git a/test/Modules/at-import-in-framework-header.m b/test/Modules/at-import-in-framework-header.m new file mode 100644 index 0000000000..fe36638120 --- /dev/null +++ b/test/Modules/at-import-in-framework-header.m @@ -0,0 +1,17 @@ +// REQUIRES: shell + +// RUN: rm -rf %t +// RUN: mkdir %t + +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache \ +// RUN: -F%S/Inputs/at-import-in-framework-header -I%S/Inputs/at-import-in-framework-header \ +// RUN: -Watimport-in-framework-header -fsyntax-only %s \ +// RUN: 2>%t/stderr +// RUN: FileCheck --input-file=%t/stderr %s + +// CHECK: use of '@import' in framework header is discouraged + +#import + +int bar() { return foo(); } + diff --git a/test/VFS/umbrella-mismatch.m b/test/VFS/umbrella-mismatch.m index fc51802416..8167a21f48 100644 --- a/test/VFS/umbrella-mismatch.m +++ b/test/VFS/umbrella-mismatch.m @@ -1,7 +1,7 @@ // RUN: rm -rf %t // RUN: sed -e "s;INPUT_DIR;%/S/Inputs;g" -e "s;OUT_DIR;%/S/Inputs;g" %S/Inputs/vfsoverlay.yaml > %t.yaml -// RUN: %clang_cc1 -Werror -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ivfsoverlay %t.yaml -F %S/Inputs -fsyntax-only %s -verify -// RUN: %clang_cc1 -Werror -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -F %S/Inputs -fsyntax-only %s -verify +// RUN: %clang_cc1 -Werror -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -ivfsoverlay %t.yaml -F %S/Inputs -fsyntax-only %s -Wno-atimport-in-framework-header -verify +// RUN: %clang_cc1 -Werror -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -F %S/Inputs -fsyntax-only %s -Wno-atimport-in-framework-header -verify // expected-no-diagnostics @import UsesFoo;