Title: [294542] trunk/Tools/Scripts/webkitpy/style/checkers
Revision
294542
Author
yu...@chromium.org
Date
2022-05-20 05:58:07 -0700 (Fri, 20 May 2022)

Log Message

check-webkit-style: more strict upper case enum exceptions
https://bugs.webkit.org/show_bug.cgi?id=240681

Reviewed by Darin Adler.

Added a new list of common abbreviations which are allowed to be
used as all uppercase enum values.

* Tools/Scripts/webkitpy/style/checkers/cpp.py:
(_EnumState.process_clean_line):
(_EnumState.process_clean_line.is_case_error): Extracted common
logic for inline and multiline enum declarations.
(_EnumState):
* Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py:
(NoNonVirtualDestructorsTest):

Canonical link: https://commits.webkit.org/250797@main

Modified Paths

Diff

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py (294541 => 294542)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2022-05-20 12:02:01 UTC (rev 294541)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2022-05-20 12:58:07 UTC (rev 294542)
@@ -1353,18 +1353,27 @@
         # FIXME: The regular expressions for expr_all_uppercase and expr_enum_end only accept integers
         # and identifiers for the value of the enumerator, but do not accept any other constant
         # expressions. However, this is sufficient for now (11/27/2012).
-        expr_all_uppercase = r'\s*[A-Z0-9_]+\s*(?:=\s*[a-zA-Z0-9]+\s*)?,?\s*$'
+        expr_all_uppercase = r'\s*(?P<value>[A-Z0-9_]+)\s*(?:=\s*[a-zA-Z0-9]+\s*)?,?\s*$'
         expr_starts_lowercase = r'\s*[a-jl-z]'
         expr_enum_end = r'}\s*(?:[a-zA-Z0-9]+\s*(?:=\s*[a-zA-Z0-9]+)?)?\s*;\s*'
         expr_enum_start = r'\s*(?:enum(?:\s+class)?(?:\s+(?P<identifier>[a-zA-Z0-9]+))?)(?:\s*:\s*[a-zA-Z0-9_]+?)?\s*\{?\s*'
+
+        def is_case_error(enum_name, value_declaration):
+            all_uppercase = match(expr_all_uppercase, value_declaration)
+            if all_uppercase:
+                if self.is_webidl_enum:
+                    return False
+                if enum_name in _ALLOW_ALL_UPPERCASE_ENUM:
+                    return False
+                return not all_uppercase.group('value') in _ALLOW_ABBREVIATION_ENUM_VALUES
+            return match(expr_starts_lowercase, value_declaration)
+
         if self.in_enum_decl:
             if match(r'\s*' + expr_enum_end + r'$', line):
                 self.in_enum_decl = False
                 self.is_webidl_enum = False
                 return True
-            elif match(expr_all_uppercase, line):
-                return self.is_webidl_enum or self.enum_decl_name in _ALLOW_ALL_UPPERCASE_ENUM
-            elif match(expr_starts_lowercase, line):
+            elif is_case_error(self.enum_decl_name, line):
                 return False
         matched = match(expr_enum_start + r'$', line)
         if matched:
@@ -1374,14 +1383,9 @@
             matched = match(expr_enum_start + r'(?P<members>[^{]*)' + expr_enum_end + r'$', line)
             if matched:
                 members = matched.group('members').split(',')
-                allow_all_uppercase = matched.group('identifier') in _ALLOW_ALL_UPPERCASE_ENUM
-                found_invalid_member = False
+                enum_name = matched.group('identifier')
                 for member in members:
-                    if match(expr_all_uppercase, member):
-                        found_invalid_member = not self.is_webidl_enum and not allow_all_uppercase
-                    if match(expr_starts_lowercase, member):
-                        found_invalid_member = True
-                    if found_invalid_member:
+                    if is_case_error(enum_name, member):
                         self.is_webidl_enum = False
                         return False
                 return True
@@ -2546,8 +2550,10 @@
 
 
 # Enum declaration allowlist
-_ALLOW_ALL_UPPERCASE_ENUM = ['JSTokenType', 'Meridiem', 'NSType', 'Requester']
+_ALLOW_ALL_UPPERCASE_ENUM = ['JSTokenType']
 
+# Enum value allowlist
+_ALLOW_ABBREVIATION_ENUM_VALUES = ['AM', 'CF', 'PM', 'URL', 'XHR']
 
 def check_enum_casing(clean_lines, line_number, enum_state, error):
     """Looks for incorrectly named enum values.

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py (294541 => 294542)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2022-05-20 12:02:01 UTC (rev 294541)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2022-05-20 12:58:07 UTC (rev 294542)
@@ -4333,8 +4333,23 @@
                 enum class Requester { Main, XHR };''',
             '')
 
+        self.assert_lint(
+            '''enum class CommonAbbreviations { AM, CF, PM, URL, XHR };''',
+            '')
+
         self.assert_multi_line_lint(
             '''\
+                enum class CommonAbbreviationsMultiline {
+                    AM,
+                    CF,
+                    PM,
+                    URL,
+                    XHR
+                };''',
+            '')
+
+        self.assert_multi_line_lint(
+            '''\
                 enum Foo {
                     fooOne = 1,
                     FooTwo = 2
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to