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   case EmitObj:
38     return std::make_unique<EmitObjAction>();
39     break;
40   case DebugUnparse:
41     return std::make_unique<DebugUnparseAction>();
42     break;
43   case DebugUnparseWithSymbols:
44     return std::make_unique<DebugUnparseWithSymbolsAction>();
45     break;
46   default:
47     break;
48     // TODO:
49     // case RunPreprocessor:
50     // case ParserSyntaxOnly:
51     // case EmitLLVM:
52     // case EmitLLVMOnly:
53     // case EmitCodeGenOnly:
54     // (...)
55   }
56   return 0;
57 }
58 
59 std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) {
60   // Create the underlying action.
61   std::unique_ptr<FrontendAction> act = CreateFrontendBaseAction(ci);
62   if (!act)
63     return nullptr;
64 
65   return act;
66 }
67 bool ExecuteCompilerInvocation(CompilerInstance *flang) {
68   // Honor -help.
69   if (flang->frontendOpts().showHelp_) {
70     clang::driver::getDriverOptTable().PrintHelp(llvm::outs(),
71         "flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
72         /*Include=*/clang::driver::options::FC1Option,
73         /*Exclude=*/llvm::opt::DriverFlag::HelpHidden,
74         /*ShowAllAliases=*/false);
75     return true;
76   }
77 
78   // Honor -version.
79   if (flang->frontendOpts().showVersion_) {
80     llvm::cl::PrintVersionMessage();
81     return true;
82   }
83 
84   // Create and execute the frontend action.
85   std::unique_ptr<FrontendAction> act(CreateFrontendAction(*flang));
86   if (!act)
87     return false;
88 
89   bool success = flang->ExecuteAction(*act);
90   return success;
91 }
92 
93 } // namespace Fortran::frontend
94