-
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.
forbid line continuations inside string-literals (#674)
* forbid line continuations inside string-literals Adding: - TokenStreamLintRule definition for linter - tests for TokenStreamLintRule Fixes #384 Signed-off-by: Pawel Sagan <[email protected]>
- Loading branch information
Showing
8 changed files
with
258 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
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
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,83 @@ | ||
// 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/token_stream_lint_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/analysis/matcher/matcher.h" | ||
#include "common/text/symbol.h" | ||
#include "common/text/syntax_tree_context.h" | ||
#include "verilog/CST/verilog_matchers.h" // IWYU pragma: keep | ||
#include "verilog/analysis/descriptions.h" | ||
#include "verilog/analysis/lint_rule_registry.h" | ||
|
||
namespace verilog { | ||
namespace analysis { | ||
|
||
using verible::GetStyleGuideCitation; | ||
using verible::LintRuleStatus; | ||
using verible::LintViolation; | ||
using verible::SyntaxTreeContext; | ||
using verible::matcher::Matcher; | ||
|
||
// Register TokenStreamLintRule | ||
VERILOG_REGISTER_LINT_RULE(TokenStreamLintRule); | ||
|
||
absl::string_view TokenStreamLintRule::Name() { | ||
return "forbid-line-continuations"; | ||
} | ||
const char TokenStreamLintRule::kTopic[] = "forbid-line-continuations"; | ||
const char TokenStreamLintRule::kMessage[] = | ||
"The lines can't be continued with \'\\\', use concatenation operator with " | ||
"braces"; | ||
|
||
std::string TokenStreamLintRule::GetDescription( | ||
DescriptionType description_type) { | ||
return absl::StrCat("Checks that there are no occurrences of ", | ||
Codify("\'\\\'", description_type), | ||
" when breaking the string literal line.", | ||
"Use concatenation operator with braces instead. See ", | ||
GetStyleGuideCitation(kTopic), "."); | ||
} | ||
|
||
static const Matcher& StringLiteralMatcher() { | ||
static const Matcher matcher(StringLiteralKeyword()); | ||
return matcher; | ||
} | ||
|
||
void TokenStreamLintRule::HandleSymbol(const verible::Symbol& symbol, | ||
const SyntaxTreeContext& context) { | ||
verible::matcher::BoundSymbolManager manager; | ||
if (StringLiteralMatcher().Matches(symbol, &manager)) { | ||
const auto& string_node = SymbolCastToNode(symbol); | ||
const auto& string_literal = SymbolCastToLeaf(*string_node.children()[0]); | ||
if (string_literal.get().text().find("\\\n") != std::string::npos) { | ||
violations_.insert(LintViolation(symbol, kMessage, context)); | ||
} | ||
} | ||
} | ||
|
||
LintRuleStatus TokenStreamLintRule::Report() const { | ||
return LintRuleStatus(violations_, Name(), GetStyleGuideCitation(kTopic)); | ||
} | ||
|
||
} // namespace analysis | ||
} // namespace verilog |
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,58 @@ | ||
// 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_TOKEN_STREAM_LINT_RULE_H_ | ||
#define VERIBLE_VERILOG_ANALYSIS_CHECKERS_TOKEN_STREAM_LINT_RULE_H_ | ||
|
||
#include <set> | ||
#include <string> | ||
|
||
#include "common/analysis/lint_rule_status.h" | ||
#include "common/analysis/syntax_tree_lint_rule.h" | ||
#include "common/text/symbol.h" | ||
#include "common/text/syntax_tree_context.h" | ||
#include "verilog/analysis/descriptions.h" | ||
|
||
namespace verilog { | ||
namespace analysis { | ||
|
||
// TokenStreamLintRule finds occurrences of any string literal. | ||
class TokenStreamLintRule : public verible::SyntaxTreeLintRule { | ||
public: | ||
using rule_type = verible::SyntaxTreeLintRule; | ||
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); | ||
|
||
void HandleSymbol(const verible::Symbol& symbol, | ||
const verible::SyntaxTreeContext& context) override; | ||
|
||
verible::LintRuleStatus Report() const override; | ||
|
||
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_TOKEN_STREAM_LINT_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,65 @@ | ||
// 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/token_stream_lint_rule.h" | ||
|
||
#include <initializer_list> | ||
|
||
#include "common/analysis/linter_test_utils.h" | ||
#include "common/analysis/syntax_tree_linter_test_utils.h" | ||
#include "common/text/symbol.h" | ||
#include "gtest/gtest.h" | ||
#include "verilog/CST/verilog_nonterminals.h" | ||
#include "verilog/CST/verilog_treebuilder_utils.h" | ||
#include "verilog/analysis/verilog_analyzer.h" | ||
#include "verilog/parser/verilog_token_enum.h" | ||
|
||
namespace verilog { | ||
namespace analysis { | ||
namespace { | ||
|
||
using verible::LintTestCase; | ||
using verible::RunLintTestCases; | ||
|
||
TEST(StringLiteralConcatenationTest, FunctionFailures) { | ||
constexpr int kToken = TK_StringLiteral; | ||
const std::initializer_list<LintTestCase> kStringLiteralTestCases = { | ||
{""}, | ||
{"module m;\nendmodule\n"}, | ||
{"module m;\n", "string tmp = {\"Humpty Dumpty sat on a wall.\",", | ||
"\"Humpty Dumpty had a great fall.\"};", "\nendmodule"}, | ||
{"module m;\n", "string tmp = {\"Humpty Dumpty \\ sat on a wall.\",", | ||
"\"Humpty Dumpty had a great fall.\"};", "\nendmodule"}, | ||
{"module m;\n", | ||
"string tmp=", | ||
{kToken, | ||
"\"Humpty Dumpty sat on a wall. \\\nHumpty Dumpty had a great fall.\""}, | ||
";", | ||
"\nendmodule"}, | ||
{"module m;\n", | ||
"string tmp=", | ||
{kToken, | ||
"\"Humpty Dumpty sat on a wall. \\\n\\\nHumpty Dumpty had a great " | ||
"fall.\""}, | ||
";", | ||
"\nendmodule"}, | ||
}; | ||
|
||
RunLintTestCases<VerilogAnalyzer, TokenStreamLintRule>( | ||
kStringLiteralTestCases); | ||
} | ||
|
||
} // namespace | ||
} // namespace analysis | ||
} // namespace verilog |
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
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
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,9 @@ | ||
module forbid_line_continuations; | ||
|
||
string bad_literal = "Humpty Dumpty sat on a wall. \ | ||
Humpty Dumpty had a great fall."; | ||
|
||
string good_literal = {"Humpty Dumpty sat on a wall.", | ||
"Humpty Dumpty had a great fall."}; | ||
|
||
endmodule |