Factor out C-string argv building and simplify vlogs (#6478)

This extracts the C-string `argv`-like building routine to a more
broadly reusable location. It also sinks the verbose logging logic out
of it and into the relevant runners. In turn, it simplifies the verbose
logging logic significantly.

The biggest functional change is removing the implicit synthesis of a
tool's `-v` verbose flag from the presence of a `vlog` stream. I thought
this would be helpful, but in practice of debugging these layers it has
been more of a hindrance than a help -- I pretty often only want verbose
logging on one side or the other, and we have ways of explicitly passing
a `-v` flag to the underlying tools already. I think my instinct to do
this was just wrong, so rip it out and simplify.

This does add an unused feature -- prepending a prefix of arguments
while building the C-string variant. This isn't used in this PR but will
be used in subsequent PRs and it seemed more disruptive to undo that
logic and then re-do it in a later PR. Let me know if it's too confusing
here.

Assisted-by: Gemini Code Assist

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Chandler Carruth
2025-12-10 22:07:01 +00:00
committed by GitHub
co-authored by Geoff Romer
parent 1c3d3e9284
commit ff8ce31e1b
9 changed files with 180 additions and 82 deletions
-54
View File
@@ -18,58 +18,4 @@ ToolRunnerBase::ToolRunnerBase(const InstallPaths* install_paths,
llvm::raw_ostream* vlog_stream)
: installation_(install_paths), vlog_stream_(vlog_stream) {}
auto ToolRunnerBase::BuildCStrArgs(llvm::StringRef tool_name,
llvm::StringRef tool_path,
std::optional<llvm::StringRef> verbose_flag,
llvm::ArrayRef<llvm::StringRef> args,
llvm::OwningArrayRef<char>& cstr_arg_storage)
-> llvm::SmallVector<const char*, 64> {
// TODO: Maybe handle response file expansion similar to the Clang CLI?
// If we have a verbose logging stream, and that stream is the same as
// `llvm::errs`, then add the `-v` flag so that the driver also prints verbose
// information.
bool inject_v_arg = verbose_flag.has_value() && vlog_stream_ == &llvm::errs();
std::array<llvm::StringRef, 1> v_arg_storage;
llvm::ArrayRef<llvm::StringRef> maybe_v_arg;
if (inject_v_arg) {
v_arg_storage[0] = *verbose_flag;
maybe_v_arg = v_arg_storage;
}
CARBON_VLOG("Running {} driver with arguments:\n", tool_name);
// Render the arguments into null-terminated C-strings. Command lines can get
// quite long in build systems so this tries to minimize the memory allocation
// overhead.
// Provide the wrapped tool path as the synthetic `argv[0]`.
std::array<llvm::StringRef, 1> exe_arg = {tool_path};
auto args_range =
llvm::concat<const llvm::StringRef>(exe_arg, maybe_v_arg, args);
int total_size = 0;
for (llvm::StringRef arg : args_range) {
// Accumulate both the string size and a null terminator byte.
total_size += arg.size() + 1;
}
// Allocate one chunk of storage for the actual C-strings and a vector of
// pointers into the storage.
cstr_arg_storage = llvm::OwningArrayRef<char>(total_size);
llvm::SmallVector<const char*, 64> cstr_args;
cstr_args.reserve(args.size() + inject_v_arg + 1);
for (ssize_t i = 0; llvm::StringRef arg : args_range) {
cstr_args.push_back(&cstr_arg_storage[i]);
memcpy(&cstr_arg_storage[i], arg.data(), arg.size());
i += arg.size();
cstr_arg_storage[i] = '\0';
++i;
}
for (const char* cstr_arg : llvm::ArrayRef(cstr_args)) {
CARBON_VLOG(" '{0}'\n", cstr_arg);
}
return cstr_args;
}
} // namespace Carbon