Skip to content

Commit

Permalink
feat(verifier): add a flag to elide nodes not bound as goals (#5269)
Browse files Browse the repository at this point in the history
* feat(verifier): add a flag to elide nodes not bound as goals

* chore: fix typos

* chore: final typo
  • Loading branch information
shahms committed May 2, 2022
1 parent 6e551c5 commit 2d4bc69
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
24 changes: 20 additions & 4 deletions kythe/cxx/verifier/verifier.cc
Expand Up @@ -1546,6 +1546,12 @@ void Verifier::DumpAsDot() {
return std::string();
}
};
auto ElideNode = [&](AstNode* node) {
if (show_unlabeled_) {
return false;
}
return GetLabel(node).empty();
};
std::sort(facts_.begin(), facts_.end(), GraphvizSortOrder);
FileHandlePrettyPrinter printer(stdout);
QuoteEscapingPrettyPrinter quote_printer(printer);
Expand All @@ -1555,11 +1561,7 @@ void Verifier::DumpAsDot() {
for (size_t i = 0; i < facts_.size(); ++i) {
AstNode* fact = facts_[i];
Tuple* t = fact->AsApp()->rhs()->AsTuple();
printer.Print("\"");
t->element(0)->Dump(symbol_table_, &quote_printer);
printer.Print("\"");
if (t->element(1) == empty_string_id()) {
std::string label = GetLabel(t->element(0));
// Node. We sorted these above st all the facts should come subsequent.
// Figure out if the node is an anchor.
bool is_anchor_node = false;
Expand All @@ -1582,6 +1584,14 @@ void Verifier::DumpAsDot() {
}
}
}
if (ElideNode(t->element(0))) {
--i;
continue;
}
printer.Print("\"");
t->element(0)->Dump(symbol_table_, &quote_printer);
printer.Print("\"");
std::string label = GetLabel(t->element(0));
if (is_anchor_node && !show_anchors_) {
printer.Print(" [ shape=circle, label=\"@");
printer.Print(label);
Expand Down Expand Up @@ -1625,6 +1635,12 @@ void Verifier::DumpAsDot() {
--i; // Don't skip the fact following the block.
} else {
// Edge.
if (ElideNode(t->element(0)) || ElideNode(t->element(2))) {
continue;
}
printer.Print("\"");
t->element(0)->Dump(symbol_table_, &quote_printer);
printer.Print("\"");
printer.Print(" -> \"");
t->element(2)->Dump(symbol_table_, &quote_printer);
printer.Print("\" [ label=\"");
Expand Down
6 changes: 6 additions & 0 deletions kythe/cxx/verifier/verifier.h
Expand Up @@ -182,6 +182,9 @@ class Verifier {
/// \brief Show anchor locations in graph dumps (instead of @).
void ShowAnchors() { show_anchors_ = true; }

/// \brief Elide unlabeled nodes from graph dumps.
void ElideUnlabeled() { show_unlabeled_ = false; }

/// \brief Check for singleton EVars.
/// \return true if there were singletons.
bool CheckForSingletonEVars() { return parser_.CheckForSingletonEVars(); }
Expand Down Expand Up @@ -336,6 +339,9 @@ class Verifier {
/// If true, show anchor locations in graph dumps (instead of @).
bool show_anchors_ = false;

/// If true, show unlabeled nodes in graph dumps.
bool show_unlabeled_ = true;

/// Identifier for MarkedSource child edges.
AstNode* marked_source_child_id_;

Expand Down
10 changes: 9 additions & 1 deletion kythe/cxx/verifier/verifier_main.cc
Expand Up @@ -41,6 +41,8 @@ ABSL_FLAG(bool, graphviz, false,
"Only dump facts as a GraphViz-compatible graph");
ABSL_FLAG(bool, annotated_graphviz, false,
"Solve and annotate a GraphViz graph.");
ABSL_FLAG(bool, minimal_graphviz, false,
"Solve and dump a GraphViz graph eliding unused nodes.");
ABSL_FLAG(std::string, goal_prefix, "//-", "Denote goals with this string.");
ABSL_FLAG(bool, use_file_nodes, false,
"Look for assertions in UTF8 file nodes.");
Expand Down Expand Up @@ -91,6 +93,11 @@ invocation and rule syntax.
v.SaveEVarAssignments();
}

if (absl::GetFlag(FLAGS_minimal_graphviz)) {
v.SaveEVarAssignments();
v.ElideUnlabeled();
}

if (absl::GetFlag(FLAGS_use_file_nodes)) {
v.UseFileNodes();
}
Expand Down Expand Up @@ -176,7 +183,8 @@ invocation and rule syntax.
}

if (absl::GetFlag(FLAGS_graphviz) ||
absl::GetFlag(FLAGS_annotated_graphviz)) {
absl::GetFlag(FLAGS_annotated_graphviz) ||
absl::GetFlag(FLAGS_minimal_graphviz)) {
v.DumpAsDot();
}

Expand Down

0 comments on commit 2d4bc69

Please sign in to comment.