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