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