2018-03-15 05:05:51 +08:00
|
|
|
//===- Tooling.cpp - Running clang standalone tools -----------------------===//
|
2012-04-04 20:07:46 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-04-04 20:07:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements functions to run clang tools standalone instead
|
|
|
|
// of running them as a plugin.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/FileSystemOptions.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Driver/Job.h"
|
2016-08-31 01:42:29 +08:00
|
|
|
#include "clang/Driver/Options.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Driver/Tool.h"
|
2015-10-06 18:45:03 +08:00
|
|
|
#include "clang/Driver/ToolChain.h"
|
2013-11-07 04:12:45 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Frontend/FrontendOptions.h"
|
2012-04-04 20:07:46 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Tooling/ArgumentsAdjusters.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2016-08-31 01:42:29 +08:00
|
|
|
#include "llvm/Option/ArgList.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/Option/OptTable.h"
|
2013-06-15 01:17:23 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2013-03-16 04:14:01 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-04-04 21:59:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-04-04 21:59:41 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2012-04-04 21:59:41 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2018-03-15 05:05:51 +08:00
|
|
|
#include <vector>
|
2012-04-04 20:07:46 +08:00
|
|
|
|
2014-04-22 06:55:36 +08:00
|
|
|
#define DEBUG_TYPE "clang-tooling"
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace tooling;
|
2012-04-04 20:07:46 +08:00
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
ToolAction::~ToolAction() = default;
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
FrontendActionFactory::~FrontendActionFactory() = default;
|
2012-04-04 20:07:46 +08:00
|
|
|
|
|
|
|
// FIXME: This file contains structural duplication with other parts of the
|
|
|
|
// code that sets up a compiler to run tools on it, and we should refactor
|
|
|
|
// it to be based on the same framework.
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Builds a clang driver initialized for running clang tools.
|
2018-10-10 21:27:25 +08:00
|
|
|
static driver::Driver *
|
|
|
|
newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName,
|
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
|
2018-03-15 05:05:51 +08:00
|
|
|
driver::Driver *CompilerDriver =
|
|
|
|
new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
|
2020-09-11 17:17:31 +08:00
|
|
|
*Diagnostics, "clang LLVM compiler", std::move(VFS));
|
2012-04-04 20:07:46 +08:00
|
|
|
CompilerDriver->setTitle("clang_based_tool");
|
|
|
|
return CompilerDriver;
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:40:43 +08:00
|
|
|
/// Decide whether extra compiler frontend commands can be ignored.
|
|
|
|
static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation) {
|
2018-03-15 05:05:51 +08:00
|
|
|
const driver::JobList &Jobs = Compilation->getJobs();
|
2019-10-11 07:05:55 +08:00
|
|
|
const driver::ActionList &Actions = Compilation->getActions();
|
2021-07-26 19:40:43 +08:00
|
|
|
|
2019-10-11 07:05:55 +08:00
|
|
|
bool OffloadCompilation = false;
|
2021-07-26 19:40:43 +08:00
|
|
|
|
|
|
|
// Jobs and Actions look very different depending on whether the Clang tool
|
|
|
|
// injected -fsyntax-only or not. Try to handle both cases here.
|
|
|
|
|
|
|
|
for (const auto &Job : Jobs)
|
|
|
|
if (StringRef(Job.getExecutable()) == "clang-offload-bundler")
|
|
|
|
OffloadCompilation = true;
|
|
|
|
|
2019-10-11 07:05:55 +08:00
|
|
|
if (Jobs.size() > 1) {
|
|
|
|
for (auto A : Actions){
|
|
|
|
// 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)) {
|
|
|
|
// Offload compilation has 2 top-level actions, one (at the front) is
|
|
|
|
// the original host compilation and the other is offload action
|
|
|
|
// composed of at least one device compilation. For such case, general
|
|
|
|
// tooling will consider host-compilation only. For tooling on device
|
|
|
|
// compilation, device compilation only option, such as
|
|
|
|
// `--cuda-device-only`, needs specifying.
|
2019-10-18 23:03:34 +08:00
|
|
|
assert(Actions.size() > 1);
|
2019-10-11 07:45:20 +08:00
|
|
|
assert(
|
|
|
|
isa<driver::CompileJobAction>(Actions.front()) ||
|
|
|
|
// On MacOSX real actions may end up being wrapped in
|
|
|
|
// BindArchAction.
|
|
|
|
(isa<driver::BindArchAction>(Actions.front()) &&
|
|
|
|
isa<driver::CompileJobAction>(*Actions.front()->input_begin())));
|
2019-10-11 07:05:55 +08:00
|
|
|
OffloadCompilation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 19:40:43 +08:00
|
|
|
|
|
|
|
return OffloadCompilation;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
|
|
|
const llvm::opt::ArgStringList *
|
|
|
|
getCC1Arguments(DiagnosticsEngine *Diagnostics,
|
|
|
|
driver::Compilation *Compilation) {
|
|
|
|
const driver::JobList &Jobs = Compilation->getJobs();
|
|
|
|
|
|
|
|
auto IsCC1Command = [](const driver::Command &Cmd) {
|
|
|
|
return StringRef(Cmd.getCreator().getName()) == "clang";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto IsSrcFile = [](const driver::InputInfo &II) {
|
|
|
|
return isSrcFile(II.getType());
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::SmallVector<const driver::Command *, 1> CC1Jobs;
|
|
|
|
for (const driver::Command &Job : Jobs)
|
|
|
|
if (IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
|
|
|
|
CC1Jobs.push_back(&Job);
|
|
|
|
|
|
|
|
if (CC1Jobs.empty() ||
|
|
|
|
(CC1Jobs.size() > 1 && !ignoreExtraCC1Commands(Compilation))) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<256> error_msg;
|
2012-04-04 20:07:46 +08:00
|
|
|
llvm::raw_svector_ostream error_stream(error_msg);
|
2013-09-13 02:23:34 +08:00
|
|
|
Jobs.Print(error_stream, "; ", true);
|
2018-03-15 05:05:51 +08:00
|
|
|
Diagnostics->Report(diag::err_fe_expected_compiler_job)
|
2012-04-04 20:07:46 +08:00
|
|
|
<< error_stream.str();
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2021-07-26 19:40:43 +08:00
|
|
|
return &CC1Jobs[0]->getArguments();
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a clang build invocation initialized from the CC1 flags.
|
2020-06-18 20:57:50 +08:00
|
|
|
CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
|
|
|
|
const llvm::opt::ArgStringList &CC1Args,
|
|
|
|
const char *const BinaryName) {
|
2012-04-04 20:07:46 +08:00
|
|
|
assert(!CC1Args.empty() && "Must at least contain the program name!");
|
2018-03-15 05:05:51 +08:00
|
|
|
CompilerInvocation *Invocation = new CompilerInvocation;
|
2020-06-18 20:57:50 +08:00
|
|
|
CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics,
|
|
|
|
BinaryName);
|
2012-04-04 20:07:46 +08:00
|
|
|
Invocation->getFrontendOpts().DisableFree = false;
|
2013-06-26 01:01:21 +08:00
|
|
|
Invocation->getCodeGenOpts().DisableFree = false;
|
2012-04-04 20:07:46 +08:00
|
|
|
return Invocation;
|
|
|
|
}
|
|
|
|
|
2019-08-30 17:29:34 +08:00
|
|
|
bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
|
|
|
|
const Twine &Code, const Twine &FileName,
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
|
2019-08-30 17:29:34 +08:00
|
|
|
return runToolOnCodeWithArgs(std::move(ToolAction), Code,
|
|
|
|
std::vector<std::string>(), FileName,
|
|
|
|
"clang-tool", std::move(PCHContainerOps));
|
2012-08-30 10:02:19 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
} // namespace tooling
|
|
|
|
} // namespace clang
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
static std::vector<std::string>
|
2016-01-29 19:29:02 +08:00
|
|
|
getSyntaxOnlyToolArgs(const Twine &ToolName,
|
|
|
|
const std::vector<std::string> &ExtraArgs,
|
2013-11-07 04:12:45 +08:00
|
|
|
StringRef FileName) {
|
|
|
|
std::vector<std::string> Args;
|
2016-01-29 19:29:02 +08:00
|
|
|
Args.push_back(ToolName.str());
|
2013-11-07 04:12:45 +08:00
|
|
|
Args.push_back("-fsyntax-only");
|
|
|
|
Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
|
|
|
|
Args.push_back(FileName.str());
|
|
|
|
return Args;
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
bool runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
2015-06-21 02:53:08 +08:00
|
|
|
const std::vector<std::string> &Args, const Twine &FileName,
|
2016-01-29 19:29:02 +08:00
|
|
|
const Twine &ToolName,
|
2018-05-19 00:06:19 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
|
2012-04-04 20:07:46 +08:00
|
|
|
SmallString<16> FileNameStorage;
|
|
|
|
StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
|
2018-05-19 00:06:19 +08:00
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<FileManager> Files(
|
2018-05-19 00:06:19 +08:00
|
|
|
new FileManager(FileSystemOptions(), VFS));
|
2017-07-07 06:47:19 +08:00
|
|
|
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
|
|
|
|
ToolInvocation Invocation(
|
|
|
|
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
|
2019-08-30 17:29:34 +08:00
|
|
|
std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
|
2018-05-19 00:06:19 +08:00
|
|
|
return Invocation.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
|
2018-05-19 00:06:19 +08:00
|
|
|
const std::vector<std::string> &Args, const Twine &FileName,
|
|
|
|
const Twine &ToolName,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
const FileContentMappings &VirtualMappedFiles) {
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
|
|
|
|
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2018-05-19 00:06:19 +08:00
|
|
|
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
2012-04-04 20:07:46 +08:00
|
|
|
|
|
|
|
SmallString<1024> CodeStorage;
|
2018-05-19 00:06:19 +08:00
|
|
|
InMemoryFileSystem->addFile(FileName, 0,
|
2015-10-09 17:54:37 +08:00
|
|
|
llvm::MemoryBuffer::getMemBuffer(
|
|
|
|
Code.toNullTerminatedStringRef(CodeStorage)));
|
2014-11-26 01:01:06 +08:00
|
|
|
|
|
|
|
for (auto &FilenameWithContent : VirtualMappedFiles) {
|
2015-10-09 17:54:37 +08:00
|
|
|
InMemoryFileSystem->addFile(
|
|
|
|
FilenameWithContent.first, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
|
2014-11-26 01:01:06 +08:00
|
|
|
}
|
|
|
|
|
2019-08-30 17:29:34 +08:00
|
|
|
return runToolOnCodeWithArgs(std::move(ToolAction), Code, OverlayFileSystem,
|
|
|
|
Args, FileName, ToolName);
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS,
|
2018-08-30 00:35:31 +08:00
|
|
|
StringRef File) {
|
2012-04-04 20:07:46 +08:00
|
|
|
StringRef RelativePath(File);
|
2012-05-24 06:24:20 +08:00
|
|
|
// FIXME: Should '.\\' be accepted on Win32?
|
2012-04-04 20:07:46 +08:00
|
|
|
if (RelativePath.startswith("./")) {
|
|
|
|
RelativePath = RelativePath.substr(strlen("./"));
|
|
|
|
}
|
2013-08-10 09:40:10 +08:00
|
|
|
|
|
|
|
SmallString<1024> AbsolutePath = RelativePath;
|
2018-08-30 00:35:31 +08:00
|
|
|
if (auto EC = FS.makeAbsolute(AbsolutePath))
|
|
|
|
return llvm::errorCodeToError(EC);
|
2013-09-11 19:23:15 +08:00
|
|
|
llvm::sys::path::native(AbsolutePath);
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(AbsolutePath.str());
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 00:35:31 +08:00
|
|
|
std::string getAbsolutePath(StringRef File) {
|
2018-10-10 21:27:25 +08:00
|
|
|
return llvm::cantFail(getAbsolutePath(*llvm::vfs::getRealFileSystem(), File));
|
2018-08-30 00:35:31 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:45:03 +08:00
|
|
|
void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
|
|
|
|
StringRef InvokedAs) {
|
2020-08-02 00:55:30 +08:00
|
|
|
if (CommandLine.empty() || InvokedAs.empty())
|
|
|
|
return;
|
|
|
|
const auto &Table = driver::getDriverOptTable();
|
|
|
|
// --target=X
|
|
|
|
const std::string TargetOPT =
|
|
|
|
Table.getOption(driver::options::OPT_target).getPrefixedName();
|
|
|
|
// -target X
|
|
|
|
const std::string TargetOPTLegacy =
|
|
|
|
Table.getOption(driver::options::OPT_target_legacy_spelling)
|
|
|
|
.getPrefixedName();
|
|
|
|
// --driver-mode=X
|
|
|
|
const std::string DriverModeOPT =
|
|
|
|
Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
|
2020-08-02 01:03:40 +08:00
|
|
|
auto TargetMode =
|
|
|
|
driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
|
|
|
|
// No need to search for target args if we don't have a target/mode to insert.
|
|
|
|
bool ShouldAddTarget = TargetMode.TargetIsValid;
|
|
|
|
bool ShouldAddMode = TargetMode.DriverMode != nullptr;
|
2020-08-02 00:55:30 +08:00
|
|
|
// Skip CommandLine[0].
|
|
|
|
for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
|
|
|
|
++Token) {
|
|
|
|
StringRef TokenRef(*Token);
|
2020-08-02 01:03:40 +08:00
|
|
|
ShouldAddTarget = ShouldAddTarget && !TokenRef.startswith(TargetOPT) &&
|
|
|
|
!TokenRef.equals(TargetOPTLegacy);
|
|
|
|
ShouldAddMode = ShouldAddMode && !TokenRef.startswith(DriverModeOPT);
|
2020-08-02 00:55:30 +08:00
|
|
|
}
|
2020-08-02 01:03:40 +08:00
|
|
|
if (ShouldAddMode) {
|
2020-08-02 00:55:30 +08:00
|
|
|
CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
|
|
|
|
}
|
2020-08-02 01:03:40 +08:00
|
|
|
if (ShouldAddTarget) {
|
2020-08-02 00:55:30 +08:00
|
|
|
CommandLine.insert(++CommandLine.begin(),
|
|
|
|
TargetOPT + TargetMode.TargetPrefix);
|
2015-10-06 18:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
} // namespace tooling
|
|
|
|
} // namespace clang
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SingleFrontendActionFactory : public FrontendActionFactory {
|
2019-08-30 00:38:36 +08:00
|
|
|
std::unique_ptr<FrontendAction> Action;
|
2013-11-07 04:12:45 +08:00
|
|
|
|
|
|
|
public:
|
2019-08-30 00:38:36 +08:00
|
|
|
SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
|
|
|
|
: Action(std::move(Action)) {}
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2019-08-30 00:38:36 +08:00
|
|
|
std::unique_ptr<FrontendAction> create() override {
|
|
|
|
return std::move(Action);
|
|
|
|
}
|
2013-11-07 04:12:45 +08:00
|
|
|
};
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
} // namespace
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
ToolInvocation::ToolInvocation(
|
|
|
|
std::vector<std::string> CommandLine, ToolAction *Action,
|
|
|
|
FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
|
|
|
|
: CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false),
|
2018-03-15 05:05:51 +08:00
|
|
|
Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
ToolInvocation::ToolInvocation(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::vector<std::string> CommandLine,
|
|
|
|
std::unique_ptr<FrontendAction> FAction, FileManager *Files,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
|
2014-03-20 20:48:36 +08:00
|
|
|
: CommandLine(std::move(CommandLine)),
|
2019-08-30 17:29:34 +08:00
|
|
|
Action(new SingleFrontendActionFactory(std::move(FAction))),
|
2019-08-30 00:38:36 +08:00
|
|
|
OwnsAction(true), Files(Files),
|
|
|
|
PCHContainerOps(std::move(PCHContainerOps)) {}
|
2013-11-07 04:12:45 +08:00
|
|
|
|
|
|
|
ToolInvocation::~ToolInvocation() {
|
|
|
|
if (OwnsAction)
|
|
|
|
delete Action;
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolInvocation::run() {
|
|
|
|
std::vector<const char*> Argv;
|
2014-03-20 20:48:36 +08:00
|
|
|
for (const std::string &Str : CommandLine)
|
|
|
|
Argv.push_back(Str.c_str());
|
2012-04-04 20:07:46 +08:00
|
|
|
const char *const BinaryName = Argv[0];
|
2021-09-10 18:50:51 +08:00
|
|
|
|
|
|
|
// Parse diagnostic options from the driver command-line only if none were
|
|
|
|
// explicitly set.
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> ParsedDiagOpts;
|
|
|
|
DiagnosticOptions *DiagOpts = this->DiagOpts;
|
|
|
|
if (!DiagOpts) {
|
|
|
|
ParsedDiagOpts = CreateAndPopulateDiagOpts(Argv);
|
|
|
|
DiagOpts = &*ParsedDiagOpts;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
|
2021-09-10 19:46:01 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics =
|
|
|
|
CompilerInstance::createDiagnostics(
|
|
|
|
&*DiagOpts, DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
|
2021-04-06 16:38:19 +08:00
|
|
|
// Although `Diagnostics` are used only for command-line parsing, the custom
|
|
|
|
// `DiagConsumer` might expect a `SourceManager` to be present.
|
2021-09-10 19:46:01 +08:00
|
|
|
SourceManager SrcMgr(*Diagnostics, *Files);
|
|
|
|
Diagnostics->setSourceManager(&SrcMgr);
|
2012-04-04 20:07:46 +08:00
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
const std::unique_ptr<driver::Driver> Driver(
|
2021-09-10 19:46:01 +08:00
|
|
|
newDriver(&*Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
|
2018-11-09 19:49:22 +08:00
|
|
|
// The "input file not found" diagnostics from the driver are useful.
|
|
|
|
// The driver is only aware of the VFS working directory, but some clients
|
|
|
|
// change this at the FileManager level instead.
|
|
|
|
// In this case the checks have false positives, so skip them.
|
|
|
|
if (!Files->getFileSystemOpts().WorkingDir.empty())
|
|
|
|
Driver->setCheckInputsExist(false);
|
2018-03-15 05:05:51 +08:00
|
|
|
const std::unique_ptr<driver::Compilation> Compilation(
|
2012-04-04 20:07:46 +08:00
|
|
|
Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
|
2017-05-24 19:57:37 +08:00
|
|
|
if (!Compilation)
|
|
|
|
return false;
|
2013-06-15 01:17:23 +08:00
|
|
|
const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
|
2021-09-10 19:46:01 +08:00
|
|
|
&*Diagnostics, Compilation.get());
|
2018-03-15 05:05:51 +08:00
|
|
|
if (!CC1Args)
|
2012-04-04 20:07:46 +08:00
|
|
|
return false;
|
2018-03-15 05:05:51 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> Invocation(
|
2021-09-10 19:46:01 +08:00
|
|
|
newInvocation(&*Diagnostics, *CC1Args, BinaryName));
|
2017-01-07 03:49:01 +08:00
|
|
|
return runInvocation(BinaryName, Compilation.get(), std::move(Invocation),
|
2016-06-13 04:05:23 +08:00
|
|
|
std::move(PCHContainerOps));
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolInvocation::runInvocation(
|
2018-03-15 05:05:51 +08:00
|
|
|
const char *BinaryName, driver::Compilation *Compilation,
|
|
|
|
std::shared_ptr<CompilerInvocation> Invocation,
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
|
2012-04-04 20:07:46 +08:00
|
|
|
// Show the invocation, with -v.
|
|
|
|
if (Invocation->getHeaderSearchOpts().Verbose) {
|
|
|
|
llvm::errs() << "clang Invocation:\n";
|
2013-09-13 02:23:34 +08:00
|
|
|
Compilation->getJobs().Print(llvm::errs(), "\n", true);
|
2012-04-04 20:07:46 +08:00
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
|
2017-01-07 03:49:01 +08:00
|
|
|
return Action->runInvocation(std::move(Invocation), Files,
|
|
|
|
std::move(PCHContainerOps), DiagConsumer);
|
2013-11-07 04:12:45 +08:00
|
|
|
}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
bool FrontendActionFactory::runInvocation(
|
2017-01-07 03:49:01 +08:00
|
|
|
std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files,
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
|
|
|
DiagnosticConsumer *DiagConsumer) {
|
2012-04-04 20:07:46 +08:00
|
|
|
// Create a compiler instance to handle the actual work.
|
2018-03-15 05:05:51 +08:00
|
|
|
CompilerInstance Compiler(std::move(PCHContainerOps));
|
2017-01-07 03:49:01 +08:00
|
|
|
Compiler.setInvocation(std::move(Invocation));
|
2012-04-04 20:07:46 +08:00
|
|
|
Compiler.setFileManager(Files);
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
// The FrontendAction can have lifetime requirements for Compiler or its
|
|
|
|
// members, and we need to ensure it's deleted earlier than Compiler. So we
|
2014-03-08 04:03:18 +08:00
|
|
|
// pass it to an std::unique_ptr declared after the Compiler variable.
|
|
|
|
std::unique_ptr<FrontendAction> ScopedToolAction(create());
|
2012-06-01 01:58:43 +08:00
|
|
|
|
2014-05-16 21:45:29 +08:00
|
|
|
// Create the compiler's actual diagnostics engine.
|
2013-11-08 07:18:05 +08:00
|
|
|
Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
|
2012-04-04 20:07:46 +08:00
|
|
|
if (!Compiler.hasDiagnostics())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Compiler.createSourceManager(*Files);
|
|
|
|
|
2012-06-01 01:58:43 +08:00
|
|
|
const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
|
2012-04-04 20:07:46 +08:00
|
|
|
|
2018-12-22 03:33:09 +08:00
|
|
|
Files->clearStatCache();
|
2012-04-04 20:07:46 +08:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangTool::ClangTool(const CompilationDatabase &Compilations,
|
2015-06-21 02:53:08 +08:00
|
|
|
ArrayRef<std::string> SourcePaths,
|
2018-01-23 20:30:02 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
2019-08-30 06:56:38 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
|
|
|
|
IntrusiveRefCntPtr<FileManager> Files)
|
2014-08-28 05:36:39 +08:00
|
|
|
: Compilations(Compilations), SourcePaths(SourcePaths),
|
2016-05-27 22:27:13 +08:00
|
|
|
PCHContainerOps(std::move(PCHContainerOps)),
|
2018-10-10 21:27:25 +08:00
|
|
|
OverlayFileSystem(new llvm::vfs::OverlayFileSystem(std::move(BaseFS))),
|
|
|
|
InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
|
2019-08-30 06:56:38 +08:00
|
|
|
Files(Files ? Files
|
|
|
|
: new FileManager(FileSystemOptions(), OverlayFileSystem)) {
|
2015-10-09 17:54:37 +08:00
|
|
|
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
2014-12-04 01:53:02 +08:00
|
|
|
appendArgumentsAdjuster(getClangStripOutputAdjuster());
|
|
|
|
appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
|
2017-07-15 02:33:30 +08:00
|
|
|
appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
|
2019-08-30 06:56:38 +08:00
|
|
|
if (Files)
|
|
|
|
Files->setVirtualFileSystem(OverlayFileSystem);
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
ClangTool::~ClangTool() = default;
|
2013-11-08 07:18:05 +08:00
|
|
|
|
2012-04-04 20:07:46 +08:00
|
|
|
void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
|
|
|
|
MappedFileContents.push_back(std::make_pair(FilePath, Content));
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
|
2017-10-26 18:38:14 +08:00
|
|
|
ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
|
2013-06-04 22:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClangTool::clearArgumentsAdjusters() {
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgsAdjuster = nullptr;
|
2012-05-10 00:18:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-21 18:18:18 +08:00
|
|
|
static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
|
|
|
|
void *MainAddr) {
|
|
|
|
// Allow users to override the resource dir.
|
|
|
|
for (StringRef Arg : Args)
|
|
|
|
if (Arg.startswith("-resource-dir"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If there's no override in place add our resource dir.
|
[Tooling] Handle compilation databases containing commands with double dashes
As of CMake commit https://gitlab.kitware.com/cmake/cmake/-/commit/d993ebd4,
which first appeared in CMake 3.19.x series, in the compile commands for
clang-cl, CMake puts `--` before the input file. When operating on such a
database, the `InterpolatingCompilationDatabase` - specifically, the
`TransferableCommand` constructor - does not recognize that pattern and so, does
not strip the input, or the double dash when 'transferring' the compile command.
This results in a incorrect compile command - with the double dash and old input
file left in, and the language options and new input file appended after them,
where they're all treated as inputs, including the language version option.
Test files for some tests have names similar enough to be matched to commands
from the database, e.g.:
`.../path-mappings.test.tmp/server/bar.cpp`
can be matched to:
`.../Driver/ToolChains/BareMetal.cpp`
etc. When that happens, the tool being tested tries to use the matched, and
incorrectly 'transferred' compile command, and fails, reporting errors similar
to:
`error: no such file or directory: '/std:c++14'; did you mean '/std:c++14'? [clang-diagnostic-error]`
This happens in at least 4 tests:
Clang Tools :: clang-tidy/checkers/performance-trivially-destructible.cpp
Clangd :: check-fail.test
Clangd :: check.test
Clangd :: path-mappings.test
The fix for `TransferableCommand` removes the `--` and everything after it when
determining the arguments that apply to the new file. `--` is inserted in the
'transferred' command if the new file name starts with `-` and when operating in
clang-cl mode, also `/`. Additionally, other places in the code known to do
argument adjustment without accounting for the `--` and causing the tests to
fail are fixed as well.
Differential Revision: https://reviews.llvm.org/D98824
2021-03-25 04:01:08 +08:00
|
|
|
Args = getInsertArgumentAdjuster(
|
|
|
|
("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
|
|
|
|
.c_str())(Args, "");
|
2016-04-21 18:18:18 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
int ClangTool::run(ToolAction *Action) {
|
2012-06-05 03:02:59 +08:00
|
|
|
// Exists solely for the purpose of lookup of the resource path.
|
|
|
|
// This just needs to be some symbol in the binary.
|
|
|
|
static int StaticSymbol;
|
|
|
|
|
2015-10-09 17:54:37 +08:00
|
|
|
// First insert all absolute paths into the in-memory VFS. These are global
|
|
|
|
// for all compile commands.
|
|
|
|
if (SeenWorkingDirectories.insert("/").second)
|
|
|
|
for (const auto &MappedFile : MappedFileContents)
|
|
|
|
if (llvm::sys::path::is_absolute(MappedFile.first))
|
|
|
|
InMemoryFileSystem->addFile(
|
|
|
|
MappedFile.first, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
|
|
|
|
|
2012-04-04 20:07:46 +08:00
|
|
|
bool ProcessingFailed = false;
|
2018-02-03 02:19:22 +08:00
|
|
|
bool FileSkipped = false;
|
2018-08-30 00:35:31 +08:00
|
|
|
// Compute all absolute paths before we run any actions, as those will change
|
|
|
|
// the working directory.
|
|
|
|
std::vector<std::string> AbsolutePaths;
|
|
|
|
AbsolutePaths.reserve(SourcePaths.size());
|
2014-08-28 05:36:39 +08:00
|
|
|
for (const auto &SourcePath : SourcePaths) {
|
2018-08-30 00:35:31 +08:00
|
|
|
auto AbsPath = getAbsolutePath(*OverlayFileSystem, SourcePath);
|
|
|
|
if (!AbsPath) {
|
|
|
|
llvm::errs() << "Skipping " << SourcePath
|
|
|
|
<< ". Error while getting an absolute path: "
|
|
|
|
<< llvm::toString(AbsPath.takeError()) << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AbsolutePaths.push_back(std::move(*AbsPath));
|
|
|
|
}
|
2014-08-28 05:36:39 +08:00
|
|
|
|
2018-09-11 15:29:09 +08:00
|
|
|
// Remember the working directory in case we need to restore it.
|
|
|
|
std::string InitialWorkingDir;
|
|
|
|
if (RestoreCWD) {
|
|
|
|
if (auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
|
|
|
|
InitialWorkingDir = std::move(*CWD);
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "Could not get working directory: "
|
|
|
|
<< CWD.getError().message() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:35:31 +08:00
|
|
|
for (llvm::StringRef File : AbsolutePaths) {
|
2014-11-10 23:42:31 +08:00
|
|
|
// Currently implementations of CompilationDatabase::getCompileCommands can
|
|
|
|
// change the state of the file system (e.g. prepare generated headers), so
|
|
|
|
// this method needs to run right before we invoke the tool, as the next
|
|
|
|
// file may require a different (incompatible) state of the file system.
|
|
|
|
//
|
|
|
|
// FIXME: Make the compilation database interface more explicit about the
|
|
|
|
// requirements to the order of invocation of its members.
|
2014-08-28 05:36:39 +08:00
|
|
|
std::vector<CompileCommand> CompileCommandsForFile =
|
|
|
|
Compilations.getCompileCommands(File);
|
|
|
|
if (CompileCommandsForFile.empty()) {
|
|
|
|
llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
|
2018-02-03 02:19:22 +08:00
|
|
|
FileSkipped = true;
|
2014-08-28 05:36:39 +08:00
|
|
|
continue;
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
2014-08-28 05:36:39 +08:00
|
|
|
for (CompileCommand &CompileCommand : CompileCommandsForFile) {
|
|
|
|
// FIXME: chdir is thread hostile; on the other hand, creating the same
|
|
|
|
// behavior as chdir is complex: chdir resolves the path once, thus
|
|
|
|
// guaranteeing that all subsequent relative path operations work
|
|
|
|
// on the same path the original chdir resulted in. This makes a
|
|
|
|
// difference for example on network filesystems, where symlinks might be
|
|
|
|
// switched during runtime of the tool. Fixing this depends on having a
|
|
|
|
// file system abstraction that allows openat() style interactions.
|
2015-10-09 17:54:37 +08:00
|
|
|
if (OverlayFileSystem->setCurrentWorkingDirectory(
|
|
|
|
CompileCommand.Directory))
|
2014-08-28 05:36:39 +08:00
|
|
|
llvm::report_fatal_error("Cannot chdir into \"" +
|
2019-06-21 04:25:59 +08:00
|
|
|
Twine(CompileCommand.Directory) + "\"!");
|
2015-10-09 17:54:37 +08:00
|
|
|
|
|
|
|
// Now fill the in-memory VFS with the relative file mappings so it will
|
|
|
|
// have the correct relative paths. We never remove mappings but that
|
|
|
|
// should be fine.
|
|
|
|
if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
|
|
|
|
for (const auto &MappedFile : MappedFileContents)
|
|
|
|
if (!llvm::sys::path::is_absolute(MappedFile.first))
|
|
|
|
InMemoryFileSystem->addFile(
|
|
|
|
MappedFile.first, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
|
|
|
|
|
2014-08-28 05:36:39 +08:00
|
|
|
std::vector<std::string> CommandLine = CompileCommand.CommandLine;
|
2014-12-04 01:53:02 +08:00
|
|
|
if (ArgsAdjuster)
|
2015-11-05 10:19:53 +08:00
|
|
|
CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename);
|
2014-08-28 05:36:39 +08:00
|
|
|
assert(!CommandLine.empty());
|
2016-04-21 18:18:18 +08:00
|
|
|
|
|
|
|
// Add the resource dir based on the binary of this tool. argv[0] in the
|
|
|
|
// compilation database may refer to a different compiler and we want to
|
|
|
|
// pick up the very same standard library that compiler is using. The
|
|
|
|
// builtin headers in the resource dir need to match the exact clang
|
|
|
|
// version the tool is using.
|
|
|
|
// FIXME: On linux, GetMainExecutable is independent of the value of the
|
|
|
|
// first argument, thus allowing ClangTool and runToolOnCode to just
|
|
|
|
// pass in made-up names here. Make sure this works on other platforms.
|
|
|
|
injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
|
|
|
|
|
2014-08-28 05:36:39 +08:00
|
|
|
// FIXME: We need a callback mechanism for the tool writer to output a
|
|
|
|
// customized message for each file.
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; });
|
2015-06-21 02:53:08 +08:00
|
|
|
ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
|
|
|
|
PCHContainerOps);
|
2014-08-28 05:36:39 +08:00
|
|
|
Invocation.setDiagnosticConsumer(DiagConsumer);
|
2015-10-09 17:54:37 +08:00
|
|
|
|
2014-08-28 05:36:39 +08:00
|
|
|
if (!Invocation.run()) {
|
|
|
|
// FIXME: Diagnostics should be used instead.
|
2019-06-27 05:11:51 +08:00
|
|
|
if (PrintErrorMessage)
|
|
|
|
llvm::errs() << "Error while processing " << File << ".\n";
|
2014-08-28 05:36:39 +08:00
|
|
|
ProcessingFailed = true;
|
|
|
|
}
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 15:29:09 +08:00
|
|
|
|
|
|
|
if (!InitialWorkingDir.empty()) {
|
|
|
|
if (auto EC =
|
|
|
|
OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
|
|
|
|
llvm::errs() << "Error when trying to restore working dir: "
|
|
|
|
<< EC.message() << "\n";
|
|
|
|
}
|
2018-02-03 02:19:22 +08:00
|
|
|
return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
|
2012-04-04 20:07:46 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ASTBuilderAction : public ToolAction {
|
2014-04-25 22:49:37 +08:00
|
|
|
std::vector<std::unique_ptr<ASTUnit>> &ASTs;
|
2013-11-07 04:12:45 +08:00
|
|
|
|
|
|
|
public:
|
2014-04-25 22:49:37 +08:00
|
|
|
ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2017-01-07 03:49:01 +08:00
|
|
|
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
|
|
|
|
FileManager *Files,
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
2014-03-15 12:29:04 +08:00
|
|
|
DiagnosticConsumer *DiagConsumer) override {
|
2014-04-26 01:01:33 +08:00
|
|
|
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
|
2016-06-13 04:05:23 +08:00
|
|
|
Invocation, std::move(PCHContainerOps),
|
2015-06-21 02:53:08 +08:00
|
|
|
CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
|
|
|
|
DiagConsumer,
|
2015-10-06 22:45:20 +08:00
|
|
|
/*ShouldOwnClient=*/false),
|
|
|
|
Files);
|
2013-11-07 04:12:45 +08:00
|
|
|
if (!AST)
|
|
|
|
return false;
|
|
|
|
|
2014-04-25 22:49:37 +08:00
|
|
|
ASTs.push_back(std::move(AST));
|
2013-11-07 04:12:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2018-03-15 05:05:51 +08:00
|
|
|
|
|
|
|
} // namespace
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2014-04-25 22:49:37 +08:00
|
|
|
int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
|
2013-11-07 04:12:45 +08:00
|
|
|
ASTBuilderAction Action(ASTs);
|
|
|
|
return run(&Action);
|
|
|
|
}
|
|
|
|
|
2018-09-11 15:29:09 +08:00
|
|
|
void ClangTool::setRestoreWorkingDir(bool RestoreCWD) {
|
|
|
|
this->RestoreCWD = RestoreCWD;
|
|
|
|
}
|
|
|
|
|
2019-06-27 05:11:51 +08:00
|
|
|
void ClangTool::setPrintErrorMessage(bool PrintErrorMessage) {
|
|
|
|
this->PrintErrorMessage = PrintErrorMessage;
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
std::unique_ptr<ASTUnit>
|
2019-01-09 00:55:13 +08:00
|
|
|
buildASTFromCode(StringRef Code, StringRef FileName,
|
2015-06-21 02:53:08 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
|
|
|
|
return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName,
|
2016-06-13 04:05:23 +08:00
|
|
|
"clang-tool", std::move(PCHContainerOps));
|
2013-11-07 04:12:45 +08:00
|
|
|
}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
|
2019-01-09 00:55:13 +08:00
|
|
|
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
|
|
|
|
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
|
2020-02-18 22:04:44 +08:00
|
|
|
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
|
|
|
|
DiagnosticConsumer *DiagConsumer) {
|
2014-04-25 22:49:37 +08:00
|
|
|
std::vector<std::unique_ptr<ASTUnit>> ASTs;
|
2013-11-07 04:12:45 +08:00
|
|
|
ASTBuilderAction Action(ASTs);
|
2018-10-10 21:27:25 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
|
|
|
|
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2015-10-09 17:54:37 +08:00
|
|
|
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
2015-10-06 23:04:13 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<FileManager> Files(
|
2015-10-09 17:54:37 +08:00
|
|
|
new FileManager(FileSystemOptions(), OverlayFileSystem));
|
2017-07-07 05:02:52 +08:00
|
|
|
|
|
|
|
ToolInvocation Invocation(
|
2019-01-09 00:55:13 +08:00
|
|
|
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),
|
2017-07-07 05:02:52 +08:00
|
|
|
&Action, Files.get(), std::move(PCHContainerOps));
|
2020-02-18 22:04:44 +08:00
|
|
|
Invocation.setDiagnosticConsumer(DiagConsumer);
|
2013-11-07 04:12:45 +08:00
|
|
|
|
2019-01-09 00:55:13 +08:00
|
|
|
InMemoryFileSystem->addFile(FileName, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(Code));
|
2019-11-13 22:04:13 +08:00
|
|
|
for (auto &FilenameWithContent : VirtualMappedFiles) {
|
|
|
|
InMemoryFileSystem->addFile(
|
|
|
|
FilenameWithContent.first, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
|
|
|
|
}
|
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
if (!Invocation.run())
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2020-11-18 16:20:37 +08:00
|
|
|
|
2013-11-07 04:12:45 +08:00
|
|
|
assert(ASTs.size() == 1);
|
2014-04-26 01:01:33 +08:00
|
|
|
return std::move(ASTs[0]);
|
2013-11-07 04:12:45 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:05:51 +08:00
|
|
|
} // namespace tooling
|
|
|
|
} // namespace clang
|