From: Adrian Prantl Date: Wed, 17 Aug 2016 23:13:53 +0000 (+0000) Subject: Support object-file-wrapped modules in clang -module-file-info. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e6658afdf3ce9f6c603ca1d8a236bd254837e4ac;p=clang Support object-file-wrapped modules in clang -module-file-info. rdar://problem/24504815 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@279004 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/FrontendActions.h b/include/clang/Frontend/FrontendActions.h index 0cbacbb4b1..b56a04ae06 100644 --- a/include/clang/Frontend/FrontendActions.h +++ b/include/clang/Frontend/FrontendActions.h @@ -138,6 +138,7 @@ class DumpModuleInfoAction : public ASTFrontendAction { protected: std::unique_ptr CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override; + bool BeginInvocation(CompilerInstance &CI) override; void ExecuteAction() override; public: diff --git a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp index de40e41211..f2090f9583 100644 --- a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp +++ b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp @@ -314,25 +314,29 @@ ObjectFilePCHContainerWriter::CreatePCHContainerGenerator( void ObjectFilePCHContainerReader::ExtractPCH( llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { - if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) { - auto *Obj = OF.get().get(); - bool IsCOFF = isa(Obj); + auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer); + if (OFOrErr) { + auto &OF = OFOrErr.get(); + bool IsCOFF = isa(*OF); // Find the clang AST section in the container. - for (auto &Section : OF->get()->sections()) { + for (auto &Section : OF->sections()) { StringRef Name; Section.getName(Name); - if ((!IsCOFF && Name == "__clangast") || - ( IsCOFF && Name == "clangast")) { + if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) { StringRef Buf; Section.getContents(Buf); - StreamFile.init((const unsigned char *)Buf.begin(), - (const unsigned char *)Buf.end()); - return; + return StreamFile.init((const unsigned char *)Buf.begin(), + (const unsigned char *)Buf.end()); } } } - - // As a fallback, treat the buffer as a raw AST. - StreamFile.init((const unsigned char *)Buffer.getBufferStart(), - (const unsigned char *)Buffer.getBufferEnd()); + handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) { + if (EIB.convertToErrorCode() == + llvm::object::object_error::invalid_file_type) + // As a fallback, treat the buffer as a raw AST. + StreamFile.init((const unsigned char *)Buffer.getBufferStart(), + (const unsigned char *)Buffer.getBufferEnd()); + else + EIB.log(llvm::errs()); + }); } diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index babaa2de77..88ff7e0894 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -596,6 +596,13 @@ namespace { }; } +bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) { + // The Object file reader also supports raw ast files and there is no point in + // being strict about the module file format in -module-file-info mode. + CI.getHeaderSearchOpts().ModuleFormat = "obj"; + return true; +} + void DumpModuleInfoAction::ExecuteAction() { // Set up the output file. std::unique_ptr OutFile; @@ -608,6 +615,7 @@ void DumpModuleInfoAction::ExecuteAction() { llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); Out << "Information for module file '" << getCurrentFile() << "':\n"; + Preprocessor &PP = getCompilerInstance().getPreprocessor(); DumpModuleInfoListener Listener(Out); HeaderSearchOptions &HSOpts = diff --git a/test/Modules/module_file_info.m b/test/Modules/module_file_info.m index 8693d2b894..6f32659f97 100644 --- a/test/Modules/module_file_info.m +++ b/test/Modules/module_file_info.m @@ -1,10 +1,13 @@ @import DependsOnModule; -// RUN: rm -rf %t -// RUN: %clang_cc1 -w -Wunused -fmodules -fimplicit-module-maps -fdisable-module-hash -fmodules-cache-path=%t -F %S/Inputs -DBLARG -DWIBBLE=WOBBLE -fmodule-feature myfeature %s +// RUN: rm -rf %t %t-obj +// RUN: %clang_cc1 -w -Wunused -fmodules -fmodule-format=raw -fimplicit-module-maps -fdisable-module-hash -fmodules-cache-path=%t -F %S/Inputs -DBLARG -DWIBBLE=WOBBLE -fmodule-feature myfeature %s // RUN: %clang_cc1 -module-file-info %t/DependsOnModule.pcm | FileCheck %s +// RUN: %clang_cc1 -w -Wunused -fmodules -fmodule-format=obj -fimplicit-module-maps -fdisable-module-hash -fmodules-cache-path=%t-obj -F %S/Inputs -DBLARG -DWIBBLE=WOBBLE -fmodule-feature myfeature %s +// RUN: %clang_cc1 -module-file-info %t-obj/DependsOnModule.pcm | FileCheck %s + // CHECK: Generated by this Clang: // CHECK: Module name: DependsOnModule