r330152 - Use export_as for autolinking frameworks

Bruno Cardoso Lopes via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 16 12:42:32 PDT 2018


Author: bruno
Date: Mon Apr 16 12:42:32 2018
New Revision: 330152

URL: http://llvm.org/viewvc/llvm-project?rev=330152&view=rev
Log:
Use export_as for autolinking frameworks

framework module SomeKitCore {
  ...
  export_as SomeKit
}

Given the module above, while generting autolink information during
codegen, clang should to emit '-framework SomeKitCore' only if SomeKit
was not imported in the relevant TU, otherwise it should use '-framework
SomeKit' instead.

rdar://problem/38269782

Added:
    cfe/trunk/test/Modules/Inputs/exportas-link/
    cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/
    cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/
    cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/OtherKit.h
    cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/
    cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/module.modulemap
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SKWidget.h
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SomeKit.h
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/module.modulemap
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/SomeKit.tbd
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SKWidget.h
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SomeKitCore.h
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/module.modulemap
    cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/SomeKitCore.tbd
    cfe/trunk/test/Modules/use-exportas-for-link.m
Modified:
    cfe/trunk/include/clang/Basic/Module.h
    cfe/trunk/include/clang/Lex/ModuleMap.h
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Lex/ModuleMap.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp

Modified: cfe/trunk/include/clang/Basic/Module.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Module.h?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Module.h (original)
+++ cfe/trunk/include/clang/Basic/Module.h Mon Apr 16 12:42:32 2018
@@ -331,6 +331,10 @@ public:
   /// an entity from this module is used.
   llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
 
+  /// Autolinking uses the framework name for linking purposes
+  /// when this is false and the export_as name otherwise.
+  bool UseExportAsModuleLinkName = false;
+
   /// \brief The set of "configuration macros", which are macros that
   /// (intentionally) change how this module is built.
   std::vector<std::string> ConfigMacros;

