Skip to content

Commit

Permalink
Make verbosity a level not only boolean, to show more 'noisy' messages.
Browse files Browse the repository at this point in the history
Supplying `-v` multiple times will increase verbosity.
  • Loading branch information
hzeller committed May 30, 2024
1 parent 5071956 commit 087664b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bant/bant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int main(int argc, char *argv[]) {
}
flags.output_format = found->second;
} break;
case 'v': flags.verbose = true; break;
case 'v': flags.verbose++; break; // More -v, more detail.
case 'V': return print_version();
default: return usage(argv[0], nullptr, EXIT_SUCCESS);
}
Expand Down
20 changes: 11 additions & 9 deletions bant/cli-commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ enum class Command {
void PrintOneToN(bant::Session &session, const BazelPattern &pattern,
const OneToN<BazelTarget, BazelTarget> &table,
const std::string &header1, const std::string &header2) {
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{header1, header2});
auto printer = TablePrinter::Create(
session.out(), session.flags().output_format, {header1, header2});
std::vector<std::string> repeat_print;
for (const auto &d : table) {
if (!pattern.Match(d.first)) continue;
Expand Down Expand Up @@ -136,7 +136,7 @@ CliStatus RunCommand(Session &session, Command cmd,
graph =
bant::BuildDependencyGraph(session, workspace, dep_pattern,
flags.recurse_dependency_depth, &project);
if (session.verbose()) {
if (session.flags().verbose) {
session.info() << "Found " << graph.depends_on.size()
<< " Targets with dependencies; "
<< graph.has_dependents.size()
Expand Down Expand Up @@ -199,17 +199,18 @@ CliStatus RunCommand(Session &session, Command cmd,
break;

case Command::kListPackages: {
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{"bazel-file", "package"});
auto printer = TablePrinter::Create(
session.out(), session.flags().output_format, {"bazel-file", "package"});
for (const auto &[package, parsed] : project.ParsedFiles()) {
printer->AddRow({std::string(parsed->name()), package.ToString()});
}
printer->Finish();
} break;

case Command::kListTargets: {
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{"file-location", "rule", "target"});
auto printer =
TablePrinter::Create(session.out(), session.flags().output_format,
{"file-location", "rule", "target"});
for (const auto &[package, parsed] : project.ParsedFiles()) {
FindTargets(parsed->ast, {}, [&](const Result &target) {
auto target_name =
Expand All @@ -229,8 +230,9 @@ CliStatus RunCommand(Session &session, Command cmd,
case Command::kListWorkkspace: {
// For now, we just load the workspace file in this command. We might need
// it later also to resolve dependencies.
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{"project", "version", "directory"});
auto printer =
TablePrinter::Create(session.out(), session.flags().output_format,
{"project", "version", "directory"});
for (const auto &[project, file] : workspace_or->project_location) {
printer->AddRow({project.project,
project.version.empty() ? "-" : project.version,
Expand Down
2 changes: 1 addition & 1 deletion bant/explore/dependency-graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ DependencyGraph BuildDependencyGraph(Session &session,
deps_to_resolve_todo = next_round_deps_to_resolve_todo;
} while (!deps_to_resolve_todo.empty() && (nesting_depth-- > 0));

if (session.verbose()) {
if (session.flags().verbose) {
// Currently, we have a lot of targets that we don't deal with yet, such as
// genrules or protobuffer rules. Goal: should be zero.
// But for now: hide behind 'verbose' flag, to not be too noisy.
Expand Down
10 changes: 6 additions & 4 deletions bant/explore/header-providers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ std::optional<ProvidedFromTargetSet::const_iterator> FindBySuffix(
void PrintProvidedSources(Session &session, const std::string &table_header,
const BazelPattern &pattern,
const ProvidedFromTarget &provided_from_lib) {
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{table_header, "providing-rule"});
auto printer =
TablePrinter::Create(session.out(), session.flags().output_format,
{table_header, "providing-rule"});
for (const auto &[provided, lib] : provided_from_lib) {
if (!pattern.Match(lib)) continue;
printer->AddRow({provided, lib.ToString()});
Expand All @@ -381,8 +382,9 @@ void PrintProvidedSources(Session &session, const std::string &table_header,
void PrintProvidedSources(Session &session, const std::string &table_header,
const BazelPattern &pattern,
const ProvidedFromTargetSet &provided_from_lib) {
auto printer = TablePrinter::Create(session.out(), session.output_format(),
{table_header, "providing-rule"});
auto printer =
TablePrinter::Create(session.out(), session.flags().output_format,
{table_header, "providing-rule"});
for (const auto &[provided, libs] : provided_from_lib) {
std::vector<std::string> list;
for (const BazelTarget &target : libs) {
Expand Down
2 changes: 1 addition & 1 deletion bant/frontend/elaboration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ElaborationTest : public testing::Test {
elaborated_ = pp_.Add("//elab", to_elaborate);
const ParsedBuildFile *expected_parsed = pp_.Add("//expected", expected);

Session session(&std::cerr, &std::cerr, CommandlineFlags{.verbose = true});
Session session(&std::cerr, &std::cerr, CommandlineFlags{.verbose = 1});
std::stringstream elab_print;
elab_print << bant::Elaborate(session, &pp_.project(), elaborated_->package,
elaborated_->ast);
Expand Down
9 changes: 3 additions & 6 deletions bant/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class SessionStreams {
std::ostream *info_;
};

// Command line flags needed by
// Command line flags filled in main(), used by tools (some only needed
// in some commands)
struct CommandlineFlags {
bool verbose = false;
int verbose = 0;
bool print_ast = false;
bool print_only_errors = false;
bool elaborate = false;
Expand All @@ -69,10 +70,6 @@ class Session {
std::ostream &info() { return streams_.info(); }
std::ostream &error() { return streams_.error(); }

// Deprecated. Use Flags directly.
bool verbose() const { return flags_.verbose; }
OutputFormat output_format() const { return flags_.output_format; }

const CommandlineFlags &flags() const { return flags_; }

// Get a stat object to fill/update. The "subsystem_name" describes who is
Expand Down
2 changes: 1 addition & 1 deletion bant/tool/canon-targets_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cc_library(
)
)");

Session session(&std::cerr, &std::cerr, CommandlineFlags{.verbose = true});
Session session(&std::cerr, &std::cerr, CommandlineFlags{.verbose = 1});
EditExpector edit_expector;
edit_expector.ExpectRename("baz", ":baz");
edit_expector.ExpectRename("//some/path:bar", ":bar");
Expand Down
15 changes: 6 additions & 9 deletions bant/tool/dwyu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
#include "bant/workspace.h"
#include "re2/re2.h"

// Used for messages that are a little bit too noisy right now.
static constexpr bool kNoisy = false;

// Looking for source files directly in the source tree, but if not found
// in the various locations generated files could be.
#define LINK_PREFIX "bazel-"
Expand Down Expand Up @@ -168,10 +165,10 @@ void DWYUGenerator::CreateEditsForTarget(const BazelTarget &target,
// Emit the edits.
if (potential_remove_suggestion_safe) {
emit_deps_edit_(EditRequest::kRemove, target, dependency_target, "");
} else if (!all_header_deps_known && session_.verbose() && kNoisy) {
} else if (!all_header_deps_known && session_.flags().verbose > 1) {
project_.Loc(session_.info(), dependency_target)
<< ": Probably not needed " << dependency_target
<< ", but can't safely remove: not all headers accounted for.\n";
<< ": Unsure what " << requested_target->ToString()
<< " provides, but there are also unaccounted headers. Won't remove.\n";
}
}

Expand All @@ -192,7 +189,7 @@ void DWYUGenerator::CreateEditsForTarget(const BazelTarget &target,
if (CanSee(target, need_add)) {
emit_deps_edit_(EditRequest::kAdd, target, "",
need_add.ToStringRelativeTo(target.package));
} else if (session_.verbose() && kNoisy) {
} else if (session_.flags().verbose > 1) {
project_.Loc(session_.info(), details.name)
<< ": Would add " << need_add << ", but not visible\n";
}
Expand Down Expand Up @@ -389,7 +386,7 @@ DWYUGenerator::DependenciesNeededBySources(
// Do some reporting if fuzzy match hit.
const size_t found_len = found_it->first.length();
const size_t inc_len = inc_file.length();
if (found_len != inc_len && session_.verbose()) {
if (found_len != inc_len && session_.flags().verbose > 1) {
source.Loc(info_out, inc_file)
<< " FYI: instead of '" << inc_file << "' found library that "
<< "provides " << ((found_len < inc_len) ? "shorter" : "longer")
Expand Down Expand Up @@ -438,7 +435,7 @@ DWYUGenerator::DependenciesNeededBySources(

// No luck. Source includes it, but we don't know where it is.
// Be careful with remove suggestion, so consider 'not accounted for'.
if (session_.verbose()) {
if (session_.flags().verbose) {
// Until all common reasons why we don't find a provider is resolved,
// keep this hidden behind verbose.
source.Loc(info_out, inc_file)
Expand Down
2 changes: 1 addition & 1 deletion bant/tool/dwyu_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class DWYUTestFixture {
public:
explicit DWYUTestFixture(const ParsedProject &project)
: session_{&log_messages_, &log_messages_,
CommandlineFlags{.verbose = true}},
CommandlineFlags{.verbose = 2}},
dwyu_(session_, project, edit_expector_.checker()){};

~DWYUTestFixture() {
Expand Down

0 comments on commit 087664b

Please sign in to comment.