amiro-os / tools / cpplint / python / cpplint_unittest.py @ 53710ca3
History | View | Annotate | Download (216.815 KB)
| 1 | e545e620 | Thomas Schöpping | #!/usr/bin/python
|
|---|---|---|---|
| 2 | # -*- coding: utf-8; -*-
|
||
| 3 | #
|
||
| 4 | # Copyright (c) 2009 Google Inc. All rights reserved.
|
||
| 5 | #
|
||
| 6 | # Redistribution and use in source and binary forms, with or without
|
||
| 7 | # modification, are permitted provided that the following conditions are
|
||
| 8 | # met:
|
||
| 9 | #
|
||
| 10 | # * Redistributions of source code must retain the above copyright
|
||
| 11 | # notice, this list of conditions and the following disclaimer.
|
||
| 12 | # * Redistributions in binary form must reproduce the above
|
||
| 13 | # copyright notice, this list of conditions and the following disclaimer
|
||
| 14 | # in the documentation and/or other materials provided with the
|
||
| 15 | # distribution.
|
||
| 16 | # * Neither the name of Google Inc. nor the names of its
|
||
| 17 | # contributors may be used to endorse or promote products derived from
|
||
| 18 | # this software without specific prior written permission.
|
||
| 19 | #
|
||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
| 31 | |||
| 32 | """Unit test for cpplint.py."""
|
||
| 33 | |||
| 34 | # TODO(unknown): Add a good test that tests UpdateIncludeState.
|
||
| 35 | |||
| 36 | import codecs |
||
| 37 | import os |
||
| 38 | import random |
||
| 39 | import re |
||
| 40 | import sys |
||
| 41 | import unittest |
||
| 42 | |||
| 43 | import cpplint |
||
| 44 | |||
| 45 | |||
| 46 | # This class works as an error collector and replaces cpplint.Error
|
||
| 47 | # function for the unit tests. We also verify each category we see
|
||
| 48 | # is in cpplint._ERROR_CATEGORIES, to help keep that list up to date.
|
||
| 49 | class ErrorCollector(object): |
||
| 50 | # These are a global list, covering all categories seen ever.
|
||
| 51 | _ERROR_CATEGORIES = cpplint._ERROR_CATEGORIES |
||
| 52 | _SEEN_ERROR_CATEGORIES = {}
|
||
| 53 | |||
| 54 | def __init__(self, assert_fn): |
||
| 55 | """assert_fn: a function to call when we notice a problem."""
|
||
| 56 | self._assert_fn = assert_fn
|
||
| 57 | self._errors = []
|
||
| 58 | cpplint.ResetNolintSuppressions() |
||
| 59 | |||
| 60 | def __call__(self, unused_filename, linenum, |
||
| 61 | category, confidence, message): |
||
| 62 | self._assert_fn(category in self._ERROR_CATEGORIES, |
||
| 63 | 'Message "%s" has category "%s",'
|
||
| 64 | ' which is not in _ERROR_CATEGORIES' % (message, category))
|
||
| 65 | self._SEEN_ERROR_CATEGORIES[category] = 1 |
||
| 66 | if cpplint._ShouldPrintError(category, confidence, linenum):
|
||
| 67 | self._errors.append('%s [%s] [%d]' % (message, category, confidence)) |
||
| 68 | |||
| 69 | def Results(self): |
||
| 70 | if len(self._errors) < 2: |
||
| 71 | return ''.join(self._errors) # Most tests expect to have a string. |
||
| 72 | else:
|
||
| 73 | return self._errors # Let's give a list if there is more than one. |
||
| 74 | |||
| 75 | def ResultList(self): |
||
| 76 | return self._errors |
||
| 77 | |||
| 78 | def VerifyAllCategoriesAreSeen(self): |
||
| 79 | """Fails if there's a category in _ERROR_CATEGORIES~_SEEN_ERROR_CATEGORIES.
|
||
| 80 |
|
||
| 81 | This should only be called after all tests are run, so
|
||
| 82 | _SEEN_ERROR_CATEGORIES has had a chance to fully populate. Since
|
||
| 83 | this isn't called from within the normal unittest framework, we
|
||
| 84 | can't use the normal unittest assert macros. Instead we just exit
|
||
| 85 | when we see an error. Good thing this test is always run last!
|
||
| 86 | """
|
||
| 87 | for category in self._ERROR_CATEGORIES: |
||
| 88 | if category not in self._SEEN_ERROR_CATEGORIES: |
||
| 89 | sys.exit('FATAL ERROR: There are no tests for category "%s"' % category)
|
||
| 90 | |||
| 91 | def RemoveIfPresent(self, substr): |
||
| 92 | for (index, error) in enumerate(self._errors): |
||
| 93 | if error.find(substr) != -1: |
||
| 94 | self._errors = self._errors[0:index] + self._errors[(index + 1):] |
||
| 95 | break
|
||
| 96 | |||
| 97 | |||
| 98 | # This class is a lame mock of codecs. We do not verify filename, mode, or
|
||
| 99 | # encoding, but for the current use case it is not needed.
|
||
| 100 | class MockIo(object): |
||
| 101 | |||
| 102 | def __init__(self, mock_file): |
||
| 103 | self.mock_file = mock_file
|
||
| 104 | |||
| 105 | def open(self, # pylint: disable-msg=C6409 |
||
| 106 | unused_filename, unused_mode, unused_encoding, _): |
||
| 107 | return self.mock_file |
||
| 108 | |||
| 109 | |||
| 110 | class CpplintTestBase(unittest.TestCase): |
||
| 111 | """Provides some useful helper functions for cpplint tests."""
|
||
| 112 | |||
| 113 | def setUp(self): |
||
| 114 | # Allow subclasses to cheat os.path.abspath called in FileInfo class.
|
||
| 115 | self.os_path_abspath_orig = os.path.abspath
|
||
| 116 | |||
| 117 | def tearDown(self): |
||
| 118 | os.path.abspath = self.os_path_abspath_orig
|
||
| 119 | |||
| 120 | # Perform lint on single line of input and return the error message.
|
||
| 121 | def PerformSingleLineLint(self, code): |
||
| 122 | error_collector = ErrorCollector(self.assert_)
|
||
| 123 | lines = code.split('\n')
|
||
| 124 | cpplint.RemoveMultiLineComments('foo.h', lines, error_collector)
|
||
| 125 | clean_lines = cpplint.CleansedLines(lines) |
||
| 126 | include_state = cpplint._IncludeState() |
||
| 127 | function_state = cpplint._FunctionState() |
||
| 128 | nesting_state = cpplint.NestingState() |
||
| 129 | cpplint.ProcessLine('foo.cc', 'cc', clean_lines, 0, |
||
| 130 | include_state, function_state, |
||
| 131 | nesting_state, error_collector) |
||
| 132 | # Single-line lint tests are allowed to fail the 'unlintable function'
|
||
| 133 | # check.
|
||
| 134 | error_collector.RemoveIfPresent( |
||
| 135 | 'Lint failed to find start of function body.')
|
||
| 136 | return error_collector.Results()
|
||
| 137 | |||
| 138 | # Perform lint over multiple lines and return the error message.
|
||
| 139 | def PerformMultiLineLint(self, code): |
||
| 140 | error_collector = ErrorCollector(self.assert_)
|
||
| 141 | lines = code.split('\n')
|
||
| 142 | cpplint.RemoveMultiLineComments('foo.h', lines, error_collector)
|
||
| 143 | lines = cpplint.CleansedLines(lines) |
||
| 144 | nesting_state = cpplint.NestingState() |
||
| 145 | for i in xrange(lines.NumLines()): |
||
| 146 | nesting_state.Update('foo.h', lines, i, error_collector)
|
||
| 147 | cpplint.CheckStyle('foo.h', lines, i, 'h', nesting_state, |
||
| 148 | error_collector) |
||
| 149 | cpplint.CheckForNonStandardConstructs('foo.h', lines, i,
|
||
| 150 | nesting_state, error_collector) |
||
| 151 | nesting_state.CheckCompletedBlocks('foo.h', error_collector)
|
||
| 152 | return error_collector.Results()
|
||
| 153 | |||
| 154 | # Similar to PerformMultiLineLint, but calls CheckLanguage instead of
|
||
| 155 | # CheckForNonStandardConstructs
|
||
| 156 | def PerformLanguageRulesCheck(self, file_name, code): |
||
| 157 | error_collector = ErrorCollector(self.assert_)
|
||
| 158 | include_state = cpplint._IncludeState() |
||
| 159 | nesting_state = cpplint.NestingState() |
||
| 160 | lines = code.split('\n')
|
||
| 161 | cpplint.RemoveMultiLineComments(file_name, lines, error_collector) |
||
| 162 | lines = cpplint.CleansedLines(lines) |
||
| 163 | ext = file_name[file_name.rfind('.') + 1:] |
||
| 164 | for i in xrange(lines.NumLines()): |
||
| 165 | cpplint.CheckLanguage(file_name, lines, i, ext, include_state, |
||
| 166 | nesting_state, error_collector) |
||
| 167 | return error_collector.Results()
|
||
| 168 | |||
| 169 | def PerformFunctionLengthsCheck(self, code): |
||
| 170 | """Perform Lint function length check on block of code and return warnings.
|
||
| 171 |
|
||
| 172 | Builds up an array of lines corresponding to the code and strips comments
|
||
| 173 | using cpplint functions.
|
||
| 174 |
|
||
| 175 | Establishes an error collector and invokes the function length checking
|
||
| 176 | function following cpplint's pattern.
|
||
| 177 |
|
||
| 178 | Args:
|
||
| 179 | code: C++ source code expected to generate a warning message.
|
||
| 180 |
|
||
| 181 | Returns:
|
||
| 182 | The accumulated errors.
|
||
| 183 | """
|
||
| 184 | file_name = 'foo.cc'
|
||
| 185 | error_collector = ErrorCollector(self.assert_)
|
||
| 186 | function_state = cpplint._FunctionState() |
||
| 187 | lines = code.split('\n')
|
||
| 188 | cpplint.RemoveMultiLineComments(file_name, lines, error_collector) |
||
| 189 | lines = cpplint.CleansedLines(lines) |
||
| 190 | for i in xrange(lines.NumLines()): |
||
| 191 | cpplint.CheckForFunctionLengths(file_name, lines, i, |
||
| 192 | function_state, error_collector) |
||
| 193 | return error_collector.Results()
|
||
| 194 | |||
| 195 | def PerformIncludeWhatYouUse(self, code, filename='foo.h', io=codecs): |
||
| 196 | # First, build up the include state.
|
||
| 197 | error_collector = ErrorCollector(self.assert_)
|
||
| 198 | include_state = cpplint._IncludeState() |
||
| 199 | nesting_state = cpplint.NestingState() |
||
| 200 | lines = code.split('\n')
|
||
| 201 | cpplint.RemoveMultiLineComments(filename, lines, error_collector) |
||
| 202 | lines = cpplint.CleansedLines(lines) |
||
| 203 | for i in xrange(lines.NumLines()): |
||
| 204 | cpplint.CheckLanguage(filename, lines, i, '.h', include_state,
|
||
| 205 | nesting_state, error_collector) |
||
| 206 | # We could clear the error_collector here, but this should
|
||
| 207 | # also be fine, since our IncludeWhatYouUse unittests do not
|
||
| 208 | # have language problems.
|
||
| 209 | |||
| 210 | # Second, look for missing includes.
|
||
| 211 | cpplint.CheckForIncludeWhatYouUse(filename, lines, include_state, |
||
| 212 | error_collector, io) |
||
| 213 | return error_collector.Results()
|
||
| 214 | |||
| 215 | # Perform lint and compare the error message with "expected_message".
|
||
| 216 | def TestLint(self, code, expected_message): |
||
| 217 | self.assertEquals(expected_message, self.PerformSingleLineLint(code)) |
||
| 218 | |||
| 219 | def TestMultiLineLint(self, code, expected_message): |
||
| 220 | self.assertEquals(expected_message, self.PerformMultiLineLint(code)) |
||
| 221 | |||
| 222 | def TestMultiLineLintRE(self, code, expected_message_re): |
||
| 223 | message = self.PerformMultiLineLint(code)
|
||
| 224 | if not re.search(expected_message_re, message): |
||
| 225 | self.fail('Message was:\n' + message + 'Expected match to "' + |
||
| 226 | expected_message_re + '"')
|
||
| 227 | |||
| 228 | def TestLanguageRulesCheck(self, file_name, code, expected_message): |
||
| 229 | self.assertEquals(expected_message,
|
||
| 230 | self.PerformLanguageRulesCheck(file_name, code))
|
||
| 231 | |||
| 232 | def TestIncludeWhatYouUse(self, code, expected_message): |
||
| 233 | self.assertEquals(expected_message,
|
||
| 234 | self.PerformIncludeWhatYouUse(code))
|
||
| 235 | |||
| 236 | def TestBlankLinesCheck(self, lines, start_errors, end_errors): |
||
| 237 | error_collector = ErrorCollector(self.assert_)
|
||
| 238 | cpplint.ProcessFileData('foo.cc', 'cc', lines, error_collector) |
||
| 239 | self.assertEquals(
|
||
| 240 | start_errors, |
||
| 241 | error_collector.Results().count( |
||
| 242 | 'Redundant blank line at the start of a code block '
|
||
| 243 | 'should be deleted. [whitespace/blank_line] [2]'))
|
||
| 244 | self.assertEquals(
|
||
| 245 | end_errors, |
||
| 246 | error_collector.Results().count( |
||
| 247 | 'Redundant blank line at the end of a code block '
|
||
| 248 | 'should be deleted. [whitespace/blank_line] [3]'))
|
||
| 249 | |||
| 250 | |||
| 251 | class CpplintTest(CpplintTestBase): |
||
| 252 | |||
| 253 | def GetNamespaceResults(self, lines): |
||
| 254 | error_collector = ErrorCollector(self.assert_)
|
||
| 255 | cpplint.RemoveMultiLineComments('foo.h', lines, error_collector)
|
||
| 256 | lines = cpplint.CleansedLines(lines) |
||
| 257 | nesting_state = cpplint.NestingState() |
||
| 258 | for i in xrange(lines.NumLines()): |
||
| 259 | nesting_state.Update('foo.h', lines, i, error_collector)
|
||
| 260 | cpplint.CheckForNamespaceIndentation('foo.h', nesting_state,
|
||
| 261 | lines, i, error_collector) |
||
| 262 | |||
| 263 | return error_collector.Results()
|
||
| 264 | |||
| 265 | def testForwardDeclarationNameSpaceIndentation(self): |
||
| 266 | lines = ['namespace Test {',
|
||
| 267 | ' class ForwardDeclaration;',
|
||
| 268 | '} // namespace Test']
|
||
| 269 | |||
| 270 | results = self.GetNamespaceResults(lines)
|
||
| 271 | self.assertEquals(results, 'Do not indent within a namespace ' |
||
| 272 | ' [runtime/indentation_namespace] [4]')
|
||
| 273 | |||
| 274 | def testNameSpaceIndentationForClass(self): |
||
| 275 | lines = ['namespace Test {',
|
||
| 276 | 'void foo() { }',
|
||
| 277 | ' class Test {',
|
||
| 278 | ' };',
|
||
| 279 | '} // namespace Test']
|
||
| 280 | |||
| 281 | results = self.GetNamespaceResults(lines)
|
||
| 282 | self.assertEquals(results, 'Do not indent within a namespace ' |
||
| 283 | ' [runtime/indentation_namespace] [4]')
|
||
| 284 | |||
| 285 | def testNameSpaceIndentationNoError(self): |
||
| 286 | lines = ['namespace Test {',
|
||
| 287 | 'void foo() { }',
|
||
| 288 | '} // namespace Test']
|
||
| 289 | |||
| 290 | results = self.GetNamespaceResults(lines)
|
||
| 291 | self.assertEquals(results, '') |
||
| 292 | |||
| 293 | def testFalsePositivesNoError(self): |
||
| 294 | lines = ['namespace Test {',
|
||
| 295 | 'struct OuterClass {',
|
||
| 296 | ' struct NoFalsePositivesHere;',
|
||
| 297 | ' struct NoFalsePositivesHere member_variable;',
|
||
| 298 | '};',
|
||
| 299 | '} // namespace Test']
|
||
| 300 | |||
| 301 | results = self.GetNamespaceResults(lines)
|
||
| 302 | self.assertEquals(results, '') |
||
| 303 | |||
| 304 | |||
| 305 | # Test get line width.
|
||
| 306 | def testGetLineWidth(self): |
||
| 307 | self.assertEquals(0, cpplint.GetLineWidth('')) |
||
| 308 | self.assertEquals(10, cpplint.GetLineWidth(u'x' * 10)) |
||
| 309 | self.assertEquals(16, cpplint.GetLineWidth(u'都|道|府|県|支庁')) |
||
| 310 | |||
| 311 | def testGetTextInside(self): |
||
| 312 | self.assertEquals('', cpplint._GetTextInside('fun()', r'fun\(')) |
||
| 313 | self.assertEquals('x, y', cpplint._GetTextInside('f(x, y)', r'f\(')) |
||
| 314 | self.assertEquals('a(), b(c())', cpplint._GetTextInside( |
||
| 315 | 'printf(a(), b(c()))', r'printf\(')) |
||
| 316 | self.assertEquals('x, y{}', cpplint._GetTextInside('f[x, y{}]', r'f\[')) |
||
| 317 | self.assertEquals(None, cpplint._GetTextInside('f[a, b(}]', r'f\[')) |
||
| 318 | self.assertEquals(None, cpplint._GetTextInside('f[x, y]', r'f\(')) |
||
| 319 | self.assertEquals('y, h(z, (a + b))', cpplint._GetTextInside( |
||
| 320 | 'f(x, g(y, h(z, (a + b))))', r'g\(')) |
||
| 321 | self.assertEquals('f(f(x))', cpplint._GetTextInside('f(f(f(x)))', r'f\(')) |
||
| 322 | # Supports multiple lines.
|
||
| 323 | self.assertEquals('\n return loop(x);\n', |
||
| 324 | cpplint._GetTextInside( |
||
| 325 | 'int loop(int x) {\n return loop(x);\n}\n', r'\{')) |
||
| 326 | # '^' matches the beginning of each line.
|
||
| 327 | self.assertEquals('x, y', |
||
| 328 | cpplint._GetTextInside( |
||
| 329 | '#include "inl.h" // skip #define\n'
|
||
| 330 | '#define A2(x, y) a_inl_(x, y, __LINE__)\n'
|
||
| 331 | '#define A(x) a_inl_(x, "", __LINE__)\n',
|
||
| 332 | r'^\s*#define\s*\w+\('))
|
||
| 333 | |||
| 334 | def testFindNextMultiLineCommentStart(self): |
||
| 335 | self.assertEquals(1, cpplint.FindNextMultiLineCommentStart([''], 0)) |
||
| 336 | |||
| 337 | lines = ['a', 'b', '/* c'] |
||
| 338 | self.assertEquals(2, cpplint.FindNextMultiLineCommentStart(lines, 0)) |
||
| 339 | |||
| 340 | lines = ['char a[] = "/*";'] # not recognized as comment. |
||
| 341 | self.assertEquals(1, cpplint.FindNextMultiLineCommentStart(lines, 0)) |
||
| 342 | |||
| 343 | def testFindNextMultiLineCommentEnd(self): |
||
| 344 | self.assertEquals(1, cpplint.FindNextMultiLineCommentEnd([''], 0)) |
||
| 345 | lines = ['a', 'b', ' c */'] |
||
| 346 | self.assertEquals(2, cpplint.FindNextMultiLineCommentEnd(lines, 0)) |
||
| 347 | |||
| 348 | def testRemoveMultiLineCommentsFromRange(self): |
||
| 349 | lines = ['a', ' /* comment ', ' * still comment', ' comment */ ', 'b'] |
||
| 350 | cpplint.RemoveMultiLineCommentsFromRange(lines, 1, 4) |
||
| 351 | self.assertEquals(['a', '/**/', '/**/', '/**/', 'b'], lines) |
||
| 352 | |||
| 353 | def testSpacesAtEndOfLine(self): |
||
| 354 | self.TestLint(
|
||
| 355 | '// Hello there ',
|
||
| 356 | 'Line ends in whitespace. Consider deleting these extra spaces.'
|
||
| 357 | ' [whitespace/end_of_line] [4]')
|
||
| 358 | |||
| 359 | # Test line length check.
|
||
| 360 | def testLineLengthCheck(self): |
||
| 361 | self.TestLint(
|
||
| 362 | '// Hello',
|
||
| 363 | '')
|
||
| 364 | self.TestLint(
|
||
| 365 | '// ' + 'x' * 80, |
||
| 366 | 'Lines should be <= 80 characters long'
|
||
| 367 | ' [whitespace/line_length] [2]')
|
||
| 368 | self.TestLint(
|
||
| 369 | '// ' + 'x' * 100, |
||
| 370 | 'Lines should very rarely be longer than 100 characters'
|
||
| 371 | ' [whitespace/line_length] [4]')
|
||
| 372 | self.TestLint(
|
||
| 373 | '// http://g' + ('o' * 100) + 'gle.com/', |
||
| 374 | '')
|
||
| 375 | self.TestLint(
|
||
| 376 | '// https://g' + ('o' * 100) + 'gle.com/', |
||
| 377 | '')
|
||
| 378 | self.TestLint(
|
||
| 379 | '// https://g' + ('o' * 60) + 'gle.com/ and some comments', |
||
| 380 | 'Lines should be <= 80 characters long'
|
||
| 381 | ' [whitespace/line_length] [2]')
|
||
| 382 | self.TestLint(
|
||
| 383 | '// Read https://g' + ('o' * 60) + 'gle.com/', |
||
| 384 | '')
|
||
| 385 | self.TestLint(
|
||
| 386 | '// $Id: g' + ('o' * 80) + 'gle.cc#1 $', |
||
| 387 | '')
|
||
| 388 | self.TestLint(
|
||
| 389 | '// $Id: g' + ('o' * 80) + 'gle.cc#1', |
||
| 390 | 'Lines should be <= 80 characters long'
|
||
| 391 | ' [whitespace/line_length] [2]')
|
||
| 392 | self.TestMultiLineLint(
|
||
| 393 | 'static const char kCStr[] = "g' + ('o' * 50) + 'gle";\n', |
||
| 394 | 'Lines should be <= 80 characters long'
|
||
| 395 | ' [whitespace/line_length] [2]')
|
||
| 396 | self.TestMultiLineLint(
|
||
| 397 | 'static const char kRawStr[] = R"(g' + ('o' * 50) + 'gle)";\n', |
||
| 398 | '') # no warning because raw string content is elided |
||
| 399 | self.TestMultiLineLint(
|
||
| 400 | 'static const char kMultiLineRawStr[] = R"(\n'
|
||
| 401 | 'g' + ('o' * 80) + 'gle\n' |
||
| 402 | ')";',
|
||
| 403 | '')
|
||
| 404 | self.TestMultiLineLint(
|
||
| 405 | 'static const char kL' + ('o' * 50) + 'ngIdentifier[] = R"()";\n', |
||
| 406 | 'Lines should be <= 80 characters long'
|
||
| 407 | ' [whitespace/line_length] [2]')
|
||
| 408 | |||
| 409 | # Test error suppression annotations.
|
||
| 410 | def testErrorSuppression(self): |
||
| 411 | # Two errors on same line:
|
||
| 412 | self.TestLint(
|
||
| 413 | 'long a = (int64) 65;',
|
||
| 414 | ['Using C-style cast. Use static_cast<int64>(...) instead'
|
||
| 415 | ' [readability/casting] [4]',
|
||
| 416 | 'Use int16/int64/etc, rather than the C type long'
|
||
| 417 | ' [runtime/int] [4]',
|
||
| 418 | ]) |
||
| 419 | # One category of error suppressed:
|
||
| 420 | self.TestLint(
|
||
| 421 | 'long a = (int64) 65; // NOLINT(runtime/int)',
|
||
| 422 | 'Using C-style cast. Use static_cast<int64>(...) instead'
|
||
| 423 | ' [readability/casting] [4]')
|
||
| 424 | # All categories suppressed: (two aliases)
|
||
| 425 | self.TestLint('long a = (int64) 65; // NOLINT', '') |
||
| 426 | self.TestLint('long a = (int64) 65; // NOLINT(*)', '') |
||
| 427 | # Malformed NOLINT directive:
|
||
| 428 | self.TestLint(
|
||
| 429 | 'long a = 65; // NOLINT(foo)',
|
||
| 430 | ['Unknown NOLINT error category: foo'
|
||
| 431 | ' [readability/nolint] [5]',
|
||
| 432 | 'Use int16/int64/etc, rather than the C type long [runtime/int] [4]',
|
||
| 433 | ]) |
||
| 434 | # Irrelevant NOLINT directive has no effect:
|
||
| 435 | self.TestLint(
|
||
| 436 | 'long a = 65; // NOLINT(readability/casting)',
|
||
| 437 | 'Use int16/int64/etc, rather than the C type long'
|
||
| 438 | ' [runtime/int] [4]')
|
||
| 439 | # NOLINTNEXTLINE silences warning for the next line instead of current line
|
||
| 440 | error_collector = ErrorCollector(self.assert_)
|
||
| 441 | cpplint.ProcessFileData('test.cc', 'cc', |
||
| 442 | ['// Copyright 2014 Your Company.',
|
||
| 443 | '// NOLINTNEXTLINE(whitespace/line_length)',
|
||
| 444 | '// ./command' + (' -verbose' * 80), |
||
| 445 | ''],
|
||
| 446 | error_collector) |
||
| 447 | self.assertEquals('', error_collector.Results()) |
||
| 448 | |||
| 449 | # Test Variable Declarations.
|
||
| 450 | def testVariableDeclarations(self): |
||
| 451 | self.TestLint(
|
||
| 452 | 'long a = 65;',
|
||
| 453 | 'Use int16/int64/etc, rather than the C type long'
|
||
| 454 | ' [runtime/int] [4]')
|
||
| 455 | self.TestLint(
|
||
| 456 | 'long double b = 65.0;',
|
||
| 457 | '')
|
||
| 458 | self.TestLint(
|
||
| 459 | 'long long aa = 6565;',
|
||
| 460 | 'Use int16/int64/etc, rather than the C type long'
|
||
| 461 | ' [runtime/int] [4]')
|
||
| 462 | |||
| 463 | # Test C-style cast cases.
|
||
| 464 | def testCStyleCast(self): |
||
| 465 | self.TestLint(
|
||
| 466 | 'int a = (int)1.0;',
|
||
| 467 | 'Using C-style cast. Use static_cast<int>(...) instead'
|
||
| 468 | ' [readability/casting] [4]')
|
||
| 469 | self.TestLint(
|
||
| 470 | 'int a = (int)-1.0;',
|
||
| 471 | 'Using C-style cast. Use static_cast<int>(...) instead'
|
||
| 472 | ' [readability/casting] [4]')
|
||
| 473 | self.TestLint(
|
||
| 474 | 'int *a = (int *)NULL;',
|
||
| 475 | 'Using C-style cast. Use reinterpret_cast<int *>(...) instead'
|
||
| 476 | ' [readability/casting] [4]')
|
||
| 477 | |||
| 478 | self.TestLint(
|
||
| 479 | 'uint16 a = (uint16)1.0;',
|
||
| 480 | 'Using C-style cast. Use static_cast<uint16>(...) instead'
|
||
| 481 | ' [readability/casting] [4]')
|
||
| 482 | self.TestLint(
|
||
| 483 | 'int32 a = (int32)1.0;',
|
||
| 484 | 'Using C-style cast. Use static_cast<int32>(...) instead'
|
||
| 485 | ' [readability/casting] [4]')
|
||
| 486 | self.TestLint(
|
||
| 487 | 'uint64 a = (uint64)1.0;',
|
||
| 488 | 'Using C-style cast. Use static_cast<uint64>(...) instead'
|
||
| 489 | ' [readability/casting] [4]')
|
||
| 490 | |||
| 491 | # These shouldn't be recognized casts.
|
||
| 492 | self.TestLint('u a = (u)NULL;', '') |
||
| 493 | self.TestLint('uint a = (uint)NULL;', '') |
||
| 494 | self.TestLint('typedef MockCallback<int(int)> CallbackType;', '') |
||
| 495 | self.TestLint('scoped_ptr< MockCallback<int(int)> > callback_value;', '') |
||
| 496 | self.TestLint('std::function<int(bool)>', '') |
||
| 497 | self.TestLint('x = sizeof(int)', '') |
||
| 498 | self.TestLint('x = alignof(int)', '') |
||
| 499 | self.TestLint('alignas(int) char x[42]', '') |
||
| 500 | self.TestLint('alignas(alignof(x)) char y[42]', '') |
||
| 501 | self.TestLint('void F(int (func)(int));', '') |
||
| 502 | self.TestLint('void F(int (func)(int*));', '') |
||
| 503 | self.TestLint('void F(int (Class::member)(int));', '') |
||
| 504 | self.TestLint('void F(int (Class::member)(int*));', '') |
||
| 505 | self.TestLint('void F(int (Class::member)(int), int param);', '') |
||
| 506 | self.TestLint('void F(int (Class::member)(int*), int param);', '') |
||
| 507 | |||
| 508 | # These should not be recognized (lambda functions without arg names).
|
||
| 509 | self.TestLint('[](int/*unused*/) -> bool {', '') |
||
| 510 | self.TestLint('[](int /*unused*/) -> bool {', '') |
||
| 511 | self.TestLint('auto f = [](MyStruct* /*unused*/)->int {', '') |
||
| 512 | self.TestLint(
|
||
| 513 | '[](int) -> bool {',
|
||
| 514 | 'All parameters should be named in a function'
|
||
| 515 | ' [readability/function] [3]')
|
||
| 516 | self.TestLint(
|
||
| 517 | 'auto f = [](MyStruct*)->int {',
|
||
| 518 | 'All parameters should be named in a function'
|
||
| 519 | ' [readability/function] [3]')
|
||
| 520 | |||
| 521 | # Test taking address of casts (runtime/casting)
|
||
| 522 | def testRuntimeCasting(self): |
||
| 523 | error_msg = ('Are you taking an address of a cast? '
|
||
| 524 | 'This is dangerous: could be a temp var. '
|
||
| 525 | 'Take the address before doing the cast, rather than after'
|
||
| 526 | ' [runtime/casting] [4]')
|
||
| 527 | self.TestLint('int* x = &static_cast<int*>(foo);', error_msg) |
||
| 528 | self.TestLint('int* x = &reinterpret_cast<int *>(foo);', error_msg) |
||
| 529 | self.TestLint('int* x = &(int*)foo;', |
||
| 530 | ['Using C-style cast. Use reinterpret_cast<int*>(...) '
|
||
| 531 | 'instead [readability/casting] [4]',
|
||
| 532 | error_msg]) |
||
| 533 | self.TestLint('BudgetBuckets&(BudgetWinHistory::*BucketFn)(void) const;', |
||
| 534 | '')
|
||
| 535 | self.TestLint('&(*func_ptr)(arg)', '') |
||
| 536 | self.TestLint('Compute(arg, &(*func_ptr)(i, j));', '') |
||
| 537 | |||
| 538 | # Alternative error message
|
||
| 539 | alt_error_msg = ('Are you taking an address of something dereferenced '
|
||
| 540 | 'from a cast? Wrapping the dereferenced expression in '
|
||
| 541 | 'parentheses will make the binding more obvious'
|
||
| 542 | ' [readability/casting] [4]')
|
||
| 543 | self.TestLint('int* x = &down_cast<Obj*>(obj)->member_;', alt_error_msg) |
||
| 544 | self.TestLint('int* x = &down_cast<Obj*>(obj)[index];', alt_error_msg) |
||
| 545 | self.TestLint('int* x = &(down_cast<Obj*>(obj)->member_);', '') |
||
| 546 | self.TestLint('int* x = &(down_cast<Obj*>(obj)[index]);', '') |
||
| 547 | self.TestLint('int* x = &down_cast<Obj*>(obj)\n->member_;', alt_error_msg) |
||
| 548 | self.TestLint('int* x = &(down_cast<Obj*>(obj)\n->member_);', '') |
||
| 549 | |||
| 550 | # It's OK to cast an address.
|
||
| 551 | self.TestLint('int* x = reinterpret_cast<int *>(&foo);', '') |
||
| 552 | |||
| 553 | # Function pointers returning references should not be confused
|
||
| 554 | # with taking address of old-style casts.
|
||
| 555 | self.TestLint('auto x = implicit_cast<string &(*)(int)>(&foo);', '') |
||
| 556 | |||
| 557 | def testRuntimeSelfinit(self): |
||
| 558 | self.TestLint(
|
||
| 559 | 'Foo::Foo(Bar r, Bel l) : r_(r_), l_(l_) { }',
|
||
| 560 | 'You seem to be initializing a member variable with itself.'
|
||
| 561 | ' [runtime/init] [4]')
|
||
| 562 | self.TestLint(
|
||
| 563 | 'Foo::Foo(Bar r, Bel l) : r_(r), l_(l) { }',
|
||
| 564 | '')
|
||
| 565 | self.TestLint(
|
||
| 566 | 'Foo::Foo(Bar r) : r_(r), l_(r_), ll_(l_) { }',
|
||
| 567 | '')
|
||
| 568 | |||
| 569 | # Test for unnamed arguments in a method.
|
||
| 570 | def testCheckForUnnamedParams(self): |
||
| 571 | message = ('All parameters should be named in a function'
|
||
| 572 | ' [readability/function] [3]')
|
||
| 573 | self.TestLint('virtual void Func(int*) const;', message) |
||
| 574 | self.TestLint('virtual void Func(int*);', message) |
||
| 575 | self.TestLint('void Method(char*) {', message) |
||
| 576 | self.TestLint('void Method(char*);', message) |
||
| 577 | self.TestLint('static void operator delete[](void*) throw();', message) |
||
| 578 | self.TestLint('int Method(int);', message) |
||
| 579 | |||
| 580 | self.TestLint('virtual void Func(int* p);', '') |
||
| 581 | self.TestLint('void operator delete(void* x) throw();', '') |
||
| 582 | self.TestLint('void Method(char* x) {', '') |
||
| 583 | self.TestLint('void Method(char* /*x*/) {', '') |
||
| 584 | self.TestLint('void Method(char* x);', '') |
||
| 585 | self.TestLint('typedef void (*Method)(int32 x);', '') |
||
| 586 | self.TestLint('static void operator delete[](void* x) throw();', '') |
||
| 587 | self.TestLint('static void operator delete[](void* /*x*/) throw();', '') |
||
| 588 | |||
| 589 | self.TestLint('X operator++(int);', '') |
||
| 590 | self.TestLint('X operator++(int) {', '') |
||
| 591 | self.TestLint('X operator--(int);', '') |
||
| 592 | self.TestLint('X operator--(int /*unused*/) {', '') |
||
| 593 | self.TestLint('MACRO(int);', '') |
||
| 594 | self.TestLint('MACRO(func(int));', '') |
||
| 595 | self.TestLint('MACRO(arg, func(int));', '') |
||
| 596 | |||
| 597 | self.TestLint('void (*func)(void*);', '') |
||
| 598 | self.TestLint('void Func((*func)(void*)) {}', '') |
||
| 599 | self.TestLint('template <void Func(void*)> void func();', '') |
||
| 600 | self.TestLint('virtual void f(int /*unused*/) {', '') |
||
| 601 | self.TestLint('void f(int /*unused*/) override {', '') |
||
| 602 | self.TestLint('void f(int /*unused*/) final {', '') |
||
| 603 | |||
| 604 | # Test deprecated casts such as int(d)
|
||
| 605 | def testDeprecatedCast(self): |
||
| 606 | self.TestLint(
|
||
| 607 | 'int a = int(2.2);',
|
||
| 608 | 'Using deprecated casting style. '
|
||
| 609 | 'Use static_cast<int>(...) instead'
|
||
| 610 | ' [readability/casting] [4]')
|
||
| 611 | |||
| 612 | self.TestLint(
|
||
| 613 | '(char *) "foo"',
|
||
| 614 | 'Using C-style cast. '
|
||
| 615 | 'Use const_cast<char *>(...) instead'
|
||
| 616 | ' [readability/casting] [4]')
|
||
| 617 | |||
| 618 | self.TestLint(
|
||
| 619 | '(int*)foo',
|
||
| 620 | 'Using C-style cast. '
|
||
| 621 | 'Use reinterpret_cast<int*>(...) instead'
|
||
| 622 | ' [readability/casting] [4]')
|
||
| 623 | |||
| 624 | # Checks for false positives...
|
||
| 625 | self.TestLint('int a = int();', '') # constructor |
||
| 626 | self.TestLint('X::X() : a(int()) {}', '') # default constructor |
||
| 627 | self.TestLint('operator bool();', '') # Conversion operator |
||
| 628 | self.TestLint('new int64(123);', '') # "new" operator on basic type |
||
| 629 | self.TestLint('new int64(123);', '') # "new" operator on basic type |
||
| 630 | self.TestLint('using a = bool(int arg);', '') # C++11 alias-declaration |
||
| 631 | self.TestLint('x = bit_cast<double(*)[3]>(y);', '') # array of array |
||
| 632 | self.TestLint('void F(const char(&src)[N]);', '') # array of references |
||
| 633 | |||
| 634 | # Placement new
|
||
| 635 | self.TestLint(
|
||
| 636 | 'new(field_ptr) int(field->default_value_enum()->number());',
|
||
| 637 | '')
|
||
| 638 | |||
| 639 | # C++11 function wrappers
|
||
| 640 | self.TestLint('std::function<int(bool)>', '') |
||
| 641 | self.TestLint('std::function<const int(bool)>', '') |
||
| 642 | self.TestLint('std::function< int(bool) >', '') |
||
| 643 | self.TestLint('mfunction<int(bool)>', '') |
||
| 644 | |||
| 645 | error_collector = ErrorCollector(self.assert_)
|
||
| 646 | cpplint.ProcessFileData( |
||
| 647 | 'test.cc', 'cc', |
||
| 648 | ['// Copyright 2014 Your Company. All Rights Reserved.',
|
||
| 649 | 'typedef std::function<',
|
||
| 650 | ' bool(int)> F;',
|
||
| 651 | ''],
|
||
| 652 | error_collector) |
||
| 653 | self.assertEquals('', error_collector.Results()) |
||
| 654 | |||
| 655 | # Return types for function pointers
|
||
| 656 | self.TestLint('typedef bool(FunctionPointer)();', '') |
||
| 657 | self.TestLint('typedef bool(FunctionPointer)(int param);', '') |
||
| 658 | self.TestLint('typedef bool(MyClass::*MemberFunctionPointer)();', '') |
||
| 659 | self.TestLint('typedef bool(MyClass::* MemberFunctionPointer)();', '') |
||
| 660 | self.TestLint('typedef bool(MyClass::*MemberFunctionPointer)() const;', '') |
||
| 661 | self.TestLint('void Function(bool(FunctionPointerArg)());', '') |
||
| 662 | self.TestLint('void Function(bool(FunctionPointerArg)()) {}', '') |
||
| 663 | self.TestLint('typedef set<int64, bool(*)(int64, int64)> SortedIdSet', '') |
||
| 664 | self.TestLint(
|
||
| 665 | 'bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *t)) {}',
|
||
| 666 | '')
|
||
| 667 | |||
| 668 | # The second parameter to a gMock method definition is a function signature
|
||
| 669 | # that often looks like a bad cast but should not picked up by lint.
|
||
| 670 | def testMockMethod(self): |
||
| 671 | self.TestLint(
|
||
| 672 | 'MOCK_METHOD0(method, int());',
|
||
| 673 | '')
|
||
| 674 | self.TestLint(
|
||
| 675 | 'MOCK_CONST_METHOD1(method, float(string));',
|
||
| 676 | '')
|
||
| 677 | self.TestLint(
|
||
| 678 | 'MOCK_CONST_METHOD2_T(method, double(float, float));',
|
||
| 679 | '')
|
||
| 680 | self.TestLint(
|
||
| 681 | 'MOCK_CONST_METHOD1(method, SomeType(int));',
|
||
| 682 | '')
|
||
| 683 | |||
| 684 | error_collector = ErrorCollector(self.assert_)
|
||
| 685 | cpplint.ProcessFileData('mock.cc', 'cc', |
||
| 686 | ['MOCK_METHOD1(method1,',
|
||
| 687 | ' bool(int));',
|
||
| 688 | 'MOCK_METHOD1(',
|
||
| 689 | ' method2,',
|
||
| 690 | ' bool(int));',
|
||
| 691 | 'MOCK_CONST_METHOD2(',
|
||
| 692 | ' method3, bool(int,',
|
||
| 693 | ' int));',
|
||
| 694 | 'MOCK_METHOD1(method4, int(bool));',
|
||
| 695 | 'const int kConstant = int(42);'], # true positive |
||
| 696 | error_collector) |
||
| 697 | self.assertEquals(
|
||
| 698 | 0,
|
||
| 699 | error_collector.Results().count( |
||
| 700 | ('Using deprecated casting style. '
|
||
| 701 | 'Use static_cast<bool>(...) instead '
|
||
| 702 | '[readability/casting] [4]')))
|
||
| 703 | self.assertEquals(
|
||
| 704 | 1,
|
||
| 705 | error_collector.Results().count( |
||
| 706 | ('Using deprecated casting style. '
|
||
| 707 | 'Use static_cast<int>(...) instead '
|
||
| 708 | '[readability/casting] [4]')))
|
||
| 709 | |||
| 710 | # Like gMock method definitions, MockCallback instantiations look very similar
|
||
| 711 | # to bad casts.
|
||
| 712 | def testMockCallback(self): |
||
| 713 | self.TestLint(
|
||
| 714 | 'MockCallback<bool(int)>',
|
||
| 715 | '')
|
||
| 716 | self.TestLint(
|
||
| 717 | 'MockCallback<int(float, char)>',
|
||
| 718 | '')
|
||
| 719 | |||
| 720 | # Test false errors that happened with some include file names
|
||
| 721 | def testIncludeFilenameFalseError(self): |
||
| 722 | self.TestLint(
|
||
| 723 | '#include "foo/long-foo.h"',
|
||
| 724 | '')
|
||
| 725 | self.TestLint(
|
||
| 726 | '#include "foo/sprintf.h"',
|
||
| 727 | '')
|
||
| 728 | |||
| 729 | # Test typedef cases. There was a bug that cpplint misidentified
|
||
| 730 | # typedef for pointer to function as C-style cast and produced
|
||
| 731 | # false-positive error messages.
|
||
| 732 | def testTypedefForPointerToFunction(self): |
||
| 733 | self.TestLint(
|
||
| 734 | 'typedef void (*Func)(int x);',
|
||
| 735 | '')
|
||
| 736 | self.TestLint(
|
||
| 737 | 'typedef void (*Func)(int *x);',
|
||
| 738 | '')
|
||
| 739 | self.TestLint(
|
||
| 740 | 'typedef void Func(int x);',
|
||
| 741 | '')
|
||
| 742 | self.TestLint(
|
||
| 743 | 'typedef void Func(int *x);',
|
||
| 744 | '')
|
||
| 745 | |||
| 746 | def testIncludeWhatYouUseNoImplementationFiles(self): |
||
| 747 | code = 'std::vector<int> foo;'
|
||
| 748 | self.assertEquals('Add #include <vector> for vector<>' |
||
| 749 | ' [build/include_what_you_use] [4]',
|
||
| 750 | self.PerformIncludeWhatYouUse(code, 'foo.h')) |
||
| 751 | self.assertEquals('', |
||
| 752 | self.PerformIncludeWhatYouUse(code, 'foo.cc')) |
||
| 753 | |||
| 754 | def testIncludeWhatYouUse(self): |
||
| 755 | self.TestIncludeWhatYouUse(
|
||
| 756 | """#include <vector>
|
||
| 757 | std::vector<int> foo;
|
||
| 758 | """,
|
||
| 759 | '')
|
||
| 760 | self.TestIncludeWhatYouUse(
|
||
| 761 | """#include <map>
|
||
| 762 | std::pair<int,int> foo;
|
||
| 763 | """,
|
||
| 764 | 'Add #include <utility> for pair<>'
|
||
| 765 | ' [build/include_what_you_use] [4]')
|
||
| 766 | self.TestIncludeWhatYouUse(
|
||
| 767 | """#include <multimap>
|
||
| 768 | std::pair<int,int> foo;
|
||
| 769 | """,
|
||
| 770 | 'Add #include <utility> for pair<>'
|
||
| 771 | ' [build/include_what_you_use] [4]')
|
||
| 772 | self.TestIncludeWhatYouUse(
|
||
| 773 | """#include <hash_map>
|
||
| 774 | std::pair<int,int> foo;
|
||
| 775 | """,
|
||
| 776 | 'Add #include <utility> for pair<>'
|
||
| 777 | ' [build/include_what_you_use] [4]')
|
||
| 778 | self.TestIncludeWhatYouUse(
|
||
| 779 | """#include <utility>
|
||
| 780 | std::pair<int,int> foo;
|
||
| 781 | """,
|
||
| 782 | '')
|
||
| 783 | self.TestIncludeWhatYouUse(
|
||
| 784 | """#include <vector>
|
||
| 785 | DECLARE_string(foobar);
|
||
| 786 | """,
|
||
| 787 | '')
|
||
| 788 | self.TestIncludeWhatYouUse(
|
||
| 789 | """#include <vector>
|
||
| 790 | DEFINE_string(foobar, "", "");
|
||
| 791 | """,
|
||
| 792 | '')
|
||
| 793 | self.TestIncludeWhatYouUse(
|
||
| 794 | """#include <vector>
|
||
| 795 | std::pair<int,int> foo;
|
||
| 796 | """,
|
||
| 797 | 'Add #include <utility> for pair<>'
|
||
| 798 | ' [build/include_what_you_use] [4]')
|
||
| 799 | self.TestIncludeWhatYouUse(
|
||
| 800 | """#include "base/foobar.h"
|
||
| 801 | std::vector<int> foo;
|
||
| 802 | """,
|
||
| 803 | 'Add #include <vector> for vector<>'
|
||
| 804 | ' [build/include_what_you_use] [4]')
|
||
| 805 | self.TestIncludeWhatYouUse(
|
||
| 806 | """#include <vector>
|
||
| 807 | std::set<int> foo;
|
||
| 808 | """,
|
||
| 809 | 'Add #include <set> for set<>'
|
||
| 810 | ' [build/include_what_you_use] [4]')
|
||
| 811 | self.TestIncludeWhatYouUse(
|
||
| 812 | """#include "base/foobar.h"
|
||
| 813 | hash_map<int, int> foobar;
|
||
| 814 | """,
|
||
| 815 | 'Add #include <hash_map> for hash_map<>'
|
||
| 816 | ' [build/include_what_you_use] [4]')
|
||
| 817 | self.TestIncludeWhatYouUse(
|
||
| 818 | """#include "base/foobar.h"
|
||
| 819 | bool foobar = std::less<int>(0,1);
|
||
| 820 | """,
|
||
| 821 | 'Add #include <functional> for less<>'
|
||
| 822 | ' [build/include_what_you_use] [4]')
|
||
| 823 | self.TestIncludeWhatYouUse(
|
||
| 824 | """#include "base/foobar.h"
|
||
| 825 | bool foobar = min<int>(0,1);
|
||
| 826 | """,
|
||
| 827 | 'Add #include <algorithm> for min [build/include_what_you_use] [4]')
|
||
| 828 | self.TestIncludeWhatYouUse(
|
||
| 829 | 'void a(const string &foobar);',
|
||
| 830 | 'Add #include <string> for string [build/include_what_you_use] [4]')
|
||
| 831 | self.TestIncludeWhatYouUse(
|
||
| 832 | 'void a(const std::string &foobar);',
|
||
| 833 | 'Add #include <string> for string [build/include_what_you_use] [4]')
|
||
| 834 | self.TestIncludeWhatYouUse(
|
||
| 835 | 'void a(const my::string &foobar);',
|
||
| 836 | '') # Avoid false positives on strings in other namespaces. |
||
| 837 | self.TestIncludeWhatYouUse(
|
||
| 838 | """#include "base/foobar.h"
|
||
| 839 | bool foobar = swap(0,1);
|
||
| 840 | """,
|
||
| 841 | 'Add #include <algorithm> for swap [build/include_what_you_use] [4]')
|
||
| 842 | self.TestIncludeWhatYouUse(
|
||
| 843 | """#include "base/foobar.h"
|
||
| 844 | bool foobar = transform(a.begin(), a.end(), b.start(), Foo);
|
||
| 845 | """,
|
||
| 846 | 'Add #include <algorithm> for transform '
|
||
| 847 | '[build/include_what_you_use] [4]')
|
||
| 848 | self.TestIncludeWhatYouUse(
|
||
| 849 | """#include "base/foobar.h"
|
||
| 850 | bool foobar = min_element(a.begin(), a.end());
|
||
| 851 | """,
|
||
| 852 | 'Add #include <algorithm> for min_element '
|
||
| 853 | '[build/include_what_you_use] [4]')
|
||
| 854 | self.TestIncludeWhatYouUse(
|
||
| 855 | """foo->swap(0,1);
|
||
| 856 | foo.swap(0,1);
|
||
| 857 | """,
|
||
| 858 | '')
|
||
| 859 | self.TestIncludeWhatYouUse(
|
||
| 860 | """#include <string>
|
||
| 861 | void a(const std::multimap<int,string> &foobar);
|
||
| 862 | """,
|
||
| 863 | 'Add #include <map> for multimap<>'
|
||
| 864 | ' [build/include_what_you_use] [4]')
|
||
| 865 | self.TestIncludeWhatYouUse(
|
||
| 866 | """#include <queue>
|
||
| 867 | void a(const std::priority_queue<int> &foobar);
|
||
| 868 | """,
|
||
| 869 | '')
|
||
| 870 | self.TestIncludeWhatYouUse(
|
||
| 871 | """#include <assert.h>
|
||
| 872 | #include <string>
|
||
| 873 | #include <vector>
|
||
| 874 | #include "base/basictypes.h"
|
||
| 875 | #include "base/port.h"
|
||
| 876 | vector<string> hajoa;""", '') |
||
| 877 | self.TestIncludeWhatYouUse(
|
||
| 878 | """#include <string>
|
||
| 879 | int i = numeric_limits<int>::max()
|
||
| 880 | """,
|
||
| 881 | 'Add #include <limits> for numeric_limits<>'
|
||
| 882 | ' [build/include_what_you_use] [4]')
|
||
| 883 | self.TestIncludeWhatYouUse(
|
||
| 884 | """#include <limits>
|
||
| 885 | int i = numeric_limits<int>::max()
|
||
| 886 | """,
|
||
| 887 | '')
|
||
| 888 | |||
| 889 | # Test the UpdateIncludeState code path.
|
||
| 890 | mock_header_contents = ['#include "blah/foo.h"', '#include "blah/bar.h"'] |
||
| 891 | message = self.PerformIncludeWhatYouUse(
|
||
| 892 | '#include "blah/a.h"',
|
||
| 893 | filename='blah/a.cc',
|
||
| 894 | io=MockIo(mock_header_contents)) |
||
| 895 | self.assertEquals(message, '') |
||
| 896 | |||
| 897 | mock_header_contents = ['#include <set>']
|
||
| 898 | message = self.PerformIncludeWhatYouUse(
|
||
| 899 | """#include "blah/a.h"
|
||
| 900 | std::set<int> foo;""",
|
||
| 901 | filename='blah/a.cc',
|
||
| 902 | io=MockIo(mock_header_contents)) |
||
| 903 | self.assertEquals(message, '') |
||
| 904 | |||
| 905 | # Make sure we can find the correct header file if the cc file seems to be
|
||
| 906 | # a temporary file generated by Emacs's flymake.
|
||
| 907 | mock_header_contents = ['']
|
||
| 908 | message = self.PerformIncludeWhatYouUse(
|
||
| 909 | """#include "blah/a.h"
|
||
| 910 | std::set<int> foo;""",
|
||
| 911 | filename='blah/a_flymake.cc',
|
||
| 912 | io=MockIo(mock_header_contents)) |
||
| 913 | self.assertEquals(message, 'Add #include <set> for set<> ' |
||
| 914 | '[build/include_what_you_use] [4]')
|
||
| 915 | |||
| 916 | # If there's just a cc and the header can't be found then it's ok.
|
||
| 917 | message = self.PerformIncludeWhatYouUse(
|
||
| 918 | """#include "blah/a.h"
|
||
| 919 | std::set<int> foo;""",
|
||
| 920 | filename='blah/a.cc')
|
||
| 921 | self.assertEquals(message, '') |
||
| 922 | |||
| 923 | # Make sure we find the headers with relative paths.
|
||
| 924 | mock_header_contents = ['']
|
||
| 925 | message = self.PerformIncludeWhatYouUse(
|
||
| 926 | """#include "%s/a.h"
|
||
| 927 | std::set<int> foo;""" % os.path.basename(os.getcwd()),
|
||
| 928 | filename='a.cc',
|
||
| 929 | io=MockIo(mock_header_contents)) |
||
| 930 | self.assertEquals(message, 'Add #include <set> for set<> ' |
||
| 931 | '[build/include_what_you_use] [4]')
|
||
| 932 | |||
| 933 | def testFilesBelongToSameModule(self): |
||
| 934 | f = cpplint.FilesBelongToSameModule |
||
| 935 | self.assertEquals((True, ''), f('a.cc', 'a.h')) |
||
| 936 | self.assertEquals((True, ''), f('base/google.cc', 'base/google.h')) |
||
| 937 | self.assertEquals((True, ''), f('base/google_test.cc', 'base/google.h')) |
||
| 938 | self.assertEquals((True, ''), |
||
| 939 | f('base/google_unittest.cc', 'base/google.h')) |
||
| 940 | self.assertEquals((True, ''), |
||
| 941 | f('base/internal/google_unittest.cc',
|
||
| 942 | 'base/public/google.h'))
|
||
| 943 | self.assertEquals((True, 'xxx/yyy/'), |
||
| 944 | f('xxx/yyy/base/internal/google_unittest.cc',
|
||
| 945 | 'base/public/google.h'))
|
||
| 946 | self.assertEquals((True, 'xxx/yyy/'), |
||
| 947 | f('xxx/yyy/base/google_unittest.cc',
|
||
| 948 | 'base/public/google.h'))
|
||
| 949 | self.assertEquals((True, ''), |
||
| 950 | f('base/google_unittest.cc', 'base/google-inl.h')) |
||
| 951 | self.assertEquals((True, '/home/build/google3/'), |
||
| 952 | f('/home/build/google3/base/google.cc', 'base/google.h')) |
||
| 953 | |||
| 954 | self.assertEquals((False, ''), |
||
| 955 | f('/home/build/google3/base/google.cc', 'basu/google.h')) |
||
| 956 | self.assertEquals((False, ''), f('a.cc', 'b.h')) |
||
| 957 | |||
| 958 | def testCleanseLine(self): |
||
| 959 | self.assertEquals('int foo = 0;', |
||
| 960 | cpplint.CleanseComments('int foo = 0; // danger!'))
|
||
| 961 | self.assertEquals('int o = 0;', |
||
| 962 | cpplint.CleanseComments('int /* foo */ o = 0;'))
|
||
| 963 | self.assertEquals('foo(int a, int b);', |
||
| 964 | cpplint.CleanseComments('foo(int a /* abc */, int b);'))
|
||
| 965 | self.assertEqual('f(a, b);', |
||
| 966 | cpplint.CleanseComments('f(a, /* name */ b);'))
|
||
| 967 | self.assertEqual('f(a, b);', |
||
| 968 | cpplint.CleanseComments('f(a /* name */, b);'))
|
||
| 969 | self.assertEqual('f(a, b);', |
||
| 970 | cpplint.CleanseComments('f(a, /* name */b);'))
|
||
| 971 | self.assertEqual('f(a, b, c);', |
||
| 972 | cpplint.CleanseComments('f(a, /**/b, /**/c);'))
|
||
| 973 | self.assertEqual('f(a, b, c);', |
||
| 974 | cpplint.CleanseComments('f(a, /**/b/**/, c);'))
|
||
| 975 | |||
| 976 | def testRawStrings(self): |
||
| 977 | self.TestMultiLineLint(
|
||
| 978 | """
|
||
| 979 | void Func() {
|
||
| 980 | static const char kString[] = R"(
|
||
| 981 | #endif <- invalid preprocessor should be ignored
|
||
| 982 | */ <- invalid comment should be ignored too
|
||
| 983 | )";
|
||
| 984 | }""",
|
||
| 985 | '')
|
||
| 986 | self.TestMultiLineLint(
|
||
| 987 | """
|
||
| 988 | void Func() {
|
||
| 989 | string s = R"TrueDelimiter(
|
||
| 990 | )"
|
||
| 991 | )FalseDelimiter"
|
||
| 992 | )TrueDelimiter";
|
||
| 993 | }""",
|
||
| 994 | '')
|
||
| 995 | self.TestMultiLineLint(
|
||
| 996 | """
|
||
| 997 | void Func() {
|
||
| 998 | char char kString[] = R"( ";" )";
|
||
| 999 | }""",
|
||
| 1000 | '')
|
||
| 1001 | self.TestMultiLineLint(
|
||
| 1002 | """
|
||
| 1003 | static const char kRawString[] = R"(
|
||
| 1004 | \tstatic const int kLineWithTab = 1;
|
||
| 1005 | static const int kLineWithTrailingWhiteSpace = 1;\x20
|
||
| 1006 |
|
||
| 1007 | void WeirdNumberOfSpacesAtLineStart() {
|
||
| 1008 | string x;
|
||
| 1009 | x += StrCat("Use StrAppend instead");
|
||
| 1010 | }
|
||
| 1011 |
|
||
| 1012 | void BlankLineAtEndOfBlock() {
|
||
| 1013 | // TODO incorrectly formatted
|
||
| 1014 | //Badly formatted comment
|
||
| 1015 |
|
||
| 1016 | }
|
||
| 1017 |
|
||
| 1018 | )";""",
|
||
| 1019 | '')
|
||
| 1020 | self.TestMultiLineLint(
|
||
| 1021 | """
|
||
| 1022 | void Func() {
|
||
| 1023 | string s = StrCat(R"TrueDelimiter(
|
||
| 1024 | )"
|
||
| 1025 | )FalseDelimiter"
|
||
| 1026 | )TrueDelimiter", R"TrueDelimiter2(
|
||
| 1027 | )"
|
||
| 1028 | )FalseDelimiter2"
|
||
| 1029 | )TrueDelimiter2");
|
||
| 1030 | }""",
|
||
| 1031 | '')
|
||
| 1032 | self.TestMultiLineLint(
|
||
| 1033 | """
|
||
| 1034 | static SomeStruct kData = {
|
||
| 1035 | {0, R"(line1
|
||
| 1036 | line2
|
||
| 1037 | )"}
|
||
| 1038 | };""",
|
||
| 1039 | '')
|
||
| 1040 | |||
| 1041 | def testMultiLineComments(self): |
||
| 1042 | # missing explicit is bad
|
||
| 1043 | self.TestMultiLineLint(
|
||
| 1044 | r"""int a = 0;
|
||
| 1045 | /* multi-liner
|
||
| 1046 | class Foo {
|
||
| 1047 | Foo(int f); // should cause a lint warning in code
|
||
| 1048 | }
|
||
| 1049 | */ """,
|
||
| 1050 | '')
|
||
| 1051 | self.TestMultiLineLint(
|
||
| 1052 | r"""/* int a = 0; multi-liner
|
||
| 1053 | static const int b = 0;""",
|
||
| 1054 | 'Could not find end of multi-line comment'
|
||
| 1055 | ' [readability/multiline_comment] [5]')
|
||
| 1056 | self.TestMultiLineLint(r""" /* multi-line comment""", |
||
| 1057 | 'Could not find end of multi-line comment'
|
||
| 1058 | ' [readability/multiline_comment] [5]')
|
||
| 1059 | self.TestMultiLineLint(r""" // /* comment, but not multi-line""", '') |
||
| 1060 | self.TestMultiLineLint(r"""/********** |
||
| 1061 | */""", '') |
||
| 1062 | self.TestMultiLineLint(r"""/** |
||
| 1063 | * Doxygen comment
|
||
| 1064 | */""",
|
||
| 1065 | '')
|
||
| 1066 | self.TestMultiLineLint(r"""/*! |
||
| 1067 | * Doxygen comment
|
||
| 1068 | */""",
|
||
| 1069 | '')
|
||
| 1070 | |||
| 1071 | def testMultilineStrings(self): |
||
| 1072 | multiline_string_error_message = ( |
||
| 1073 | 'Multi-line string ("...") found. This lint script doesn\'t '
|
||
| 1074 | 'do well with such strings, and may give bogus warnings. '
|
||
| 1075 | 'Use C++11 raw strings or concatenation instead.'
|
||
| 1076 | ' [readability/multiline_string] [5]')
|
||
| 1077 | |||
| 1078 | file_path = 'mydir/foo.cc'
|
||
| 1079 | |||
| 1080 | error_collector = ErrorCollector(self.assert_)
|
||
| 1081 | cpplint.ProcessFileData(file_path, 'cc',
|
||
| 1082 | ['const char* str = "This is a\\',
|
||
| 1083 | ' multiline string.";'],
|
||
| 1084 | error_collector) |
||
| 1085 | self.assertEquals(
|
||
| 1086 | 2, # One per line. |
||
| 1087 | error_collector.ResultList().count(multiline_string_error_message)) |
||
| 1088 | |||
| 1089 | # Test non-explicit single-argument constructors
|
||
| 1090 | def testExplicitSingleArgumentConstructors(self): |
||
| 1091 | old_verbose_level = cpplint._cpplint_state.verbose_level |
||
| 1092 | cpplint._cpplint_state.verbose_level = 0
|
||
| 1093 | |||
| 1094 | try:
|
||
| 1095 | # missing explicit is bad
|
||
| 1096 | self.TestMultiLineLint(
|
||
| 1097 | """
|
||
| 1098 | class Foo {
|
||
| 1099 | Foo(int f);
|
||
| 1100 | };""",
|
||
| 1101 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1102 | ' [runtime/explicit] [5]')
|
||
| 1103 | # missing explicit is bad, even with whitespace
|
||
| 1104 | self.TestMultiLineLint(
|
||
| 1105 | """
|
||
| 1106 | class Foo {
|
||
| 1107 | Foo (int f);
|
||
| 1108 | };""",
|
||
| 1109 | ['Extra space before ( in function call [whitespace/parens] [4]',
|
||
| 1110 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1111 | ' [runtime/explicit] [5]'])
|
||
| 1112 | # missing explicit, with distracting comment, is still bad
|
||
| 1113 | self.TestMultiLineLint(
|
||
| 1114 | """
|
||
| 1115 | class Foo {
|
||
| 1116 | Foo(int f); // simpler than Foo(blargh, blarg)
|
||
| 1117 | };""",
|
||
| 1118 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1119 | ' [runtime/explicit] [5]')
|
||
| 1120 | # missing explicit, with qualified classname
|
||
| 1121 | self.TestMultiLineLint(
|
||
| 1122 | """
|
||
| 1123 | class Qualifier::AnotherOne::Foo {
|
||
| 1124 | Foo(int f);
|
||
| 1125 | };""",
|
||
| 1126 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1127 | ' [runtime/explicit] [5]')
|
||
| 1128 | # missing explicit for inline constructors is bad as well
|
||
| 1129 | self.TestMultiLineLint(
|
||
| 1130 | """
|
||
| 1131 | class Foo {
|
||
| 1132 | inline Foo(int f);
|
||
| 1133 | };""",
|
||
| 1134 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1135 | ' [runtime/explicit] [5]')
|
||
| 1136 | # structs are caught as well.
|
||
| 1137 | self.TestMultiLineLint(
|
||
| 1138 | """
|
||
| 1139 | struct Foo {
|
||
| 1140 | Foo(int f);
|
||
| 1141 | };""",
|
||
| 1142 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1143 | ' [runtime/explicit] [5]')
|
||
| 1144 | # Templatized classes are caught as well.
|
||
| 1145 | self.TestMultiLineLint(
|
||
| 1146 | """
|
||
| 1147 | template<typename T> class Foo {
|
||
| 1148 | Foo(int f);
|
||
| 1149 | };""",
|
||
| 1150 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1151 | ' [runtime/explicit] [5]')
|
||
| 1152 | # inline case for templatized classes.
|
||
| 1153 | self.TestMultiLineLint(
|
||
| 1154 | """
|
||
| 1155 | template<typename T> class Foo {
|
||
| 1156 | inline Foo(int f);
|
||
| 1157 | };""",
|
||
| 1158 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1159 | ' [runtime/explicit] [5]')
|
||
| 1160 | # constructors with a default argument should still be marked explicit
|
||
| 1161 | self.TestMultiLineLint(
|
||
| 1162 | """
|
||
| 1163 | class Foo {
|
||
| 1164 | Foo(int f = 0);
|
||
| 1165 | };""",
|
||
| 1166 | 'Constructors callable with one argument should be marked explicit.'
|
||
| 1167 | ' [runtime/explicit] [5]')
|
||
| 1168 | # multi-argument constructors with all but one default argument should be
|
||
| 1169 | # marked explicit
|
||
| 1170 | self.TestMultiLineLint(
|
||
| 1171 | """
|
||
| 1172 | class Foo {
|
||
| 1173 | Foo(int f, int g = 0);
|
||
| 1174 | };""",
|
||
| 1175 | 'Constructors callable with one argument should be marked explicit.'
|
||
| 1176 | ' [runtime/explicit] [5]')
|
||
| 1177 | # multi-argument constructors with all default arguments should be marked
|
||
| 1178 | # explicit
|
||
| 1179 | self.TestMultiLineLint(
|
||
| 1180 | """
|
||
| 1181 | class Foo {
|
||
| 1182 | Foo(int f = 0, int g = 0);
|
||
| 1183 | };""",
|
||
| 1184 | 'Constructors callable with one argument should be marked explicit.'
|
||
| 1185 | ' [runtime/explicit] [5]')
|
||
| 1186 | # explicit no-argument constructors are bad
|
||
| 1187 | self.TestMultiLineLint(
|
||
| 1188 | """
|
||
| 1189 | class Foo {
|
||
| 1190 | explicit Foo();
|
||
| 1191 | };""",
|
||
| 1192 | 'Zero-parameter constructors should not be marked explicit.'
|
||
| 1193 | ' [runtime/explicit] [5]')
|
||
| 1194 | # void constructors are considered no-argument
|
||
| 1195 | self.TestMultiLineLint(
|
||
| 1196 | """
|
||
| 1197 | class Foo {
|
||
| 1198 | explicit Foo(void);
|
||
| 1199 | };""",
|
||
| 1200 | 'Zero-parameter constructors should not be marked explicit.'
|
||
| 1201 | ' [runtime/explicit] [5]')
|
||
| 1202 | # Warn explicit multi-argument constructors at lowest severity
|
||
| 1203 | self.TestMultiLineLint(
|
||
| 1204 | """
|
||
| 1205 | class Foo {
|
||
| 1206 | explicit Foo(int f, int g);
|
||
| 1207 | };""",
|
||
| 1208 | 'Constructors that require multiple arguments '
|
||
| 1209 | 'should not be marked explicit. [runtime/explicit] [0]')
|
||
| 1210 | # but explicit multi-argument constructors with only one non-default
|
||
| 1211 | # argument are OK
|
||
| 1212 | self.TestMultiLineLint(
|
||
| 1213 | """
|
||
| 1214 | class Foo {
|
||
| 1215 | explicit Foo(int f, int g = 0);
|
||
| 1216 | };""",
|
||
| 1217 | '')
|
||
| 1218 | # single-argument constructors that take a function that takes multiple
|
||
| 1219 | # arguments should be explicit
|
||
| 1220 | self.TestMultiLineLint(
|
||
| 1221 | """
|
||
| 1222 | class Foo {
|
||
| 1223 | Foo(void (*f)(int f, int g));
|
||
| 1224 | };""",
|
||
| 1225 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1226 | ' [runtime/explicit] [5]')
|
||
| 1227 | # single-argument constructors that take a single template argument with
|
||
| 1228 | # multiple parameters should be explicit
|
||
| 1229 | self.TestMultiLineLint(
|
||
| 1230 | """
|
||
| 1231 | template <typename T, typename S>
|
||
| 1232 | class Foo {
|
||
| 1233 | Foo(Bar<T, S> b);
|
||
| 1234 | };""",
|
||
| 1235 | 'Single-parameter constructors should be marked explicit.'
|
||
| 1236 | ' [runtime/explicit] [5]')
|
||
| 1237 | # but copy constructors that take multiple template parameters are OK
|
||
| 1238 | self.TestMultiLineLint(
|
||
| 1239 | """
|
||
| 1240 | template <typename T, S>
|
||
| 1241 | class Foo {
|
||
| 1242 | Foo(Foo<T, S>& f);
|
||
| 1243 | };""",
|
||
| 1244 | '')
|
||
| 1245 | # proper style is okay
|
||
| 1246 | self.TestMultiLineLint(
|
||
| 1247 | """
|
||
| 1248 | class Foo {
|
||
| 1249 | explicit Foo(int f);
|
||
| 1250 | };""",
|
||
| 1251 | '')
|
||
| 1252 | # two argument constructor is okay
|
||
| 1253 | self.TestMultiLineLint(
|
||
| 1254 | """
|
||
| 1255 | class Foo {
|
||
| 1256 | Foo(int f, int b);
|
||
| 1257 | };""",
|
||
| 1258 | '')
|
||
| 1259 | # two argument constructor, across two lines, is okay
|
||
| 1260 | self.TestMultiLineLint(
|
||
| 1261 | """
|
||
| 1262 | class Foo {
|
||
| 1263 | Foo(int f,
|
||
| 1264 | int b);
|
||
| 1265 | };""",
|
||
| 1266 | '')
|
||
| 1267 | # non-constructor (but similar name), is okay
|
||
| 1268 | self.TestMultiLineLint(
|
||
| 1269 | """
|
||
| 1270 | class Foo {
|
||
| 1271 | aFoo(int f);
|
||
| 1272 | };""",
|
||
| 1273 | '')
|
||
| 1274 | # constructor with void argument is okay
|
||
| 1275 | self.TestMultiLineLint(
|
||
| 1276 | """
|
||
| 1277 | class Foo {
|
||
| 1278 | Foo(void);
|
||
| 1279 | };""",
|
||
| 1280 | '')
|
||
| 1281 | # single argument method is okay
|
||
| 1282 | self.TestMultiLineLint(
|
||
| 1283 | """
|
||
| 1284 | class Foo {
|
||
| 1285 | Bar(int b);
|
||
| 1286 | };""",
|
||
| 1287 | '')
|
||
| 1288 | # comments should be ignored
|
||
| 1289 | self.TestMultiLineLint(
|
||
| 1290 | """
|
||
| 1291 | class Foo {
|
||
| 1292 | // Foo(int f);
|
||
| 1293 | };""",
|
||
| 1294 | '')
|
||
| 1295 | # single argument function following class definition is okay
|
||
| 1296 | # (okay, it's not actually valid, but we don't want a false positive)
|
||
| 1297 | self.TestMultiLineLint(
|
||
| 1298 | """
|
||
| 1299 | class Foo {
|
||
| 1300 | Foo(int f, int b);
|
||
| 1301 | };
|
||
| 1302 | Foo(int f);""",
|
||
| 1303 | '')
|
||
| 1304 | # single argument function is okay
|
||
| 1305 | self.TestMultiLineLint(
|
||
| 1306 | """static Foo(int f);""",
|
||
| 1307 | '')
|
||
| 1308 | # single argument copy constructor is okay.
|
||
| 1309 | self.TestMultiLineLint(
|
||
| 1310 | """
|
||
| 1311 | class Foo {
|
||
| 1312 | Foo(const Foo&);
|
||
| 1313 | };""",
|
||
| 1314 | '')
|
||
| 1315 | self.TestMultiLineLint(
|
||
| 1316 | """
|
||
| 1317 | class Foo {
|
||
| 1318 | Foo(Foo const&);
|
||
| 1319 | };""",
|
||
| 1320 | '')
|
||
| 1321 | self.TestMultiLineLint(
|
||
| 1322 | """
|
||
| 1323 | class Foo {
|
||
| 1324 | Foo(Foo&);
|
||
| 1325 | };""",
|
||
| 1326 | '')
|
||
| 1327 | # templatized copy constructor is okay.
|
||
| 1328 | self.TestMultiLineLint(
|
||
| 1329 | """
|
||
| 1330 | template<typename T> class Foo {
|
||
| 1331 | Foo(const Foo<T>&);
|
||
| 1332 | };""",
|
||
| 1333 | '')
|
||
| 1334 | # Special case for std::initializer_list
|
||
| 1335 | self.TestMultiLineLint(
|
||
| 1336 | """
|
||
| 1337 | class Foo {
|
||
| 1338 | Foo(std::initializer_list<T> &arg) {}
|
||
| 1339 | };""",
|
||
| 1340 | '')
|
||
| 1341 | # Anything goes inside an assembly block
|
||
| 1342 | error_collector = ErrorCollector(self.assert_)
|
||
| 1343 | cpplint.ProcessFileData('foo.cc', 'cc', |
||
| 1344 | ['void Func() {',
|
||
| 1345 | ' __asm__ (',
|
||
| 1346 | ' "hlt"',
|
||
| 1347 | ' );',
|
||
| 1348 | ' asm {',
|
||
| 1349 | ' movdqa [edx + 32], xmm2',
|
||
| 1350 | ' }',
|
||
| 1351 | '}'],
|
||
| 1352 | error_collector) |
||
| 1353 | self.assertEquals(
|
||
| 1354 | 0,
|
||
| 1355 | error_collector.ResultList().count( |
||
| 1356 | 'Extra space before ( in function call [whitespace/parens] [4]'))
|
||
| 1357 | self.assertEquals(
|
||
| 1358 | 0,
|
||
| 1359 | error_collector.ResultList().count( |
||
| 1360 | 'Closing ) should be moved to the previous line '
|
||
| 1361 | '[whitespace/parens] [2]'))
|
||
| 1362 | self.assertEquals(
|
||
| 1363 | 0,
|
||
| 1364 | error_collector.ResultList().count( |
||
| 1365 | 'Extra space before [ [whitespace/braces] [5]'))
|
||
| 1366 | finally:
|
||
| 1367 | cpplint._cpplint_state.verbose_level = old_verbose_level |
||
| 1368 | |||
| 1369 | def testSlashStarCommentOnSingleLine(self): |
||
| 1370 | self.TestMultiLineLint(
|
||
| 1371 | """/* static */ Foo(int f);""",
|
||
| 1372 | '')
|
||
| 1373 | self.TestMultiLineLint(
|
||
| 1374 | """/*/ static */ Foo(int f);""",
|
||
| 1375 | '')
|
||
| 1376 | self.TestMultiLineLint(
|
||
| 1377 | """/*/ static Foo(int f);""",
|
||
| 1378 | 'Could not find end of multi-line comment'
|
||
| 1379 | ' [readability/multiline_comment] [5]')
|
||
| 1380 | self.TestMultiLineLint(
|
||
| 1381 | """ /*/ static Foo(int f);""",
|
||
| 1382 | 'Could not find end of multi-line comment'
|
||
| 1383 | ' [readability/multiline_comment] [5]')
|
||
| 1384 | self.TestMultiLineLint(
|
||
| 1385 | """ /**/ static Foo(int f);""",
|
||
| 1386 | '')
|
||
| 1387 | |||
| 1388 | # Test suspicious usage of "if" like this:
|
||
| 1389 | # if (a == b) {
|
||
| 1390 | # DoSomething();
|
||
| 1391 | # } if (a == c) { // Should be "else if".
|
||
| 1392 | # DoSomething(); // This gets called twice if a == b && a == c.
|
||
| 1393 | # }
|
||
| 1394 | def testSuspiciousUsageOfIf(self): |
||
| 1395 | self.TestLint(
|
||
| 1396 | ' if (a == b) {',
|
||
| 1397 | '')
|
||
| 1398 | self.TestLint(
|
||
| 1399 | ' } if (a == b) {',
|
||
| 1400 | 'Did you mean "else if"? If not, start a new line for "if".'
|
||
| 1401 | ' [readability/braces] [4]')
|
||
| 1402 | |||
| 1403 | # Test suspicious usage of memset. Specifically, a 0
|
||
| 1404 | # as the final argument is almost certainly an error.
|
||
| 1405 | def testSuspiciousUsageOfMemset(self): |
||
| 1406 | # Normal use is okay.
|
||
| 1407 | self.TestLint(
|
||
| 1408 | ' memset(buf, 0, sizeof(buf))',
|
||
| 1409 | '')
|
||
| 1410 | |||
| 1411 | # A 0 as the final argument is almost certainly an error.
|
||
| 1412 | self.TestLint(
|
||
| 1413 | ' memset(buf, sizeof(buf), 0)',
|
||
| 1414 | 'Did you mean "memset(buf, 0, sizeof(buf))"?'
|
||
| 1415 | ' [runtime/memset] [4]')
|
||
| 1416 | self.TestLint(
|
||
| 1417 | ' memset(buf, xsize * ysize, 0)',
|
||
| 1418 | 'Did you mean "memset(buf, 0, xsize * ysize)"?'
|
||
| 1419 | ' [runtime/memset] [4]')
|
||
| 1420 | |||
| 1421 | # There is legitimate test code that uses this form.
|
||
| 1422 | # This is okay since the second argument is a literal.
|
||
| 1423 | self.TestLint(
|
||
| 1424 | " memset(buf, 'y', 0)",
|
||
| 1425 | '')
|
||
| 1426 | self.TestLint(
|
||
| 1427 | ' memset(buf, 4, 0)',
|
||
| 1428 | '')
|
||
| 1429 | self.TestLint(
|
||
| 1430 | ' memset(buf, -1, 0)',
|
||
| 1431 | '')
|
||
| 1432 | self.TestLint(
|
||
| 1433 | ' memset(buf, 0xF1, 0)',
|
||
| 1434 | '')
|
||
| 1435 | self.TestLint(
|
||
| 1436 | ' memset(buf, 0xcd, 0)',
|
||
| 1437 | '')
|
||
| 1438 | |||
| 1439 | def testRedundantVirtual(self): |
||
| 1440 | self.TestLint('virtual void F()', '') |
||
| 1441 | self.TestLint('virtual void F();', '') |
||
| 1442 | self.TestLint('virtual void F() {}', '') |
||
| 1443 | |||
| 1444 | message_template = ('"%s" is redundant since function is already '
|
||
| 1445 | 'declared as "%s" [readability/inheritance] [4]')
|
||
| 1446 | for virt_specifier in ['override', 'final']: |
||
| 1447 | error_message = message_template % ('virtual', virt_specifier)
|
||
| 1448 | self.TestLint('virtual int F() %s' % virt_specifier, error_message) |
||
| 1449 | self.TestLint('virtual int F() %s;' % virt_specifier, error_message) |
||
| 1450 | self.TestLint('virtual int F() %s {' % virt_specifier, error_message) |
||
| 1451 | |||
| 1452 | error_collector = ErrorCollector(self.assert_)
|
||
| 1453 | cpplint.ProcessFileData( |
||
| 1454 | 'foo.cc', 'cc', |
||
| 1455 | ['// Copyright 2014 Your Company.',
|
||
| 1456 | 'virtual void F(int a,',
|
||
| 1457 | ' int b) ' + virt_specifier + ';', |
||
| 1458 | 'virtual void F(int a,',
|
||
| 1459 | ' int b) LOCKS_EXCLUDED(lock) ' + virt_specifier + ';', |
||
| 1460 | 'virtual void F(int a,',
|
||
| 1461 | ' int b)',
|
||
| 1462 | ' LOCKS_EXCLUDED(lock) ' + virt_specifier + ';', |
||
| 1463 | ''],
|
||
| 1464 | error_collector) |
||
| 1465 | self.assertEquals(
|
||
| 1466 | [error_message, error_message, error_message], |
||
| 1467 | error_collector.Results()) |
||
| 1468 | |||
| 1469 | error_message = message_template % ('override', 'final') |
||
| 1470 | self.TestLint('int F() override final', error_message) |
||
| 1471 | self.TestLint('int F() override final;', error_message) |
||
| 1472 | self.TestLint('int F() override final {}', error_message) |
||
| 1473 | self.TestLint('int F() final override', error_message) |
||
| 1474 | self.TestLint('int F() final override;', error_message) |
||
| 1475 | self.TestLint('int F() final override {}', error_message) |
||
| 1476 | |||
| 1477 | error_collector = ErrorCollector(self.assert_)
|
||
| 1478 | cpplint.ProcessFileData( |
||
| 1479 | 'foo.cc', 'cc', |
||
| 1480 | ['// Copyright 2014 Your Company.',
|
||
| 1481 | 'struct A : virtual B {',
|
||
| 1482 | ' ~A() override;'
|
||
| 1483 | '};',
|
||
| 1484 | 'class C',
|
||
| 1485 | ' : public D,',
|
||
| 1486 | ' public virtual E {',
|
||
| 1487 | ' void Func() override;',
|
||
| 1488 | '}',
|
||
| 1489 | ''],
|
||
| 1490 | error_collector) |
||
| 1491 | self.assertEquals('', error_collector.Results()) |
||
| 1492 | |||
| 1493 | self.TestLint('void Finalize(AnnotationProto *final) override;', '') |
||
| 1494 | |||
| 1495 | def testCheckDeprecated(self): |
||
| 1496 | self.TestLanguageRulesCheck('foo_test.cc', '#include <iostream>', '') |
||
| 1497 | self.TestLanguageRulesCheck('foo_unittest.cc', '#include <iostream>', '') |
||
| 1498 | |||
| 1499 | def testCheckPosixThreading(self): |
||
| 1500 | self.TestLint('var = sctime_r()', '') |
||
| 1501 | self.TestLint('var = strtok_r()', '') |
||
| 1502 | self.TestLint('var = strtok_r(foo, ba, r)', '') |
||
| 1503 | self.TestLint('var = brand()', '') |
||
| 1504 | self.TestLint('_rand()', '') |
||
| 1505 | self.TestLint('.rand()', '') |
||
| 1506 | self.TestLint('->rand()', '') |
||
| 1507 | self.TestLint('ACMRandom rand(seed)', '') |
||
| 1508 | self.TestLint('ISAACRandom rand()', '') |
||
| 1509 | self.TestLint('var = rand()', |
||
| 1510 | 'Consider using rand_r(...) instead of rand(...)'
|
||
| 1511 | ' for improved thread safety.'
|
||
| 1512 | ' [runtime/threadsafe_fn] [2]')
|
||
| 1513 | self.TestLint('var = strtok(str, delim)', |
||
| 1514 | 'Consider using strtok_r(...) '
|
||
| 1515 | 'instead of strtok(...)'
|
||
| 1516 | ' for improved thread safety.'
|
||
| 1517 | ' [runtime/threadsafe_fn] [2]')
|
||
| 1518 | |||
| 1519 | def testVlogMisuse(self): |
||
| 1520 | self.TestLint('VLOG(1)', '') |
||
| 1521 | self.TestLint('VLOG(99)', '') |
||
| 1522 | self.TestLint('LOG(ERROR)', '') |
||
| 1523 | self.TestLint('LOG(INFO)', '') |
||
| 1524 | self.TestLint('LOG(WARNING)', '') |
||
| 1525 | self.TestLint('LOG(FATAL)', '') |
||
| 1526 | self.TestLint('LOG(DFATAL)', '') |
||
| 1527 | self.TestLint('VLOG(SOMETHINGWEIRD)', '') |
||
| 1528 | self.TestLint('MYOWNVLOG(ERROR)', '') |
||
| 1529 | errmsg = ('VLOG() should be used with numeric verbosity level. '
|
||
| 1530 | 'Use LOG() if you want symbolic severity levels.'
|
||
| 1531 | ' [runtime/vlog] [5]')
|
||
| 1532 | self.TestLint('VLOG(ERROR)', errmsg) |
||
| 1533 | self.TestLint('VLOG(INFO)', errmsg) |
||
| 1534 | self.TestLint('VLOG(WARNING)', errmsg) |
||
| 1535 | self.TestLint('VLOG(FATAL)', errmsg) |
||
| 1536 | self.TestLint('VLOG(DFATAL)', errmsg) |
||
| 1537 | self.TestLint(' VLOG(ERROR)', errmsg) |
||
| 1538 | self.TestLint(' VLOG(INFO)', errmsg) |
||
| 1539 | self.TestLint(' VLOG(WARNING)', errmsg) |
||
| 1540 | self.TestLint(' VLOG(FATAL)', errmsg) |
||
| 1541 | self.TestLint(' VLOG(DFATAL)', errmsg) |
||
| 1542 | |||
| 1543 | |||
| 1544 | # Test potential format string bugs like printf(foo).
|
||
| 1545 | def testFormatStrings(self): |
||
| 1546 | self.TestLint('printf("foo")', '') |
||
| 1547 | self.TestLint('printf("foo: %s", foo)', '') |
||
| 1548 | self.TestLint('DocidForPrintf(docid)', '') # Should not trigger. |
||
| 1549 | self.TestLint('printf(format, value)', '') # Should not trigger. |
||
| 1550 | self.TestLint('printf(__VA_ARGS__)', '') # Should not trigger. |
||
| 1551 | self.TestLint('printf(format.c_str(), value)', '') # Should not trigger. |
||
| 1552 | self.TestLint('printf(format(index).c_str(), value)', '') |
||
| 1553 | self.TestLint(
|
||
| 1554 | 'printf(foo)',
|
||
| 1555 | 'Potential format string bug. Do printf("%s", foo) instead.'
|
||
| 1556 | ' [runtime/printf] [4]')
|
||
| 1557 | self.TestLint(
|
||
| 1558 | 'printf(foo.c_str())',
|
||
| 1559 | 'Potential format string bug. '
|
||
| 1560 | 'Do printf("%s", foo.c_str()) instead.'
|
||
| 1561 | ' [runtime/printf] [4]')
|
||
| 1562 | self.TestLint(
|
||
| 1563 | 'printf(foo->c_str())',
|
||
| 1564 | 'Potential format string bug. '
|
||
| 1565 | 'Do printf("%s", foo->c_str()) instead.'
|
||
| 1566 | ' [runtime/printf] [4]')
|
||
| 1567 | self.TestLint(
|
||
| 1568 | 'StringPrintf(foo)',
|
||
| 1569 | 'Potential format string bug. Do StringPrintf("%s", foo) instead.'
|
||
| 1570 | ''
|
||
| 1571 | ' [runtime/printf] [4]')
|
||
| 1572 | |||
| 1573 | # Test disallowed use of operator& and other operators.
|
||
| 1574 | def testIllegalOperatorOverloading(self): |
||
| 1575 | errmsg = ('Unary operator& is dangerous. Do not use it.'
|
||
| 1576 | ' [runtime/operator] [4]')
|
||
| 1577 | self.TestLint('void operator=(const Myclass&)', '') |
||
| 1578 | self.TestLint('void operator&(int a, int b)', '') # binary operator& ok |
||
| 1579 | self.TestLint('void operator&() { }', errmsg) |
||
| 1580 | self.TestLint('void operator & ( ) { }', |
||
| 1581 | ['Extra space after ( [whitespace/parens] [2]', errmsg])
|
||
| 1582 | |||
| 1583 | # const string reference members are dangerous..
|
||
| 1584 | def testConstStringReferenceMembers(self): |
||
| 1585 | errmsg = ('const string& members are dangerous. It is much better to use '
|
||
| 1586 | 'alternatives, such as pointers or simple constants.'
|
||
| 1587 | ' [runtime/member_string_references] [2]')
|
||
| 1588 | |||
| 1589 | members_declarations = ['const string& church',
|
||
| 1590 | 'const string &turing',
|
||
| 1591 | 'const string & godel']
|
||
| 1592 | # TODO(unknown): Enable also these tests if and when we ever
|
||
| 1593 | # decide to check for arbitrary member references.
|
||
| 1594 | # "const Turing & a",
|
||
| 1595 | # "const Church& a",
|
||
| 1596 | # "const vector<int>& a",
|
||
| 1597 | # "const Kurt::Godel & godel",
|
||
| 1598 | # "const Kazimierz::Kuratowski& kk" ]
|
||
| 1599 | |||
| 1600 | # The Good.
|
||
| 1601 | |||
| 1602 | self.TestLint('void f(const string&)', '') |
||
| 1603 | self.TestLint('const string& f(const string& a, const string& b)', '') |
||
| 1604 | self.TestLint('typedef const string& A;', '') |
||
| 1605 | |||
| 1606 | for decl in members_declarations: |
||
| 1607 | self.TestLint(decl + ' = b;', '') |
||
| 1608 | self.TestLint(decl + ' =', '') |
||
| 1609 | |||
| 1610 | # The Bad.
|
||
| 1611 | |||
| 1612 | for decl in members_declarations: |
||
| 1613 | self.TestLint(decl + ';', errmsg) |
||
| 1614 | |||
| 1615 | # Variable-length arrays are not permitted.
|
||
| 1616 | def testVariableLengthArrayDetection(self): |
||
| 1617 | errmsg = ('Do not use variable-length arrays. Use an appropriately named '
|
||
| 1618 | "('k' followed by CamelCase) compile-time constant for the size."
|
||
| 1619 | ' [runtime/arrays] [1]')
|
||
| 1620 | |||
| 1621 | self.TestLint('int a[any_old_variable];', errmsg) |
||
| 1622 | self.TestLint('int doublesize[some_var * 2];', errmsg) |
||
| 1623 | self.TestLint('int a[afunction()];', errmsg) |
||
| 1624 | self.TestLint('int a[function(kMaxFooBars)];', errmsg) |
||
| 1625 | self.TestLint('bool a_list[items_->size()];', errmsg) |
||
| 1626 | self.TestLint('namespace::Type buffer[len+1];', errmsg) |
||
| 1627 | |||
| 1628 | self.TestLint('int a[64];', '') |
||
| 1629 | self.TestLint('int a[0xFF];', '') |
||
| 1630 | self.TestLint('int first[256], second[256];', '') |
||
| 1631 | self.TestLint('int array_name[kCompileTimeConstant];', '') |
||
| 1632 | self.TestLint('char buf[somenamespace::kBufSize];', '') |
||
| 1633 | self.TestLint('int array_name[ALL_CAPS];', '') |
||
| 1634 | self.TestLint('AClass array1[foo::bar::ALL_CAPS];', '') |
||
| 1635 | self.TestLint('int a[kMaxStrLen + 1];', '') |
||
| 1636 | self.TestLint('int a[sizeof(foo)];', '') |
||
| 1637 | self.TestLint('int a[sizeof(*foo)];', '') |
||
| 1638 | self.TestLint('int a[sizeof foo];', '') |
||
| 1639 | self.TestLint('int a[sizeof(struct Foo)];', '') |
||
| 1640 | self.TestLint('int a[128 - sizeof(const bar)];', '') |
||
| 1641 | self.TestLint('int a[(sizeof(foo) * 4)];', '') |
||
| 1642 | self.TestLint('int a[(arraysize(fixed_size_array)/2) << 1];', '') |
||
| 1643 | self.TestLint('delete a[some_var];', '') |
||
| 1644 | self.TestLint('return a[some_var];', '') |
||
| 1645 | |||
| 1646 | # DISALLOW_COPY_AND_ASSIGN and DISALLOW_IMPLICIT_CONSTRUCTORS should be at
|
||
| 1647 | # end of class if present.
|
||
| 1648 | def testDisallowMacrosAtEnd(self): |
||
| 1649 | for macro_name in ( |
||
| 1650 | 'DISALLOW_COPY_AND_ASSIGN',
|
||
| 1651 | 'DISALLOW_IMPLICIT_CONSTRUCTORS'):
|
||
| 1652 | error_collector = ErrorCollector(self.assert_)
|
||
| 1653 | cpplint.ProcessFileData( |
||
| 1654 | 'foo.cc', 'cc', |
||
| 1655 | ['// Copyright 2014 Your Company.',
|
||
| 1656 | 'class SomeClass {',
|
||
| 1657 | ' private:',
|
||
| 1658 | ' %s(SomeClass);' % macro_name,
|
||
| 1659 | ' int member_;',
|
||
| 1660 | '};',
|
||
| 1661 | ''],
|
||
| 1662 | error_collector) |
||
| 1663 | self.assertEquals(
|
||
| 1664 | ('%s should be the last thing in the class' % macro_name) +
|
||
| 1665 | ' [readability/constructors] [3]',
|
||
| 1666 | error_collector.Results()) |
||
| 1667 | |||
| 1668 | error_collector = ErrorCollector(self.assert_)
|
||
| 1669 | cpplint.ProcessFileData( |
||
| 1670 | 'foo.cc', 'cc', |
||
| 1671 | ['// Copyright 2014 Your Company.',
|
||
| 1672 | 'class OuterClass {',
|
||
| 1673 | ' private:',
|
||
| 1674 | ' struct InnerClass {',
|
||
| 1675 | ' private:',
|
||
| 1676 | ' %s(InnerClass);' % macro_name,
|
||
| 1677 | ' int member;',
|
||
| 1678 | ' };',
|
||
| 1679 | '};',
|
||
| 1680 | ''],
|
||
| 1681 | error_collector) |
||
| 1682 | self.assertEquals(
|
||
| 1683 | ('%s should be the last thing in the class' % macro_name) +
|
||
| 1684 | ' [readability/constructors] [3]',
|
||
| 1685 | error_collector.Results()) |
||
| 1686 | |||
| 1687 | error_collector = ErrorCollector(self.assert_)
|
||
| 1688 | cpplint.ProcessFileData( |
||
| 1689 | 'foo.cc', 'cc', |
||
| 1690 | ['// Copyright 2014 Your Company.',
|
||
| 1691 | 'class OuterClass1 {',
|
||
| 1692 | ' private:',
|
||
| 1693 | ' struct InnerClass1 {',
|
||
| 1694 | ' private:',
|
||
| 1695 | ' %s(InnerClass1);' % macro_name,
|
||
| 1696 | ' };',
|
||
| 1697 | ' %s(OuterClass1);' % macro_name,
|
||
| 1698 | '};',
|
||
| 1699 | 'struct OuterClass2 {',
|
||
| 1700 | ' private:',
|
||
| 1701 | ' class InnerClass2 {',
|
||
| 1702 | ' private:',
|
||
| 1703 | ' %s(InnerClass2);' % macro_name,
|
||
| 1704 | ' // comment',
|
||
| 1705 | ' };',
|
||
| 1706 | '',
|
||
| 1707 | ' %s(OuterClass2);' % macro_name,
|
||
| 1708 | '',
|
||
| 1709 | ' // comment',
|
||
| 1710 | '};',
|
||
| 1711 | 'void Func() {',
|
||
| 1712 | ' struct LocalClass {',
|
||
| 1713 | ' private:',
|
||
| 1714 | ' %s(LocalClass);' % macro_name,
|
||
| 1715 | ' } variable;',
|
||
| 1716 | '}',
|
||
| 1717 | ''],
|
||
| 1718 | error_collector) |
||
| 1719 | self.assertEquals('', error_collector.Results()) |
||
| 1720 | |||
| 1721 | # DISALLOW* macros should be in the private: section.
|
||
| 1722 | def testMisplacedDisallowMacros(self): |
||
| 1723 | for macro_name in ( |
||
| 1724 | 'DISALLOW_COPY_AND_ASSIGN',
|
||
| 1725 | 'DISALLOW_IMPLICIT_CONSTRUCTORS'):
|
||
| 1726 | self.TestMultiLineLint(
|
||
| 1727 | """
|
||
| 1728 | class A {'
|
||
| 1729 | public:
|
||
| 1730 | %s(A);
|
||
| 1731 | };""" % macro_name,
|
||
| 1732 | ('%s must be in the private: section' % macro_name) +
|
||
| 1733 | ' [readability/constructors] [3]')
|
||
| 1734 | |||
| 1735 | self.TestMultiLineLint(
|
||
| 1736 | """
|
||
| 1737 | struct B {'
|
||
| 1738 | %s(B);
|
||
| 1739 | };""" % macro_name,
|
||
| 1740 | ('%s must be in the private: section' % macro_name) +
|
||
| 1741 | ' [readability/constructors] [3]')
|
||
| 1742 | |||
| 1743 | self.TestMultiLineLint(
|
||
| 1744 | """
|
||
| 1745 | class Outer1 {'
|
||
| 1746 | private:
|
||
| 1747 | struct Inner1 {
|
||
| 1748 | %s(Inner1);
|
||
| 1749 | };
|
||
| 1750 | %s(Outer1);
|
||
| 1751 | };""" % (macro_name, macro_name),
|
||
| 1752 | ('%s must be in the private: section' % macro_name) +
|
||
| 1753 | ' [readability/constructors] [3]')
|
||
| 1754 | |||
| 1755 | self.TestMultiLineLint(
|
||
| 1756 | """
|
||
| 1757 | class Outer2 {'
|
||
| 1758 | private:
|
||
| 1759 | class Inner2 {
|
||
| 1760 | %s(Inner2);
|
||
| 1761 | };
|
||
| 1762 | %s(Outer2);
|
||
| 1763 | };""" % (macro_name, macro_name),
|
||
| 1764 | '')
|
||
| 1765 | # Extra checks to make sure that nested classes are handled
|
||
| 1766 | # correctly. Use different macros for inner and outer classes so
|
||
| 1767 | # that we can tell the error messages apart.
|
||
| 1768 | self.TestMultiLineLint(
|
||
| 1769 | """
|
||
| 1770 | class Outer3 {
|
||
| 1771 | struct Inner3 {
|
||
| 1772 | DISALLOW_COPY_AND_ASSIGN(Inner3);
|
||
| 1773 | };
|
||
| 1774 | DISALLOW_IMPLICIT_CONSTRUCTORS(Outer3);
|
||
| 1775 | };""",
|
||
| 1776 | ('DISALLOW_COPY_AND_ASSIGN must be in the private: section'
|
||
| 1777 | ' [readability/constructors] [3]'))
|
||
| 1778 | self.TestMultiLineLint(
|
||
| 1779 | """
|
||
| 1780 | struct Outer4 {
|
||
| 1781 | class Inner4 {
|
||
| 1782 | DISALLOW_COPY_AND_ASSIGN(Inner4);
|
||
| 1783 | };
|
||
| 1784 | DISALLOW_IMPLICIT_CONSTRUCTORS(Outer4);
|
||
| 1785 | };""",
|
||
| 1786 | ('DISALLOW_IMPLICIT_CONSTRUCTORS must be in the private: section'
|
||
| 1787 | ' [readability/constructors] [3]'))
|
||
| 1788 | |||
| 1789 | # Brace usage
|
||
| 1790 | def testBraces(self): |
||
| 1791 | # Braces shouldn't be followed by a ; unless they're defining a struct
|
||
| 1792 | # or initializing an array
|
||
| 1793 | self.TestLint('int a[3] = { 1, 2, 3 };', '') |
||
| 1794 | self.TestLint(
|
||
| 1795 | """const int foo[] =
|
||
| 1796 | {1, 2, 3 };""",
|
||
| 1797 | '')
|
||
| 1798 | # For single line, unmatched '}' with a ';' is ignored (not enough context)
|
||
| 1799 | self.TestMultiLineLint(
|
||
| 1800 | """int a[3] = { 1,
|
||
| 1801 | 2,
|
||
| 1802 | 3 };""",
|
||
| 1803 | '')
|
||
| 1804 | self.TestMultiLineLint(
|
||
| 1805 | """int a[2][3] = { { 1, 2 },
|
||
| 1806 | { 3, 4 } };""",
|
||
| 1807 | '')
|
||
| 1808 | self.TestMultiLineLint(
|
||
| 1809 | """int a[2][3] =
|
||
| 1810 | { { 1, 2 },
|
||
| 1811 | { 3, 4 } };""",
|
||
| 1812 | '')
|
||
| 1813 | |||
| 1814 | # CHECK/EXPECT_TRUE/EXPECT_FALSE replacements
|
||
| 1815 | def testCheckCheck(self): |
||
| 1816 | self.TestLint('CHECK(x == 42);', |
||
| 1817 | 'Consider using CHECK_EQ instead of CHECK(a == b)'
|
||
| 1818 | ' [readability/check] [2]')
|
||
| 1819 | self.TestLint('CHECK(x != 42);', |
||
| 1820 | 'Consider using CHECK_NE instead of CHECK(a != b)'
|
||
| 1821 | ' [readability/check] [2]')
|
||
| 1822 | self.TestLint('CHECK(x >= 42);', |
||
| 1823 | 'Consider using CHECK_GE instead of CHECK(a >= b)'
|
||
| 1824 | ' [readability/check] [2]')
|
||
| 1825 | self.TestLint('CHECK(x > 42);', |
||
| 1826 | 'Consider using CHECK_GT instead of CHECK(a > b)'
|
||
| 1827 | ' [readability/check] [2]')
|
||
| 1828 | self.TestLint('CHECK(x <= 42);', |
||
| 1829 | 'Consider using CHECK_LE instead of CHECK(a <= b)'
|
||
| 1830 | ' [readability/check] [2]')
|
||
| 1831 | self.TestLint('CHECK(x < 42);', |
||
| 1832 | 'Consider using CHECK_LT instead of CHECK(a < b)'
|
||
| 1833 | ' [readability/check] [2]')
|
||
| 1834 | |||
| 1835 | self.TestLint('DCHECK(x == 42);', |
||
| 1836 | 'Consider using DCHECK_EQ instead of DCHECK(a == b)'
|
||
| 1837 | ' [readability/check] [2]')
|
||
| 1838 | self.TestLint('DCHECK(x != 42);', |
||
| 1839 | 'Consider using DCHECK_NE instead of DCHECK(a != b)'
|
||
| 1840 | ' [readability/check] [2]')
|
||
| 1841 | self.TestLint('DCHECK(x >= 42);', |
||
| 1842 | 'Consider using DCHECK_GE instead of DCHECK(a >= b)'
|
||
| 1843 | ' [readability/check] [2]')
|
||
| 1844 | self.TestLint('DCHECK(x > 42);', |
||
| 1845 | 'Consider using DCHECK_GT instead of DCHECK(a > b)'
|
||
| 1846 | ' [readability/check] [2]')
|
||
| 1847 | self.TestLint('DCHECK(x <= 42);', |
||
| 1848 | 'Consider using DCHECK_LE instead of DCHECK(a <= b)'
|
||
| 1849 | ' [readability/check] [2]')
|
||
| 1850 | self.TestLint('DCHECK(x < 42);', |
||
| 1851 | 'Consider using DCHECK_LT instead of DCHECK(a < b)'
|
||
| 1852 | ' [readability/check] [2]')
|
||
| 1853 | |||
| 1854 | self.TestLint(
|
||
| 1855 | 'EXPECT_TRUE("42" == x);',
|
||
| 1856 | 'Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b)'
|
||
| 1857 | ' [readability/check] [2]')
|
||
| 1858 | self.TestLint(
|
||
| 1859 | 'EXPECT_TRUE("42" != x);',
|
||
| 1860 | 'Consider using EXPECT_NE instead of EXPECT_TRUE(a != b)'
|
||
| 1861 | ' [readability/check] [2]')
|
||
| 1862 | self.TestLint(
|
||
| 1863 | 'EXPECT_TRUE(+42 >= x);',
|
||
| 1864 | 'Consider using EXPECT_GE instead of EXPECT_TRUE(a >= b)'
|
||
| 1865 | ' [readability/check] [2]')
|
||
| 1866 | self.TestLint(
|
||
| 1867 | 'EXPECT_TRUE_M(-42 > x);',
|
||
| 1868 | 'Consider using EXPECT_GT_M instead of EXPECT_TRUE_M(a > b)'
|
||
| 1869 | ' [readability/check] [2]')
|
||
| 1870 | self.TestLint(
|
||
| 1871 | 'EXPECT_TRUE_M(42U <= x);',
|
||
| 1872 | 'Consider using EXPECT_LE_M instead of EXPECT_TRUE_M(a <= b)'
|
||
| 1873 | ' [readability/check] [2]')
|
||
| 1874 | self.TestLint(
|
||
| 1875 | 'EXPECT_TRUE_M(42L < x);',
|
||
| 1876 | 'Consider using EXPECT_LT_M instead of EXPECT_TRUE_M(a < b)'
|
||
| 1877 | ' [readability/check] [2]')
|
||
| 1878 | |||
| 1879 | self.TestLint(
|
||
| 1880 | 'EXPECT_FALSE(x == 42);',
|
||
| 1881 | 'Consider using EXPECT_NE instead of EXPECT_FALSE(a == b)'
|
||
| 1882 | ' [readability/check] [2]')
|
||
| 1883 | self.TestLint(
|
||
| 1884 | 'EXPECT_FALSE(x != 42);',
|
||
| 1885 | 'Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b)'
|
||
| 1886 | ' [readability/check] [2]')
|
||
| 1887 | self.TestLint(
|
||
| 1888 | 'EXPECT_FALSE(x >= 42);',
|
||
| 1889 | 'Consider using EXPECT_LT instead of EXPECT_FALSE(a >= b)'
|
||
| 1890 | ' [readability/check] [2]')
|
||
| 1891 | self.TestLint(
|
||
| 1892 | 'ASSERT_FALSE(x > 42);',
|
||
| 1893 | 'Consider using ASSERT_LE instead of ASSERT_FALSE(a > b)'
|
||
| 1894 | ' [readability/check] [2]')
|
||
| 1895 | self.TestLint(
|
||
| 1896 | 'ASSERT_FALSE(x <= 42);',
|
||
| 1897 | 'Consider using ASSERT_GT instead of ASSERT_FALSE(a <= b)'
|
||
| 1898 | ' [readability/check] [2]')
|
||
| 1899 | self.TestLint(
|
||
| 1900 | 'ASSERT_FALSE_M(x < 42);',
|
||
| 1901 | 'Consider using ASSERT_GE_M instead of ASSERT_FALSE_M(a < b)'
|
||
| 1902 | ' [readability/check] [2]')
|
||
| 1903 | |||
| 1904 | self.TestLint('CHECK(x<42);', |
||
| 1905 | ['Missing spaces around <'
|
||
| 1906 | ' [whitespace/operators] [3]',
|
||
| 1907 | 'Consider using CHECK_LT instead of CHECK(a < b)'
|
||
| 1908 | ' [readability/check] [2]'])
|
||
| 1909 | self.TestLint('CHECK(x>42);', |
||
| 1910 | ['Missing spaces around >'
|
||
| 1911 | ' [whitespace/operators] [3]',
|
||
| 1912 | 'Consider using CHECK_GT instead of CHECK(a > b)'
|
||
| 1913 | ' [readability/check] [2]'])
|
||
| 1914 | |||
| 1915 | self.TestLint('using some::namespace::operator<<;', '') |
||
| 1916 | self.TestLint('using some::namespace::operator>>;', '') |
||
| 1917 | |||
| 1918 | self.TestLint('CHECK(x->y == 42);', |
||
| 1919 | 'Consider using CHECK_EQ instead of CHECK(a == b)'
|
||
| 1920 | ' [readability/check] [2]')
|
||
| 1921 | |||
| 1922 | self.TestLint(
|
||
| 1923 | ' EXPECT_TRUE(42 < x); // Random comment.',
|
||
| 1924 | 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
|
||
| 1925 | ' [readability/check] [2]')
|
||
| 1926 | self.TestLint(
|
||
| 1927 | 'EXPECT_TRUE( 42 < x );',
|
||
| 1928 | ['Extra space after ( in function call'
|
||
| 1929 | ' [whitespace/parens] [4]',
|
||
| 1930 | 'Extra space before ) [whitespace/parens] [2]',
|
||
| 1931 | 'Consider using EXPECT_LT instead of EXPECT_TRUE(a < b)'
|
||
| 1932 | ' [readability/check] [2]'])
|
||
| 1933 | |||
| 1934 | self.TestLint('CHECK(4\'2 == x);', |
||
| 1935 | 'Consider using CHECK_EQ instead of CHECK(a == b)'
|
||
| 1936 | ' [readability/check] [2]')
|
||
| 1937 | |||
| 1938 | def testCheckCheckFalsePositives(self): |
||
| 1939 | self.TestLint('CHECK(some_iterator == obj.end());', '') |
||
| 1940 | self.TestLint('EXPECT_TRUE(some_iterator == obj.end());', '') |
||
| 1941 | self.TestLint('EXPECT_FALSE(some_iterator == obj.end());', '') |
||
| 1942 | self.TestLint('CHECK(some_pointer != NULL);', '') |
||
| 1943 | self.TestLint('EXPECT_TRUE(some_pointer != NULL);', '') |
||
| 1944 | self.TestLint('EXPECT_FALSE(some_pointer != NULL);', '') |
||
| 1945 | |||
| 1946 | self.TestLint('CHECK(CreateTestFile(dir, (1 << 20)));', '') |
||
| 1947 | self.TestLint('CHECK(CreateTestFile(dir, (1 >> 20)));', '') |
||
| 1948 | |||
| 1949 | self.TestLint('CHECK(x ^ (y < 42));', '') |
||
| 1950 | self.TestLint('CHECK((x > 42) ^ (x < 54));', '') |
||
| 1951 | self.TestLint('CHECK(a && b < 42);', '') |
||
| 1952 | self.TestLint('CHECK(42 < a && a < b);', '') |
||
| 1953 | self.TestLint('SOFT_CHECK(x > 42);', '') |
||
| 1954 | |||
| 1955 | self.TestMultiLineLint(
|
||
| 1956 | """_STLP_DEFINE_BINARY_OP_CHECK(==, _OP_EQUAL);
|
||
| 1957 | _STLP_DEFINE_BINARY_OP_CHECK(!=, _OP_NOT_EQUAL);
|
||
| 1958 | _STLP_DEFINE_BINARY_OP_CHECK(<, _OP_LESS_THAN);
|
||
| 1959 | _STLP_DEFINE_BINARY_OP_CHECK(<=, _OP_LESS_EQUAL);
|
||
| 1960 | _STLP_DEFINE_BINARY_OP_CHECK(>, _OP_GREATER_THAN);
|
||
| 1961 | _STLP_DEFINE_BINARY_OP_CHECK(>=, _OP_GREATER_EQUAL);
|
||
| 1962 | _STLP_DEFINE_BINARY_OP_CHECK(+, _OP_PLUS);
|
||
| 1963 | _STLP_DEFINE_BINARY_OP_CHECK(*, _OP_TIMES);
|
||
| 1964 | _STLP_DEFINE_BINARY_OP_CHECK(/, _OP_DIVIDE);
|
||
| 1965 | _STLP_DEFINE_BINARY_OP_CHECK(-, _OP_SUBTRACT);
|
||
| 1966 | _STLP_DEFINE_BINARY_OP_CHECK(%, _OP_MOD);""",
|
||
| 1967 | '')
|
||
| 1968 | |||
| 1969 | self.TestLint('CHECK(x < 42) << "Custom error message";', '') |
||
| 1970 | |||
| 1971 | # Alternative token to punctuation operator replacements
|
||
| 1972 | def testCheckAltTokens(self): |
||
| 1973 | self.TestLint('true or true', |
||
| 1974 | 'Use operator || instead of or'
|
||
| 1975 | ' [readability/alt_tokens] [2]')
|
||
| 1976 | self.TestLint('true and true', |
||
| 1977 | 'Use operator && instead of and'
|
||
| 1978 | ' [readability/alt_tokens] [2]')
|
||
| 1979 | self.TestLint('if (not true)', |
||
| 1980 | 'Use operator ! instead of not'
|
||
| 1981 | ' [readability/alt_tokens] [2]')
|
||
| 1982 | self.TestLint('1 bitor 1', |
||
| 1983 | 'Use operator | instead of bitor'
|
||
| 1984 | ' [readability/alt_tokens] [2]')
|
||
| 1985 | self.TestLint('1 xor 1', |
||
| 1986 | 'Use operator ^ instead of xor'
|
||
| 1987 | ' [readability/alt_tokens] [2]')
|
||
| 1988 | self.TestLint('1 bitand 1', |
||
| 1989 | 'Use operator & instead of bitand'
|
||
| 1990 | ' [readability/alt_tokens] [2]')
|
||
| 1991 | self.TestLint('x = compl 1', |
||
| 1992 | 'Use operator ~ instead of compl'
|
||
| 1993 | ' [readability/alt_tokens] [2]')
|
||
| 1994 | self.TestLint('x and_eq y', |
||
| 1995 | 'Use operator &= instead of and_eq'
|
||
| 1996 | ' [readability/alt_tokens] [2]')
|
||
| 1997 | self.TestLint('x or_eq y', |
||
| 1998 | 'Use operator |= instead of or_eq'
|
||
| 1999 | ' [readability/alt_tokens] [2]')
|
||
| 2000 | self.TestLint('x xor_eq y', |
||
| 2001 | 'Use operator ^= instead of xor_eq'
|
||
| 2002 | ' [readability/alt_tokens] [2]')
|
||
| 2003 | self.TestLint('x not_eq y', |
||
| 2004 | 'Use operator != instead of not_eq'
|
||
| 2005 | ' [readability/alt_tokens] [2]')
|
||
| 2006 | self.TestLint('line_continuation or', |
||
| 2007 | 'Use operator || instead of or'
|
||
| 2008 | ' [readability/alt_tokens] [2]')
|
||
| 2009 | self.TestLint('if(true and(parentheses', |
||
| 2010 | 'Use operator && instead of and'
|
||
| 2011 | ' [readability/alt_tokens] [2]')
|
||
| 2012 | |||
| 2013 | self.TestLint('#include "base/false-and-false.h"', '') |
||
| 2014 | self.TestLint('#error false or false', '') |
||
| 2015 | self.TestLint('false nor false', '') |
||
| 2016 | self.TestLint('false nand false', '') |
||
| 2017 | |||
| 2018 | # Passing and returning non-const references
|
||
| 2019 | def testNonConstReference(self): |
||
| 2020 | # Passing a non-const reference as function parameter is forbidden.
|
||
| 2021 | operand_error_message = ('Is this a non-const reference? '
|
||
| 2022 | 'If so, make const or use a pointer: %s'
|
||
| 2023 | ' [runtime/references] [2]')
|
||
| 2024 | # Warn of use of a non-const reference in operators and functions
|
||
| 2025 | self.TestLint('bool operator>(Foo& s, Foo& f);', |
||
| 2026 | [operand_error_message % 'Foo& s',
|
||
| 2027 | operand_error_message % 'Foo& f'])
|
||
| 2028 | self.TestLint('bool operator+(Foo& s, Foo& f);', |
||
| 2029 | [operand_error_message % 'Foo& s',
|
||
| 2030 | operand_error_message % 'Foo& f'])
|
||
| 2031 | self.TestLint('int len(Foo& s);', operand_error_message % 'Foo& s') |
||
| 2032 | # Allow use of non-const references in a few specific cases
|
||
| 2033 | self.TestLint('stream& operator>>(stream& s, Foo& f);', '') |
||
| 2034 | self.TestLint('stream& operator<<(stream& s, Foo& f);', '') |
||
| 2035 | self.TestLint('void swap(Bar& a, Bar& b);', '') |
||
| 2036 | # Returning a non-const reference from a function is OK.
|
||
| 2037 | self.TestLint('int& g();', '') |
||
| 2038 | # Passing a const reference to a struct (using the struct keyword) is OK.
|
||
| 2039 | self.TestLint('void foo(const struct tm& tm);', '') |
||
| 2040 | # Passing a const reference to a typename is OK.
|
||
| 2041 | self.TestLint('void foo(const typename tm& tm);', '') |
||
| 2042 | # Const reference to a pointer type is OK.
|
||
| 2043 | self.TestLint('void foo(const Bar* const& p) {', '') |
||
| 2044 | self.TestLint('void foo(Bar const* const& p) {', '') |
||
| 2045 | self.TestLint('void foo(Bar* const& p) {', '') |
||
| 2046 | # Const reference to a templated type is OK.
|
||
| 2047 | self.TestLint('void foo(const std::vector<std::string>& v);', '') |
||
| 2048 | # Non-const reference to a pointer type is not OK.
|
||
| 2049 | self.TestLint('void foo(Bar*& p);', |
||
| 2050 | operand_error_message % 'Bar*& p')
|
||
| 2051 | self.TestLint('void foo(const Bar*& p);', |
||
| 2052 | operand_error_message % 'const Bar*& p')
|
||
| 2053 | self.TestLint('void foo(Bar const*& p);', |
||
| 2054 | operand_error_message % 'Bar const*& p')
|
||
| 2055 | self.TestLint('void foo(struct Bar*& p);', |
||
| 2056 | operand_error_message % 'struct Bar*& p')
|
||
| 2057 | self.TestLint('void foo(const struct Bar*& p);', |
||
| 2058 | operand_error_message % 'const struct Bar*& p')
|
||
| 2059 | self.TestLint('void foo(struct Bar const*& p);', |
||
| 2060 | operand_error_message % 'struct Bar const*& p')
|
||
| 2061 | # Non-const reference to a templated type is not OK.
|
||
| 2062 | self.TestLint('void foo(std::vector<int>& p);', |
||
| 2063 | operand_error_message % 'std::vector<int>& p')
|
||
| 2064 | # Returning an address of something is not prohibited.
|
||
| 2065 | self.TestLint('return &something;', '') |
||
| 2066 | self.TestLint('if (condition) {return &something; }', '') |
||
| 2067 | self.TestLint('if (condition) return &something;', '') |
||
| 2068 | self.TestLint('if (condition) address = &something;', '') |
||
| 2069 | self.TestLint('if (condition) result = lhs&rhs;', '') |
||
| 2070 | self.TestLint('if (condition) result = lhs & rhs;', '') |
||
| 2071 | self.TestLint('a = (b+c) * sizeof &f;', '') |
||
| 2072 | self.TestLint('a = MySize(b) * sizeof &f;', '') |
||
| 2073 | # We don't get confused by C++11 range-based for loops.
|
||
| 2074 | self.TestLint('for (const string& s : c)', '') |
||
| 2075 | self.TestLint('for (auto& r : c)', '') |
||
| 2076 | self.TestLint('for (typename Type& a : b)', '') |
||
| 2077 | # We don't get confused by some other uses of '&'.
|
||
| 2078 | self.TestLint('T& operator=(const T& t);', '') |
||
| 2079 | self.TestLint('int g() { return (a & b); }', '') |
||
| 2080 | self.TestLint('T& r = (T&)*(vp());', '') |
||
| 2081 | self.TestLint('T& r = v', '') |
||
| 2082 | self.TestLint('static_assert((kBits & kMask) == 0, "text");', '') |
||
| 2083 | self.TestLint('COMPILE_ASSERT((kBits & kMask) == 0, text);', '') |
||
| 2084 | # Spaces before template arguments. This is poor style, but
|
||
| 2085 | # happens 0.15% of the time.
|
||
| 2086 | self.TestLint('void Func(const vector <int> &const_x, ' |
||
| 2087 | 'vector <int> &nonconst_x) {',
|
||
| 2088 | operand_error_message % 'vector<int> &nonconst_x')
|
||
| 2089 | |||
| 2090 | # Derived member functions are spared from override check
|
||
| 2091 | self.TestLint('void Func(X& x);', operand_error_message % 'X& x') |
||
| 2092 | self.TestLint('void Func(X& x) {}', operand_error_message % 'X& x') |
||
| 2093 | self.TestLint('void Func(X& x) override;', '') |
||
| 2094 | self.TestLint('void Func(X& x) override {', '') |
||
| 2095 | self.TestLint('void Func(X& x) const override;', '') |
||
| 2096 | self.TestLint('void Func(X& x) const override {', '') |
||
| 2097 | |||
| 2098 | # Don't warn on out-of-line method definitions.
|
||
| 2099 | self.TestLint('void NS::Func(X& x) {', '') |
||
| 2100 | error_collector = ErrorCollector(self.assert_)
|
||
| 2101 | cpplint.ProcessFileData( |
||
| 2102 | 'foo.cc', 'cc', |
||
| 2103 | ['// Copyright 2014 Your Company. All Rights Reserved.',
|
||
| 2104 | 'void a::b() {}',
|
||
| 2105 | 'void f(int& q) {}',
|
||
| 2106 | ''],
|
||
| 2107 | error_collector) |
||
| 2108 | self.assertEquals(
|
||
| 2109 | operand_error_message % 'int& q',
|
||
| 2110 | error_collector.Results()) |
||
| 2111 | |||
| 2112 | # Other potential false positives. These need full parser
|
||
| 2113 | # state to reproduce as opposed to just TestLint.
|
||
| 2114 | error_collector = ErrorCollector(self.assert_)
|
||
| 2115 | cpplint.ProcessFileData( |
||
| 2116 | 'foo.cc', 'cc', |
||
| 2117 | ['// Copyright 2014 Your Company. All Rights Reserved.',
|
||
| 2118 | 'void swap(int &x,',
|
||
| 2119 | ' int &y) {',
|
||
| 2120 | '}',
|
||
| 2121 | 'void swap(',
|
||
| 2122 | ' sparsegroup<T, GROUP_SIZE, Alloc> &x,',
|
||
| 2123 | ' sparsegroup<T, GROUP_SIZE, Alloc> &y) {',
|
||
| 2124 | '}',
|
||
| 2125 | 'ostream& operator<<(',
|
||
| 2126 | ' ostream& out',
|
||
| 2127 | ' const dense_hash_set<Value, Hash, Equals, Alloc>& seq) {',
|
||
| 2128 | '}',
|
||
| 2129 | 'class A {',
|
||
| 2130 | ' void Function(',
|
||
| 2131 | ' string &x) override {',
|
||
| 2132 | ' }',
|
||
| 2133 | '};',
|
||
| 2134 | 'void Derived::Function(',
|
||
| 2135 | ' string &x) {',
|
||
| 2136 | '}',
|
||
| 2137 | '#define UNSUPPORTED_MASK(_mask) \\',
|
||
| 2138 | ' if (flags & _mask) { \\',
|
||
| 2139 | ' LOG(FATAL) << "Unsupported flag: " << #_mask; \\',
|
||
| 2140 | ' }',
|
||
| 2141 | 'Constructor::Constructor()',
|
||
| 2142 | ' : initializer1_(a1 & b1),',
|
||
| 2143 | ' initializer2_(a2 & b2) {',
|
||
| 2144 | '}',
|
||
| 2145 | 'Constructor::Constructor()',
|
||
| 2146 | ' : initializer1_{a3 & b3},',
|
||
| 2147 | ' initializer2_(a4 & b4) {',
|
||
| 2148 | '}',
|
||
| 2149 | 'Constructor::Constructor()',
|
||
| 2150 | ' : initializer1_{a5 & b5},',
|
||
| 2151 | ' initializer2_(a6 & b6) {}',
|
||
| 2152 | ''],
|
||
| 2153 | error_collector) |
||
| 2154 | self.assertEquals('', error_collector.Results()) |
||
| 2155 | |||
| 2156 | # Multi-line references
|
||
| 2157 | error_collector = ErrorCollector(self.assert_)
|
||
| 2158 | cpplint.ProcessFileData( |
||
| 2159 | 'foo.cc', 'cc', |
||
| 2160 | ['// Copyright 2014 Your Company. All Rights Reserved.',
|
||
| 2161 | 'void Func(const Outer::',
|
||
| 2162 | ' Inner& const_x,',
|
||
| 2163 | ' const Outer',
|
||
| 2164 | ' ::Inner& const_y,',
|
||
| 2165 | ' const Outer<',
|
||
| 2166 | ' int>::Inner& const_z,',
|
||
| 2167 | ' Outer::',
|
||
| 2168 | ' Inner& nonconst_x,',
|
||
| 2169 | ' Outer',
|
||
| 2170 | ' ::Inner& nonconst_y,',
|
||
| 2171 | ' Outer<',
|
||
| 2172 | ' int>::Inner& nonconst_z) {',
|
||
| 2173 | '}',
|
||
| 2174 | ''],
|
||
| 2175 | error_collector) |
||
| 2176 | self.assertEquals(
|
||
| 2177 | [operand_error_message % 'Outer::Inner& nonconst_x',
|
||
| 2178 | operand_error_message % 'Outer::Inner& nonconst_y',
|
||
| 2179 | operand_error_message % 'Outer<int>::Inner& nonconst_z'],
|
||
| 2180 | error_collector.Results()) |
||
| 2181 | |||
| 2182 | # A peculiar false positive due to bad template argument parsing
|
||
| 2183 | error_collector = ErrorCollector(self.assert_)
|
||
| 2184 | cpplint.ProcessFileData( |
||
| 2185 | 'foo.cc', 'cc', |
||
| 2186 | ['// Copyright 2014 Your Company. All Rights Reserved.',
|
||
| 2187 | 'inline RCULocked<X>::ReadPtr::ReadPtr(const RCULocked* rcu) {',
|
||
| 2188 | ' DCHECK(!(data & kFlagMask)) << "Error";',
|
||
| 2189 | '}',
|
||
| 2190 | '',
|
||
| 2191 | 'RCULocked<X>::WritePtr::WritePtr(RCULocked* rcu)',
|
||
| 2192 | ' : lock_(&rcu_->mutex_) {',
|
||
| 2193 | '}',
|
||
| 2194 | ''],
|
||
| 2195 | error_collector.Results()) |
||
| 2196 | self.assertEquals('', error_collector.Results()) |
||
| 2197 | |||
| 2198 | def testBraceAtBeginOfLine(self): |
||
| 2199 | self.TestLint('{', |
||
| 2200 | '{ should almost always be at the end of the previous line'
|
||
| 2201 | ' [whitespace/braces] [4]')
|
||
| 2202 | |||
| 2203 | error_collector = ErrorCollector(self.assert_)
|
||
| 2204 | cpplint.ProcessFileData('foo.cc', 'cc', |
||
| 2205 | ['int function()',
|
||
| 2206 | '{', # warning here |
||
| 2207 | ' MutexLock l(&mu);',
|
||
| 2208 | '}',
|
||
| 2209 | 'int variable;'
|
||
| 2210 | '{', # no warning |
||
| 2211 | ' MutexLock l(&mu);',
|
||
| 2212 | '}',
|
||
| 2213 | 'MyType m = {',
|
||
| 2214 | ' {value1, value2},',
|
||
| 2215 | ' {', # no warning |
||
| 2216 | ' loooong_value1, looooong_value2',
|
||
| 2217 | ' }',
|
||
| 2218 | '};',
|
||
| 2219 | '#if PREPROCESSOR',
|
||
| 2220 | '{', # no warning |
||
| 2221 | ' MutexLock l(&mu);',
|
||
| 2222 | '}',
|
||
| 2223 | '#endif'],
|
||
| 2224 | error_collector) |
||
| 2225 | self.assertEquals(1, error_collector.Results().count( |
||
| 2226 | '{ should almost always be at the end of the previous line'
|
||
| 2227 | ' [whitespace/braces] [4]'))
|
||
| 2228 | |||
| 2229 | self.TestMultiLineLint(
|
||
| 2230 | """
|
||
| 2231 | foo(
|
||
| 2232 | {
|
||
| 2233 | loooooooooooooooong_value,
|
||
| 2234 | });""",
|
||
| 2235 | '')
|
||
| 2236 | |||
| 2237 | def testMismatchingSpacesInParens(self): |
||
| 2238 | self.TestLint('if (foo ) {', 'Mismatching spaces inside () in if' |
||
| 2239 | ' [whitespace/parens] [5]')
|
||
| 2240 | self.TestLint('switch ( foo) {', 'Mismatching spaces inside () in switch' |
||
| 2241 | ' [whitespace/parens] [5]')
|
||
| 2242 | self.TestLint('for (foo; ba; bar ) {', 'Mismatching spaces inside () in for' |
||
| 2243 | ' [whitespace/parens] [5]')
|
||
| 2244 | self.TestLint('for (; foo; bar) {', '') |
||
| 2245 | self.TestLint('for ( ; foo; bar) {', '') |
||
| 2246 | self.TestLint('for ( ; foo; bar ) {', '') |
||
| 2247 | self.TestLint('for (foo; bar; ) {', '') |
||
| 2248 | self.TestLint('while ( foo ) {', 'Should have zero or one spaces inside' |
||
| 2249 | ' ( and ) in while [whitespace/parens] [5]')
|
||
| 2250 | |||
| 2251 | def testSpacingForFncall(self): |
||
| 2252 | self.TestLint('if (foo) {', '') |
||
| 2253 | self.TestLint('for (foo; bar; baz) {', '') |
||
| 2254 | self.TestLint('for (;;) {', '') |
||
| 2255 | # Space should be allowed in placement new operators.
|
||
| 2256 | self.TestLint('Something* p = new (place) Something();', '') |
||
| 2257 | # Test that there is no warning when increment statement is empty.
|
||
| 2258 | self.TestLint('for (foo; baz;) {', '') |
||
| 2259 | self.TestLint('for (foo;bar;baz) {', 'Missing space after ;' |
||
| 2260 | ' [whitespace/semicolon] [3]')
|
||
| 2261 | # we don't warn about this semicolon, at least for now
|
||
| 2262 | self.TestLint('if (condition) {return &something; }', |
||
| 2263 | '')
|
||
| 2264 | # seen in some macros
|
||
| 2265 | self.TestLint('DoSth();\\', '') |
||
| 2266 | # Test that there is no warning about semicolon here.
|
||
| 2267 | self.TestLint('abc;// this is abc', |
||
| 2268 | 'At least two spaces is best between code'
|
||
| 2269 | ' and comments [whitespace/comments] [2]')
|
||
| 2270 | self.TestLint('while (foo) {', '') |
||
| 2271 | self.TestLint('switch (foo) {', '') |
||
| 2272 | self.TestLint('foo( bar)', 'Extra space after ( in function call' |
||
| 2273 | ' [whitespace/parens] [4]')
|
||
| 2274 | self.TestLint('foo( // comment', '') |
||
| 2275 | self.TestLint('foo( // comment', |
||
| 2276 | 'At least two spaces is best between code'
|
||
| 2277 | ' and comments [whitespace/comments] [2]')
|
||
| 2278 | self.TestLint('foobar( \\', '') |
||
| 2279 | self.TestLint('foobar( \\', '') |
||
| 2280 | self.TestLint('( a + b)', 'Extra space after (' |
||
| 2281 | ' [whitespace/parens] [2]')
|
||
| 2282 | self.TestLint('((a+b))', '') |
||
| 2283 | self.TestLint('foo (foo)', 'Extra space before ( in function call' |
||
| 2284 | ' [whitespace/parens] [4]')
|
||
| 2285 | self.TestLint('} catch (const Foo& ex) {', '') |
||
| 2286 | self.TestLint('case (42):', '') |
||
| 2287 | self.TestLint('typedef foo (*foo)(foo)', '') |
||
| 2288 | self.TestLint('typedef foo (*foo12bar_)(foo)', '') |
||
| 2289 | self.TestLint('typedef foo (Foo::*bar)(foo)', '') |
||
| 2290 | self.TestLint('using foo = type (Foo::*bar)(foo)', '') |
||
| 2291 | self.TestLint('using foo = type (Foo::*bar)(', '') |
||
| 2292 | self.TestLint('using foo = type (Foo::*)(', '') |
||
| 2293 | self.TestLint('foo (Foo::*bar)(', '') |
||
| 2294 | self.TestLint('foo (x::y::*z)(', '') |
||
| 2295 | self.TestLint('foo (Foo::bar)(', |
||
| 2296 | 'Extra space before ( in function call'
|
||
| 2297 | ' [whitespace/parens] [4]')
|
||
| 2298 | self.TestLint('foo (*bar)(', '') |
||
| 2299 | self.TestLint('typedef foo (Foo::*bar)(', '') |
||
| 2300 | self.TestLint('(foo)(bar)', '') |
||
| 2301 | self.TestLint('Foo (*foo)(bar)', '') |
||
| 2302 | self.TestLint('Foo (*foo)(Bar bar,', '') |
||
| 2303 | self.TestLint('char (*p)[sizeof(foo)] = &foo', '') |
||
| 2304 | self.TestLint('char (&ref)[sizeof(foo)] = &foo', '') |
||
| 2305 | self.TestLint('const char32 (*table[])[6];', '') |
||
| 2306 | # The sizeof operator is often written as if it were a function call, with
|
||
| 2307 | # an opening parenthesis directly following the operator name, but it can
|
||
| 2308 | # also be written like any other operator, with a space following the
|
||
| 2309 | # operator name, and the argument optionally in parentheses.
|
||
| 2310 | self.TestLint('sizeof(foo)', '') |
||
| 2311 | self.TestLint('sizeof foo', '') |
||
| 2312 | self.TestLint('sizeof (foo)', '') |
||
| 2313 | |||
| 2314 | def testSpacingBeforeBraces(self): |
||
| 2315 | self.TestLint('if (foo){', 'Missing space before {' |
||
| 2316 | ' [whitespace/braces] [5]')
|
||
| 2317 | self.TestLint('for{', 'Missing space before {' |
||
| 2318 | ' [whitespace/braces] [5]')
|
||
| 2319 | self.TestLint('for {', '') |
||
| 2320 | self.TestLint('EXPECT_DEBUG_DEATH({', '') |
||
| 2321 | self.TestLint('std::is_convertible<A, B>{}', '') |
||
| 2322 | |||
| 2323 | def testSemiColonAfterBraces(self): |
||
| 2324 | self.TestLint('if (cond) {};', |
||
| 2325 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2326 | self.TestLint('void Func() {};', |
||
| 2327 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2328 | self.TestLint('void Func() const {};', |
||
| 2329 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2330 | self.TestLint('class X {};', '') |
||
| 2331 | for keyword in ['struct', 'union']: |
||
| 2332 | for align in ['', ' alignas(16)']: |
||
| 2333 | for typename in ['', ' X']: |
||
| 2334 | for identifier in ['', ' x']: |
||
| 2335 | self.TestLint(keyword + align + typename + ' {}' + identifier + ';', |
||
| 2336 | '')
|
||
| 2337 | |||
| 2338 | self.TestLint('class X : public Y {};', '') |
||
| 2339 | self.TestLint('class X : public MACRO() {};', '') |
||
| 2340 | self.TestLint('DEFINE_FACADE(PCQueue::Watcher, PCQueue) {};', '') |
||
| 2341 | self.TestLint('VCLASS(XfaTest, XfaContextTest) {};', '') |
||
| 2342 | self.TestLint('TEST(TestCase, TestName) {};', |
||
| 2343 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2344 | self.TestLint('TEST_F(TestCase, TestName) {};', |
||
| 2345 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2346 | |||
| 2347 | self.TestLint('file_tocs_[i] = (FileToc) {a, b, c};', '') |
||
| 2348 | self.TestMultiLineLint('class X : public Y,\npublic Z {};', '') |
||
| 2349 | |||
| 2350 | def testLambda(self): |
||
| 2351 | self.TestLint('auto x = []() {};', '') |
||
| 2352 | self.TestLint('return []() {};', '') |
||
| 2353 | self.TestMultiLineLint('auto x = []() {\n};\n', '') |
||
| 2354 | self.TestLint('int operator[](int x) {};', |
||
| 2355 | 'You don\'t need a ; after a } [readability/braces] [4]')
|
||
| 2356 | |||
| 2357 | self.TestMultiLineLint('auto x = [&a,\nb]() {};', '') |
||
| 2358 | self.TestMultiLineLint('auto x = [&a,\nb]\n() {};', '') |
||
| 2359 | self.TestMultiLineLint('auto x = [&a,\n' |
||
| 2360 | ' b](\n'
|
||
| 2361 | ' int a,\n'
|
||
| 2362 | ' int b) {\n'
|
||
| 2363 | ' return a +\n'
|
||
| 2364 | ' b;\n'
|
||
| 2365 | '};\n',
|
||
| 2366 | '')
|
||
| 2367 | |||
| 2368 | for lambda_with_default_capture in ('void f() { [=]{}; }', |
||
| 2369 | 'void f() { [=](int i) {}; }',
|
||
| 2370 | 'void f() { [=, &x]{}; }',
|
||
| 2371 | 'void f() { [&]{}; }',
|
||
| 2372 | 'void f() { [ & ]{}; }',
|
||
| 2373 | 'void f() { [&, y]{}; }'):
|
||
| 2374 | self.TestLint(lambda_with_default_capture,
|
||
| 2375 | 'Default lambda captures are an unapproved C++ feature. '
|
||
| 2376 | '[build/c++11] [4]')
|
||
| 2377 | |||
| 2378 | # "[&...]" isn't necessarily a default capture, though "[=...]" always is.
|
||
| 2379 | self.TestLint('void f() { [&variable]{}; }', '') |
||
| 2380 | |||
| 2381 | # Avoid false positives with operator[]
|
||
| 2382 | self.TestLint('table_to_children[&*table].push_back(dependent);', '') |
||
| 2383 | |||
| 2384 | def testBraceInitializerList(self): |
||
| 2385 | self.TestLint('MyStruct p = {1, 2};', '') |
||
| 2386 | self.TestLint('MyStruct p{1, 2};', '') |
||
| 2387 | self.TestLint('vector<int> p = {1, 2};', '') |
||
| 2388 | self.TestLint('vector<int> p{1, 2};', '') |
||
| 2389 | self.TestLint('x = vector<int>{1, 2};', '') |
||
| 2390 | self.TestLint('x = (struct in_addr){ 0 };', '') |
||
| 2391 | self.TestLint('Func(vector<int>{1, 2})', '') |
||
| 2392 | self.TestLint('Func((struct in_addr){ 0 })', '') |
||
| 2393 | self.TestLint('Func(vector<int>{1, 2}, 3)', '') |
||
| 2394 | self.TestLint('Func((struct in_addr){ 0 }, 3)', '') |
||
| 2395 | self.TestLint('LOG(INFO) << char{7};', '') |
||
| 2396 | self.TestLint('LOG(INFO) << char{7} << "!";', '') |
||
| 2397 | self.TestLint('int p[2] = {1, 2};', '') |
||
| 2398 | self.TestLint('return {1, 2};', '') |
||
| 2399 | self.TestLint('std::unique_ptr<Foo> foo{new Foo{}};', '') |
||
| 2400 | self.TestLint('auto foo = std::unique_ptr<Foo>{new Foo{}};', '') |
||
| 2401 | self.TestLint('static_assert(Max7String{}.IsValid(), "");', '') |
||
| 2402 | self.TestLint('map_of_pairs[{1, 2}] = 3;', '') |
||
| 2403 | self.TestLint('ItemView{has_offer() ? new Offer{offer()} : nullptr', '') |
||
| 2404 | self.TestLint('template <class T, EnableIf<::std::is_const<T>{}> = 0>', '') |
||
| 2405 | |||
| 2406 | self.TestMultiLineLint('std::unique_ptr<Foo> foo{\n' |
||
| 2407 | ' new Foo{}\n'
|
||
| 2408 | '};\n', '') |
||
| 2409 | self.TestMultiLineLint('std::unique_ptr<Foo> foo{\n' |
||
| 2410 | ' new Foo{\n'
|
||
| 2411 | ' new Bar{}\n'
|
||
| 2412 | ' }\n'
|
||
| 2413 | '};\n', '') |
||
| 2414 | self.TestMultiLineLint('if (true) {\n' |
||
| 2415 | ' if (false){}\n'
|
||
| 2416 | '}\n',
|
||
| 2417 | 'Missing space before { [whitespace/braces] [5]')
|
||
| 2418 | self.TestMultiLineLint('MyClass::MyClass()\n' |
||
| 2419 | ' : initializer_{\n'
|
||
| 2420 | ' Func()} {\n'
|
||
| 2421 | '}\n', '') |
||
| 2422 | |||
| 2423 | def testSpacingAroundElse(self): |
||
| 2424 | self.TestLint('}else {', 'Missing space before else' |
||
| 2425 | ' [whitespace/braces] [5]')
|
||
| 2426 | self.TestLint('} else{', 'Missing space before {' |
||
| 2427 | ' [whitespace/braces] [5]')
|
||
| 2428 | self.TestLint('} else {', '') |
||
| 2429 | self.TestLint('} else if (foo) {', '') |
||
| 2430 | |||
| 2431 | def testSpacingWithInitializerLists(self): |
||
| 2432 | self.TestLint('int v[1][3] = {{1, 2, 3}};', '') |
||
| 2433 | self.TestLint('int v[1][1] = {{0}};', '') |
||
| 2434 | |||
| 2435 | def testSpacingForBinaryOps(self): |
||
| 2436 | self.TestLint('if (foo||bar) {', 'Missing spaces around ||' |
||
| 2437 | ' [whitespace/operators] [3]')
|
||
| 2438 | self.TestLint('if (foo<=bar) {', 'Missing spaces around <=' |
||
| 2439 | ' [whitespace/operators] [3]')
|
||
| 2440 | self.TestLint('if (foo<bar) {', 'Missing spaces around <' |
||
| 2441 | ' [whitespace/operators] [3]')
|
||
| 2442 | self.TestLint('if (foo>bar) {', 'Missing spaces around >' |
||
| 2443 | ' [whitespace/operators] [3]')
|
||
| 2444 | self.TestLint('if (foo<bar->baz) {', 'Missing spaces around <' |
||
| 2445 | ' [whitespace/operators] [3]')
|
||
| 2446 | self.TestLint('if (foo<bar->bar) {', 'Missing spaces around <' |
||
| 2447 | ' [whitespace/operators] [3]')
|
||
| 2448 | self.TestLint('template<typename T = double>', '') |
||
| 2449 | self.TestLint('std::unique_ptr<No<Spaces>>', '') |
||
| 2450 | self.TestLint('typedef hash_map<Foo, Bar>', '') |
||
| 2451 | self.TestLint('10<<20', '') |
||
| 2452 | self.TestLint('10<<a', |
||
| 2453 | 'Missing spaces around << [whitespace/operators] [3]')
|
||
| 2454 | self.TestLint('a<<20', |
||
| 2455 | 'Missing spaces around << [whitespace/operators] [3]')
|
||
| 2456 | self.TestLint('a<<b', |
||
| 2457 | 'Missing spaces around << [whitespace/operators] [3]')
|
||
| 2458 | self.TestLint('10ULL<<20', '') |
||
| 2459 | self.TestLint('a>>b', |
||
| 2460 | 'Missing spaces around >> [whitespace/operators] [3]')
|
||
| 2461 | self.TestLint('10>>b', |
||
| 2462 | 'Missing spaces around >> [whitespace/operators] [3]')
|
||
| 2463 | self.TestLint('LOG(ERROR)<<*foo', |
||
| 2464 | 'Missing spaces around << [whitespace/operators] [3]')
|
||
| 2465 | self.TestLint('LOG(ERROR)<<&foo', |
||
| 2466 | 'Missing spaces around << [whitespace/operators] [3]')
|
||
| 2467 | self.TestLint('StringCoder<vector<string>>::ToString()', '') |
||
| 2468 | self.TestLint('map<pair<int, int>, map<int, int>>::iterator', '') |
||
| 2469 | self.TestLint('func<int, pair<int, pair<int, int>>>()', '') |
||
| 2470 | self.TestLint('MACRO1(list<list<int>>)', '') |
||
| 2471 | self.TestLint('MACRO2(list<list<int>>, 42)', '') |
||
| 2472 | self.TestLint('void DoFoo(const set<vector<string>>& arg1);', '') |
||
| 2473 | self.TestLint('void SetFoo(set<vector<string>>* arg1);', '') |
||
| 2474 | self.TestLint('foo = new set<vector<string>>;', '') |
||
| 2475 | self.TestLint('reinterpret_cast<set<vector<string>>*>(a);', '') |
||
| 2476 | self.TestLint('MACRO(<<)', '') |
||
| 2477 | self.TestLint('MACRO(<<, arg)', '') |
||
| 2478 | self.TestLint('MACRO(<<=)', '') |
||
| 2479 | self.TestLint('MACRO(<<=, arg)', '') |
||
| 2480 | |||
| 2481 | self.TestLint('using Vector3<T>::operator==;', '') |
||
| 2482 | self.TestLint('using Vector3<T>::operator!=;', '') |
||
| 2483 | |||
| 2484 | def testRvalueReference(self): |
||
| 2485 | space_error = 'Missing spaces around && [whitespace/operators] [3]'
|
||
| 2486 | rvalue_error = ('RValue references are an unapproved C++ feature.'
|
||
| 2487 | ' [build/c++11] [3]')
|
||
| 2488 | |||
| 2489 | # Places where lack of space are allowed
|
||
| 2490 | self.TestLint('DEFINE_BINARY_OPERATOR(&&)', '') |
||
| 2491 | self.TestLint('bool operator&&(A b) {}', '') |
||
| 2492 | self.TestLint('bool operator&&(A b) {', '') |
||
| 2493 | self.TestLint('bool operator&&(A b);', '') |
||
| 2494 | |||
| 2495 | # Assignment expressions
|
||
| 2496 | self.TestLint('a = b && c;', '') |
||
| 2497 | self.TestLint('a = b&& c;', space_error) |
||
| 2498 | self.TestLint('a = b &&c;', space_error) |
||
| 2499 | self.TestLint('a = (b&& c);', space_error) |
||
| 2500 | self.TestLint('a = (b &&c);', space_error) |
||
| 2501 | self.TestLint('a&& b = c;', rvalue_error) |
||
| 2502 | self.TestLint('a<b>&& c = d;', rvalue_error) |
||
| 2503 | self.TestLint('auto&& a = b;', rvalue_error) |
||
| 2504 | self.TestLint('const a&& b = c;', rvalue_error) |
||
| 2505 | self.TestLint('struct a&& b = c;', rvalue_error) |
||
| 2506 | self.TestLint('decltype(a)&& b = c;', rvalue_error) |
||
| 2507 | |||
| 2508 | # Cast expressions
|
||
| 2509 | self.TestLint('a = const_cast<b&&>(c);', rvalue_error) |
||
| 2510 | self.TestLint('a = const_cast<const b&&>(c);', rvalue_error) |
||
| 2511 | self.TestLint('a = static_cast<b&&>(c);', rvalue_error) |
||
| 2512 | self.TestLint('a = static_cast<const b&&>(c);', rvalue_error) |
||
| 2513 | self.TestLint('a = dynamic_cast<b&&>(c);', rvalue_error) |
||
| 2514 | self.TestLint('a = dynamic_cast<const b&&>(c);', rvalue_error) |
||
| 2515 | self.TestLint('a = reinterpret_cast<b&&>(c);', rvalue_error) |
||
| 2516 | self.TestLint('a = reinterpret_cast<const b&&>(c);', rvalue_error) |
||
| 2517 | self.TestLint('a = cast < b&& c;', space_error) |
||
| 2518 | |||
| 2519 | # Function parameters
|
||
| 2520 | for indent in ['', ' ']: |
||
| 2521 | for head in ['void Func', 'vector<int> Func', 'vector<int>\nFunc', |
||
| 2522 | 'inline void Func',
|
||
| 2523 | 'Constructor', 'Constructor::Constructor', |
||
| 2524 | 'operator=', 'operator =', 'operator = ']: |
||
| 2525 | for body in [' {}', ' {', ';']: |
||
| 2526 | self.TestMultiLineLint(indent + head + '(A&& b)' + body, rvalue_error) |
||
| 2527 | self.TestMultiLineLint(indent + head + '(A &&b)' + body, rvalue_error) |
||
| 2528 | self.TestMultiLineLint(indent + head + '(A&&... b)' + body, |
||
| 2529 | rvalue_error) |
||
| 2530 | self.TestMultiLineLint(indent + head + '(A<B>&& c)' + body, |
||
| 2531 | rvalue_error) |
||
| 2532 | self.TestMultiLineLint(indent + head + '(A<B> &&c)' + body, |
||
| 2533 | rvalue_error) |
||
| 2534 | |||
| 2535 | # Function templates
|
||
| 2536 | self.TestLint('std::conditional<A, B&, C&&>::type', rvalue_error) |
||
| 2537 | self.TestLint('std::conditional<A, B&&, C&>::type', rvalue_error) |
||
| 2538 | |||
| 2539 | # Template functions
|
||
| 2540 | self.TestLint('template <typename T> R&& F()', rvalue_error) |
||
| 2541 | self.TestLint('template <typename T> R&& F() {', rvalue_error) |
||
| 2542 | self.TestMultiLineLint('template <typename T>\nR&& F()', rvalue_error) |
||
| 2543 | self.TestMultiLineLint('template <typename T>\nR&& F() {', rvalue_error) |
||
| 2544 | self.TestLint('template <typename T> void F(T a, R&& b)', rvalue_error) |
||
| 2545 | self.TestLint('template <typename T> void F(T a, R &&b)', rvalue_error) |
||
| 2546 | self.TestLint('template <typename T> void F(T a, R&& b) {', rvalue_error) |
||
| 2547 | |||
| 2548 | # For loops
|
||
| 2549 | self.TestLint('for (a&& b;;)', rvalue_error) |
||
| 2550 | self.TestLint('for (a&& b;;) {', rvalue_error) |
||
| 2551 | self.TestLint('for (; a&& b;)', space_error) |
||
| 2552 | self.TestLint('for (; a&& b;) {', space_error) |
||
| 2553 | |||
| 2554 | # Constructors
|
||
| 2555 | self.TestLint('A(a&& b)', rvalue_error) |
||
| 2556 | self.TestLint('explicit A(a&& b)', rvalue_error) |
||
| 2557 | self.TestLint('A(a b) : c(d&& e)', space_error) |
||
| 2558 | self.TestLint('A(a b) : c(), d(e&& f)', space_error) |
||
| 2559 | |||
| 2560 | def testAllowedRvalueReference(self): |
||
| 2561 | # Verify that RValue reference warnings for a line range can be silenced
|
||
| 2562 | error_collector = ErrorCollector(self.assert_)
|
||
| 2563 | cpplint.ProcessFileData('foo.cc', 'cc', |
||
| 2564 | ['// Copyright 2014 Your Company.',
|
||
| 2565 | 'GOOGLE_ALLOW_RVALUE_REFERENCES_PUSH |