xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // Construct a compiler invocation object for command line driver arguments
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "clang/Frontend/Utils.h"
14*0b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
15*0b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
16*0b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
17*0b57cec5SDimitry Andric #include "clang/Driver/Action.h"
18*0b57cec5SDimitry Andric #include "clang/Driver/Options.h"
19*0b57cec5SDimitry Andric #include "clang/Driver/Tool.h"
20*0b57cec5SDimitry Andric #include "clang/Frontend/CompilerInstance.h"
21*0b57cec5SDimitry Andric #include "clang/Frontend/FrontendDiagnostic.h"
22*0b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/Host.h"
24*0b57cec5SDimitry Andric using namespace clang;
25*0b57cec5SDimitry Andric using namespace llvm::opt;
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric /// createInvocationFromCommandLine - Construct a compiler invocation object for
28*0b57cec5SDimitry Andric /// a command line argument vector.
29*0b57cec5SDimitry Andric ///
30*0b57cec5SDimitry Andric /// \return A CompilerInvocation, or 0 if none was built for the given
31*0b57cec5SDimitry Andric /// argument vector.
32*0b57cec5SDimitry Andric std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
33*0b57cec5SDimitry Andric     ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
34*0b57cec5SDimitry Andric     IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
35*0b57cec5SDimitry Andric   if (!Diags.get()) {
36*0b57cec5SDimitry Andric     // No diagnostics engine was provided, so create our own diagnostics object
37*0b57cec5SDimitry Andric     // with the default options.
38*0b57cec5SDimitry Andric     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
39*0b57cec5SDimitry Andric   }
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric   SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric   // FIXME: Find a cleaner way to force the driver into restricted modes.
44*0b57cec5SDimitry Andric   Args.push_back("-fsyntax-only");
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric   // FIXME: We shouldn't have to pass in the path info.
47*0b57cec5SDimitry Andric   driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
48*0b57cec5SDimitry Andric                            *Diags, VFS);
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric   // Don't check that inputs exist, they may have been remapped.
51*0b57cec5SDimitry Andric   TheDriver.setCheckInputsExist(false);
52*0b57cec5SDimitry Andric 
53*0b57cec5SDimitry Andric   std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
54*0b57cec5SDimitry Andric   if (!C)
55*0b57cec5SDimitry Andric     return nullptr;
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric   // Just print the cc1 options if -### was present.
58*0b57cec5SDimitry Andric   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
59*0b57cec5SDimitry Andric     C->getJobs().Print(llvm::errs(), "\n", true);
60*0b57cec5SDimitry Andric     return nullptr;
61*0b57cec5SDimitry Andric   }
62*0b57cec5SDimitry Andric 
63*0b57cec5SDimitry Andric   // We expect to get back exactly one command job, if we didn't something
64*0b57cec5SDimitry Andric   // failed. Offload compilation is an exception as it creates multiple jobs. If
65*0b57cec5SDimitry Andric   // that's the case, we proceed with the first job. If caller needs a
66*0b57cec5SDimitry Andric   // particular job, it should be controlled via options (e.g.
67*0b57cec5SDimitry Andric   // --cuda-{host|device}-only for CUDA) passed to the driver.
68*0b57cec5SDimitry Andric   const driver::JobList &Jobs = C->getJobs();
69*0b57cec5SDimitry Andric   bool OffloadCompilation = false;
70*0b57cec5SDimitry Andric   if (Jobs.size() > 1) {
71*0b57cec5SDimitry Andric     for (auto &A : C->getActions()){
72*0b57cec5SDimitry Andric       // On MacOSX real actions may end up being wrapped in BindArchAction
73*0b57cec5SDimitry Andric       if (isa<driver::BindArchAction>(A))
74*0b57cec5SDimitry Andric         A = *A->input_begin();
75*0b57cec5SDimitry Andric       if (isa<driver::OffloadAction>(A)) {
76*0b57cec5SDimitry Andric         OffloadCompilation = true;
77*0b57cec5SDimitry Andric         break;
78*0b57cec5SDimitry Andric       }
79*0b57cec5SDimitry Andric     }
80*0b57cec5SDimitry Andric   }
81*0b57cec5SDimitry Andric   if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
82*0b57cec5SDimitry Andric       (Jobs.size() > 1 && !OffloadCompilation)) {
83*0b57cec5SDimitry Andric     SmallString<256> Msg;
84*0b57cec5SDimitry Andric     llvm::raw_svector_ostream OS(Msg);
85*0b57cec5SDimitry Andric     Jobs.Print(OS, "; ", true);
86*0b57cec5SDimitry Andric     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
87*0b57cec5SDimitry Andric     return nullptr;
88*0b57cec5SDimitry Andric   }
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric   const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
91*0b57cec5SDimitry Andric   if (StringRef(Cmd.getCreator().getName()) != "clang") {
92*0b57cec5SDimitry Andric     Diags->Report(diag::err_fe_expected_clang_command);
93*0b57cec5SDimitry Andric     return nullptr;
94*0b57cec5SDimitry Andric   }
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric   const ArgStringList &CCArgs = Cmd.getArguments();
97*0b57cec5SDimitry Andric   auto CI = llvm::make_unique<CompilerInvocation>();
98*0b57cec5SDimitry Andric   if (!CompilerInvocation::CreateFromArgs(*CI,
99*0b57cec5SDimitry Andric                                      const_cast<const char **>(CCArgs.data()),
100*0b57cec5SDimitry Andric                                      const_cast<const char **>(CCArgs.data()) +
101*0b57cec5SDimitry Andric                                      CCArgs.size(),
102*0b57cec5SDimitry Andric                                      *Diags))
103*0b57cec5SDimitry Andric     return nullptr;
104*0b57cec5SDimitry Andric   return CI;
105*0b57cec5SDimitry Andric }
106