From d3062fa1038be154c33a1747c2d3fcfe7b65aa83 Mon Sep 17 00:00:00 2001 From: Manman Ren Date: Wed, 11 Jan 2017 00:48:19 +0000 Subject: [PATCH] Module: Do not create Implicit ImportDecl for module X if we are building an implemenation of module X. This fixes a regression caused by r280409. rdar://problem/29930553 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@291628 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 3 ++- lib/Sema/SemaDecl.cpp | 13 +++++++++---- test/Modules/Inputs/module-impl-with-link/foo.h | 1 + .../Inputs/module-impl-with-link/module.modulemap | 4 ++++ test/Modules/module-impl-with-link.c | 7 +++++++ 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/Modules/Inputs/module-impl-with-link/foo.h create mode 100644 test/Modules/Inputs/module-impl-with-link/module.modulemap create mode 100644 test/Modules/module-impl-with-link.c diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 01141b32f7..943bbca7fd 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -1905,7 +1905,8 @@ public: /// \brief The parser has processed a module import translated from a /// #include or similar preprocessing directive. void ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod); - void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod); + void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod, + bool NoImport); /// \brief The parsed has entered a submodule. void ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cdc9882d14..1fa131f627 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -15652,10 +15652,11 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc, void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); - BuildModuleInclude(DirectiveLoc, Mod); + BuildModuleInclude(DirectiveLoc, Mod, false/*NoImport*/); } -void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { +void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod, + bool NoImport) { // 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 // implementation detail of us building the module. @@ -15665,7 +15666,7 @@ void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { TUKind == TU_Module && getSourceManager().isWrittenInMainFile(DirectiveLoc); - bool ShouldAddImport = !IsInModuleIncludes; + bool ShouldAddImport = !IsInModuleIncludes && !NoImport; // If this module import was due to an inclusion directive, create an // implicit import declaration to capture it in the AST. @@ -15713,7 +15714,11 @@ void Sema::ActOnModuleEnd(SourceLocation EofLoc, Module *Mod) { assert(File != getSourceManager().getMainFileID() && "end of submodule in main source file"); SourceLocation DirectiveLoc = getSourceManager().getIncludeLoc(File); - BuildModuleInclude(DirectiveLoc, Mod); + // Do not create implicit ImportDecl if we are building the implementation + // of a module. + bool NoImport = Mod->getTopLevelModuleName() == getLangOpts().CurrentModule && + !getLangOpts().isCompilingModule(); + BuildModuleInclude(DirectiveLoc, Mod, NoImport); } void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, diff --git a/test/Modules/Inputs/module-impl-with-link/foo.h b/test/Modules/Inputs/module-impl-with-link/foo.h new file mode 100644 index 0000000000..90fe1bcc58 --- /dev/null +++ b/test/Modules/Inputs/module-impl-with-link/foo.h @@ -0,0 +1 @@ +//empty diff --git a/test/Modules/Inputs/module-impl-with-link/module.modulemap b/test/Modules/Inputs/module-impl-with-link/module.modulemap new file mode 100644 index 0000000000..b85f8b6fe8 --- /dev/null +++ b/test/Modules/Inputs/module-impl-with-link/module.modulemap @@ -0,0 +1,4 @@ +module Clib { + header "foo.h" + link "Clib" +} diff --git a/test/Modules/module-impl-with-link.c b/test/Modules/module-impl-with-link.c new file mode 100644 index 0000000000..5e5ca83aaf --- /dev/null +++ b/test/Modules/module-impl-with-link.c @@ -0,0 +1,7 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -fmodule-name=Clib %s -I %S/Inputs/module-impl-with-link -emit-llvm -o - +#include "foo.h" +// CHECK: !{{[0-9]+}} = !{i32 6, !"Linker Options", ![[LINK_OPTIONS:[0-9]+]]} +// Make sure we don't generate linker option for module Clib since this TU is +// an implementation of Clib. +// CHECK: ![[LINK_OPTIONS]] = !{} -- 2.50.1