Title: [104995] trunk
Revision
104995
Author
[email protected]
Date
2012-01-13 14:58:00 -0800 (Fri, 13 Jan 2012)

Log Message

check-webkit-style: should encourage the use of Own* classes for Windows DC.
https://bugs.webkit.org/show_bug.cgi?id=76227

Reviewed by Dirk Pranke.

Source/_javascript_Core:

* wtf/win/HWndDCWin.h:
(WTF::HwndDC::HwndDC): Add a way to do GetDCEx.
There are no users, but I want to catch this in check-webkit-style
and tell any users to use HwndDC to avoid leaks.

Tools:

* Scripts/webkitpy/style/checkers/cpp.py:
(check_for_leaky_patterns): The new check.
(process_line): Added a call to the new check.
(CppChecker): Added the new error type.
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(CppStyleTestBase):
(CppStyleTestBase.perform_leaky_pattern_check):
The check for only leaky pattern errors.
(LeakyPatternTest): Test cases.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (104994 => 104995)


--- trunk/Source/_javascript_Core/ChangeLog	2012-01-13 22:52:08 UTC (rev 104994)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-01-13 22:58:00 UTC (rev 104995)
@@ -1,5 +1,17 @@
 2012-01-13  David Levin  <[email protected]>
 
+        check-webkit-style: should encourage the use of Own* classes for Windows DC.
+        https://bugs.webkit.org/show_bug.cgi?id=76227
+
+        Reviewed by Dirk Pranke.
+
+        * wtf/win/HWndDCWin.h:
+        (WTF::HwndDC::HwndDC): Add a way to do GetDCEx.
+        There are no users, but I want to catch this in check-webkit-style
+        and tell any users to use HwndDC to avoid leaks.
+
+2012-01-13  David Levin  <[email protected]>
+
         Header file is missing header guard.
 
         Reviewed by Dirk Pranke.

Modified: trunk/Source/_javascript_Core/wtf/win/HWndDCWin.h (104994 => 104995)


--- trunk/Source/_javascript_Core/wtf/win/HWndDCWin.h	2012-01-13 22:52:08 UTC (rev 104994)
+++ trunk/Source/_javascript_Core/wtf/win/HWndDCWin.h	2012-01-13 22:58:00 UTC (rev 104995)
@@ -38,6 +38,12 @@
     {
     }
 
+    HWndDC(HWND hwnd, HRGN hrgnClip, DWORD flags)
+        : m_hwnd(hwnd)
+        , m_hdc(::GetDCEx(hwnd, hrgnClip, flags))
+    {
+    }
+
     ~HWndDC()
     {
         if (m_hdc)

Modified: trunk/Tools/ChangeLog (104994 => 104995)


--- trunk/Tools/ChangeLog	2012-01-13 22:52:08 UTC (rev 104994)
+++ trunk/Tools/ChangeLog	2012-01-13 22:58:00 UTC (rev 104995)
@@ -1,3 +1,20 @@
+2012-01-13  David Levin  <[email protected]>
+
+        check-webkit-style: should encourage the use of Own* classes for Windows DC.
+        https://bugs.webkit.org/show_bug.cgi?id=76227
+
+        Reviewed by Dirk Pranke.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_for_leaky_patterns): The new check.
+        (process_line): Added a call to the new check.
+        (CppChecker): Added the new error type.
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (CppStyleTestBase):
+        (CppStyleTestBase.perform_leaky_pattern_check):
+        The check for only leaky pattern errors.
+        (LeakyPatternTest): Test cases.
+
 2012-01-13  Dirk Pranke  <[email protected]>
 
         test-webkitpy: clean up handling of test directories, QueueStatusServer

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2012-01-13 22:52:08 UTC (rev 104994)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2012-01-13 22:58:00 UTC (rev 104995)
@@ -1662,6 +1662,31 @@
                   'http://webkit.org/coding/RefPtr.html).' % type_name)
 
 
