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