Commit 13b62afb authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Add the precise-width integer check for short type in cpp.py

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

Bug: 929985
Change-Id: Ifae1e6ba1af4d6c6ee0b0b8dffe608e0e710b291
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1503174Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Miyoung Shin <myid.shin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638462}
parent f451f9ea
...@@ -1866,11 +1866,11 @@ def check_language(filename, clean_lines, line_number, file_extension, include_s ...@@ -1866,11 +1866,11 @@ 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. # Check if they're using a precise-width integer type.
matched = search(r'\bunsigned short\b', line) matched = search(r'\b((un)?signed\s+)?short\b', line)
if matched: if matched:
error(line_number, 'runtime/int', 1, error(line_number, 'runtime/int', 1,
'Use a precise-width integer type from <stdint.h> or <cstdint>' 'Use a precise-width integer type from <stdint.h> or <cstdint>'
' such as uint16_t instead of unsigned short') ' such as uint16_t instead of %s' % matched.group(0))
# 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.
......
...@@ -458,10 +458,11 @@ class CppStyleTest(CppStyleTestBase): ...@@ -458,10 +458,11 @@ class CppStyleTest(CppStyleTestBase):
# Test the integer type. # Test the integer type.
def test_precise_width_integer(self): def test_precise_width_integer(self):
self.assert_lint( errmsg = ('Use a precise-width integer type from <stdint.h> or <cstdint> such as uint16_t instead of %s')
'unsigned short a = 1', self.assert_lint('unsigned short a = 1', errmsg % 'unsigned short [runtime/int] [1]')
'Use a precise-width integer type from <stdint.h> or <cstdint> such as uint16_t instead of unsigned short' self.assert_lint('uint16_t unsignedshort = 1', '')
' [runtime/int] [1]') self.assert_lint('signed short a = 1', errmsg % 'signed short [runtime/int] [1]')
self.assert_lint('short a = 1', errmsg % 'short [runtime/int] [1]')
# Test C-style cast cases. # Test C-style cast cases.
def test_cstyle_cast(self): def test_cstyle_cast(self):
...@@ -1508,7 +1509,7 @@ class CppStyleTest(CppStyleTestBase): ...@@ -1508,7 +1509,7 @@ class CppStyleTest(CppStyleTestBase):
errmsg = ('Please declare integral type bitfields with either signed or unsigned. [runtime/bitfields] [5]') errmsg = ('Please declare integral type bitfields with either signed or unsigned. [runtime/bitfields] [5]')
self.assert_lint('int a : 30;', errmsg) self.assert_lint('int a : 30;', errmsg)
self.assert_lint('mutable short a : 14;', errmsg) self.assert_lint('mutable int a : 14;', errmsg)
self.assert_lint('const char a : 6;', errmsg) self.assert_lint('const char a : 6;', errmsg)
self.assert_lint('long int a : 30;', errmsg) self.assert_lint('long int a : 30;', errmsg)
self.assert_lint('int a = 1 ? 0 : 30;', '') self.assert_lint('int a = 1 ? 0 : 30;', '')
......
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