From e842a47452223f9f3b683e0f7f9cccb48192cbb6 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 22 Oct 2014 02:05:46 +0000 Subject: [modules] Initial support for explicitly loading .pcm files. Implicit module builds are not well-suited to a lot of build systems. In particular, they fare badly in distributed build systems, and they lead to build artifacts that are not tracked as part of the usual dependency management process. This change allows explicitly-built module files (which are already supported through the -emit-module flag) to be explicitly loaded into a build, allowing build systems to opt to manage module builds and dependencies themselves. This is only the first step in supporting such configurations, and it should be considered experimental and subject to change or removal for now. llvm-svn: 220359 --- clang/lib/Frontend/CompilerInstance.cpp | 54 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'clang/lib/Frontend/CompilerInstance.cpp') diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index e1803d0f311..ba0743abd26 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1040,7 +1040,7 @@ static bool compileAndLoadModule(CompilerInstance &ImportingInstance, // Try to read the module file, now that we've compiled it. ASTReader::ASTReadResult ReadResult = ImportingInstance.getModuleManager()->ReadAST( - ModuleFileName, serialization::MK_Module, ImportLoc, + ModuleFileName, serialization::MK_ImplicitModule, ImportLoc, ModuleLoadCapabilities); if (ReadResult == ASTReader::OutOfDate && @@ -1267,6 +1267,53 @@ void CompilerInstance::createModuleManager() { } } +ModuleLoadResult +CompilerInstance::loadModuleFile(StringRef FileName, SourceLocation Loc) { + if (!ModuleManager) + createModuleManager(); + if (!ModuleManager) + return ModuleLoadResult(); + + // Load the module if this is the first time we've been told about this file. + auto *MF = ModuleManager->getModuleManager().lookup(FileName); + if (!MF) { + struct ReadModuleNameListener : ASTReaderListener { + std::function OnRead; + ReadModuleNameListener(std::function F) : OnRead(F) {} + void ReadModuleName(StringRef ModuleName) override { OnRead(ModuleName); } + }; + + // Register listener to track the modules that are loaded by explicitly + // loading a module file. We suppress any attempts to implicitly load + // module files for any such module. + ASTReader::ListenerScope OnReadModuleName( + *ModuleManager, + llvm::make_unique([&](StringRef ModuleName) { + auto &PP = getPreprocessor(); + auto *NameII = PP.getIdentifierInfo(ModuleName); + auto *Module = PP.getHeaderSearchInfo().lookupModule(ModuleName, false); + if (!KnownModules.insert(std::make_pair(NameII, Module)).second) + getDiagnostics().Report(Loc, diag::err_module_already_loaded) + << ModuleName << FileName; + })); + + if (ModuleManager->ReadAST(FileName, serialization::MK_ExplicitModule, Loc, + ASTReader::ARR_None) != ASTReader::Success) + return ModuleLoadResult(); + + MF = ModuleManager->getModuleManager().lookup(FileName); + assert(MF && "unexpectedly failed to load module file"); + } + + if (MF->ModuleName.empty()) { + getDiagnostics().Report(Loc, diag::err_module_file_not_module) + << FileName; + return ModuleLoadResult(); + } + auto *Module = PP->getHeaderSearchInfo().lookupModule(MF->ModuleName, false); + return ModuleLoadResult(Module, false); +} + ModuleLoadResult CompilerInstance::loadModule(SourceLocation ImportLoc, ModuleIdPath Path, @@ -1330,8 +1377,9 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, // Try to load the module file. unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing; - switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module, - ImportLoc, ARRFlags)) { + switch (ModuleManager->ReadAST(ModuleFileName, + serialization::MK_ImplicitModule, ImportLoc, + ARRFlags)) { case ASTReader::Success: break; -- cgit v1.2.3