Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py (206896 => 206897)
--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py 2016-10-07 03:21:27 UTC (rev 206896)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py 2016-10-07 03:26:03 UTC (rev 206897)
@@ -896,74 +896,35 @@
'You should have a line: "Copyright [year] <Copyright Owner>"')
-def get_header_guard_cpp_variable(filename):
- """Returns the CPP variable that should be used as a header guard.
-
- Args:
- filename: The name of a C++ header file.
-
- Returns:
- The CPP variable that should be used as a header guard in the
- named file.
-
- """
-
- # Restores original filename in case that style checker is invoked from Emacs's
- # flymake.
- filename = re.sub(r'_flymake\.h$', '.h', filename)
-
- standard_name = sub(r'[-.\s]', '_', os.path.basename(filename))
-
- # Files under WTF typically have header guards that start with WTF_.
- if '/wtf/' in filename:
- special_name = "WTF_" + standard_name
- else:
- special_name = standard_name
- return (special_name, standard_name)
-
-
-def check_for_header_guard(filename, lines, error):
+def check_for_header_guard(lines, error):
"""Checks that the file contains a header guard.
- Logs an error if no #ifndef header guard is present. For other
- headers, checks that the full pathname is used.
+ Logs an error if no #pragma once header guard is present
+ of if there was an #ifndef guard that was modified.
Args:
- filename: The name of the C++ header file.
lines: An array of strings, each representing a line of the file.
error: The function to call with any errors found.
"""
- cppvar = get_header_guard_cpp_variable(filename)
+ for line_number, line in enumerate(lines):
+ if line.startswith('#pragma once'):
+ return
- ifndef = None
+ # If there is no #pragma once, but there is an #ifndef, warn only if it was modified.
ifndef_line_number = 0
- define = None
for line_number, line in enumerate(lines):
line_split = line.split()
if len(line_split) >= 2:
- # find the first occurrence of #ifndef and #define, save arg
- if not ifndef and line_split[0] == '#ifndef':
- # set ifndef to the header guard presented on the #ifndef line.
- ifndef = line_split[1]
- ifndef_line_number = line_number
- if not define and line_split[0] == '#define':
- define = line_split[1]
- if define and ifndef:
- break
+ if line_split[0] == '#ifndef' and line_split[1].endswith('_h'):
+ error(line_number, 'build/header_guard', 5,
+ 'Use #pragma once instead of #ifndef for header guard.')
+ return
- if not ifndef or not define or ifndef != define:
- error(0, 'build/header_guard', 5,
- 'No #ifndef header guard found, suggested CPP variable is: %s' %
- cppvar[0])
- return
+ error(0, 'build/header_guard', 5,
+ 'Use #pragma once header guard.')
- # The guard should be File_h.
- if ifndef not in cppvar:
- error(ifndef_line_number, 'build/header_guard', 5,
- '#ifndef header guard has wrong style, please use: %s' % cppvar[0])
-
def check_for_unicode_replacement_characters(lines, error):
"""Logs an error for each line containing Unicode replacement characters.
@@ -3855,7 +3816,7 @@
check_for_copyright(lines, error)
if file_extension == 'h':
- check_for_header_guard(filename, lines, error)
+ check_for_header_guard(lines, error)
if filename == 'Source/WTF/wtf/Platform.h':
check_platformh_comments(lines, error)
Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py (206896 => 206897)
--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py 2016-10-07 03:21:27 UTC (rev 206896)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py 2016-10-07 03:26:03 UTC (rev 206897)
@@ -288,29 +288,30 @@
# Only keep function length errors.
def perform_function_lengths_check(self, code):
- basic_error_rules = ('-',
- '+readability/fn_size')
+ basic_error_rules = ('-', '+readability/fn_size')
return self.perform_lint(code, 'test.cpp', basic_error_rules)
# Only keep pass ptr errors.
def perform_pass_ptr_check(self, code):
- basic_error_rules = ('-',
- '+readability/pass_ptr')
+ basic_error_rules = ('-', '+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')
+ 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 = ('-',
- '+build/include_what_you_use')
+ basic_error_rules = ('-', '+build/include_what_you_use')
unit_test_config = {cpp_style.INCLUDE_IO_INJECTION_KEY: io}
return self.perform_lint(code, filename, basic_error_rules, unit_test_config)
+ # Only include header guard errors.
+ def perform_header_guard_check(self, code, filename='foo.h'):
+ basic_error_rules = ('-', '+build/header_guard')
+ return self.perform_lint(code, filename, basic_error_rules)
+
# Perform lint and compare the error message with "expected_message".
def assert_lint(self, code, expected_message, file_name='foo.cpp'):
self.assertEqual(expected_message, self.perform_single_line_lint(code, file_name))
@@ -341,6 +342,10 @@
self.assertEqual(expected_message,
self.perform_include_what_you_use(code))
+ def assert_header_guard(self, code, expected_message):
+ self.assertEqual(expected_message,
+ self.perform_header_guard_check(code))
+
def assert_blank_lines_check(self, lines, start_errors, end_errors):
error_collector = ErrorCollector(self.assertTrue)
self.process_file_data('foo.cpp', 'cpp', lines, error_collector)
@@ -2415,138 +2420,21 @@
' [build/forward_decl] [5]')
def test_build_header_guard(self):
- file_path = 'mydir/Foo.h'
+ rules = ('-', '+build/header_guard')
- # We can't rely on our internal stuff to get a sane path on the open source
- # side of things, so just parse out the suggested header guard. This
- # doesn't allow us to test the suggested header guard, but it does let us
- # test all the other header tests.
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h', [], error_collector)
- expected_guard = ''
- matcher = re.compile(
- 'No \#ifndef header guard found\, suggested CPP variable is\: ([A-Za-z_0-9]+) ')
- for error in error_collector.result_list():
- matches = matcher.match(error)
- if matches:
- expected_guard = matches.group(1)
- break
+ # No header guard.
+ self.assert_header_guard('',
+ 'Use #pragma once header guard.'
+ ' [build/header_guard] [5]')
- # Make sure we extracted something for our header guard.
- self.assertNotEqual(expected_guard, '')
+ # Old header guard.
+ self.assert_header_guard('#ifndef Foo_h',
+ 'Use #pragma once instead of #ifndef for header guard.'
+ ' [build/header_guard] [5]')
- # Wrong guard
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h',
- ['#ifndef FOO_H', '#define FOO_H'], error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- '#ifndef header guard has wrong style, please use: %s'
- ' [build/header_guard] [5]' % expected_guard),
- error_collector.result_list())
+ # Valid header guard.
+ self.assert_header_guard('#pragma once', '')
- # No define
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h',
- ['#ifndef %s' % expected_guard], error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- 'No #ifndef header guard found, suggested CPP variable is: %s'
- ' [build/header_guard] [5]' % expected_guard),
- error_collector.result_list())
-
- # Mismatched define
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h',
- ['#ifndef %s' % expected_guard,
- '#define FOO_H'],
- error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- 'No #ifndef header guard found, suggested CPP variable is: %s'
- ' [build/header_guard] [5]' % expected_guard),
- error_collector.result_list())
-
- # No header guard errors
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h',
- ['#ifndef %s' % expected_guard,
- '#define %s' % expected_guard,
- '#endif // %s' % expected_guard],
- error_collector)
- for line in error_collector.result_list():
- if line.find('build/header_guard') != -1:
- self.fail('Unexpected error: %s' % line)
-
- # Completely incorrect header guard
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data(file_path, 'h',
- ['#ifndef FOO',
- '#define FOO',
- '#endif // FOO'],
- error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- '#ifndef header guard has wrong style, please use: %s'
- ' [build/header_guard] [5]' % expected_guard),
- error_collector.result_list())
-
- # Special case for flymake
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data('mydir/Foo_flymake.h', 'h',
- ['#ifndef %s' % expected_guard,
- '#define %s' % expected_guard,
- '#endif // %s' % expected_guard],
- error_collector)
- for line in error_collector.result_list():
- if line.find('build/header_guard') != -1:
- self.fail('Unexpected error: %s' % line)
-
- error_collector = ErrorCollector(self.assertTrue)
- self.process_file_data('mydir/Foo_flymake.h', 'h', [], error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- 'No #ifndef header guard found, suggested CPP variable is: %s'
- ' [build/header_guard] [5]' % expected_guard),
- error_collector.result_list())
-
- # Verify that we don't blindly suggest the WTF prefix for all headers.
- self.assertFalse(expected_guard.startswith('WTF_'))
-
- # Allow the WTF_ prefix for files in that directory.
- header_guard_filter = FilterConfiguration(('-', '+build/header_guard'))
- error_collector = ErrorCollector(self.assertTrue, header_guard_filter)
- self.process_file_data('Source/_javascript_Core/wtf/TestName.h', 'h',
- ['#ifndef WTF_TestName_h', '#define WTF_TestName_h'],
- error_collector)
- self.assertEqual(0, len(error_collector.result_list()),
- error_collector.result_list())
-
- # Also allow the non WTF_ prefix for files in that directory.
- error_collector = ErrorCollector(self.assertTrue, header_guard_filter)
- self.process_file_data('Source/_javascript_Core/wtf/TestName.h', 'h',
- ['#ifndef TestName_h', '#define TestName_h'],
- error_collector)
- self.assertEqual(0, len(error_collector.result_list()),
- error_collector.result_list())
-
- # Verify that we suggest the WTF prefix version.
- error_collector = ErrorCollector(self.assertTrue, header_guard_filter)
- self.process_file_data('Source/_javascript_Core/wtf/TestName.h', 'h',
- ['#ifndef BAD_TestName_h', '#define BAD_TestName_h'],
- error_collector)
- self.assertEqual(
- 1,
- error_collector.result_list().count(
- '#ifndef header guard has wrong style, please use: WTF_TestName_h'
- ' [build/header_guard] [5]'),
- error_collector.result_list())
-
def test_build_printf_format(self):
self.assert_lint(
r'printf("\%%d", value);',