From 448a1d565f45cf6c7c2c01f5d12d76d5dcf046ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Z=C3=BCnd?= Date: Thu, 13 Sep 2018 09:17:10 +0200 Subject: [PATCH] [torque] Add linter rule for type naming convention MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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/1223831 Reviewed-by: Tobias Tebbi Commit-Queue: Simon Zünd Cr-Commit-Position: refs/heads/master@{#55847} --- src/torque/torque-parser.cc | 38 ++++++++++--------------------------- src/torque/utils.cc | 26 +++++++++++++++++++++++++ src/torque/utils.h | 6 ++++++ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/torque/torque-parser.cc b/src/torque/torque-parser.cc index f6367aab7a..3593436e9b 100644 --- a/src/torque/torque-parser.cc +++ b/src/torque/torque-parser.cc @@ -186,10 +186,7 @@ T* MakeNode(Args... args) { void LintGenericParameters(const GenericParameters& parameters) { for (const std::string& parameter : parameters) { if (!IsUpperCamelCase(parameter)) { - std::stringstream sstream; - sstream << "Generic parameter \"" << parameter << "\" doesn't follow " - << "\"UpperCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Generic parameter", parameter, "UpperCamelCase"); } } } @@ -245,10 +242,7 @@ base::Optional MakeParameterListFromNameAndTypeList( ParameterList result; for (NameAndTypeExpression& pair : params) { if (!IsLowerCamelCase(pair.name)) { - std::stringstream sstream; - sstream << "Parameter \"" << pair.name << "\" doesn't follow " - << "\"lowerCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Parameter", pair.name, "lowerCamelCase"); } result.names.push_back(std::move(pair.name)); @@ -308,10 +302,7 @@ base::Optional MakeTorqueMacroDeclaration( auto operator_name = child_results->NextAs>(); auto name = child_results->NextAs(); if (!IsUpperCamelCase(name)) { - std::stringstream sstream; - sstream << "Macro \"" << name << "\" doesn't follow " - << "\"UpperCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Macro", name, "UpperCamelCase"); } auto generic_parameters = child_results->NextAs(); @@ -338,10 +329,7 @@ base::Optional MakeTorqueBuiltinDeclaration( auto javascript_linkage = child_results->NextAs(); auto name = child_results->NextAs(); if (!IsUpperCamelCase(name)) { - std::stringstream sstream; - sstream << "Builtin \"" << name << "\" doesn't follow " - << "\"UpperCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Builtin", name, "UpperCamelCase"); } auto generic_parameters = child_results->NextAs(); @@ -366,10 +354,7 @@ base::Optional MakeConstDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs(); if (!IsValidModuleConstName(name)) { - std::stringstream sstream; - sstream << "Constant \"" << name << "\" doesn't follow " - << "\"kUpperCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Constant", name, "kUpperCamelCase"); } auto type = child_results->NextAs(); @@ -400,6 +385,9 @@ base::Optional MakeTypeAliasDeclaration( base::Optional MakeTypeDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs(); + if (!IsValidTypeName(name)) { + NamingConventionError("Type", name, "UpperCamelCase"); + } auto extends = child_results->NextAs>(); auto generates = child_results->NextAs>(); auto constexpr_generates = @@ -414,10 +402,7 @@ base::Optional MakeExplicitModuleDeclaration( ParseResultIterator* child_results) { auto name = child_results->NextAs(); if (!IsSnakeCase(name)) { - std::stringstream sstream; - sstream << "Module \"" << name << "\" doesn't follow " - << "\"snake_case\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Module", name, "snake_case"); } auto declarations = child_results->NextAs>(); Declaration* result = MakeNode( @@ -653,10 +638,7 @@ base::Optional MakeVarDeclarationStatement( if (!const_qualified) DCHECK_EQ("let", kind); auto name = child_results->NextAs(); if (!IsLowerCamelCase(name)) { - std::stringstream sstream; - sstream << "Variable \"" << name << "\" doesn't follow " - << "\"lowerCamelCase\" naming convention."; - LintError(sstream.str()); + NamingConventionError("Variable", name, "lowerCamelCase"); } auto type = child_results->NextAs(); diff --git a/src/torque/utils.cc b/src/torque/utils.cc index 32ec8dee1f..cda92340c2 100644 --- a/src/torque/utils.cc +++ b/src/torque/utils.cc @@ -88,6 +88,14 @@ void LintError(const std::string& error) { 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 { bool ContainsUnderscore(const std::string& s) { @@ -111,6 +119,17 @@ bool IsKeywordLikeName(const std::string& s) { 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 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 bool IsLowerCamelCase(const std::string& s) { @@ -135,6 +154,13 @@ bool IsValidModuleConstName(const std::string& s) { 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 result; bool word_beginning = true; diff --git a/src/torque/utils.h b/src/torque/utils.h index a85fb6d02a..883e83379c 100644 --- a/src/torque/utils.h +++ b/src/torque/utils.h @@ -35,10 +35,16 @@ class LintErrorStatus : public ContextualClass { [[noreturn]] void ReportError(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 IsUpperCamelCase(const std::string& s); bool IsSnakeCase(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 DashifyString(const std::string& underscore_string);