diff options
Diffstat (limited to 'clang/lib/Tooling/Tooling.cpp')
-rw-r--r-- | clang/lib/Tooling/Tooling.cpp | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 40b6cff0d627..79851ac723da 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -78,7 +78,7 @@ newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { driver::Driver *CompilerDriver = new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(), - *Diagnostics, std::move(VFS)); + *Diagnostics, "clang LLVM compiler", std::move(VFS)); CompilerDriver->setTitle("clang_based_tool"); return CompilerDriver; } @@ -245,27 +245,38 @@ std::string getAbsolutePath(StringRef File) { void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, StringRef InvokedAs) { - if (!CommandLine.empty() && !InvokedAs.empty()) { - bool AlreadyHasTarget = false; - bool AlreadyHasMode = false; - // Skip CommandLine[0]. - for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); - ++Token) { - StringRef TokenRef(*Token); - AlreadyHasTarget |= - (TokenRef == "-target" || TokenRef.startswith("-target=")); - AlreadyHasMode |= (TokenRef == "--driver-mode" || - TokenRef.startswith("--driver-mode=")); - } - auto TargetMode = - driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs); - if (!AlreadyHasMode && TargetMode.DriverMode) { - CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode); - } - if (!AlreadyHasTarget && TargetMode.TargetIsValid) { - CommandLine.insert(++CommandLine.begin(), {"-target", - TargetMode.TargetPrefix}); - } + 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(); + 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; + // Skip CommandLine[0]. + for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); + ++Token) { + StringRef TokenRef(*Token); + ShouldAddTarget = ShouldAddTarget && !TokenRef.startswith(TargetOPT) && + !TokenRef.equals(TargetOPTLegacy); + ShouldAddMode = ShouldAddMode && !TokenRef.startswith(DriverModeOPT); + } + if (ShouldAddMode) { + CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode); + } + if (ShouldAddTarget) { + CommandLine.insert(++CommandLine.begin(), + TargetOPT + TargetMode.TargetPrefix); } } @@ -308,12 +319,6 @@ ToolInvocation::~ToolInvocation() { delete Action; } -void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { - SmallString<1024> PathStorage; - llvm::sys::path::native(FilePath, PathStorage); - MappedFileContents[PathStorage] = Content; -} - bool ToolInvocation::run() { std::vector<const char*> Argv; for (const std::string &Str : CommandLine) @@ -348,14 +353,6 @@ bool ToolInvocation::run() { return false; std::unique_ptr<CompilerInvocation> Invocation( newInvocation(&Diagnostics, *CC1Args, BinaryName)); - // FIXME: remove this when all users have migrated! - for (const auto &It : MappedFileContents) { - // Inject the code as the given file name into the preprocessor options. - std::unique_ptr<llvm::MemoryBuffer> Input = - llvm::MemoryBuffer::getMemBuffer(It.getValue()); - Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), - Input.release()); - } return runInvocation(BinaryName, Compilation.get(), std::move(Invocation), std::move(PCHContainerOps)); } @@ -648,7 +645,7 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( if (!Invocation.run()) return nullptr; - + assert(ASTs.size() == 1); return std::move(ASTs[0]); } |