1 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file holds ExecuteCompilerInvocation(). It is split into its own file to 10 // minimize the impact of pulling in essentially everything else in Flang. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "flang/Frontend/CompilerInstance.h" 15 #include "flang/Frontend/FrontendActions.h" 16 #include "clang/Driver/Options.h" 17 #include "llvm/Option/OptTable.h" 18 #include "llvm/Option/Option.h" 19 #include "llvm/Support/BuryPointer.h" 20 #include "llvm/Support/CommandLine.h" 21 22 namespace Fortran::frontend { 23 24 static std::unique_ptr<FrontendAction> CreateFrontendBaseAction( 25 CompilerInstance &ci) { 26 27 ActionKind ak = ci.frontendOpts().programAction_; 28 switch (ak) { 29 case InputOutputTest: 30 return std::make_unique<InputOutputTestAction>(); 31 break; 32 case PrintPreprocessedInput: 33 return std::make_unique<PrintPreprocessedAction>(); 34 break; 35 case ParseSyntaxOnly: 36 return std::make_unique<ParseSyntaxOnlyAction>(); 37 break; 38 default: 39 break; 40 // TODO: 41 // case RunPreprocessor: 42 // case ParserSyntaxOnly: 43 // case EmitLLVM: 44 // case EmitLLVMOnly: 45 // case EmitCodeGenOnly: 46 // (...) 47 } 48 return 0; 49 } 50 51 std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) { 52 // Create the underlying action. 53 std::unique_ptr<FrontendAction> act = CreateFrontendBaseAction(ci); 54 if (!act) 55 return nullptr; 56 57 return act; 58 } 59 bool ExecuteCompilerInvocation(CompilerInstance *flang) { 60 // Honor -help. 61 if (flang->frontendOpts().showHelp_) { 62 clang::driver::getDriverOptTable().PrintHelp(llvm::outs(), 63 "flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler", 64 /*Include=*/clang::driver::options::FC1Option, 65 /*Exclude=*/llvm::opt::DriverFlag::HelpHidden, 66 /*ShowAllAliases=*/false); 67 return true; 68 } 69 70 // Honor -version. 71 if (flang->frontendOpts().showVersion_) { 72 llvm::cl::PrintVersionMessage(); 73 return true; 74 } 75 76 // Create and execute the frontend action. 77 std::unique_ptr<FrontendAction> act(CreateFrontendAction(*flang)); 78 if (!act) 79 return false; 80 81 bool success = flang->ExecuteAction(*act); 82 return success; 83 } 84 85 } // namespace Fortran::frontend 86