[clang] bdc20d6 - [clang][tooling] Allow -cc1 arguments in ToolInvocation
Ben Langmuir via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 24 19:51:26 PDT 2022
Author: Ben Langmuir
Date: 2022-08-24T19:51:12-07:00
New Revision: bdc20d61b8e4a62ea00e1eb05ba87eefaf820434
URL: https://github.com/llvm/llvm-project/commit/bdc20d61b8e4a62ea00e1eb05ba87eefaf820434
DIFF: https://github.com/llvm/llvm-project/commit/bdc20d61b8e4a62ea00e1eb05ba87eefaf820434.diff
LOG: [clang][tooling] Allow -cc1 arguments in ToolInvocation
ToolInvocation is useful even if you already have a -cc1 invocation,
since it provides a standard way to setup diagnostics, parse arguments,
and handoff to a ToolAction. So teach it to support -cc1 commands by
skipping the driver bits.
Differential Revision: https://reviews.llvm.org/D132615
Added:
Modified:
clang/include/clang/Tooling/Tooling.h
clang/lib/Tooling/Tooling.cpp
clang/unittests/Tooling/ToolingTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h
index b36f58ad3046..52a81cd9e778 100644
--- a/clang/include/clang/Tooling/Tooling.h
+++ b/clang/include/clang/Tooling/Tooling.h
@@ -508,7 +508,7 @@ void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
/// Creates a \c CompilerInvocation.
CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
- const llvm::opt::ArgStringList &CC1Args,
+ ArrayRef<const char *> CC1Args,
const char *const BinaryName);
} // namespace tooling
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 6314615f83c8..65d850842432 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -161,7 +161,7 @@ getCC1Arguments(DiagnosticsEngine *Diagnostics,
/// Returns a clang build invocation initialized from the CC1 flags.
CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
- const llvm::opt::ArgStringList &CC1Args,
+ ArrayRef<const char *> CC1Args,
const char *const BinaryName) {
assert(!CC1Args.empty() && "Must at least contain the program name!");
CompilerInvocation *Invocation = new CompilerInvocation;
@@ -339,7 +339,7 @@ ToolInvocation::~ToolInvocation() {
}
bool ToolInvocation::run() {
- std::vector<const char*> Argv;
+ llvm::opt::ArgStringList Argv;
for (const std::string &Str : CommandLine)
Argv.push_back(Str.c_str());
const char *const BinaryName = Argv[0];
@@ -362,6 +362,17 @@ bool ToolInvocation::run() {
SourceManager SrcMgr(*Diagnostics, *Files);
Diagnostics->setSourceManager(&SrcMgr);
+ // We already have a cc1, just create an invocation.
+ if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
+ ArrayRef<const char *> CC1Args = makeArrayRef(Argv).drop_front();
+ std::unique_ptr<CompilerInvocation> Invocation(
+ newInvocation(&*Diagnostics, CC1Args, BinaryName));
+ if (Diagnostics->hasErrorOccurred())
+ return false;
+ return Action->runInvocation(std::move(Invocation), Files,
+ std::move(PCHContainerOps), DiagConsumer);
+ }
+
const std::unique_ptr<driver::Driver> Driver(
newDriver(&*Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
// The "input file not found" diagnostics from the driver are useful.
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index aca343a963da..a476a9e3b510 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -326,6 +326,46 @@ TEST(ToolInvocation, DiagConsumerExpectingSourceManager) {
EXPECT_TRUE(Consumer.SawSourceManager);
}
+TEST(ToolInvocation, CC1Args) {
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
+ OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+ llvm::IntrusiveRefCntPtr<FileManager> Files(
+ new FileManager(FileSystemOptions(), OverlayFileSystem));
+ std::vector<std::string> Args;
+ Args.push_back("tool-executable");
+ Args.push_back("-cc1");
+ Args.push_back("-fsyntax-only");
+ Args.push_back("test.cpp");
+ clang::tooling::ToolInvocation Invocation(
+ Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
+ InMemoryFileSystem->addFile(
+ "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("void foo(void);\n"));
+ EXPECT_TRUE(Invocation.run());
+}
+
+TEST(ToolInvocation, CC1ArgsInvalid) {
+ llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
+ new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
+ OverlayFileSystem->pushOverlay(InMemoryFileSystem);
+ llvm::IntrusiveRefCntPtr<FileManager> Files(
+ new FileManager(FileSystemOptions(), OverlayFileSystem));
+ std::vector<std::string> Args;
+ Args.push_back("tool-executable");
+ Args.push_back("-cc1");
+ Args.push_back("-invalid-arg");
+ Args.push_back("test.cpp");
+ clang::tooling::ToolInvocation Invocation(
+ Args, std::make_unique<SyntaxOnlyAction>(), Files.get());
+ InMemoryFileSystem->addFile(
+ "test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("void foo(void);\n"));
+ EXPECT_FALSE(Invocation.run());
+}
+
namespace {
/// Overlays the real filesystem with the given VFS and returns the result.
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>
More information about the cfe-commits
mailing list