Skip to content

Don't use down_cast<> with the assumption that type-mismatch returns … #2423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion verible/common/analysis/matcher/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cc_library(
hdrs = ["bound-symbol-manager.h"],
deps = [
"//verible/common/text:symbol",
"//verible/common/util:casts",
"//verible/common/text:tree-utils",
"//verible/common/util:container-util",
"//verible/common/util:logging",
],
Expand Down
15 changes: 11 additions & 4 deletions verible/common/analysis/matcher/bound-symbol-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include <string>

#include "verible/common/text/symbol.h"
#include "verible/common/util/casts.h"
#include "verible/common/text/tree-utils.h"

namespace verible {
class SyntaxTreeNode;
class SyntaxTreeLeaf;
namespace matcher {

// Manages sets of Bound Symbols created when matching against a syntax tree.
Expand Down Expand Up @@ -52,9 +54,14 @@ class BoundSymbolManager {
return bound_symbols_;
}

template <typename T>
const T *GetAs(const std::string &key) const {
return down_cast<const T *>(FindSymbol(key));
// Return named node as Node if available of of that type.
const SyntaxTreeNode *GetAsNode(const std::string &key) const {
return verible::MaybeNode(FindSymbol(key));
}

// Return named node as Node if available of of that type.
const SyntaxTreeLeaf *GetAsLeaf(const std::string &key) const {
return verible::MaybeLeaf(FindSymbol(key));
}

private:
Expand Down
2 changes: 0 additions & 2 deletions verible/common/text/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ constexpr SymbolTag NodeTag(EnumType tag) {
}
constexpr SymbolTag LeafTag(int tag) { return {SymbolKind::kLeaf, tag}; }

// forward declare Visitor classes to allow references in Symbol

class Symbol {
public:
virtual ~Symbol() = default;
Expand Down
9 changes: 9 additions & 0 deletions verible/common/text/tree-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ const SyntaxTreeLeaf &SymbolCastToLeaf(const Symbol &symbol) {
return down_cast<const SyntaxTreeLeaf &>(symbol);
}

const SyntaxTreeNode *MaybeNode(const Symbol *symbol) {
if (!symbol || symbol->Kind() != SymbolKind::kNode) return nullptr;
return static_cast<const SyntaxTreeNode *>(symbol);
}
const SyntaxTreeLeaf *MaybeLeaf(const Symbol *symbol) {
if (!symbol || symbol->Kind() != SymbolKind::kLeaf) return nullptr;
return static_cast<const SyntaxTreeLeaf *>(symbol);
}

namespace {
// FirstSubtreeFinderMutable is a visitor class that supports the implementation
// of FindFirstSubtreeMutable(). It is derived from
Expand Down
11 changes: 11 additions & 0 deletions verible/common/text/tree-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "verible/common/util/logging.h"
#include "verible/common/util/type-traits.h"

// TODO: the functions below that can crash while asserting a type should
// probably return a usable error (std::optional).

namespace verible {

// Returns the leftmost/rightmost leaf contained in Symbol.
Expand All @@ -47,6 +50,7 @@ std::string_view StringSpanOfSymbol(const Symbol &symbol);
std::string_view StringSpanOfSymbol(const Symbol &lsym, const Symbol &rsym);

// Returns a SyntaxTreeNode down_casted from a Symbol.
// Will panic if not of that kind.
const SyntaxTreeNode &SymbolCastToNode(const Symbol &);
// Mutable variant.
SyntaxTreeNode &SymbolCastToNode(Symbol &); // NOLINT
Expand All @@ -59,8 +63,15 @@ inline const SyntaxTreeNode &SymbolCastToNode(const SyntaxTreeNode &node) {
inline SyntaxTreeNode &SymbolCastToNode(SyntaxTreeNode &node) { return node; }

// Returns a SyntaxTreeLeaf down_casted from a Symbol.
// Will panic if not of that kind.
const SyntaxTreeLeaf &SymbolCastToLeaf(const Symbol &);

// Return Node if symbol is of that kind, otherwise nullptr.
const SyntaxTreeNode *MaybeNode(const Symbol *symbol);

// Return Leaf if symbol is of that kind, otherwise nullptr.
const SyntaxTreeLeaf *MaybeLeaf(const Symbol *symbol);

// Unwrap layers of only-child nodes until reaching a leaf or a node with
// multiple children.
const Symbol *DescendThroughSingletons(const Symbol &symbol);
Expand Down
1 change: 1 addition & 0 deletions verible/verilog/CST/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ cc_library(
"//verible/common/text:concrete-syntax-tree",
"//verible/common/text:symbol",
"//verible/common/text:token-info",
"//verible/common/text:tree-utils",
"//verible/common/util:logging",
"@abseil-cpp//absl/strings",
],
Expand Down
5 changes: 3 additions & 2 deletions verible/verilog/CST/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ const verible::TokenInfo *GetUnaryPrefixOperator(
return nullptr;
}
const verible::Symbol *leaf_symbol = node->front().get();
return &verible::down_cast<const verible::SyntaxTreeLeaf *>(leaf_symbol)
->get();
const verible::SyntaxTreeLeaf *leaf = verible::MaybeLeaf(leaf_symbol);
if (!leaf) return nullptr;
return &leaf->get();
}

const verible::Symbol *GetUnaryPrefixOperand(const verible::Symbol &symbol) {
Expand Down
3 changes: 1 addition & 2 deletions verible/verilog/CST/identifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ const verible::SyntaxTreeLeaf *GetIdentifier(const verible::Symbol &symbol) {
return nullptr;
}
const auto &node = down_cast<const verible::SyntaxTreeNode &>(symbol);
const auto *leaf = down_cast<const verible::SyntaxTreeLeaf *>(node[0].get());
return leaf;
return verible::MaybeLeaf(node[0].get());
}

const verible::SyntaxTreeLeaf *AutoUnwrapIdentifier(
Expand Down
5 changes: 2 additions & 3 deletions verible/verilog/CST/verilog-treebuilder-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
#include "verible/common/text/concrete-syntax-tree.h"
#include "verible/common/text/symbol.h"
#include "verible/common/text/token-info.h"
#include "verible/common/text/tree-utils.h"
#include "verible/common/util/logging.h"

namespace verilog {

using verible::down_cast;

// Set of utility functions for embedded a statement into a certain context.
std::string EmbedInClass(std::string_view text) {
return absl::StrCat("class test_class;\n", text, "\nendclass\n");
Expand All @@ -47,7 +46,7 @@ std::string EmbedInClassMethod(std::string_view text) {
}

void ExpectString(const verible::SymbolPtr &symbol, std::string_view expected) {
const auto *leaf = down_cast<const verible::SyntaxTreeLeaf *>(symbol.get());
const verible::SyntaxTreeLeaf *leaf = verible::MaybeLeaf(symbol.get());
CHECK(leaf != nullptr) << "expected: " << expected;
CHECK_EQ(leaf->get().text(), expected);
}
Expand Down
7 changes: 4 additions & 3 deletions verible/verilog/analysis/checkers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ cc_library(
"//verible/common/text:symbol",
"//verible/common/text:syntax-tree-context",
"//verible/common/text:token-info",
"//verible/common/text:tree-utils",
"//verible/common/util:logging",
"//verible/verilog/CST:numbers",
"//verible/verilog/CST:verilog-matchers",
Expand Down Expand Up @@ -971,6 +972,7 @@ cc_library(
"//verible/common/text:symbol",
"//verible/common/text:syntax-tree-context",
"//verible/common/text:token-info",
"//verible/common/text:tree-utils",
"//verible/verilog/CST:numbers",
"//verible/verilog/CST:verilog-matchers",
"//verible/verilog/analysis:descriptions",
Expand Down Expand Up @@ -1010,7 +1012,7 @@ cc_library(
"//verible/common/text:symbol",
"//verible/common/text:syntax-tree-context",
"//verible/common/text:token-info",
"//verible/common/util:casts",
"//verible/common/text:tree-utils",
"//verible/verilog/CST:expression",
"//verible/verilog/CST:verilog-matchers",
"//verible/verilog/CST:verilog-nonterminals",
Expand Down Expand Up @@ -1156,7 +1158,6 @@ cc_library(
"//verible/common/text:symbol",
"//verible/common/text:syntax-tree-context",
"//verible/common/text:tree-utils",
"//verible/common/util:casts",
"//verible/verilog/CST:verilog-matchers",
"//verible/verilog/CST:verilog-nonterminals",
"//verible/verilog/analysis:descriptions",
Expand Down Expand Up @@ -1195,7 +1196,7 @@ cc_library(
"//verible/common/text:config-utils",
"//verible/common/text:symbol",
"//verible/common/text:syntax-tree-context",
"//verible/common/util:casts",
"//verible/common/text:tree-utils",
"//verible/common/util:logging",
"//verible/verilog/CST:verilog-matchers",
"//verible/verilog/CST:verilog-nonterminals",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "verible/common/text/symbol.h"
#include "verible/common/text/syntax-tree-context.h"
#include "verible/common/text/tree-utils.h"
#include "verible/common/util/casts.h"
#include "verible/verilog/CST/verilog-matchers.h" // IWYU pragma: keep
#include "verible/verilog/CST/verilog-nonterminals.h"
#include "verible/verilog/analysis/descriptions.h"
Expand All @@ -38,7 +37,6 @@ namespace verilog {
namespace analysis {

using verible::AutoFix;
using verible::down_cast;
using verible::LintRuleStatus;
using verible::LintViolation;
using verible::SearchSyntaxTree;
Expand Down Expand Up @@ -77,8 +75,8 @@ void AlwaysCombBlockingRule::HandleSymbol(const verible::Symbol &symbol,
SearchSyntaxTree(symbol, NodekNonblockingAssignmentStatement())) {
if (match.match->Kind() != verible::SymbolKind::kNode) continue;

const auto *node =
down_cast<const verible::SyntaxTreeNode *>(match.match);
const verible::SyntaxTreeNode *node = verible::MaybeNode(match.match);
if (!node) return;

const verible::SyntaxTreeLeaf *leaf = verible::GetSubtreeAsLeaf(
*node, NodeEnum::kNonblockingAssignmentStatement, 1);
Expand Down
35 changes: 14 additions & 21 deletions verible/verilog/analysis/checkers/always-ff-non-blocking-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "verible/common/text/config-utils.h"
#include "verible/common/text/symbol.h"
#include "verible/common/text/syntax-tree-context.h"
#include "verible/common/util/casts.h"
#include "verible/common/text/tree-utils.h"
#include "verible/common/util/logging.h"
#include "verible/verilog/CST/verilog-matchers.h"
#include "verible/verilog/CST/verilog-nonterminals.h"
Expand Down Expand Up @@ -108,22 +108,18 @@ void AlwaysFFNonBlockingRule::HandleSymbol(const verible::Symbol &symbol,

verible::matcher::BoundSymbolManager symbol_man;
if (asgn_blocking_matcher.Matches(symbol, &symbol_man)) {
if (const auto *const node =
verible::down_cast<const verible::SyntaxTreeNode *>(&symbol)) {
check_root =
/* lhs */ verible::down_cast<const verible::SyntaxTreeNode *>(
node->front().get());
if (const verible::SyntaxTreeNode *const node =
verible::MaybeNode(&symbol)) {
check_root = /* lhs */ verible::MaybeNode(node->front().get());
}
} else {
// Not interested in any other blocking assignments unless flagged
if (!catch_modifying_assignments_) return;

if (asgn_modify_matcher.Matches(symbol, &symbol_man)) {
if (const auto *const node =
verible::down_cast<const verible::SyntaxTreeNode *>(&symbol)) {
check_root =
/* lhs */ verible::down_cast<const verible::SyntaxTreeNode *>(
node->front().get());
if (const verible::SyntaxTreeNode *const node =
verible::MaybeNode(&symbol)) {
check_root = /* lhs */ verible::MaybeNode(node->front().get());
}
} else if (asgn_incdec_matcher.Matches(symbol, &symbol_man)) {
check_root = &symbol;
Expand All @@ -144,11 +140,9 @@ void AlwaysFFNonBlockingRule::HandleSymbol(const verible::Symbol &symbol,
if (var.context.IsInside(NodeEnum::kHierarchyExtension)) continue;

bool found = false;
if (const auto *const varn =
verible::down_cast<const verible::SyntaxTreeNode *>(var.match)) {
if (const auto *const ident =
verible::down_cast<const verible::SyntaxTreeLeaf *>(
varn->front().get())) {
if (const verible::SyntaxTreeNode *const varn =
verible::MaybeNode(var.match)) {
if (const auto *const ident = verible::MaybeLeaf(varn->front().get())) {
found = std::find(locals_.begin(), locals_.end(),
ident->get().text()) != locals_.end();
VLOG(4) << "LHS='" << ident->get().text() << "' FOUND=" << found
Expand Down Expand Up @@ -210,11 +204,10 @@ bool AlwaysFFNonBlockingRule::LocalDeclaration(const verible::Symbol &symbol) {
if (decl_matcher.Matches(symbol, &symbol_man)) {
auto &count = scopes_.top().inherited_local_count;
for (const auto &var : SearchSyntaxTree(symbol, var_matcher)) {
if (const auto *const node =
verible::down_cast<const verible::SyntaxTreeNode *>(var.match)) {
if (const auto *const ident =
verible::down_cast<const verible::SyntaxTreeLeaf *>(
node->front().get())) {
if (const verible::SyntaxTreeNode *const node =
verible::MaybeNode(var.match)) {
if (const verible::SyntaxTreeLeaf *const ident =
verible::MaybeLeaf(node->front().get())) {
const std::string_view name = ident->get().text();
VLOG(4) << "Registering '" << name << '\'' << std::endl;
locals_.emplace_back(name);
Expand Down
32 changes: 13 additions & 19 deletions verible/verilog/analysis/checkers/create-object-name-match-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "verible/common/text/symbol.h"
#include "verible/common/text/syntax-tree-context.h"
#include "verible/common/text/token-info.h"
#include "verible/common/util/casts.h"
#include "verible/common/text/tree-utils.h"
#include "verible/verilog/CST/expression.h"
#include "verible/verilog/CST/verilog-matchers.h"
#include "verible/verilog/CST/verilog-nonterminals.h"
Expand All @@ -40,7 +40,6 @@
namespace verilog {
namespace analysis {

using verible::down_cast;
using verible::LintRuleStatus;
using verible::LintViolation;
using verible::SyntaxTreeContext;
Expand Down Expand Up @@ -89,8 +88,7 @@ static bool UnqualifiedIdEquals(const SyntaxTreeNode &node,
if (node.MatchesTag(NodeEnum::kUnqualifiedId)) {
if (!node.empty()) {
// The one-and-only child is the SymbolIdentifier token
const auto &leaf_ptr =
down_cast<const SyntaxTreeLeaf *>(node.front().get());
const SyntaxTreeLeaf *leaf_ptr = verible::MaybeLeaf(node.front().get());
if (leaf_ptr != nullptr) {
const TokenInfo &token = leaf_ptr->get();
return token.token_enum() == SymbolIdentifier && token.text() == name;
Expand All @@ -109,10 +107,11 @@ static bool QualifiedCallIsTypeIdCreate(
// my_pkg::class_type::type_id::create.
// 5: 3 segments + 2 separators (in alternation), e.g. A::B::C
if (qualified_id_node.size() >= 5) {
const auto *create_leaf_ptr =
down_cast<const SyntaxTreeNode *>(qualified_id_node.back().get());
const auto *type_id_leaf_ptr = down_cast<const SyntaxTreeNode *>(
qualified_id_node[num_children - 3].get());
// TODO: naming is off. These are not leafs
const SyntaxTreeNode *create_leaf_ptr =
verible::MaybeNode(qualified_id_node.back().get());
const SyntaxTreeNode *type_id_leaf_ptr =
verible::MaybeNode(qualified_id_node[num_children - 3].get());
if (create_leaf_ptr != nullptr && type_id_leaf_ptr != nullptr) {
return UnqualifiedIdEquals(*create_leaf_ptr, "create") &&
UnqualifiedIdEquals(*type_id_leaf_ptr, "type_id");
Expand All @@ -138,12 +137,7 @@ static const TokenInfo *ExtractStringLiteralToken(
if (!expr_node.MatchesTag(NodeEnum::kExpression)) return nullptr;

// this check is limited to only checking string literal leaf tokens
if (expr_node.front().get()->Kind() != verible::SymbolKind::kLeaf) {
return nullptr;
}

const auto *leaf_ptr =
down_cast<const SyntaxTreeLeaf *>(expr_node.front().get());
const SyntaxTreeLeaf *leaf_ptr = verible::MaybeLeaf(expr_node.front().get());
if (leaf_ptr != nullptr) {
const TokenInfo &token = leaf_ptr->get();
if (token.token_enum() == TK_StringLiteral) {
Expand All @@ -158,8 +152,8 @@ static const SyntaxTreeNode *GetFirstExpressionFromArgs(
const SyntaxTreeNode &args_node) {
if (!args_node.empty()) {
const auto &first_arg = args_node.front();
if (const auto *first_expr =
down_cast<const SyntaxTreeNode *>(first_arg.get())) {
if (const SyntaxTreeNode *first_expr =
verible::MaybeNode(first_arg.get())) {
return first_expr;
}
}
Expand All @@ -183,15 +177,15 @@ void CreateObjectNameMatchRule::HandleSymbol(const verible::Symbol &symbol,

// Extract named bindings for matched nodes within this match.

const auto *lval_ref = manager.GetAs<SyntaxTreeNode>("lval_ref");
const SyntaxTreeNode *lval_ref = manager.GetAsNode("lval_ref");
if (lval_ref == nullptr) return;

const TokenInfo *lval_id = ReferenceIsSimpleIdentifier(*lval_ref);
if (lval_id == nullptr) return;
if (lval_id->token_enum() != SymbolIdentifier) return;

const auto *call = manager.GetAs<SyntaxTreeNode>("func");
const auto *args = manager.GetAs<SyntaxTreeNode>("args");
const SyntaxTreeNode *call = manager.GetAsNode("func");
const SyntaxTreeNode *args = manager.GetAsNode("args");
if (call == nullptr) return;
if (args == nullptr) return;
if (!QualifiedCallIsTypeIdCreate(*call)) return;
Expand Down
2 changes: 1 addition & 1 deletion verible/verilog/analysis/checkers/forbidden-macro-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void ForbiddenMacroRule::HandleSymbol(
const verible::Symbol &symbol, const verible::SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
if (MacroCallMatcher().Matches(symbol, &manager)) {
if (const auto *leaf = manager.GetAs<verible::SyntaxTreeLeaf>("name")) {
if (const verible::SyntaxTreeLeaf *leaf = manager.GetAsLeaf("name")) {
const auto &imm = InvalidMacrosMap();
if (imm.find(std::string(leaf->get().text())) != imm.end()) {
violations_.insert(
Expand Down
2 changes: 1 addition & 1 deletion verible/verilog/analysis/checkers/forbidden-symbol-rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void ForbiddenSystemTaskFunctionRule::HandleSymbol(
const verible::Symbol &symbol, const verible::SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
if (IdMatcher().Matches(symbol, &manager)) {
if (const auto *leaf = manager.GetAs<verible::SyntaxTreeLeaf>("name")) {
if (const verible::SyntaxTreeLeaf *leaf = manager.GetAsLeaf("name")) {
const auto &ism = InvalidSymbolsMap();
if (ism.find(std::string(leaf->get().text())) != ism.end()) {
violations_.insert(
Expand Down
Loading
Loading