[clang] [Tooling] Fix misleading progress report when files have multiple compile commands (PR #169640)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 27 02:16:44 PST 2025
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/169640
>From 0bcc0a061e4c03f85a2f7481591e97779785f731 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Mon, 24 Nov 2025 10:14:58 +0800
Subject: [PATCH 1/2] [Tooling] Fix misleading progress report when files have
multiple compile commands
---
clang/lib/Tooling/Tooling.cpp | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 9bae12454d2dc..491294b731e85 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -37,6 +37,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptTable.h"
@@ -96,7 +97,7 @@ static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation) {
OffloadCompilation = true;
if (Jobs.size() > 1) {
- for (auto *A : Actions){
+ for (auto *A : Actions) {
// On MacOSX real actions may end up being wrapped in BindArchAction
if (isa<driver::BindArchAction>(A))
A = *A->input_begin();
@@ -413,8 +414,8 @@ bool ToolInvocation::run() {
Driver->BuildCompilation(llvm::ArrayRef(Argv)));
if (!Compilation)
return false;
- const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
- &*Diagnostics, Compilation.get());
+ const llvm::opt::ArgStringList *const CC1Args =
+ getCC1Arguments(&*Diagnostics, Compilation.get());
if (!CC1Args)
return false;
std::unique_ptr<CompilerInvocation> Invocation(
@@ -497,9 +498,7 @@ void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
}
-void ClangTool::clearArgumentsAdjusters() {
- ArgsAdjuster = nullptr;
-}
+void ClangTool::clearArgumentsAdjusters() { ArgsAdjuster = nullptr; }
static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
void *MainAddr) {
@@ -555,8 +554,9 @@ int ClangTool::run(ToolAction *Action) {
}
size_t NumOfTotalFiles = AbsolutePaths.size();
- unsigned ProcessedFileCounter = 0;
+ unsigned CurrentFileIndex = 0;
for (llvm::StringRef File : AbsolutePaths) {
+ ++CurrentFileIndex; // Increment file counter once per file.
// 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
@@ -571,6 +571,7 @@ int ClangTool::run(ToolAction *Action) {
FileSkipped = true;
continue;
}
+ unsigned CurrentCommandIndexForFile = 0;
for (CompileCommand &CompileCommand : CompileCommandsForFile) {
// If the 'directory' field of the compilation database is empty, display
// an error and use the working directory instead.
@@ -617,12 +618,16 @@ int ClangTool::run(ToolAction *Action) {
// pass in made-up names here. Make sure this works on other platforms.
injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
+ ++CurrentCommandIndexForFile;
+
// FIXME: We need a callback mechanism for the tool writer to output a
// customized message for each file.
- if (NumOfTotalFiles > 1)
- llvm::errs() << "[" + std::to_string(++ProcessedFileCounter) + "/" +
- std::to_string(NumOfTotalFiles) +
- "] Processing file " + File
+ if (NumOfTotalFiles > 1 || CompileCommandsForFile.size() > 1)
+ llvm::errs() << "[" + std::to_string(CurrentFileIndex) + "/" +
+ std::to_string(NumOfTotalFiles) + "] (" +
+ std::to_string(CurrentCommandIndexForFile) + "/" +
+ std::to_string(CompileCommandsForFile.size()) +
+ ") Processing file " + File
<< ".\n";
ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
PCHContainerOps);
>From 3df57dec9880788efb6dbd50892fdf5f8a0d3b79 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Thu, 27 Nov 2025 18:14:10 +0800
Subject: [PATCH 2/2] Fix printing format
---
clang/lib/Tooling/Tooling.cpp | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 491294b731e85..2914da9a21888 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -556,7 +556,7 @@ int ClangTool::run(ToolAction *Action) {
size_t NumOfTotalFiles = AbsolutePaths.size();
unsigned CurrentFileIndex = 0;
for (llvm::StringRef File : AbsolutePaths) {
- ++CurrentFileIndex; // Increment file counter once per file.
+ ++CurrentFileIndex;
// 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
@@ -622,13 +622,17 @@ int ClangTool::run(ToolAction *Action) {
// FIXME: We need a callback mechanism for the tool writer to output a
// customized message for each file.
- if (NumOfTotalFiles > 1 || CompileCommandsForFile.size() > 1)
+ if (NumOfTotalFiles > 1 || CompileCommandsForFile.size() > 1) {
llvm::errs() << "[" + std::to_string(CurrentFileIndex) + "/" +
- std::to_string(NumOfTotalFiles) + "] (" +
- std::to_string(CurrentCommandIndexForFile) + "/" +
- std::to_string(CompileCommandsForFile.size()) +
- ") Processing file " + File
- << ".\n";
+ std::to_string(NumOfTotalFiles) + "]";
+ if (CompileCommandsForFile.size() > 1) {
+ llvm::errs() << " (" + std::to_string(CurrentCommandIndexForFile) +
+ "/" +
+ std::to_string(CompileCommandsForFile.size()) +
+ ")";
+ }
+ llvm::errs() << " Processing file " + File << ".\n";
+ }
ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
PCHContainerOps);
Invocation.setDiagnosticConsumer(DiagConsumer);
More information about the cfe-commits
mailing list