clang  14.0.0git
Flang.cpp
Go to the documentation of this file.
1 //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===//
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 
10 #include "Flang.h"
11 #include "CommonArgs.h"
12 
13 #include "clang/Driver/Options.h"
14 
15 #include <cassert>
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 void Flang::AddFortranDialectOptions(const ArgList &Args,
23  ArgStringList &CmdArgs) const {
24  Args.AddAllArgs(
25  CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form,
26  options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp,
27  options::OPT_fopenacc, options::OPT_finput_charset_EQ,
28  options::OPT_fimplicit_none, options::OPT_fno_implicit_none,
29  options::OPT_fbackslash, options::OPT_fno_backslash,
30  options::OPT_flogical_abbreviations,
31  options::OPT_fno_logical_abbreviations,
32  options::OPT_fxor_operator, options::OPT_fno_xor_operator,
33  options::OPT_falternative_parameter_statement,
34  options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8,
35  options::OPT_fdefault_double_8, options::OPT_flarge_sizes});
36 }
37 
38 void Flang::AddPreprocessingOptions(const ArgList &Args,
39  ArgStringList &CmdArgs) const {
40  Args.AddAllArgs(CmdArgs,
41  {options::OPT_P, options::OPT_D, options::OPT_U,
42  options::OPT_I, options::OPT_cpp, options::OPT_nocpp});
43 }
44 
45 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
46  Args.AddAllArgs(CmdArgs,
47  {options::OPT_module_dir, options::OPT_fdebug_module_writer,
48  options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
49  options::OPT_std_EQ, options::OPT_W_Joined});
50 }
51 
53  const InputInfo &Output, const InputInfoList &Inputs,
54  const ArgList &Args, const char *LinkingOutput) const {
55  const auto &TC = getToolChain();
56  // TODO: Once code-generation is available, this will need to be commented
57  // out.
58  // const llvm::Triple &Triple = TC.getEffectiveTriple();
59  // const std::string &TripleStr = Triple.getTriple();
60 
61  ArgStringList CmdArgs;
62 
63  // Invoke ourselves in -fc1 mode.
64  CmdArgs.push_back("-fc1");
65 
66  // TODO: Once code-generation is available, this will need to be commented
67  // out.
68  // Add the "effective" target triple.
69  // CmdArgs.push_back("-triple");
70  // CmdArgs.push_back(Args.MakeArgString(TripleStr));
71 
72  if (isa<PreprocessJobAction>(JA)) {
73  CmdArgs.push_back("-E");
74  } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
75  if (JA.getType() == types::TY_Nothing) {
76  CmdArgs.push_back("-fsyntax-only");
77  } else if (JA.getType() == types::TY_AST) {
78  CmdArgs.push_back("-emit-ast");
79  } else if (JA.getType() == types::TY_LLVM_IR ||
80  JA.getType() == types::TY_LTO_IR) {
81  CmdArgs.push_back("-emit-llvm");
82  } else if (JA.getType() == types::TY_LLVM_BC ||
83  JA.getType() == types::TY_LTO_BC) {
84  CmdArgs.push_back("-emit-llvm-bc");
85  } else if (JA.getType() == types::TY_PP_Asm) {
86  CmdArgs.push_back("-S");
87  } else {
88  assert(false && "Unexpected output type!");
89  }
90  } else if (isa<AssembleJobAction>(JA)) {
91  CmdArgs.push_back("-emit-obj");
92  } else {
93  assert(false && "Unexpected action class for Flang tool.");
94  }
95 
96  const InputInfo &Input = Inputs[0];
97  types::ID InputType = Input.getType();
98 
99  // Add preprocessing options like -I, -D, etc. if we are using the
100  // preprocessor (i.e. skip when dealing with e.g. binary files).
102  AddPreprocessingOptions(Args, CmdArgs);
103 
104  AddFortranDialectOptions(Args, CmdArgs);
105 
106  // Add other compile options
107  AddOtherOptions(Args, CmdArgs);
108 
109  // Forward -Xflang arguments to -fc1
110  Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
111 
112  if (Output.isFilename()) {
113  CmdArgs.push_back("-o");
114  CmdArgs.push_back(Output.getFilename());
115  } else {
116  assert(Output.isNothing() && "Invalid output.");
117  }
118 
119  assert(Input.isFilename() && "Invalid input.");
120  CmdArgs.push_back(Input.getFilename());
121 
122  const auto& D = C.getDriver();
123  // TODO: Replace flang-new with flang once the new driver replaces the
124  // throwaway driver
125  const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
126  C.addCommand(std::make_unique<Command>(JA, *this,
128  Exec, CmdArgs, Inputs, Output));
129 }
130 
131 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
132 
clang::driver::tools::Flang::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Flang.cpp:52
clang::driver::ResponseFileSupport::AtFileUTF8
static constexpr ResponseFileSupport AtFileUTF8()
Definition: Job.h:85
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::tools
Definition: AIX.h:17
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
Options.h
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:75
clang::driver::tools::Flang::Flang
Flang(const ToolChain &TC)
Definition: Flang.cpp:131
clang::driver::Action::getType
types::ID getType() const
Definition: Action.h:143
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::types::TY_INVALID
@ TY_INVALID
Definition: Types.h:24
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::types::ID
ID
Definition: Types.h:23
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::InputInfo::getType
types::ID getType() const
Definition: InputInfo.h:77
clang::driver::tools::Flang::~Flang
~Flang() override
Definition: Flang.cpp:133
clang::driver::types::getPreprocessedType
ID getPreprocessedType(ID Id)
getPreprocessedType - Get the ID of the type for this input when it has been preprocessed,...
Definition: Types.cpp:56
Flang.h
clang::driver
Definition: Action.h:31
clang::driver::JobAction
Definition: Action.h:380