Commit d6011e17 authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Add the precise-width integer check in cpp.py

Add a PRESUBMIT check to ensure new |unsigned short| use don't
come in.

Bug: 929986
Change-Id: Ib4031362495ceedd94bc7e33fcbb913aea1e6ee2
Reviewed-on: https://chromium-review.googlesource.com/c/1493642Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Commit-Queue: Miyoung Shin <myid.shin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636674}
parent 16810537
...@@ -1865,6 +1865,13 @@ def check_language(filename, clean_lines, line_number, file_extension, include_s ...@@ -1865,6 +1865,13 @@ def check_language(filename, clean_lines, line_number, file_extension, include_s
# FIXME: figure out if they're using default arguments in fn proto. # FIXME: figure out if they're using default arguments in fn proto.
# Check if they're using a precise-width integer type.
matched = search(r'\bunsigned short\b', line)
if matched:
error(line_number, 'runtime/int', 1,
'Use a precise-width integer type from <stdint.h> or <cstdint>'
' such as uint16_t instead of unsigned short')
# Check to see if they're using an conversion function cast. # Check to see if they're using an conversion function cast.
# I just try to capture the most common basic types, though there are more. # I just try to capture the most common basic types, though there are more.
# Parameterless conversion functions, such as bool(), are allowed as they are # Parameterless conversion functions, such as bool(), are allowed as they are
...@@ -2623,6 +2630,7 @@ class CppChecker(object): ...@@ -2623,6 +2630,7 @@ class CppChecker(object):
'runtime/casting', 'runtime/casting',
'runtime/ctype_function', 'runtime/ctype_function',
'runtime/explicit', 'runtime/explicit',
'runtime/int',
'runtime/invalid_increment', 'runtime/invalid_increment',
'runtime/max_min_macros', 'runtime/max_min_macros',
'runtime/memset', 'runtime/memset',
......
...@@ -456,6 +456,13 @@ class CppStyleTest(CppStyleTestBase): ...@@ -456,6 +456,13 @@ class CppStyleTest(CppStyleTestBase):
self.assertEqual(cpp_style.Position(1, 1), cpp_style.close_expression(['}{}{', '}'], cpp_style.Position(0, 3))) self.assertEqual(cpp_style.Position(1, 1), cpp_style.close_expression(['}{}{', '}'], cpp_style.Position(0, 3)))
self.assertEqual(cpp_style.Position(2, -1), cpp_style.close_expression(['][][', ' '], cpp_style.Position(0, 3))) self.assertEqual(cpp_style.Position(2, -1), cpp_style.close_expression(['][][', ' '], cpp_style.Position(0, 3)))
# Test the integer type.
def test_precise_width_integer(self):
self.assert_lint(
'unsigned short a = 1',
'Use a precise-width integer type from <stdint.h> or <cstdint> such as uint16_t instead of unsigned short'
' [runtime/int] [1]')
# Test C-style cast cases. # Test C-style cast cases.
def test_cstyle_cast(self): def test_cstyle_cast(self):
self.assert_lint( self.assert_lint(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment