//===--- ExecuteCompilerInvocation.cpp ------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file holds ExecuteCompilerInvocation(). It is split into its own file to // minimize the impact of pulling in essentially everything else in Flang. // //===----------------------------------------------------------------------===// #include "flang/Frontend/CompilerInstance.h" #include "flang/Frontend/FrontendActions.h" #include "flang/Frontend/FrontendAction.h" #include "flang/Frontend/FrontendPluginRegistry.h" #include "clang/Driver/Options.h" #include "llvm/Option/OptTable.h" #include "llvm/Option/Option.h" #include "llvm/Support/BuryPointer.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/DynamicLibrary.h" namespace Fortran::frontend { static std::unique_ptr CreateFrontendBaseAction( CompilerInstance &ci) { ActionKind ak = ci.frontendOpts().programAction_; switch (ak) { case InputOutputTest: return std::make_unique(); break; case PrintPreprocessedInput: return std::make_unique(); break; case ParseSyntaxOnly: return std::make_unique(); case EmitObj: return std::make_unique(); break; case DebugUnparse: return std::make_unique(); break; case DebugUnparseNoSema: return std::make_unique(); break; case DebugUnparseWithSymbols: return std::make_unique(); break; case DebugDumpSymbols: return std::make_unique(); break; case DebugDumpParseTree: return std::make_unique(); break; case DebugDumpParseTreeNoSema: return std::make_unique(); break; case DebugDumpAll: return std::make_unique(); break; case DebugDumpProvenance: return std::make_unique(); break; case DebugDumpParsingLog: return std::make_unique(); break; case DebugMeasureParseTree: return std::make_unique(); break; case DebugPreFIRTree: return std::make_unique(); break; case GetDefinition: return std::make_unique(); break; case GetSymbolsSources: return std::make_unique(); break; case InitOnly: return std::make_unique(); break; case PluginAction: { llvm::outs() << "---------- (case: PluginAction) --------\n"; llvm::outs() << " Plugin Action: " << ci.frontendOpts().ActionName << "\n"; for (const FrontendPluginRegistry::entry &plugin : FrontendPluginRegistry::entries()) { llvm::outs() << " " << plugin.getName() << "\t-- " << plugin.getDesc() << "\n"; if (plugin.getName() == ci.frontendOpts().ActionName) { llvm::outs() << "We have found the plugin name!! :-)\n"; std::unique_ptr P(plugin.instantiate()); return std::move(P); } } unsigned diagID = ci.diagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "unable to find plugin '%0'"); ci.diagnostics().Report(diagID) << ci.frontendOpts().ActionName; return nullptr; } default: break; // TODO: // case RunPreprocessor: // case ParserSyntaxOnly: // case EmitLLVM: // case EmitLLVMOnly: // case EmitCodeGenOnly: // (...) } return 0; } /// <<< TEMP Plugin Example class HelloWorldFlangPlugin : public PluginParseTreeAction { protected: void ExecuteAction() override { llvm::outs() << "Hello World from your new plugin (Hello World)\n"; } }; class HelloTwoFlangPlugin : public PluginParseTreeAction { protected: void ExecuteAction() override { llvm::outs() << "Hello World from your new plugin (Hello Two)\n"; } }; /// <<<<< TEMP Plugin Example std::unique_ptr CreateFrontendAction(CompilerInstance &ci) { // Create the underlying action. std::unique_ptr act = CreateFrontendBaseAction(ci); if (!act) return nullptr; return act; } bool ExecuteCompilerInvocation(CompilerInstance *flang) { // Honor -help. if (flang->frontendOpts().showHelp_) { clang::driver::getDriverOptTable().printHelp(llvm::outs(), "flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler", /*Include=*/clang::driver::options::FC1Option, /*Exclude=*/llvm::opt::DriverFlag::HelpHidden, /*ShowAllAliases=*/false); return true; } // Honor -version. if (flang->frontendOpts().showVersion_) { llvm::cl::PrintVersionMessage(); return true; } llvm::outs() << "------ (ExecuteCompilerInvocation) -----\n"; // Load any requested plugins. for (const std::string &Path : flang->frontendOpts().plugins) { llvm::outs() << " Load :: Path >> " << Path << "\n"; std::string Error; if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) { unsigned diagID = flang->diagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "unable to load plugin '%0': '%1'"); flang->diagnostics().Report(diagID) << Path << Error; } } llvm::outs() << " Plugin Registry List >>\n"; for (const FrontendPluginRegistry::entry &plugin : FrontendPluginRegistry::entries()) { llvm::outs() << plugin.getName() << " -- " << plugin.getDesc() << "\n"; } llvm::outs() << " << Plugin Registry List\n"; static FrontendPluginRegistry::Add X("-hello-wor", "simple Plugin example"); static FrontendPluginRegistry::Add Y("hellotwo", "another print plugin example"); llvm::outs() << "----- (\\ExecuteCompilerInvocation) -----\n"; // If there were errors in processing arguments, don't do anything else. if (flang->diagnostics().hasErrorOccurred()) return false; // Create and execute the frontend action. std::unique_ptr act(CreateFrontendAction(*flang)); if (!act) return false; bool success = flang->ExecuteAction(*act); return success; } } // namespace Fortran::frontend