-
Notifications
You must be signed in to change notification settings - Fork 217
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
Mar 17, 2021
1 parent
08bdd3b
commit 43169ff
Showing
13 changed files
with
472 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
98 changes: 98 additions & 0 deletions
98
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,98 @@ | ||
// 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."); | ||
} | ||
|
||
void ForbidImplicitDeclarationsRule::Lint(const verible::TextStructureView& text_structure, | ||
absl::string_view filename) { | ||
SymbolTable symbol_table(nullptr); | ||
|
||
ParsedVerilogSourceFile* src = | ||
new ParsedVerilogSourceFile("internal", text_structure); | ||
// Already parsed, calling to ensure that VerilogSourceFile internals are in | ||
// correct state | ||
src->Parse(); | ||
|
||
auto diagnostics = BuildSymbolTable(*src, &symbol_table); | ||
symbol_table.Resolve(&diagnostics); // FIXME(ldk): Resolve only some symbols | ||
|
||
auto& violations = this->violations_; | ||
const SymbolTableNode& symbol_root(symbol_table.Root()); | ||
symbol_root.ApplyPreOrder( | ||
[&violations, &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, &text_structure](const ReferenceComponent& node) { | ||
if ((node.resolved_symbol == nullptr) || // Unresolved | ||
(node.identifier.begin() < node.resolved_symbol->Key()->begin())) { // Implicit | ||
const auto& contents = text_structure.Contents(); | ||
const auto offset = std::distance(contents.begin(), | ||
node.identifier.begin()); | ||
auto range = text_structure.TokenRangeSpanningOffsets(offset, offset); | ||
auto token = range.begin(); | ||
const auto& token_info = *token; | ||
// FIXME(ldk): Insert only kLPValue? | ||
violations.insert(LintViolation(token_info, kMessage)); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
LintRuleStatus ForbidImplicitDeclarationsRule::Report() const { | ||
return LintRuleStatus(violations_, Name(), GetStyleGuideCitation(kTopic)); | ||
} | ||
|
||
} // namespace analysis | ||
} // namespace verilog |
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
// 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: | ||
|
||
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.