Skip to content

Commit

Permalink
Merge pull request #1125 from hzeller/bubble-up-nullptr
Browse files Browse the repository at this point in the history
Have GetSubtreeAsLeaf() return a pointer instead of a reference.
  • Loading branch information
hzeller authored Dec 21, 2021
2 parents 9f0278e + e84b636 commit 034c05e
Show file tree
Hide file tree
Showing 34 changed files with 289 additions and 179 deletions.
10 changes: 4 additions & 6 deletions common/text/tree_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,16 @@ typename match_const<SyntaxTreeNode, S>::type& GetSubtreeAsNode(
}

// Same as GetSubtreeAsSymbol, but casts the result to a leaf.
// If subtree does not exist, returns nullptr.
template <class S, class E>
const SyntaxTreeLeaf& GetSubtreeAsLeaf(const S& symbol,
const SyntaxTreeLeaf* GetSubtreeAsLeaf(const S& symbol,
E parent_must_be_node_enum,
size_t child_position) {
internal::MustBeCSTSymbolOrNode(symbol);
const Symbol* subtree =
GetSubtreeAsSymbol(symbol, parent_must_be_node_enum, child_position);
// TODO(ikr): avoid this check-failure.
CHECK(subtree != nullptr)
<< symbol.Kind() << " e:" << parent_must_be_node_enum
<< " p:" << child_position;
return SymbolCastToLeaf(*subtree);
if (!subtree) return nullptr;
return &SymbolCastToLeaf(*subtree);
}

template <class S, class E>
Expand Down
4 changes: 2 additions & 2 deletions common/text/tree_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1435,8 +1435,8 @@ TEST(GetSubtreeAsNodeTest, ValidatedFoundNodeEnumChildMismatches) {

TEST(GetSubtreeAsLeafTest, ValidatedFoundLeaf) {
auto root = TNode(FakeEnum::kZero, Leaf(7, "foo"));
const auto& leaf = GetSubtreeAsLeaf(*root, FakeEnum::kZero, 0);
EXPECT_EQ(leaf.get().token_enum(), 7);
const SyntaxTreeLeaf* leaf = GetSubtreeAsLeaf(*root, FakeEnum::kZero, 0);
EXPECT_EQ(leaf->get().token_enum(), 7);
}

TEST(GetSubtreeAsLeafTest, GotNodeInsteadOfLeaf) {
Expand Down
12 changes: 6 additions & 6 deletions verilog/CST/class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ const verible::SyntaxTreeNode& GetClassHeader(
const verible::SyntaxTreeLeaf& GetClassName(
const verible::Symbol& class_declaration) {
const auto& header_node = GetClassHeader(class_declaration);
const auto& name_leaf =
const verible::SyntaxTreeLeaf* name_leaf =
verible::GetSubtreeAsLeaf(header_node, NodeEnum::kClassHeader, 3);
return name_leaf;
// TODO(hzeller): bubble up nullptr.
return *ABSL_DIE_IF_NULL(name_leaf);
}

const verible::SyntaxTreeNode* GetExtendedClass(
Expand All @@ -79,9 +80,8 @@ const verible::SyntaxTreeLeaf* GetClassEndLabel(
if (label_node == nullptr) {
return nullptr;
}
const auto& class_name = verible::GetSubtreeAsLeaf(
verible::SymbolCastToNode(*label_node), NodeEnum::kLabel, 1);
return &class_name;
return verible::GetSubtreeAsLeaf(verible::SymbolCastToNode(*label_node),
NodeEnum::kLabel, 1);
}

const verible::SyntaxTreeNode& GetClassItemList(
Expand Down Expand Up @@ -112,7 +112,7 @@ const verible::SyntaxTreeNode& GetClassConstructorStatementList(
NodeEnum::kClassConstructor, 2);
}

const verible::SyntaxTreeLeaf& GetNewKeywordFromClassConstructor(
const verible::SyntaxTreeLeaf* GetNewKeywordFromClassConstructor(
const verible::Symbol& class_constructor) {
const verible::SyntaxTreeNode& constructor_prototype =
verible::GetSubtreeAsNode(class_constructor, NodeEnum::kClassConstructor,
Expand Down
2 changes: 1 addition & 1 deletion verilog/CST/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const verible::SyntaxTreeNode& GetClassConstructorStatementList(
const verible::Symbol& class_constructor);

// Returns the leaf spanning the "new" keyword from class constructor.
const verible::SyntaxTreeLeaf& GetNewKeywordFromClassConstructor(
const verible::SyntaxTreeLeaf* GetNewKeywordFromClassConstructor(
const verible::Symbol& class_constructor);

} // namespace verilog
Expand Down
4 changes: 2 additions & 2 deletions verilog/CST/class_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ TEST(GetClassConstructorTest, GetNewKeyword) {

std::vector<TreeSearchMatch> keywords;
for (const auto& constructor : constructors) {
const auto& keyword =
const auto* keyword =
GetNewKeywordFromClassConstructor(*constructor.match);
keywords.emplace_back(
TreeSearchMatch{&keyword, {/* ignored context */}});
TreeSearchMatch{keyword, {/* ignored context */}});
}
return keywords;
});
Expand Down
10 changes: 6 additions & 4 deletions verilog/CST/declaration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,18 @@ const verible::SyntaxTreeNode* GetParamListFromDataDeclaration(

const verible::TokenInfo& GetModuleInstanceNameTokenInfoFromGateInstance(
const verible::Symbol& gate_instance) {
const verible::SyntaxTreeLeaf& instance_name =
const verible::SyntaxTreeLeaf* instance_name =
GetSubtreeAsLeaf(gate_instance, NodeEnum::kGateInstance, 0);
return instance_name.get();
// TODO(hzeller): bubble up nullptr
return ABSL_DIE_IF_NULL(instance_name)->get();
}

const verible::TokenInfo& GetInstanceNameTokenInfoFromRegisterVariable(
const verible::Symbol& regiseter_variable) {
const verible::SyntaxTreeLeaf& instance_name =
const verible::SyntaxTreeLeaf* instance_name =
GetSubtreeAsLeaf(regiseter_variable, NodeEnum::kRegisterVariable, 0);
return instance_name.get();
// TODO(hzeller): bubble up nullptr.
return ABSL_DIE_IF_NULL(instance_name)->get();
}

const verible::SyntaxTreeNode& GetParenGroupFromModuleInstantiation(
Expand Down
11 changes: 7 additions & 4 deletions verilog/CST/macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ std::vector<verible::TreeSearchMatch> FindAllMacroDefinitionsArgs(
}

const TokenInfo& GetMacroCallId(const Symbol& s) {
return GetSubtreeAsLeaf(s, NodeEnum::kMacroCall, 0).get();
// TODO(hzeller): bubble up nullptr.
return ABSL_DIE_IF_NULL(GetSubtreeAsLeaf(s, NodeEnum::kMacroCall, 0))->get();
}

const TokenInfo& GetMacroGenericItemId(const Symbol& s) {
return GetSubtreeAsLeaf(s, NodeEnum::kMacroGenericItem, 0).get();
// TODO(hzeller): bubble up nullptr.
return ABSL_DIE_IF_NULL(GetSubtreeAsLeaf(s, NodeEnum::kMacroGenericItem, 0))
->get();
}

const SyntaxTreeNode& GetMacroCallParenGroup(const Symbol& s) {
Expand All @@ -77,13 +80,13 @@ bool MacroCallArgsIsEmpty(const SyntaxTreeNode& args) {
return sub.front() == nullptr;
}

const verible::SyntaxTreeLeaf& GetMacroName(
const verible::SyntaxTreeLeaf* GetMacroName(
const verible::Symbol& preprocessor_define) {
return GetSubtreeAsLeaf(preprocessor_define, NodeEnum::kPreprocessorDefine,
1);
}

const verible::SyntaxTreeLeaf& GetMacroArgName(
const verible::SyntaxTreeLeaf* GetMacroArgName(
const verible::Symbol& macro_formal_arg) {
return GetSubtreeAsLeaf(macro_formal_arg, NodeEnum::kMacroFormalArg, 0);
}
Expand Down
8 changes: 4 additions & 4 deletions verilog/CST/macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ const verible::SyntaxTreeNode& GetMacroCallArgs(const verible::Symbol&);
bool MacroCallArgsIsEmpty(const verible::SyntaxTreeNode&);

// Returns the leaf node containing the macro name from node tagged with
// kPreprocessorDefine.
const verible::SyntaxTreeLeaf& GetMacroName(const verible::Symbol&);
// kPreprocessorDefine or nullptr if it doesn't exist.
const verible::SyntaxTreeLeaf* GetMacroName(const verible::Symbol&);

// Returns the leaf node containing the macro arg name from node tagged with
// kMacroFormalArg.
const verible::SyntaxTreeLeaf& GetMacroArgName(const verible::Symbol&);
// kMacroFormalArg or nullptr if it doesn't exist.
const verible::SyntaxTreeLeaf* GetMacroArgName(const verible::Symbol&);

// Returns the leaf node containing the filename from the node tagged with
// kPreprocessorInclude or nullptr if the argument is not a simple
Expand Down
8 changes: 4 additions & 4 deletions verilog/CST/macro_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ TEST(FindAllMacroDefinitions, MacroName) {
const auto decls = FindAllMacroDefinitions(*ABSL_DIE_IF_NULL(root));
std::vector<TreeSearchMatch> names;
for (const auto& decl : decls) {
const auto& type = GetMacroName(*decl.match);
names.push_back(TreeSearchMatch{&type, {/* ignored context */}});
const auto* type = GetMacroName(*decl.match);
names.push_back(TreeSearchMatch{type, {/* ignored context */}});
}
return names;
});
Expand Down Expand Up @@ -271,8 +271,8 @@ TEST(FindAllMacroDefinitions, MacroArgsName) {
for (const auto& decl : decls) {
const auto& args = FindAllMacroDefinitionsArgs(*decl.match);
for (const auto& arg : args) {
const auto& name = GetMacroArgName(*arg.match);
names.push_back(TreeSearchMatch{&name, {/* ignored context */}});
const auto* name = GetMacroArgName(*arg.match);
names.push_back(TreeSearchMatch{name, {/* ignored context */}});
}
}
return names;
Expand Down
12 changes: 6 additions & 6 deletions verilog/CST/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ const SyntaxTreeNode& GetInterfaceHeader(const Symbol& module_symbol) {
NodeEnum::kModuleHeader);
}

const verible::SyntaxTreeLeaf& GetModuleName(const Symbol& s) {
const verible::SyntaxTreeLeaf* GetModuleName(const Symbol& s) {
const auto& header_node = GetModuleHeader(s);
return verible::GetSubtreeAsLeaf(header_node, NodeEnum::kModuleHeader, 2);
}

const TokenInfo& GetInterfaceNameToken(const Symbol& s) {
const auto& header_node = GetInterfaceHeader(s);
const auto& name_leaf =
const verible::SyntaxTreeLeaf* name_leaf =
verible::GetSubtreeAsLeaf(header_node, NodeEnum::kModuleHeader, 2);
return name_leaf.get();
// TODO(hzeller): bubble up nullptr.
return ABSL_DIE_IF_NULL(name_leaf)->get();
}

const SyntaxTreeNode* GetModulePortParenGroup(
Expand Down Expand Up @@ -112,9 +113,8 @@ const verible::SyntaxTreeLeaf* GetModuleEndLabel(
if (label_node == nullptr) {
return nullptr;
}
const auto& module_name = verible::GetSubtreeAsLeaf(
verible::SymbolCastToNode(*label_node), NodeEnum::kLabel, 1);
return &module_name;
return verible::GetSubtreeAsLeaf(verible::SymbolCastToNode(*label_node),
NodeEnum::kLabel, 1);
}

const verible::SyntaxTreeNode& GetModuleItemList(
Expand Down
5 changes: 3 additions & 2 deletions verilog/CST/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ const verible::SyntaxTreeNode& GetModuleHeader(const verible::Symbol&);
// Returns the full header of an interface (params, ports, etc...).
const verible::SyntaxTreeNode& GetInterfaceHeader(const verible::Symbol&);

// Extract the subnode of a module declaration that is the module name.
const verible::SyntaxTreeLeaf& GetModuleName(const verible::Symbol&);
// Extract the subnode of a module declaration that is the module name or
// nullptr if not found.
const verible::SyntaxTreeLeaf* GetModuleName(const verible::Symbol&);

// Extract the subnode of an interface declaration that is the module name.
const verible::TokenInfo& GetInterfaceNameToken(const verible::Symbol&);
Expand Down
12 changes: 6 additions & 6 deletions verilog/CST/module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ TEST(GetModuleNameTokenTest, ValidModule) {

std::vector<TreeSearchMatch> names;
for (const auto& instance : programs) {
const auto& name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{&name, {/* ignored context */}});
const auto* name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{name, {/* ignored context */}});
}
return names;
});
Expand All @@ -148,8 +148,8 @@ TEST(GetModuleNameTokenTest, ValidInterface) {

std::vector<TreeSearchMatch> names;
for (const auto& instance : programs) {
const auto& name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{&name, {/* ignored context */}});
const auto* name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{name, {/* ignored context */}});
}
return names;
});
Expand All @@ -172,8 +172,8 @@ TEST(GetModuleNameTokenTest, ValidProgram) {

std::vector<TreeSearchMatch> names;
for (const auto& instance : programs) {
const auto& name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{&name, {/* ignored context */}});
const auto* name = GetModuleName(*instance.match);
names.emplace_back(TreeSearchMatch{name, {/* ignored context */}});
}
return names;
});
Expand Down
4 changes: 2 additions & 2 deletions verilog/CST/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ static bool ExpectedTagPredicate(const Symbol& symbol) {
return symbol.Tag() == var_symbol || symbol.Tag() == assign_symbol;
}

const verible::SyntaxTreeLeaf& GetNameLeafOfNetVariable(
const verible::SyntaxTreeLeaf* GetNameLeafOfNetVariable(
const verible::Symbol& net_variable) {
return verible::GetSubtreeAsLeaf(net_variable, NodeEnum::kNetVariable, 0);
}

const verible::SyntaxTreeLeaf& GetNameLeafOfRegisterVariable(
const verible::SyntaxTreeLeaf* GetNameLeafOfRegisterVariable(
const verible::Symbol& register_variable) {
return verible::GetSubtreeAsLeaf(register_variable,
NodeEnum::kRegisterVariable, 0);
Expand Down
6 changes: 3 additions & 3 deletions verilog/CST/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ std::vector<verible::TreeSearchMatch> FindAllNetDeclarations(
std::vector<const verible::TokenInfo*> GetIdentifiersFromNetDeclaration(
const verible::Symbol& symbol);

// Returns the declared identifier from a kNetVariable.
const verible::SyntaxTreeLeaf& GetNameLeafOfNetVariable(
// Returns the declared identifier from a kNetVariable or nullptr if invalid.
const verible::SyntaxTreeLeaf* GetNameLeafOfNetVariable(
const verible::Symbol& net_variable);

// Returns the declared identifier from a kRegisterVariable.
const verible::SyntaxTreeLeaf& GetNameLeafOfRegisterVariable(
const verible::SyntaxTreeLeaf* GetNameLeafOfRegisterVariable(
const verible::Symbol& register_variable);

} // namespace verilog
Expand Down
8 changes: 4 additions & 4 deletions verilog/CST/net_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ TEST(GetNameLeafOfNetVariableTest, Various) {

std::vector<TreeSearchMatch> net_ids;
for (const auto& decl : net_decls) {
const auto& net_name = GetNameLeafOfNetVariable(*decl.match);
const auto* net_name = GetNameLeafOfNetVariable(*decl.match);
net_ids.emplace_back(
TreeSearchMatch{&net_name, {/* ignored context */}});
TreeSearchMatch{net_name, {/* ignored context */}});
}
return net_ids;
});
Expand Down Expand Up @@ -376,9 +376,9 @@ TEST(GetNameLeafOfRegisterVariableTest, Various) {

std::vector<TreeSearchMatch> net_ids;
for (const auto& decl : net_decls) {
const auto& net_name = GetNameLeafOfRegisterVariable(*decl.match);
const auto* net_name = GetNameLeafOfRegisterVariable(*decl.match);
net_ids.emplace_back(
TreeSearchMatch{&net_name, {/* ignored context */}});
TreeSearchMatch{net_name, {/* ignored context */}});
}
return net_ids;
});
Expand Down
20 changes: 10 additions & 10 deletions verilog/CST/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ std::vector<verible::TreeSearchMatch> FindAllPackageImportItems(
}

const verible::TokenInfo& GetPackageNameToken(const verible::Symbol& s) {
const auto& name_node = GetPackageNameLeaf(s);
return name_node.get();
// TODO(hzeller): bubble up nullptr.
return ABSL_DIE_IF_NULL(GetPackageNameLeaf(s))->get();
}

const verible::SyntaxTreeLeaf& GetPackageNameLeaf(const verible::Symbol& s) {
const verible::SyntaxTreeLeaf* GetPackageNameLeaf(const verible::Symbol& s) {
return verible::GetSubtreeAsLeaf(s, NodeEnum::kPackageDeclaration, 2);
}

Expand All @@ -54,9 +54,8 @@ const verible::SyntaxTreeLeaf* GetPackageNameEndLabel(
if (label_node == nullptr) {
return nullptr;
}
const auto& package_name = verible::GetSubtreeAsLeaf(
verible::SymbolCastToNode(*label_node), NodeEnum::kLabel, 1);
return &package_name;
return verible::GetSubtreeAsLeaf(verible::SymbolCastToNode(*label_node),
NodeEnum::kLabel, 1);
}

const verible::Symbol* GetPackageItemList(
Expand All @@ -72,7 +71,7 @@ const verible::SyntaxTreeNode& GetScopePrefixFromPackageImportItem(
NodeEnum::kScopePrefix);
}

const verible::SyntaxTreeLeaf& GetImportedPackageName(
const verible::SyntaxTreeLeaf* GetImportedPackageName(
const verible::Symbol& package_import_item) {
return verible::GetSubtreeAsLeaf(
GetScopePrefixFromPackageImportItem(package_import_item),
Expand All @@ -81,14 +80,15 @@ const verible::SyntaxTreeLeaf& GetImportedPackageName(

const verible::SyntaxTreeLeaf* GeImportedItemNameFromPackageImportItem(
const verible::Symbol& package_import_item) {
const auto& imported_item = verible::GetSubtreeAsLeaf(
const verible::SyntaxTreeLeaf* imported_item = verible::GetSubtreeAsLeaf(
package_import_item, NodeEnum::kPackageImportItem, 1);

if (imported_item.get().token_enum() != verilog_tokentype::SymbolIdentifier) {
if (!imported_item || imported_item->get().token_enum() !=
verilog_tokentype::SymbolIdentifier) {
return nullptr;
}

return &imported_item;
return imported_item;
}

} // namespace verilog
4 changes: 2 additions & 2 deletions verilog/CST/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ std::vector<verible::TreeSearchMatch> FindAllPackageImportItems(
const verible::TokenInfo& GetPackageNameToken(const verible::Symbol&);

// Return the node spanning the name of the package.
const verible::SyntaxTreeLeaf& GetPackageNameLeaf(const verible::Symbol& s);
const verible::SyntaxTreeLeaf* GetPackageNameLeaf(const verible::Symbol& s);

// Extracts the node that spans the name of the package after endpackage if
// exists.
Expand All @@ -57,7 +57,7 @@ const verible::SyntaxTreeNode& GetScopePrefixFromPackageImportItem(
// Extracts package name for package import (node tagged with
// kPackageImportItem).
// e.g import pkg::my_integer return the node spanning "pkg".
const verible::SyntaxTreeLeaf& GetImportedPackageName(
const verible::SyntaxTreeLeaf* GetImportedPackageName(
const verible::Symbol& package_import_item);

// Extracts the symbol identifier from PackageImportItem if exits.
Expand Down
Loading

0 comments on commit 034c05e

Please sign in to comment.