+def check_for_leaky_patterns(clean_lines, line_number, function_state, error):
+    """Check for constructs known to be leak prone.
+    Args:
+      clean_lines: A CleansedLines instance containing the file.
+      line_number: The number of the line to check.
+      function_state: Current function name and lines in body so far.
+      error: The function to call with any errors found.
+    """
+    lines = clean_lines.lines
+    line = lines[line_number]
+
+    matched_get_dc = search(r'\b(?P<function_name>GetDC(Ex)?)\s*\(', line)
+    if matched_get_dc:
+        error(line_number, 'runtime/leaky_pattern', 5,
+              'Use the class HWndDC instead of calling %s to avoid potential '
+              'memory leaks.' % matched_get_dc.group('function_name'))
+
+    matched_create_dc = search(r'\b(?P<function_name>Create(Compatible)?DC)\s*\(', line)
+    matched_own_dc = search(r'\bOwnPtr\<HDC\>\s+', line)
+    if matched_create_dc and not matched_own_dc:
+        error(line_number, 'runtime/leaky_pattern', 5,
+              'Use OwnPtr<HDC> when calling %s to avoid potential '
+              'memory leaks.' % matched_create_dc.group('function_name'))
+
+
 def check_spacing(file_extension, clean_lines, line_number, error):
     """Checks for the correctness of various spacing issues in the code.
 
@@ -3400,6 +3425,7 @@
         return
     check_function_definition(filename, file_extension, clean_lines, line, function_state, error)
     check_pass_ptr_usage(clean_lines, line, function_state, error)
+    check_for_leaky_patterns(clean_lines, line, function_state, error)
     check_for_multiline_comments_and_strings(clean_lines, line, error)
     check_style(clean_lines, line, file_extension, class_state, file_state, error)
     check_language(filename, clean_lines, line, file_extension, include_state,
@@ -3497,6 +3523,7 @@
         'runtime/init',
         'runtime/int',
         'runtime/invalid_increment',
+        'runtime/leaky_pattern',
         'runtime/max_min_macros',
         'runtime/memset',
         'runtime/printf',

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


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2012-01-13 22:52:08 UTC (rev 104994)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2012-01-13 22:58:00 UTC (rev 104995)
@@ -292,6 +292,12 @@
                              '+readability/pass_ptr')
         return self.perform_lint(code, 'test.cpp', basic_error_rules)
 
+    # Only keep leaky pattern errors.
+    def perform_leaky_pattern_check(self, code):
+        basic_error_rules = ('-',
+                             '+runtime/leaky_pattern')
+        return self.perform_lint(code, 'test.cpp', basic_error_rules)
+
     # Only include what you use errors.
     def perform_include_what_you_use(self, code, filename='foo.h', io=codecs):
         basic_error_rules = ('-',
@@ -3327,6 +3333,55 @@
             '')
 
 
+class LeakyPatternTest(CppStyleTestBase):
+
+    def assert_leaky_pattern_check(self, code, expected_message):
+        """Check warnings for leaky patterns are as expected.
+
+        Args:
+          code: C++ source code expected to generate a warning message.
+          expected_message: Message expected to be generated by the C++ code.
+        """
+        self.assertEquals(expected_message,
+                          self.perform_leaky_pattern_check(code))
+
+    def test_get_dc(self):
+        self.assert_leaky_pattern_check(
+            'HDC hdc = GetDC(hwnd);',
+            'Use the class HWndDC instead of calling GetDC to avoid potential '
+            'memory leaks.  [runtime/leaky_pattern] [5]')
+
+    def test_get_dc(self):
+        self.assert_leaky_pattern_check(
+            'HDC hdc = GetDCEx(hwnd, 0, 0);',
+            'Use the class HWndDC instead of calling GetDCEx to avoid potential '
+            'memory leaks.  [runtime/leaky_pattern] [5]')
+
+    def test_own_get_dc(self):
+        self.assert_leaky_pattern_check(
+            'HWndDC hdc(hwnd);',
+            '')
+
+    def test_create_dc(self):
+        self.assert_leaky_pattern_check(
+            'HDC dc2 = ::CreateDC();',
+            'Use OwnPtr<HDC> when calling CreateDC to avoid potential '
+            'memory leaks.  [runtime/leaky_pattern] [5]')
+
+        self.assert_leaky_pattern_check(
+            'OwnPtr<HDC> dc2 = adoptPtr(CreateDC());',
+            '')
+
+    def test_create_compatible_dc(self):
+        self.assert_leaky_pattern_check(
+            'HDC dc2 = CreateCompatibleDC(dc);',
+            'Use OwnPtr<HDC> when calling CreateCompatibleDC to avoid potential '
+            'memory leaks.  [runtime/leaky_pattern] [5]')
+        self.assert_leaky_pattern_check(
+            'OwnPtr<HDC> dc2 = adoptPtr(CreateCompatibleDC(dc));',
+            '')
+
+
 class WebKitStyleTest(CppStyleTestBase):
 
     # for http://webkit.org/coding/coding-style.html
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to