clang 22.0.0git
CreateInvocationFromCommandLine.cpp
Go to the documentation of this file.
1//===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
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// Construct a compiler invocation object for command line driver arguments
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/Driver/Action.h"
16#include "clang/Driver/Driver.h"
18#include "clang/Driver/Tool.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Option/ArgList.h"
25#include "llvm/Support/VirtualFileSystem.h"
26#include "llvm/TargetParser/Host.h"
27using namespace clang;
28using namespace llvm::opt;
29
30std::unique_ptr<CompilerInvocation>
33 assert(!ArgList.empty());
34 std::optional<DiagnosticOptions> LocalDiagOpts;
36 if (Opts.Diags) {
37 Diags = std::move(Opts.Diags);
38 } else {
39 LocalDiagOpts.emplace();
41 Opts.VFS ? *Opts.VFS : *llvm::vfs::getRealFileSystem(), *LocalDiagOpts);
42 }
43
45
46 // FIXME: Find a cleaner way to force the driver into restricted modes.
47 Args.insert(
48 llvm::find_if(
49 Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
50 "-fsyntax-only");
51
52 // FIXME: We shouldn't have to pass in the path info.
53 driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
54 "clang LLVM compiler", Opts.VFS);
55
56 // Don't check that inputs exist, they may have been remapped.
57 TheDriver.setCheckInputsExist(false);
59
60 std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
61 if (!C)
62 return nullptr;
63
64 if (C->getArgs().hasArg(driver::options::OPT_fdriver_only))
65 return nullptr;
66
67 // Just print the cc1 options if -### was present.
68 if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
69 C->getJobs().Print(llvm::errs(), "\n", true);
70 return nullptr;
71 }
72
73 // We expect to get back exactly one command job, if we didn't something
74 // failed. Offload compilation is an exception as it creates multiple jobs. If
75 // that's the case, we proceed with the first job. If caller needs a
76 // particular job, it should be controlled via options (e.g.
77 // --cuda-{host|device}-only for CUDA) passed to the driver.
78 const driver::JobList &Jobs = C->getJobs();
79 bool OffloadCompilation = false;
80 if (Jobs.size() > 1) {
81 for (auto &A : C->getActions()){
82 // On MacOSX real actions may end up being wrapped in BindArchAction
83 if (isa<driver::BindArchAction>(A))
84 A = *A->input_begin();
85 if (isa<driver::OffloadAction>(A)) {
86 OffloadCompilation = true;
87 break;
88 }
89 }
90 }
91
92 bool PickFirstOfMany = OffloadCompilation || Opts.RecoverOnError;
93 if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
95 llvm::raw_svector_ostream OS(Msg);
96 Jobs.Print(OS, "; ", true);
97 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
98 return nullptr;
99 }
100 auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
101 return StringRef(Cmd.getCreator().getName()) == "clang";
102 });
103 if (Cmd == Jobs.end()) {
104 Diags->Report(diag::err_fe_expected_clang_command);
105 return nullptr;
106 }
107
108 const ArgStringList &CCArgs = Cmd->getArguments();
109 if (Opts.CC1Args)
110 *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
111 auto CI = std::make_unique<CompilerInvocation>();
112 if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
113 !Opts.RecoverOnError)
114 return nullptr;
115 return CI;
116}
CompileCommand Cmd
void createDiagnostics(llvm::vfs::FileSystem &VFS, DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Command - An executable path/name and argument vector to execute.
Definition: Job.h:106
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:99
void setCheckInputsExist(bool Value)
Definition: Driver.h:436
void setProbePrecompiled(bool Value)
Definition: Driver.h:439
Compilation * BuildCompilation(ArrayRef< const char * > Args)
BuildCompilation - Construct a compilation object for a command line argument vector.
Definition: Driver.cpp:1476
JobList - A sequence of jobs to perform.
Definition: Job.h:260
size_type size() const
Definition: Job.h:283
iterator end()
Definition: Job.h:286
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:449
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< CompilerInvocation > createInvocation(ArrayRef< const char * > Args, CreateInvocationOptions Opts={})
Interpret clang arguments in preparation to parse a file.
Optional inputs to createInvocation.
Definition: Utils.h:195
IntrusiveRefCntPtr< DiagnosticsEngine > Diags
Receives diagnostics encountered while parsing command-line flags.
Definition: Utils.h:198
bool ProbePrecompiled
Allow the driver to probe the filesystem for PCH files.
Definition: Utils.h:211
bool RecoverOnError
Whether to attempt to produce a non-null (possibly incorrect) invocation if any errors were encounter...
Definition: Utils.h:206
IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Used e.g.
Definition: Utils.h:202
std::vector< std::string > * CC1Args
If set, the target is populated with the cc1 args produced by the driver.
Definition: Utils.h:214