Add style check for ctype functions that are generally frowned upon in WebKit
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Feb 2012 03:18:27 +0000 (03:18 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Feb 2012 03:18:27 +0000 (03:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=78748

Patch by Sam Weinig <sam@webkit.org> on 2012-02-15
Reviewed by Anders Carlsson.

Not every platform has DisallowCType.h to check for uses of the ctype.h
functions, so add a style check for them as well.

* Scripts/webkitpy/style/checkers/cpp.py:
(check_ctype_functions):
(check_style):
(CppChecker):
Add check.

* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(WebKitStyleTest.test_ctype_fucntion):
Add checker.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@107872 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/webkitpy/style/checkers/cpp.py
Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py

index 12114b3..f8a80e2 100644 (file)
@@ -1,3 +1,23 @@
+2012-02-15  Sam Weinig  <sam@webkit.org>
+
+        Add style check for ctype functions that are generally frowned upon in WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=78748
+
+        Reviewed by Anders Carlsson.
+
+        Not every platform has DisallowCType.h to check for uses of the ctype.h
+        functions, so add a style check for them as well.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_ctype_functions):
+        (check_style):
+        (CppChecker):
+        Add check.
+
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (WebKitStyleTest.test_ctype_fucntion):
+        Add checker.
+
 2012-02-15  Szilard Ledan  <Ledan-Muntean.Szilard@stud.u-szeged.hu>
 
         [Qt][WK2] WebKitTestRunner should use 480x360 sized view for W3C SVG tests
index 02784e5..f293617 100644 (file)
@@ -2077,6 +2077,29 @@ def check_max_min_macros(clean_lines, line_number, file_state, error):
           % (max_min_macro_lower, max_min_macro_lower, max_min_macro))
 
 
+def check_ctype_functions(clean_lines, line_number, file_state, error):
+    """Looks for use of the standard functions in ctype.h and suggest they be replaced
+       by use of equivilent ones in <wtf/ASCIICType.h>?.
+
+    Args:
+      clean_lines: A CleansedLines instance containing the file.
+      line_number: The number of the line to check.
+      file_state: A _FileState instance which maintains information about
+                  the state of things in the file.
+      error: The function to call with any errors found.
+    """
+
+    line = clean_lines.elided[line_number]  # Get rid of comments and strings.
+
+    ctype_function_search = search(r'\b(?P<ctype_function>(isalnum|isalpha|isascii|isblank|iscntrl|isdigit|isgraph|islower|isprint|ispunct|isspace|isupper|isxdigit|toascii|tolower|toupper))\s*\(', line)
+    if not ctype_function_search:
+        return
+
+    ctype_function = ctype_function_search.group('ctype_function')
+    error(line_number, 'runtime/ctype_function', 4,
+          'Use equivelent function in <wtf/ASCIICType.h> instead of the %s() function.'
+          % (ctype_function))
+
 def check_switch_indentation(clean_lines, line_number, error):
     """Looks for indentation errors inside of switch statements.
 
@@ -2540,6 +2563,7 @@ def check_style(clean_lines, line_number, file_extension, class_state, file_stat
     check_namespace_indentation(clean_lines, line_number, file_extension, file_state, error)
     check_using_std(clean_lines, line_number, file_state, error)
     check_max_min_macros(clean_lines, line_number, file_state, error)
+    check_ctype_functions(clean_lines, line_number, file_state, error)
     check_switch_indentation(clean_lines, line_number, error)
     check_braces(clean_lines, line_number, error)
     check_exit_statement_simplifications(clean_lines, line_number, error)
@@ -3528,6 +3552,7 @@ class CppChecker(object):
         'runtime/arrays',
         'runtime/bitfields',
         'runtime/casting',
+        'runtime/ctype_function',
         'runtime/explicit',
         'runtime/init',
         'runtime/int',
index 17b1590..544e830 100644 (file)
@@ -4347,6 +4347,13 @@ class WebKitStyleTest(CppStyleTestBase):
             '  [runtime/max_min_macros] [4]',
             'foo.h')
 
+    def test_ctype_fucntion(self):
+        self.assert_lint(
+            'int i = isascii(8);',
+            'Use equivelent function in <wtf/ASCIICType.h> instead of the '
+            'isascii() function.  [runtime/ctype_function] [4]',
+            'foo.cpp')
+
     def test_names(self):
         name_underscore_error_message = " is incorrectly named. Don't use underscores in your identifier names.  [readability/naming] [4]"
         name_tooshort_error_message = " is incorrectly named. Don't use the single letter 'l' as an identifier name.  [readability/naming] [4]"