-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linter: Add forbid-implicit-declarations rule
Signed-off-by: Lukasz Dalek <[email protected]>
- Loading branch information
Lukasz Dalek
committed
Feb 16, 2021
1 parent
231ce1b
commit 33421a6
Showing
13 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
verilog/analysis/checkers/forbid_implicit_declarations_rule.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2017-2020 The Verible Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "verilog/analysis/checkers/forbid_implicit_declarations_rule.h" | ||
|
||
#include <set> | ||
#include <string> | ||
|
||
#include "absl/strings/str_cat.h" | ||
#include "absl/strings/string_view.h" | ||
#include "common/analysis/citation.h" | ||
#include "common/analysis/lint_rule_status.h" | ||
#include "common/analysis/matcher/bound_symbol_manager.h" | ||
#include "common/text/symbol.h" | ||
#include "common/text/syntax_tree_context.h" | ||
#include "common/text/tree_context_visitor.h" | ||
#include "verilog/analysis/descriptions.h" | ||
#include "verilog/analysis/lint_rule_registry.h" | ||
#include "verilog/CST/identifier.h" | ||
#include "verilog/analysis/symbol_table.h" | ||
|
||
namespace verilog { | ||
namespace analysis { | ||
|
||
using verible::GetStyleGuideCitation; | ||
using verible::LintRuleStatus; | ||
using verible::LintViolation; | ||
using verible::SyntaxTreeContext; | ||
|
||
// Register ForbidImplicitDeclarationsRule | ||
VERILOG_REGISTER_LINT_RULE(ForbidImplicitDeclarationsRule); | ||
|
||
// forbid-implicit-net-declarations? | ||
absl::string_view ForbidImplicitDeclarationsRule::Name() { | ||
return "forbid-implicit-declarations"; | ||
} | ||
const char ForbidImplicitDeclarationsRule::kTopic[] = "implicit-declarations"; | ||
const char ForbidImplicitDeclarationsRule::kMessage[] = | ||
"Nets must be declared explicitly."; | ||
|
||
std::string ForbidImplicitDeclarationsRule::GetDescription(DescriptionType description_type) { | ||
return absl::StrCat("Checks that there are no occurrences of " | ||
"implicitly declared nets."); | ||
} | ||
|
||
const verible::TokenInfo* ForbidImplicitDeclarationsRule::FindTwinToken(absl::string_view contents, | ||
absl::string_view identifier, | ||
const verible::TextStructureView& text_structure) { | ||
const auto offset = std::distance(contents.begin(), identifier.begin()); | ||
|
||
// find token with that identifier | ||
class UglyVisitor : public verible::TreeContextPathVisitor { | ||
public: | ||
UglyVisitor(decltype(offset) o, absl::string_view c) : | ||
offset_(o), contents_(c), token_(nullptr) {} | ||
~UglyVisitor() {} | ||
|
||
void Visit(const verible::SyntaxTreeLeaf& leaf) override { | ||
if (token_ != nullptr) { | ||
return; | ||
} | ||
|
||
auto offset = std::distance(contents_.begin(), leaf.get().text().begin()); | ||
if (offset_ == offset && Context().IsInside(NodeEnum::kNetVariableAssignment) && | ||
Context().IsInside(NodeEnum::kLPValue)) { | ||
token_ = &leaf.get(); | ||
} | ||
}; | ||
|
||
decltype(offset) offset_; | ||
absl::string_view contents_; | ||
const verible::TokenInfo* token_; | ||
}; | ||
|
||
UglyVisitor ugly_visitor(offset, text_structure.Contents()); | ||
text_structure.SyntaxTree()->Accept(&ugly_visitor); | ||
return ugly_visitor.token_; | ||
} | ||
|
||
void ForbidImplicitDeclarationsRule::Lint(const verible::TextStructureView& text_structure, | ||
absl::string_view filename) { | ||
SymbolTable symbol_table(nullptr); | ||
|
||
// FIXME(ldk): Modify SymbolTable to skip this eyesore | ||
InMemoryVerilogSourceFile* src = new InMemoryVerilogSourceFile("internal", text_structure.Contents()); | ||
src->Parse(); | ||
auto diagnostics = BuildSymbolTable(*src, &symbol_table); | ||
symbol_table.Resolve(&diagnostics); | ||
|
||
const SymbolTableNode& symbol_root(symbol_table.Root()); | ||
auto& violations = this->violations_; | ||
symbol_root.ApplyPreOrder( | ||
[&violations,&src,&text_structure](const decltype(symbol_root)& node) { | ||
for (const auto& itr : node.Value().local_references_to_bind) { | ||
ABSL_DIE_IF_NULL(itr.LastLeaf())->ApplyPreOrder( | ||
[&violations,&src,&text_structure](const ReferenceComponent& node) { | ||
if ((node.resolved_symbol == nullptr) || // Unresolved | ||
(node.identifier.begin() < node.resolved_symbol->Key()->begin())) { // Implicit | ||
const auto* token = FindTwinToken(src->GetTextStructure()->Contents(), | ||
node.identifier, | ||
text_structure); | ||
if (token != nullptr) { | ||
violations.insert(LintViolation(*ABSL_DIE_IF_NULL(token), kMessage)); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
LintRuleStatus ForbidImplicitDeclarationsRule::Report() const { | ||
return LintRuleStatus(violations_, Name(), GetStyleGuideCitation(kTopic)); | ||
} | ||
|
||
} // namespace analysis | ||
} // namespace verilog |
73 changes: 73 additions & 0 deletions
73
verilog/analysis/checkers/forbid_implicit_declarations_rule.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2017-2020 The Verible Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef VERIBLE_VERILOG_ANALYSIS_CHECKERS_FORBID_IMPLICIT_DECLARATIONS_RULE_H_ | ||
#define VERIBLE_VERILOG_ANALYSIS_CHECKERS_FORBID_IMPLICIT_DECLARATIONS_RULE_H_ | ||
|
||
#include <set> | ||
#include <string> | ||
|
||
#include "common/analysis/lint_rule_status.h" | ||
#include "common/analysis/matcher/core_matchers.h" | ||
#include "common/analysis/matcher/matcher.h" | ||
#include "common/analysis/matcher/matcher_builders.h" | ||
#include "common/analysis/text_structure_lint_rule.h" | ||
#include "common/text/symbol.h" | ||
#include "common/text/syntax_tree_context.h" | ||
#include "common/text/tree_context_visitor.h" | ||
#include "common/util/auto_pop_stack.h" | ||
#include "verilog/CST/verilog_matchers.h" // IWYU pragma: keep | ||
#include "verilog/analysis/descriptions.h" | ||
#include "verilog/analysis/symbol_table.h" | ||
#include "verilog/analysis/verilog_project.h" | ||
|
||
namespace verilog { | ||
namespace analysis { | ||
|
||
// ForbidImplicitDeclarationsRule detect implicitly declared nets | ||
class ForbidImplicitDeclarationsRule : public verible::TextStructureLintRule { | ||
//public ScopeTreeVisitor { | ||
public: | ||
using rule_type = verible::TextStructureLintRule; | ||
static absl::string_view Name(); | ||
|
||
// Returns the description of the rule implemented formatted for either the | ||
// helper flag or markdown depending on the parameter type. | ||
static std::string GetDescription(DescriptionType); | ||
|
||
// Analyze text structure for violations. | ||
void Lint(const verible::TextStructureView& text_structure, | ||
absl::string_view filename) override; | ||
|
||
verible::LintRuleStatus Report() const override; | ||
|
||
private: | ||
static const verible::TokenInfo* FindTwinToken( | ||
absl::string_view contents, absl::string_view identifier, | ||
const verible::TextStructureView& text_structure); | ||
|
||
private: | ||
// Link to style guide rule. | ||
static const char kTopic[]; | ||
|
||
// Diagnostic message. | ||
static const char kMessage[]; | ||
|
||
std::set<verible::LintViolation> violations_; | ||
}; | ||
|
||
} // namespace analysis | ||
} // namespace verilog | ||
|
||
#endif // VERIBLE_VERILOG_ANALYSIS_CHECKERS_FORBID_IMPLICIT_DECLARATIONS_RULE_H_ |
Oops, something went wrong.