Skip to content

Commit

Permalink
[Tooling] Add "-filter" option to AllTUsExecution
Browse files Browse the repository at this point in the history
Summary: We can run the tools on a subset files of compilation database.

Reviewers: ioeric

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D54092

llvm-svn: 346131
  • Loading branch information
hokein committed Nov 5, 2018
1 parent 5904c41 commit cd5e59f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Tooling/AllTUsExecution.h
Expand Up @@ -72,6 +72,8 @@ class AllTUsToolExecutor : public ToolExecutor {
unsigned ThreadCount;
};

extern llvm::cl::opt<std::string> Filter;

} // end namespace tooling
} // end namespace clang

Expand Down
12 changes: 11 additions & 1 deletion clang/lib/Tooling/AllTUsExecution.cpp
Expand Up @@ -53,6 +53,12 @@ class ThreadSafeToolResults : public ToolResults {

} // namespace

llvm::cl::opt<std::string>
Filter("filter",
llvm::cl::desc("Only process files that match this filter. "
"This flag only applies to all-TUs."),
llvm::cl::init(".*"));

AllTUsToolExecutor::AllTUsToolExecutor(
const CompilationDatabase &Compilations, unsigned ThreadCount,
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
Expand Down Expand Up @@ -110,7 +116,10 @@ llvm::Error AllTUsToolExecutor::execute(
llvm::errs() << "Error while getting current working directory: "
<< EC.message() << "\n";
}
llvm::Regex RegexFilter(Filter);
for (std::string File : Files) {
if (!RegexFilter.match(File))
continue;
Pool.async(
[&](std::string Path) {
Log("[" + std::to_string(Count()) + "/" + TotalNumStr +
Expand Down Expand Up @@ -147,7 +156,8 @@ llvm::Error AllTUsToolExecutor::execute(
static llvm::cl::opt<unsigned> ExecutorConcurrency(
"execute-concurrency",
llvm::cl::desc("The number of threads used to process all files in "
"parallel. Set to 0 for hardware concurrency."),
"parallel. Set to 0 for hardware concurrency. "
"This flag only applies to all-TUs."),
llvm::cl::init(0));

class AllTUsToolExecutorPlugin : public ToolExecutorPlugin {
Expand Down
7 changes: 5 additions & 2 deletions clang/unittests/Tooling/ExecutionTest.cpp
Expand Up @@ -248,19 +248,22 @@ class FixedCompilationDatabaseWithFiles : public CompilationDatabase {
MATCHER_P(Named, Name, "") { return arg.first == Name; }

TEST(AllTUsToolTest, AFewFiles) {
FixedCompilationDatabaseWithFiles Compilations(".", {"a.cc", "b.cc", "c.cc"},
std::vector<std::string>());
FixedCompilationDatabaseWithFiles Compilations(
".", {"a.cc", "b.cc", "c.cc", "ignore.cc"}, std::vector<std::string>());
AllTUsToolExecutor Executor(Compilations, /*ThreadCount=*/0);
Filter.setValue("[a-c].cc");
Executor.mapVirtualFile("a.cc", "void x() {}");
Executor.mapVirtualFile("b.cc", "void y() {}");
Executor.mapVirtualFile("c.cc", "void z() {}");
Executor.mapVirtualFile("ignore.cc", "void d() {}");

auto Err = Executor.execute(std::unique_ptr<FrontendActionFactory>(
new ReportResultActionFactory(Executor.getExecutionContext())));
ASSERT_TRUE(!Err);
EXPECT_THAT(
Executor.getToolResults()->AllKVResults(),
::testing::UnorderedElementsAre(Named("x"), Named("y"), Named("z")));
Filter.setValue(".*"); // reset to default value.
}

TEST(AllTUsToolTest, ManyFiles) {
Expand Down

0 comments on commit cd5e59f

Please sign in to comment.