Modified: cfe/trunk/include/clang/Lex/ModuleMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleMap.h?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/ModuleMap.h (original)
+++ cfe/trunk/include/clang/Lex/ModuleMap.h Mon Apr 16 12:42:32 2018
@@ -21,6 +21,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerIntPair.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
@@ -104,7 +105,19 @@ class ModuleMap {
   /// \brief The number of modules we have created in total.
   unsigned NumCreatedModules = 0;
 
+  /// In case a module has a export_as entry, it might have a pending link
+  /// name to be determined if that module is imported.
+  llvm::StringMap<llvm::StringSet<>> PendingLinkAsModule;
+
 public:
+  /// Use PendingLinkAsModule information to mark top level link names that
+  /// are going to be replaced by export_as aliases.
+  void resolveLinkAsDependencies(Module *Mod);
+
+  /// Make module to use export_as as the link dependency name if enough
+  /// information is available or add it to a pending list otherwise.
+  void addLinkAsDependency(Module *Mod);
+
   /// \brief Flags describing the role of a module header.
   enum ModuleHeaderRole {
     /// \brief This header is normally included in the module.

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Apr 16 12:42:32 2018
@@ -1556,6 +1556,12 @@ static void addLinkOptionsPostorder(Code
   // Add linker options to link against the libraries/frameworks
   // described by this module.
   llvm::LLVMContext &Context = CGM.getLLVMContext();
+
+  // For modules that use export_as for linking, use that module
+  // name instead.
+  if (Mod->UseExportAsModuleLinkName)
+    return;
+
   for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
     // Link against a framework.  Frameworks are currently Darwin only, so we
     // don't to ask TargetCodeGenInfo for the spelling of the linker option.

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Mon Apr 16 12:42:32 2018
@@ -1979,6 +1979,12 @@ CompilerInstance::loadModule(SourceLocat
                      Module, ImportLoc);
   }
 
+  // Resolve any remaining module using export_as for this one.
+  getPreprocessor()
+      .getHeaderSearchInfo()
+      .getModuleMap()
+      .resolveLinkAsDependencies(TopModule);
+
   LastModuleImportLoc = ImportLoc;
   LastModuleImportResult = ModuleLoadResult(Module);
   return LastModuleImportResult;

Modified: cfe/trunk/lib/Lex/ModuleMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ModuleMap.cpp?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/ModuleMap.cpp (original)
+++ cfe/trunk/lib/Lex/ModuleMap.cpp Mon Apr 16 12:42:32 2018
@@ -54,6 +54,24 @@
 
 using namespace clang;
 
+void ModuleMap::resolveLinkAsDependencies(Module *Mod) {
+  auto PendingLinkAs = PendingLinkAsModule.find(Mod->Name);
+  if (PendingLinkAs != PendingLinkAsModule.end()) {
+    for (auto &Name : PendingLinkAs->second) {
+      auto *M = findModule(Name.getKey());
+      if (M)
+        M->UseExportAsModuleLinkName = true;
+    }
+  }
+}
+
+void ModuleMap::addLinkAsDependency(Module *Mod) {
+  if (findModule(Mod->ExportAsModule))
+    Mod->UseExportAsModuleLinkName = true;
+  else
+    PendingLinkAsModule[Mod->ExportAsModule].insert(Mod->Name);
+}
+
 Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) {
   switch ((int)Role) {
   default: llvm_unreachable("unknown header role");
@@ -2412,6 +2430,8 @@ void ModuleMapParser::parseExportAsDecl(
   }
   
   ActiveModule->ExportAsModule = Tok.getString();
+  Map.addLinkAsDependency(ActiveModule);
+
   consumeToken();
 }
 

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=330152&r1=330151&r2=330152&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Mon Apr 16 12:42:32 2018
@@ -5171,6 +5171,7 @@ ASTReader::ReadSubmoduleBlock(ModuleFile
       break;
 
     case SUBMODULE_LINK_LIBRARY:
+      ModMap.resolveLinkAsDependencies(CurrentModule);
       CurrentModule->LinkLibraries.push_back(
                                          Module::LinkLibrary(Blob, Record[0]));
       break;
@@ -5203,6 +5204,7 @@ ASTReader::ReadSubmoduleBlock(ModuleFile
 
     case SUBMODULE_EXPORT_AS:
       CurrentModule->ExportAsModule = Blob.str();
+      ModMap.addLinkAsDependency(CurrentModule);
       break;
     }
   }

Added: cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/OtherKit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/OtherKit.h?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/OtherKit.h (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Headers/OtherKit.h Mon Apr 16 12:42:32 2018
@@ -0,0 +1,6 @@
+#import <SomeKitCore/SomeKitCore.h>
+
+#ifdef F
+#import <SomeKit/SomeKit.h>
+#endif
+

Added: cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/module.modulemap?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/module.modulemap (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/OtherKit.framework/Modules/module.modulemap Mon Apr 16 12:42:32 2018
@@ -0,0 +1,5 @@
+
+framework module OtherKit {
+  header "OtherKit.h"
+  export *
+}

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SKWidget.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SKWidget.h?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SKWidget.h (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SKWidget.h Mon Apr 16 12:42:32 2018
@@ -0,0 +1 @@
+#import <SomeKitCore/SKWidget.h>

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SomeKit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SomeKit.h?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SomeKit.h (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Headers/SomeKit.h Mon Apr 16 12:42:32 2018
@@ -0,0 +1 @@
+#import <SomeKit/SKWidget.h>

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/module.modulemap?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/module.modulemap (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/Modules/module.modulemap Mon Apr 16 12:42:32 2018
@@ -0,0 +1,6 @@
+framework module SomeKit {
+  umbrella header "SomeKit.h"
+  module * {
+    export *
+  }
+}

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/SomeKit.tbd
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/SomeKit.tbd?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/SomeKit.tbd (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKit.framework/SomeKit.tbd Mon Apr 16 12:42:32 2018
@@ -0,0 +1 @@
+dummy tbd file

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SKWidget.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SKWidget.h?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SKWidget.h (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SKWidget.h Mon Apr 16 12:42:32 2018
@@ -0,0 +1,4 @@
+ at interface SKWidget
+- (void)someObjCMethod;
+ at end
+

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SomeKitCore.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SomeKitCore.h?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SomeKitCore.h (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Headers/SomeKitCore.h Mon Apr 16 12:42:32 2018
@@ -0,0 +1 @@
+#import <SomeKitCore/SKWidget.h>

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/module.modulemap?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/module.modulemap (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/Modules/module.modulemap Mon Apr 16 12:42:32 2018
@@ -0,0 +1,7 @@
+framework module SomeKitCore {
+  umbrella header "SomeKitCore.h"
+  export_as SomeKit
+  module * {
+    export *
+  }
+}

Added: cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/SomeKitCore.tbd
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/SomeKitCore.tbd?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/SomeKitCore.tbd (added)
+++ cfe/trunk/test/Modules/Inputs/exportas-link/SomeKitCore.framework/SomeKitCore.tbd Mon Apr 16 12:42:32 2018
@@ -0,0 +1 @@
+dummy tbd file

Added: cfe/trunk/test/Modules/use-exportas-for-link.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/use-exportas-for-link.m?rev=330152&view=auto
==============================================================================
--- cfe/trunk/test/Modules/use-exportas-for-link.m (added)
+++ cfe/trunk/test/Modules/use-exportas-for-link.m Mon Apr 16 12:42:32 2018
@@ -0,0 +1,44 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DA -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_A %s
+// CHECK_A: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_A: ![[MODULE]] = !{!"-framework", !"SomeKit"}
+#ifdef A
+ at import SomeKitCore;
+ at import SomeKit;
+#endif
+
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DB -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_B %s
+// CHECK_B: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_B: ![[MODULE]] = !{!"-framework", !"SomeKit"}
+#ifdef B
+ at import SomeKit;
+ at import SomeKitCore;
+#endif
+
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DC -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_C %s
+// CHECK_C: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_C: ![[MODULE]] = !{!"-framework", !"SomeKitCore"}
+#ifdef C
+ at import SomeKitCore;
+#endif
+
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DD -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_D %s
+// CHECK_D: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_D: ![[MODULE]] = !{!"-framework", !"SomeKit"}
+#ifdef D
+ at import SomeKit;
+#endif
+
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DE -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_E %s
+// CHECK_E: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_E: ![[MODULE]] = !{!"-framework", !"SomeKitCore"}
+#ifdef E
+ at import OtherKit;
+#endif
+
+// RUN: %clang_cc1 -emit-llvm -o - -fmodules-cache-path=%t -DF -fmodules -fimplicit-module-maps -F %S/Inputs/exportas-link %s | FileCheck --check-prefix=CHECK_F %s
+// CHECK_F: !llvm.linker.options = !{![[MODULE:[0-9]+]]}
+// CHECK_F: ![[MODULE]] = !{!"-framework", !"SomeKit"}
+#ifdef F
+ at import OtherKit;
+#endif




More information about the cfe-commits mailing list