Commit 448a1d56 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Add linter rule for type naming convention

Drive-by change: Add helper method for reporting naming
convention errors and refactor existing call sites.

R=tebbi@chromium.org

Bug: v8:7793
Change-Id: I7315b935229ffb7557fa55ebcb222fc91124aab7
Reviewed-on: https://chromium-review.googlesource.com/1223831Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Simon Zünd <szuend@google.com>
Cr-Commit-Position: refs/heads/master@{#55847}
parent 4a751684
...@@ -186,10 +186,7 @@ T* MakeNode(Args... args) { ...@@ -186,10 +186,7 @@ T* MakeNode(Args... args) {
void LintGenericParameters(const GenericParameters& parameters) { void LintGenericParameters(const GenericParameters& parameters) {
for (const std::string& parameter : parameters) { for (const std::string& parameter : parameters) {
if (!IsUpperCamelCase(parameter)) { if (!IsUpperCamelCase(parameter)) {
std::stringstream sstream; NamingConventionError("Generic parameter", parameter, "UpperCamelCase");
sstream << "Generic parameter \"" << parameter << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
} }
} }
} }
...@@ -245,10 +242,7 @@ base::Optional<ParseResult> MakeParameterListFromNameAndTypeList( ...@@ -245,10 +242,7 @@ base::Optional<ParseResult> MakeParameterListFromNameAndTypeList(
ParameterList result; ParameterList result;
for (NameAndTypeExpression& pair : params) { for (NameAndTypeExpression& pair : params) {
if (!IsLowerCamelCase(pair.name)) { if (!IsLowerCamelCase(pair.name)) {
std::stringstream sstream; NamingConventionError("Parameter", pair.name, "lowerCamelCase");
sstream << "Parameter \"" << pair.name << "\" doesn't follow "
<< "\"lowerCamelCase\" naming convention.";
LintError(sstream.str());
} }
result.names.push_back(std::move(pair.name)); result.names.push_back(std::move(pair.name));
...@@ -308,10 +302,7 @@ base::Optional<ParseResult> MakeTorqueMacroDeclaration( ...@@ -308,10 +302,7 @@ base::Optional<ParseResult> MakeTorqueMacroDeclaration(
auto operator_name = child_results->NextAs<base::Optional<std::string>>(); auto operator_name = child_results->NextAs<base::Optional<std::string>>();
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsUpperCamelCase(name)) { if (!IsUpperCamelCase(name)) {
std::stringstream sstream; NamingConventionError("Macro", name, "UpperCamelCase");
sstream << "Macro \"" << name << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
} }
auto generic_parameters = child_results->NextAs<GenericParameters>(); auto generic_parameters = child_results->NextAs<GenericParameters>();
...@@ -338,10 +329,7 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration( ...@@ -338,10 +329,7 @@ base::Optional<ParseResult> MakeTorqueBuiltinDeclaration(
auto javascript_linkage = child_results->NextAs<bool>(); auto javascript_linkage = child_results->NextAs<bool>();
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsUpperCamelCase(name)) { if (!IsUpperCamelCase(name)) {
std::stringstream sstream; NamingConventionError("Builtin", name, "UpperCamelCase");
sstream << "Builtin \"" << name << "\" doesn't follow "
<< "\"UpperCamelCase\" naming convention.";
LintError(sstream.str());
} }
auto generic_parameters = child_results->NextAs<GenericParameters>(); auto generic_parameters = child_results->NextAs<GenericParameters>();
...@@ -366,10 +354,7 @@ base::Optional<ParseResult> MakeConstDeclaration( ...@@ -366,10 +354,7 @@ base::Optional<ParseResult> MakeConstDeclaration(
ParseResultIterator* child_results) { ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsValidModuleConstName(name)) { if (!IsValidModuleConstName(name)) {
std::stringstream sstream; NamingConventionError("Constant", name, "kUpperCamelCase");
sstream << "Constant \"" << name << "\" doesn't follow "
<< "\"kUpperCamelCase\" naming convention.";
LintError(sstream.str());
} }
auto type = child_results->NextAs<TypeExpression*>(); auto type = child_results->NextAs<TypeExpression*>();
...@@ -400,6 +385,9 @@ base::Optional<ParseResult> MakeTypeAliasDeclaration( ...@@ -400,6 +385,9 @@ base::Optional<ParseResult> MakeTypeAliasDeclaration(
base::Optional<ParseResult> MakeTypeDeclaration( base::Optional<ParseResult> MakeTypeDeclaration(
ParseResultIterator* child_results) { ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsValidTypeName(name)) {
NamingConventionError("Type", name, "UpperCamelCase");
}
auto extends = child_results->NextAs<base::Optional<std::string>>(); auto extends = child_results->NextAs<base::Optional<std::string>>();
auto generates = child_results->NextAs<base::Optional<std::string>>(); auto generates = child_results->NextAs<base::Optional<std::string>>();
auto constexpr_generates = auto constexpr_generates =
...@@ -414,10 +402,7 @@ base::Optional<ParseResult> MakeExplicitModuleDeclaration( ...@@ -414,10 +402,7 @@ base::Optional<ParseResult> MakeExplicitModuleDeclaration(
ParseResultIterator* child_results) { ParseResultIterator* child_results) {
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsSnakeCase(name)) { if (!IsSnakeCase(name)) {
std::stringstream sstream; NamingConventionError("Module", name, "snake_case");
sstream << "Module \"" << name << "\" doesn't follow "
<< "\"snake_case\" naming convention.";
LintError(sstream.str());
} }
auto declarations = child_results->NextAs<std::vector<Declaration*>>(); auto declarations = child_results->NextAs<std::vector<Declaration*>>();
Declaration* result = MakeNode<ExplicitModuleDeclaration>( Declaration* result = MakeNode<ExplicitModuleDeclaration>(
...@@ -653,10 +638,7 @@ base::Optional<ParseResult> MakeVarDeclarationStatement( ...@@ -653,10 +638,7 @@ base::Optional<ParseResult> MakeVarDeclarationStatement(
if (!const_qualified) DCHECK_EQ("let", kind); if (!const_qualified) DCHECK_EQ("let", kind);
auto name = child_results->NextAs<std::string>(); auto name = child_results->NextAs<std::string>();
if (!IsLowerCamelCase(name)) { if (!IsLowerCamelCase(name)) {
std::stringstream sstream; NamingConventionError("Variable", name, "lowerCamelCase");
sstream << "Variable \"" << name << "\" doesn't follow "
<< "\"lowerCamelCase\" naming convention.";
LintError(sstream.str());
} }
auto type = child_results->NextAs<TypeExpression*>(); auto type = child_results->NextAs<TypeExpression*>();
......
...@@ -88,6 +88,14 @@ void LintError(const std::string& error) { ...@@ -88,6 +88,14 @@ void LintError(const std::string& error) {
std::cerr << CurrentPositionAsString() << ": Lint error: " << error << "\n"; std::cerr << CurrentPositionAsString() << ": Lint error: " << error << "\n";
} }
void NamingConventionError(const std::string& type, const std::string& name,
const std::string& convention) {
std::stringstream sstream;
sstream << type << " \"" << name << "\" doesn't follow \"" << convention
<< "\" naming convention.";
LintError(sstream.str());
}
namespace { namespace {
bool ContainsUnderscore(const std::string& s) { bool ContainsUnderscore(const std::string& s) {
...@@ -111,6 +119,17 @@ bool IsKeywordLikeName(const std::string& s) { ...@@ -111,6 +119,17 @@ bool IsKeywordLikeName(const std::string& s) {
s) != keyword_like_constants.end(); s) != keyword_like_constants.end();
} }
// Untagged/MachineTypes like 'int32', 'intptr' etc. follow a 'all-lowercase'
// naming convention and are those exempt from the normal type convention.
bool IsMachineType(const std::string& s) {
static const std::vector<std::string> machine_types{
"void", "never", "int32", "uint32", "int64", "intptr",
"uintptr", "float32", "float64", "bool", "string", "int31"};
return std::find(machine_types.begin(), machine_types.end(), s) !=
machine_types.end();
}
} // namespace } // namespace
bool IsLowerCamelCase(const std::string& s) { bool IsLowerCamelCase(const std::string& s) {
...@@ -135,6 +154,13 @@ bool IsValidModuleConstName(const std::string& s) { ...@@ -135,6 +154,13 @@ bool IsValidModuleConstName(const std::string& s) {
return s[0] == 'k' && IsUpperCamelCase(s.substr(1)); return s[0] == 'k' && IsUpperCamelCase(s.substr(1));
} }
bool IsValidTypeName(const std::string& s) {
if (s.empty()) return false;
if (IsMachineType(s)) return true;
return IsUpperCamelCase(s);
}
std::string CamelifyString(const std::string& underscore_string) { std::string CamelifyString(const std::string& underscore_string) {
std::string result; std::string result;
bool word_beginning = true; bool word_beginning = true;
......
...@@ -35,10 +35,16 @@ class LintErrorStatus : public ContextualClass<LintErrorStatus> { ...@@ -35,10 +35,16 @@ class LintErrorStatus : public ContextualClass<LintErrorStatus> {
[[noreturn]] void ReportError(const std::string& error); [[noreturn]] void ReportError(const std::string& error);
void LintError(const std::string& error); void LintError(const std::string& error);
// Prints a LintError with the format "{type} '{name}' doesn't follow
// '{convention}' naming convention".
void NamingConventionError(const std::string& type, const std::string& name,
const std::string& convention);
bool IsLowerCamelCase(const std::string& s); bool IsLowerCamelCase(const std::string& s);
bool IsUpperCamelCase(const std::string& s); bool IsUpperCamelCase(const std::string& s);
bool IsSnakeCase(const std::string& s); bool IsSnakeCase(const std::string& s);
bool IsValidModuleConstName(const std::string& s); bool IsValidModuleConstName(const std::string& s);
bool IsValidTypeName(const std::string& s);
std::string CamelifyString(const std::string& underscore_string); std::string CamelifyString(const std::string& underscore_string);
std::string DashifyString(const std::string& underscore_string); std::string DashifyString(const std::string& underscore_string);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment