[cfe-commits] r172437 - in /cfe/trunk: include/clang/Basic/DiagnosticLexKinds.td include/clang/Basic/Module.h include/clang/Serialization/ASTBitCodes.h lib/Basic/Module.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h lib/Lex/ModuleMap.cpp lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp test/Modules/Inputs/autolink-sub.h test/Modules/Inputs/autolink-sub2.h test/Modules/Inputs/autolink.h test/Modules/Inputs/module.map test/Modules/autolink.m
Douglas Gregor
dgregor at apple.com
Mon Jan 14 09:21:01 PST 2013
Author: dgregor
Date: Mon Jan 14 11:21:00 2013
New Revision: 172437
URL: http://llvm.org/viewvc/llvm-project?rev=172437&view=rev
Log:
Implement parsing, AST, (de-)serialization, and placeholder global
metadata for linking against the libraries/frameworks for imported
modules.
The module map language is extended with a new "link" directive that
specifies what library or framework to link against when a module is
imported, e.g.,
link "clangAST"
or
link framework "MyFramework"
Importing the corresponding module (or any of its submodules) will
eventually link against the named library/framework.
For now, I've added some placeholder global metadata that encodes the
imported libraries/frameworks, so that we can test that this
information gets through to the IR. The format of the data is still
under discussion.
Added:
cfe/trunk/test/Modules/Inputs/autolink-sub.h
cfe/trunk/test/Modules/Inputs/autolink-sub2.h
cfe/trunk/test/Modules/Inputs/autolink.h
cfe/trunk/test/Modules/autolink.m
Modified:
cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
cfe/trunk/include/clang/Basic/Module.h
cfe/trunk/include/clang/Serialization/ASTBitCodes.h
cfe/trunk/lib/Basic/Module.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.h
cfe/trunk/lib/Lex/ModuleMap.cpp
cfe/trunk/lib/Serialization/ASTReader.cpp
cfe/trunk/lib/Serialization/ASTWriter.cpp
cfe/trunk/test/Modules/Inputs/module.map
Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Mon Jan 14 11:21:00 2013
@@ -486,6 +486,8 @@
"umbrella for module '%0' already covers this directory">;
def err_mmap_export_module_id : Error<
"expected an exported module name or '*'">;
+def err_mmap_expected_library_name : Error<
+ "expected %select{library|framework}0 name as a string">;
def err_mmap_missing_module_unqualified : Error<
"no module named '%0' visible from '%1'">;
def err_mmap_missing_module_qualified : Error<
Modified: cfe/trunk/include/clang/Basic/Module.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Module.h?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Module.h (original)
+++ cfe/trunk/include/clang/Basic/Module.h Mon Jan 14 11:21:00 2013
@@ -164,7 +164,28 @@
/// \brief The set of export declarations that have yet to be resolved.
SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
-
+
+ /// \brief A library or framework to link against when an entity from this
+ /// module is used.
+ struct LinkLibrary {
+ LinkLibrary() : IsFramework(false) { }
+ LinkLibrary(const std::string &Library, bool IsFramework)
+ : Library(Library), IsFramework(IsFramework) { }
+
+ /// \brief The library to link against.
+ ///
+ /// This will typically be a library or framework name, but can also
+ /// be an absolute path to the library or framework.
+ std::string Library;
+
+ /// \brief Whether this is a framework rather than a library.
+ bool IsFramework;
+ };
+
+ /// \brief The set of libraries or frameworks to link against when
+ /// an entity from this module is used.
+ llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
+
/// \brief Construct a top-level module.
explicit Module(StringRef Name, SourceLocation DefinitionLoc,
bool IsFramework)
Modified: cfe/trunk/include/clang/Serialization/ASTBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTBitCodes.h?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTBitCodes.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTBitCodes.h Mon Jan 14 11:21:00 2013
@@ -603,7 +603,9 @@
SUBMODULE_REQUIRES = 8,
/// \brief Specifies a header that has been explicitly excluded
/// from this submodule.
- SUBMODULE_EXCLUDED_HEADER = 9
+ SUBMODULE_EXCLUDED_HEADER = 9,
+ /// \brief Specifies a library or framework to link against.
+ SUBMODULE_LINK_LIBRARY = 10
};
/// \brief Record types used within a comments block.
Modified: cfe/trunk/lib/Basic/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Module.cpp?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Module.cpp (original)
+++ cfe/trunk/lib/Basic/Module.cpp Mon Jan 14 11:21:00 2013
@@ -257,6 +257,16 @@
OS << "\n";
}
+ for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
+ OS.indent(Indent + 2);
+ OS << "link ";
+ if (LinkLibraries[I].IsFramework)
+ OS << "framework ";
+ OS << "\"";
+ OS.write_escaped(LinkLibraries[I].Library);
+ OS << "\"";
+ }
+
if (InferSubmodules) {
OS.indent(Indent + 2);
if (InferExplicitSubmodules)
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Jan 14 11:21:00 2013
@@ -32,6 +32,7 @@
#include "clang/Basic/Builtins.h"
#include "clang/Basic/ConvertUTF.h"
#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/Module.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CodeGenOptions.h"
@@ -172,7 +173,8 @@
EmitCtorList(GlobalDtors, "llvm.global_dtors");
EmitGlobalAnnotations();
EmitLLVMUsed();
-
+ EmitLinkLibraries();
+
SimplifyPersonality();
if (getCodeGenOpts().EmitDeclMetadata)
@@ -714,6 +716,24 @@
GV->setSection("llvm.metadata");
}
+void CodeGenModule::EmitLinkLibraries() {
+ // If there are no libraries to link against, do nothing.
+ if (LinkLibraries.empty())
+ return;
+
+ // Create metadata for each library we're linking against.
+ llvm::NamedMDNode *Metadata
+ = getModule().getOrInsertNamedMetadata("llvm.link.libraries");
+ for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
+ llvm::Value *Args[2] = {
+ llvm::MDString::get(getLLVMContext(), LinkLibraries[I].Library),
+ llvm::ConstantInt::get(llvm::Type::getInt1Ty(getLLVMContext()),
+ LinkLibraries[I].IsFramework)
+ };
+ Metadata->addOperand(llvm::MDNode::get(getLLVMContext(), Args));
+ }
+}
+
void CodeGenModule::EmitDeferred() {
// Emit code for any potentially referenced deferred decls. Since a
// previously unused static decl may become used during the generation of code
@@ -2681,7 +2701,6 @@
case Decl::TypeAliasTemplate:
case Decl::NamespaceAlias:
case Decl::Block:
- case Decl::Import:
break;
case Decl::CXXConstructor:
// Skip function templates
@@ -2762,6 +2781,53 @@
break;
}
+ case Decl::Import: {
+ ImportDecl *Import = cast<ImportDecl>(D);
+
+ // Ignore import declarations that come from imported modules.
+ if (clang::Module *Owner = Import->getOwningModule()) {
+ if (getLangOpts().CurrentModule.empty() ||
+ Owner->getTopLevelModule()->Name == getLangOpts().CurrentModule)
+ break;
+ }
+
+ // Walk from this module up to its top-level module; we'll import all of
+ // these modules and their non-explicit child modules.
+ llvm::SmallVector<clang::Module *, 2> Stack;
+ for (clang::Module *Mod = Import->getImportedModule(); Mod;
+ Mod = Mod->Parent) {
+ if (!ImportedModules.insert(Mod))
+ break;
+
+ Stack.push_back(Mod);
+ }
+
+ // Find all of the non-explicit submodules of the modules we've imported and
+ // import them.
+ while (!Stack.empty()) {
+ clang::Module *Mod = Stack.back();
+ Stack.pop_back();
+
+ // Add the link libraries for this module.
+ LinkLibraries.insert(LinkLibraries.end(),
+ Mod->LinkLibraries.begin(),
+ Mod->LinkLibraries.end());
+
+ // We've imported this module; now import any of its children that haven't
+ // already been imported.
+ for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
+ SubEnd = Mod->submodule_end();
+ Sub != SubEnd; ++Sub) {
+ if ((*Sub)->IsExplicit)
+ continue;
+
+ if (ImportedModules.insert(*Sub))
+ Stack.push_back(*Sub);
+ }
+ }
+ break;
+ }
+
default:
// Make sure we handled everything we should, every other kind is a
// non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon Jan 14 11:21:00 2013
@@ -23,7 +23,9 @@
#include "clang/AST/Mangle.h"
#include "clang/Basic/ABI.h"
#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/Module.h"
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/Module.h"
@@ -66,6 +68,7 @@
class AnnotateAttr;
class CXXDestructorDecl;
class MangleBuffer;
+ class Module;
namespace CodeGen {
@@ -313,6 +316,12 @@
/// run on termination.
std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
+ /// \brief The complete set of modules that has been imported.
+ llvm::SetVector<clang::Module *> ImportedModules;
+
+ /// \brief The set of libraries to link against.
+ std::vector<clang::Module::LinkLibrary> LinkLibraries;
+
/// @name Cache for Objective-C runtime types
/// @{
@@ -989,6 +998,9 @@
/// references to global which may otherwise be optimized out.
void EmitLLVMUsed();
+ /// \brief Emit the set of libraries to link against.
+ void EmitLinkLibraries();
+
void EmitDeclMetadata();
/// EmitCoverageFile - Emit the llvm.gcov metadata used to tell LLVM where
Modified: cfe/trunk/lib/Lex/ModuleMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ModuleMap.cpp?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/ModuleMap.cpp (original)
+++ cfe/trunk/lib/Lex/ModuleMap.cpp Mon Jan 14 11:21:00 2013
@@ -645,6 +645,7 @@
ExplicitKeyword,
ExportKeyword,
FrameworkKeyword,
+ LinkKeyword,
ModuleKeyword,
Period,
UmbrellaKeyword,
@@ -732,6 +733,7 @@
void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc);
void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
void parseExportDecl();
+ void parseLinkDecl();
void parseInferredModuleDecl(bool Framework, bool Explicit);
bool parseOptionalAttributes(Attributes &Attrs);
@@ -774,6 +776,7 @@
.Case("explicit", MMToken::ExplicitKeyword)
.Case("export", MMToken::ExportKeyword)
.Case("framework", MMToken::FrameworkKeyword)
+ .Case("link", MMToken::LinkKeyword)
.Case("module", MMToken::ModuleKeyword)
.Case("requires", MMToken::RequiresKeyword)
.Case("umbrella", MMToken::UmbrellaKeyword)
@@ -944,6 +947,7 @@
/// header-declaration
/// submodule-declaration
/// export-declaration
+/// link-declaration
///
/// submodule-declaration:
/// module-declaration
@@ -1123,7 +1127,11 @@
case MMToken::HeaderKeyword:
parseHeaderDecl(SourceLocation(), SourceLocation());
break;
-
+
+ case MMToken::LinkKeyword:
+ parseLinkDecl();
+ break;
+
default:
Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
consumeToken();
@@ -1440,7 +1448,36 @@
ActiveModule->UnresolvedExports.push_back(Unresolved);
}
-/// \brief Parse an inferried module declaration (wildcard modules).
+/// \brief Parse a link declaration.
+///
+/// module-declaration:
+/// 'link' 'framework'[opt] string-literal
+void ModuleMapParser::parseLinkDecl() {
+ assert(Tok.is(MMToken::LinkKeyword));
+ SourceLocation LinkLoc = consumeToken();
+
+ // Parse the optional 'framework' keyword.
+ bool IsFramework = false;
+ if (Tok.is(MMToken::FrameworkKeyword)) {
+ consumeToken();
+ IsFramework = true;
+ }
+
+ // Parse the library name
+ if (!Tok.is(MMToken::StringLiteral)) {
+ Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
+ << IsFramework << SourceRange(LinkLoc);
+ HadError = true;
+ return;
+ }
+
+ std::string LibraryName = Tok.getString();
+ consumeToken();
+ ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
+ IsFramework));
+}
+
+/// \brief Parse an inferred module declaration (wildcard modules).
///
/// module-declaration:
/// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
@@ -1679,13 +1716,14 @@
case MMToken::FrameworkKeyword:
parseModuleDecl();
break;
-
+
case MMToken::Comma:
case MMToken::ExcludeKeyword:
case MMToken::ExportKeyword:
case MMToken::HeaderKeyword:
case MMToken::Identifier:
case MMToken::LBrace:
+ case MMToken::LinkKeyword:
case MMToken::LSquare:
case MMToken::Period:
case MMToken::RBrace:
Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Mon Jan 14 11:21:00 2013
@@ -3413,6 +3413,9 @@
DeserializationListener->ModuleRead(GlobalID, CurrentModule);
SubmodulesLoaded[GlobalIndex] = CurrentModule;
+
+ // Clear out link libraries; the module file has them.
+ CurrentModule->LinkLibraries.clear();
break;
}
@@ -3600,6 +3603,20 @@
Context.getTargetInfo());
break;
}
+
+ case SUBMODULE_LINK_LIBRARY:
+ if (First) {
+ Error("missing submodule metadata record at beginning of block");
+ return true;
+ }
+
+ if (!CurrentModule)
+ break;
+
+ CurrentModule->LinkLibraries.push_back(
+ Module::LinkLibrary(StringRef(BlobStart, BlobLen),
+ Record[0]));
+ break;
}
}
}
Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Mon Jan 14 11:21:00 2013
@@ -2107,6 +2107,12 @@
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
+ Abbrev = new BitCodeAbbrev();
+ Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
+ Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
+ Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
+ unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
+
// Write the submodule metadata block.
RecordData Record;
Record.push_back(getNumberOfModules(WritingModule));
@@ -2209,7 +2215,16 @@
}
Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
}
-
+
+ // Emit the link libraries.
+ for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
+ Record.clear();
+ Record.push_back(SUBMODULE_LINK_LIBRARY);
+ Record.push_back(Mod->LinkLibraries[I].IsFramework);
+ Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
+ Mod->LinkLibraries[I].Library);
+ }
+
// Queue up the submodules of this module.
for (Module::submodule_iterator Sub = Mod->submodule_begin(),
SubEnd = Mod->submodule_end();
Added: cfe/trunk/test/Modules/Inputs/autolink-sub.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/autolink-sub.h?rev=172437&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/autolink-sub.h (added)
+++ cfe/trunk/test/Modules/Inputs/autolink-sub.h Mon Jan 14 11:21:00 2013
@@ -0,0 +1 @@
+int autolink_sub(void);
Added: cfe/trunk/test/Modules/Inputs/autolink-sub2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/autolink-sub2.h?rev=172437&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/autolink-sub2.h (added)
+++ cfe/trunk/test/Modules/Inputs/autolink-sub2.h Mon Jan 14 11:21:00 2013
@@ -0,0 +1 @@
+int autolink_sub2(void);
Added: cfe/trunk/test/Modules/Inputs/autolink.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/autolink.h?rev=172437&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/autolink.h (added)
+++ cfe/trunk/test/Modules/Inputs/autolink.h Mon Jan 14 11:21:00 2013
@@ -0,0 +1 @@
+extern int autolink;
Modified: cfe/trunk/test/Modules/Inputs/module.map
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/module.map?rev=172437&r1=172436&r2=172437&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/module.map (original)
+++ cfe/trunk/test/Modules/Inputs/module.map Mon Jan 14 11:21:00 2013
@@ -124,3 +124,18 @@
header "linkage-merge-sub.h"
}
}
+
+module autolink {
+ header "autolink.h"
+ link "autolink"
+
+ explicit module sub {
+ header "autolink-sub.h"
+ link "autolink_sub"
+ }
+
+ explicit module sub2 {
+ header "autolink-sub2.h"
+ link framework "autolink_framework"
+ }
+}
Added: cfe/trunk/test/Modules/autolink.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/autolink.m?rev=172437&view=auto
==============================================================================
--- cfe/trunk/test/Modules/autolink.m (added)
+++ cfe/trunk/test/Modules/autolink.m Mon Jan 14 11:21:00 2013
@@ -0,0 +1,18 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -emit-llvm -o - -fmodule-cache-path %t -fmodules -F %S/Inputs -I %S/Inputs %s | FileCheck %s
+
+ at import autolink.sub2;
+
+int f() {
+ return autolink_sub2();
+}
+
+ at import autolink;
+
+int g() {
+ return autolink;
+}
+
+// CHECK: !llvm.link.libraries = !{![[AUTOLINK:[0-9]+]], ![[AUTOLINK_FRAMEWORK:[0-9]+]]}
+// CHECK: ![[AUTOLINK]] = metadata !{metadata !"autolink", i1 false}
+// CHECK: ![[AUTOLINK_FRAMEWORK]] = metadata !{metadata !"autolink_framework", i1 true}
More information about the cfe-commits
mailing list