diff options
Diffstat (limited to 'gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp')
| -rw-r--r-- | gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp | 81 |
1 files changed, 52 insertions, 29 deletions
diff --git a/gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp b/gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp index fd5596ec2de..4c7fed1e617 100644 --- a/gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp +++ b/gnu/llvm/tools/clang/lib/Tooling/Tooling.cpp @@ -31,6 +31,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/raw_ostream.h" +#include <utility> #define DEBUG_TYPE "clang-tooling" @@ -49,8 +50,9 @@ FrontendActionFactory::~FrontendActionFactory() {} static clang::driver::Driver *newDriver( clang::DiagnosticsEngine *Diagnostics, const char *BinaryName, IntrusiveRefCntPtr<vfs::FileSystem> VFS) { - clang::driver::Driver *CompilerDriver = new clang::driver::Driver( - BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics, VFS); + clang::driver::Driver *CompilerDriver = + new clang::driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(), + *Diagnostics, std::move(VFS)); CompilerDriver->setTitle("clang_based_tool"); return CompilerDriver; } @@ -103,14 +105,16 @@ bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName, std::shared_ptr<PCHContainerOperations> PCHContainerOps) { return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(), - FileName, PCHContainerOps); + FileName, "clang-tool", + std::move(PCHContainerOps)); } static std::vector<std::string> -getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, +getSyntaxOnlyToolArgs(const Twine &ToolName, + const std::vector<std::string> &ExtraArgs, StringRef FileName) { std::vector<std::string> Args; - Args.push_back("clang-tool"); + Args.push_back(ToolName.str()); Args.push_back("-fsyntax-only"); Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); Args.push_back(FileName.str()); @@ -120,6 +124,7 @@ getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, bool runToolOnCodeWithArgs( clang::FrontendAction *ToolAction, const Twine &Code, const std::vector<std::string> &Args, const Twine &FileName, + const Twine &ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps, const FileContentMappings &VirtualMappedFiles) { @@ -132,8 +137,9 @@ bool runToolOnCodeWithArgs( OverlayFileSystem->pushOverlay(InMemoryFileSystem); llvm::IntrusiveRefCntPtr<FileManager> Files( new FileManager(FileSystemOptions(), OverlayFileSystem)); - ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), - ToolAction, Files.get(), PCHContainerOps); + ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef), + ToolAction, Files.get(), + std::move(PCHContainerOps)); SmallString<1024> CodeStorage; InMemoryFileSystem->addFile(FileNameRef, 0, @@ -206,14 +212,16 @@ ToolInvocation::ToolInvocation( std::vector<std::string> CommandLine, ToolAction *Action, FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false), - Files(Files), PCHContainerOps(PCHContainerOps), DiagConsumer(nullptr) {} + Files(Files), PCHContainerOps(std::move(PCHContainerOps)), + DiagConsumer(nullptr) {} ToolInvocation::ToolInvocation( std::vector<std::string> CommandLine, FrontendAction *FAction, FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : CommandLine(std::move(CommandLine)), Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), - Files(Files), PCHContainerOps(PCHContainerOps), DiagConsumer(nullptr) {} + Files(Files), PCHContainerOps(std::move(PCHContainerOps)), + DiagConsumer(nullptr) {} ToolInvocation::~ToolInvocation() { if (OwnsAction) @@ -260,7 +268,7 @@ bool ToolInvocation::run() { Input.release()); } return runInvocation(BinaryName, Compilation.get(), Invocation.release(), - PCHContainerOps); + std::move(PCHContainerOps)); } bool ToolInvocation::runInvocation( @@ -274,7 +282,7 @@ bool ToolInvocation::runInvocation( llvm::errs() << "\n"; } - return Action->runInvocation(Invocation, Files, PCHContainerOps, + return Action->runInvocation(Invocation, Files, std::move(PCHContainerOps), DiagConsumer); } @@ -283,7 +291,7 @@ bool FrontendActionFactory::runInvocation( std::shared_ptr<PCHContainerOperations> PCHContainerOps, DiagnosticConsumer *DiagConsumer) { // Create a compiler instance to handle the actual work. - clang::CompilerInstance Compiler(PCHContainerOps); + clang::CompilerInstance Compiler(std::move(PCHContainerOps)); Compiler.setInvocation(Invocation); Compiler.setFileManager(Files); @@ -309,7 +317,7 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : Compilations(Compilations), SourcePaths(SourcePaths), - PCHContainerOps(PCHContainerOps), + PCHContainerOps(std::move(PCHContainerOps)), OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())), InMemoryFileSystem(new vfs::InMemoryFileSystem), Files(new FileManager(FileSystemOptions(), OverlayFileSystem)), @@ -327,26 +335,32 @@ void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { if (ArgsAdjuster) - ArgsAdjuster = combineAdjusters(ArgsAdjuster, Adjuster); + ArgsAdjuster = + combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster)); else - ArgsAdjuster = Adjuster; + ArgsAdjuster = std::move(Adjuster); } void ClangTool::clearArgumentsAdjusters() { ArgsAdjuster = nullptr; } +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. + Args.push_back("-resource-dir=" + + CompilerInvocation::GetResourcesPath(Argv0, MainAddr)); +} + int ClangTool::run(ToolAction *Action) { // Exists solely for the purpose of lookup of the resource path. // This just needs to be some symbol in the binary. static int StaticSymbol; - // The driver detects the builtin header path based on the path of the - // executable. - // 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. - std::string MainExecutable = - llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); llvm::SmallString<128> InitialDirectory; if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) @@ -411,7 +425,17 @@ int ClangTool::run(ToolAction *Action) { if (ArgsAdjuster) CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename); assert(!CommandLine.empty()); - CommandLine[0] = MainExecutable; + + // 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); + // FIXME: We need a callback mechanism for the tool writer to output a // customized message for each file. DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); @@ -446,7 +470,7 @@ public: std::shared_ptr<PCHContainerOperations> PCHContainerOps, DiagnosticConsumer *DiagConsumer) override { std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( - Invocation, PCHContainerOps, + Invocation, std::move(PCHContainerOps), CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), DiagConsumer, /*ShouldOwnClient=*/false), @@ -458,7 +482,6 @@ public: return true; } }; - } int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) { @@ -470,12 +493,12 @@ std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code, const Twine &FileName, std::shared_ptr<PCHContainerOperations> PCHContainerOps) { return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName, - PCHContainerOps); + "clang-tool", std::move(PCHContainerOps)); } std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( const Twine &Code, const std::vector<std::string> &Args, - const Twine &FileName, + const Twine &FileName, const Twine &ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); @@ -489,8 +512,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( OverlayFileSystem->pushOverlay(InMemoryFileSystem); llvm::IntrusiveRefCntPtr<FileManager> Files( new FileManager(FileSystemOptions(), OverlayFileSystem)); - ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, - Files.get(), PCHContainerOps); + ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef), + &Action, Files.get(), std::move(PCHContainerOps)); SmallString<1024> CodeStorage; InMemoryFileSystem->addFile(FileNameRef, 0, |
