blob: 26124b4008e09bb93d0f983bbd45440b506d848b [file]
// 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 "verible/common/analysis/syntax-tree-linter.h"
#include <memory>
#include <vector>
#include "absl/log/die_if_null.h"
#include "verible/common/analysis/lint-rule-status.h"
#include "verible/common/analysis/syntax-tree-lint-rule.h"
#include "verible/common/text/concrete-syntax-leaf.h"
#include "verible/common/text/concrete-syntax-tree.h"
#include "verible/common/text/symbol.h"
#include "verible/common/text/tree-context-visitor.h"
#include "verible/common/util/logging.h"
namespace verible {
void SyntaxTreeLinter::Lint(const Symbol &root) {
VLOG(1) << "SyntaxTreeLinter analyzing syntax tree with " << rules_.size()
<< " rules.";
root.Accept(this);
}
std::vector<LintRuleStatus> SyntaxTreeLinter::ReportStatus() const {
std::vector<LintRuleStatus> status;
status.reserve(rules_.size());
for (const auto &rule : rules_) {
status.push_back(ABSL_DIE_IF_NULL(rule)->Report());
}
return status;
}
// Visits a leaf. Every held rule handles that leaf.
void SyntaxTreeLinter::Visit(const SyntaxTreeLeaf &leaf) {
for (const auto &rule : rules_) {
// Have rule handle the leaf as both a leaf and a symbol.
ABSL_DIE_IF_NULL(rule)->HandleLeaf(leaf, Context());
rule->HandleSymbol(leaf, Context());
}
}
// Visits a node. First, linter has every rule handle that node
// Second, linter recurses on every non-null child of that node in order
// to visit the entire tree
void SyntaxTreeLinter::Visit(const SyntaxTreeNode &node) {
for (const auto &rule : rules_) {
// Have rule handle the node as both a node and a symbol.
ABSL_DIE_IF_NULL(rule)->HandleNode(node, Context());
rule->HandleSymbol(node, Context());
}
// Visit subtree children.
TreeContextVisitor::Visit(node);
}
} // namespace verible