llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-clang Author: cratelschen (cratelschen) <details> <summary>Changes</summary> --- Patch is 34.00 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/93500.diff 7 Files Affected: - (modified) clang/include/clang/Frontend/CompilerInstance.h (+13-23) - (modified) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp (+74-44) - (modified) clang/tools/driver/cc1_main.cpp (+6-5) - (modified) clang/tools/driver/driver.cpp (+25-9) - (added) kick-off.md (+8) - (modified) llvm/CMakeLists.txt (+37) - (modified) llvm/include/llvm/Support/TargetSelect.h (+104-97) ``````````diff diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index 3464654284f19..109ccd2dc03a8 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -35,7 +35,7 @@ namespace llvm { class raw_fd_ostream; class Timer; class TimerGroup; -} +} // namespace llvm namespace clang { class ASTContext; @@ -58,6 +58,7 @@ class SourceManager; class TargetInfo; enum class DisableValidationForModuleKind; +// Cratels: CompilerInstance是一个工具类来持有单例的 Clang compiler对象。 /// CompilerInstance - Helper class for managing a single instance of the Clang /// compiler. /// @@ -204,6 +205,7 @@ class CompilerInstance : public ModuleLoader { CompilerInstance(const CompilerInstance &) = delete; void operator=(const CompilerInstance &) = delete; + public: explicit CompilerInstance( std::shared_ptr<PCHContainerOperations> PCHContainerOps = @@ -270,9 +272,7 @@ class CompilerInstance : public ModuleLoader { /// Set the flag indicating whether we should (re)build the global /// module index. - void setBuildGlobalModuleIndex(bool Build) { - BuildGlobalModuleIndex = Build; - } + void setBuildGlobalModuleIndex(bool Build) { BuildGlobalModuleIndex = Build; } /// @} /// @name Forwarding Methods @@ -280,9 +280,7 @@ class CompilerInstance : public ModuleLoader { AnalyzerOptions &getAnalyzerOpts() { return Invocation->getAnalyzerOpts(); } - CodeGenOptions &getCodeGenOpts() { - return Invocation->getCodeGenOpts(); - } + CodeGenOptions &getCodeGenOpts() { return Invocation->getCodeGenOpts(); } const CodeGenOptions &getCodeGenOpts() const { return Invocation->getCodeGenOpts(); } @@ -308,9 +306,7 @@ class CompilerInstance : public ModuleLoader { return Invocation->getFileSystemOpts(); } - FrontendOptions &getFrontendOpts() { - return Invocation->getFrontendOpts(); - } + FrontendOptions &getFrontendOpts() { return Invocation->getFrontendOpts(); } const FrontendOptions &getFrontendOpts() const { return Invocation->getFrontendOpts(); } @@ -350,9 +346,7 @@ class CompilerInstance : public ModuleLoader { return Invocation->getPreprocessorOutputOpts(); } - TargetOptions &getTargetOpts() { - return Invocation->getTargetOpts(); - } + TargetOptions &getTargetOpts() { return Invocation->getTargetOpts(); } const TargetOptions &getTargetOpts() const { return Invocation->getTargetOpts(); } @@ -394,9 +388,7 @@ class CompilerInstance : public ModuleLoader { void setVerboseOutputStream(std::unique_ptr<raw_ostream> Value); /// Get the current stream for verbose output. - raw_ostream &getVerboseOutputStream() { - return *VerboseOutputStream; - } + raw_ostream &getVerboseOutputStream() { return *VerboseOutputStream; } /// @} /// @name Target Info @@ -574,8 +566,8 @@ class CompilerInstance : public ModuleLoader { void setASTReader(IntrusiveRefCntPtr<ASTReader> Reader); std::shared_ptr<ModuleDependencyCollector> getModuleDepCollector() const; - void setModuleDepCollector( - std::shared_ptr<ModuleDependencyCollector> Collector); + void + setModuleDepCollector(std::shared_ptr<ModuleDependencyCollector> Collector); std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { return ThePCHContainerOperations; @@ -701,11 +693,9 @@ class CompilerInstance : public ModuleLoader { /// used by some diagnostics printers (for logging purposes only). /// /// \return The new object on success, or null on failure. - static IntrusiveRefCntPtr<DiagnosticsEngine> - createDiagnostics(DiagnosticOptions *Opts, - DiagnosticConsumer *Client = nullptr, - bool ShouldOwnClient = true, - const CodeGenOptions *CodeGenOpts = nullptr); + static IntrusiveRefCntPtr<DiagnosticsEngine> createDiagnostics( + DiagnosticOptions *Opts, DiagnosticConsumer *Client = nullptr, + bool ShouldOwnClient = true, const CodeGenOptions *CodeGenOpts = nullptr); /// Create the file manager and replace any existing one with it. /// diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index f85f0365616f9..134799a38a2c3 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -43,24 +43,38 @@ CreateFrontendBaseAction(CompilerInstance &CI) { (void)Action; switch (CI.getFrontendOpts().ProgramAction) { - case ASTDeclList: return std::make_unique<ASTDeclListAction>(); - case ASTDump: return std::make_unique<ASTDumpAction>(); - case ASTPrint: return std::make_unique<ASTPrintAction>(); - case ASTView: return std::make_unique<ASTViewAction>(); + case ASTDeclList: + return std::make_unique<ASTDeclListAction>(); + case ASTDump: + return std::make_unique<ASTDumpAction>(); + case ASTPrint: + return std::make_unique<ASTPrintAction>(); + case ASTView: + return std::make_unique<ASTViewAction>(); case DumpCompilerOptions: return std::make_unique<DumpCompilerOptionsAction>(); - case DumpRawTokens: return std::make_unique<DumpRawTokensAction>(); - case DumpTokens: return std::make_unique<DumpTokensAction>(); - case EmitAssembly: return std::make_unique<EmitAssemblyAction>(); - case EmitBC: return std::make_unique<EmitBCAction>(); - case EmitHTML: return std::make_unique<HTMLPrintAction>(); - case EmitLLVM: return std::make_unique<EmitLLVMAction>(); - case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>(); - case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>(); - case EmitObj: return std::make_unique<EmitObjAction>(); + case DumpRawTokens: + return std::make_unique<DumpRawTokensAction>(); + case DumpTokens: + return std::make_unique<DumpTokensAction>(); + case EmitAssembly: + return std::make_unique<EmitAssemblyAction>(); + case EmitBC: + return std::make_unique<EmitBCAction>(); + case EmitHTML: + return std::make_unique<HTMLPrintAction>(); + case EmitLLVM: + return std::make_unique<EmitLLVMAction>(); + case EmitLLVMOnly: + return std::make_unique<EmitLLVMOnlyAction>(); + case EmitCodeGenOnly: + return std::make_unique<EmitCodeGenOnlyAction>(); + case EmitObj: + return std::make_unique<EmitObjAction>(); case ExtractAPI: return std::make_unique<ExtractAPIAction>(); - case FixIt: return std::make_unique<FixItAction>(); + case FixIt: + return std::make_unique<FixItAction>(); case GenerateModule: return std::make_unique<GenerateModuleFromModuleMapAction>(); case GenerateModuleInterface: @@ -69,14 +83,20 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique<GenerateReducedModuleInterfaceAction>(); case GenerateHeaderUnit: return std::make_unique<GenerateHeaderUnitAction>(); - case GeneratePCH: return std::make_unique<GeneratePCHAction>(); + case GeneratePCH: + return std::make_unique<GeneratePCHAction>(); case GenerateInterfaceStubs: return std::make_unique<GenerateInterfaceStubsAction>(); - case InitOnly: return std::make_unique<InitOnlyAction>(); - case ParseSyntaxOnly: return std::make_unique<SyntaxOnlyAction>(); - case ModuleFileInfo: return std::make_unique<DumpModuleInfoAction>(); - case VerifyPCH: return std::make_unique<VerifyPCHAction>(); - case TemplightDump: return std::make_unique<TemplightDumpAction>(); + case InitOnly: + return std::make_unique<InitOnlyAction>(); + case ParseSyntaxOnly: + return std::make_unique<SyntaxOnlyAction>(); + case ModuleFileInfo: + return std::make_unique<DumpModuleInfoAction>(); + case VerifyPCH: + return std::make_unique<VerifyPCHAction>(); + case TemplightDump: + return std::make_unique<TemplightDumpAction>(); case PluginAction: { for (const FrontendPluginRegistry::entry &Plugin : @@ -94,11 +114,12 @@ CreateFrontendBaseAction(CompilerInstance &CI) { } CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) - << CI.getFrontendOpts().ActionName; + << CI.getFrontendOpts().ActionName; return nullptr; } - case PrintPreamble: return std::make_unique<PrintPreambleAction>(); + case PrintPreamble: + return std::make_unique<PrintPreambleAction>(); case PrintPreprocessedInput: { if (CI.getPreprocessorOutputOpts().RewriteIncludes || CI.getPreprocessorOutputOpts().RewriteImports) @@ -106,31 +127,42 @@ CreateFrontendBaseAction(CompilerInstance &CI) { return std::make_unique<PrintPreprocessedAction>(); } - case RewriteMacros: return std::make_unique<RewriteMacrosAction>(); - case RewriteTest: return std::make_unique<RewriteTestAction>(); + case RewriteMacros: + return std::make_unique<RewriteMacrosAction>(); + case RewriteTest: + return std::make_unique<RewriteTestAction>(); #if CLANG_ENABLE_OBJC_REWRITER - case RewriteObjC: return std::make_unique<RewriteObjCAction>(); + case RewriteObjC: + return std::make_unique<RewriteObjCAction>(); #else - case RewriteObjC: Action = "RewriteObjC"; break; + case RewriteObjC: + Action = "RewriteObjC"; + break; #endif #if CLANG_ENABLE_ARCMT case MigrateSource: return std::make_unique<arcmt::MigrateSourceAction>(); #else - case MigrateSource: Action = "MigrateSource"; break; + case MigrateSource: + Action = "MigrateSource"; + break; #endif #if CLANG_ENABLE_STATIC_ANALYZER - case RunAnalysis: return std::make_unique<ento::AnalysisAction>(); + case RunAnalysis: + return std::make_unique<ento::AnalysisAction>(); #else - case RunAnalysis: Action = "RunAnalysis"; break; + case RunAnalysis: + Action = "RunAnalysis"; + break; #endif - case RunPreprocessorOnly: return std::make_unique<PreprocessOnlyAction>(); + case RunPreprocessorOnly: + return std::make_unique<PreprocessOnlyAction>(); case PrintDependencyDirectivesSourceMinimizerOutput: return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>(); } -#if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \ - || !CLANG_ENABLE_OBJC_REWRITER +#if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER || \ + !CLANG_ENABLE_OBJC_REWRITER CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action; return 0; #else @@ -138,8 +170,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) { #endif } -std::unique_ptr<FrontendAction> -CreateFrontendAction(CompilerInstance &CI) { +std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &CI) { // Create the underlying action. std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI); if (!Act) @@ -165,17 +196,15 @@ CreateFrontendAction(CompilerInstance &CI) { Act = std::make_unique<arcmt::ModifyAction>(std::move(Act)); break; case FrontendOptions::ARCMT_Migrate: - Act = std::make_unique<arcmt::MigrateAction>(std::move(Act), - FEOpts.MTMigrateDir, - FEOpts.ARCMTMigrateReportOut, - FEOpts.ARCMTMigrateEmitARCErrors); + Act = std::make_unique<arcmt::MigrateAction>( + std::move(Act), FEOpts.MTMigrateDir, FEOpts.ARCMTMigrateReportOut, + FEOpts.ARCMTMigrateEmitARCErrors); break; } if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { - Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act), - FEOpts.MTMigrateDir, - FEOpts.ObjCMTAction); + Act = std::make_unique<arcmt::ObjCMigrateAction>( + std::move(Act), FEOpts.MTMigrateDir, FEOpts.ObjCMTAction); } } #endif @@ -195,8 +224,8 @@ CreateFrontendAction(CompilerInstance &CI) { // If there are any AST files to merge, create a frontend action // adaptor to perform the merge. if (!FEOpts.ASTMergeFiles.empty()) - Act = std::make_unique<ASTMergeAction>(std::move(Act), - FEOpts.ASTMergeFiles); + Act = + std::make_unique<ASTMergeAction>(std::move(Act), FEOpts.ASTMergeFiles); return Act; } @@ -228,7 +257,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) { // This should happen AFTER plugins have been loaded! if (!Clang->getFrontendOpts().LLVMArgs.empty()) { unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); - auto Args = std::make_unique<const char*[]>(NumArgs + 2); + auto Args = std::make_unique<const char *[]>(NumArgs + 2); Args[0] = "clang (LLVM option parsing)"; for (unsigned i = 0; i != NumArgs; ++i) Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); @@ -268,6 +297,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) { } #endif + // Cratels: // If there were errors in processing arguments, don't do anything else. if (Clang->getDiagnostics().hasErrorOccurred()) return false; diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index b5c6be3c557bb..12bed1c607a6b 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -63,7 +63,7 @@ using namespace llvm::opt; static void LLVMErrorHandler(void *UserData, const char *Message, bool GenCrashDiag) { - DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); + DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine *>(UserData); Diags.Report(diag::err_fe_error_backend) << Message; @@ -139,7 +139,7 @@ static int PrintSupportedExtensions(std::string TargetStr) { const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); const llvm::ArrayRef<llvm::SubtargetFeatureKV> Features = - MCInfo->getAllProcessorFeatures(); + MCInfo->getAllProcessorFeatures(); llvm::StringMap<llvm::StringRef> DescMap; for (const llvm::SubtargetFeatureKV &feature : Features) @@ -161,6 +161,7 @@ static int PrintSupportedExtensions(std::string TargetStr) { return 0; } +// 通过指定-cc1 option 之后,LLVM 最终会执行到这个方法作为编译器前端来工作 int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { ensureSufficientStack(); @@ -208,7 +209,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && Clang->getHeaderSearchOpts().ResourceDir.empty()) Clang->getHeaderSearchOpts().ResourceDir = - CompilerInvocation::GetResourcesPath(Argv0, MainAddr); + CompilerInvocation::GetResourcesPath(Argv0, MainAddr); // Create the actual diagnostics engine. Clang->createDiagnostics(); @@ -217,8 +218,8 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { // Set an error handler, so that any LLVM backend diagnostics go through our // error handler. - llvm::install_fatal_error_handler(LLVMErrorHandler, - static_cast<void*>(&Clang->getDiagnostics())); + llvm::install_fatal_error_handler( + LLVMErrorHandler, static_cast<void *>(&Clang->getDiagnostics())); DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics()); if (!Success) { diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp index 83b5bbb71f521..f22ccda01a9e1 100644 --- a/clang/tools/driver/driver.cpp +++ b/clang/tools/driver/driver.cpp @@ -69,7 +69,7 @@ std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) { // This just needs to be some symbol in the binary; C++ doesn't // allow taking the address of ::main however. - void *P = (void*) (intptr_t) GetExecutablePath; + void *P = (void *)(intptr_t)GetExecutablePath; return llvm::sys::fs::getMainExecutable(Argv0, P); } @@ -102,10 +102,10 @@ static void insertTargetAndModeArgs(const ParsedClangName &NameParts, } if (NameParts.TargetIsValid) { - const char *arr[] = {"-target", GetStableCStr(SavedStrings, - NameParts.TargetPrefix)}; - ArgVector.insert(ArgVector.begin() + InsertionPoint, - std::begin(arr), std::end(arr)); + const char *arr[] = {"-target", + GetStableCStr(SavedStrings, NameParts.TargetPrefix)}; + ArgVector.insert(ArgVector.begin() + InsertionPoint, std::begin(arr), + std::end(arr)); } } @@ -225,11 +225,27 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV, return 1; } +// Cratels: build 目录下会生成一个 main 方法。在该方法中调用 clang_main +// 这里是所有程序的入口 + +// int clang_main(int argc, char **, const llvm::ToolContext &); + +// int main(int argc, char **argv) { // Cratels: 程序入口 +// llvm::InitLLVM X(argc, argv); // Cratels: 初始化 LLVM +// return clang_main(argc, argv, {argv[0], nullptr, false}); +// } + +// Cratels: clang 驱动器调用的第一个工具是 clang +// 本身,给以参数-cc1,关闭编译器驱动器模式而开启编译器模式。 + int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) { noteBottomOfStack(); + llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL " and include the crash backtrace, preprocessed " "source, and associated run script.\n"); + + // Cratels: 自定义 vector 来存放编译参数 SmallVector<const char *, 256> Args(Argv, Argv + Argc); if (llvm::sys::Process::FixupStandardFileDescriptors()) @@ -316,8 +332,8 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) { IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(Args); - TextDiagnosticPrinter *DiagClient - = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); + TextDiagnosticPrinter *DiagClient = + new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); FixupDiagPrefixExeName(DiagClient, ProgName); IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); @@ -423,8 +439,8 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) { if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")) llvm::dbgs() << llvm::getBugReportMsg(); if (FailingCommand != nullptr && - TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel, - *C, *FailingCommand)) + TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel, + *C, *FailingCommand)) Res = 1; Diags.getClient()->finish(); diff --git a/kick-off.md b/kick-off.md new file mode 100644 index 0000000000000..97084536fe1eb --- /dev/null +++ b/kick-off.md @@ -0,0 +1,8 @@ +# configuration +> cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug + +LLVM的工程很大,源码的源头一般认为是 llvm 文件夹,可以看到这里也是从其开始寻找 cmake 文件的。 +当前要求配置时必须制定 build 类型。 + +# build +> cmake --build build diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index d511376e18ba5..8f8cfbcf6a1e9 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -1,24 +1,54 @@ +# -S 指定目录为 llvm 时,cmake 就会从当前路径中查找 CMakeLists.txt 并执行 +# +# 项目入口处 +# # See docs/CMake.html for instructions about how to build LLVM with CMake. +# 指定当前工程支持的 cmake 语法的最小版本。该之前的语法旧语法就不再支持了。 cmake_minimum_required(VERSION 3.20.0) +# 设置变量LLVM_COMMON_CMAKE_UTILS执行外层 cmake 文件夹的路径。 +# 该路径下面的 Modules 文件夹下有很多 .cmake 文件后续可能会用。 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) +message(${LLVM_COMMON_CMAKE_UTILS}) + +# message的信息种类: +# (无) = 重要消息; +# STATUS = 非重要消息; +# WARNING = CMake 警告, 会继续执行; +# AUTHOR_WARNING = CMake 警告 (dev), 会继续执行; +# SEND_ERROR = CMake 错误, 继续执行,但是会跳过生成的步骤; +# FATAL_ERROR = CMake 错误, 终止所有处理过程; + + +message("将 policy 文件引入当前 cmake 文件") include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake NO_POLICY_SCOPE) # Builds with custom install names and installation rpath setups may not work # in the build tree. Allow these cases to use CMake's default build tree # behavior by setting `LLVM_NO_INSTALL_NAME_DIR_FOR_BUILD_T... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/93500 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits