xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Construct a compiler invocation object for command line driver arguments
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
14fe6060f1SDimitry Andric #include "clang/Driver/Action.h"
150b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
160b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
170b57cec5SDimitry Andric #include "clang/Driver/Options.h"
180b57cec5SDimitry Andric #include "clang/Driver/Tool.h"
190b57cec5SDimitry Andric #include "clang/Frontend/CompilerInstance.h"
200b57cec5SDimitry Andric #include "clang/Frontend/FrontendDiagnostic.h"
21fe6060f1SDimitry Andric #include "clang/Frontend/Utils.h"
22fe6060f1SDimitry Andric #include "llvm/ADT/STLExtras.h"
23fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h"
240b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
250b57cec5SDimitry Andric #include "llvm/Support/Host.h"
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric using namespace llvm::opt;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
300b57cec5SDimitry Andric     ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
31480093f4SDimitry Andric     IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
32480093f4SDimitry Andric     std::vector<std::string> *CC1Args) {
33*349cc55cSDimitry Andric   assert(!ArgList.empty());
340b57cec5SDimitry Andric   if (!Diags.get()) {
350b57cec5SDimitry Andric     // No diagnostics engine was provided, so create our own diagnostics object
360b57cec5SDimitry Andric     // with the default options.
370b57cec5SDimitry Andric     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
380b57cec5SDimitry Andric   }
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   // FIXME: Find a cleaner way to force the driver into restricted modes.
43fe6060f1SDimitry Andric   Args.insert(
44fe6060f1SDimitry Andric       llvm::find_if(
45fe6060f1SDimitry Andric           Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
46fe6060f1SDimitry Andric       "-fsyntax-only");
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   // FIXME: We shouldn't have to pass in the path info.
49e8d8bef9SDimitry Andric   driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
50e8d8bef9SDimitry Andric                            "clang LLVM compiler", VFS);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // Don't check that inputs exist, they may have been remapped.
530b57cec5SDimitry Andric   TheDriver.setCheckInputsExist(false);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
560b57cec5SDimitry Andric   if (!C)
570b57cec5SDimitry Andric     return nullptr;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // Just print the cc1 options if -### was present.
600b57cec5SDimitry Andric   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
610b57cec5SDimitry Andric     C->getJobs().Print(llvm::errs(), "\n", true);
620b57cec5SDimitry Andric     return nullptr;
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // We expect to get back exactly one command job, if we didn't something
660b57cec5SDimitry Andric   // failed. Offload compilation is an exception as it creates multiple jobs. If
670b57cec5SDimitry Andric   // that's the case, we proceed with the first job. If caller needs a
680b57cec5SDimitry Andric   // particular job, it should be controlled via options (e.g.
690b57cec5SDimitry Andric   // --cuda-{host|device}-only for CUDA) passed to the driver.
700b57cec5SDimitry Andric   const driver::JobList &Jobs = C->getJobs();
710b57cec5SDimitry Andric   bool OffloadCompilation = false;
720b57cec5SDimitry Andric   if (Jobs.size() > 1) {
730b57cec5SDimitry Andric     for (auto &A : C->getActions()){
740b57cec5SDimitry Andric       // On MacOSX real actions may end up being wrapped in BindArchAction
750b57cec5SDimitry Andric       if (isa<driver::BindArchAction>(A))
760b57cec5SDimitry Andric         A = *A->input_begin();
770b57cec5SDimitry Andric       if (isa<driver::OffloadAction>(A)) {
780b57cec5SDimitry Andric         OffloadCompilation = true;
790b57cec5SDimitry Andric         break;
800b57cec5SDimitry Andric       }
810b57cec5SDimitry Andric     }
820b57cec5SDimitry Andric   }
83*349cc55cSDimitry Andric 
84*349cc55cSDimitry Andric   bool PickFirstOfMany = OffloadCompilation || ShouldRecoverOnErorrs;
85*349cc55cSDimitry Andric   if (Jobs.size() == 0 || (Jobs.size() > 1 && !PickFirstOfMany)) {
860b57cec5SDimitry Andric     SmallString<256> Msg;
870b57cec5SDimitry Andric     llvm::raw_svector_ostream OS(Msg);
880b57cec5SDimitry Andric     Jobs.Print(OS, "; ", true);
890b57cec5SDimitry Andric     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
900b57cec5SDimitry Andric     return nullptr;
910b57cec5SDimitry Andric   }
92*349cc55cSDimitry Andric   auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
93*349cc55cSDimitry Andric     return StringRef(Cmd.getCreator().getName()) == "clang";
94*349cc55cSDimitry Andric   });
95*349cc55cSDimitry Andric   if (Cmd == Jobs.end()) {
960b57cec5SDimitry Andric     Diags->Report(diag::err_fe_expected_clang_command);
970b57cec5SDimitry Andric     return nullptr;
980b57cec5SDimitry Andric   }
990b57cec5SDimitry Andric 
100*349cc55cSDimitry Andric   const ArgStringList &CCArgs = Cmd->getArguments();
101480093f4SDimitry Andric   if (CC1Args)
102480093f4SDimitry Andric     *CC1Args = {CCArgs.begin(), CCArgs.end()};
103a7dea167SDimitry Andric   auto CI = std::make_unique<CompilerInvocation>();
1045ffd83dbSDimitry Andric   if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
105a7dea167SDimitry Andric       !ShouldRecoverOnErorrs)
1060b57cec5SDimitry Andric     return nullptr;
1070b57cec5SDimitry Andric   return CI;
1080b57cec5SDimitry Andric }
109