Title: [265096] trunk/Tools
Revision
265096
Author
[email protected]
Date
2020-07-30 14:44:33 -0700 (Thu, 30 Jul 2020)

Log Message

check-webkit-style should enforce acronym capitalization at start/end of an identifier
<https://webkit.org/b/214954>

Reviewed by Jonathan Bedard.

* DumpRenderTree/mac/DumpRenderTree.mm:
(shouldIgnoreWebCoreNodeLeaks):
(changeWindowScaleIfNeeded):
- Fix case issues in variables using URL acronym.

* Scripts/webkitpy/style/checkers/cpp.py:
(_check_identifier_name_for_acronyms): Add.
- This contains the logic to report acronyms with invalid case
  at the start and at the end of an identifer.
(check_identifier_name_in_declaration):
- Keep track of whether the identifer came from a class,
  namespace or struct.
- Fix bug where `auto` variables would not be checked because
  `auto` was removed with other non-type keywords like `inline`,
  leaving no type for the variable.  Add a comment to describe
  what this line is doing.
- Fix bug when removing "new" that changed an identifier named
  "newURL" to "URL".
- Add call to _check_identifier_name_for_acronyms() to implement
  the check.
(CppChecker):
- Add 'readability/naming/acronym' to the list of active
  checkers.

* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(WebKitStyleTest.test_identifier_names_with_acronyms):
- Add tests.  About half of the tests were taken from actual
  code that initially caused false positives during development.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (265095 => 265096)


--- trunk/Tools/ChangeLog	2020-07-30 21:28:38 UTC (rev 265095)
+++ trunk/Tools/ChangeLog	2020-07-30 21:44:33 UTC (rev 265096)
@@ -1,3 +1,39 @@
+2020-07-30  David Kilzer  <[email protected]>
+
+        check-webkit-style should enforce acronym capitalization at start/end of an identifier
+        <https://webkit.org/b/214954>
+
+        Reviewed by Jonathan Bedard.
+
+        * DumpRenderTree/mac/DumpRenderTree.mm:
+        (shouldIgnoreWebCoreNodeLeaks):
+        (changeWindowScaleIfNeeded):
+        - Fix case issues in variables using URL acronym.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (_check_identifier_name_for_acronyms): Add.
+        - This contains the logic to report acronyms with invalid case
+          at the start and at the end of an identifer.
+        (check_identifier_name_in_declaration):
+        - Keep track of whether the identifer came from a class,
+          namespace or struct.
+        - Fix bug where `auto` variables would not be checked because
+          `auto` was removed with other non-type keywords like `inline`,
+          leaving no type for the variable.  Add a comment to describe
+          what this line is doing.
+        - Fix bug when removing "new" that changed an identifier named
+          "newURL" to "URL".
+        - Add call to _check_identifier_name_for_acronyms() to implement
+          the check.
+        (CppChecker):
+        - Add 'readability/naming/acronym' to the list of active
+          checkers.
+
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (WebKitStyleTest.test_identifier_names_with_acronyms):
+        - Add tests.  About half of the tests were taken from actual
+          code that initially caused false positives during development.
+
 2020-07-30  Beth Dakin  <[email protected]>
 
         Make report-non-inclusive-language ignore xcuserstate

Modified: trunk/Tools/DumpRenderTree/mac/DumpRenderTree.mm (265095 => 265096)


--- trunk/Tools/DumpRenderTree/mac/DumpRenderTree.mm	2020-07-30 21:28:38 UTC (rev 265095)
+++ trunk/Tools/DumpRenderTree/mac/DumpRenderTree.mm	2020-07-30 21:44:33 UTC (rev 265096)
@@ -294,7 +294,7 @@
     persistentUserStyleSheetLocation = url;
 }
 
