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