mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-26 14:58:59 -04:00

Summary: When they read compiler args from compile_commands.json. This change allows to run clang-based tools, like clang-tidy or clangd, built from head using the compile_commands.json file produced for XCode toolchains. On MacOS clang can find the C++ standard library relative to the compiler installation dir. The logic to do this was based on resource dir as an approximation of where the compiler is installed. This broke the tools that read 'compile_commands.json' and don't ship with the compiler, as they typically change resource dir. To workaround this, we now use compiler install dir detected by the driver to better mimic the behavior of the original compiler when replaying the compilations using other tools. Reviewers: sammccall, arphaman, EricWF Reviewed By: sammccall Subscribers: ioeric, christof, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D54310 llvm-svn: 346652
111 lines
4.2 KiB
C++
111 lines
4.2 KiB
C++
//===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Construct a compiler invocation object for command line driver arguments
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
#include "clang/Driver/Action.h"
|
|
#include "clang/Driver/Compilation.h"
|
|
#include "clang/Driver/Driver.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "clang/Driver/Tool.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
#include "clang/Frontend/Utils.h"
|
|
#include "llvm/Option/ArgList.h"
|
|
#include "llvm/Support/Host.h"
|
|
#include "llvm/Support/Path.h"
|
|
using namespace clang;
|
|
using namespace llvm::opt;
|
|
|
|
/// createInvocationFromCommandLine - Construct a compiler invocation object for
|
|
/// a command line argument vector.
|
|
///
|
|
/// \return A CompilerInvocation, or 0 if none was built for the given
|
|
/// argument vector.
|
|
std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
|
|
ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
|
|
if (!Diags.get()) {
|
|
// No diagnostics engine was provided, so create our own diagnostics object
|
|
// with the default options.
|
|
Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
|
|
}
|
|
|
|
SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
|
|
|
|
// FIXME: Find a cleaner way to force the driver into restricted modes.
|
|
Args.push_back("-fsyntax-only");
|
|
|
|
// FIXME: We shouldn't have to pass in the path info.
|
|
driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(),
|
|
*Diags, VFS);
|
|
|
|
// Don't check that inputs exist, they may have been remapped.
|
|
TheDriver.setCheckInputsExist(false);
|
|
|
|
std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
|
|
if (!C)
|
|
return nullptr;
|
|
|
|
// Just print the cc1 options if -### was present.
|
|
if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
|
|
C->getJobs().Print(llvm::errs(), "\n", true);
|
|
return nullptr;
|
|
}
|
|
|
|
// We expect to get back exactly one command job, if we didn't something
|
|
// failed. Offload compilation is an exception as it creates multiple jobs. If
|
|
// that's the case, we proceed with the first job. If caller needs a
|
|
// particular job, it should be controlled via options (e.g.
|
|
// --cuda-{host|device}-only for CUDA) passed to the driver.
|
|
const driver::JobList &Jobs = C->getJobs();
|
|
bool OffloadCompilation = false;
|
|
if (Jobs.size() > 1) {
|
|
for (auto &A : C->getActions()){
|
|
// On MacOSX real actions may end up being wrapped in BindArchAction
|
|
if (isa<driver::BindArchAction>(A))
|
|
A = *A->input_begin();
|
|
if (isa<driver::OffloadAction>(A)) {
|
|
OffloadCompilation = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
|
|
(Jobs.size() > 1 && !OffloadCompilation)) {
|
|
SmallString<256> Msg;
|
|
llvm::raw_svector_ostream OS(Msg);
|
|
Jobs.Print(OS, "; ", true);
|
|
Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
|
|
return nullptr;
|
|
}
|
|
|
|
const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
|
|
if (StringRef(Cmd.getCreator().getName()) != "clang") {
|
|
Diags->Report(diag::err_fe_expected_clang_command);
|
|
return nullptr;
|
|
}
|
|
|
|
const ArgStringList &CCArgs = Cmd.getArguments();
|
|
auto CI = llvm::make_unique<CompilerInvocation>();
|
|
if (!CompilerInvocation::CreateFromArgs(*CI,
|
|
const_cast<const char **>(CCArgs.data()),
|
|
const_cast<const char **>(CCArgs.data()) +
|
|
CCArgs.size(),
|
|
*Diags))
|
|
return nullptr;
|
|
// Patch up the install dir, so we find the same standard library as the
|
|
// original compiler on MacOS.
|
|
CI->getHeaderSearchOpts().InstallDir = TheDriver.getInstalledDir();
|
|
return CI;
|
|
}
|