xref: /llvm-project/flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp (revision c1f068b8f1d5f6ca75acc07fdedf2e7416e834ec)
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 DebugUnparseNoSema:
44     return std::make_unique<DebugUnparseNoSemaAction>();
45     break;
46   case DebugUnparseWithSymbols:
47     return std::make_unique<DebugUnparseWithSymbolsAction>();
48     break;
49   case DebugDumpSymbols:
50     return std::make_unique<DebugDumpSymbolsAction>();
51     break;
52   case DebugDumpParseTree:
53     return std::make_unique<DebugDumpParseTreeAction>();
54     break;
55   case DebugDumpParseTreeNoSema:
56     return std::make_unique<DebugDumpParseTreeNoSemaAction>();
57     break;
58   case DebugDumpAll:
59     return std::make_unique<DebugDumpAllAction>();
60     break;
61   case DebugDumpProvenance:
62     return std::make_unique<DebugDumpProvenanceAction>();
63     break;
64   case DebugDumpParsingLog:
65     return std::make_unique<DebugDumpParsingLogAction>();
66     break;
67   case DebugMeasureParseTree:
68     return std::make_unique<DebugMeasureParseTreeAction>();
69     break;
70   case DebugPreFIRTree:
71     return std::make_unique<DebugPreFIRTreeAction>();
72     break;
73   case GetDefinition:
74     return std::make_unique<GetDefinitionAction>();
75     break;
76   case GetSymbolsSources:
77     return std::make_unique<GetSymbolsSourcesAction>();
78     break;
79   case InitOnly:
80     return std::make_unique<InitOnlyAction>();
81     break;
82   default:
83     break;
84     // TODO:
85     // case RunPreprocessor:
86     // case ParserSyntaxOnly:
87     // case EmitLLVM:
88     // case EmitLLVMOnly:
89     // case EmitCodeGenOnly:
90     // (...)
91   }
92   return 0;
93 }
94 
95 std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) {
96   // Create the underlying action.
97   std::unique_ptr<FrontendAction> act = CreateFrontendBaseAction(ci);
98   if (!act)
99     return nullptr;
100 
101   return act;
102 }
103 bool ExecuteCompilerInvocation(CompilerInstance *flang) {
104   // Honor -help.
105   if (flang->frontendOpts().showHelp_) {
106     clang::driver::getDriverOptTable().printHelp(llvm::outs(),
107         "flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
108         /*Include=*/clang::driver::options::FC1Option,
109         /*Exclude=*/llvm::opt::DriverFlag::HelpHidden,
110         /*ShowAllAliases=*/false);
111     return true;
112   }
113 
114   // Honor -version.
115   if (flang->frontendOpts().showVersion_) {
116     llvm::cl::PrintVersionMessage();
117     return true;
118   }
119 
120   // Create and execute the frontend action.
121   std::unique_ptr<FrontendAction> act(CreateFrontendAction(*flang));
122   if (!act)
123     return false;
124 
125   bool success = flang->ExecuteAction(*act);
126   return success;
127 }
128 
129 } // namespace Fortran::frontend
130