-static bool shouldIgnoreWebCoreNodeLeaks(const string& URLString)
+static bool shouldIgnoreWebCoreNodeLeaks(const string& urlString)
 {
     static char* const ignoreSet[] = {
         // Keeping this infrastructure around in case we ever need it again.
@@ -304,8 +304,8 @@
     for (int i = 0; i < ignoreSetCount; i++) {
         // FIXME: ignore case
         string curIgnore(ignoreSet[i]);
-        // Match at the end of the URLString
-        if (!URLString.compare(URLString.length() - curIgnore.length(), curIgnore.length(), curIgnore))
+        // Match at the end of the urlString.
+        if (!urlString.compare(urlString.length() - curIgnore.length(), curIgnore.length(), curIgnore))
             return true;
     }
     return false;
@@ -1645,14 +1645,14 @@
 }
 
 #if !PLATFORM(IOS_FAMILY)
-static void changeWindowScaleIfNeeded(const char* testPathOrUrl)
+static void changeWindowScaleIfNeeded(const char* testPathOrURL)
 {
-    auto localPathOrUrl = String(testPathOrUrl);
+    auto localPathOrURL = String(testPathOrURL);
     float currentScaleFactor = [[[mainFrame webView] window] backingScaleFactor];
     float requiredScaleFactor = 1;
-    if (localPathOrUrl.containsIgnoringASCIICase("/hidpi-3x-"))
+    if (localPathOrURL.containsIgnoringASCIICase("/hidpi-3x-"))
         requiredScaleFactor = 3;
-    else if (localPathOrUrl.containsIgnoringASCIICase("/hidpi-"))
+    else if (localPathOrURL.containsIgnoringASCIICase("/hidpi-"))
         requiredScaleFactor = 2;
     if (currentScaleFactor == requiredScaleFactor)
         return;

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2020-07-30 21:28:38 UTC (rev 265095)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2020-07-30 21:44:33 UTC (rev 265096)
@@ -1796,6 +1796,40 @@
     return True
 
 
+def _check_identifier_name_for_acronyms(identifier, line_number, is_class_or_namespace_or_struct_name, error):
+    """Checks to see if the identifier name contains an acronym with improper case.
+
+    Using "url" is okay at the start of an identifier name, and "URL" is okay in the
+    middle or at the end of an identifier name, but "Url" is never okay.
+    """
+    acronyms = '|'.join(['MIME', 'URL'])
+
+    start_re = re.compile('^(%s)(|$)' % acronyms, re.IGNORECASE)
+    start_expected_re = re.compile('^(%s)([^:]|$)' % acronyms.lower())
+    # Identifiers that start with an acronym must be all lowercase, except for class/namespace/struct names.
+    if start_re.search(identifier) and not start_expected_re.search(identifier):
+        start_uppercase_re = re.compile('^(%s)' % acronyms)
+        # Ignore class/namespace/struct names that start with all-uppercase acronyms.
+        if start_uppercase_re.search(identifier) and \
+                (is_class_or_namespace_or_struct_name or identifier.find('::') != -1):
+            return True
+        error(line_number, 'readability/naming/acronym', 5,
+              'The identifier name "%s" starts with a acronym that is not all lowercase.' % identifier)
+        return False
+
+    # FIXME: Hard to check middle words without knowing that the word to the left doesn't end with an acronym.
+
+    # Identifiers that end with an acronym must be all uppercase, except for variables like 'm_url' and 'Class::url()'.
+    end_re = re.compile('[^_:](%s)$' % acronyms, re.IGNORECASE)
+    end_expected_re = re.compile('[^_:](%s)$' % acronyms)
+    if end_re.search(identifier) and not end_expected_re.search(identifier):
+        error(line_number, 'readability/naming/acronym', 5,
+              'The identifier name "%s" ends with a acronym that is not all uppercase.' % identifier)
+        return False
+
+    return True
+
+
 def _error_redundant_specifier(line_number, redundant_specifier, good_specifier, error):
     error(line_number, 'readability/inheritance', 4,
           '"%s" is redundant since function is already declared as "%s"'
@@ -3700,7 +3734,7 @@
 
 
 def check_identifier_name_in_declaration(filename, line_number, line, file_state, error):
-    """Checks if identifier names contain any underscores.
+    """Checks if identifier names contain any anti-patterns like underscores.
 
     As identifiers in libraries we are using have a bunch of
     underscores, we only warn about the declarations of identifiers
@@ -3745,10 +3779,18 @@
     line = sub(r'long (long )?(?=long|double|int)', '', line)
     # Convert unsigned/signed types to simple types, too.
     line = sub(r'(unsigned|signed) (?=char|short|int|long)', '', line)
-    line = sub(r'\b(inline|using|static|const|volatile|auto|register|extern|typedef|restrict|struct|class|virtual)(?=\W)', '', line)
 
+    is_class_or_namespace_or_struct_name = False
+    class_namespace_struct_name_re = re.compile('\\b(class|explicit|namespace|struct|%s)(?=\\W)' % '|'.join(_EXPORT_MACROS))
+    if class_namespace_struct_name_re.search(line):
+        is_class_or_namespace_or_struct_name = True
+
+    # Remove keywords that aren't types.
+    line = sub(r'\b(inline|using|static|const|volatile|register|extern|typedef|restrict|struct|class|virtual)(?=\W)', '', line)
+
     # Remove "new" and "new (expr)" to simplify, too.
-    line = sub(r'new\s*(\([^)]*\))?', '', line)
+    line = sub(r'new\s+', '', line)
+    line = sub(r'new\s*(\([^)]*\))', '', line)
 
     # Remove all template parameters by removing matching < and >.
     # Loop until no templates are removed to remove nested templates.
@@ -3838,6 +3880,8 @@
         if modified_identifier == 'l':
             error(line_number, 'readability/naming', 4, identifier + " is incorrectly named. Don't use the single letter 'l' as an identifier name.")
 
+        _check_identifier_name_for_acronyms(identifier, line_number, is_class_or_namespace_or_struct_name, error)
+
         # There can be only one declaration in non-for-control statements.
         if control_statement:
             return
@@ -4321,6 +4365,7 @@
         'readability/multiline_string',
         'readability/parameter_name',
         'readability/naming',
+        'readability/naming/acronym',
         'readability/naming/protected',
         'readability/naming/underscores',
         'readability/null',

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2020-07-30 21:28:38 UTC (rev 265095)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2020-07-30 21:44:33 UTC (rev 265096)
@@ -5949,6 +5949,79 @@
                                             '{\n'
                                             '}\n', 'test.cpp', parameter_error_rules))
 
+    def test_identifier_names_with_acronyms(self):
+        identifier_error_rules = ('-',
+                                  '+readability/naming/acronym')
+
+        # Start of parameter name.
+        error_start = 'The identifier name "%s" starts with a acronym that is not all lowercase.'\
+                      '  [readability/naming/acronym] [5]'
+
+        self.assertEqual('',
+                         self.perform_lint('void load(URL url);', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_start % 'Url',
+                         self.perform_lint('void load(URL Url);', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_start % 'URL',
+                         self.perform_lint('void load(URL URL);', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('void load(URL urlToLoad);', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_start % 'UrlToLoad',
+                         self.perform_lint('void load(URL UrlToLoad);', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_start % 'URLToLoad',
+                         self.perform_lint('void load(URL URLToLoad);', 'test.cpp', identifier_error_rules))
+
+        self.assertEqual('',
+                         self.perform_lint('friend class URL;', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('enum class URLPart;', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('using namespace URLHelpers;', 'test.cpp', identifier_error_rules))
+
+        self.assertEqual('',
+                         self.perform_lint('explicit URL(HashTableDeletedValueType);', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('WTF_EXPORT_PRIVATE URL(CFURLRef);', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('void URL::invalidate()', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('void URL::URL(const URL& base, const String& relative, const URLTextEncoding* encoding)', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('URL URL::isolatedCopy() const', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('URLParser::URLParser(const String& input, const URL& base, const URLTextEncoding* nonUTF8QueryEncoding)', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('bool URLParser::internalValuesConsistent(const URL& url)', 'test.cpp', identifier_error_rules))
+
+        self.assertEqual('',
+                         self.perform_lint('String m_url;', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('JSRetainPtr<JSStringRef> AccessibilityUIElement::url()', 'test.cpp', identifier_error_rules))
+
+        self.assertEqual(error_start % 'Url::Url',
+                         self.perform_lint('void Url::Url()', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_start % 'UrlParse::UrlParse',
+                         self.perform_lint('void UrlParse::UrlParse()', 'test.cpp', identifier_error_rules))
+
+        # FIXME: Hard to check middle words without knowing that the word to the left doesn't end with an acronym.
+
+        error_end = 'The identifier name "%s" ends with a acronym that is not all uppercase.'\
+                    '  [readability/naming/acronym] [5]'
+
+        # End of parameter name.
+        self.assertEqual(error_end % 'loadurl',
+                         self.perform_lint('void load(URL loadurl);', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_end % 'loadUrl',
+                         self.perform_lint('void load(URL loadUrl);', 'test.cpp', identifier_error_rules))
+        self.assertEqual('',
+                         self.perform_lint('void load(URL loadURL);', 'test.cpp', identifier_error_rules))
+
+        self.assertEqual('',
+                         self.perform_lint('void InspectorFrontendHost::inspectedURLChanged(const String& newURL)', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_end % 'testPathOrUrl',
+                         self.perform_lint('static void changeWindowScaleIfNeeded(const char* testPathOrUrl)', 'test.cpp', identifier_error_rules))
+        self.assertEqual(error_end % 'localPathOrUrl',
+                         self.perform_lint('auto localPathOrUrl = String(testPathOrURL);', 'test.cpp', identifier_error_rules))
+
     def test_comments(self):
         # A comment at the beginning of a line is ok.
         self.assert_lint('// comment', '')
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to