| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
"""Unit test for cpplint.py.""" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
import codecs |
| 36 |
import os |
| 37 |
import platform |
| 38 |
import random |
| 39 |
import re |
| 40 |
import shutil |
| 41 |
import subprocess |
| 42 |
import sys |
| 43 |
import tempfile |
| 44 |
|
| 45 |
import pytest |
| 46 |
|
| 47 |
import cpplint |
| 48 |
|
| 49 |
|
| 50 |
def codecs_latin_encode(x): |
| 51 |
return codecs.latin_1_encode(x)[0] |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
class ErrorCollector: |
| 58 |
|
| 59 |
_ERROR_CATEGORIES = cpplint._ERROR_CATEGORIES |
| 60 |
_SEEN_ERROR_CATEGORIES: set[str] = set() |
| 61 |
|
| 62 |
def __init__(self, assert_fn): |
| 63 |
"""assert_fn: a function to call when we notice a problem.""" |
| 64 |
self._assert_fn = assert_fn |
| 65 |
self._errors = [] |
| 66 |
cpplint.ResetNolintSuppressions() |
| 67 |
|
| 68 |
def __call__(self, filename, linenum, category, confidence, message): |
| 69 |
self._assert_fn( |
| 70 |
category in self._ERROR_CATEGORIES, |
| 71 |
'Message "%s" has category "%s",' |
| 72 |
" which is not in _ERROR_CATEGORIES" % (message, category), |
| 73 |
) |
| 74 |
self._SEEN_ERROR_CATEGORIES.add(category) |
| 75 |
if cpplint._ShouldPrintError(category, confidence, filename, linenum): |
| 76 |
self._errors.append("%s [%s] [%d]" % (message, category, confidence)) |
| 77 |
|
| 78 |
def Results(self): |
| 79 |
if len(self._errors) < 2: |
| 80 |
return "".join(self._errors) |
| 81 |
return self._errors |
| 82 |
|
| 83 |
def ResultList(self): |
| 84 |
return self._errors |
| 85 |
|
| 86 |
def VerifyAllCategoriesAreSeen(self): |
| 87 |
"""Fails if there's a category in _ERROR_CATEGORIES~_SEEN_ERROR_CATEGORIES. |
| 88 |
|
| 89 |
This should only be called after all tests are run, so |
| 90 |
_SEEN_ERROR_CATEGORIES has had a chance to fully populate. Since |
| 91 |
this isn't called from within the normal unittest framework, we |
| 92 |
can't use the normal unittest assert macros. Instead we just exit |
| 93 |
when we see an error. Good thing this test is always run last! |
| 94 |
""" |
| 95 |
for category in self._ERROR_CATEGORIES: |
| 96 |
if category not in self._SEEN_ERROR_CATEGORIES: |
| 97 |
sys.exit('FATAL ERROR: There are no tests for category "%s"' % category) |
| 98 |
|
| 99 |
def RemoveIfPresent(self, substr): |
| 100 |
for index, error in enumerate(self._errors): |
| 101 |
if error.find(substr) != -1: |
| 102 |
self._errors = self._errors[0:index] + self._errors[(index + 1) :] |
| 103 |
break |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
class MockIo: |
| 109 |
def __init__(self, mock_file): |
| 110 |
|
| 111 |
class EnterableList(list): |
| 112 |
def __enter__(self): |
| 113 |
return self |
| 114 |
|
| 115 |
def __exit__(self, type, value, tb): |
| 116 |
return self |
| 117 |
|
| 118 |
self.mock_file = EnterableList(mock_file) |
| 119 |
|
| 120 |
def open( |
| 121 |
self, |
| 122 |
unused_filename, |
| 123 |
unused_mode, |
| 124 |
unused_encoding, |
| 125 |
_, |
| 126 |
): |
| 127 |
return self.mock_file |
| 128 |
|
| 129 |
|
| 130 |
class CpplintTestBase: |
| 131 |
"""Provides some useful helper functions for cpplint tests.""" |
| 132 |
|
| 133 |
def setUp(self): |
| 134 |
|
| 135 |
self.os_path_abspath_orig = os.path.abspath |
| 136 |
|
| 137 |
def tearDown(self): |
| 138 |
os.path.abspath = self.os_path_abspath_orig |
| 139 |
|
| 140 |
def assertTrue(self, condition, message=""): |
| 141 |
assert condition, message |
| 142 |
|
| 143 |
|
| 144 |
def PerformSingleLineLint(self, code): |
| 145 |
error_collector = ErrorCollector(self.assertTrue) |
| 146 |
lines = code.split("\n") |
| 147 |
cpplint.RemoveMultiLineComments("foo.h", lines, error_collector) |
| 148 |
clean_lines = cpplint.CleansedLines(lines) |
| 149 |
include_state = cpplint._IncludeState() |
| 150 |
function_state = cpplint._FunctionState() |
| 151 |
nesting_state = cpplint.NestingState() |
| 152 |
cpplint.ProcessLine( |
| 153 |
"foo.cc", |
| 154 |
"cc", |
| 155 |
clean_lines, |
| 156 |
0, |
| 157 |
include_state, |
| 158 |
function_state, |
| 159 |
nesting_state, |
| 160 |
error_collector, |
| 161 |
) |
| 162 |
|
| 163 |
|
| 164 |
error_collector.RemoveIfPresent("Lint failed to find start of function body.") |
| 165 |
return error_collector.Results() |
| 166 |
|
| 167 |
|
| 168 |
def PerformMultiLineLint(self, code): |
| 169 |
error_collector = ErrorCollector(self.assertTrue) |
| 170 |
lines = code.split("\n") |
| 171 |
cpplint.RemoveMultiLineComments("foo.h", lines, error_collector) |
| 172 |
lines = cpplint.CleansedLines(lines) |
| 173 |
nesting_state = cpplint.NestingState() |
| 174 |
for i in range(lines.NumLines()): |
| 175 |
nesting_state.Update("foo.h", lines, i, error_collector) |
| 176 |
cpplint.CheckStyle("foo.h", lines, i, "h", nesting_state, error_collector) |
| 177 |
cpplint.CheckForNonStandardConstructs("foo.h", lines, i, nesting_state, error_collector) |
| 178 |
return error_collector.Results() |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
def PerformLanguageRulesCheck(self, file_name, code): |
| 183 |
error_collector = ErrorCollector(self.assertTrue) |
| 184 |
include_state = cpplint._IncludeState() |
| 185 |
nesting_state = cpplint.NestingState() |
| 186 |
lines = code.split("\n") |
| 187 |
cpplint.RemoveMultiLineComments(file_name, lines, error_collector) |
| 188 |
lines = cpplint.CleansedLines(lines) |
| 189 |
ext = file_name[file_name.rfind(".") + 1 :] |
| 190 |
for i in range(lines.NumLines()): |
| 191 |
cpplint.CheckLanguage( |
| 192 |
file_name, lines, i, ext, include_state, nesting_state, error_collector |
| 193 |
) |
| 194 |
return error_collector.Results() |
| 195 |
|
| 196 |
def PerformFunctionLengthsCheck(self, code): |
| 197 |
"""Perform Lint function length check on block of code and return warnings. |
| 198 |
|
| 199 |
Builds up an array of lines corresponding to the code and strips comments |
| 200 |
using cpplint functions. |
| 201 |
|
| 202 |
Establishes an error collector and invokes the function length checking |
| 203 |
function following cpplint's pattern. |
| 204 |
|
| 205 |
Args: |
| 206 |
code: C++ source code expected to generate a warning message. |
| 207 |
|
| 208 |
Returns: |
| 209 |
The accumulated errors. |
| 210 |
""" |
| 211 |
file_name = "foo.cc" |
| 212 |
error_collector = ErrorCollector(self.assertTrue) |
| 213 |
function_state = cpplint._FunctionState() |
| 214 |
lines = code.split("\n") |
| 215 |
cpplint.RemoveMultiLineComments(file_name, lines, error_collector) |
| 216 |
lines = cpplint.CleansedLines(lines) |
| 217 |
for i in range(lines.NumLines()): |
| 218 |
cpplint.CheckForFunctionLengths(file_name, lines, i, function_state, error_collector) |
| 219 |
return error_collector.Results() |
| 220 |
|
| 221 |
def PerformIncludeWhatYouUse(self, code, filename="foo.h", io=codecs): |
| 222 |
|
| 223 |
error_collector = ErrorCollector(self.assertTrue) |
| 224 |
include_state = cpplint._IncludeState() |
| 225 |
nesting_state = cpplint.NestingState() |
| 226 |
lines = code.split("\n") |
| 227 |
cpplint.RemoveMultiLineComments(filename, lines, error_collector) |
| 228 |
lines = cpplint.CleansedLines(lines) |
| 229 |
for i in range(lines.NumLines()): |
| 230 |
cpplint.CheckLanguage( |
| 231 |
filename, lines, i, ".h", include_state, nesting_state, error_collector |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
cpplint.CheckForIncludeWhatYouUse(filename, lines, include_state, error_collector, io) |
| 239 |
return error_collector.Results() |
| 240 |
|
| 241 |
|
| 242 |
def TestLintContains(self, code, expected_message): |
| 243 |
assert expected_message in self.PerformSingleLineLint(code) |
| 244 |
|
| 245 |
def TestLintNotContains(self, code, expected_message): |
| 246 |
assert expected_message not in self.PerformSingleLineLint(code) |
| 247 |
|
| 248 |
|
| 249 |
def TestLint(self, code, expected_message): |
| 250 |
assert expected_message == self.PerformSingleLineLint(code) |
| 251 |
|
| 252 |
def TestMultiLineLint(self, code, expected_message): |
| 253 |
assert expected_message == self.PerformMultiLineLint(code) |
| 254 |
|
| 255 |
def TestMultiLineLintRE(self, code, expected_message_re): |
| 256 |
message = self.PerformMultiLineLint(code) |
| 257 |
assert re.search(expected_message_re, message) |
| 258 |
|
| 259 |
def TestLanguageRulesCheck(self, file_name, code, expected_message): |
| 260 |
assert expected_message == self.PerformLanguageRulesCheck(file_name, code) |
| 261 |
|
| 262 |
def TestIncludeWhatYouUse(self, code, expected_message): |
| 263 |
assert expected_message == self.PerformIncludeWhatYouUse(code) |
| 264 |
|
| 265 |
def TestBlankLinesCheck(self, lines, start_errors, end_errors): |
| 266 |
for extension in ["c", "cc", "cpp", "cxx", "c++", "cu"]: |
| 267 |
self.doTestBlankLinesCheck(lines, start_errors, end_errors, extension) |
| 268 |
|
| 269 |
def doTestBlankLinesCheck(self, lines, start_errors, end_errors, extension): |
| 270 |
error_collector = ErrorCollector(self.assertTrue) |
| 271 |
cpplint.ProcessFileData("foo." + extension, extension, lines, error_collector) |
| 272 |
assert start_errors == error_collector.Results().count( |
| 273 |
"Redundant blank line at the start of a code block should be deleted. " |
| 274 |
"[whitespace/blank_line] [2]" |
| 275 |
) |
| 276 |
assert end_errors == error_collector.Results().count( |
| 277 |
"Redundant blank line at the end of a code block should be deleted. " |
| 278 |
"[whitespace/blank_line] [3]" |
| 279 |
) |
| 280 |
|
| 281 |
|
| 282 |
class TestCpplint(CpplintTestBase): |
| 283 |
def GetNamespaceResults(self, lines): |
| 284 |
error_collector = ErrorCollector(self.assertTrue) |
| 285 |
cpplint.RemoveMultiLineComments("foo.h", lines, error_collector) |
| 286 |
lines = cpplint.CleansedLines(lines) |
| 287 |
nesting_state = cpplint.NestingState() |
| 288 |
for i in range(lines.NumLines()): |
| 289 |
nesting_state.Update("foo.h", lines, i, error_collector) |
| 290 |
cpplint.CheckForNamespaceIndentation("foo.h", nesting_state, lines, i, error_collector) |
| 291 |
|
| 292 |
return error_collector.Results() |
| 293 |
|
| 294 |
def testForwardDeclarationNamespaceIndentation(self): |
| 295 |
lines = ["namespace Test {", " class ForwardDeclaration;", "} // namespace Test"] |
| 296 |
|
| 297 |
results = self.GetNamespaceResults(lines) |
| 298 |
assert results == "Do not indent within a namespace. [whitespace/indent_namespace] [4]" |
| 299 |
|
| 300 |
def testNamespaceIndentationForClass(self): |
| 301 |
lines = [ |
| 302 |
"namespace Test {", |
| 303 |
"void foo() { }", |
| 304 |
" class Test {", |
| 305 |
" };", |
| 306 |
"} // namespace Test", |
| 307 |
] |
| 308 |
|
| 309 |
results = self.GetNamespaceResults(lines) |
| 310 |
assert results == [ |
| 311 |
"Do not indent within a namespace. [whitespace/indent_namespace] [4]", |
| 312 |
"Do not indent within a namespace. [whitespace/indent_namespace] [4]", |
| 313 |
] |
| 314 |
|
| 315 |
def testNamespaceIndentationIndentedParameter(self): |
| 316 |
lines = [ |
| 317 |
"namespace Test {", |
| 318 |
"void foo(SuperLongTypeName d = 418,", |
| 319 |
" SuperLongTypeName e = 2.71) { }", |
| 320 |
"} // namespace Test", |
| 321 |
] |
| 322 |
|
| 323 |
results = self.GetNamespaceResults(lines) |
| 324 |
assert results == "" |
| 325 |
|
| 326 |
def testNamespaceIndentationMemberInitializerList(self): |
| 327 |
lines = [ |
| 328 |
"namespace Opossum {", |
| 329 |
"", |
| 330 |
"Acme::Acme(const std::shared_ptr<const AbstractOperator> left,", |
| 331 |
" const std::shared_ptr<const AbstractOperator> nigh)", |
| 332 |
" : _left(left), _behind(nigh) {}", |
| 333 |
"", |
| 334 |
"} // namespace Opossum", |
| 335 |
] |
| 336 |
assert self.GetNamespaceResults(lines) == "" |
| 337 |
|
| 338 |
|
| 339 |
lines = [ |
| 340 |
"namespace Rosenfield {", |
| 341 |
"class Crush : public Habitual {", |
| 342 |
" public:", |
| 343 |
" Crush() : _a(1),", |
| 344 |
" _b(2)", |
| 345 |
" {}", |
| 346 |
"};", |
| 347 |
"} // namespace Rosenfield", |
| 348 |
] |
| 349 |
assert self.GetNamespaceResults(lines) == "" |
| 350 |
|
| 351 |
|
| 352 |
lines = [ |
| 353 |
"namespace Boucher {", |
| 354 |
" X::X() : _s(2) {}", |
| 355 |
" A::A() : _e(12) {", |
| 356 |
] |
| 357 |
assert self.GetNamespaceResults(lines) == [ |
| 358 |
"Do not indent within a namespace. [whitespace/indent_namespace] [4]", |
| 359 |
"Do not indent within a namespace. [whitespace/indent_namespace] [4]", |
| 360 |
] |
| 361 |
|
| 362 |
|
| 363 |
lines = [ |
| 364 |
"namespace Store {", |
| 365 |
"", |
| 366 |
" Color::Color() : my_name_is('b')", |
| 367 |
" this_is_true(true) {", |
| 368 |
] |
| 369 |
assert ( |
| 370 |
self.GetNamespaceResults(lines) |
| 371 |
== "Do not indent within a namespace. [whitespace/indent_namespace] [4]" |
| 372 |
) |
| 373 |
|
| 374 |
def testNestingInNamespace(self): |
| 375 |
lines = [ |
| 376 |
"namespace Test {", |
| 377 |
"struct OuterClass {", |
| 378 |
" struct NoFalsePositivesHere;", |
| 379 |
" struct NoFalsePositivesHere member_variable;", |
| 380 |
"};", |
| 381 |
"void foo() {", |
| 382 |
" const int no_positives_eh = 418;", |
| 383 |
"}", |
| 384 |
"} // namespace Test", |
| 385 |
] |
| 386 |
|
| 387 |
results = self.GetNamespaceResults(lines) |
| 388 |
assert results == "" |
| 389 |
|
| 390 |
|
| 391 |
def testGetLineWidth(self): |
| 392 |
assert cpplint.GetLineWidth("") == 0 |
| 393 |
assert cpplint.GetLineWidth("x" * 10) == 10 |
| 394 |
assert cpplint.GetLineWidth("都|道|府|県|支庁") == 16 |
| 395 |
assert cpplint.GetLineWidth("都|道|府|県|支庁") == 16 |
| 396 |
assert cpplint.GetLineWidth("d𝐱/dt" + "f : t ⨯ 𝐱 → ℝ" + "t ⨯ 𝐱 → ℝ") == 5 + 13 + 9 |
| 397 |
|
| 398 |
def testGetTextInside(self): |
| 399 |
assert cpplint._GetTextInside("fun()", r"fun\(") == "" |
| 400 |
assert cpplint._GetTextInside("f(x, y)", r"f\(") == "x, y" |
| 401 |
assert cpplint._GetTextInside("printf(a(), b(c()))", r"printf\(") == "a(), b(c())" |
| 402 |
assert cpplint._GetTextInside("f[x, y{}]", r"f\[") == "x, y{}" |
| 403 |
assert None is cpplint._GetTextInside("f[a, b(}]", r"f\[") |
| 404 |
assert None is cpplint._GetTextInside("f[x, y]", r"f\(") |
| 405 |
assert cpplint._GetTextInside("f(x, g(y, h(z, (a + b))))", r"g\(") == "y, h(z, (a + b))" |
| 406 |
assert cpplint._GetTextInside("f(f(f(x)))", r"f\(") == "f(f(x))" |
| 407 |
|
| 408 |
assert ( |
| 409 |
cpplint._GetTextInside("int loop(int x) {\n return loop(x);\n}\n", r"\{") |
| 410 |
== "\n return loop(x);\n" |
| 411 |
) |
| 412 |
|
| 413 |
assert ( |
| 414 |
cpplint._GetTextInside( |
| 415 |
'#include "inl.h" // skip #define\n' |
| 416 |
"#define A2(x, y) a_inl_(x, y, __LINE__)\n" |
| 417 |
'#define A(x) a_inl_(x, "", __LINE__)\n', |
| 418 |
r"^\s*#define\s*\w+\(", |
| 419 |
) |
| 420 |
== "x, y" |
| 421 |
) |
| 422 |
|
| 423 |
def testFindNextMultiLineCommentStart(self): |
| 424 |
assert cpplint.FindNextMultiLineCommentStart([""], 0) == 1 |
| 425 |
|
| 426 |
lines = ["a", "b", "/* c"] |
| 427 |
assert cpplint.FindNextMultiLineCommentStart(lines, 0) == 2 |
| 428 |
|
| 429 |
lines = ['char a[] = "/*";'] |
| 430 |
assert cpplint.FindNextMultiLineCommentStart(lines, 0) == 1 |
| 431 |
|
| 432 |
def testFindNextMultiLineCommentEnd(self): |
| 433 |
assert cpplint.FindNextMultiLineCommentEnd([""], 0) == 1 |
| 434 |
lines = ["a", "b", " c */"] |
| 435 |
assert cpplint.FindNextMultiLineCommentEnd(lines, 0) == 2 |
| 436 |
|
| 437 |
def testRemoveMultiLineCommentsFromRange(self): |
| 438 |
lines = ["a", " /* comment ", " * still comment", " comment */ ", "b"] |
| 439 |
cpplint.RemoveMultiLineCommentsFromRange(lines, 1, 4) |
| 440 |
assert lines == ["a", "/**/", "/**/", "/**/", "b"] |
| 441 |
|
| 442 |
def testSpacesAtEndOfLine(self): |
| 443 |
self.TestLint( |
| 444 |
"// Hello there ", |
| 445 |
"Line ends in whitespace. Consider deleting these extra spaces." |
| 446 |
" [whitespace/end_of_line] [4]", |
| 447 |
) |
| 448 |
|
| 449 |
|
| 450 |
def testLineLengthCheck(self): |
| 451 |
self.TestLint("// Hello", "") |
| 452 |
self.TestLint( |
| 453 |
"// x" + " x" * 40, |
| 454 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 455 |
) |
| 456 |
self.TestLint( |
| 457 |
"// x" + " x" * 50, |
| 458 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 459 |
) |
| 460 |
self.TestLint("// //some/path/to/f" + ("i" * 100) + "le", "") |
| 461 |
self.TestLint("// //some/path/to/f" + ("i" * 100) + "le", "") |
| 462 |
self.TestLint( |
| 463 |
"// //some/path/to/f" + ("i" * 50) + "le and some comments", |
| 464 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 465 |
) |
| 466 |
self.TestLint("// http://g" + ("o" * 100) + "gle.com/", "") |
| 467 |
self.TestLint("// https://g" + ("o" * 100) + "gle.com/", "") |
| 468 |
self.TestLint( |
| 469 |
"// https://g" + ("o" * 60) + "gle.com/ and some comments", |
| 470 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 471 |
) |
| 472 |
self.TestLint("// Read https://g" + ("o" * 60) + "gle.com/", "") |
| 473 |
self.TestLint("// $Id: g" + ("o" * 80) + "gle.cc#1 $", "") |
| 474 |
self.TestLint( |
| 475 |
"// $Id: g" + ("o" * 80) + "gle.cc#1", |
| 476 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 477 |
) |
| 478 |
self.TestMultiLineLint( |
| 479 |
'static const char kCStr[] = "g' + ("o" * 50) + 'gle";\n', |
| 480 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 481 |
) |
| 482 |
self.TestMultiLineLint( |
| 483 |
'static const char kRawStr[] = R"(g' + ("o" * 50) + 'gle)";\n', "" |
| 484 |
) |
| 485 |
self.TestMultiLineLint( |
| 486 |
'static const char kMultiLineRawStr[] = R"(\ng' + ("o" * 80) + 'gle\n)";', "" |
| 487 |
) |
| 488 |
self.TestMultiLineLint( |
| 489 |
"static const char kL" + ("o" * 50) + 'ngIdentifier[] = R"()";\n', |
| 490 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 491 |
) |
| 492 |
self.TestLint(" /// @copydoc " + ("o" * (cpplint._line_length * 2)), "") |
| 493 |
self.TestLint(" /// @copydetails " + ("o" * (cpplint._line_length * 2)), "") |
| 494 |
self.TestLint(" /// @copybrief " + ("o" * (cpplint._line_length * 2)), "") |
| 495 |
|
| 496 |
|
| 497 |
def testErrorSuppression(self): |
| 498 |
|
| 499 |
self.TestLint( |
| 500 |
"long a = (int64_t) 65;", |
| 501 |
[ |
| 502 |
"Using C-style cast. Use static_cast<int64_t>(...) instead" |
| 503 |
" [readability/casting] [4]", |
| 504 |
"Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]", |
| 505 |
], |
| 506 |
) |
| 507 |
|
| 508 |
self.TestLint( |
| 509 |
"long a = (int64_t) 65; // NOLINT(runtime/int)", |
| 510 |
"Using C-style cast. Use static_cast<int64_t>(...) instead [readability/casting] [4]", |
| 511 |
) |
| 512 |
|
| 513 |
self.TestLint("long a = (int64_t) 65; // NOLINT(runtime/int,readability/casting)", "") |
| 514 |
|
| 515 |
|
| 516 |
self.TestLint("long a = (int64_t) 65; // NOLINT", "") |
| 517 |
self.TestLint("long a = (int64_t) 65; // NOLINT(*)", "") |
| 518 |
|
| 519 |
|
| 520 |
error_collector = ErrorCollector(self.assertTrue) |
| 521 |
cpplint.ProcessFileData( |
| 522 |
"test.c", |
| 523 |
"c", |
| 524 |
["// Copyright 2014 Your Majesty.", "int64_t a = (int64_t) 65;", ""], |
| 525 |
error_collector, |
| 526 |
) |
| 527 |
assert error_collector.Results() == "" |
| 528 |
|
| 529 |
|
| 530 |
self.TestLint( |
| 531 |
"long a = 65; // NOLINT(foo)", |
| 532 |
[ |
| 533 |
"Unknown NOLINT error category: foo [readability/nolint] [5]", |
| 534 |
"Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]", |
| 535 |
], |
| 536 |
) |
| 537 |
|
| 538 |
self.TestLint( |
| 539 |
"long a = 65; // NOLINT(readability/casting)", |
| 540 |
"Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]", |
| 541 |
) |
| 542 |
|
| 543 |
error_collector = ErrorCollector(self.assertTrue) |
| 544 |
cpplint.ProcessFileData( |
| 545 |
"test.cc", |
| 546 |
"cc", |
| 547 |
[ |
| 548 |
"// Copyright 2014 Your Company.", |
| 549 |
"// NOLINTNEXTLINE(whitespace/line_length)", |
| 550 |
"// ./command" + (" -verbose" * 80), |
| 551 |
"", |
| 552 |
], |
| 553 |
error_collector, |
| 554 |
) |
| 555 |
assert error_collector.Results() == "" |
| 556 |
|
| 557 |
|
| 558 |
error_collector = ErrorCollector(self.assertTrue) |
| 559 |
cpplint.ProcessFileData( |
| 560 |
"test.cc", |
| 561 |
"cc", |
| 562 |
[ |
| 563 |
"// Copyright 2014 Your Company.", |
| 564 |
"// NOLINTNEXTLINE(runtime/int,readability/casting)", |
| 565 |
"long a = (int64_t) 65;", |
| 566 |
"", |
| 567 |
], |
| 568 |
error_collector, |
| 569 |
) |
| 570 |
assert error_collector.Results() == "" |
| 571 |
|
| 572 |
error_collector = ErrorCollector(self.assertTrue) |
| 573 |
cpplint.ProcessFileData( |
| 574 |
"test.h", |
| 575 |
"h", |
| 576 |
[ |
| 577 |
"// Copyright 2014 Your Company.", |
| 578 |
"// NOLINT(build/header_guard)", |
| 579 |
"int64_t a = (uint64_t) 65;", |
| 580 |
"// LINT_C_FILE", |
| 581 |
"", |
| 582 |
], |
| 583 |
error_collector, |
| 584 |
) |
| 585 |
assert error_collector.Results() == "" |
| 586 |
|
| 587 |
for modeline in [ |
| 588 |
"vi:filetype=c", |
| 589 |
"vi:sw=8 filetype=c", |
| 590 |
"vi:sw=8 filetype=c ts=8", |
| 591 |
"vi: filetype=c", |
| 592 |
"vi: sw=8 filetype=c", |
| 593 |
"vi: sw=8 filetype=c ts=8", |
| 594 |
"vim:filetype=c", |
| 595 |
"vim:sw=8 filetype=c", |
| 596 |
"vim:sw=8 filetype=c ts=8", |
| 597 |
"vim: filetype=c", |
| 598 |
"vim: sw=8 filetype=c", |
| 599 |
"vim: sw=8 filetype=c ts=8", |
| 600 |
"vim: set filetype=c:", |
| 601 |
"vim: set sw=8 filetype=c:", |
| 602 |
"vim: set sw=8 filetype=c ts=8:", |
| 603 |
"vim: set filetype=c :", |
| 604 |
"vim: set sw=8 filetype=c :", |
| 605 |
"vim: set sw=8 filetype=c ts=8 :", |
| 606 |
"vim: se filetype=c:", |
| 607 |
"vim: se sw=8 filetype=c:", |
| 608 |
"vim: se sw=8 filetype=c ts=8:", |
| 609 |
"vim: se filetype=c :", |
| 610 |
"vim: se sw=8 filetype=c :", |
| 611 |
"vim: se sw=8 filetype=c ts=8 :", |
| 612 |
]: |
| 613 |
error_collector = ErrorCollector(self.assertTrue) |
| 614 |
cpplint.ProcessFileData( |
| 615 |
"test.h", |
| 616 |
"h", |
| 617 |
[ |
| 618 |
"// Copyright 2014 Your Company.", |
| 619 |
"// NOLINT(build/header_guard)", |
| 620 |
"int64_t a = (uint64_t) 65;", |
| 621 |
"/* Prevent warnings about the modeline", |
| 622 |
modeline, |
| 623 |
"*/", |
| 624 |
"", |
| 625 |
], |
| 626 |
error_collector, |
| 627 |
) |
| 628 |
assert error_collector.Results() == "" |
| 629 |
|
| 630 |
error_collector = ErrorCollector(self.assertTrue) |
| 631 |
cpplint.ProcessFileData( |
| 632 |
"test.h", |
| 633 |
"h", |
| 634 |
[ |
| 635 |
"// Copyright 2014 Your Company.", |
| 636 |
"// NOLINT(build/header_guard)", |
| 637 |
"struct test {", |
| 638 |
"\tint member;", |
| 639 |
"};", |
| 640 |
"// LINT_KERNEL_FILE", |
| 641 |
"", |
| 642 |
], |
| 643 |
error_collector, |
| 644 |
) |
| 645 |
assert error_collector.Results() == "" |
| 646 |
|
| 647 |
error_collector = ErrorCollector(self.assertTrue) |
| 648 |
cpplint.ProcessFileData( |
| 649 |
"test.cc", |
| 650 |
"cc", |
| 651 |
[ |
| 652 |
"// Copyright 2014 Your Company.", |
| 653 |
"#include <iostream>", |
| 654 |
"for (int i = 0; i != 100; ++i) {", |
| 655 |
" std::cout << i << std::endl;", |
| 656 |
"}; // NOLINT", |
| 657 |
"for (int i = 0; i != 100; ++i) {", |
| 658 |
" std::cout << i << std::endl;", |
| 659 |
"// NOLINTNEXTLINE", |
| 660 |
"};", |
| 661 |
"// LINT_KERNEL_FILE", |
| 662 |
"", |
| 663 |
], |
| 664 |
error_collector, |
| 665 |
) |
| 666 |
assert error_collector.Results() == "" |
| 667 |
|
| 668 |
error_collector = ErrorCollector(self.assertTrue) |
| 669 |
cpplint.ProcessFileData( |
| 670 |
"test.cc", |
| 671 |
"cc", |
| 672 |
[ |
| 673 |
"// Copyright 2014 Your Company.", |
| 674 |
"// NOLINTBEGIN", |
| 675 |
"long a = (int64_t) 65;long a = 65;", |
| 676 |
"// ./command" + (" -verbose" * 80), |
| 677 |
], |
| 678 |
error_collector, |
| 679 |
) |
| 680 |
assert error_collector.Results() == "" |
| 681 |
error_collector = ErrorCollector(self.assertTrue) |
| 682 |
cpplint.ProcessFileData( |
| 683 |
"test.cc", |
| 684 |
"cc", |
| 685 |
[ |
| 686 |
"// Copyright 2014 Your Company.", |
| 687 |
"// NOLINTBEGIN(*)", |
| 688 |
"long a = (int64_t) 65;long a = 65;", |
| 689 |
"// ./command" + (" -verbose" * 80), |
| 690 |
], |
| 691 |
error_collector, |
| 692 |
) |
| 693 |
assert error_collector.Results() == "" |
| 694 |
|
| 695 |
error_collector = ErrorCollector(self.assertTrue) |
| 696 |
cpplint.ProcessFileData( |
| 697 |
"test.cc", |
| 698 |
"cc", |
| 699 |
[ |
| 700 |
"// Copyright 2014 Your Company.", |
| 701 |
"// NOLINTBEGIN", |
| 702 |
"long a = (int64_t) 65;long a = 65;", |
| 703 |
"// NOLINTEND", |
| 704 |
"// ./command" + (" -verbose" * 80), |
| 705 |
"", |
| 706 |
], |
| 707 |
error_collector, |
| 708 |
) |
| 709 |
assert ( |
| 710 |
error_collector.Results() |
| 711 |
== "Lines should be <= 80 characters long [whitespace/line_length] [2]" |
| 712 |
) |
| 713 |
|
| 714 |
error_collector = ErrorCollector(self.assertTrue) |
| 715 |
cpplint.ProcessFileData( |
| 716 |
"test.cc", |
| 717 |
"cc", |
| 718 |
[ |
| 719 |
"// Copyright 2014 Your Company.", |
| 720 |
"// NOLINTBEGIN(readability/casting,runtime/int)", |
| 721 |
"long a = (int64_t) 65;", |
| 722 |
"long a = 65;", |
| 723 |
"// ./command" + (" -verbose" * 80), |
| 724 |
"// NOLINTEND", |
| 725 |
"", |
| 726 |
], |
| 727 |
error_collector, |
| 728 |
) |
| 729 |
assert ( |
| 730 |
error_collector.Results() |
| 731 |
== "Lines should be <= 80 characters long [whitespace/line_length] [2]" |
| 732 |
) |
| 733 |
|
| 734 |
error_collector = ErrorCollector(self.assertTrue) |
| 735 |
cpplint.ProcessFileData( |
| 736 |
"test.cc", |
| 737 |
"cc", |
| 738 |
[ |
| 739 |
"// Copyright 2014 Your Company.", |
| 740 |
"// NOLINTBEGIN(readability/casting,runtime/int)", |
| 741 |
"long a = (int64_t) 65;", |
| 742 |
"long a = 65;", |
| 743 |
"// NOLINTEND(readability/casting)", |
| 744 |
"", |
| 745 |
], |
| 746 |
error_collector, |
| 747 |
) |
| 748 |
assert ( |
| 749 |
error_collector.Results() |
| 750 |
== "NOLINT categories not supported in block END: readability/casting " |
| 751 |
"[readability/nolint] [5]" |
| 752 |
) |
| 753 |
|
| 754 |
error_collector = ErrorCollector(self.assertTrue) |
| 755 |
cpplint.ProcessFileData( |
| 756 |
"test.cc", |
| 757 |
"cc", |
| 758 |
[ |
| 759 |
"// Copyright 2014 Your Company.", |
| 760 |
"// NOLINTBEGIN(readability/casting,runtime/int)", |
| 761 |
"long a = (int64_t) 65;", |
| 762 |
"// NOLINTBEGIN(runtime/int)", |
| 763 |
"long a = 65;", |
| 764 |
"// NOLINTEND(*)", |
| 765 |
"", |
| 766 |
], |
| 767 |
error_collector, |
| 768 |
) |
| 769 |
assert ( |
| 770 |
error_collector.Results() |
| 771 |
== "NONLINT block already defined on line 2 [readability/nolint] [5]" |
| 772 |
) |
| 773 |
|
| 774 |
error_collector = ErrorCollector(self.assertTrue) |
| 775 |
cpplint.ProcessFileData( |
| 776 |
"test.cc", |
| 777 |
"cc", |
| 778 |
[ |
| 779 |
"// Copyright 2014 Your Company.", |
| 780 |
"// NOLINTBEGIN(readability/casting,runtime/int)", |
| 781 |
"long a = (int64_t) 65;", |
| 782 |
"long a = 65;", |
| 783 |
"", |
| 784 |
], |
| 785 |
error_collector, |
| 786 |
) |
| 787 |
assert error_collector.Results() == "NONLINT block never ended [readability/nolint] [5]" |
| 788 |
|
| 789 |
self.TestLint("// NOLINTEND", "Not in a NOLINT block [readability/nolint] [5]") |
| 790 |
self.TestLint("// NOLINTEND(*)", "Not in a NOLINT block [readability/nolint] [5]") |
| 791 |
|
| 792 |
|
| 793 |
def testVariableDeclarations(self): |
| 794 |
self.TestLint( |
| 795 |
"long a = 65;", |
| 796 |
"Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]", |
| 797 |
) |
| 798 |
self.TestLint("long double b = 65.0;", "") |
| 799 |
self.TestLint( |
| 800 |
"long long aa = 6565;", |
| 801 |
"Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]", |
| 802 |
) |
| 803 |
|
| 804 |
|
| 805 |
def testCStyleCast(self): |
| 806 |
self.TestLint( |
| 807 |
"int a = (int)1.0;", |
| 808 |
"Using C-style cast. Use static_cast<int>(...) instead [readability/casting] [4]", |
| 809 |
) |
| 810 |
self.TestLint( |
| 811 |
"int a = (int)-1.0;", |
| 812 |
"Using C-style cast. Use static_cast<int>(...) instead [readability/casting] [4]", |
| 813 |
) |
| 814 |
self.TestLint( |
| 815 |
"int *a = (int *)NULL;", |
| 816 |
"Using C-style cast. Use reinterpret_cast<int *>(...) instead" |
| 817 |
" [readability/casting] [4]", |
| 818 |
) |
| 819 |
|
| 820 |
self.TestLint( |
| 821 |
"uint16_t a = (uint16_t)1.0;", |
| 822 |
"Using C-style cast. Use static_cast<uint16_t>(...) instead" |
| 823 |
" [readability/casting] [4]", |
| 824 |
) |
| 825 |
self.TestLint( |
| 826 |
"int32_t a = (int32_t)1.0;", |
| 827 |
"Using C-style cast. Use static_cast<int32_t>(...) instead [readability/casting] [4]", |
| 828 |
) |
| 829 |
self.TestLint( |
| 830 |
"uint64_t a = (uint64_t)1.0;", |
| 831 |
"Using C-style cast. Use static_cast<uint64_t>(...) instead" |
| 832 |
" [readability/casting] [4]", |
| 833 |
) |
| 834 |
self.TestLint( |
| 835 |
"size_t a = (size_t)1.0;", |
| 836 |
"Using C-style cast. Use static_cast<size_t>(...) instead [readability/casting] [4]", |
| 837 |
) |
| 838 |
|
| 839 |
|
| 840 |
self.TestLint("u a = (u)NULL;", "") |
| 841 |
self.TestLint("uint a = (uint)NULL;", "") |
| 842 |
self.TestLint("typedef MockCallback<int(int)> CallbackType;", "") |
| 843 |
self.TestLint("scoped_ptr< MockCallback<int(int)> > callback_value;", "") |
| 844 |
self.TestLint("std::function<int(bool)>", "") |
| 845 |
self.TestLint("x = sizeof(int)", "") |
| 846 |
self.TestLint("x = alignof(int)", "") |
| 847 |
self.TestLint("alignas(int) char x[42]", "") |
| 848 |
self.TestLint("alignas(alignof(x)) char y[42]", "") |
| 849 |
self.TestLint("void F(int (func)(int));", "") |
| 850 |
self.TestLint("void F(int (func)(int*));", "") |
| 851 |
self.TestLint("void F(int (Class::member)(int));", "") |
| 852 |
self.TestLint("void F(int (Class::member)(int*));", "") |
| 853 |
self.TestLint("void F(int (Class::member)(int), int param);", "") |
| 854 |
self.TestLint("void F(int (Class::member)(int*), int param);", "") |
| 855 |
self.TestLint("X Class::operator++(int)", "") |
| 856 |
self.TestLint("X Class::operator--(int)", "") |
| 857 |
|
| 858 |
|
| 859 |
self.TestLint("[](int/*unused*/) -> bool {", "") |
| 860 |
self.TestLint("[](int /*unused*/) -> bool {", "") |
| 861 |
self.TestLint("auto f = [](MyStruct* /*unused*/)->int {", "") |
| 862 |
self.TestLint("[](int) -> bool {", "") |
| 863 |
self.TestLint("auto f = [](MyStruct*)->int {", "") |
| 864 |
|
| 865 |
|
| 866 |
self.TestLint("int64_t{4096} * 1000 * 1000", "") |
| 867 |
self.TestLint("size_t{4096} * 1000 * 1000", "") |
| 868 |
self.TestLint("uint_fast16_t{4096} * 1000 * 1000", "") |
| 869 |
|
| 870 |
|
| 871 |
self.TestMultiLineLint( |
| 872 |
""" |
| 873 |
template <typename Type1, |
| 874 |
typename Type2> |
| 875 |
void Function(int arg1, |
| 876 |
int arg2) { |
| 877 |
variable &= ~Type1{0} - 1; |
| 878 |
}""", |
| 879 |
"", |
| 880 |
) |
| 881 |
self.TestMultiLineLint( |
| 882 |
""" |
| 883 |
template <typename Type> |
| 884 |
class Class { |
| 885 |
void Function() { |
| 886 |
variable &= ~Type{0} - 1; |
| 887 |
} |
| 888 |
};""", |
| 889 |
"", |
| 890 |
) |
| 891 |
self.TestMultiLineLint( |
| 892 |
""" |
| 893 |
template <typename Type> |
| 894 |
class Class { |
| 895 |
void Function() { |
| 896 |
variable &= ~Type{0} - 1; |
| 897 |
} |
| 898 |
};""", |
| 899 |
"", |
| 900 |
) |
| 901 |
self.TestMultiLineLint( |
| 902 |
""" |
| 903 |
namespace { |
| 904 |
template <typename Type> |
| 905 |
class Class { |
| 906 |
void Function() { |
| 907 |
if (block) { |
| 908 |
variable &= ~Type{0} - 1; |
| 909 |
} |
| 910 |
} |
| 911 |
}; |
| 912 |
}""", |
| 913 |
"", |
| 914 |
) |
| 915 |
|
| 916 |
|
| 917 |
def testRuntimeCasting(self): |
| 918 |
error_msg = ( |
| 919 |
"Are you taking an address of a cast? " |
| 920 |
"This is dangerous: could be a temp var. " |
| 921 |
"Take the address before doing the cast, rather than after" |
| 922 |
" [runtime/casting] [4]" |
| 923 |
) |
| 924 |
self.TestLint("int* x = &static_cast<int*>(foo);", error_msg) |
| 925 |
self.TestLint("int* x = &reinterpret_cast<int *>(foo);", error_msg) |
| 926 |
self.TestLint( |
| 927 |
"int* x = &(int*)foo;", |
| 928 |
[ |
| 929 |
"Using C-style cast. Use reinterpret_cast<int*>(...) " |
| 930 |
"instead [readability/casting] [4]", |
| 931 |
error_msg, |
| 932 |
], |
| 933 |
) |
| 934 |
self.TestLint("BudgetBuckets&(BudgetWinHistory::*BucketFn)(void) const;", "") |
| 935 |
self.TestLint("&(*func_ptr)(arg)", "") |
| 936 |
self.TestLint("Compute(arg, &(*func_ptr)(i, j));", "") |
| 937 |
|
| 938 |
|
| 939 |
alt_error_msg = ( |
| 940 |
"Are you taking an address of something dereferenced " |
| 941 |
"from a cast? Wrapping the dereferenced expression in " |
| 942 |
"parentheses will make the binding more obvious" |
| 943 |
" [readability/casting] [4]" |
| 944 |
) |
| 945 |
self.TestLint("int* x = &down_cast<Obj*>(obj)->member_;", alt_error_msg) |
| 946 |
self.TestLint("int* x = &down_cast<Obj*>(obj)[index];", alt_error_msg) |
| 947 |
self.TestLint("int* x = &(down_cast<Obj*>(obj)->member_);", "") |
| 948 |
self.TestLint("int* x = &(down_cast<Obj*>(obj)[index]);", "") |
| 949 |
self.TestLint("int* x = &down_cast<Obj*>(obj)\n->member_;", alt_error_msg) |
| 950 |
self.TestLint("int* x = &(down_cast<Obj*>(obj)\n->member_);", "") |
| 951 |
|
| 952 |
|
| 953 |
self.TestLint("int* x = reinterpret_cast<int *>(&foo);", "") |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
self.TestLint("auto x = implicit_cast<string &(*)(int)>(&foo);", "") |
| 958 |
|
| 959 |
def testRuntimeSelfinit(self): |
| 960 |
self.TestLint( |
| 961 |
"Foo::Foo(Bar r, Bel l) : r_(r_), l_(l_) { }", |
| 962 |
"You seem to be initializing a member variable with itself. [runtime/init] [4]", |
| 963 |
) |
| 964 |
self.TestLint( |
| 965 |
"Foo::Foo(Bar r, Bel l) : r_(CHECK_NOTNULL(r_)) { }", |
| 966 |
"You seem to be initializing a member variable with itself. [runtime/init] [4]", |
| 967 |
) |
| 968 |
self.TestLint("Foo::Foo(Bar r, Bel l) : r_(r), l_(l) { }", "") |
| 969 |
self.TestLint("Foo::Foo(Bar r) : r_(r), l_(r_), ll_(l_) { }", "") |
| 970 |
|
| 971 |
|
| 972 |
def testCheckForUnnamedParams(self): |
| 973 |
self.TestLint("virtual void Func(int*) const;", "") |
| 974 |
self.TestLint("virtual void Func(int*);", "") |
| 975 |
self.TestLint("void Method(char*) {", "") |
| 976 |
self.TestLint("void Method(char*);", "") |
| 977 |
self.TestLint("static void operator delete[](void*) throw();", "") |
| 978 |
self.TestLint("int Method(int);", "") |
| 979 |
|
| 980 |
self.TestLint("virtual void Func(int* p);", "") |
| 981 |
self.TestLint("void operator delete(void* x) throw();", "") |
| 982 |
self.TestLint("void Method(char* x) {", "") |
| 983 |
self.TestLint("void Method(char* /*x*/) {", "") |
| 984 |
self.TestLint("void Method(char* x);", "") |
| 985 |
self.TestLint("typedef void (*Method)(int32_t x);", "") |
| 986 |
self.TestLint("static void operator delete[](void* x) throw();", "") |
| 987 |
self.TestLint("static void operator delete[](void* /*x*/) throw();", "") |
| 988 |
|
| 989 |
self.TestLint("X operator++(int);", "") |
| 990 |
self.TestLint("X operator++(int) {", "") |
| 991 |
self.TestLint("X operator--(int);", "") |
| 992 |
self.TestLint("X operator--(int /*unused*/) {", "") |
| 993 |
self.TestLint("MACRO(int);", "") |
| 994 |
self.TestLint("MACRO(func(int));", "") |
| 995 |
self.TestLint("MACRO(arg, func(int));", "") |
| 996 |
|
| 997 |
self.TestLint("void (*func)(void*);", "") |
| 998 |
self.TestLint("void Func((*func)(void*)) {}", "") |
| 999 |
self.TestLint("template <void Func(void*)> void func();", "") |
| 1000 |
self.TestLint("virtual void f(int /*unused*/) {", "") |
| 1001 |
self.TestLint("void f(int /*unused*/) override {", "") |
| 1002 |
self.TestLint("void f(int /*unused*/) final {", "") |
| 1003 |
|
| 1004 |
|
| 1005 |
def testDeprecatedCast(self): |
| 1006 |
self.TestLint( |
| 1007 |
"int a = int(2.2);", |
| 1008 |
"Using deprecated casting style. " |
| 1009 |
"Use static_cast<int>(...) instead" |
| 1010 |
" [readability/casting] [4]", |
| 1011 |
) |
| 1012 |
|
| 1013 |
self.TestLint( |
| 1014 |
'(char *) "foo"', |
| 1015 |
"Using C-style cast. Use const_cast<char *>(...) instead [readability/casting] [4]", |
| 1016 |
) |
| 1017 |
|
| 1018 |
self.TestLint( |
| 1019 |
"(int*)foo", |
| 1020 |
"Using C-style cast. " |
| 1021 |
"Use reinterpret_cast<int*>(...) instead" |
| 1022 |
" [readability/casting] [4]", |
| 1023 |
) |
| 1024 |
|
| 1025 |
|
| 1026 |
self.TestLint("int a = int();", "") |
| 1027 |
self.TestLint("X::X() : a(int()) {}", "") |
| 1028 |
self.TestLint("operator bool();", "") |
| 1029 |
self.TestLint("new int64_t(123);", "") |
| 1030 |
self.TestLint("new int64_t(123);", "") |
| 1031 |
self.TestLint("new const int(42);", "") |
| 1032 |
self.TestLint("using a = bool(int arg);", "") |
| 1033 |
self.TestLint("x = bit_cast<double(*)[3]>(y);", "") |
| 1034 |
self.TestLint("void F(const char(&src)[N]);", "") |
| 1035 |
|
| 1036 |
|
| 1037 |
self.TestLint("new(field_ptr) int(field->default_value_enum()->number());", "") |
| 1038 |
|
| 1039 |
|
| 1040 |
self.TestLint("std::function<int(bool)>", "") |
| 1041 |
self.TestLint("std::function<const int(bool)>", "") |
| 1042 |
self.TestLint("std::function< int(bool) >", "") |
| 1043 |
self.TestLint("mfunction<int(bool)>", "") |
| 1044 |
|
| 1045 |
error_collector = ErrorCollector(self.assertTrue) |
| 1046 |
cpplint.ProcessFileData( |
| 1047 |
"test.cc", |
| 1048 |
"cc", |
| 1049 |
[ |
| 1050 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 1051 |
"typedef std::function<", |
| 1052 |
" bool(int)> F;", |
| 1053 |
"", |
| 1054 |
], |
| 1055 |
error_collector, |
| 1056 |
) |
| 1057 |
assert error_collector.Results() == "" |
| 1058 |
|
| 1059 |
|
| 1060 |
self.TestLint("typedef bool(FunctionPointer)();", "") |
| 1061 |
self.TestLint("typedef bool(FunctionPointer)(int param);", "") |
| 1062 |
self.TestLint("typedef bool(MyClass::*MemberFunctionPointer)();", "") |
| 1063 |
self.TestLint("typedef bool(MyClass::* MemberFunctionPointer)();", "") |
| 1064 |
self.TestLint("typedef bool(MyClass::*MemberFunctionPointer)() const;", "") |
| 1065 |
self.TestLint("void Function(bool(FunctionPointerArg)());", "") |
| 1066 |
self.TestLint("void Function(bool(FunctionPointerArg)()) {}", "") |
| 1067 |
self.TestLint("typedef set<int64_t, bool(*)(int64_t, int64_t)> SortedIdSet", "") |
| 1068 |
self.TestLint("bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *t)) {}", "") |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
def testMockMethod(self): |
| 1073 |
self.TestLint("MOCK_METHOD0(method, int());", "") |
| 1074 |
self.TestLint("MOCK_CONST_METHOD1(method, float(string));", "") |
| 1075 |
self.TestLint("MOCK_CONST_METHOD2_T(method, double(float, float));", "") |
| 1076 |
self.TestLint("MOCK_CONST_METHOD1(method, SomeType(int));", "") |
| 1077 |
|
| 1078 |
error_collector = ErrorCollector(self.assertTrue) |
| 1079 |
cpplint.ProcessFileData( |
| 1080 |
"mock.cc", |
| 1081 |
"cc", |
| 1082 |
[ |
| 1083 |
"MOCK_METHOD1(method1,", |
| 1084 |
" bool(int));", |
| 1085 |
"MOCK_METHOD1(", |
| 1086 |
" method2,", |
| 1087 |
" bool(int));", |
| 1088 |
"MOCK_CONST_METHOD2(", |
| 1089 |
" method3, bool(int,", |
| 1090 |
" int));", |
| 1091 |
"MOCK_METHOD1(method4, int(bool));", |
| 1092 |
"const int kConstant = int(42);", |
| 1093 |
], |
| 1094 |
error_collector, |
| 1095 |
) |
| 1096 |
assert ( |
| 1097 |
error_collector.Results().count( |
| 1098 |
"Using deprecated casting style. Use static_cast<bool>(...) instead " |
| 1099 |
"[readability/casting] [4]" |
| 1100 |
) |
| 1101 |
== 0 |
| 1102 |
) |
| 1103 |
assert ( |
| 1104 |
error_collector.Results().count( |
| 1105 |
"Using deprecated casting style. Use static_cast<int>(...) instead " |
| 1106 |
"[readability/casting] [4]" |
| 1107 |
) |
| 1108 |
== 1 |
| 1109 |
) |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
def testMockCallback(self): |
| 1114 |
self.TestLint("MockCallback<bool(int)>", "") |
| 1115 |
self.TestLint("MockCallback<int(float, char)>", "") |
| 1116 |
|
| 1117 |
|
| 1118 |
def testIncludeFilenameFalseError(self): |
| 1119 |
self.TestLint('#include "foo/long-foo.h"', "") |
| 1120 |
self.TestLint('#include "foo/sprintf.h"', "") |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
def testTypedefForPointerToFunction(self): |
| 1126 |
self.TestLint("typedef void (*Func)(int x);", "") |
| 1127 |
self.TestLint("typedef void (*Func)(int *x);", "") |
| 1128 |
self.TestLint("typedef void Func(int x);", "") |
| 1129 |
self.TestLint("typedef void Func(int *x);", "") |
| 1130 |
|
| 1131 |
def testIncludeWhatYouUseNoImplementationFiles(self): |
| 1132 |
code = "std::vector<int> foo;" |
| 1133 |
for extension in ["h", "hpp", "hxx", "h++", "cuh", "c", "cc", "cpp", "cxx", "c++", "cu"]: |
| 1134 |
assert ( |
| 1135 |
self.PerformIncludeWhatYouUse(code, "foo." + extension) |
| 1136 |
== "Add #include <vector> for vector<> [build/include_what_you_use] [4]" |
| 1137 |
) |
| 1138 |
|
| 1139 |
def testIncludeWhatYouUse(self): |
| 1140 |
self.TestIncludeWhatYouUse( |
| 1141 |
"""#include <vector> |
| 1142 |
std::vector<int> foo; |
| 1143 |
""", |
| 1144 |
"", |
| 1145 |
) |
| 1146 |
self.TestIncludeWhatYouUse( |
| 1147 |
"""#include <map> |
| 1148 |
std::pair<int,int> foo; |
| 1149 |
""", |
| 1150 |
"Add #include <utility> for pair<> [build/include_what_you_use] [4]", |
| 1151 |
) |
| 1152 |
self.TestIncludeWhatYouUse( |
| 1153 |
"""#include <multimap> |
| 1154 |
std::pair<int,int> foo; |
| 1155 |
""", |
| 1156 |
"Add #include <utility> for pair<> [build/include_what_you_use] [4]", |
| 1157 |
) |
| 1158 |
self.TestIncludeWhatYouUse( |
| 1159 |
"""#include <hash_map> |
| 1160 |
std::pair<int,int> foo; |
| 1161 |
""", |
| 1162 |
"Add #include <utility> for pair<> [build/include_what_you_use] [4]", |
| 1163 |
) |
| 1164 |
self.TestIncludeWhatYouUse( |
| 1165 |
"""#include <hash_map> |
| 1166 |
auto foo = std::make_pair(1, 2); |
| 1167 |
""", |
| 1168 |
"Add #include <utility> for make_pair [build/include_what_you_use] [4]", |
| 1169 |
) |
| 1170 |
self.TestIncludeWhatYouUse( |
| 1171 |
"""#include <utility> |
| 1172 |
std::pair<int,int> foo; |
| 1173 |
""", |
| 1174 |
"", |
| 1175 |
) |
| 1176 |
self.TestIncludeWhatYouUse( |
| 1177 |
"""#include <vector> |
| 1178 |
DECLARE_string(foobar); |
| 1179 |
""", |
| 1180 |
"", |
| 1181 |
) |
| 1182 |
self.TestIncludeWhatYouUse( |
| 1183 |
"""#include <vector> |
| 1184 |
DEFINE_string(foobar, "", ""); |
| 1185 |
""", |
| 1186 |
"", |
| 1187 |
) |
| 1188 |
self.TestIncludeWhatYouUse( |
| 1189 |
"""#include <vector> |
| 1190 |
std::pair<int,int> foo; |
| 1191 |
""", |
| 1192 |
"Add #include <utility> for pair<> [build/include_what_you_use] [4]", |
| 1193 |
) |
| 1194 |
self.TestIncludeWhatYouUse( |
| 1195 |
"""#include "base/foobar.h" |
| 1196 |
std::vector<int> foo; |
| 1197 |
""", |
| 1198 |
"Add #include <vector> for vector<> [build/include_what_you_use] [4]", |
| 1199 |
) |
| 1200 |
self.TestIncludeWhatYouUse( |
| 1201 |
"""#include <vector> |
| 1202 |
std::set<int> foo; |
| 1203 |
""", |
| 1204 |
"Add #include <set> for set<> [build/include_what_you_use] [4]", |
| 1205 |
) |
| 1206 |
self.TestIncludeWhatYouUse( |
| 1207 |
"""#include "base/foobar.h" |
| 1208 |
hash_map<int, int> foobar; |
| 1209 |
""", |
| 1210 |
"Add #include <hash_map> for hash_map<> [build/include_what_you_use] [4]", |
| 1211 |
) |
| 1212 |
self.TestIncludeWhatYouUse( |
| 1213 |
"""#include "base/containers/hash_tables.h" |
| 1214 |
base::hash_map<int, int> foobar; |
| 1215 |
""", |
| 1216 |
"", |
| 1217 |
) |
| 1218 |
self.TestIncludeWhatYouUse( |
| 1219 |
"""#include "base/foobar.h" |
| 1220 |
bool foobar = std::less<int>(0,1); |
| 1221 |
""", |
| 1222 |
"Add #include <functional> for less<> [build/include_what_you_use] [4]", |
| 1223 |
) |
| 1224 |
self.TestIncludeWhatYouUse( |
| 1225 |
"""#include "base/foobar.h" |
| 1226 |
bool foobar = min<int>(0,1); |
| 1227 |
""", |
| 1228 |
"Add #include <algorithm> for min [build/include_what_you_use] [4]", |
| 1229 |
) |
| 1230 |
self.TestIncludeWhatYouUse( |
| 1231 |
'cout << "hello world" << endl;', |
| 1232 |
"Add #include <iostream> for cout [build/include_what_you_use] [4]", |
| 1233 |
) |
| 1234 |
self.TestIncludeWhatYouUse( |
| 1235 |
'printf("hello world");', |
| 1236 |
"Add #include <cstdio> for printf [build/include_what_you_use] [4]", |
| 1237 |
) |
| 1238 |
self.TestIncludeWhatYouUse( |
| 1239 |
"""#include <stdio.h> |
| 1240 |
printf("hello world");""", |
| 1241 |
"", |
| 1242 |
) |
| 1243 |
self.TestIncludeWhatYouUse( |
| 1244 |
"void a(const string &foobar);", |
| 1245 |
"Add #include <string> for string [build/include_what_you_use] [4]", |
| 1246 |
) |
| 1247 |
self.TestIncludeWhatYouUse( |
| 1248 |
"void a(const std::string &foobar);", |
| 1249 |
"Add #include <string> for string [build/include_what_you_use] [4]", |
| 1250 |
) |
| 1251 |
self.TestIncludeWhatYouUse( |
| 1252 |
"void a(const my::string &foobar);", "" |
| 1253 |
) |
| 1254 |
self.TestIncludeWhatYouUse( |
| 1255 |
"""#include "base/foobar.h" |
| 1256 |
bool foobar = swap(0,1); |
| 1257 |
""", |
| 1258 |
"Add #include <utility> for swap [build/include_what_you_use] [4]", |
| 1259 |
) |
| 1260 |
self.TestIncludeWhatYouUse( |
| 1261 |
"""#include "base/foobar.h" |
| 1262 |
bool foobar = transform(a.begin(), a.end(), b.start(), Foo); |
| 1263 |
""", |
| 1264 |
"Add #include <algorithm> for transform [build/include_what_you_use] [4]", |
| 1265 |
) |
| 1266 |
self.TestIncludeWhatYouUse( |
| 1267 |
"""#include "base/foobar.h" |
| 1268 |
boost::range::transform(input, std::back_inserter(output), square); |
| 1269 |
""", |
| 1270 |
"", |
| 1271 |
) |
| 1272 |
self.TestIncludeWhatYouUse( |
| 1273 |
"""#include "base/foobar.h" |
| 1274 |
bool foobar = std::min_element(a.begin(), a.end()); |
| 1275 |
""", |
| 1276 |
"Add #include <algorithm> for min_element [build/include_what_you_use] [4]", |
| 1277 |
) |
| 1278 |
self.TestIncludeWhatYouUse( |
| 1279 |
"""foo->swap(0,1); |
| 1280 |
foo.swap(0,1); |
| 1281 |
""", |
| 1282 |
"", |
| 1283 |
) |
| 1284 |
self.TestIncludeWhatYouUse( |
| 1285 |
"""#include <string> |
| 1286 |
void a(const std::multimap<int,string> &foobar); |
| 1287 |
""", |
| 1288 |
"Add #include <map> for multimap<> [build/include_what_you_use] [4]", |
| 1289 |
) |
| 1290 |
self.TestIncludeWhatYouUse( |
| 1291 |
"""#include <string> |
| 1292 |
void a(const std::unordered_map<int,string> &foobar); |
| 1293 |
""", |
| 1294 |
"Add #include <unordered_map> for unordered_map<> [build/include_what_you_use] [4]", |
| 1295 |
) |
| 1296 |
self.TestIncludeWhatYouUse( |
| 1297 |
"""#include <string> |
| 1298 |
void a(const std::unordered_set<int> &foobar); |
| 1299 |
""", |
| 1300 |
"Add #include <unordered_set> for unordered_set<> [build/include_what_you_use] [4]", |
| 1301 |
) |
| 1302 |
self.TestIncludeWhatYouUse( |
| 1303 |
"""#include <queue> |
| 1304 |
void a(const std::priority_queue<int> &foobar); |
| 1305 |
""", |
| 1306 |
"", |
| 1307 |
) |
| 1308 |
self.TestIncludeWhatYouUse( |
| 1309 |
"""#include <assert.h> |
| 1310 |
#include <string> |
| 1311 |
#include <vector> |
| 1312 |
#include "base/basictypes.h" |
| 1313 |
#include "base/port.h" |
| 1314 |
vector<string> hajoa;""", |
| 1315 |
"", |
| 1316 |
) |
| 1317 |
self.TestIncludeWhatYouUse( |
| 1318 |
"""#include <string> |
| 1319 |
int i = numeric_limits<int>::max() |
| 1320 |
""", |
| 1321 |
"Add #include <limits> for numeric_limits<> [build/include_what_you_use] [4]", |
| 1322 |
) |
| 1323 |
self.TestIncludeWhatYouUse( |
| 1324 |
"""#include <limits> |
| 1325 |
int i = numeric_limits<int>::max() |
| 1326 |
""", |
| 1327 |
"", |
| 1328 |
) |
| 1329 |
self.TestIncludeWhatYouUse( |
| 1330 |
"""#include <string> |
| 1331 |
std::unique_ptr<int> x; |
| 1332 |
""", |
| 1333 |
"Add #include <memory> for unique_ptr<> [build/include_what_you_use] [4]", |
| 1334 |
) |
| 1335 |
self.TestIncludeWhatYouUse( |
| 1336 |
"""#include <string> |
| 1337 |
auto x = std::make_unique<int>(0); |
| 1338 |
""", |
| 1339 |
"Add #include <memory> for make_unique<> [build/include_what_you_use] [4]", |
| 1340 |
) |
| 1341 |
self.TestIncludeWhatYouUse( |
| 1342 |
"""#include <vector> |
| 1343 |
vector<int> foo(vector<int> x) { return std::move(x); } |
| 1344 |
""", |
| 1345 |
"Add #include <utility> for move [build/include_what_you_use] [4]", |
| 1346 |
) |
| 1347 |
self.TestIncludeWhatYouUse( |
| 1348 |
"""#include <string> |
| 1349 |
int a, b; |
| 1350 |
std::swap(a, b); |
| 1351 |
""", |
| 1352 |
"Add #include <utility> for swap [build/include_what_you_use] [4]", |
| 1353 |
) |
| 1354 |
|
| 1355 |
self.TestIncludeWhatYouUse( |
| 1356 |
""" |
| 1357 |
#include <string> |
| 1358 |
struct Foo { |
| 1359 |
template <typename T> |
| 1360 |
void set(const std::string& name, const T& value); |
| 1361 |
}; |
| 1362 |
Foo bar; |
| 1363 |
Foo* pbar = &bar; |
| 1364 |
bar.set<int>("int", 5); |
| 1365 |
pbar->set<bool>("bool", false);""", |
| 1366 |
"", |
| 1367 |
) |
| 1368 |
|
| 1369 |
self.TestIncludeWhatYouUse( |
| 1370 |
""" |
| 1371 |
template <typename T> |
| 1372 |
struct Foo { |
| 1373 |
T t; |
| 1374 |
}; |
| 1375 |
template <typename T> |
| 1376 |
Foo<T> map(T t) { |
| 1377 |
return Foo<T>{ t }; |
| 1378 |
} |
| 1379 |
struct Bar { |
| 1380 |
}; |
| 1381 |
auto res = map<Bar>(); |
| 1382 |
""", |
| 1383 |
"", |
| 1384 |
) |
| 1385 |
|
| 1386 |
self.TestIncludeWhatYouUse( |
| 1387 |
""" |
| 1388 |
boost::container::set<int> foo; |
| 1389 |
""", |
| 1390 |
"", |
| 1391 |
) |
| 1392 |
|
| 1393 |
def testFilesBelongToSameModule(self): |
| 1394 |
f = cpplint.FilesBelongToSameModule |
| 1395 |
assert f("a.cc", "a.h") == (True, "") |
| 1396 |
assert f("base/google.cc", "base/google.h") == (True, "") |
| 1397 |
assert f("base/google_test.c", "base/google.h") == (True, "") |
| 1398 |
assert f("base/google_test.cc", "base/google.h") == (True, "") |
| 1399 |
assert f("base/google_test.cc", "base/google.hpp") == (True, "") |
| 1400 |
assert f("base/google_test.cxx", "base/google.hxx") == (True, "") |
| 1401 |
assert f("base/google_test.cpp", "base/google.hpp") == (True, "") |
| 1402 |
assert f("base/google_test.c++", "base/google.h++") == (True, "") |
| 1403 |
assert f("base/google_test.cu", "base/google.cuh") == (True, "") |
| 1404 |
assert f("base/google_unittest.cc", "base/google.h") == (True, "") |
| 1405 |
assert f("base/internal/google_unittest.cc", "base/public/google.h") == (True, "") |
| 1406 |
assert f("xxx/yyy/base/internal/google_unittest.cc", "base/public/google.h") == ( |
| 1407 |
True, |
| 1408 |
"xxx/yyy/", |
| 1409 |
) |
| 1410 |
assert f("xxx/yyy/base/google_unittest.cc", "base/public/google.h") == (True, "xxx/yyy/") |
| 1411 |
assert f("base/google_unittest.cc", "base/google-inl.h") == (True, "") |
| 1412 |
assert f("/home/build/google3/base/google.cc", "base/google.h") == ( |
| 1413 |
True, |
| 1414 |
"/home/build/google3/", |
| 1415 |
) |
| 1416 |
|
| 1417 |
assert f("/home/build/google3/base/google.cc", "basu/google.h") == (False, "") |
| 1418 |
assert f("a.cc", "b.h") == (False, "") |
| 1419 |
|
| 1420 |
def testCleanseLine(self): |
| 1421 |
assert cpplint.CleanseComments("int foo = 0; // danger!") == "int foo = 0;" |
| 1422 |
assert cpplint.CleanseComments("int /* foo */ o = 0;") == "int o = 0;" |
| 1423 |
assert cpplint.CleanseComments("foo(int a /* abc */, int b);") == "foo(int a, int b);" |
| 1424 |
assert cpplint.CleanseComments("f(a, /* name */ b);") == "f(a, b);" |
| 1425 |
assert cpplint.CleanseComments("f(a /* name */, b);") == "f(a, b);" |
| 1426 |
assert cpplint.CleanseComments("f(a, /* name */b);") == "f(a, b);" |
| 1427 |
assert cpplint.CleanseComments("f(a, /**/b, /**/c);") == "f(a, b, c);" |
| 1428 |
assert cpplint.CleanseComments("f(a, /**/b/**/, c);") == "f(a, b, c);" |
| 1429 |
|
| 1430 |
def testRawStrings(self): |
| 1431 |
self.TestMultiLineLint( |
| 1432 |
""" |
| 1433 |
int main() { |
| 1434 |
struct A { |
| 1435 |
A(std::string s, A&& a); |
| 1436 |
}; |
| 1437 |
}""", |
| 1438 |
"", |
| 1439 |
) |
| 1440 |
self.TestMultiLineLint( |
| 1441 |
""" |
| 1442 |
template <class T, class D = default_delete<T>> class unique_ptr { |
| 1443 |
public: |
| 1444 |
unique_ptr(unique_ptr&& u) noexcept; |
| 1445 |
};""", |
| 1446 |
"", |
| 1447 |
) |
| 1448 |
self.TestMultiLineLint( |
| 1449 |
""" |
| 1450 |
void Func() { |
| 1451 |
static const char kString[] = R"( |
| 1452 |
#endif <- invalid preprocessor should be ignored |
| 1453 |
*/ <- invalid comment should be ignored too |
| 1454 |
)"; |
| 1455 |
}""", |
| 1456 |
"", |
| 1457 |
) |
| 1458 |
self.TestMultiLineLint( |
| 1459 |
""" |
| 1460 |
void Func() { |
| 1461 |
string s = R"TrueDelimiter( |
| 1462 |
)" |
| 1463 |
)FalseDelimiter" |
| 1464 |
)TrueDelimiter"; |
| 1465 |
}""", |
| 1466 |
"", |
| 1467 |
) |
| 1468 |
self.TestMultiLineLint( |
| 1469 |
""" |
| 1470 |
void Func() { |
| 1471 |
char char kString[] = R"( ";" )"; |
| 1472 |
}""", |
| 1473 |
"", |
| 1474 |
) |
| 1475 |
self.TestMultiLineLint( |
| 1476 |
""" |
| 1477 |
static const char kRawString[] = R"( |
| 1478 |
\tstatic const int kLineWithTab = 1; |
| 1479 |
static const int kLineWithTrailingWhiteSpace = 1;\x20 |
| 1480 |
|
| 1481 |
void WeirdNumberOfSpacesAtLineStart() { |
| 1482 |
string x; |
| 1483 |
x += StrCat("Use StrAppend instead"); |
| 1484 |
} |
| 1485 |
|
| 1486 |
void BlankLineAtEndOfBlock() { |
| 1487 |
// TODO incorrectly formatted |
| 1488 |
//Badly formatted comment |
| 1489 |
|
| 1490 |
} |
| 1491 |
|
| 1492 |
)";""", |
| 1493 |
"", |
| 1494 |
) |
| 1495 |
self.TestMultiLineLint( |
| 1496 |
""" |
| 1497 |
void Func() { |
| 1498 |
string s = StrCat(R"TrueDelimiter( |
| 1499 |
)" |
| 1500 |
)FalseDelimiter" |
| 1501 |
)TrueDelimiter", R"TrueDelimiter2( |
| 1502 |
)" |
| 1503 |
)FalseDelimiter2" |
| 1504 |
)TrueDelimiter2"); |
| 1505 |
}""", |
| 1506 |
"", |
| 1507 |
) |
| 1508 |
self.TestMultiLineLint( |
| 1509 |
""" |
| 1510 |
static SomeStruct kData = { |
| 1511 |
{0, R"(line1 |
| 1512 |
line2 |
| 1513 |
)"} |
| 1514 |
};""", |
| 1515 |
"", |
| 1516 |
) |
| 1517 |
|
| 1518 |
def testMultiLineComments(self): |
| 1519 |
|
| 1520 |
self.TestMultiLineLint( |
| 1521 |
r"""int a = 0; |
| 1522 |
/* multi-liner |
| 1523 |
class Foo { |
| 1524 |
Foo(int f); // should cause a lint warning in code |
| 1525 |
} |
| 1526 |
*/ """, |
| 1527 |
"", |
| 1528 |
) |
| 1529 |
self.TestMultiLineLint( |
| 1530 |
r"""/* int a = 0; multi-liner |
| 1531 |
static const int b = 0;""", |
| 1532 |
"Could not find end of multi-line comment [readability/multiline_comment] [5]", |
| 1533 |
) |
| 1534 |
self.TestMultiLineLint( |
| 1535 |
r""" /* multi-line comment""", |
| 1536 |
"Could not find end of multi-line comment [readability/multiline_comment] [5]", |
| 1537 |
) |
| 1538 |
self.TestMultiLineLint(r""" // /* comment, but not multi-line""", "") |
| 1539 |
self.TestMultiLineLint( |
| 1540 |
r"""/********** |
| 1541 |
*/""", |
| 1542 |
"", |
| 1543 |
) |
| 1544 |
self.TestMultiLineLint( |
| 1545 |
r"""/** |
| 1546 |
* Doxygen comment |
| 1547 |
*/""", |
| 1548 |
"", |
| 1549 |
) |
| 1550 |
self.TestMultiLineLint( |
| 1551 |
r"""/*! |
| 1552 |
* Doxygen comment |
| 1553 |
*/""", |
| 1554 |
"", |
| 1555 |
) |
| 1556 |
|
| 1557 |
def testMultilineStrings(self): |
| 1558 |
multiline_string_error_message = ( |
| 1559 |
'Multi-line string ("...") found. This lint script doesn\'t ' |
| 1560 |
"do well with such strings, and may give bogus warnings. " |
| 1561 |
"Use C++11 raw strings or concatenation instead." |
| 1562 |
" [readability/multiline_string] [5]" |
| 1563 |
) |
| 1564 |
|
| 1565 |
for extension in ["c", "cc", "cpp", "cxx", "c++", "cu"]: |
| 1566 |
file_path = "mydir/foo." + extension |
| 1567 |
|
| 1568 |
error_collector = ErrorCollector(self.assertTrue) |
| 1569 |
cpplint.ProcessFileData( |
| 1570 |
file_path, |
| 1571 |
extension, |
| 1572 |
['const char* str = "This is a\\', ' multiline string.";'], |
| 1573 |
error_collector, |
| 1574 |
) |
| 1575 |
|
| 1576 |
assert error_collector.ResultList().count(multiline_string_error_message) == 2 |
| 1577 |
|
| 1578 |
|
| 1579 |
def testExplicitSingleArgumentConstructors(self): |
| 1580 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 1581 |
cpplint._cpplint_state.verbose_level = 0 |
| 1582 |
|
| 1583 |
try: |
| 1584 |
|
| 1585 |
self.TestMultiLineLint( |
| 1586 |
""" |
| 1587 |
class Foo { |
| 1588 |
Foo(int f); |
| 1589 |
};""", |
| 1590 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1591 |
) |
| 1592 |
|
| 1593 |
self.TestMultiLineLint( |
| 1594 |
""" |
| 1595 |
class Foo { |
| 1596 |
Foo (int f); |
| 1597 |
};""", |
| 1598 |
[ |
| 1599 |
"Extra space before ( in function call [whitespace/parens] [4]", |
| 1600 |
"Single-parameter constructors should be marked explicit." |
| 1601 |
" [runtime/explicit] [4]", |
| 1602 |
], |
| 1603 |
) |
| 1604 |
|
| 1605 |
self.TestMultiLineLint( |
| 1606 |
""" |
| 1607 |
class Foo { |
| 1608 |
Foo(int f); // simpler than Foo(blargh, blarg) |
| 1609 |
};""", |
| 1610 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1611 |
) |
| 1612 |
|
| 1613 |
self.TestMultiLineLint( |
| 1614 |
""" |
| 1615 |
class Qualifier::AnotherOne::Foo { |
| 1616 |
Foo(int f); |
| 1617 |
};""", |
| 1618 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1619 |
) |
| 1620 |
|
| 1621 |
self.TestMultiLineLint( |
| 1622 |
""" |
| 1623 |
class Foo { |
| 1624 |
inline Foo(int f); |
| 1625 |
};""", |
| 1626 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1627 |
) |
| 1628 |
|
| 1629 |
self.TestMultiLineLint( |
| 1630 |
""" |
| 1631 |
class Foo { |
| 1632 |
constexpr Foo(int f); |
| 1633 |
};""", |
| 1634 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1635 |
) |
| 1636 |
|
| 1637 |
self.TestMultiLineLint( |
| 1638 |
""" |
| 1639 |
class Foo { |
| 1640 |
constexpr inline Foo(int f); |
| 1641 |
};""", |
| 1642 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1643 |
) |
| 1644 |
self.TestMultiLineLint( |
| 1645 |
""" |
| 1646 |
class Foo { |
| 1647 |
inline constexpr Foo(int f); |
| 1648 |
};""", |
| 1649 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1650 |
) |
| 1651 |
|
| 1652 |
self.TestMultiLineLint( |
| 1653 |
""" |
| 1654 |
class Foo { |
| 1655 |
inline explicit Foo(int f); |
| 1656 |
};""", |
| 1657 |
"", |
| 1658 |
) |
| 1659 |
self.TestMultiLineLint( |
| 1660 |
""" |
| 1661 |
class Foo { |
| 1662 |
explicit inline Foo(int f); |
| 1663 |
};""", |
| 1664 |
"", |
| 1665 |
) |
| 1666 |
|
| 1667 |
self.TestMultiLineLint( |
| 1668 |
""" |
| 1669 |
class Foo { |
| 1670 |
constexpr explicit Foo(int f); |
| 1671 |
};""", |
| 1672 |
"", |
| 1673 |
) |
| 1674 |
self.TestMultiLineLint( |
| 1675 |
""" |
| 1676 |
class Foo { |
| 1677 |
explicit constexpr Foo(int f); |
| 1678 |
};""", |
| 1679 |
"", |
| 1680 |
) |
| 1681 |
|
| 1682 |
self.TestMultiLineLint( |
| 1683 |
""" |
| 1684 |
class Foo { |
| 1685 |
inline constexpr explicit Foo(int f); |
| 1686 |
};""", |
| 1687 |
"", |
| 1688 |
) |
| 1689 |
self.TestMultiLineLint( |
| 1690 |
""" |
| 1691 |
class Foo { |
| 1692 |
explicit inline constexpr Foo(int f); |
| 1693 |
};""", |
| 1694 |
"", |
| 1695 |
) |
| 1696 |
self.TestMultiLineLint( |
| 1697 |
""" |
| 1698 |
class Foo { |
| 1699 |
constexpr inline explicit Foo(int f); |
| 1700 |
};""", |
| 1701 |
"", |
| 1702 |
) |
| 1703 |
self.TestMultiLineLint( |
| 1704 |
""" |
| 1705 |
class Foo { |
| 1706 |
explicit constexpr inline Foo(int f); |
| 1707 |
};""", |
| 1708 |
"", |
| 1709 |
) |
| 1710 |
|
| 1711 |
self.TestMultiLineLint( |
| 1712 |
""" |
| 1713 |
struct Foo { |
| 1714 |
Foo(int f); |
| 1715 |
};""", |
| 1716 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1717 |
) |
| 1718 |
|
| 1719 |
self.TestMultiLineLint( |
| 1720 |
""" |
| 1721 |
template<typename T> class Foo { |
| 1722 |
Foo(int f); |
| 1723 |
};""", |
| 1724 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1725 |
) |
| 1726 |
|
| 1727 |
self.TestMultiLineLint( |
| 1728 |
""" |
| 1729 |
template<typename T> class Foo { |
| 1730 |
inline Foo(int f); |
| 1731 |
};""", |
| 1732 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1733 |
) |
| 1734 |
|
| 1735 |
self.TestMultiLineLint( |
| 1736 |
""" |
| 1737 |
class Foo { |
| 1738 |
Foo(int f = 0); |
| 1739 |
};""", |
| 1740 |
"Constructors callable with one argument should be marked explicit." |
| 1741 |
" [runtime/explicit] [4]", |
| 1742 |
) |
| 1743 |
|
| 1744 |
|
| 1745 |
self.TestMultiLineLint( |
| 1746 |
""" |
| 1747 |
class Foo { |
| 1748 |
Foo(int f, int g = 0); |
| 1749 |
};""", |
| 1750 |
"Constructors callable with one argument should be marked explicit." |
| 1751 |
" [runtime/explicit] [4]", |
| 1752 |
) |
| 1753 |
|
| 1754 |
|
| 1755 |
self.TestMultiLineLint( |
| 1756 |
""" |
| 1757 |
class Foo { |
| 1758 |
Foo(int f = 0, int g = 0); |
| 1759 |
};""", |
| 1760 |
"Constructors callable with one argument should be marked explicit." |
| 1761 |
" [runtime/explicit] [4]", |
| 1762 |
) |
| 1763 |
|
| 1764 |
self.TestMultiLineLint( |
| 1765 |
""" |
| 1766 |
class Foo { |
| 1767 |
explicit Foo(); |
| 1768 |
};""", |
| 1769 |
"", |
| 1770 |
) |
| 1771 |
|
| 1772 |
self.TestMultiLineLint( |
| 1773 |
""" |
| 1774 |
class Foo { |
| 1775 |
explicit Foo(void); |
| 1776 |
};""", |
| 1777 |
"", |
| 1778 |
) |
| 1779 |
|
| 1780 |
self.TestMultiLineLint( |
| 1781 |
""" |
| 1782 |
class Foo { |
| 1783 |
explicit Foo(int f, int g); |
| 1784 |
};""", |
| 1785 |
"", |
| 1786 |
) |
| 1787 |
self.TestMultiLineLint( |
| 1788 |
""" |
| 1789 |
class Foo { |
| 1790 |
explicit Foo(int f, int g = 0); |
| 1791 |
};""", |
| 1792 |
"", |
| 1793 |
) |
| 1794 |
|
| 1795 |
|
| 1796 |
self.TestMultiLineLint( |
| 1797 |
""" |
| 1798 |
class Foo { |
| 1799 |
Foo(void (*f)(int f, int g)); |
| 1800 |
};""", |
| 1801 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1802 |
) |
| 1803 |
|
| 1804 |
|
| 1805 |
self.TestMultiLineLint( |
| 1806 |
""" |
| 1807 |
template <typename T, typename S> |
| 1808 |
class Foo { |
| 1809 |
Foo(Bar<T, S> b); |
| 1810 |
};""", |
| 1811 |
"Single-parameter constructors should be marked explicit. [runtime/explicit] [4]", |
| 1812 |
) |
| 1813 |
|
| 1814 |
self.TestMultiLineLint( |
| 1815 |
""" |
| 1816 |
template <typename T, S> |
| 1817 |
class Foo { |
| 1818 |
Foo(Foo<T, S>& f); |
| 1819 |
};""", |
| 1820 |
"", |
| 1821 |
) |
| 1822 |
|
| 1823 |
self.TestMultiLineLint( |
| 1824 |
""" |
| 1825 |
class Foo { |
| 1826 |
explicit Foo(int f); |
| 1827 |
};""", |
| 1828 |
"", |
| 1829 |
) |
| 1830 |
|
| 1831 |
self.TestMultiLineLint( |
| 1832 |
""" |
| 1833 |
class Foo { |
| 1834 |
Foo(int f, int b); |
| 1835 |
};""", |
| 1836 |
"", |
| 1837 |
) |
| 1838 |
|
| 1839 |
self.TestMultiLineLint( |
| 1840 |
""" |
| 1841 |
class Foo { |
| 1842 |
Foo(int f, |
| 1843 |
int b); |
| 1844 |
};""", |
| 1845 |
"", |
| 1846 |
) |
| 1847 |
|
| 1848 |
self.TestMultiLineLint( |
| 1849 |
""" |
| 1850 |
class Foo { |
| 1851 |
aFoo(int f); |
| 1852 |
};""", |
| 1853 |
"", |
| 1854 |
) |
| 1855 |
|
| 1856 |
self.TestMultiLineLint( |
| 1857 |
""" |
| 1858 |
class Foo { |
| 1859 |
Foo(void); |
| 1860 |
};""", |
| 1861 |
"", |
| 1862 |
) |
| 1863 |
|
| 1864 |
self.TestMultiLineLint( |
| 1865 |
""" |
| 1866 |
class Foo { |
| 1867 |
Bar(int b); |
| 1868 |
};""", |
| 1869 |
"", |
| 1870 |
) |
| 1871 |
|
| 1872 |
self.TestMultiLineLint( |
| 1873 |
""" |
| 1874 |
class Foo { |
| 1875 |
// Foo(int f); |
| 1876 |
};""", |
| 1877 |
"", |
| 1878 |
) |
| 1879 |
|
| 1880 |
|
| 1881 |
self.TestMultiLineLint( |
| 1882 |
""" |
| 1883 |
class Foo { |
| 1884 |
Foo(int f, int b); |
| 1885 |
}; |
| 1886 |
Foo(int f);""", |
| 1887 |
"", |
| 1888 |
) |
| 1889 |
|
| 1890 |
self.TestMultiLineLint("""static Foo(int f);""", "") |
| 1891 |
|
| 1892 |
self.TestMultiLineLint( |
| 1893 |
""" |
| 1894 |
class Foo { |
| 1895 |
Foo(const Foo&); |
| 1896 |
};""", |
| 1897 |
"", |
| 1898 |
) |
| 1899 |
self.TestMultiLineLint( |
| 1900 |
""" |
| 1901 |
class Foo { |
| 1902 |
Foo(volatile Foo&); |
| 1903 |
};""", |
| 1904 |
"", |
| 1905 |
) |
| 1906 |
self.TestMultiLineLint( |
| 1907 |
""" |
| 1908 |
class Foo { |
| 1909 |
Foo(volatile const Foo&); |
| 1910 |
};""", |
| 1911 |
"", |
| 1912 |
) |
| 1913 |
self.TestMultiLineLint( |
| 1914 |
""" |
| 1915 |
class Foo { |
| 1916 |
Foo(const volatile Foo&); |
| 1917 |
};""", |
| 1918 |
"", |
| 1919 |
) |
| 1920 |
self.TestMultiLineLint( |
| 1921 |
""" |
| 1922 |
class Foo { |
| 1923 |
Foo(Foo const&); |
| 1924 |
};""", |
| 1925 |
"", |
| 1926 |
) |
| 1927 |
self.TestMultiLineLint( |
| 1928 |
""" |
| 1929 |
class Foo { |
| 1930 |
Foo(Foo&); |
| 1931 |
};""", |
| 1932 |
"", |
| 1933 |
) |
| 1934 |
|
| 1935 |
self.TestMultiLineLint( |
| 1936 |
""" |
| 1937 |
template<typename T> class Foo { |
| 1938 |
Foo(const Foo<T>&); |
| 1939 |
};""", |
| 1940 |
"", |
| 1941 |
) |
| 1942 |
|
| 1943 |
self.TestMultiLineLint( |
| 1944 |
""" |
| 1945 |
class Foo { |
| 1946 |
Foo(std::initializer_list<T> &arg) {} |
| 1947 |
};""", |
| 1948 |
"", |
| 1949 |
) |
| 1950 |
|
| 1951 |
error_collector = ErrorCollector(self.assertTrue) |
| 1952 |
cpplint.ProcessFileData( |
| 1953 |
"foo.cc", |
| 1954 |
"cc", |
| 1955 |
[ |
| 1956 |
"class Foo {", |
| 1957 |
" template<typename... Args>", |
| 1958 |
" explicit Foo(const int arg, Args&&... args) {}", |
| 1959 |
"};", |
| 1960 |
], |
| 1961 |
error_collector, |
| 1962 |
) |
| 1963 |
assert ( |
| 1964 |
error_collector.ResultList().count( |
| 1965 |
"Constructors that require multiple arguments should not be marked explicit. " |
| 1966 |
"[runtime/explicit] [0]" |
| 1967 |
) |
| 1968 |
== 0 |
| 1969 |
) |
| 1970 |
error_collector = ErrorCollector(self.assertTrue) |
| 1971 |
cpplint.ProcessFileData( |
| 1972 |
"foo.cc", |
| 1973 |
"cc", |
| 1974 |
[ |
| 1975 |
"class Foo {", |
| 1976 |
" template<typename... Args>", |
| 1977 |
" explicit Foo(Args&&... args) {}", |
| 1978 |
"};", |
| 1979 |
], |
| 1980 |
error_collector, |
| 1981 |
) |
| 1982 |
assert ( |
| 1983 |
error_collector.ResultList().count( |
| 1984 |
"Constructors that require multiple arguments should not be marked explicit. " |
| 1985 |
"[runtime/explicit] [0]" |
| 1986 |
) |
| 1987 |
== 0 |
| 1988 |
) |
| 1989 |
error_collector = ErrorCollector(self.assertTrue) |
| 1990 |
cpplint.ProcessFileData( |
| 1991 |
"foo.cc", |
| 1992 |
"cc", |
| 1993 |
[ |
| 1994 |
"class Foo {", |
| 1995 |
" template<typename... Args>", |
| 1996 |
" Foo(const int arg, Args&&... args) {}", |
| 1997 |
"};", |
| 1998 |
], |
| 1999 |
error_collector, |
| 2000 |
) |
| 2001 |
assert ( |
| 2002 |
error_collector.ResultList().count( |
| 2003 |
"Constructors callable with one argument should be marked explicit. " |
| 2004 |
"[runtime/explicit] [4]" |
| 2005 |
) |
| 2006 |
== 1 |
| 2007 |
) |
| 2008 |
error_collector = ErrorCollector(self.assertTrue) |
| 2009 |
cpplint.ProcessFileData( |
| 2010 |
"foo.cc", |
| 2011 |
"cc", |
| 2012 |
["class Foo {", " template<typename... Args>", " Foo(Args&&... args) {}", "};"], |
| 2013 |
error_collector, |
| 2014 |
) |
| 2015 |
assert ( |
| 2016 |
error_collector.ResultList().count( |
| 2017 |
"Constructors callable with one argument should be marked explicit. " |
| 2018 |
"[runtime/explicit] [4]" |
| 2019 |
) |
| 2020 |
== 1 |
| 2021 |
) |
| 2022 |
|
| 2023 |
error_collector = ErrorCollector(self.assertTrue) |
| 2024 |
cpplint.ProcessFileData( |
| 2025 |
"foo.cc", |
| 2026 |
"cc", |
| 2027 |
[ |
| 2028 |
"void Func() {", |
| 2029 |
" __asm__ (", |
| 2030 |
' "hlt"', |
| 2031 |
" );", |
| 2032 |
" asm {", |
| 2033 |
" movdqa [edx + 32], xmm2", |
| 2034 |
" }", |
| 2035 |
"}", |
| 2036 |
], |
| 2037 |
error_collector, |
| 2038 |
) |
| 2039 |
assert ( |
| 2040 |
error_collector.ResultList().count( |
| 2041 |
"Extra space before ( in function call [whitespace/parens] [4]" |
| 2042 |
) |
| 2043 |
== 0 |
| 2044 |
) |
| 2045 |
assert ( |
| 2046 |
error_collector.ResultList().count( |
| 2047 |
"Closing ) should be moved to the previous line [whitespace/parens] [2]" |
| 2048 |
) |
| 2049 |
== 0 |
| 2050 |
) |
| 2051 |
assert ( |
| 2052 |
error_collector.ResultList().count("Extra space before [ [whitespace/braces] [5]") |
| 2053 |
== 0 |
| 2054 |
) |
| 2055 |
finally: |
| 2056 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 2057 |
|
| 2058 |
def testSlashStarCommentOnSingleLine(self): |
| 2059 |
self.TestMultiLineLint("""/* static */ Foo(int f);""", "") |
| 2060 |
self.TestMultiLineLint("""/*/ static */ Foo(int f);""", "") |
| 2061 |
self.TestMultiLineLint( |
| 2062 |
"""/*/ static Foo(int f);""", |
| 2063 |
"Could not find end of multi-line comment [readability/multiline_comment] [5]", |
| 2064 |
) |
| 2065 |
self.TestMultiLineLint( |
| 2066 |
""" /*/ static Foo(int f);""", |
| 2067 |
"Could not find end of multi-line comment [readability/multiline_comment] [5]", |
| 2068 |
) |
| 2069 |
self.TestMultiLineLint(""" /**/ static Foo(int f);""", "") |
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
def testSuspiciousUsageOfIf(self): |
| 2078 |
self.TestLint(" if (a == b) {", "") |
| 2079 |
self.TestLint( |
| 2080 |
" } if (a == b) {", |
| 2081 |
'Did you mean "else if"? If not, start a new line for "if". [readability/braces] [4]', |
| 2082 |
) |
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
def testSuspiciousUsageOfMemset(self): |
| 2087 |
|
| 2088 |
self.TestLint(" memset(buf, 0, sizeof(buf))", "") |
| 2089 |
|
| 2090 |
|
| 2091 |
self.TestLint( |
| 2092 |
" memset(buf, sizeof(buf), 0)", |
| 2093 |
'Did you mean "memset(buf, 0, sizeof(buf))"? [runtime/memset] [4]', |
| 2094 |
) |
| 2095 |
self.TestLint( |
| 2096 |
" memset(buf, xsize * ysize, 0)", |
| 2097 |
'Did you mean "memset(buf, 0, xsize * ysize)"? [runtime/memset] [4]', |
| 2098 |
) |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
self.TestLint(" memset(buf, 'y', 0)", "") |
| 2103 |
self.TestLint(" memset(buf, 4, 0)", "") |
| 2104 |
self.TestLint(" memset(buf, -1, 0)", "") |
| 2105 |
self.TestLint(" memset(buf, 0xF1, 0)", "") |
| 2106 |
self.TestLint(" memset(buf, 0xcd, 0)", "") |
| 2107 |
|
| 2108 |
def testRedundantVirtual(self): |
| 2109 |
self.TestLint("virtual void F()", "") |
| 2110 |
self.TestLint("virtual void F();", "") |
| 2111 |
self.TestLint("virtual void F() {}", "") |
| 2112 |
|
| 2113 |
message_template = ( |
| 2114 |
'"%s" is redundant since function is already ' |
| 2115 |
'declared as "%s" [readability/inheritance] [4]' |
| 2116 |
) |
| 2117 |
for virt_specifier in ["override", "final"]: |
| 2118 |
error_message = message_template % ("virtual", virt_specifier) |
| 2119 |
self.TestLint("virtual int F() %s" % virt_specifier, error_message) |
| 2120 |
self.TestLint("virtual int F() %s;" % virt_specifier, error_message) |
| 2121 |
self.TestLint("virtual int F() %s {" % virt_specifier, error_message) |
| 2122 |
|
| 2123 |
error_collector = ErrorCollector(self.assertTrue) |
| 2124 |
cpplint.ProcessFileData( |
| 2125 |
"foo.cc", |
| 2126 |
"cc", |
| 2127 |
[ |
| 2128 |
"// Copyright 2014 Your Company.", |
| 2129 |
"virtual void F(int a,", |
| 2130 |
" int b) " + virt_specifier + ";", |
| 2131 |
"virtual void F(int a,", |
| 2132 |
" int b) LOCKS_EXCLUDED(lock) " + virt_specifier + ";", |
| 2133 |
"virtual void F(int a,", |
| 2134 |
" int b)", |
| 2135 |
" LOCKS_EXCLUDED(lock) " + virt_specifier + ";", |
| 2136 |
"", |
| 2137 |
], |
| 2138 |
error_collector, |
| 2139 |
) |
| 2140 |
assert [error_message, error_message, error_message] == error_collector.Results() |
| 2141 |
|
| 2142 |
error_message = message_template % ("override", "final") |
| 2143 |
self.TestLint("int F() override final", error_message) |
| 2144 |
self.TestLint("int F() override final;", error_message) |
| 2145 |
self.TestLint("int F() override final {}", error_message) |
| 2146 |
self.TestLint("int F() final override", error_message) |
| 2147 |
self.TestLint("int F() final override;", error_message) |
| 2148 |
self.TestLint("int F() final override {}", error_message) |
| 2149 |
|
| 2150 |
error_collector = ErrorCollector(self.assertTrue) |
| 2151 |
cpplint.ProcessFileData( |
| 2152 |
"foo.cc", |
| 2153 |
"cc", |
| 2154 |
[ |
| 2155 |
"// Copyright 2014 Your Company.", |
| 2156 |
"struct A : virtual B {", |
| 2157 |
" ~A() override;};", |
| 2158 |
"class C", |
| 2159 |
" : public D,", |
| 2160 |
" public virtual E {", |
| 2161 |
" void Func() override;", |
| 2162 |
"}", |
| 2163 |
"", |
| 2164 |
], |
| 2165 |
error_collector, |
| 2166 |
) |
| 2167 |
assert error_collector.Results() == "" |
| 2168 |
|
| 2169 |
self.TestLint("void Finalize(AnnotationProto *final) override;", "") |
| 2170 |
|
| 2171 |
def testCheckDeprecated(self): |
| 2172 |
self.TestLanguageRulesCheck("foo_test.cc", "#include <iostream>", "") |
| 2173 |
self.TestLanguageRulesCheck("foo_unittest.cc", "#include <iostream>", "") |
| 2174 |
|
| 2175 |
def testCheckPosixThreading(self): |
| 2176 |
self.TestLint("var = sctime_r()", "") |
| 2177 |
self.TestLint("var = strtok_r()", "") |
| 2178 |
self.TestLint("var = strtok_r(foo, ba, r)", "") |
| 2179 |
self.TestLint("var = brand()", "") |
| 2180 |
self.TestLint("_rand()", "") |
| 2181 |
self.TestLint(".rand()", "") |
| 2182 |
self.TestLint("->rand()", "") |
| 2183 |
self.TestLint("ACMRandom rand(seed)", "") |
| 2184 |
self.TestLint("ISAACRandom rand()", "") |
| 2185 |
self.TestLint( |
| 2186 |
"var = rand()", |
| 2187 |
"Consider using rand_r(...) instead of rand(...)" |
| 2188 |
" for improved thread safety." |
| 2189 |
" [runtime/threadsafe_fn] [2]", |
| 2190 |
) |
| 2191 |
self.TestLint( |
| 2192 |
"var = strtok(str, delim)", |
| 2193 |
"Consider using strtok_r(...) " |
| 2194 |
"instead of strtok(...)" |
| 2195 |
" for improved thread safety." |
| 2196 |
" [runtime/threadsafe_fn] [2]", |
| 2197 |
) |
| 2198 |
|
| 2199 |
def testVlogMisuse(self): |
| 2200 |
self.TestLint("VLOG(1)", "") |
| 2201 |
self.TestLint("VLOG(99)", "") |
| 2202 |
self.TestLint("LOG(ERROR)", "") |
| 2203 |
self.TestLint("LOG(INFO)", "") |
| 2204 |
self.TestLint("LOG(WARNING)", "") |
| 2205 |
self.TestLint("LOG(FATAL)", "") |
| 2206 |
self.TestLint("LOG(DFATAL)", "") |
| 2207 |
self.TestLint("VLOG(SOMETHINGWEIRD)", "") |
| 2208 |
self.TestLint("MYOWNVLOG(ERROR)", "") |
| 2209 |
errmsg = ( |
| 2210 |
"VLOG() should be used with numeric verbosity level. " |
| 2211 |
"Use LOG() if you want symbolic severity levels." |
| 2212 |
" [runtime/vlog] [5]" |
| 2213 |
) |
| 2214 |
self.TestLint("VLOG(ERROR)", errmsg) |
| 2215 |
self.TestLint("VLOG(INFO)", errmsg) |
| 2216 |
self.TestLint("VLOG(WARNING)", errmsg) |
| 2217 |
self.TestLint("VLOG(FATAL)", errmsg) |
| 2218 |
self.TestLint("VLOG(DFATAL)", errmsg) |
| 2219 |
self.TestLint(" VLOG(ERROR)", errmsg) |
| 2220 |
self.TestLint(" VLOG(INFO)", errmsg) |
| 2221 |
self.TestLint(" VLOG(WARNING)", errmsg) |
| 2222 |
self.TestLint(" VLOG(FATAL)", errmsg) |
| 2223 |
self.TestLint(" VLOG(DFATAL)", errmsg) |
| 2224 |
|
| 2225 |
|
| 2226 |
def testFormatStrings(self): |
| 2227 |
self.TestLint('printf("foo")', "") |
| 2228 |
self.TestLint('printf("foo: %s", foo)', "") |
| 2229 |
self.TestLint("DocidForPrintf(docid)", "") |
| 2230 |
self.TestLint("printf(format, value)", "") |
| 2231 |
self.TestLint("printf(__VA_ARGS__)", "") |
| 2232 |
self.TestLint("printf(format.c_str(), value)", "") |
| 2233 |
self.TestLint("printf(format(index).c_str(), value)", "") |
| 2234 |
self.TestLint( |
| 2235 |
"printf(foo)", |
| 2236 |
'Potential format string bug. Do printf("%s", foo) instead. [runtime/printf] [4]', |
| 2237 |
) |
| 2238 |
self.TestLint( |
| 2239 |
"printf(foo.c_str())", |
| 2240 |
"Potential format string bug. " |
| 2241 |
'Do printf("%s", foo.c_str()) instead.' |
| 2242 |
" [runtime/printf] [4]", |
| 2243 |
) |
| 2244 |
self.TestLint( |
| 2245 |
"printf(foo->c_str())", |
| 2246 |
"Potential format string bug. " |
| 2247 |
'Do printf("%s", foo->c_str()) instead.' |
| 2248 |
" [runtime/printf] [4]", |
| 2249 |
) |
| 2250 |
self.TestLint( |
| 2251 |
"StringPrintf(foo)", |
| 2252 |
'Potential format string bug. Do StringPrintf("%s", foo) instead.' |
| 2253 |
"" |
| 2254 |
" [runtime/printf] [4]", |
| 2255 |
) |
| 2256 |
|
| 2257 |
|
| 2258 |
def testIllegalOperatorOverloading(self): |
| 2259 |
errmsg = "Unary operator& is dangerous. Do not use it. [runtime/operator] [4]" |
| 2260 |
self.TestLint("void operator=(const Myclass&)", "") |
| 2261 |
self.TestLint("void operator&(int a, int b)", "") |
| 2262 |
self.TestLint("void operator&() { }", errmsg) |
| 2263 |
self.TestLint( |
| 2264 |
"void operator & ( ) { }", ["Extra space after ( [whitespace/parens] [2]", errmsg] |
| 2265 |
) |
| 2266 |
|
| 2267 |
|
| 2268 |
def testConstStringReferenceMembers(self): |
| 2269 |
errmsg = ( |
| 2270 |
"const string& members are dangerous. It is much better to use " |
| 2271 |
"alternatives, such as pointers or simple constants." |
| 2272 |
" [runtime/member_string_references] [2]" |
| 2273 |
) |
| 2274 |
|
| 2275 |
members_declarations = [ |
| 2276 |
"const string& church", |
| 2277 |
"const string &turing", |
| 2278 |
"const string & godel", |
| 2279 |
] |
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
self.TestLint("void f(const string&)", "") |
| 2291 |
self.TestLint("const string& f(const string& a, const string& b)", "") |
| 2292 |
self.TestLint("typedef const string& A;", "") |
| 2293 |
|
| 2294 |
for decl in members_declarations: |
| 2295 |
self.TestLint(decl + " = b;", "") |
| 2296 |
self.TestLint(decl + " =", "") |
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
for decl in members_declarations: |
| 2301 |
self.TestLint(decl + ";", errmsg) |
| 2302 |
|
| 2303 |
|
| 2304 |
def testVariableLengthArrayDetection(self): |
| 2305 |
errmsg = ( |
| 2306 |
"Do not use variable-length arrays. Use an appropriately named " |
| 2307 |
"('k' followed by CamelCase) compile-time constant for the size." |
| 2308 |
" [runtime/arrays] [1]" |
| 2309 |
) |
| 2310 |
|
| 2311 |
self.TestLint("int a[any_old_variable];", errmsg) |
| 2312 |
self.TestLint("int doublesize[some_var * 2];", errmsg) |
| 2313 |
self.TestLint("int a[afunction()];", errmsg) |
| 2314 |
self.TestLint("int a[function(kMaxFooBars)];", errmsg) |
| 2315 |
self.TestLint("bool a_list[items_->size()];", errmsg) |
| 2316 |
self.TestLint("namespace::Type buffer[len+1];", errmsg) |
| 2317 |
|
| 2318 |
self.TestLint("int a[64];", "") |
| 2319 |
self.TestLint("int a[0xFF];", "") |
| 2320 |
self.TestLint("int first[256], second[256];", "") |
| 2321 |
self.TestLint("int array_name[kCompileTimeConstant];", "") |
| 2322 |
self.TestLint("char buf[somenamespace::kBufSize];", "") |
| 2323 |
self.TestLint("int array_name[ALL_CAPS];", "") |
| 2324 |
self.TestLint("AClass array1[foo::bar::ALL_CAPS];", "") |
| 2325 |
self.TestLint("int a[kMaxStrLen + 1];", "") |
| 2326 |
self.TestLint("int a[sizeof(foo)];", "") |
| 2327 |
self.TestLint("int a[sizeof(*foo)];", "") |
| 2328 |
self.TestLint("int a[sizeof foo];", "") |
| 2329 |
self.TestLint("int a[sizeof(struct Foo)];", "") |
| 2330 |
self.TestLint("int a[128 - sizeof(const bar)];", "") |
| 2331 |
self.TestLint("int a[(sizeof(foo) * 4)];", "") |
| 2332 |
self.TestLint("int a[(arraysize(fixed_size_array)/2) << 1];", "") |
| 2333 |
self.TestLint("delete a[some_var];", "") |
| 2334 |
self.TestLint("return a[some_var];", "") |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
def testDisallowMacrosAtEnd(self): |
| 2339 |
for macro_name in ("DISALLOW_COPY_AND_ASSIGN", "DISALLOW_IMPLICIT_CONSTRUCTORS"): |
| 2340 |
error_collector = ErrorCollector(self.assertTrue) |
| 2341 |
cpplint.ProcessFileData( |
| 2342 |
"foo.cc", |
| 2343 |
"cc", |
| 2344 |
[ |
| 2345 |
"// Copyright 2014 Your Company.", |
| 2346 |
"class SomeClass {", |
| 2347 |
" private:", |
| 2348 |
" %s(SomeClass);" % macro_name, |
| 2349 |
" int member_;", |
| 2350 |
"};", |
| 2351 |
"", |
| 2352 |
], |
| 2353 |
error_collector, |
| 2354 |
) |
| 2355 |
assert ( |
| 2356 |
"%s should be the last thing in the class" % macro_name |
| 2357 |
+ " [readability/constructors] [3]" |
| 2358 |
== error_collector.Results() |
| 2359 |
) |
| 2360 |
|
| 2361 |
error_collector = ErrorCollector(self.assertTrue) |
| 2362 |
cpplint.ProcessFileData( |
| 2363 |
"foo.cc", |
| 2364 |
"cc", |
| 2365 |
[ |
| 2366 |
"// Copyright 2014 Your Company.", |
| 2367 |
"class OuterClass {", |
| 2368 |
" private:", |
| 2369 |
" struct InnerClass {", |
| 2370 |
" private:", |
| 2371 |
" %s(InnerClass);" % macro_name, |
| 2372 |
" int member;", |
| 2373 |
" };", |
| 2374 |
"};", |
| 2375 |
"", |
| 2376 |
], |
| 2377 |
error_collector, |
| 2378 |
) |
| 2379 |
assert ( |
| 2380 |
"%s should be the last thing in the class" % macro_name |
| 2381 |
+ " [readability/constructors] [3]" |
| 2382 |
== error_collector.Results() |
| 2383 |
) |
| 2384 |
|
| 2385 |
error_collector = ErrorCollector(self.assertTrue) |
| 2386 |
cpplint.ProcessFileData( |
| 2387 |
"foo.cc", |
| 2388 |
"cc", |
| 2389 |
[ |
| 2390 |
"// Copyright 2014 Your Company.", |
| 2391 |
"class OuterClass1 {", |
| 2392 |
" private:", |
| 2393 |
" struct InnerClass1 {", |
| 2394 |
" private:", |
| 2395 |
" %s(InnerClass1);" % macro_name, |
| 2396 |
" };", |
| 2397 |
" %s(OuterClass1);" % macro_name, |
| 2398 |
"};", |
| 2399 |
"struct OuterClass2 {", |
| 2400 |
" private:", |
| 2401 |
" class InnerClass2 {", |
| 2402 |
" private:", |
| 2403 |
" %s(InnerClass2);" % macro_name, |
| 2404 |
" // comment", |
| 2405 |
" };", |
| 2406 |
"", |
| 2407 |
" %s(OuterClass2);" % macro_name, |
| 2408 |
"", |
| 2409 |
" // comment", |
| 2410 |
"};", |
| 2411 |
"void Func() {", |
| 2412 |
" struct LocalClass {", |
| 2413 |
" private:", |
| 2414 |
" %s(LocalClass);" % macro_name, |
| 2415 |
" } variable;", |
| 2416 |
"}", |
| 2417 |
"", |
| 2418 |
], |
| 2419 |
error_collector, |
| 2420 |
) |
| 2421 |
assert error_collector.Results() == "" |
| 2422 |
|
| 2423 |
|
| 2424 |
def testBraces(self): |
| 2425 |
|
| 2426 |
|
| 2427 |
self.TestLint("int a[3] = { 1, 2, 3 };", "") |
| 2428 |
self.TestLint( |
| 2429 |
"""const int foo[] = |
| 2430 |
{1, 2, 3 };""", |
| 2431 |
"", |
| 2432 |
) |
| 2433 |
|
| 2434 |
self.TestMultiLineLint( |
| 2435 |
"""int a[3] = { 1, |
| 2436 |
2, |
| 2437 |
3 };""", |
| 2438 |
"", |
| 2439 |
) |
| 2440 |
self.TestMultiLineLint( |
| 2441 |
"""int a[2][3] = { { 1, 2 }, |
| 2442 |
{ 3, 4 } };""", |
| 2443 |
"", |
| 2444 |
) |
| 2445 |
self.TestMultiLineLint( |
| 2446 |
"""int a[2][3] = |
| 2447 |
{ { 1, 2 }, |
| 2448 |
{ 3, 4 } };""", |
| 2449 |
"", |
| 2450 |
) |
| 2451 |
self.TestMultiLineLint( |
| 2452 |
"""if (foo) { |
| 2453 |
bar; |
| 2454 |
} |
| 2455 |
else { |
| 2456 |
baz; |
| 2457 |
}""", |
| 2458 |
"An else should appear on the same line as the preceding } [whitespace/newline] [4]", |
| 2459 |
) |
| 2460 |
|
| 2461 |
|
| 2462 |
def testCheckCheck(self): |
| 2463 |
self.TestLint( |
| 2464 |
"CHECK(x == 42);", |
| 2465 |
"Consider using CHECK_EQ instead of CHECK(a == b) [readability/check] [2]", |
| 2466 |
) |
| 2467 |
self.TestLint( |
| 2468 |
"CHECK(x != 42);", |
| 2469 |
"Consider using CHECK_NE instead of CHECK(a != b) [readability/check] [2]", |
| 2470 |
) |
| 2471 |
self.TestLint( |
| 2472 |
"CHECK(x >= 42);", |
| 2473 |
"Consider using CHECK_GE instead of CHECK(a >= b) [readability/check] [2]", |
| 2474 |
) |
| 2475 |
self.TestLint( |
| 2476 |
"CHECK(x > 42);", |
| 2477 |
"Consider using CHECK_GT instead of CHECK(a > b) [readability/check] [2]", |
| 2478 |
) |
| 2479 |
self.TestLint( |
| 2480 |
"CHECK(x <= 42);", |
| 2481 |
"Consider using CHECK_LE instead of CHECK(a <= b) [readability/check] [2]", |
| 2482 |
) |
| 2483 |
self.TestLint( |
| 2484 |
"CHECK(x < 42);", |
| 2485 |
"Consider using CHECK_LT instead of CHECK(a < b) [readability/check] [2]", |
| 2486 |
) |
| 2487 |
|
| 2488 |
self.TestLint( |
| 2489 |
"DCHECK(x == 42);", |
| 2490 |
"Consider using DCHECK_EQ instead of DCHECK(a == b) [readability/check] [2]", |
| 2491 |
) |
| 2492 |
self.TestLint( |
| 2493 |
"DCHECK(x != 42);", |
| 2494 |
"Consider using DCHECK_NE instead of DCHECK(a != b) [readability/check] [2]", |
| 2495 |
) |
| 2496 |
self.TestLint( |
| 2497 |
"DCHECK(x >= 42);", |
| 2498 |
"Consider using DCHECK_GE instead of DCHECK(a >= b) [readability/check] [2]", |
| 2499 |
) |
| 2500 |
self.TestLint( |
| 2501 |
"DCHECK(x > 42);", |
| 2502 |
"Consider using DCHECK_GT instead of DCHECK(a > b) [readability/check] [2]", |
| 2503 |
) |
| 2504 |
self.TestLint( |
| 2505 |
"DCHECK(x <= 42);", |
| 2506 |
"Consider using DCHECK_LE instead of DCHECK(a <= b) [readability/check] [2]", |
| 2507 |
) |
| 2508 |
self.TestLint( |
| 2509 |
"DCHECK(x < 42);", |
| 2510 |
"Consider using DCHECK_LT instead of DCHECK(a < b) [readability/check] [2]", |
| 2511 |
) |
| 2512 |
|
| 2513 |
self.TestLint( |
| 2514 |
'EXPECT_TRUE("42" == x);', |
| 2515 |
"Consider using EXPECT_EQ instead of EXPECT_TRUE(a == b) [readability/check] [2]", |
| 2516 |
) |
| 2517 |
self.TestLint( |
| 2518 |
'EXPECT_TRUE("42" != x);', |
| 2519 |
"Consider using EXPECT_NE instead of EXPECT_TRUE(a != b) [readability/check] [2]", |
| 2520 |
) |
| 2521 |
self.TestLint( |
| 2522 |
"EXPECT_TRUE(+42 >= x);", |
| 2523 |
"Consider using EXPECT_GE instead of EXPECT_TRUE(a >= b) [readability/check] [2]", |
| 2524 |
) |
| 2525 |
|
| 2526 |
self.TestLint( |
| 2527 |
"EXPECT_FALSE(x == 42);", |
| 2528 |
"Consider using EXPECT_NE instead of EXPECT_FALSE(a == b) [readability/check] [2]", |
| 2529 |
) |
| 2530 |
self.TestLint( |
| 2531 |
"EXPECT_FALSE(x != 42);", |
| 2532 |
"Consider using EXPECT_EQ instead of EXPECT_FALSE(a != b) [readability/check] [2]", |
| 2533 |
) |
| 2534 |
self.TestLint( |
| 2535 |
"EXPECT_FALSE(x >= 42);", |
| 2536 |
"Consider using EXPECT_LT instead of EXPECT_FALSE(a >= b) [readability/check] [2]", |
| 2537 |
) |
| 2538 |
self.TestLint( |
| 2539 |
"ASSERT_FALSE(x > 42);", |
| 2540 |
"Consider using ASSERT_LE instead of ASSERT_FALSE(a > b) [readability/check] [2]", |
| 2541 |
) |
| 2542 |
self.TestLint( |
| 2543 |
"ASSERT_FALSE(x <= 42);", |
| 2544 |
"Consider using ASSERT_GT instead of ASSERT_FALSE(a <= b) [readability/check] [2]", |
| 2545 |
) |
| 2546 |
|
| 2547 |
self.TestLint( |
| 2548 |
"CHECK(x<42);", |
| 2549 |
[ |
| 2550 |
"Missing spaces around < [whitespace/operators] [3]", |
| 2551 |
"Consider using CHECK_LT instead of CHECK(a < b) [readability/check] [2]", |
| 2552 |
], |
| 2553 |
) |
| 2554 |
self.TestLint( |
| 2555 |
"CHECK(x>42);", |
| 2556 |
[ |
| 2557 |
"Missing spaces around > [whitespace/operators] [3]", |
| 2558 |
"Consider using CHECK_GT instead of CHECK(a > b) [readability/check] [2]", |
| 2559 |
], |
| 2560 |
) |
| 2561 |
|
| 2562 |
self.TestLint("using some::namespace::operator<<;", "") |
| 2563 |
self.TestLint("using some::namespace::operator>>;", "") |
| 2564 |
|
| 2565 |
self.TestLint( |
| 2566 |
"CHECK(x->y == 42);", |
| 2567 |
"Consider using CHECK_EQ instead of CHECK(a == b) [readability/check] [2]", |
| 2568 |
) |
| 2569 |
|
| 2570 |
self.TestLint( |
| 2571 |
" EXPECT_TRUE(42 < x); // Random comment.", |
| 2572 |
"Consider using EXPECT_LT instead of EXPECT_TRUE(a < b) [readability/check] [2]", |
| 2573 |
) |
| 2574 |
self.TestLint( |
| 2575 |
"EXPECT_TRUE( 42 < x );", |
| 2576 |
[ |
| 2577 |
"Extra space after ( in function call [whitespace/parens] [4]", |
| 2578 |
"Extra space before ) [whitespace/parens] [2]", |
| 2579 |
"Consider using EXPECT_LT instead of EXPECT_TRUE(a < b) [readability/check] [2]", |
| 2580 |
], |
| 2581 |
) |
| 2582 |
|
| 2583 |
self.TestLint( |
| 2584 |
"CHECK(4'2 == x);", |
| 2585 |
"Consider using CHECK_EQ instead of CHECK(a == b) [readability/check] [2]", |
| 2586 |
) |
| 2587 |
|
| 2588 |
def testCheckCheckFalsePositives(self): |
| 2589 |
self.TestLint("CHECK(some_iterator == obj.end());", "") |
| 2590 |
self.TestLint("EXPECT_TRUE(some_iterator == obj.end());", "") |
| 2591 |
self.TestLint("EXPECT_FALSE(some_iterator == obj.end());", "") |
| 2592 |
self.TestLint("CHECK(some_pointer != NULL);", "") |
| 2593 |
self.TestLint("EXPECT_TRUE(some_pointer != NULL);", "") |
| 2594 |
self.TestLint("EXPECT_FALSE(some_pointer != NULL);", "") |
| 2595 |
|
| 2596 |
self.TestLint("CHECK(CreateTestFile(dir, (1 << 20)));", "") |
| 2597 |
self.TestLint("CHECK(CreateTestFile(dir, (1 >> 20)));", "") |
| 2598 |
|
| 2599 |
self.TestLint("CHECK(x ^ (y < 42));", "") |
| 2600 |
self.TestLint("CHECK((x > 42) ^ (x < 54));", "") |
| 2601 |
self.TestLint("CHECK(a && b < 42);", "") |
| 2602 |
self.TestLint("CHECK(42 < a && a < b);", "") |
| 2603 |
self.TestLint("SOFT_CHECK(x > 42);", "") |
| 2604 |
|
| 2605 |
self.TestMultiLineLint( |
| 2606 |
"""_STLP_DEFINE_BINARY_OP_CHECK(==, _OP_EQUAL); |
| 2607 |
_STLP_DEFINE_BINARY_OP_CHECK(!=, _OP_NOT_EQUAL); |
| 2608 |
_STLP_DEFINE_BINARY_OP_CHECK(<, _OP_LESS_THAN); |
| 2609 |
_STLP_DEFINE_BINARY_OP_CHECK(<=, _OP_LESS_EQUAL); |
| 2610 |
_STLP_DEFINE_BINARY_OP_CHECK(>, _OP_GREATER_THAN); |
| 2611 |
_STLP_DEFINE_BINARY_OP_CHECK(>=, _OP_GREATER_EQUAL); |
| 2612 |
_STLP_DEFINE_BINARY_OP_CHECK(+, _OP_PLUS); |
| 2613 |
_STLP_DEFINE_BINARY_OP_CHECK(*, _OP_TIMES); |
| 2614 |
_STLP_DEFINE_BINARY_OP_CHECK(/, _OP_DIVIDE); |
| 2615 |
_STLP_DEFINE_BINARY_OP_CHECK(-, _OP_SUBTRACT); |
| 2616 |
_STLP_DEFINE_BINARY_OP_CHECK(%, _OP_MOD);""", |
| 2617 |
"", |
| 2618 |
) |
| 2619 |
|
| 2620 |
self.TestLint('CHECK(x < 42) << "Custom error message";', "") |
| 2621 |
|
| 2622 |
|
| 2623 |
def testCheckAltTokens(self): |
| 2624 |
self.TestLint("true or true", "Use operator || instead of or [readability/alt_tokens] [2]") |
| 2625 |
self.TestLint( |
| 2626 |
"true and true", "Use operator && instead of and [readability/alt_tokens] [2]" |
| 2627 |
) |
| 2628 |
self.TestLint( |
| 2629 |
"if (not true)", "Use operator ! instead of not [readability/alt_tokens] [2]" |
| 2630 |
) |
| 2631 |
self.TestLint("1 bitor 1", "Use operator | instead of bitor [readability/alt_tokens] [2]") |
| 2632 |
self.TestLint("1 xor 1", "Use operator ^ instead of xor [readability/alt_tokens] [2]") |
| 2633 |
self.TestLint( |
| 2634 |
"1 bitand 1", "Use operator & instead of bitand [readability/alt_tokens] [2]" |
| 2635 |
) |
| 2636 |
self.TestLint( |
| 2637 |
"x = compl 1", "Use operator ~ instead of compl [readability/alt_tokens] [2]" |
| 2638 |
) |
| 2639 |
self.TestLint( |
| 2640 |
"x and_eq y", "Use operator &= instead of and_eq [readability/alt_tokens] [2]" |
| 2641 |
) |
| 2642 |
self.TestLint("x or_eq y", "Use operator |= instead of or_eq [readability/alt_tokens] [2]") |
| 2643 |
self.TestLint( |
| 2644 |
"x xor_eq y", "Use operator ^= instead of xor_eq [readability/alt_tokens] [2]" |
| 2645 |
) |
| 2646 |
self.TestLint( |
| 2647 |
"x not_eq y", "Use operator != instead of not_eq [readability/alt_tokens] [2]" |
| 2648 |
) |
| 2649 |
self.TestLint( |
| 2650 |
"line_continuation or", "Use operator || instead of or [readability/alt_tokens] [2]" |
| 2651 |
) |
| 2652 |
self.TestLint( |
| 2653 |
"if(true and(parentheses", |
| 2654 |
"Use operator && instead of and [readability/alt_tokens] [2]", |
| 2655 |
) |
| 2656 |
|
| 2657 |
self.TestLint('#include "base/false-and-false.h"', "") |
| 2658 |
self.TestLint("#error false or false", "") |
| 2659 |
self.TestLint("false nor false", "") |
| 2660 |
self.TestLint("false nand false", "") |
| 2661 |
|
| 2662 |
|
| 2663 |
def testNonConstReference(self): |
| 2664 |
|
| 2665 |
operand_error_message = ( |
| 2666 |
"Is this a non-const reference? " |
| 2667 |
"If so, make const or use a pointer: %s" |
| 2668 |
" [runtime/references] [2]" |
| 2669 |
) |
| 2670 |
|
| 2671 |
self.TestLint( |
| 2672 |
"bool operator>(Foo& s, Foo& f);", |
| 2673 |
[operand_error_message % "Foo& s", operand_error_message % "Foo& f"], |
| 2674 |
) |
| 2675 |
self.TestLint( |
| 2676 |
"bool operator+(Foo& s, Foo& f);", |
| 2677 |
[operand_error_message % "Foo& s", operand_error_message % "Foo& f"], |
| 2678 |
) |
| 2679 |
self.TestLint("int len(Foo& s);", operand_error_message % "Foo& s") |
| 2680 |
|
| 2681 |
self.TestLint("stream& operator>>(stream& s, Foo& f);", "") |
| 2682 |
self.TestLint("stream& operator<<(stream& s, Foo& f);", "") |
| 2683 |
self.TestLint("void swap(Bar& a, Bar& b);", "") |
| 2684 |
self.TestLint("ostream& LogFunc(ostream& s);", "") |
| 2685 |
self.TestLint("ostringstream& LogFunc(ostringstream& s);", "") |
| 2686 |
self.TestLint("istream& LogFunc(istream& s);", "") |
| 2687 |
self.TestLint("istringstream& LogFunc(istringstream& s);", "") |
| 2688 |
|
| 2689 |
self.TestLint("int& g();", "") |
| 2690 |
|
| 2691 |
self.TestLint("void foo(const struct tm& tm);", "") |
| 2692 |
|
| 2693 |
self.TestLint("void foo(const typename tm& tm);", "") |
| 2694 |
|
| 2695 |
self.TestLint("void foo(const Bar* const& p) {", "") |
| 2696 |
self.TestLint("void foo(Bar const* const& p) {", "") |
| 2697 |
self.TestLint("void foo(Bar* const& p) {", "") |
| 2698 |
|
| 2699 |
self.TestLint("void foo(const std::vector<std::string>& v);", "") |
| 2700 |
|
| 2701 |
self.TestLint("void foo(Bar*& p);", operand_error_message % "Bar*& p") |
| 2702 |
self.TestLint("void foo(const Bar*& p);", operand_error_message % "const Bar*& p") |
| 2703 |
self.TestLint("void foo(Bar const*& p);", operand_error_message % "Bar const*& p") |
| 2704 |
self.TestLint("void foo(struct Bar*& p);", operand_error_message % "struct Bar*& p") |
| 2705 |
self.TestLint( |
| 2706 |
"void foo(const struct Bar*& p);", operand_error_message % "const struct Bar*& p" |
| 2707 |
) |
| 2708 |
self.TestLint( |
| 2709 |
"void foo(struct Bar const*& p);", operand_error_message % "struct Bar const*& p" |
| 2710 |
) |
| 2711 |
|
| 2712 |
self.TestLint( |
| 2713 |
"void foo(std::vector<int>& p);", operand_error_message % "std::vector<int>& p" |
| 2714 |
) |
| 2715 |
|
| 2716 |
self.TestLint("return &something;", "") |
| 2717 |
self.TestLint( |
| 2718 |
"if (condition) {return &something; }", |
| 2719 |
"Controlled statements inside brackets of if clause should be on a separate line" |
| 2720 |
" [whitespace/newline] [5]", |
| 2721 |
) |
| 2722 |
self.TestLint("if (condition) return &something;", "") |
| 2723 |
self.TestLint("if (condition) address = &something;", "") |
| 2724 |
self.TestLint("if (condition) result = lhs&rhs;", "") |
| 2725 |
self.TestLint("if (condition) result = lhs & rhs;", "") |
| 2726 |
self.TestLint("a = (b+c) * sizeof &f;", "") |
| 2727 |
self.TestLint("a = MySize(b) * sizeof &f;", "") |
| 2728 |
|
| 2729 |
self.TestLint("for (const string& s : c)", "") |
| 2730 |
self.TestLint("for (auto& r : c)", "") |
| 2731 |
self.TestLint("for (typename Type& a : b)", "") |
| 2732 |
|
| 2733 |
self.TestLint("T& operator=(const T& t);", "") |
| 2734 |
self.TestLint("int g() { return (a & b); }", "") |
| 2735 |
self.TestLint("T& r = (T&)*(vp());", "") |
| 2736 |
self.TestLint("T& r = v", "") |
| 2737 |
self.TestLint('static_assert((kBits & kMask) == 0, "text");', "") |
| 2738 |
self.TestLint("COMPILE_ASSERT((kBits & kMask) == 0, text);", "") |
| 2739 |
|
| 2740 |
|
| 2741 |
self.TestLint( |
| 2742 |
"void Func(const vector <int> &const_x, vector <int> &nonconst_x) {", |
| 2743 |
operand_error_message % "vector<int> &nonconst_x", |
| 2744 |
) |
| 2745 |
|
| 2746 |
|
| 2747 |
self.TestLint("void Func(X& x);", operand_error_message % "X& x") |
| 2748 |
self.TestLint("void Func(X& x) {}", operand_error_message % "X& x") |
| 2749 |
self.TestLint("void Func(X& x) override;", "") |
| 2750 |
self.TestLint("void Func(X& x) override {", "") |
| 2751 |
self.TestLint("void Func(X& x) const override;", "") |
| 2752 |
self.TestLint("void Func(X& x) const override {", "") |
| 2753 |
|
| 2754 |
|
| 2755 |
self.TestLint("void NS::Func(X& x) {", "") |
| 2756 |
error_collector = ErrorCollector(self.assertTrue) |
| 2757 |
cpplint.ProcessFileData( |
| 2758 |
"foo.cc", |
| 2759 |
"cc", |
| 2760 |
[ |
| 2761 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 2762 |
"void a::b() {}", |
| 2763 |
"void f(int& q) {}", |
| 2764 |
"", |
| 2765 |
], |
| 2766 |
error_collector, |
| 2767 |
) |
| 2768 |
assert operand_error_message % "int& q" == error_collector.Results() |
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
error_collector = ErrorCollector(self.assertTrue) |
| 2773 |
cpplint.ProcessFileData( |
| 2774 |
"foo.cc", |
| 2775 |
"cc", |
| 2776 |
[ |
| 2777 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 2778 |
"#include <string>", |
| 2779 |
"#include <utility>", |
| 2780 |
"void swap(int &x,", |
| 2781 |
" int &y) {", |
| 2782 |
"}", |
| 2783 |
"void swap(", |
| 2784 |
" sparsegroup<T, GROUP_SIZE, Alloc> &x,", |
| 2785 |
" sparsegroup<T, GROUP_SIZE, Alloc> &y) {", |
| 2786 |
"}", |
| 2787 |
"ostream& operator<<(", |
| 2788 |
" ostream& out", |
| 2789 |
" const dense_hash_set<Value, Hash, Equals, Alloc>& seq) {", |
| 2790 |
"}", |
| 2791 |
"class A {", |
| 2792 |
" void Function(", |
| 2793 |
" string &x) override {", |
| 2794 |
" }", |
| 2795 |
"};", |
| 2796 |
"void Derived::Function(", |
| 2797 |
" string &x) {", |
| 2798 |
"}", |
| 2799 |
"#define UNSUPPORTED_MASK(_mask) \\", |
| 2800 |
" if (flags & _mask) { \\", |
| 2801 |
' LOG(FATAL) << "Unsupported flag: " << #_mask; \\', |
| 2802 |
" }", |
| 2803 |
"Constructor::Constructor()", |
| 2804 |
" : initializer1_(a1 & b1),", |
| 2805 |
" initializer2_(a2 & b2) {", |
| 2806 |
"}", |
| 2807 |
"Constructor::Constructor()", |
| 2808 |
" : initializer1_{a3 & b3},", |
| 2809 |
" initializer2_(a4 & b4) {", |
| 2810 |
"}", |
| 2811 |
"Constructor::Constructor()", |
| 2812 |
" : initializer1_{a5 & b5},", |
| 2813 |
" initializer2_(a6 & b6) {}", |
| 2814 |
"", |
| 2815 |
], |
| 2816 |
error_collector, |
| 2817 |
) |
| 2818 |
assert error_collector.Results() == "" |
| 2819 |
|
| 2820 |
|
| 2821 |
error_collector = ErrorCollector(self.assertTrue) |
| 2822 |
cpplint.ProcessFileData( |
| 2823 |
"foo.cc", |
| 2824 |
"cc", |
| 2825 |
[ |
| 2826 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 2827 |
"void Func(const Outer::", |
| 2828 |
" Inner& const_x,", |
| 2829 |
" const Outer", |
| 2830 |
" ::Inner& const_y,", |
| 2831 |
" const Outer<", |
| 2832 |
" int>::Inner& const_z,", |
| 2833 |
" Outer::", |
| 2834 |
" Inner& nonconst_x,", |
| 2835 |
" Outer", |
| 2836 |
" ::Inner& nonconst_y,", |
| 2837 |
" Outer<", |
| 2838 |
" int>::Inner& nonconst_z) {", |
| 2839 |
"}", |
| 2840 |
"", |
| 2841 |
], |
| 2842 |
error_collector, |
| 2843 |
) |
| 2844 |
assert [ |
| 2845 |
operand_error_message % "Outer::Inner& nonconst_x", |
| 2846 |
operand_error_message % "Outer::Inner& nonconst_y", |
| 2847 |
operand_error_message % "Outer<int>::Inner& nonconst_z", |
| 2848 |
] == error_collector.Results() |
| 2849 |
|
| 2850 |
|
| 2851 |
error_collector = ErrorCollector(self.assertTrue) |
| 2852 |
cpplint.ProcessFileData( |
| 2853 |
"foo.cc", |
| 2854 |
"cc", |
| 2855 |
[ |
| 2856 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 2857 |
"inline RCULocked<X>::ReadPtr::ReadPtr(const RCULocked* rcu) {", |
| 2858 |
' DCHECK(!(data & kFlagMask)) << "Error";', |
| 2859 |
"}", |
| 2860 |
"", |
| 2861 |
"RCULocked<X>::WritePtr::WritePtr(RCULocked* rcu)", |
| 2862 |
" : lock_(&rcu_->mutex_) {", |
| 2863 |
"}", |
| 2864 |
"", |
| 2865 |
], |
| 2866 |
error_collector.Results(), |
| 2867 |
) |
| 2868 |
assert error_collector.Results() == "" |
| 2869 |
|
| 2870 |
def testBraceAtBeginOfLine(self): |
| 2871 |
self.TestLint( |
| 2872 |
"{", |
| 2873 |
"{ should almost always be at the end of the previous line [whitespace/braces] [4]", |
| 2874 |
) |
| 2875 |
|
| 2876 |
error_collector = ErrorCollector(self.assertTrue) |
| 2877 |
cpplint.ProcessFileData( |
| 2878 |
"foo.cc", |
| 2879 |
"cc", |
| 2880 |
[ |
| 2881 |
"int function()", |
| 2882 |
"{", |
| 2883 |
" MutexLock l(&mu);", |
| 2884 |
"}", |
| 2885 |
"int variable;{", |
| 2886 |
" MutexLock l(&mu);", |
| 2887 |
"}", |
| 2888 |
"MyType m = {", |
| 2889 |
" {value1, value2},", |
| 2890 |
" {", |
| 2891 |
" loooong_value1, looooong_value2", |
| 2892 |
" }", |
| 2893 |
"};", |
| 2894 |
"#if PREPROCESSOR", |
| 2895 |
"{", |
| 2896 |
" MutexLock l(&mu);", |
| 2897 |
"}", |
| 2898 |
"#endif", |
| 2899 |
], |
| 2900 |
error_collector, |
| 2901 |
) |
| 2902 |
assert ( |
| 2903 |
error_collector.Results().count( |
| 2904 |
"{ should almost always be at the end of the previous line [whitespace/braces] [4]" |
| 2905 |
) |
| 2906 |
== 1 |
| 2907 |
) |
| 2908 |
|
| 2909 |
self.TestMultiLineLint( |
| 2910 |
""" |
| 2911 |
foo( |
| 2912 |
{ |
| 2913 |
loooooooooooooooong_value, |
| 2914 |
});""", |
| 2915 |
"", |
| 2916 |
) |
| 2917 |
|
| 2918 |
def testMismatchingSpacesInParens(self): |
| 2919 |
self.TestLint("if (foo ) {", "Mismatching spaces inside () in if [whitespace/parens] [5]") |
| 2920 |
self.TestLint( |
| 2921 |
"switch ( foo) {", "Mismatching spaces inside () in switch [whitespace/parens] [5]" |
| 2922 |
) |
| 2923 |
self.TestLint( |
| 2924 |
"for (foo; ba; bar ) {", "Mismatching spaces inside () in for [whitespace/parens] [5]" |
| 2925 |
) |
| 2926 |
self.TestLint("for (; foo; bar) {", "") |
| 2927 |
self.TestLint("for ( ; foo; bar) {", "") |
| 2928 |
self.TestLint("for ( ; foo; bar ) {", "") |
| 2929 |
self.TestLint("for (foo; bar; ) {", "") |
| 2930 |
self.TestLint( |
| 2931 |
"while ( foo ) {", |
| 2932 |
"Should have zero or one spaces inside ( and ) in while [whitespace/parens] [5]", |
| 2933 |
) |
| 2934 |
|
| 2935 |
def testSpacingForFncall(self): |
| 2936 |
self.TestLint("if (foo) {", "") |
| 2937 |
self.TestLint("for (foo; bar; baz) {", "") |
| 2938 |
self.TestLint("for (;;) {", "") |
| 2939 |
|
| 2940 |
self.TestLint("Something* p = new (place) Something();", "") |
| 2941 |
|
| 2942 |
self.TestLint("for (foo; baz;) {", "") |
| 2943 |
self.TestLint("for (foo;bar;baz) {", "Missing space after ; [whitespace/semicolon] [3]") |
| 2944 |
|
| 2945 |
self.TestLintNotContains( |
| 2946 |
"if (condition) { return &something; }", |
| 2947 |
"Missing space after ; [whitespace/semicolon] [3]", |
| 2948 |
) |
| 2949 |
|
| 2950 |
self.TestLint("DoSth();\\", "") |
| 2951 |
|
| 2952 |
self.TestLint( |
| 2953 |
"abc;// this is abc", |
| 2954 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 2955 |
) |
| 2956 |
self.TestLint("while (foo) {", "") |
| 2957 |
self.TestLint("switch (foo) {", "") |
| 2958 |
self.TestLint("foo( bar)", "Extra space after ( in function call [whitespace/parens] [4]") |
| 2959 |
self.TestLint("foo( // comment", "") |
| 2960 |
self.TestLint( |
| 2961 |
"foo( // comment", |
| 2962 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 2963 |
) |
| 2964 |
self.TestLint("foobar( \\", "") |
| 2965 |
self.TestLint("foobar( \\", "") |
| 2966 |
self.TestLint("( a + b)", "Extra space after ( [whitespace/parens] [2]") |
| 2967 |
self.TestLint("((a+b))", "") |
| 2968 |
self.TestLint("foo (foo)", "Extra space before ( in function call [whitespace/parens] [4]") |
| 2969 |
|
| 2970 |
self.TestLint('asm volatile ("")', "") |
| 2971 |
self.TestLint('__asm__ __volatile__ ("")', "") |
| 2972 |
self.TestLint("} catch (const Foo& ex) {", "") |
| 2973 |
self.TestLint("case (42):", "") |
| 2974 |
self.TestLint("typedef foo (*foo)(foo)", "") |
| 2975 |
self.TestLint("typedef foo (*foo12bar_)(foo)", "") |
| 2976 |
self.TestLint("typedef foo (Foo::*bar)(foo)", "") |
| 2977 |
self.TestLint("using foo = type (Foo::*bar)(foo)", "") |
| 2978 |
self.TestLint("using foo = type (Foo::*bar)(", "") |
| 2979 |
self.TestLint("using foo = type (Foo::*)(", "") |
| 2980 |
self.TestLint("foo (Foo::*bar)(", "") |
| 2981 |
self.TestLint("foo (x::y::*z)(", "") |
| 2982 |
self.TestLint( |
| 2983 |
"foo (Foo::bar)(", "Extra space before ( in function call [whitespace/parens] [4]" |
| 2984 |
) |
| 2985 |
self.TestLint("foo (*bar)(", "") |
| 2986 |
self.TestLint("typedef foo (Foo::*bar)(", "") |
| 2987 |
self.TestLint("(foo)(bar)", "") |
| 2988 |
self.TestLint("Foo (*foo)(bar)", "") |
| 2989 |
self.TestLint("Foo (*foo)(Bar bar,", "") |
| 2990 |
self.TestLint("char (*p)[sizeof(foo)] = &foo", "") |
| 2991 |
self.TestLint("char (&ref)[sizeof(foo)] = &foo", "") |
| 2992 |
self.TestLint("const char32 (*table[])[6];", "") |
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
self.TestLint("sizeof(foo)", "") |
| 2998 |
self.TestLint("sizeof foo", "") |
| 2999 |
self.TestLint("sizeof (foo)", "") |
| 3000 |
|
| 3001 |
def testReplaceAlternateTokens(self): |
| 3002 |
assert cpplint.ReplaceAlternateTokens("tor or orc") == "tor || orc" |
| 3003 |
assert cpplint.ReplaceAlternateTokens("orc or tor") == "orc || tor" |
| 3004 |
assert cpplint.ReplaceAlternateTokens("tor or (orc)") == "tor || (orc)" |
| 3005 |
assert cpplint.ReplaceAlternateTokens("tor or(orc)") == "tor ||(orc)" |
| 3006 |
assert cpplint.ReplaceAlternateTokens("sand and(android)") == "sand &&(android)" |
| 3007 |
assert cpplint.ReplaceAlternateTokens("(sand) and (android)") == "(sand) && (android)" |
| 3008 |
assert cpplint.ReplaceAlternateTokens(" not note") == " !note" |
| 3009 |
assert cpplint.ReplaceAlternateTokens(")not note") == ")!note" |
| 3010 |
assert cpplint.ReplaceAlternateTokens("(not note") == "(!note" |
| 3011 |
assert cpplint.ReplaceAlternateTokens(" not(note)") == " !(note)" |
| 3012 |
assert cpplint.ReplaceAlternateTokens(" not (splinot)") == " !(splinot)" |
| 3013 |
assert cpplint.ReplaceAlternateTokens("tor and orc or android") == "tor && orc || android" |
| 3014 |
assert ( |
| 3015 |
cpplint.ReplaceAlternateTokens("tor or orc and ands not note") |
| 3016 |
== "tor || orc && ands !note" |
| 3017 |
) |
| 3018 |
|
| 3019 |
def testSpacingAfterAlternateToken(self): |
| 3020 |
try: |
| 3021 |
cpplint._cpplint_state.AddFilters("-readability/alt_tokens") |
| 3022 |
self.TestLint("if (foo or (bar) or foobar) {", "") |
| 3023 |
self.TestLint("if (foo or (bar)) {", "") |
| 3024 |
self.TestLint("if ((foo) or (bar)) {", "") |
| 3025 |
self.TestLint("if (not foo) {", "") |
| 3026 |
self.TestLint("if (not (foo)) {", "") |
| 3027 |
self.TestLint("if (not(foo)) {", "") |
| 3028 |
self.TestLint( |
| 3029 |
"if ((foo)or(bar)) {", "Missing spaces around || [whitespace/operators] [3]" |
| 3030 |
) |
| 3031 |
finally: |
| 3032 |
cpplint._cpplint_state.SetFilters("") |
| 3033 |
|
| 3034 |
def testSpacingBeforeBraces(self): |
| 3035 |
self.TestLint("if (foo){", "Missing space before { [whitespace/braces] [5]") |
| 3036 |
self.TestLint("for{", "Missing space before { [whitespace/braces] [5]") |
| 3037 |
self.TestLint("for {", "") |
| 3038 |
self.TestLint("EXPECT_DEBUG_DEATH({", "") |
| 3039 |
self.TestLint("std::is_convertible<A, B>{}", "") |
| 3040 |
self.TestLint("blah{32}", "Missing space before { [whitespace/braces] [5]") |
| 3041 |
self.TestLint("int8_t{3}", "") |
| 3042 |
self.TestLint("int16_t{3}", "") |
| 3043 |
self.TestLint("int32_t{3}", "") |
| 3044 |
self.TestLint("uint64_t{12345}", "") |
| 3045 |
self.TestLint( |
| 3046 |
"constexpr int64_t kBatchGapMicros = int64_t{7} * 24 * 3600 * 1000000; // 1 wk.", "" |
| 3047 |
) |
| 3048 |
self.TestLint("MoveOnly(int i1, int i2) : ip1{new int{i1}}, ip2{new int{i2}} {}", "") |
| 3049 |
|
| 3050 |
def testSemiColonAfterBraces(self): |
| 3051 |
self.TestLintContains( |
| 3052 |
"if (cond) { func(); };", "You don't need a ; after a } [readability/braces] [4]" |
| 3053 |
) |
| 3054 |
self.TestLint("void Func() {};", "You don't need a ; after a } [readability/braces] [4]") |
| 3055 |
self.TestLint( |
| 3056 |
"void Func() const {};", "You don't need a ; after a } [readability/braces] [4]" |
| 3057 |
) |
| 3058 |
self.TestLint("class X {};", "") |
| 3059 |
for keyword in ["struct", "union"]: |
| 3060 |
for align in ["", " alignas(16)"]: |
| 3061 |
for typename in ["", " X"]: |
| 3062 |
for identifier in ["", " x"]: |
| 3063 |
self.TestLint(keyword + align + typename + " {}" + identifier + ";", "") |
| 3064 |
|
| 3065 |
self.TestLint("class X : public Y {};", "") |
| 3066 |
self.TestLint("class X : public MACRO() {};", "") |
| 3067 |
self.TestLint("class X : public decltype(expr) {};", "") |
| 3068 |
self.TestLint("DEFINE_FACADE(PCQueue::Watcher, PCQueue) {};", "") |
| 3069 |
self.TestLint("VCLASS(XfaTest, XfaContextTest) {};", "") |
| 3070 |
self.TestLint("class STUBBY_CLASS(H, E) {};", "") |
| 3071 |
self.TestLint("class STUBBY2_CLASS(H, E) {};", "") |
| 3072 |
self.TestLint( |
| 3073 |
"TEST(TestCase, TestName) {};", "You don't need a ; after a } [readability/braces] [4]" |
| 3074 |
) |
| 3075 |
self.TestLint( |
| 3076 |
"TEST_F(TestCase, TestName) {};", |
| 3077 |
"You don't need a ; after a } [readability/braces] [4]", |
| 3078 |
) |
| 3079 |
|
| 3080 |
self.TestLint("file_tocs_[i] = (FileToc) {a, b, c};", "") |
| 3081 |
self.TestMultiLineLint("class X : public Y,\npublic Z {};", "") |
| 3082 |
self.TestMultiLineLint( |
| 3083 |
"template<typename T>\nconcept Addable = requires(T x) { x + x; };", "" |
| 3084 |
) |
| 3085 |
self.TestMultiLineLint( |
| 3086 |
"template <typename T>\nconcept C = requires(T a, T b) {\n requires a == b;\n};", "" |
| 3087 |
) |
| 3088 |
self.TestMultiLineLint( |
| 3089 |
"template <typename T, typename U>\n" |
| 3090 |
"concept C = (std::integral<T> || std::floating_point<T>) &&\n" |
| 3091 |
" (std::integral<U> || std::floating_point<U>) &&\n" |
| 3092 |
" requires(T t, U u) {\n" |
| 3093 |
" std::min(static_cast<float>(t), static_cast<float>(u));\n" |
| 3094 |
"};", |
| 3095 |
"", |
| 3096 |
) |
| 3097 |
|
| 3098 |
def testSpacingBeforeBrackets(self): |
| 3099 |
self.TestLint( |
| 3100 |
"int numbers [] = { 1, 2, 3 };", "Extra space before [ [whitespace/braces] [5]" |
| 3101 |
) |
| 3102 |
|
| 3103 |
self.TestLint("auto [abc, def] = func();", "") |
| 3104 |
self.TestLint("#define NODISCARD [[nodiscard]]", "") |
| 3105 |
self.TestLint("void foo(int param [[maybe_unused]]);", "") |
| 3106 |
|
| 3107 |
def testLambda(self): |
| 3108 |
self.TestLint("auto x = []() {};", "") |
| 3109 |
self.TestLint("return []() {};", "") |
| 3110 |
self.TestMultiLineLint("auto x = []() {\n};\n", "") |
| 3111 |
self.TestLint( |
| 3112 |
"int operator[](int x) {};", "You don't need a ; after a } [readability/braces] [4]" |
| 3113 |
) |
| 3114 |
|
| 3115 |
self.TestMultiLineLint("auto x = [&a,\nb]() {};", "") |
| 3116 |
self.TestMultiLineLint("auto x = [&a,\nb]\n() {};", "") |
| 3117 |
self.TestMultiLineLint( |
| 3118 |
"auto x = [&a,\n" |
| 3119 |
" b](\n" |
| 3120 |
" int a,\n" |
| 3121 |
" int b) {\n" |
| 3122 |
" return a +\n" |
| 3123 |
" b;\n" |
| 3124 |
"};\n", |
| 3125 |
"", |
| 3126 |
) |
| 3127 |
|
| 3128 |
|
| 3129 |
self.TestLint("table_to_children[&*table].push_back(dependent);", "") |
| 3130 |
|
| 3131 |
def testBraceInitializerList(self): |
| 3132 |
self.TestLint("MyStruct p = {1, 2};", "") |
| 3133 |
self.TestLint("MyStruct p{1, 2};", "") |
| 3134 |
self.TestLint("vector<int> p = {1, 2};", "") |
| 3135 |
self.TestLint("vector<int> p{1, 2};", "") |
| 3136 |
self.TestLint("x = vector<int>{1, 2};", "") |
| 3137 |
self.TestLint("x = (struct in_addr){ 0 };", "") |
| 3138 |
self.TestLint("Func(vector<int>{1, 2})", "") |
| 3139 |
self.TestLint("Func((struct in_addr){ 0 })", "") |
| 3140 |
self.TestLint("Func(vector<int>{1, 2}, 3)", "") |
| 3141 |
self.TestLint("Func((struct in_addr){ 0 }, 3)", "") |
| 3142 |
self.TestLint("LOG(INFO) << char{7};", "") |
| 3143 |
self.TestLint('LOG(INFO) << char{7} << "!";', "") |
| 3144 |
self.TestLint("int p[2] = {1, 2};", "") |
| 3145 |
self.TestLint("return {1, 2};", "") |
| 3146 |
self.TestLint("std::unique_ptr<Foo> foo{new Foo{}};", "") |
| 3147 |
self.TestLint("auto foo = std::unique_ptr<Foo>{new Foo{}};", "") |
| 3148 |
self.TestLint('static_assert(Max7String{}.IsValid(), "");', "") |
| 3149 |
self.TestLint("map_of_pairs[{1, 2}] = 3;", "") |
| 3150 |
self.TestLint("ItemView{has_offer() ? new Offer{offer()} : nullptr", "") |
| 3151 |
self.TestLint("template <class T, EnableIf<::std::is_const<T>{}> = 0>", "") |
| 3152 |
|
| 3153 |
self.TestMultiLineLint("std::unique_ptr<Foo> foo{\n new Foo{}\n};\n", "") |
| 3154 |
self.TestMultiLineLint( |
| 3155 |
"std::unique_ptr<Foo> foo{\n new Foo{\n new Bar{}\n }\n};\n", "" |
| 3156 |
) |
| 3157 |
self.TestMultiLineLint( |
| 3158 |
"if (true) {\n if (false){\n func();\n }}\n", |
| 3159 |
"Missing space before { [whitespace/braces] [5]", |
| 3160 |
) |
| 3161 |
self.TestMultiLineLint( |
| 3162 |
"MyClass::MyClass()\n : initializer_{\n Func()} {\n}\n", "" |
| 3163 |
) |
| 3164 |
self.TestLint( |
| 3165 |
"const pair<string, string> kCL" + ("o" * 41) + "gStr[] = {\n", |
| 3166 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 3167 |
) |
| 3168 |
self.TestMultiLineLint( |
| 3169 |
"const pair<string, string> kCL" + ("o" * 40) + "ngStr[] =\n" |
| 3170 |
" {\n" |
| 3171 |
' {"gooooo", "oooogle"},\n' |
| 3172 |
"};\n", |
| 3173 |
"", |
| 3174 |
) |
| 3175 |
self.TestMultiLineLint( |
| 3176 |
"const pair<string, string> kCL" + ("o" * 39) + "ngStr[] =\n" |
| 3177 |
" {\n" |
| 3178 |
' {"gooooo", "oooogle"},\n' |
| 3179 |
"};\n", |
| 3180 |
"{ should almost always be at the end of the previous line [whitespace/braces] [4]", |
| 3181 |
) |
| 3182 |
|
| 3183 |
def testSpacingAroundElse(self): |
| 3184 |
self.TestLint("}else {", "Missing space before else [whitespace/braces] [5]") |
| 3185 |
self.TestLint("} else{", "Missing space before { [whitespace/braces] [5]") |
| 3186 |
self.TestLint("} else {", "") |
| 3187 |
self.TestLint("} else if (foo) {", "") |
| 3188 |
|
| 3189 |
def testSpacingWithInitializerLists(self): |
| 3190 |
self.TestLint("int v[1][3] = {{1, 2, 3}};", "") |
| 3191 |
self.TestLint("int v[1][1] = {{0}};", "") |
| 3192 |
|
| 3193 |
def testSpacingForBinaryOps(self): |
| 3194 |
self.TestLint("if (foo||bar) {", "Missing spaces around || [whitespace/operators] [3]") |
| 3195 |
self.TestLint("if (foo<=bar) {", "Missing spaces around <= [whitespace/operators] [3]") |
| 3196 |
self.TestLint("if (foo<bar) {", "Missing spaces around < [whitespace/operators] [3]") |
| 3197 |
self.TestLint("if (foo>bar) {", "Missing spaces around > [whitespace/operators] [3]") |
| 3198 |
self.TestLint("if (foo<bar->baz) {", "Missing spaces around < [whitespace/operators] [3]") |
| 3199 |
self.TestLint("if (foo<bar->bar) {", "Missing spaces around < [whitespace/operators] [3]") |
| 3200 |
self.TestLint("template<typename T = double>", "") |
| 3201 |
self.TestLint("std::unique_ptr<No<Spaces>>", "") |
| 3202 |
self.TestLint("typedef hash_map<Foo, Bar>", "") |
| 3203 |
self.TestLint("10<<20", "") |
| 3204 |
self.TestLint("10<<a", "Missing spaces around << [whitespace/operators] [3]") |
| 3205 |
self.TestLint("a<<20", "Missing spaces around << [whitespace/operators] [3]") |
| 3206 |
self.TestLint("a<<b", "Missing spaces around << [whitespace/operators] [3]") |
| 3207 |
self.TestLint("10LL<<20", "") |
| 3208 |
self.TestLint("10ULL<<20", "") |
| 3209 |
self.TestLint("a>>b", "Missing spaces around >> [whitespace/operators] [3]") |
| 3210 |
self.TestLint("10>>b", "Missing spaces around >> [whitespace/operators] [3]") |
| 3211 |
self.TestLint("LOG(ERROR)<<*foo", "Missing spaces around << [whitespace/operators] [3]") |
| 3212 |
self.TestLint("LOG(ERROR)<<&foo", "Missing spaces around << [whitespace/operators] [3]") |
| 3213 |
self.TestLint("StringCoder<vector<string>>::ToString()", "") |
| 3214 |
self.TestLint("map<pair<int, int>, map<int, int>>::iterator", "") |
| 3215 |
self.TestLint("func<int, pair<int, pair<int, int>>>()", "") |
| 3216 |
self.TestLint("MACRO1(list<list<int>>)", "") |
| 3217 |
self.TestLint("MACRO2(list<list<int>>, 42)", "") |
| 3218 |
self.TestLint("void DoFoo(const set<vector<string>>& arg1);", "") |
| 3219 |
self.TestLint("void SetFoo(set<vector<string>>* arg1);", "") |
| 3220 |
self.TestLint("foo = new set<vector<string>>;", "") |
| 3221 |
self.TestLint("reinterpret_cast<set<vector<string>>*>(a);", "") |
| 3222 |
self.TestLint("MACRO(<<)", "") |
| 3223 |
self.TestLint("MACRO(<<, arg)", "") |
| 3224 |
self.TestLint("MACRO(<<=)", "") |
| 3225 |
self.TestLint("MACRO(<<=, arg)", "") |
| 3226 |
|
| 3227 |
self.TestLint("using Vector3<T>::operator==;", "") |
| 3228 |
self.TestLint("using Vector3<T>::operator!=;", "") |
| 3229 |
|
| 3230 |
def testSpacingBeforeLastSemicolon(self): |
| 3231 |
self.TestLint( |
| 3232 |
"call_function() ;", |
| 3233 |
"Extra space before last semicolon. If this should be an " |
| 3234 |
"empty statement, use {} instead." |
| 3235 |
" [whitespace/semicolon] [5]", |
| 3236 |
) |
| 3237 |
self.TestLint( |
| 3238 |
"while (true) ;", |
| 3239 |
"Extra space before last semicolon. If this should be an " |
| 3240 |
"empty statement, use {} instead." |
| 3241 |
" [whitespace/semicolon] [5]", |
| 3242 |
) |
| 3243 |
self.TestLint( |
| 3244 |
"default:;", |
| 3245 |
"Semicolon defining empty statement. Use {} instead. [whitespace/semicolon] [5]", |
| 3246 |
) |
| 3247 |
self.TestLint( |
| 3248 |
" ;", |
| 3249 |
"Line contains only semicolon. If this should be an empty " |
| 3250 |
"statement, use {} instead." |
| 3251 |
" [whitespace/semicolon] [5]", |
| 3252 |
) |
| 3253 |
self.TestLint("for (int i = 0; ;", "") |
| 3254 |
|
| 3255 |
def testEmptyBlockBody(self): |
| 3256 |
self.TestLint( |
| 3257 |
"while (true);", |
| 3258 |
"Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]", |
| 3259 |
) |
| 3260 |
self.TestLint( |
| 3261 |
"if (true);", |
| 3262 |
"Empty conditional bodies should use {} [whitespace/empty_conditional_body] [5]", |
| 3263 |
) |
| 3264 |
self.TestLint("while (true)", "") |
| 3265 |
self.TestLint("while (true) continue;", "") |
| 3266 |
self.TestLint( |
| 3267 |
"for (;;);", |
| 3268 |
"Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]", |
| 3269 |
) |
| 3270 |
self.TestLint("for (;;)", "") |
| 3271 |
self.TestLint("for (;;) continue;", "") |
| 3272 |
self.TestLint("for (;;) func();", "") |
| 3273 |
self.TestLint( |
| 3274 |
"if (test) {}", |
| 3275 |
"If statement had no body and no else clause [whitespace/empty_if_body] [4]", |
| 3276 |
) |
| 3277 |
self.TestLint("if (test) func();", "") |
| 3278 |
self.TestLint("if (test) {} else {}", "") |
| 3279 |
self.TestMultiLineLint( |
| 3280 |
"""while (true && |
| 3281 |
false);""", |
| 3282 |
"Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]", |
| 3283 |
) |
| 3284 |
self.TestMultiLineLint( |
| 3285 |
"""do { |
| 3286 |
} while (false);""", |
| 3287 |
"", |
| 3288 |
) |
| 3289 |
self.TestMultiLineLint( |
| 3290 |
"""#define MACRO \\ |
| 3291 |
do { \\ |
| 3292 |
} while (false);""", |
| 3293 |
"", |
| 3294 |
) |
| 3295 |
self.TestMultiLineLint( |
| 3296 |
"""do { |
| 3297 |
} while (false); // next line gets a warning |
| 3298 |
while (false);""", |
| 3299 |
"Empty loop bodies should use {} or continue [whitespace/empty_loop_body] [5]", |
| 3300 |
) |
| 3301 |
self.TestMultiLineLint( |
| 3302 |
"""if (test) { |
| 3303 |
}""", |
| 3304 |
"If statement had no body and no else clause [whitespace/empty_if_body] [4]", |
| 3305 |
) |
| 3306 |
self.TestMultiLineLint( |
| 3307 |
"""if (test, |
| 3308 |
func({})) { |
| 3309 |
}""", |
| 3310 |
"If statement had no body and no else clause [whitespace/empty_if_body] [4]", |
| 3311 |
) |
| 3312 |
self.TestMultiLineLint( |
| 3313 |
"""if (test) |
| 3314 |
func();""", |
| 3315 |
"", |
| 3316 |
) |
| 3317 |
self.TestLint( |
| 3318 |
"if (test) { hello; }", |
| 3319 |
"Controlled statements inside brackets of if clause should be on a separate line" |
| 3320 |
" [whitespace/newline] [5]", |
| 3321 |
) |
| 3322 |
self.TestLint( |
| 3323 |
"if (test({})) { hello; }", |
| 3324 |
"Controlled statements inside brackets of if clause should be on a separate line" |
| 3325 |
" [whitespace/newline] [5]", |
| 3326 |
) |
| 3327 |
self.TestMultiLineLint( |
| 3328 |
"""if (test) { |
| 3329 |
func(); |
| 3330 |
}""", |
| 3331 |
"", |
| 3332 |
) |
| 3333 |
self.TestMultiLineLint( |
| 3334 |
"""if (test) { |
| 3335 |
// multiline |
| 3336 |
// comment |
| 3337 |
}""", |
| 3338 |
"", |
| 3339 |
) |
| 3340 |
self.TestMultiLineLint( |
| 3341 |
"""if (test) { // comment |
| 3342 |
}""", |
| 3343 |
"", |
| 3344 |
) |
| 3345 |
self.TestMultiLineLint( |
| 3346 |
"""if (test) { |
| 3347 |
} else { |
| 3348 |
}""", |
| 3349 |
"", |
| 3350 |
) |
| 3351 |
self.TestMultiLineLint( |
| 3352 |
"""if (func(p1, |
| 3353 |
p2, |
| 3354 |
p3)) { |
| 3355 |
func(); |
| 3356 |
}""", |
| 3357 |
"", |
| 3358 |
) |
| 3359 |
self.TestMultiLineLint( |
| 3360 |
"""if (func({}, p1)) { |
| 3361 |
func(); |
| 3362 |
}""", |
| 3363 |
"", |
| 3364 |
) |
| 3365 |
|
| 3366 |
def testSpacingForRangeBasedFor(self): |
| 3367 |
|
| 3368 |
self.TestLint("for (int i : numbers) {", "") |
| 3369 |
|
| 3370 |
|
| 3371 |
self.TestLint( |
| 3372 |
"for (int i: numbers) {", |
| 3373 |
"Missing space around colon in range-based for loop [whitespace/forcolon] [2]", |
| 3374 |
) |
| 3375 |
|
| 3376 |
self.TestLint( |
| 3377 |
"for (int i :numbers) {", |
| 3378 |
"Missing space around colon in range-based for loop [whitespace/forcolon] [2]", |
| 3379 |
) |
| 3380 |
|
| 3381 |
self.TestLint( |
| 3382 |
"for (int i:numbers) {", |
| 3383 |
"Missing space around colon in range-based for loop [whitespace/forcolon] [2]", |
| 3384 |
) |
| 3385 |
|
| 3386 |
|
| 3387 |
self.TestLint("for (std::size_t i : sizes) {}", "") |
| 3388 |
|
| 3389 |
self.TestLint( |
| 3390 |
"for (std::size_t i: sizes) {}", |
| 3391 |
"Missing space around colon in range-based for loop [whitespace/forcolon] [2]", |
| 3392 |
) |
| 3393 |
|
| 3394 |
|
| 3395 |
def testStaticOrGlobalSTLStrings(self): |
| 3396 |
|
| 3397 |
error_msg = ( |
| 3398 |
"For a static/global string constant, use a C style " |
| 3399 |
'string instead: "%s[]". [runtime/string] [4]' |
| 3400 |
) |
| 3401 |
|
| 3402 |
nonconst_error_msg = ( |
| 3403 |
"Static/global string variables are not permitted. [runtime/string] [4]" |
| 3404 |
) |
| 3405 |
|
| 3406 |
self.TestLint("string foo;", nonconst_error_msg) |
| 3407 |
self.TestLint('string kFoo = "hello"; // English', nonconst_error_msg) |
| 3408 |
self.TestLint("static string foo;", nonconst_error_msg) |
| 3409 |
self.TestLint("static const string foo;", error_msg % "static const char foo") |
| 3410 |
self.TestLint("static const std::string foo;", error_msg % "static const char foo") |
| 3411 |
self.TestLint("string Foo::bar;", nonconst_error_msg) |
| 3412 |
|
| 3413 |
self.TestLint("std::string foo;", nonconst_error_msg) |
| 3414 |
self.TestLint('std::string kFoo = "hello"; // English', nonconst_error_msg) |
| 3415 |
self.TestLint("static std::string foo;", nonconst_error_msg) |
| 3416 |
self.TestLint("static const std::string foo;", error_msg % "static const char foo") |
| 3417 |
self.TestLint("std::string Foo::bar;", nonconst_error_msg) |
| 3418 |
|
| 3419 |
self.TestLint("::std::string foo;", nonconst_error_msg) |
| 3420 |
self.TestLint('::std::string kFoo = "hello"; // English', nonconst_error_msg) |
| 3421 |
self.TestLint("static ::std::string foo;", nonconst_error_msg) |
| 3422 |
self.TestLint("static const ::std::string foo;", error_msg % "static const char foo") |
| 3423 |
self.TestLint("::std::string Foo::bar;", nonconst_error_msg) |
| 3424 |
|
| 3425 |
self.TestLint("string* pointer", "") |
| 3426 |
self.TestLint("string *pointer", "") |
| 3427 |
self.TestLint("string* pointer = Func();", "") |
| 3428 |
self.TestLint("string *pointer = Func();", "") |
| 3429 |
self.TestLint("const string* pointer", "") |
| 3430 |
self.TestLint("const string *pointer", "") |
| 3431 |
self.TestLint("const string* pointer = Func();", "") |
| 3432 |
self.TestLint("const string *pointer = Func();", "") |
| 3433 |
self.TestLint("string const* pointer", "") |
| 3434 |
self.TestLint("string const *pointer", "") |
| 3435 |
self.TestLint("string const* pointer = Func();", "") |
| 3436 |
self.TestLint("string const *pointer = Func();", "") |
| 3437 |
self.TestLint("string* const pointer", "") |
| 3438 |
self.TestLint("string *const pointer", "") |
| 3439 |
self.TestLint("string* const pointer = Func();", "") |
| 3440 |
self.TestLint("string *const pointer = Func();", "") |
| 3441 |
self.TestLint("string Foo::bar() {}", "") |
| 3442 |
self.TestLint("string Foo::operator*() {}", "") |
| 3443 |
|
| 3444 |
self.TestLint('string foo("foobar");', nonconst_error_msg) |
| 3445 |
|
| 3446 |
self.TestLint(" string foo", "") |
| 3447 |
|
| 3448 |
self.TestLint('string EmptyString() { return ""; }', "") |
| 3449 |
self.TestLint('string EmptyString () { return ""; }', "") |
| 3450 |
self.TestLint("string const& FileInfo::Pathname() const;", "") |
| 3451 |
self.TestLint("string const &FileInfo::Pathname() const;", "") |
| 3452 |
self.TestLint( |
| 3453 |
"string VeryLongNameFunctionSometimesEndsWith(\n" |
| 3454 |
" VeryLongNameType very_long_name_variable) {}", |
| 3455 |
"", |
| 3456 |
) |
| 3457 |
self.TestLint( |
| 3458 |
"template<>\n" |
| 3459 |
"string FunctionTemplateSpecialization<SomeType>(\n" |
| 3460 |
' int x) { return ""; }', |
| 3461 |
"", |
| 3462 |
) |
| 3463 |
self.TestLint( |
| 3464 |
"template<>\n" |
| 3465 |
"string FunctionTemplateSpecialization<vector<A::B>* >(\n" |
| 3466 |
' int x) { return ""; }', |
| 3467 |
"", |
| 3468 |
) |
| 3469 |
|
| 3470 |
|
| 3471 |
self.TestLint('string Class<Type>::Method() const {\n return "";\n}\n', "") |
| 3472 |
self.TestLint('string Class<Type>::Method(\n int arg) const {\n return "";\n}\n', "") |
| 3473 |
|
| 3474 |
|
| 3475 |
error_collector = ErrorCollector(self.assertTrue) |
| 3476 |
cpplint.ProcessFileData( |
| 3477 |
"foo.cc", |
| 3478 |
"cc", |
| 3479 |
[ |
| 3480 |
"// Copyright 2014 Your Company.", |
| 3481 |
"#include <string>", |
| 3482 |
"string Class", |
| 3483 |
"::MemberFunction1();", |
| 3484 |
"string Class::", |
| 3485 |
"MemberFunction2();", |
| 3486 |
"string Class::", |
| 3487 |
"NestedClass::MemberFunction3();", |
| 3488 |
"string TemplateClass<T>::", |
| 3489 |
"NestedClass::MemberFunction4();", |
| 3490 |
"const string Class", |
| 3491 |
"::static_member_variable1;", |
| 3492 |
"const string Class::", |
| 3493 |
"static_member_variable2;", |
| 3494 |
"const string Class", |
| 3495 |
'::static_member_variable3 = "initial value";', |
| 3496 |
"const string Class::", |
| 3497 |
'static_member_variable4 = "initial value";', |
| 3498 |
"string Class::", |
| 3499 |
"static_member_variable5;", |
| 3500 |
"", |
| 3501 |
], |
| 3502 |
error_collector, |
| 3503 |
) |
| 3504 |
assert error_collector.Results() == [ |
| 3505 |
error_msg % "const char Class::static_member_variable1", |
| 3506 |
error_msg % "const char Class::static_member_variable2", |
| 3507 |
error_msg % "const char Class::static_member_variable3", |
| 3508 |
error_msg % "const char Class::static_member_variable4", |
| 3509 |
nonconst_error_msg, |
| 3510 |
] |
| 3511 |
|
| 3512 |
def testNoSpacesInFunctionCalls(self): |
| 3513 |
self.TestLint("TellStory(1, 3);", "") |
| 3514 |
self.TestLint("TellStory(1, 3 );", "Extra space before ) [whitespace/parens] [2]") |
| 3515 |
self.TestLint("TellStory(1 /* wolf */, 3 /* pigs */);", "") |
| 3516 |
self.TestMultiLineLint( |
| 3517 |
"""TellStory(1, 3 |
| 3518 |
);""", |
| 3519 |
"Closing ) should be moved to the previous line [whitespace/parens] [2]", |
| 3520 |
) |
| 3521 |
self.TestMultiLineLint( |
| 3522 |
"""TellStory(Wolves(1), |
| 3523 |
Pigs(3 |
| 3524 |
));""", |
| 3525 |
"Closing ) should be moved to the previous line [whitespace/parens] [2]", |
| 3526 |
) |
| 3527 |
self.TestMultiLineLint( |
| 3528 |
"""TellStory(1, |
| 3529 |
3 );""", |
| 3530 |
"Extra space before ) [whitespace/parens] [2]", |
| 3531 |
) |
| 3532 |
|
| 3533 |
def testToDoComments(self): |
| 3534 |
start_space = "Too many spaces before TODO [whitespace/todo] [2]" |
| 3535 |
missing_username = ( |
| 3536 |
"Missing username in TODO; it should look like " |
| 3537 |
'"// TODO(my_username): Stuff."' |
| 3538 |
" [readability/todo] [2]" |
| 3539 |
) |
| 3540 |
end_space = "TODO(my_username) should be followed by a space [whitespace/todo] [2]" |
| 3541 |
|
| 3542 |
self.TestLint("// TODOfix this", [start_space, missing_username, end_space]) |
| 3543 |
self.TestLint("// TODO(ljenkins)fix this", [start_space, end_space]) |
| 3544 |
self.TestLint("// TODO fix this", [start_space, missing_username]) |
| 3545 |
self.TestLint("// TODO fix this", missing_username) |
| 3546 |
self.TestLint("// TODO: fix this", missing_username) |
| 3547 |
self.TestLint( |
| 3548 |
"//TODO(ljenkins): Fix this", |
| 3549 |
"Should have a space between // and comment [whitespace/comments] [4]", |
| 3550 |
) |
| 3551 |
self.TestLint("// TODO(ljenkins):Fix this", end_space) |
| 3552 |
self.TestLint("// TODO(ljenkins):", "") |
| 3553 |
self.TestLint("// TODO(ljenkins): fix this", "") |
| 3554 |
self.TestLint("// TODO(ljenkins): Fix this", "") |
| 3555 |
self.TestLint("#if 1 // TEST_URLTODOCID_WHICH_HAS_THAT_WORD_IN_IT_H_", "") |
| 3556 |
self.TestLint("// See also similar TODO above", "") |
| 3557 |
self.TestLint( |
| 3558 |
r'EXPECT_EQ("\\", ' |
| 3559 |
r'NormalizePath("/./../foo///bar/..//x/../..", ""));', |
| 3560 |
"", |
| 3561 |
) |
| 3562 |
|
| 3563 |
def testTwoSpacesBetweenCodeAndComments(self): |
| 3564 |
self.TestLint( |
| 3565 |
"} // namespace foo", |
| 3566 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3567 |
) |
| 3568 |
self.TestLint( |
| 3569 |
"}// namespace foo", |
| 3570 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3571 |
) |
| 3572 |
self.TestLint( |
| 3573 |
'printf("foo"); // Outside quotes.', |
| 3574 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3575 |
) |
| 3576 |
self.TestLint("int i = 0; // Having two spaces is fine.", "") |
| 3577 |
self.TestLint("int i = 0; // Having three spaces is OK.", "") |
| 3578 |
self.TestLint("// Top level comment", "") |
| 3579 |
self.TestLint(" // Line starts with two spaces.", "") |
| 3580 |
self.TestMultiLineLint("void foo() {\n { // A scope is opening.\n int a;", "") |
| 3581 |
self.TestMultiLineLint( |
| 3582 |
"void foo() {\n { // A scope is opening.\n#define A a", |
| 3583 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3584 |
) |
| 3585 |
self.TestMultiLineLint(" foo();\n { // An indented scope is opening.\n int a;", "") |
| 3586 |
self.TestMultiLineLint( |
| 3587 |
"vector<int> my_elements = {// first\n 1,", "" |
| 3588 |
) |
| 3589 |
self.TestMultiLineLint( |
| 3590 |
"vector<int> my_elements = {// my_elements is ..\n 1,", |
| 3591 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3592 |
) |
| 3593 |
self.TestLint( |
| 3594 |
"if (foo) { // not a pure scope; comment is too close!", |
| 3595 |
"At least two spaces is best between code and comments [whitespace/comments] [2]", |
| 3596 |
) |
| 3597 |
self.TestLint('printf("// In quotes.")', "") |
| 3598 |
self.TestLint('printf("\\"%s // In quotes.")', "") |
| 3599 |
self.TestLint('printf("%s", "// In quotes.")', "") |
| 3600 |
|
| 3601 |
def testSpaceAfterCommentMarker(self): |
| 3602 |
self.TestLint("//", "") |
| 3603 |
self.TestLint( |
| 3604 |
"//x", "Should have a space between // and comment [whitespace/comments] [4]" |
| 3605 |
) |
| 3606 |
self.TestLint("// x", "") |
| 3607 |
self.TestLint("///", "") |
| 3608 |
self.TestLint("/// x", "") |
| 3609 |
self.TestLint("//!", "") |
| 3610 |
self.TestLint("//----", "") |
| 3611 |
self.TestLint("//====", "") |
| 3612 |
self.TestLint("//////", "") |
| 3613 |
self.TestLint("////// x", "") |
| 3614 |
self.TestLint("///< x", "") |
| 3615 |
self.TestLint("//!< x", "") |
| 3616 |
self.TestLint( |
| 3617 |
"////x", "Should have a space between // and comment [whitespace/comments] [4]" |
| 3618 |
) |
| 3619 |
self.TestLint("//}", "") |
| 3620 |
self.TestLint( |
| 3621 |
"//}x", "Should have a space between // and comment [whitespace/comments] [4]" |
| 3622 |
) |
| 3623 |
self.TestLint( |
| 3624 |
"//!<x", "Should have a space between // and comment [whitespace/comments] [4]" |
| 3625 |
) |
| 3626 |
self.TestLint( |
| 3627 |
"///<x", "Should have a space between // and comment [whitespace/comments] [4]" |
| 3628 |
) |
| 3629 |
|
| 3630 |
|
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
def testLinePrecededByEmptyOrCommentLines(self): |
| 3636 |
def DoTest(self, lines): |
| 3637 |
error_collector = ErrorCollector(self.assertTrue) |
| 3638 |
cpplint.ProcessFileData("foo.cc", "cc", lines, error_collector) |
| 3639 |
|
| 3640 |
assert ( |
| 3641 |
error_collector.Results().count( |
| 3642 |
"Do not use namespace using-directives. Use using-declarations instead. " |
| 3643 |
"[build/namespaces] [5]" |
| 3644 |
) |
| 3645 |
== 1 |
| 3646 |
) |
| 3647 |
|
| 3648 |
DoTest(self, ["using namespace foo;"]) |
| 3649 |
DoTest(self, ["", "", "", "using namespace foo;"]) |
| 3650 |
DoTest(self, ["// hello", "using namespace foo;"]) |
| 3651 |
|
| 3652 |
def testUsingLiteralsNamespaces(self): |
| 3653 |
self.TestLint( |
| 3654 |
"using namespace std::literals;", |
| 3655 |
"Do not use namespace" |
| 3656 |
" using-directives. Use using-declarations instead." |
| 3657 |
" [build/namespaces_literals] [5]", |
| 3658 |
) |
| 3659 |
self.TestLint( |
| 3660 |
"using namespace std::literals::chrono_literals;", |
| 3661 |
"Do" |
| 3662 |
" not use namespace using-directives. Use using-declarations instead." |
| 3663 |
" [build/namespaces_literals] [5]", |
| 3664 |
) |
| 3665 |
|
| 3666 |
def testNewlineAtEOF(self): |
| 3667 |
def DoTest(self, data, is_missing_eof): |
| 3668 |
error_collector = ErrorCollector(self.assertTrue) |
| 3669 |
cpplint.ProcessFileData("foo.cc", "cc", data.split("\n"), error_collector) |
| 3670 |
|
| 3671 |
assert int(is_missing_eof) == error_collector.Results().count( |
| 3672 |
"Could not find a newline character at the end of the file. " |
| 3673 |
"[whitespace/ending_newline] [5]" |
| 3674 |
) |
| 3675 |
|
| 3676 |
DoTest(self, "// Newline\n// at EOF\n", False) |
| 3677 |
DoTest(self, "// No newline\n// at EOF", True) |
| 3678 |
|
| 3679 |
def testInvalidUtf8(self): |
| 3680 |
def DoTest(self, raw_bytes, has_invalid_utf8): |
| 3681 |
error_collector = ErrorCollector(self.assertTrue) |
| 3682 |
unidata = str(raw_bytes, "utf8", "replace").split("\n") |
| 3683 |
cpplint.ProcessFileData("foo.cc", "cc", unidata, error_collector) |
| 3684 |
|
| 3685 |
assert int(has_invalid_utf8) == error_collector.Results().count( |
| 3686 |
"Line contains invalid UTF-8 (or Unicode replacement character). " |
| 3687 |
"[readability/utf8] [5]" |
| 3688 |
) |
| 3689 |
|
| 3690 |
DoTest(self, codecs_latin_encode("Hello world\n"), False) |
| 3691 |
DoTest(self, codecs_latin_encode("\xe9\x8e\xbd\n"), False) |
| 3692 |
DoTest(self, codecs_latin_encode("\xe9x\x8e\xbd\n"), True) |
| 3693 |
|
| 3694 |
|
| 3695 |
DoTest(self, codecs_latin_encode("\xef\xbf\xbd\n"), True) |
| 3696 |
|
| 3697 |
def testBadCharacters(self): |
| 3698 |
|
| 3699 |
error_collector = ErrorCollector(self.assertTrue) |
| 3700 |
cpplint.ProcessFileData( |
| 3701 |
"nul_input.cc", "cc", ["// Copyright 2014 Your Company.", "\0", ""], error_collector |
| 3702 |
) |
| 3703 |
assert error_collector.Results() == "Line contains NUL byte. [readability/nul] [5]" |
| 3704 |
|
| 3705 |
|
| 3706 |
|
| 3707 |
error_collector = ErrorCollector(self.assertTrue) |
| 3708 |
raw_bytes = codecs_latin_encode("\xe9x\0") |
| 3709 |
unidata = str(raw_bytes, "utf8", "replace") |
| 3710 |
cpplint.ProcessFileData( |
| 3711 |
"nul_utf8.cc", "cc", ["// Copyright 2014 Your Company.", unidata, ""], error_collector |
| 3712 |
) |
| 3713 |
assert error_collector.Results() == [ |
| 3714 |
"Line contains invalid UTF-8 (or Unicode replacement character). " |
| 3715 |
"[readability/utf8] [5]", |
| 3716 |
"Line contains NUL byte. [readability/nul] [5]", |
| 3717 |
] |
| 3718 |
|
| 3719 |
def testIsBlankLine(self): |
| 3720 |
assert cpplint.IsBlankLine("") |
| 3721 |
assert cpplint.IsBlankLine(" ") |
| 3722 |
assert cpplint.IsBlankLine(" \t\r\n") |
| 3723 |
assert not cpplint.IsBlankLine("int a;") |
| 3724 |
assert not cpplint.IsBlankLine("{") |
| 3725 |
|
| 3726 |
def testBlankLinesCheck(self): |
| 3727 |
self.TestBlankLinesCheck(["{\n", "\n", "\n", "}\n"], 1, 1) |
| 3728 |
self.TestBlankLinesCheck([" if (foo) {\n", "\n", " }\n"], 1, 1) |
| 3729 |
self.TestBlankLinesCheck(["\n", "// {\n", "\n", "\n", "// Comment\n", "{\n", "}\n"], 0, 0) |
| 3730 |
self.TestBlankLinesCheck(["\n", 'run("{");\n', "\n"], 0, 0) |
| 3731 |
self.TestBlankLinesCheck(["\n", " if (foo) { return 0; }\n", "\n"], 0, 0) |
| 3732 |
self.TestBlankLinesCheck(["int x(\n", " int a) {\n", "\n", "return 0;\n", "}"], 0, 0) |
| 3733 |
self.TestBlankLinesCheck( |
| 3734 |
["int x(\n", " int a) const {\n", "\n", "return 0;\n", "}"], 0, 0 |
| 3735 |
) |
| 3736 |
self.TestBlankLinesCheck(["int x(\n", " int a) {\n", "\n", "return 0;\n", "}"], 1, 0) |
| 3737 |
self.TestBlankLinesCheck(["int x(\n", " int a) {\n", "\n", "return 0;\n", "}"], 1, 0) |
| 3738 |
|
| 3739 |
def testAllowBlankLineBeforeClosingNamespace(self): |
| 3740 |
error_collector = ErrorCollector(self.assertTrue) |
| 3741 |
cpplint.ProcessFileData( |
| 3742 |
"foo.cc", |
| 3743 |
"cc", |
| 3744 |
[ |
| 3745 |
"namespace {", |
| 3746 |
"", |
| 3747 |
"} // namespace", |
| 3748 |
"namespace another_namespace {", |
| 3749 |
"", |
| 3750 |
"}", |
| 3751 |
"namespace {", |
| 3752 |
"", |
| 3753 |
"template<class T, ", |
| 3754 |
" class A = hoge<T>, ", |
| 3755 |
" class B = piyo<T>, ", |
| 3756 |
" class C = fuga<T> >", |
| 3757 |
"class D {", |
| 3758 |
" public:", |
| 3759 |
"};", |
| 3760 |
"", |
| 3761 |
"", |
| 3762 |
"", |
| 3763 |
"", |
| 3764 |
"}", |
| 3765 |
], |
| 3766 |
error_collector, |
| 3767 |
) |
| 3768 |
assert ( |
| 3769 |
error_collector.Results().count( |
| 3770 |
"Redundant blank line at the end of a code block should be deleted. " |
| 3771 |
"[whitespace/blank_line] [3]" |
| 3772 |
) |
| 3773 |
== 0 |
| 3774 |
) |
| 3775 |
|
| 3776 |
def testAllowBlankLineBeforeIfElseChain(self): |
| 3777 |
error_collector = ErrorCollector(self.assertTrue) |
| 3778 |
cpplint.ProcessFileData( |
| 3779 |
"foo.cc", |
| 3780 |
"cc", |
| 3781 |
[ |
| 3782 |
"if (hoge) {", |
| 3783 |
"", |
| 3784 |
"} else if (piyo) {", |
| 3785 |
"", |
| 3786 |
"} else if (piyopiyo) {", |
| 3787 |
" hoge = true;", |
| 3788 |
"} else {", |
| 3789 |
"", |
| 3790 |
"}", |
| 3791 |
], |
| 3792 |
error_collector, |
| 3793 |
) |
| 3794 |
assert ( |
| 3795 |
error_collector.Results().count( |
| 3796 |
"Redundant blank line at the end of a code block should be deleted. " |
| 3797 |
"[whitespace/blank_line] [3]" |
| 3798 |
) |
| 3799 |
== 1 |
| 3800 |
) |
| 3801 |
|
| 3802 |
def testAllowBlankLineAfterExtern(self): |
| 3803 |
error_collector = ErrorCollector(self.assertTrue) |
| 3804 |
cpplint.ProcessFileData( |
| 3805 |
"foo.cc", |
| 3806 |
"cc", |
| 3807 |
['extern "C" {', "", "EXPORTAPI void APICALL Some_function() {}", "", "}"], |
| 3808 |
error_collector, |
| 3809 |
) |
| 3810 |
assert ( |
| 3811 |
error_collector.Results().count( |
| 3812 |
"Redundant blank line at the start of a code block should be deleted. " |
| 3813 |
"[whitespace/blank_line] [2]" |
| 3814 |
) |
| 3815 |
== 0 |
| 3816 |
) |
| 3817 |
assert ( |
| 3818 |
error_collector.Results().count( |
| 3819 |
"Redundant blank line at the end of a code block should be deleted. " |
| 3820 |
"[whitespace/blank_line] [3]" |
| 3821 |
) |
| 3822 |
== 0 |
| 3823 |
) |
| 3824 |
|
| 3825 |
def testBlankLineBeforeSectionKeyword(self): |
| 3826 |
error_collector = ErrorCollector(self.assertTrue) |
| 3827 |
cpplint.ProcessFileData( |
| 3828 |
"foo.cc", |
| 3829 |
"cc", |
| 3830 |
[ |
| 3831 |
"class A {", |
| 3832 |
" public:", |
| 3833 |
" protected:", |
| 3834 |
" private:", |
| 3835 |
" struct B {", |
| 3836 |
" public:", |
| 3837 |
" private:", |
| 3838 |
] |
| 3839 |
+ ([""] * 100) |
| 3840 |
+ [ |
| 3841 |
" };", |
| 3842 |
" struct C {", |
| 3843 |
" protected:", |
| 3844 |
" private:", |
| 3845 |
" };", |
| 3846 |
"};", |
| 3847 |
"class D", |
| 3848 |
" : public {", |
| 3849 |
" public:", |
| 3850 |
"};", |
| 3851 |
"class E {\\", |
| 3852 |
" public:\\", |
| 3853 |
] |
| 3854 |
+ (["\\"] * 100) |
| 3855 |
+ [ |
| 3856 |
" int non_empty_line;\\", |
| 3857 |
" private:\\", |
| 3858 |
" int a;\\", |
| 3859 |
"};", |
| 3860 |
], |
| 3861 |
error_collector, |
| 3862 |
) |
| 3863 |
assert ( |
| 3864 |
error_collector.Results().count( |
| 3865 |
'"private:" should be preceded by a blank line [whitespace/blank_line] [3]' |
| 3866 |
) |
| 3867 |
== 2 |
| 3868 |
) |
| 3869 |
assert ( |
| 3870 |
error_collector.Results().count( |
| 3871 |
'"protected:" should be preceded by a blank line [whitespace/blank_line] [3]' |
| 3872 |
) |
| 3873 |
== 1 |
| 3874 |
) |
| 3875 |
|
| 3876 |
def testNoBlankLineAfterSectionKeyword(self): |
| 3877 |
error_collector = ErrorCollector(self.assertTrue) |
| 3878 |
cpplint.ProcessFileData( |
| 3879 |
"foo.cc", |
| 3880 |
"cc", |
| 3881 |
[ |
| 3882 |
"class A {", |
| 3883 |
" public:", |
| 3884 |
"", |
| 3885 |
" private:", |
| 3886 |
"", |
| 3887 |
" struct B {", |
| 3888 |
" protected:", |
| 3889 |
"", |
| 3890 |
" };", |
| 3891 |
"};", |
| 3892 |
], |
| 3893 |
error_collector, |
| 3894 |
) |
| 3895 |
assert ( |
| 3896 |
error_collector.Results().count( |
| 3897 |
'Do not leave a blank line after "public:" [whitespace/blank_line] [3]' |
| 3898 |
) |
| 3899 |
== 1 |
| 3900 |
) |
| 3901 |
assert ( |
| 3902 |
error_collector.Results().count( |
| 3903 |
'Do not leave a blank line after "protected:" [whitespace/blank_line] [3]' |
| 3904 |
) |
| 3905 |
== 1 |
| 3906 |
) |
| 3907 |
assert ( |
| 3908 |
error_collector.Results().count( |
| 3909 |
'Do not leave a blank line after "private:" [whitespace/blank_line] [3]' |
| 3910 |
) |
| 3911 |
== 1 |
| 3912 |
) |
| 3913 |
|
| 3914 |
def testAllowBlankLinesInRawStrings(self): |
| 3915 |
error_collector = ErrorCollector(self.assertTrue) |
| 3916 |
cpplint.ProcessFileData( |
| 3917 |
"foo.cc", |
| 3918 |
"cc", |
| 3919 |
[ |
| 3920 |
"// Copyright 2014 Your Company.", |
| 3921 |
'static const char *kData[] = {R"(', |
| 3922 |
"", |
| 3923 |
')", R"(', |
| 3924 |
"", |
| 3925 |
')"};', |
| 3926 |
"", |
| 3927 |
], |
| 3928 |
error_collector, |
| 3929 |
) |
| 3930 |
assert error_collector.Results() == "" |
| 3931 |
|
| 3932 |
def testElseOnSameLineAsClosingBraces(self): |
| 3933 |
error_collector = ErrorCollector(self.assertTrue) |
| 3934 |
cpplint.ProcessFileData( |
| 3935 |
"foo.cc", |
| 3936 |
"cc", |
| 3937 |
[ |
| 3938 |
"if (hoge) {", |
| 3939 |
"}", |
| 3940 |
"else if (piyo) {", |
| 3941 |
"}", |
| 3942 |
" else {" |
| 3943 |
"", |
| 3944 |
"}", |
| 3945 |
], |
| 3946 |
error_collector, |
| 3947 |
) |
| 3948 |
assert ( |
| 3949 |
error_collector.Results().count( |
| 3950 |
"An else should appear on the same line as the preceding } " |
| 3951 |
"[whitespace/newline] [4]" |
| 3952 |
) |
| 3953 |
== 2 |
| 3954 |
) |
| 3955 |
|
| 3956 |
error_collector = ErrorCollector(self.assertTrue) |
| 3957 |
cpplint.ProcessFileData( |
| 3958 |
"foo.cc", |
| 3959 |
"cc", |
| 3960 |
[ |
| 3961 |
"if (hoge) {", |
| 3962 |
"", |
| 3963 |
"}", |
| 3964 |
"else", |
| 3965 |
"{", |
| 3966 |
"", |
| 3967 |
"}", |
| 3968 |
], |
| 3969 |
error_collector, |
| 3970 |
) |
| 3971 |
assert ( |
| 3972 |
error_collector.Results().count( |
| 3973 |
"An else should appear on the same line as the preceding } " |
| 3974 |
"[whitespace/newline] [4]" |
| 3975 |
) |
| 3976 |
== 1 |
| 3977 |
) |
| 3978 |
|
| 3979 |
error_collector = ErrorCollector(self.assertTrue) |
| 3980 |
cpplint.ProcessFileData( |
| 3981 |
"foo.cc", "cc", ["if (hoge) {", "", "}", "else_function();"], error_collector |
| 3982 |
) |
| 3983 |
assert ( |
| 3984 |
error_collector.Results().count( |
| 3985 |
"An else should appear on the same line as the preceding } " |
| 3986 |
"[whitespace/newline] [4]" |
| 3987 |
) |
| 3988 |
== 0 |
| 3989 |
) |
| 3990 |
|
| 3991 |
def testMultipleStatementsOnSameLine(self): |
| 3992 |
error_collector = ErrorCollector(self.assertTrue) |
| 3993 |
cpplint.ProcessFileData( |
| 3994 |
"foo.cc", |
| 3995 |
"cc", |
| 3996 |
[ |
| 3997 |
"for (int i = 0; i < 1; i++) {}", |
| 3998 |
"switch (x) {", |
| 3999 |
" case 0: func(); break; ", |
| 4000 |
"}", |
| 4001 |
"sum += MathUtil::SafeIntRound(x); x += 0.1;", |
| 4002 |
], |
| 4003 |
error_collector, |
| 4004 |
) |
| 4005 |
assert ( |
| 4006 |
error_collector.Results().count( |
| 4007 |
"More than one command on the same line [whitespace/newline] [0]" |
| 4008 |
) |
| 4009 |
== 0 |
| 4010 |
) |
| 4011 |
|
| 4012 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4013 |
cpplint._cpplint_state.verbose_level = 0 |
| 4014 |
cpplint.ProcessFileData( |
| 4015 |
"foo.cc", "cc", ["sum += MathUtil::SafeIntRound(x); x += 0.1;"], error_collector |
| 4016 |
) |
| 4017 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4018 |
|
| 4019 |
def testLambdasOnSameLine(self): |
| 4020 |
error_collector = ErrorCollector(self.assertTrue) |
| 4021 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4022 |
cpplint._cpplint_state.verbose_level = 0 |
| 4023 |
cpplint.ProcessFileData( |
| 4024 |
"foo.cc", "cc", ["const auto lambda = [](const int i) { return i; };"], error_collector |
| 4025 |
) |
| 4026 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4027 |
assert ( |
| 4028 |
error_collector.Results().count( |
| 4029 |
"More than one command on the same line [whitespace/newline] [0]" |
| 4030 |
) |
| 4031 |
== 0 |
| 4032 |
) |
| 4033 |
|
| 4034 |
error_collector = ErrorCollector(self.assertTrue) |
| 4035 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4036 |
cpplint._cpplint_state.verbose_level = 0 |
| 4037 |
cpplint.ProcessFileData( |
| 4038 |
"foo.cc", |
| 4039 |
"cc", |
| 4040 |
[ |
| 4041 |
"const auto result = std::any_of(vector.begin(), " |
| 4042 |
"vector.end(), " |
| 4043 |
"[](const int i) { return i > 0; });" |
| 4044 |
], |
| 4045 |
error_collector, |
| 4046 |
) |
| 4047 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4048 |
assert ( |
| 4049 |
error_collector.Results().count( |
| 4050 |
"More than one command on the same line [whitespace/newline] [0]" |
| 4051 |
) |
| 4052 |
== 0 |
| 4053 |
) |
| 4054 |
|
| 4055 |
error_collector = ErrorCollector(self.assertTrue) |
| 4056 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4057 |
cpplint._cpplint_state.verbose_level = 0 |
| 4058 |
cpplint.ProcessFileData( |
| 4059 |
"foo.cc", |
| 4060 |
"cc", |
| 4061 |
[ |
| 4062 |
"return mutex::Lock<void>([this]() { " |
| 4063 |
"this->ReadLock(); }, [this]() { " |
| 4064 |
"this->ReadUnlock(); });" |
| 4065 |
], |
| 4066 |
error_collector, |
| 4067 |
) |
| 4068 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4069 |
assert ( |
| 4070 |
error_collector.Results().count( |
| 4071 |
"More than one command on the same line [whitespace/newline] [0]" |
| 4072 |
) |
| 4073 |
== 0 |
| 4074 |
) |
| 4075 |
|
| 4076 |
error_collector = ErrorCollector(self.assertTrue) |
| 4077 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4078 |
cpplint._cpplint_state.verbose_level = 0 |
| 4079 |
cpplint.ProcessFileData( |
| 4080 |
"foo.cc", |
| 4081 |
"cc", |
| 4082 |
[ |
| 4083 |
"return mutex::Lock<void>([this]() { " |
| 4084 |
"this->ReadLock(); }, [this]() { " |
| 4085 |
"this->ReadUnlock(); }, object);" |
| 4086 |
], |
| 4087 |
error_collector, |
| 4088 |
) |
| 4089 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4090 |
assert ( |
| 4091 |
error_collector.Results().count( |
| 4092 |
"More than one command on the same line [whitespace/newline] [0]" |
| 4093 |
) |
| 4094 |
== 0 |
| 4095 |
) |
| 4096 |
|
| 4097 |
def testEndOfNamespaceComments(self): |
| 4098 |
error_collector = ErrorCollector(self.assertTrue) |
| 4099 |
cpplint.ProcessFileData( |
| 4100 |
"foo.cc", |
| 4101 |
"cc", |
| 4102 |
[ |
| 4103 |
"namespace {", |
| 4104 |
"", |
| 4105 |
"}", |
| 4106 |
"namespace expected {", |
| 4107 |
"} // namespace mismatched", |
| 4108 |
"namespace {", |
| 4109 |
"} // namespace mismatched", |
| 4110 |
"namespace outer { namespace nested {", |
| 4111 |
] |
| 4112 |
+ ([""] * 10) |
| 4113 |
+ [ |
| 4114 |
"}", |
| 4115 |
"}", |
| 4116 |
"namespace {", |
| 4117 |
] |
| 4118 |
+ ([""] * 10) |
| 4119 |
+ [ |
| 4120 |
"}", |
| 4121 |
"namespace {", |
| 4122 |
] |
| 4123 |
+ ([""] * 10) |
| 4124 |
+ [ |
| 4125 |
"} // namespace some description", |
| 4126 |
"namespace {", |
| 4127 |
] |
| 4128 |
+ ([""] * 10) |
| 4129 |
+ [ |
| 4130 |
"} // namespace anonymous", |
| 4131 |
"namespace {", |
| 4132 |
] |
| 4133 |
+ ([""] * 10) |
| 4134 |
+ [ |
| 4135 |
"} // anonymous namespace (utils)", |
| 4136 |
"namespace {", |
| 4137 |
] |
| 4138 |
+ ([""] * 10) |
| 4139 |
+ [ |
| 4140 |
"} // anonymous namespace", |
| 4141 |
"namespace missing_comment {", |
| 4142 |
] |
| 4143 |
+ ([""] * 10) |
| 4144 |
+ [ |
| 4145 |
"}", |
| 4146 |
"namespace no_warning {", |
| 4147 |
] |
| 4148 |
+ ([""] * 10) |
| 4149 |
+ ["} // namespace no_warning", "namespace no_warning {"] |
| 4150 |
+ ([""] * 10) |
| 4151 |
+ ["}; // end namespace no_warning", "#define MACRO \\", "namespace c_style { \\"] |
| 4152 |
+ (["\\"] * 10) |
| 4153 |
+ ["} /* namespace c_style. */ \\", ";"], |
| 4154 |
error_collector, |
| 4155 |
) |
| 4156 |
assert ( |
| 4157 |
error_collector.Results().count( |
| 4158 |
'Namespace should be terminated with "// namespace expected"' |
| 4159 |
" [readability/namespace] [5]" |
| 4160 |
) |
| 4161 |
== 1 |
| 4162 |
) |
| 4163 |
assert ( |
| 4164 |
error_collector.Results().count( |
| 4165 |
'Namespace should be terminated with "// namespace outer"' |
| 4166 |
" [readability/namespace] [5]" |
| 4167 |
) |
| 4168 |
== 1 |
| 4169 |
) |
| 4170 |
assert ( |
| 4171 |
error_collector.Results().count( |
| 4172 |
'Namespace should be terminated with "// namespace nested"' |
| 4173 |
" [readability/namespace] [5]" |
| 4174 |
) |
| 4175 |
== 1 |
| 4176 |
) |
| 4177 |
assert ( |
| 4178 |
error_collector.Results().count( |
| 4179 |
'Anonymous namespace should be terminated with "// namespace"' |
| 4180 |
" [readability/namespace] [5]" |
| 4181 |
) |
| 4182 |
== 3 |
| 4183 |
) |
| 4184 |
assert ( |
| 4185 |
error_collector.Results().count( |
| 4186 |
'Anonymous namespace should be terminated with "// namespace" or ' |
| 4187 |
'"// anonymous namespace" [readability/namespace] [5]"' |
| 4188 |
) |
| 4189 |
== 0 |
| 4190 |
) |
| 4191 |
assert ( |
| 4192 |
error_collector.Results().count( |
| 4193 |
'Namespace should be terminated with "// namespace missing_comment"' |
| 4194 |
" [readability/namespace] [5]" |
| 4195 |
) |
| 4196 |
== 1 |
| 4197 |
) |
| 4198 |
assert ( |
| 4199 |
error_collector.Results().count( |
| 4200 |
'Namespace should be terminated with "// namespace no_warning"' |
| 4201 |
" [readability/namespace] [5]" |
| 4202 |
) |
| 4203 |
== 0 |
| 4204 |
) |
| 4205 |
|
| 4206 |
def testComma(self): |
| 4207 |
self.TestLint("a = f(1,2);", "Missing space after , [whitespace/comma] [3]") |
| 4208 |
self.TestLint( |
| 4209 |
"int tmp=a,a=b,b=tmp;", |
| 4210 |
[ |
| 4211 |
"Missing spaces around = [whitespace/operators] [4]", |
| 4212 |
"Missing space after , [whitespace/comma] [3]", |
| 4213 |
], |
| 4214 |
) |
| 4215 |
self.TestLint("f(a, /* name */ b);", "") |
| 4216 |
self.TestLint("f(a, /* name */b);", "") |
| 4217 |
self.TestLint("f(a, /* name */-1);", "") |
| 4218 |
self.TestLint('f(a, /* name */"1");', "") |
| 4219 |
self.TestLint("f(1, /* empty macro arg */, 2)", "") |
| 4220 |
self.TestLint("f(1,, 2)", "") |
| 4221 |
self.TestLint("operator,()", "") |
| 4222 |
self.TestLint("operator,(a,b)", "Missing space after , [whitespace/comma] [3]") |
| 4223 |
self.TestLint("__VA_OPT__(,)", "") |
| 4224 |
self.TestLint( |
| 4225 |
"__VA_OPT__ (,)", "Extra space before ( in function call [whitespace/parens] [4]" |
| 4226 |
) |
| 4227 |
|
| 4228 |
def testEqualsOperatorSpacing(self): |
| 4229 |
self.TestLint("int tmp= a;", "Missing spaces around = [whitespace/operators] [4]") |
| 4230 |
self.TestLint("int tmp =a;", "Missing spaces around = [whitespace/operators] [4]") |
| 4231 |
self.TestLint("int tmp=a;", "Missing spaces around = [whitespace/operators] [4]") |
| 4232 |
self.TestLint("int tmp= 7;", "Missing spaces around = [whitespace/operators] [4]") |
| 4233 |
self.TestLint("int tmp =7;", "Missing spaces around = [whitespace/operators] [4]") |
| 4234 |
self.TestLint("int tmp=7;", "Missing spaces around = [whitespace/operators] [4]") |
| 4235 |
self.TestLint("int* tmp=*p;", "Missing spaces around = [whitespace/operators] [4]") |
| 4236 |
self.TestLint("int* tmp= *p;", "Missing spaces around = [whitespace/operators] [4]") |
| 4237 |
self.TestMultiLineLint( |
| 4238 |
TrimExtraIndent(""" |
| 4239 |
lookahead_services_= |
| 4240 |
::strings::Split(FLAGS_ls, ",", ::strings::SkipEmpty());"""), |
| 4241 |
"Missing spaces around = [whitespace/operators] [4]", |
| 4242 |
) |
| 4243 |
self.TestLint( |
| 4244 |
"bool result = a>=42;", "Missing spaces around >= [whitespace/operators] [3]" |
| 4245 |
) |
| 4246 |
self.TestLint( |
| 4247 |
"bool result = a<=42;", "Missing spaces around <= [whitespace/operators] [3]" |
| 4248 |
) |
| 4249 |
self.TestLint( |
| 4250 |
"bool result = a==42;", "Missing spaces around == [whitespace/operators] [3]" |
| 4251 |
) |
| 4252 |
self.TestLint( |
| 4253 |
"auto result = a!=42;", "Missing spaces around != [whitespace/operators] [3]" |
| 4254 |
) |
| 4255 |
self.TestLint("int a = b!=c;", "Missing spaces around != [whitespace/operators] [3]") |
| 4256 |
self.TestLint("a&=42;", "") |
| 4257 |
self.TestLint("a|=42;", "") |
| 4258 |
self.TestLint("a^=42;", "") |
| 4259 |
self.TestLint("a+=42;", "") |
| 4260 |
self.TestLint("a*=42;", "") |
| 4261 |
self.TestLint("a/=42;", "") |
| 4262 |
self.TestLint("a%=42;", "") |
| 4263 |
self.TestLint("a>>=5;", "") |
| 4264 |
self.TestLint("a<<=5;", "") |
| 4265 |
|
| 4266 |
def testShiftOperatorSpacing(self): |
| 4267 |
self.TestLint("a<<b", "Missing spaces around << [whitespace/operators] [3]") |
| 4268 |
self.TestLint("a>>b", "Missing spaces around >> [whitespace/operators] [3]") |
| 4269 |
self.TestLint("1<<20", "") |
| 4270 |
self.TestLint("1024>>10", "") |
| 4271 |
self.TestLint("Kernel<<<1, 2>>>()", "") |
| 4272 |
|
| 4273 |
def testIndent(self): |
| 4274 |
self.TestLint("static int noindent;", "") |
| 4275 |
self.TestLint(" int two_space_indent;", "") |
| 4276 |
self.TestLint(" int four_space_indent;", "") |
| 4277 |
self.TestLint( |
| 4278 |
" int one_space_indent;", |
| 4279 |
"Weird number of spaces at line-start. " |
| 4280 |
"Are you using a 2-space indent? [whitespace/indent] [3]", |
| 4281 |
) |
| 4282 |
self.TestLint( |
| 4283 |
" int three_space_indent;", |
| 4284 |
"Weird number of spaces at line-start. " |
| 4285 |
"Are you using a 2-space indent? [whitespace/indent] [3]", |
| 4286 |
) |
| 4287 |
self.TestLint( |
| 4288 |
' char* one_space_indent = "public:";', |
| 4289 |
"Weird number of spaces at line-start. " |
| 4290 |
"Are you using a 2-space indent? [whitespace/indent] [3]", |
| 4291 |
) |
| 4292 |
self.TestLint(" public:", "") |
| 4293 |
self.TestLint(" protected:", "") |
| 4294 |
self.TestLint(" private:", "") |
| 4295 |
self.TestLint(" protected: \\", "") |
| 4296 |
self.TestLint(" public: \\", "") |
| 4297 |
self.TestLint(" private: \\", "") |
| 4298 |
|
| 4299 |
self.TestMultiLineLint( |
| 4300 |
TrimExtraIndent(""" |
| 4301 |
class foo { |
| 4302 |
public slots: |
| 4303 |
void bar(); |
| 4304 |
signals: |
| 4305 |
};"""), |
| 4306 |
"", |
| 4307 |
) |
| 4308 |
self.TestMultiLineLint( |
| 4309 |
TrimExtraIndent(""" |
| 4310 |
class foo { |
| 4311 |
public slots: |
| 4312 |
void bar(); |
| 4313 |
};"""), |
| 4314 |
"public slots: should be indented +1 space inside class foo [whitespace/indent] [3]", |
| 4315 |
) |
| 4316 |
self.TestMultiLineLint( |
| 4317 |
TrimExtraIndent(""" |
| 4318 |
class foo { |
| 4319 |
signals: |
| 4320 |
void bar(); |
| 4321 |
};"""), |
| 4322 |
"signals: should be indented +1 space inside class foo [whitespace/indent] [3]", |
| 4323 |
) |
| 4324 |
self.TestMultiLineLint( |
| 4325 |
TrimExtraIndent(""" |
| 4326 |
static const char kRawString[] = R"(" |
| 4327 |
")";"""), |
| 4328 |
"", |
| 4329 |
) |
| 4330 |
self.TestMultiLineLint( |
| 4331 |
TrimExtraIndent(""" |
| 4332 |
KV<Query, |
| 4333 |
Tuple<TaxonomyId, PetacatCategoryId, double>>"""), |
| 4334 |
"", |
| 4335 |
) |
| 4336 |
self.TestMultiLineLint( |
| 4337 |
' static const char kSingleLineRawString[] = R"(...)";', |
| 4338 |
"Weird number of spaces at line-start. " |
| 4339 |
"Are you using a 2-space indent? [whitespace/indent] [3]", |
| 4340 |
) |
| 4341 |
|
| 4342 |
def testSectionIndent(self): |
| 4343 |
self.TestMultiLineLint( |
| 4344 |
""" |
| 4345 |
class A { |
| 4346 |
public: // no warning |
| 4347 |
private: // warning here |
| 4348 |
};""", |
| 4349 |
"private: should be indented +1 space inside class A [whitespace/indent] [3]", |
| 4350 |
) |
| 4351 |
self.TestMultiLineLint( |
| 4352 |
""" |
| 4353 |
class B { |
| 4354 |
public: // no warning |
| 4355 |
template<> struct C { |
| 4356 |
public: // warning here |
| 4357 |
protected: // no warning |
| 4358 |
}; |
| 4359 |
};""", |
| 4360 |
"public: should be indented +1 space inside struct C [whitespace/indent] [3]", |
| 4361 |
) |
| 4362 |
self.TestMultiLineLint( |
| 4363 |
""" |
| 4364 |
struct D { |
| 4365 |
};""", |
| 4366 |
"Closing brace should be aligned with beginning of struct D [whitespace/indent] [3]", |
| 4367 |
) |
| 4368 |
self.TestMultiLineLint( |
| 4369 |
""" |
| 4370 |
template<typename E> class F { |
| 4371 |
};""", |
| 4372 |
"Closing brace should be aligned with beginning of class F [whitespace/indent] [3]", |
| 4373 |
) |
| 4374 |
self.TestMultiLineLint( |
| 4375 |
""" |
| 4376 |
class G { |
| 4377 |
Q_OBJECT |
| 4378 |
public slots: |
| 4379 |
signals: |
| 4380 |
};""", |
| 4381 |
[ |
| 4382 |
"public slots: should be indented +1 space inside class G [whitespace/indent] [3]", |
| 4383 |
"signals: should be indented +1 space inside class G [whitespace/indent] [3]", |
| 4384 |
], |
| 4385 |
) |
| 4386 |
self.TestMultiLineLint( |
| 4387 |
""" |
| 4388 |
class H { |
| 4389 |
/* comments */ class I { |
| 4390 |
public: // no warning |
| 4391 |
private: // warning here |
| 4392 |
}; |
| 4393 |
};""", |
| 4394 |
"private: should be indented +1 space inside class I [whitespace/indent] [3]", |
| 4395 |
) |
| 4396 |
self.TestMultiLineLint( |
| 4397 |
""" |
| 4398 |
class J |
| 4399 |
: public ::K { |
| 4400 |
public: // no warning |
| 4401 |
protected: // warning here |
| 4402 |
};""", |
| 4403 |
"protected: should be indented +1 space inside class J [whitespace/indent] [3]", |
| 4404 |
) |
| 4405 |
self.TestMultiLineLint( |
| 4406 |
""" |
| 4407 |
class L |
| 4408 |
: public M, |
| 4409 |
public ::N { |
| 4410 |
};""", |
| 4411 |
"", |
| 4412 |
) |
| 4413 |
self.TestMultiLineLint( |
| 4414 |
""" |
| 4415 |
template <class O, |
| 4416 |
class P, |
| 4417 |
class Q, |
| 4418 |
typename R> |
| 4419 |
static void Func() { |
| 4420 |
}""", |
| 4421 |
"", |
| 4422 |
) |
| 4423 |
|
| 4424 |
def testConditionals(self): |
| 4425 |
self.TestMultiLineLint( |
| 4426 |
""" |
| 4427 |
if (foo) |
| 4428 |
goto fail; |
| 4429 |
goto fail;""", |
| 4430 |
"If/else bodies with multiple statements require braces [readability/braces] [4]", |
| 4431 |
) |
| 4432 |
self.TestMultiLineLint( |
| 4433 |
""" |
| 4434 |
if (foo) |
| 4435 |
goto fail; goto fail;""", |
| 4436 |
"If/else bodies with multiple statements require braces [readability/braces] [4]", |
| 4437 |
) |
| 4438 |
self.TestMultiLineLint( |
| 4439 |
""" |
| 4440 |
if (foo) |
| 4441 |
foo; |
| 4442 |
else |
| 4443 |
goto fail; |
| 4444 |
goto fail;""", |
| 4445 |
"If/else bodies with multiple statements require braces [readability/braces] [4]", |
| 4446 |
) |
| 4447 |
self.TestMultiLineLint( |
| 4448 |
""" |
| 4449 |
if (foo) goto fail; |
| 4450 |
goto fail;""", |
| 4451 |
"If/else bodies with multiple statements require braces [readability/braces] [4]", |
| 4452 |
) |
| 4453 |
self.TestMultiLineLint( |
| 4454 |
""" |
| 4455 |
if constexpr (foo) { |
| 4456 |
goto fail; |
| 4457 |
goto fail; |
| 4458 |
} else if constexpr (bar) { |
| 4459 |
hello(); |
| 4460 |
}""", |
| 4461 |
"", |
| 4462 |
) |
| 4463 |
self.TestMultiLineLint( |
| 4464 |
""" |
| 4465 |
if (foo) |
| 4466 |
if (bar) |
| 4467 |
baz; |
| 4468 |
else |
| 4469 |
qux;""", |
| 4470 |
"Else clause should be indented at the same level as if. Ambiguous" |
| 4471 |
" nested if/else chains require braces. [readability/braces] [4]", |
| 4472 |
) |
| 4473 |
self.TestMultiLineLint( |
| 4474 |
""" |
| 4475 |
if (foo) |
| 4476 |
if (bar) |
| 4477 |
baz; |
| 4478 |
else |
| 4479 |
qux;""", |
| 4480 |
"Else clause should be indented at the same level as if. Ambiguous" |
| 4481 |
" nested if/else chains require braces. [readability/braces] [4]", |
| 4482 |
) |
| 4483 |
self.TestMultiLineLint( |
| 4484 |
""" |
| 4485 |
if (foo) { |
| 4486 |
bar; |
| 4487 |
baz; |
| 4488 |
} else |
| 4489 |
qux;""", |
| 4490 |
"If an else has a brace on one side, it should have it on both" |
| 4491 |
" [readability/braces] [5]", |
| 4492 |
) |
| 4493 |
self.TestMultiLineLint( |
| 4494 |
""" |
| 4495 |
if (foo) |
| 4496 |
bar; |
| 4497 |
else { |
| 4498 |
baz; |
| 4499 |
}""", |
| 4500 |
"If an else has a brace on one side, it should have it on both" |
| 4501 |
" [readability/braces] [5]", |
| 4502 |
) |
| 4503 |
self.TestMultiLineLint( |
| 4504 |
""" |
| 4505 |
if (foo) |
| 4506 |
bar; |
| 4507 |
else if (baz) { |
| 4508 |
qux; |
| 4509 |
}""", |
| 4510 |
"If an else has a brace on one side, it should have it on both" |
| 4511 |
" [readability/braces] [5]", |
| 4512 |
) |
| 4513 |
self.TestMultiLineLint( |
| 4514 |
""" |
| 4515 |
if (foo) { |
| 4516 |
bar; |
| 4517 |
} else if (baz) |
| 4518 |
qux;""", |
| 4519 |
"If an else has a brace on one side, it should have it on both" |
| 4520 |
" [readability/braces] [5]", |
| 4521 |
) |
| 4522 |
self.TestMultiLineLint( |
| 4523 |
""" |
| 4524 |
if (foo) |
| 4525 |
goto fail; |
| 4526 |
bar;""", |
| 4527 |
"", |
| 4528 |
) |
| 4529 |
self.TestMultiLineLint( |
| 4530 |
""" |
| 4531 |
if (foo |
| 4532 |
&& bar) { |
| 4533 |
baz; |
| 4534 |
qux; |
| 4535 |
}""", |
| 4536 |
"", |
| 4537 |
) |
| 4538 |
self.TestMultiLineLint( |
| 4539 |
""" |
| 4540 |
if (foo) |
| 4541 |
goto |
| 4542 |
fail;""", |
| 4543 |
"", |
| 4544 |
) |
| 4545 |
self.TestMultiLineLint( |
| 4546 |
""" |
| 4547 |
if (foo) |
| 4548 |
bar; |
| 4549 |
else |
| 4550 |
baz; |
| 4551 |
qux;""", |
| 4552 |
"", |
| 4553 |
) |
| 4554 |
self.TestMultiLineLint( |
| 4555 |
""" |
| 4556 |
for (;;) { |
| 4557 |
if (foo) |
| 4558 |
bar; |
| 4559 |
else |
| 4560 |
baz; |
| 4561 |
}""", |
| 4562 |
"", |
| 4563 |
) |
| 4564 |
self.TestMultiLineLint( |
| 4565 |
""" |
| 4566 |
if (foo) |
| 4567 |
bar; |
| 4568 |
else if (baz) |
| 4569 |
baz;""", |
| 4570 |
"", |
| 4571 |
) |
| 4572 |
self.TestMultiLineLint( |
| 4573 |
""" |
| 4574 |
if (foo) |
| 4575 |
bar; |
| 4576 |
else |
| 4577 |
baz;""", |
| 4578 |
"", |
| 4579 |
) |
| 4580 |
self.TestMultiLineLint( |
| 4581 |
""" |
| 4582 |
if (foo) { |
| 4583 |
bar; |
| 4584 |
} else { |
| 4585 |
baz; |
| 4586 |
}""", |
| 4587 |
"", |
| 4588 |
) |
| 4589 |
self.TestMultiLineLint( |
| 4590 |
""" |
| 4591 |
if (foo) { |
| 4592 |
bar; |
| 4593 |
} else if (baz) { |
| 4594 |
qux; |
| 4595 |
}""", |
| 4596 |
"", |
| 4597 |
) |
| 4598 |
|
| 4599 |
|
| 4600 |
self.TestMultiLineLint( |
| 4601 |
""" |
| 4602 |
if (foo) |
| 4603 |
{ |
| 4604 |
bar; |
| 4605 |
baz; |
| 4606 |
}""", |
| 4607 |
"{ should almost always be at the end of the previous line [whitespace/braces] [4]", |
| 4608 |
) |
| 4609 |
self.TestMultiLineLint( |
| 4610 |
""" |
| 4611 |
void foo() { if (bar) baz; }""", |
| 4612 |
"", |
| 4613 |
) |
| 4614 |
self.TestMultiLineLint( |
| 4615 |
""" |
| 4616 |
#if foo |
| 4617 |
bar; |
| 4618 |
#else |
| 4619 |
baz; |
| 4620 |
qux; |
| 4621 |
#endif""", |
| 4622 |
"", |
| 4623 |
) |
| 4624 |
self.TestMultiLineLint( |
| 4625 |
"""void F() { |
| 4626 |
variable = [] { if (true); }; |
| 4627 |
variable = |
| 4628 |
[] { if (true); }; |
| 4629 |
Call( |
| 4630 |
[] { if (true); }, |
| 4631 |
[] { if (true); }); |
| 4632 |
}""", |
| 4633 |
"", |
| 4634 |
) |
| 4635 |
self.TestMultiLineLint( |
| 4636 |
""" |
| 4637 |
#if(A == 0) |
| 4638 |
foo(); |
| 4639 |
#elif(A == 1) |
| 4640 |
bar(); |
| 4641 |
#endif""", |
| 4642 |
"", |
| 4643 |
) |
| 4644 |
self.TestMultiLineLint( |
| 4645 |
""" |
| 4646 |
#if (A == 0) |
| 4647 |
foo(); |
| 4648 |
#elif (A == 1) |
| 4649 |
bar(); |
| 4650 |
#endif""", |
| 4651 |
"", |
| 4652 |
) |
| 4653 |
|
| 4654 |
@pytest.mark.parametrize("keyword", ["else if", "if", "while", "for", "switch"]) |
| 4655 |
def testControlClauseWithParensNewline(self, keyword): |
| 4656 |
|
| 4657 |
self.TestLintContains( |
| 4658 |
f"{keyword}{['', ' '][len(keyword) % 2]}(condition)" |
| 4659 |
f"{[' ', ''][len(keyword) % 2]}[[unlikely]]" |
| 4660 |
f"{[' ', ''][len(keyword) % 2]}{{" |
| 4661 |
f"{['', ' '][len(keyword) % 2]}do_something(); }}", |
| 4662 |
f"Controlled statements inside brackets of {keyword} clause" |
| 4663 |
f" should be on a separate line [whitespace/newline] [5]", |
| 4664 |
) |
| 4665 |
|
| 4666 |
@pytest.mark.parametrize("keyword", ["else", "do", "try"]) |
| 4667 |
def testControlClauseWithoutParensNewline(self, keyword): |
| 4668 |
|
| 4669 |
self.TestLintContains( |
| 4670 |
f"{keyword}{['', ' '][len(keyword) % 2]}{{" |
| 4671 |
f"{[' ', ''][len(keyword) % 2]}do_something(); }}", |
| 4672 |
f"Controlled statements inside brackets of {keyword} clause" |
| 4673 |
f" should be on a separate line [whitespace/newline] [5]", |
| 4674 |
) |
| 4675 |
|
| 4676 |
def testControlClauseNewlineNameFalsePositives(self): |
| 4677 |
self.TestLint(" else if_condition_do_something();", "") |
| 4678 |
self.TestLint(" } else if (blah) {", "") |
| 4679 |
self.TestLint(" variable_ends_in_else = true;", "") |
| 4680 |
|
| 4681 |
def testTab(self): |
| 4682 |
self.TestLint("\tint a;", "Tab found; better to use spaces [whitespace/tab] [1]") |
| 4683 |
self.TestLint( |
| 4684 |
"int a = 5;\t\t// set a to 5", "Tab found; better to use spaces [whitespace/tab] [1]" |
| 4685 |
) |
| 4686 |
|
| 4687 |
def testParseArguments(self): |
| 4688 |
old_output_format = cpplint._cpplint_state.output_format |
| 4689 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 4690 |
old_headers = cpplint._hpp_headers |
| 4691 |
old_filters = cpplint._cpplint_state.filters |
| 4692 |
old_line_length = cpplint._line_length |
| 4693 |
old_valid_extensions = cpplint._valid_extensions |
| 4694 |
try: |
| 4695 |
|
| 4696 |
sys.stdout = open(os.devnull, "w") |
| 4697 |
sys.stderr = open(os.devnull, "w") |
| 4698 |
|
| 4699 |
with pytest.raises(SystemExit): |
| 4700 |
cpplint.ParseArguments([]) |
| 4701 |
with pytest.raises(SystemExit): |
| 4702 |
cpplint.ParseArguments(["--badopt"]) |
| 4703 |
with pytest.raises(SystemExit): |
| 4704 |
cpplint.ParseArguments(["--help"]) |
| 4705 |
with pytest.raises(SystemExit): |
| 4706 |
cpplint.ParseArguments(["--version"]) |
| 4707 |
with pytest.raises(SystemExit): |
| 4708 |
cpplint.ParseArguments(["--v=0"]) |
| 4709 |
with pytest.raises(SystemExit): |
| 4710 |
cpplint.ParseArguments(["--filter="]) |
| 4711 |
|
| 4712 |
with pytest.raises(SystemExit): |
| 4713 |
cpplint.ParseArguments(["--filter=foo"]) |
| 4714 |
with pytest.raises(SystemExit): |
| 4715 |
cpplint.ParseArguments(["--filter=+a,b,-c"]) |
| 4716 |
with pytest.raises(SystemExit): |
| 4717 |
cpplint.ParseArguments(["--linelength=0"]) |
| 4718 |
with pytest.raises(SystemExit): |
| 4719 |
cpplint.ParseArguments(["--headers"]) |
| 4720 |
|
| 4721 |
assert cpplint.ParseArguments(["foo.cc"]) == ["foo.cc"] |
| 4722 |
assert old_output_format == cpplint._cpplint_state.output_format |
| 4723 |
assert old_verbose_level == cpplint._cpplint_state.verbose_level |
| 4724 |
|
| 4725 |
assert cpplint.ParseArguments(["--v=1", "foo.cc"]) == ["foo.cc"] |
| 4726 |
assert cpplint._cpplint_state.verbose_level == 1 |
| 4727 |
assert cpplint.ParseArguments(["--v=3", "foo.h"]) == ["foo.h"] |
| 4728 |
assert cpplint._cpplint_state.verbose_level == 3 |
| 4729 |
assert cpplint.ParseArguments(["--verbose=5", "foo.cpp"]) == ["foo.cpp"] |
| 4730 |
assert cpplint._cpplint_state.verbose_level == 5 |
| 4731 |
with pytest.raises( |
| 4732 |
ValueError, match=re.escape("invalid literal for int() with base 10: 'f'") |
| 4733 |
): |
| 4734 |
cpplint.ParseArguments(["--v=f", "foo.cc"]) |
| 4735 |
|
| 4736 |
assert cpplint.ParseArguments(["--output=emacs", "foo.cc"]) == ["foo.cc"] |
| 4737 |
assert cpplint._cpplint_state.output_format == "emacs" |
| 4738 |
assert cpplint.ParseArguments(["--output=vs7", "foo.h"]) == ["foo.h"] |
| 4739 |
assert cpplint._cpplint_state.output_format == "vs7" |
| 4740 |
with pytest.raises(SystemExit): |
| 4741 |
cpplint.ParseArguments(["--output=blah", "foo.cc"]) |
| 4742 |
|
| 4743 |
filt = "-,+whitespace,-whitespace/indent" |
| 4744 |
assert cpplint.ParseArguments(["--filter=" + filt, "foo.h"]) == ["foo.h"] |
| 4745 |
assert cpplint._cpplint_state.filters == ["-", "+whitespace", "-whitespace/indent"] |
| 4746 |
|
| 4747 |
assert cpplint.ParseArguments(["foo.cc", "foo.h"]) == ["foo.cc", "foo.h"] |
| 4748 |
|
| 4749 |
cpplint._hpp_headers = old_headers |
| 4750 |
cpplint._valid_extensions = old_valid_extensions |
| 4751 |
assert cpplint.ParseArguments(["--linelength=120", "foo.h"]) == ["foo.h"] |
| 4752 |
assert cpplint._line_length == 120 |
| 4753 |
assert { |
| 4754 |
"h", |
| 4755 |
"hh", |
| 4756 |
"hpp", |
| 4757 |
"hxx", |
| 4758 |
"h++", |
| 4759 |
"cuh", |
| 4760 |
} == cpplint.GetHeaderExtensions() |
| 4761 |
|
| 4762 |
cpplint._hpp_headers = old_headers |
| 4763 |
cpplint._valid_extensions = old_valid_extensions |
| 4764 |
assert cpplint.ParseArguments(["--headers=h", "foo.h"]) == ["foo.h"] |
| 4765 |
assert {"h", "c", "cc", "cpp", "cxx", "c++", "cu"} == cpplint.GetAllExtensions() |
| 4766 |
|
| 4767 |
cpplint._hpp_headers = old_headers |
| 4768 |
cpplint._valid_extensions = old_valid_extensions |
| 4769 |
assert cpplint.ParseArguments(["--extensions=hpp,cpp,cpp", "foo.h"]) == ["foo.h"] |
| 4770 |
assert {"hpp", "cpp"} == cpplint.GetAllExtensions() |
| 4771 |
assert {"hpp"} == cpplint.GetHeaderExtensions() |
| 4772 |
|
| 4773 |
cpplint._hpp_headers = old_headers |
| 4774 |
cpplint._valid_extensions = old_valid_extensions |
| 4775 |
assert cpplint.ParseArguments(["--extensions=cpp,cpp", "--headers=hpp,h", "foo.h"]) == [ |
| 4776 |
"foo.h" |
| 4777 |
] |
| 4778 |
assert {"hpp", "h"} == cpplint.GetHeaderExtensions() |
| 4779 |
assert {"hpp", "h", "cpp"} == cpplint.GetAllExtensions() |
| 4780 |
|
| 4781 |
finally: |
| 4782 |
sys.stdout = sys.__stdout__ |
| 4783 |
sys.stderr = sys.__stderr__ |
| 4784 |
cpplint._cpplint_state.output_format = old_output_format |
| 4785 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 4786 |
cpplint._cpplint_state.filters = old_filters |
| 4787 |
cpplint._line_length = old_line_length |
| 4788 |
cpplint._valid_extensions = old_valid_extensions |
| 4789 |
cpplint._hpp_headers = old_headers |
| 4790 |
|
| 4791 |
def testRecursiveArgument(self): |
| 4792 |
working_dir = os.getcwd() |
| 4793 |
temp_dir = os.path.realpath(tempfile.mkdtemp()) |
| 4794 |
try: |
| 4795 |
src_dir = os.path.join(temp_dir, "src") |
| 4796 |
nested_dir = os.path.join(temp_dir, "src", "nested") |
| 4797 |
os.makedirs(nested_dir) |
| 4798 |
open(os.path.join(temp_dir, "one.cpp"), "w").close() |
| 4799 |
open(os.path.join(src_dir, "two.cpp"), "w").close() |
| 4800 |
open(os.path.join(nested_dir, "three.cpp"), "w").close() |
| 4801 |
os.chdir(temp_dir) |
| 4802 |
expected = [ |
| 4803 |
"one.cpp", |
| 4804 |
os.path.join("src", "two.cpp"), |
| 4805 |
os.path.join("src", "nested", "three.cpp"), |
| 4806 |
] |
| 4807 |
cpplint._excludes = None |
| 4808 |
actual = cpplint.ParseArguments(["--recursive", "one.cpp", "src"]) |
| 4809 |
assert set(expected) == set(actual) |
| 4810 |
finally: |
| 4811 |
os.chdir(working_dir) |
| 4812 |
shutil.rmtree(temp_dir) |
| 4813 |
|
| 4814 |
def testRecursiveExcludeInvalidFileExtension(self): |
| 4815 |
working_dir = os.getcwd() |
| 4816 |
temp_dir = os.path.realpath(tempfile.mkdtemp()) |
| 4817 |
try: |
| 4818 |
src_dir = os.path.join(temp_dir, "src") |
| 4819 |
os.makedirs(src_dir) |
| 4820 |
open(os.path.join(temp_dir, "one.cpp"), "w").close() |
| 4821 |
open(os.path.join(src_dir, "two.cpp"), "w").close() |
| 4822 |
open(os.path.join(src_dir, "three.cc"), "w").close() |
| 4823 |
os.chdir(temp_dir) |
| 4824 |
expected = ["one.cpp", os.path.join("src", "two.cpp")] |
| 4825 |
cpplint._excludes = None |
| 4826 |
actual = cpplint.ParseArguments(["--recursive", "--extensions=cpp", "one.cpp", "src"]) |
| 4827 |
assert set(expected) == set(actual) |
| 4828 |
finally: |
| 4829 |
os.chdir(working_dir) |
| 4830 |
shutil.rmtree(temp_dir) |
| 4831 |
cpplint._hpp_headers = set() |
| 4832 |
cpplint._valid_extensions = set() |
| 4833 |
|
| 4834 |
def testRecursiveExclude(self): |
| 4835 |
working_dir = os.getcwd() |
| 4836 |
temp_dir = os.path.realpath(tempfile.mkdtemp()) |
| 4837 |
try: |
| 4838 |
src_dir = os.path.join(temp_dir, "src") |
| 4839 |
src2_dir = os.path.join(temp_dir, "src2") |
| 4840 |
os.makedirs(src_dir) |
| 4841 |
os.makedirs(src2_dir) |
| 4842 |
open(os.path.join(src_dir, "one.cc"), "w").close() |
| 4843 |
open(os.path.join(src_dir, "two.cc"), "w").close() |
| 4844 |
open(os.path.join(src_dir, "three.cc"), "w").close() |
| 4845 |
open(os.path.join(src2_dir, "one.cc"), "w").close() |
| 4846 |
open(os.path.join(src2_dir, "two.cc"), "w").close() |
| 4847 |
open(os.path.join(src2_dir, "three.cc"), "w").close() |
| 4848 |
os.chdir(temp_dir) |
| 4849 |
|
| 4850 |
expected = [ |
| 4851 |
os.path.join("src", "one.cc"), |
| 4852 |
os.path.join("src", "two.cc"), |
| 4853 |
os.path.join("src", "three.cc"), |
| 4854 |
] |
| 4855 |
cpplint._excludes = None |
| 4856 |
actual = cpplint.ParseArguments(["src"]) |
| 4857 |
assert {"src"} == set(actual) |
| 4858 |
|
| 4859 |
cpplint._excludes = None |
| 4860 |
actual = cpplint.ParseArguments(["--recursive", "src"]) |
| 4861 |
assert set(expected) == set(actual) |
| 4862 |
|
| 4863 |
expected = [os.path.join("src", "one.cc")] |
| 4864 |
cpplint._excludes = None |
| 4865 |
actual = cpplint.ParseArguments(["--recursive", f"--exclude=src{os.sep}t*", "src"]) |
| 4866 |
assert set(expected) == set(actual) |
| 4867 |
|
| 4868 |
expected = [os.path.join("src", "one.cc")] |
| 4869 |
cpplint._excludes = None |
| 4870 |
actual = cpplint.ParseArguments( |
| 4871 |
["--recursive", "--exclude=src/two.cc", "--exclude=src/three.cc", "src"] |
| 4872 |
) |
| 4873 |
assert set(expected) == set(actual) |
| 4874 |
|
| 4875 |
expected = { |
| 4876 |
os.path.join("src2", "one.cc"), |
| 4877 |
os.path.join("src2", "two.cc"), |
| 4878 |
os.path.join("src2", "three.cc"), |
| 4879 |
} |
| 4880 |
cpplint._excludes = None |
| 4881 |
actual = cpplint.ParseArguments(["--recursive", "--exclude=src", "."]) |
| 4882 |
assert expected == set(actual) |
| 4883 |
finally: |
| 4884 |
os.chdir(working_dir) |
| 4885 |
shutil.rmtree(temp_dir) |
| 4886 |
|
| 4887 |
def testJUnitXML(self): |
| 4888 |
try: |
| 4889 |
cpplint._cpplint_state._junit_errors = [] |
| 4890 |
cpplint._cpplint_state._junit_failures = [] |
| 4891 |
expected = ( |
| 4892 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4893 |
'<testsuite errors="0" failures="0" name="cpplint" tests="1">' |
| 4894 |
'<testcase name="passed" />' |
| 4895 |
"</testsuite>" |
| 4896 |
) |
| 4897 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4898 |
|
| 4899 |
cpplint._cpplint_state._junit_errors = ["ErrMsg1"] |
| 4900 |
cpplint._cpplint_state._junit_failures = [] |
| 4901 |
expected = ( |
| 4902 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4903 |
'<testsuite errors="1" failures="0" name="cpplint" tests="1">' |
| 4904 |
'<testcase name="errors"><error>ErrMsg1</error></testcase>' |
| 4905 |
"</testsuite>" |
| 4906 |
) |
| 4907 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4908 |
|
| 4909 |
cpplint._cpplint_state._junit_errors = ["ErrMsg1", "ErrMsg2"] |
| 4910 |
cpplint._cpplint_state._junit_failures = [] |
| 4911 |
expected = ( |
| 4912 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4913 |
'<testsuite errors="2" failures="0" name="cpplint" tests="2">' |
| 4914 |
'<testcase name="errors"><error>ErrMsg1\nErrMsg2</error></testcase>' |
| 4915 |
"</testsuite>" |
| 4916 |
) |
| 4917 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4918 |
|
| 4919 |
cpplint._cpplint_state._junit_errors = ["ErrMsg"] |
| 4920 |
cpplint._cpplint_state._junit_failures = [ |
| 4921 |
("File", 5, "FailMsg", "category/subcategory", 3) |
| 4922 |
] |
| 4923 |
expected = ( |
| 4924 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4925 |
'<testsuite errors="1" failures="1" name="cpplint" tests="2">' |
| 4926 |
'<testcase name="errors"><error>ErrMsg</error></testcase>' |
| 4927 |
'<testcase name="File"><failure>5: FailMsg [category/subcategory] ' |
| 4928 |
"[3]</failure></testcase></testsuite>" |
| 4929 |
) |
| 4930 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4931 |
|
| 4932 |
cpplint._cpplint_state._junit_errors = [] |
| 4933 |
cpplint._cpplint_state._junit_failures = [ |
| 4934 |
("File1", 5, "FailMsg1", "category/subcategory", 3), |
| 4935 |
("File2", 99, "FailMsg2", "category/subcategory", 3), |
| 4936 |
("File1", 19, "FailMsg3", "category/subcategory", 3), |
| 4937 |
] |
| 4938 |
expected = ( |
| 4939 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4940 |
'<testsuite errors="0" failures="3" name="cpplint" tests="3">' |
| 4941 |
'<testcase name="File1"><failure>5: FailMsg1 [category/subcategory]' |
| 4942 |
" [3]\n19: FailMsg3 [category/subcategory] [3]</failure></testcase>" |
| 4943 |
'<testcase name="File2"><failure>99: FailMsg2 ' |
| 4944 |
"[category/subcategory] [3]</failure></testcase></testsuite>" |
| 4945 |
) |
| 4946 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4947 |
|
| 4948 |
cpplint._cpplint_state._junit_errors = ["&</error>"] |
| 4949 |
cpplint._cpplint_state._junit_failures = [ |
| 4950 |
("File1", 5, "&</failure>", "category/subcategory", 3) |
| 4951 |
] |
| 4952 |
expected = ( |
| 4953 |
'<?xml version="1.0" encoding="UTF-8" ?>\n' |
| 4954 |
'<testsuite errors="1" failures="1" name="cpplint" tests="2">' |
| 4955 |
'<testcase name="errors"><error>&</error></error>' |
| 4956 |
'</testcase><testcase name="File1"><failure>5: ' |
| 4957 |
"&</failure> [category/subcategory] [3]</failure>" |
| 4958 |
"</testcase></testsuite>" |
| 4959 |
) |
| 4960 |
assert expected == cpplint._cpplint_state.FormatJUnitXML() |
| 4961 |
|
| 4962 |
finally: |
| 4963 |
cpplint._cpplint_state._junit_errors = [] |
| 4964 |
cpplint._cpplint_state._junit_failures = [] |
| 4965 |
|
| 4966 |
def testQuiet(self): |
| 4967 |
assert cpplint._cpplint_state.quiet is False |
| 4968 |
cpplint.ParseArguments(["--quiet", "one.cpp"]) |
| 4969 |
assert cpplint._cpplint_state.quiet is True |
| 4970 |
|
| 4971 |
def testLineLength(self): |
| 4972 |
old_line_length = cpplint._line_length |
| 4973 |
try: |
| 4974 |
cpplint._line_length = 80 |
| 4975 |
self.TestLint("// H %s" % ("H" * 75), "") |
| 4976 |
self.TestLint( |
| 4977 |
"// H %s" % ("H" * 76), |
| 4978 |
"Lines should be <= 80 characters long [whitespace/line_length] [2]", |
| 4979 |
) |
| 4980 |
cpplint._line_length = 120 |
| 4981 |
self.TestLint("// H %s" % ("H" * 115), "") |
| 4982 |
self.TestLint( |
| 4983 |
"// H %s" % ("H" * 116), |
| 4984 |
"Lines should be <= 120 characters long [whitespace/line_length] [2]", |
| 4985 |
) |
| 4986 |
finally: |
| 4987 |
cpplint._line_length = old_line_length |
| 4988 |
|
| 4989 |
def testFilter(self): |
| 4990 |
old_filters = cpplint._cpplint_state.filters |
| 4991 |
try: |
| 4992 |
cpplint._cpplint_state.SetFilters("-,+whitespace,-whitespace/indent") |
| 4993 |
self.TestLint( |
| 4994 |
"// Hello there ", |
| 4995 |
"Line ends in whitespace. Consider deleting these extra spaces." |
| 4996 |
" [whitespace/end_of_line] [4]", |
| 4997 |
) |
| 4998 |
self.TestLint("int a = (int)1.0;", "") |
| 4999 |
self.TestLint(" weird opening space", "") |
| 5000 |
finally: |
| 5001 |
cpplint._cpplint_state.filters = old_filters |
| 5002 |
|
| 5003 |
def testDefaultFilter(self): |
| 5004 |
default_filters = cpplint._DEFAULT_FILTERS |
| 5005 |
old_filters = cpplint._cpplint_state.filters |
| 5006 |
cpplint._DEFAULT_FILTERS = ["-whitespace"] |
| 5007 |
try: |
| 5008 |
|
| 5009 |
cpplint._cpplint_state.SetFilters("") |
| 5010 |
self.TestLint("// Hello there ", "") |
| 5011 |
cpplint._cpplint_state.SetFilters("+whitespace/end_of_line") |
| 5012 |
self.TestLint( |
| 5013 |
"// Hello there ", |
| 5014 |
"Line ends in whitespace. Consider deleting these extra spaces." |
| 5015 |
" [whitespace/end_of_line] [4]", |
| 5016 |
) |
| 5017 |
self.TestLint(" weird opening space", "") |
| 5018 |
finally: |
| 5019 |
cpplint._cpplint_state.filters = old_filters |
| 5020 |
cpplint._DEFAULT_FILTERS = default_filters |
| 5021 |
|
| 5022 |
def testFileSpecificFilter(self): |
| 5023 |
old_filters = cpplint._cpplint_state.filters |
| 5024 |
try: |
| 5025 |
test_code = """ |
| 5026 |
class Foo { |
| 5027 |
Foo(int f) |
| 5028 |
{ |
| 5029 |
} |
| 5030 |
};""" |
| 5031 |
cpplint._cpplint_state.SetFilters("") |
| 5032 |
self.TestMultiLineLint( |
| 5033 |
test_code, |
| 5034 |
[ |
| 5035 |
"Single-parameter constructors should be marked explicit." |
| 5036 |
" [runtime/explicit] [4]", |
| 5037 |
"{ should almost always be at the end of the previous line" |
| 5038 |
" [whitespace/braces] [4]", |
| 5039 |
], |
| 5040 |
) |
| 5041 |
|
| 5042 |
cpplint._cpplint_state.SetFilters("-runtime/explicit:foo.h") |
| 5043 |
self.TestMultiLineLint( |
| 5044 |
test_code, |
| 5045 |
"{ should almost always be at the end of the previous line" |
| 5046 |
" [whitespace/braces] [4]", |
| 5047 |
) |
| 5048 |
|
| 5049 |
cpplint._cpplint_state.SetFilters("-runtime/explicit:foo.h:2") |
| 5050 |
self.TestMultiLineLint( |
| 5051 |
test_code, |
| 5052 |
"{ should almost always be at the end of the previous line" |
| 5053 |
" [whitespace/braces] [4]", |
| 5054 |
) |
| 5055 |
|
| 5056 |
cpplint._cpplint_state.SetFilters( |
| 5057 |
"-runtime/explicit:foo.h:14,-whitespace/braces:otherfile.h:3" |
| 5058 |
) |
| 5059 |
self.TestMultiLineLint( |
| 5060 |
test_code, |
| 5061 |
[ |
| 5062 |
"Single-parameter constructors should be marked explicit." |
| 5063 |
" [runtime/explicit] [4]", |
| 5064 |
"{ should almost always be at the end of the previous line" |
| 5065 |
" [whitespace/braces] [4]", |
| 5066 |
], |
| 5067 |
) |
| 5068 |
|
| 5069 |
cpplint._cpplint_state.SetFilters( |
| 5070 |
"-runtime/explicit:foo.h:2,-whitespace/braces:foo.h:3" |
| 5071 |
) |
| 5072 |
self.TestMultiLineLint(test_code, "") |
| 5073 |
finally: |
| 5074 |
cpplint._cpplint_state.filters = old_filters |
| 5075 |
|
| 5076 |
def testDuplicateHeader(self): |
| 5077 |
error_collector = ErrorCollector(self.assertTrue) |
| 5078 |
cpplint.ProcessFileData( |
| 5079 |
"path/self.cc", |
| 5080 |
"cc", |
| 5081 |
[ |
| 5082 |
"// Copyright 2014 Your Company. All Rights Reserved.", |
| 5083 |
'#include "path/self.h"', |
| 5084 |
'#include "path/duplicate.h"', |
| 5085 |
'#include "path/duplicate.h"', |
| 5086 |
"#ifdef MACRO", |
| 5087 |
'#include "path/unique.h"', |
| 5088 |
"#else", |
| 5089 |
'#include "path/unique.h"', |
| 5090 |
"#endif", |
| 5091 |
"", |
| 5092 |
], |
| 5093 |
error_collector, |
| 5094 |
) |
| 5095 |
assert error_collector.ResultList() == [ |
| 5096 |
'"path/duplicate.h" already included at path/self.cc:3 [build/include] [4]' |
| 5097 |
] |
| 5098 |
|
| 5099 |
def testUnnamedNamespacesInHeaders(self): |
| 5100 |
for extension in ["h", "hpp", "hxx", "h++", "cuh"]: |
| 5101 |
self.doTestUnnamedNamespacesInHeaders(extension) |
| 5102 |
|
| 5103 |
def doTestUnnamedNamespacesInHeaders(self, extension): |
| 5104 |
self.TestLanguageRulesCheck( |
| 5105 |
"foo." + extension, |
| 5106 |
"namespace {", |
| 5107 |
"Do not use unnamed namespaces in header files. See" |
| 5108 |
" https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces" |
| 5109 |
" for more information. [build/namespaces_headers] [4]", |
| 5110 |
) |
| 5111 |
|
| 5112 |
self.TestLanguageRulesCheck("foo." + extension, "namespace { \\", "") |
| 5113 |
|
| 5114 |
self.TestLanguageRulesCheck("foo." + extension, "namespace foo {", "") |
| 5115 |
self.TestLanguageRulesCheck("foo." + extension, "namespace foonamespace {", "") |
| 5116 |
|
| 5117 |
def testUnnamedNamespacesInNonHeaders(self): |
| 5118 |
for extension in ["c", "cc", "cpp", "cxx", "c++", "cu"]: |
| 5119 |
self.TestLanguageRulesCheck("foo." + extension, "namespace {", "") |
| 5120 |
self.TestLanguageRulesCheck("foo." + extension, "namespace foo {", "") |
| 5121 |
|
| 5122 |
def testBuildClass(self): |
| 5123 |
|
| 5124 |
|
| 5125 |
|
| 5126 |
self.TestMultiLineLint("class Foo;", "") |
| 5127 |
self.TestMultiLineLint( |
| 5128 |
"""struct Foo* |
| 5129 |
foo = NewFoo();""", |
| 5130 |
"", |
| 5131 |
) |
| 5132 |
|
| 5133 |
self.TestMultiLineLint( |
| 5134 |
"""#ifdef DERIVE_FROM_GOO |
| 5135 |
struct Foo : public Goo { |
| 5136 |
#else |
| 5137 |
struct Foo : public Hoo { |
| 5138 |
#endif |
| 5139 |
};""", |
| 5140 |
"", |
| 5141 |
) |
| 5142 |
self.TestMultiLineLint( |
| 5143 |
""" |
| 5144 |
class Foo |
| 5145 |
#ifdef DERIVE_FROM_GOO |
| 5146 |
: public Goo { |
| 5147 |
#else |
| 5148 |
: public Hoo { |
| 5149 |
#endif |
| 5150 |
};""", |
| 5151 |
"", |
| 5152 |
) |
| 5153 |
|
| 5154 |
def testBuildEndComment(self): |
| 5155 |
|
| 5156 |
|
| 5157 |
self.TestMultiLineLint( |
| 5158 |
"""#if 0 |
| 5159 |
#endif Not a comment""", |
| 5160 |
"Uncommented text after #endif is non-standard. Use a comment." |
| 5161 |
" [build/endif_comment] [5]", |
| 5162 |
) |
| 5163 |
|
| 5164 |
def testBuildForwardDecl(self): |
| 5165 |
|
| 5166 |
|
| 5167 |
self.TestLint( |
| 5168 |
"class Foo::Goo;", |
| 5169 |
"Inner-style forward declarations are invalid." |
| 5170 |
" Remove this line." |
| 5171 |
" [build/forward_decl] [5]", |
| 5172 |
) |
| 5173 |
|
| 5174 |
def testBuildDeprecated(self): |
| 5175 |
self.TestLint( |
| 5176 |
"a <? b", |
| 5177 |
">? and <? (max and min) operators are non-standard and deprecated." |
| 5178 |
" [build/deprecated] [3]", |
| 5179 |
) |
| 5180 |
self.TestLint( |
| 5181 |
"c >?= b", |
| 5182 |
">? and <? (max and min) operators are non-standard and deprecated." |
| 5183 |
" [build/deprecated] [3]", |
| 5184 |
) |
| 5185 |
|
| 5186 |
def GetBuildHeaderGuardPreprocessorSymbol(self, file_path): |
| 5187 |
|
| 5188 |
error_collector = ErrorCollector(self.assertTrue) |
| 5189 |
cpplint.ProcessFileData(file_path, "h", [], error_collector) |
| 5190 |
for error in error_collector.ResultList(): |
| 5191 |
matched = re.search( |
| 5192 |
"No #ifndef header guard found, suggested CPP variable is: ([A-Z0-9_]+)", error |
| 5193 |
) |
| 5194 |
if matched is not None: |
| 5195 |
return matched.group(1) |
| 5196 |
return None |
| 5197 |
|
| 5198 |
def testBuildHeaderGuard(self): |
| 5199 |
file_path = "mydir/foo.h" |
| 5200 |
expected_guard = self.GetBuildHeaderGuardPreprocessorSymbol(file_path) |
| 5201 |
assert re.search("MYDIR_FOO_H_$", expected_guard) |
| 5202 |
|
| 5203 |
|
| 5204 |
error_collector = ErrorCollector(self.assertTrue) |
| 5205 |
cpplint.ProcessFileData(file_path, "h", [], error_collector) |
| 5206 |
assert ( |
| 5207 |
error_collector.ResultList().count( |
| 5208 |
"No #ifndef header guard found, suggested CPP variable is: %s " |
| 5209 |
"[build/header_guard] [5]" % expected_guard |
| 5210 |
) |
| 5211 |
== 1 |
| 5212 |
), error_collector.ResultList() |
| 5213 |
|
| 5214 |
|
| 5215 |
error_collector = ErrorCollector(self.assertTrue) |
| 5216 |
cpplint.ProcessFileData( |
| 5217 |
file_path, |
| 5218 |
"h", |
| 5219 |
["// Copyright 2014 Your Company.", "// NOLINT(build/header_guard)", ""], |
| 5220 |
error_collector, |
| 5221 |
) |
| 5222 |
assert error_collector.ResultList() == [] |
| 5223 |
|
| 5224 |
|
| 5225 |
error_collector = ErrorCollector(self.assertTrue) |
| 5226 |
cpplint.ProcessFileData(file_path, "h", ["#ifndef FOO_H", "#define FOO_H"], error_collector) |
| 5227 |
assert ( |
| 5228 |
error_collector.ResultList().count( |
| 5229 |
"#ifndef header guard has wrong style, please use: %s [build/header_guard] [5]" |
| 5230 |
% expected_guard |
| 5231 |
) |
| 5232 |
== 1 |
| 5233 |
), error_collector.ResultList() |
| 5234 |
|
| 5235 |
|
| 5236 |
error_collector = ErrorCollector(self.assertTrue) |
| 5237 |
cpplint.ProcessFileData(file_path, "h", ["#ifndef %s" % expected_guard], error_collector) |
| 5238 |
assert ( |
| 5239 |
error_collector.ResultList().count( |
| 5240 |
"No #ifndef header guard found, suggested CPP variable is: %s " |
| 5241 |
"[build/header_guard] [5]" % expected_guard |
| 5242 |
) |
| 5243 |
== 1 |
| 5244 |
), error_collector.ResultList() |
| 5245 |
|
| 5246 |
|
| 5247 |
error_collector = ErrorCollector(self.assertTrue) |
| 5248 |
cpplint.ProcessFileData( |
| 5249 |
file_path, "h", ["#ifndef %s" % expected_guard, "#define FOO_H"], error_collector |
| 5250 |
) |
| 5251 |
assert ( |
| 5252 |
error_collector.ResultList().count( |
| 5253 |
"No #ifndef header guard found, suggested CPP variable is: %s " |
| 5254 |
"[build/header_guard] [5]" % expected_guard |
| 5255 |
) |
| 5256 |
== 1 |
| 5257 |
), error_collector.ResultList() |
| 5258 |
|
| 5259 |
|
| 5260 |
error_collector = ErrorCollector(self.assertTrue) |
| 5261 |
cpplint.ProcessFileData( |
| 5262 |
file_path, |
| 5263 |
"h", |
| 5264 |
["#ifndef %s" % expected_guard, "#define %s" % expected_guard, ""], |
| 5265 |
error_collector, |
| 5266 |
) |
| 5267 |
assert ( |
| 5268 |
error_collector.ResultList().count( |
| 5269 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5270 |
) |
| 5271 |
== 1 |
| 5272 |
), error_collector.ResultList() |
| 5273 |
|
| 5274 |
|
| 5275 |
error_collector = ErrorCollector(self.assertTrue) |
| 5276 |
cpplint.ProcessFileData( |
| 5277 |
file_path, |
| 5278 |
"h", |
| 5279 |
["#ifndef %s" % expected_guard, "#define %s" % expected_guard, "#endif"], |
| 5280 |
error_collector, |
| 5281 |
) |
| 5282 |
assert ( |
| 5283 |
error_collector.ResultList().count( |
| 5284 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5285 |
) |
| 5286 |
== 1 |
| 5287 |
), error_collector.ResultList() |
| 5288 |
|
| 5289 |
|
| 5290 |
error_collector = ErrorCollector(self.assertTrue) |
| 5291 |
cpplint.ProcessFileData( |
| 5292 |
file_path, |
| 5293 |
"h", |
| 5294 |
["#ifndef %s_" % expected_guard, "#define %s_" % expected_guard, "#endif"], |
| 5295 |
error_collector, |
| 5296 |
) |
| 5297 |
assert ( |
| 5298 |
error_collector.ResultList().count( |
| 5299 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5300 |
) |
| 5301 |
== 1 |
| 5302 |
), error_collector.ResultList() |
| 5303 |
|
| 5304 |
|
| 5305 |
error_collector = ErrorCollector(self.assertTrue) |
| 5306 |
cpplint.ProcessFileData( |
| 5307 |
file_path, |
| 5308 |
"h", |
| 5309 |
[ |
| 5310 |
"#ifndef %s" % expected_guard, |
| 5311 |
"#define %s" % expected_guard, |
| 5312 |
"#endif // %s" % expected_guard, |
| 5313 |
], |
| 5314 |
error_collector, |
| 5315 |
) |
| 5316 |
for line in error_collector.ResultList(): |
| 5317 |
assert "build/header_guard" not in line |
| 5318 |
|
| 5319 |
|
| 5320 |
error_collector = ErrorCollector(self.assertTrue) |
| 5321 |
cpplint.ProcessFileData( |
| 5322 |
file_path, |
| 5323 |
"h", |
| 5324 |
[ |
| 5325 |
"#ifndef %s_" % expected_guard, |
| 5326 |
"#define %s_" % expected_guard, |
| 5327 |
"#endif // %s_" % expected_guard, |
| 5328 |
], |
| 5329 |
error_collector, |
| 5330 |
) |
| 5331 |
for line in error_collector.ResultList(): |
| 5332 |
assert "build/header_guard" not in line |
| 5333 |
|
| 5334 |
old_verbose_level = cpplint._cpplint_state.verbose_level |
| 5335 |
try: |
| 5336 |
cpplint._cpplint_state.verbose_level = 0 |
| 5337 |
|
| 5338 |
error_collector = ErrorCollector(self.assertTrue) |
| 5339 |
cpplint.ProcessFileData( |
| 5340 |
file_path, |
| 5341 |
"h", |
| 5342 |
[ |
| 5343 |
"#ifndef %s_" % expected_guard, |
| 5344 |
"#define %s_" % expected_guard, |
| 5345 |
"#endif // %s_" % expected_guard, |
| 5346 |
], |
| 5347 |
error_collector, |
| 5348 |
) |
| 5349 |
assert ( |
| 5350 |
error_collector.ResultList().count( |
| 5351 |
"#ifndef header guard has wrong style, please use: %s [build/header_guard] [0]" |
| 5352 |
% expected_guard |
| 5353 |
) |
| 5354 |
== 1 |
| 5355 |
), error_collector.ResultList() |
| 5356 |
finally: |
| 5357 |
cpplint._cpplint_state.verbose_level = old_verbose_level |
| 5358 |
|
| 5359 |
|
| 5360 |
error_collector = ErrorCollector(self.assertTrue) |
| 5361 |
cpplint.ProcessFileData( |
| 5362 |
file_path, "h", ["#ifndef FOO", "#define FOO", "#endif // FOO"], error_collector |
| 5363 |
) |
| 5364 |
assert ( |
| 5365 |
error_collector.ResultList().count( |
| 5366 |
"#ifndef header guard has wrong style, please use: %s [build/header_guard] [5]" |
| 5367 |
% expected_guard |
| 5368 |
) |
| 5369 |
== 1 |
| 5370 |
), error_collector.ResultList() |
| 5371 |
assert ( |
| 5372 |
error_collector.ResultList().count( |
| 5373 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5374 |
) |
| 5375 |
== 1 |
| 5376 |
), error_collector.ResultList() |
| 5377 |
|
| 5378 |
|
| 5379 |
error_collector = ErrorCollector(self.assertTrue) |
| 5380 |
cpplint.ProcessFileData( |
| 5381 |
file_path, |
| 5382 |
"h", |
| 5383 |
["#ifndef FOO // NOLINT", "#define FOO", "#endif // FOO NOLINT"], |
| 5384 |
error_collector, |
| 5385 |
) |
| 5386 |
assert ( |
| 5387 |
error_collector.ResultList().count( |
| 5388 |
"#ifndef header guard has wrong style, please use: %s [build/header_guard] [5]" |
| 5389 |
% expected_guard |
| 5390 |
) |
| 5391 |
== 0 |
| 5392 |
), error_collector.ResultList() |
| 5393 |
assert ( |
| 5394 |
error_collector.ResultList().count( |
| 5395 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5396 |
) |
| 5397 |
== 0 |
| 5398 |
), error_collector.ResultList() |
| 5399 |
|
| 5400 |
|
| 5401 |
for test_file in ["mydir/foo_flymake.h", "mydir/.flymake/foo.h"]: |
| 5402 |
error_collector = ErrorCollector(self.assertTrue) |
| 5403 |
cpplint.ProcessFileData( |
| 5404 |
test_file, "h", ["// Copyright 2014 Your Company.", ""], error_collector |
| 5405 |
) |
| 5406 |
assert ( |
| 5407 |
error_collector.ResultList().count( |
| 5408 |
"No #ifndef header guard found, suggested CPP variable is: %s " |
| 5409 |
"[build/header_guard] [5]" % expected_guard |
| 5410 |
) |
| 5411 |
== 1 |
| 5412 |
), error_collector.ResultList() |
| 5413 |
|
| 5414 |
|
| 5415 |
file_path = "mydir/foo.cuh" |
| 5416 |
expected_guard = self.GetBuildHeaderGuardPreprocessorSymbol(file_path) |
| 5417 |
error_collector = ErrorCollector(self.assertTrue) |
| 5418 |
cpplint.ProcessFileData( |
| 5419 |
file_path, "cuh", ["#ifndef FOO", "#define FOO", "#endif // FOO"], error_collector |
| 5420 |
) |
| 5421 |
assert ( |
| 5422 |
error_collector.ResultList().count( |
| 5423 |
"#ifndef header guard has wrong style, please use: %s [build/header_guard] [5]" |
| 5424 |
% expected_guard |
| 5425 |
) |
| 5426 |
== 1 |
| 5427 |
), error_collector.ResultList() |
| 5428 |
assert ( |
| 5429 |
error_collector.ResultList().count( |
| 5430 |
'#endif line should be "#endif // %s" [build/header_guard] [5]' % expected_guard |
| 5431 |
) |
| 5432 |
== 1 |
| 5433 |
), error_collector.ResultList() |
| 5434 |
|
| 5435 |
def testPragmaOnce(self): |
| 5436 |
error_collector = ErrorCollector(self.assertTrue) |
| 5437 |
cpplint.ProcessFileData( |
| 5438 |
"mydir/foo.h", |
| 5439 |
"h", |
| 5440 |
["// Copyright 2014 Your Company.", "#pragma once", ""], |
| 5441 |
error_collector, |
| 5442 |
) |
| 5443 |
assert error_collector.ResultList() == [] |
| 5444 |
|
| 5445 |
def testBuildHeaderGuardWithRoot(self): |
| 5446 |
temp_directory = os.path.realpath(tempfile.mkdtemp()) |
| 5447 |
try: |
| 5448 |
test_directory = os.path.join(temp_directory, "test") |
| 5449 |
os.makedirs(test_directory) |
| 5450 |
os.makedirs(os.path.join(test_directory, ".svn")) |
| 5451 |
header_directory = os.path.join(test_directory, "cpplint") |
| 5452 |
os.makedirs(header_directory) |
| 5453 |
self.doTestBuildHeaderGuardWithRoot(header_directory) |
| 5454 |
finally: |
| 5455 |
shutil.rmtree(temp_directory) |
| 5456 |
|
| 5457 |
def doTestBuildHeaderGuardWithRoot(self, header_directory): |
| 5458 |
|
| 5459 |
|
| 5460 |
file_path = os.path.join(header_directory, "cpplint_test_header.h") |
| 5461 |
open(file_path, "a").close() |
| 5462 |
file_info = cpplint.FileInfo(file_path) |
| 5463 |
if file_info.FullName() == file_info.RepositoryName(): |
| 5464 |
|
| 5465 |
|
| 5466 |
|
| 5467 |
|
| 5468 |
|
| 5469 |
|
| 5470 |
return |
| 5471 |
|
| 5472 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5473 |
|
| 5474 |
|
| 5475 |
|
| 5476 |
|
| 5477 |
|
| 5478 |
|
| 5479 |
|
| 5480 |
cpplint._root = "cpplint" |
| 5481 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_TEST_HEADER_H_" |
| 5482 |
|
| 5483 |
nested_header_directory = os.path.join(header_directory, "nested") |
| 5484 |
nested_file_path = os.path.join(nested_header_directory, "cpplint_test_header.h") |
| 5485 |
os.makedirs(nested_header_directory) |
| 5486 |
open(nested_file_path, "a").close() |
| 5487 |
|
| 5488 |
cpplint._root = os.path.join("cpplint", "nested") |
| 5489 |
actual = cpplint.GetHeaderGuardCPPVariable(nested_file_path) |
| 5490 |
assert actual == "CPPLINT_TEST_HEADER_H_" |
| 5491 |
|
| 5492 |
|
| 5493 |
|
| 5494 |
cpplint._root = header_directory |
| 5495 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_TEST_HEADER_H_" |
| 5496 |
|
| 5497 |
cpplint._root = nested_header_directory |
| 5498 |
assert cpplint.GetHeaderGuardCPPVariable(nested_file_path) == "CPPLINT_TEST_HEADER_H_" |
| 5499 |
|
| 5500 |
|
| 5501 |
cpplint._root = "NON_EXISTENT_DIR" |
| 5502 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5503 |
|
| 5504 |
|
| 5505 |
|
| 5506 |
|
| 5507 |
|
| 5508 |
|
| 5509 |
this_files_path = os.path.dirname(os.path.abspath(file_path)) |
| 5510 |
(styleguide_path, this_files_dir) = os.path.split(this_files_path) |
| 5511 |
(styleguide_parent_path, styleguide_dir_name) = os.path.split(styleguide_path) |
| 5512 |
|
| 5513 |
cpplint._root = styleguide_parent_path |
| 5514 |
assert styleguide_parent_path is not None |
| 5515 |
|
| 5516 |
expected_prefix = re.sub(r"[^a-zA-Z0-9]", "_", styleguide_dir_name).upper() + "_" |
| 5517 |
|
| 5518 |
assert ( |
| 5519 |
"%sCPPLINT_CPPLINT_TEST_HEADER_H_" % expected_prefix |
| 5520 |
== cpplint.GetHeaderGuardCPPVariable(file_path) |
| 5521 |
) |
| 5522 |
|
| 5523 |
|
| 5524 |
cur_dir = os.getcwd() |
| 5525 |
os.chdir(this_files_path) |
| 5526 |
|
| 5527 |
|
| 5528 |
styleguide_rel_path = os.path.relpath(styleguide_path, this_files_path) |
| 5529 |
|
| 5530 |
cpplint._root = styleguide_rel_path |
| 5531 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5532 |
|
| 5533 |
styleguide_rel_path = os.path.relpath(styleguide_parent_path, this_files_path) |
| 5534 |
cpplint._root = styleguide_rel_path |
| 5535 |
assert ( |
| 5536 |
"%sCPPLINT_CPPLINT_TEST_HEADER_H_" % expected_prefix |
| 5537 |
== cpplint.GetHeaderGuardCPPVariable(file_path) |
| 5538 |
) |
| 5539 |
|
| 5540 |
cpplint._root = None |
| 5541 |
|
| 5542 |
|
| 5543 |
os.chdir(cur_dir) |
| 5544 |
|
| 5545 |
def testIncludeItsHeader(self): |
| 5546 |
temp_directory = os.path.realpath(tempfile.mkdtemp()) |
| 5547 |
cur_dir = os.getcwd() |
| 5548 |
try: |
| 5549 |
test_directory = os.path.join(temp_directory, "test") |
| 5550 |
os.makedirs(test_directory) |
| 5551 |
file_path = os.path.join(test_directory, "foo.h") |
| 5552 |
open(file_path, "a").close() |
| 5553 |
file_path = os.path.join(test_directory, "Bar.h") |
| 5554 |
open(file_path, "a").close() |
| 5555 |
|
| 5556 |
os.chdir(temp_directory) |
| 5557 |
|
| 5558 |
error_collector = ErrorCollector(self.assertTrue) |
| 5559 |
cpplint.ProcessFileData("test/foo.cc", "cc", [""], error_collector) |
| 5560 |
|
| 5561 |
if platform.system() == "Windows": |
| 5562 |
test_directory = test_directory.replace("\\", "/") |
| 5563 |
fmt = "{dir}/{fn}.cc should include its header file {dir}/{fn}.h [build/include] [5]" |
| 5564 |
expected = fmt.format(fn="foo", dir=test_directory) |
| 5565 |
assert error_collector.Results().count(expected) == 1 |
| 5566 |
|
| 5567 |
error_collector = ErrorCollector(self.assertTrue) |
| 5568 |
cpplint.ProcessFileData( |
| 5569 |
"test/foo.cc", "cc", [r'#include "test/foo.h"', ""], error_collector |
| 5570 |
) |
| 5571 |
assert error_collector.Results().count(expected) == 0 |
| 5572 |
|
| 5573 |
|
| 5574 |
|
| 5575 |
error_collector = ErrorCollector(self.assertTrue) |
| 5576 |
cpplint.ProcessFileData( |
| 5577 |
"test/foo.cc", "cc", [r'#include "./test/foo.h"', ""], error_collector |
| 5578 |
) |
| 5579 |
fmt = "{dir}/{fn}.cc should include its header file {dir}/{fn}.h{unix_text}" |
| 5580 |
expected = ( |
| 5581 |
fmt.format( |
| 5582 |
fn="foo", |
| 5583 |
dir=test_directory, |
| 5584 |
unix_text=". Relative paths like . and .. are not allowed.", |
| 5585 |
) |
| 5586 |
+ " [build/include] [5]" |
| 5587 |
) |
| 5588 |
assert error_collector.Results().count(expected) == 1 |
| 5589 |
|
| 5590 |
|
| 5591 |
error_collector = ErrorCollector(self.assertTrue) |
| 5592 |
cpplint.ProcessFileData( |
| 5593 |
"test/Bar.cc", "cc", [r'#include "test/Bar.h"', ""], error_collector |
| 5594 |
) |
| 5595 |
fmt = "{dir}/{fn}.cc should include its header file {dir}/{fn}.h [build/include] [5]" |
| 5596 |
expected = fmt.format(fn="Bar", dir=test_directory) |
| 5597 |
assert error_collector.Results().count(expected) == 0 |
| 5598 |
|
| 5599 |
|
| 5600 |
error_collector = ErrorCollector(self.assertTrue) |
| 5601 |
cpplint.ProcessFileData("test/Bar.cc", "cc", [r'#include "Bar.h"', ""], error_collector) |
| 5602 |
assert error_collector.Results().count(expected) == 0 |
| 5603 |
|
| 5604 |
|
| 5605 |
open(os.path.join(test_directory, "foo.hpp"), "a").close() |
| 5606 |
cpplint.ProcessFileData( |
| 5607 |
"test/foo.cc", "cc", [r'#include "foo.hpp"', ""], error_collector |
| 5608 |
) |
| 5609 |
assert error_collector.Results().count(expected) == 0 |
| 5610 |
|
| 5611 |
finally: |
| 5612 |
|
| 5613 |
os.chdir(cur_dir) |
| 5614 |
shutil.rmtree(temp_directory) |
| 5615 |
|
| 5616 |
def testPathSplitToList(self): |
| 5617 |
assert cpplint.PathSplitToList(os.path.join("")) == [""] |
| 5618 |
|
| 5619 |
assert cpplint.PathSplitToList(os.path.join(".")) == ["."] |
| 5620 |
|
| 5621 |
assert cpplint.PathSplitToList(os.path.join("..")) == [".."] |
| 5622 |
|
| 5623 |
assert cpplint.PathSplitToList(os.path.join("..", "a", "b")) == ["..", "a", "b"] |
| 5624 |
|
| 5625 |
assert cpplint.PathSplitToList(os.path.join("a", "b", "c", "d")) == ["a", "b", "c", "d"] |
| 5626 |
|
| 5627 |
def testBuildHeaderGuardWithRepository(self): |
| 5628 |
temp_directory = os.path.realpath(tempfile.mkdtemp()) |
| 5629 |
temp_directory2 = os.path.realpath(tempfile.mkdtemp()) |
| 5630 |
|
| 5631 |
|
| 5632 |
|
| 5633 |
|
| 5634 |
|
| 5635 |
current_directory = os.getcwd() |
| 5636 |
try: |
| 5637 |
os.chdir(temp_directory) |
| 5638 |
|
| 5639 |
os.makedirs(os.path.join(temp_directory, ".svn")) |
| 5640 |
trunk_dir = os.path.join(temp_directory, "trunk") |
| 5641 |
os.makedirs(trunk_dir) |
| 5642 |
header_directory = os.path.join(trunk_dir, "cpplint") |
| 5643 |
os.makedirs(header_directory) |
| 5644 |
file_path = os.path.join(header_directory, "cpplint_test_header.h") |
| 5645 |
open(file_path, "a").close() |
| 5646 |
|
| 5647 |
|
| 5648 |
assert ( |
| 5649 |
cpplint.GetHeaderGuardCPPVariable(file_path) |
| 5650 |
== "TRUNK_CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5651 |
) |
| 5652 |
|
| 5653 |
|
| 5654 |
cpplint._repository = os.path.relpath(trunk_dir) |
| 5655 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5656 |
cpplint._repository = os.path.abspath(trunk_dir) |
| 5657 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5658 |
|
| 5659 |
|
| 5660 |
cpplint._repository = os.path.join(temp_directory, "NON_EXISTENT") |
| 5661 |
assert ( |
| 5662 |
cpplint.GetHeaderGuardCPPVariable(file_path) |
| 5663 |
== "TRUNK_CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5664 |
) |
| 5665 |
|
| 5666 |
|
| 5667 |
cpplint._repository = os.path.relpath(temp_directory2) |
| 5668 |
assert ( |
| 5669 |
cpplint.GetHeaderGuardCPPVariable(file_path) |
| 5670 |
== "TRUNK_CPPLINT_CPPLINT_TEST_HEADER_H_" |
| 5671 |
) |
| 5672 |
|
| 5673 |
|
| 5674 |
cpplint._repository = os.path.relpath(trunk_dir) |
| 5675 |
cpplint._root = "cpplint" |
| 5676 |
assert cpplint.GetHeaderGuardCPPVariable(file_path) == "CPPLINT_TEST_HEADER_H_" |
| 5677 |
|
| 5678 |
finally: |
| 5679 |
os.chdir(current_directory) |
| 5680 |
shutil.rmtree(temp_directory) |
| 5681 |
shutil.rmtree(temp_directory2) |
| 5682 |
cpplint._repository = None |
| 5683 |
cpplint._root = None |
| 5684 |
|
| 5685 |
def testBuildInclude(self): |
| 5686 |
|
| 5687 |
self.TestLint( |
| 5688 |
'#include "foo.h"', |
| 5689 |
"Include the directory when naming header files [build/include_subdir] [4]", |
| 5690 |
) |
| 5691 |
self.TestLint( |
| 5692 |
'#include "bar.hh"', |
| 5693 |
"Include the directory when naming header files [build/include_subdir] [4]", |
| 5694 |
) |
| 5695 |
self.TestLint('#include "baz.aa"', "") |
| 5696 |
self.TestLint('#include "dir/foo.h"', "") |
| 5697 |
self.TestLint('#include "Python.h"', "") |
| 5698 |
self.TestLint('#include "lua.h"', "") |
| 5699 |
|
| 5700 |
def testHppInclude(self): |
| 5701 |
code = "\n".join(["#include <vector>", "#include <boost/any.hpp>"]) |
| 5702 |
self.TestLanguageRulesCheck("foo.h", code, "") |
| 5703 |
|
| 5704 |
def testBuildPrintfFormat(self): |
| 5705 |
error_collector = ErrorCollector(self.assertTrue) |
| 5706 |
cpplint.ProcessFileData( |
| 5707 |
"foo.cc", |
| 5708 |
"cc", |
| 5709 |
[ |
| 5710 |
r'printf("\%%d", value);', |
| 5711 |
r'snprintf(buffer, sizeof(buffer), "\[%d", value);', |
| 5712 |
r'fprintf(file, "\(%d", value);', |
| 5713 |
r'vsnprintf(buffer, sizeof(buffer), "\\\{%d", ap);', |
| 5714 |
], |
| 5715 |
error_collector, |
| 5716 |
) |
| 5717 |
assert ( |
| 5718 |
error_collector.Results().count( |
| 5719 |
"%, [, (, and { are undefined character escapes. Unescape them. " |
| 5720 |
"[build/printf_format] [3]" |
| 5721 |
) |
| 5722 |
== 4 |
| 5723 |
) |
| 5724 |
|
| 5725 |
error_collector = ErrorCollector(self.assertTrue) |
| 5726 |
cpplint.ProcessFileData( |
| 5727 |
"foo.cc", |
| 5728 |
"cc", |
| 5729 |
[ |
| 5730 |
"// Copyright 2014 Your Company.", |
| 5731 |
"#include <cstdio>", |
| 5732 |
r'printf("\\%%%d", value);', |
| 5733 |
r'printf(R"(\[)");', |
| 5734 |
r'printf(R"(\[%s)", R"(\])");', |
| 5735 |
"", |
| 5736 |
], |
| 5737 |
error_collector, |
| 5738 |
) |
| 5739 |
assert error_collector.Results() == "" |
| 5740 |
|
| 5741 |
def testRuntimePrintfFormat(self): |
| 5742 |
self.TestLint( |
| 5743 |
r'fprintf(file, "%q", value);', |
| 5744 |
"%q in format strings is deprecated. Use %ll instead. [runtime/printf_format] [3]", |
| 5745 |
) |
| 5746 |
|
| 5747 |
self.TestLint( |
| 5748 |
r'aprintf(file, "The number is %12q", value);', |
| 5749 |
"%q in format strings is deprecated. Use %ll instead. [runtime/printf_format] [3]", |
| 5750 |
) |
| 5751 |
|
| 5752 |
self.TestLint( |
| 5753 |
r'printf(file, "The number is" "%-12q", value);', |
| 5754 |
"%q in format strings is deprecated. Use %ll instead. [runtime/printf_format] [3]", |
| 5755 |
) |
| 5756 |
|
| 5757 |
self.TestLint( |
| 5758 |
r'printf(file, "The number is" "%+12q", value);', |
| 5759 |
"%q in format strings is deprecated. Use %ll instead. [runtime/printf_format] [3]", |
| 5760 |
) |
| 5761 |
|
| 5762 |
self.TestLint( |
| 5763 |
r'printf(file, "The number is" "% 12q", value);', |
| 5764 |
"%q in format strings is deprecated. Use %ll instead. [runtime/printf_format] [3]", |
| 5765 |
) |
| 5766 |
|
| 5767 |
self.TestLint( |
| 5768 |
r'snprintf(file, "Never mix %d and %1$d parameters!", value);', |
| 5769 |
"%N$ formats are unconventional. Try rewriting to avoid them." |
| 5770 |
" [runtime/printf_format] [2]", |
| 5771 |
) |
| 5772 |
|
| 5773 |
def TestLintLogCodeOnError(self, code, expected_message): |
| 5774 |
|
| 5775 |
assert (result := self.PerformSingleLineLint(code)) == expected_message, ( |
| 5776 |
f'For code: "{code}"\nGot: "{result}"\nExpected: "{expected_message}"' |
| 5777 |
) |
| 5778 |
|
| 5779 |
def testBuildStorageClass(self): |
| 5780 |
qualifiers = [None, "const", "volatile"] |
| 5781 |
signs = [None, "signed", "unsigned"] |
| 5782 |
types = [ |
| 5783 |
"void", |
| 5784 |
"char", |
| 5785 |
"int", |
| 5786 |
"float", |
| 5787 |
"double", |
| 5788 |
"schar", |
| 5789 |
"int8_t", |
| 5790 |
"uint8_t", |
| 5791 |
"int16_t", |
| 5792 |
"uint16_t", |
| 5793 |
"int32_t", |
| 5794 |
"uint32_t", |
| 5795 |
"int64_t", |
| 5796 |
"uint64_t", |
| 5797 |
] |
| 5798 |
storage_classes = ["extern", "register", "static", "typedef"] |
| 5799 |
|
| 5800 |
build_storage_class_error_message = ( |
| 5801 |
"Storage-class specifier (static, extern, typedef, etc) should be " |
| 5802 |
"at the beginning of the declaration. [build/storage_class] [5]" |
| 5803 |
) |
| 5804 |
|
| 5805 |
|
| 5806 |
self.TestLint("const int static foo = 5;", build_storage_class_error_message) |
| 5807 |
|
| 5808 |
self.TestLint("char static foo;", build_storage_class_error_message) |
| 5809 |
|
| 5810 |
self.TestLint("double const static foo = 2.0;", build_storage_class_error_message) |
| 5811 |
|
| 5812 |
self.TestLint("uint64_t typedef unsigned_long_long;", build_storage_class_error_message) |
| 5813 |
|
| 5814 |
self.TestLint("int register foo = 0;", build_storage_class_error_message) |
| 5815 |
|
| 5816 |
|
| 5817 |
|
| 5818 |
|
| 5819 |
|
| 5820 |
random.seed(25) |
| 5821 |
for _i in range(10): |
| 5822 |
|
| 5823 |
other_decl_specs = [ |
| 5824 |
random.choice(qualifiers), |
| 5825 |
random.choice(signs), |
| 5826 |
random.choice(types), |
| 5827 |
] |
| 5828 |
|
| 5829 |
other_decl_specs = [x for x in other_decl_specs if x is not None] |
| 5830 |
|
| 5831 |
|
| 5832 |
random.shuffle(other_decl_specs) |
| 5833 |
|
| 5834 |
|
| 5835 |
storage_class = random.choice(storage_classes) |
| 5836 |
insertion_point = random.randint(1, len(other_decl_specs)) |
| 5837 |
decl_specs = ( |
| 5838 |
other_decl_specs[0:insertion_point] |
| 5839 |
+ [storage_class] |
| 5840 |
+ other_decl_specs[insertion_point:] |
| 5841 |
) |
| 5842 |
|
| 5843 |
self.TestLintLogCodeOnError( |
| 5844 |
" ".join(decl_specs) + ";", build_storage_class_error_message |
| 5845 |
) |
| 5846 |
|
| 5847 |
|
| 5848 |
self.TestLintLogCodeOnError(storage_class + " " + " ".join(other_decl_specs), "") |
| 5849 |
|
| 5850 |
def testLegalCopyright(self): |
| 5851 |
legal_copyright_message = ( |
| 5852 |
"No copyright message found. " |
| 5853 |
'You should have a line: "Copyright [year] <Copyright Owner>"' |
| 5854 |
" [legal/copyright] [5]" |
| 5855 |
) |
| 5856 |
|
| 5857 |
copyright_line = "// Copyright 2014 Google Inc. All Rights Reserved." |
| 5858 |
|
| 5859 |
file_path = "mydir/googleclient/foo.cc" |
| 5860 |
|
| 5861 |
|
| 5862 |
error_collector = ErrorCollector(self.assertTrue) |
| 5863 |
cpplint.ProcessFileData(file_path, "cc", [], error_collector) |
| 5864 |
assert error_collector.ResultList().count(legal_copyright_message) == 1 |
| 5865 |
|
| 5866 |
error_collector = ErrorCollector(self.assertTrue) |
| 5867 |
cpplint.ProcessFileData( |
| 5868 |
file_path, "cc", ["" for unused_i in range(10)] + [copyright_line], error_collector |
| 5869 |
) |
| 5870 |
assert error_collector.ResultList().count(legal_copyright_message) == 1 |
| 5871 |
|
| 5872 |
|
| 5873 |
error_collector = ErrorCollector(self.assertTrue) |
| 5874 |
cpplint.ProcessFileData(file_path, "cc", [copyright_line], error_collector) |
| 5875 |
for line in error_collector.ResultList(): |
| 5876 |
assert "legal/copyright" not in line |
| 5877 |
|
| 5878 |
error_collector = ErrorCollector(self.assertTrue) |
| 5879 |
cpplint.ProcessFileData( |
| 5880 |
file_path, "cc", ["" for unused_i in range(9)] + [copyright_line], error_collector |
| 5881 |
) |
| 5882 |
for line in error_collector.ResultList(): |
| 5883 |
assert "legal/copyright" not in line |
| 5884 |
|
| 5885 |
def testInvalidIncrement(self): |
| 5886 |
self.TestLint( |
| 5887 |
"*count++;", |
| 5888 |
"Changing pointer instead of value (or unused value of " |
| 5889 |
"operator*). [runtime/invalid_increment] [5]", |
| 5890 |
) |
| 5891 |
|
| 5892 |
def testSnprintfSize(self): |
| 5893 |
self.TestLint("vsnprintf(NULL, 0, format)", "") |
| 5894 |
self.TestLint( |
| 5895 |
"snprintf(fisk, 1, format)", |
| 5896 |
"If you can, use sizeof(fisk) instead of 1 as the 2nd arg " |
| 5897 |
"to snprintf. [runtime/printf] [3]", |
| 5898 |
) |
| 5899 |
|
| 5900 |
|
| 5901 |
class TestCxx(CpplintTestBase): |
| 5902 |
def Helper(self, package, extension, lines, count): |
| 5903 |
filename = package + "/foo." + extension |
| 5904 |
lines = lines[:] |
| 5905 |
|
| 5906 |
|
| 5907 |
if extension.startswith("h"): |
| 5908 |
guard = filename.upper().replace("/", "_").replace(".", "_") + "_" |
| 5909 |
lines.insert(0, "#ifndef " + guard) |
| 5910 |
lines.insert(1, "#define " + guard) |
| 5911 |
lines.append("#endif // " + guard) |
| 5912 |
|
| 5913 |
|
| 5914 |
lines.append("") |
| 5915 |
|
| 5916 |
|
| 5917 |
collector = ErrorCollector(self.assertTrue) |
| 5918 |
cpplint.ProcessFileData(filename, extension, lines, collector) |
| 5919 |
error_list = collector.ResultList() |
| 5920 |
assert count == len(error_list), error_list |
| 5921 |
|
| 5922 |
def TestCxxFeature(self, code, expected_error): |
| 5923 |
lines = code.split("\n") |
| 5924 |
collector = ErrorCollector(self.assertTrue) |
| 5925 |
cpplint.RemoveMultiLineComments("foo.h", lines, collector) |
| 5926 |
clean_lines = cpplint.CleansedLines(lines) |
| 5927 |
cpplint.FlagCxxHeaders("foo.cc", clean_lines, 0, collector) |
| 5928 |
assert expected_error == collector.Results() |
| 5929 |
|
| 5930 |
def testBlockedHeaders(self): |
| 5931 |
self.TestCxxFeature( |
| 5932 |
"#include <ratio>", "<ratio> is an unapproved C++11 header. [build/c++11] [5]" |
| 5933 |
) |
| 5934 |
self.TestCxxFeature( |
| 5935 |
"#include <fenv.h>", "<fenv.h> is an unapproved C++11 header. [build/c++11] [5]" |
| 5936 |
) |
| 5937 |
self.TestCxxFeature( |
| 5938 |
"#include <filesystem>", |
| 5939 |
"<filesystem> is an unapproved C++17 header. [build/c++17] [5]", |
| 5940 |
) |
| 5941 |
|
| 5942 |
def testExplicitMakePair(self): |
| 5943 |
self.TestLint("make_pair", "") |
| 5944 |
self.TestLint("make_pair(42, 42)", "") |
| 5945 |
self.TestLint( |
| 5946 |
"make_pair<", |
| 5947 |
"For C++11-compatibility, omit template arguments from" |
| 5948 |
" make_pair OR use pair directly OR if appropriate," |
| 5949 |
" construct a pair directly" |
| 5950 |
" [build/explicit_make_pair] [4]", |
| 5951 |
) |
| 5952 |
self.TestLint( |
| 5953 |
"make_pair <", |
| 5954 |
"For C++11-compatibility, omit template arguments from" |
| 5955 |
" make_pair OR use pair directly OR if appropriate," |
| 5956 |
" construct a pair directly" |
| 5957 |
" [build/explicit_make_pair] [4]", |
| 5958 |
) |
| 5959 |
self.TestLint("my_make_pair<int, int>", "") |
| 5960 |
|
| 5961 |
|
| 5962 |
class TestCleansedLines: |
| 5963 |
def testInit(self): |
| 5964 |
lines = [ |
| 5965 |
"Line 1", |
| 5966 |
"Line 2", |
| 5967 |
"Line 3 // Comment test", |
| 5968 |
"Line 4 /* Comment test */", |
| 5969 |
'Line 5 "foo"', |
| 5970 |
] |
| 5971 |
|
| 5972 |
clean_lines = cpplint.CleansedLines(lines) |
| 5973 |
assert lines == clean_lines.raw_lines |
| 5974 |
assert clean_lines.NumLines() == 5 |
| 5975 |
|
| 5976 |
assert clean_lines.lines == ["Line 1", "Line 2", "Line 3", "Line 4", 'Line 5 "foo"'] |
| 5977 |
|
| 5978 |
assert clean_lines.elided == ["Line 1", "Line 2", "Line 3", "Line 4", 'Line 5 ""'] |
| 5979 |
|
| 5980 |
def testInitEmpty(self): |
| 5981 |
clean_lines = cpplint.CleansedLines([]) |
| 5982 |
assert clean_lines.raw_lines == [] |
| 5983 |
assert clean_lines.NumLines() == 0 |
| 5984 |
|
| 5985 |
def testCollapseStrings(self): |
| 5986 |
collapse = cpplint.CleansedLines._CollapseStrings |
| 5987 |
assert collapse('""') == '""' |
| 5988 |
assert collapse('"""') == '"""' |
| 5989 |
assert collapse('"xyz"') == '""' |
| 5990 |
assert collapse('"\\""') == '""' |
| 5991 |
assert collapse('"\'"') == '""' |
| 5992 |
assert collapse('""') == '""' |
| 5993 |
assert collapse('"\\\\"') == '""' |
| 5994 |
assert collapse('"\\\\\\"') == '"' |
| 5995 |
assert collapse('"\\\\\\\\"') == '""' |
| 5996 |
|
| 5997 |
assert collapse("''") == "''" |
| 5998 |
assert collapse("'a'") == "''" |
| 5999 |
assert collapse("'\\''") == "''" |
| 6000 |
assert collapse("'\\'") == "'" |
| 6001 |
assert collapse("\\012") == "" |
| 6002 |
assert collapse("\\xfF0") == "" |
| 6003 |
assert collapse("\\n") == "" |
| 6004 |
assert collapse("\\#") == r"\#" |
| 6005 |
|
| 6006 |
assert collapse('"\'" + "\'"') == '"" + ""' |
| 6007 |
assert collapse("'\"', '\"'") == "'', ''" |
| 6008 |
assert collapse("\"a'b\"[0b1'0]") == '""[0b10]' |
| 6009 |
|
| 6010 |
assert collapse("4'2") == "42" |
| 6011 |
assert collapse("0b0'1'0'1") == "0b0101" |
| 6012 |
assert collapse("1'048'576") == "1048576" |
| 6013 |
assert collapse("0X10'0000") == "0X100000" |
| 6014 |
assert collapse("0'004'000'000") == "0004000000" |
| 6015 |
assert collapse("1.602'176'565e-19") == "1.602176565e-19" |
| 6016 |
assert collapse("'i' + 0xf'f'f'f") == "'' + 0xffff" |
| 6017 |
assert collapse("sizeof'x' == 1") == "sizeof'' == 1" |
| 6018 |
assert collapse("0x.0'3p1'0'0") == "0x.03p100" |
| 6019 |
assert collapse("1'23.4'5") == "123.45" |
| 6020 |
|
| 6021 |
assert ( |
| 6022 |
collapse('StringReplace(body, "\\\\", "\\\\\\\\");') == 'StringReplace(body, "", "");' |
| 6023 |
) |
| 6024 |
assert collapse('\'"\' "foo"') == "'' \"\"" |
| 6025 |
|
| 6026 |
|
| 6027 |
class TestOrderOfIncludes(CpplintTestBase): |
| 6028 |
@pytest.fixture(autouse=True) |
| 6029 |
def setUp(self): |
| 6030 |
CpplintTestBase.setUp(self) |
| 6031 |
self.include_state = cpplint._IncludeState() |
| 6032 |
os.path.abspath = lambda value: value |
| 6033 |
|
| 6034 |
def testCheckNextIncludeOrder_OtherThenCpp(self): |
| 6035 |
assert self.include_state.CheckNextIncludeOrder(cpplint._OTHER_HEADER) == "" |
| 6036 |
assert ( |
| 6037 |
self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) |
| 6038 |
== "Found C++ system header after other header" |
| 6039 |
) |
| 6040 |
|
| 6041 |
def testCheckNextIncludeOrder_CppThenC(self): |
| 6042 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6043 |
assert ( |
| 6044 |
self.include_state.CheckNextIncludeOrder(cpplint._C_SYS_HEADER) |
| 6045 |
== "Found C system header after C++ system header" |
| 6046 |
) |
| 6047 |
|
| 6048 |
def testCheckNextIncludeOrder_OtherSysThenC(self): |
| 6049 |
assert self.include_state.CheckNextIncludeOrder(cpplint._OTHER_SYS_HEADER) == "" |
| 6050 |
assert ( |
| 6051 |
self.include_state.CheckNextIncludeOrder(cpplint._C_SYS_HEADER) |
| 6052 |
== "Found C system header after other system header" |
| 6053 |
) |
| 6054 |
|
| 6055 |
def testCheckNextIncludeOrder_OtherSysThenCpp(self): |
| 6056 |
assert self.include_state.CheckNextIncludeOrder(cpplint._OTHER_SYS_HEADER) == "" |
| 6057 |
assert ( |
| 6058 |
self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) |
| 6059 |
== "Found C++ system header after other system header" |
| 6060 |
) |
| 6061 |
|
| 6062 |
def testCheckNextIncludeOrder_LikelyThenCpp(self): |
| 6063 |
assert self.include_state.CheckNextIncludeOrder(cpplint._LIKELY_MY_HEADER) == "" |
| 6064 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6065 |
|
| 6066 |
def testCheckNextIncludeOrder_PossibleThenCpp(self): |
| 6067 |
assert self.include_state.CheckNextIncludeOrder(cpplint._POSSIBLE_MY_HEADER) == "" |
| 6068 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6069 |
|
| 6070 |
def testCheckNextIncludeOrder_CppThenLikely(self): |
| 6071 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6072 |
|
| 6073 |
assert self.include_state.CheckNextIncludeOrder(cpplint._LIKELY_MY_HEADER) == "" |
| 6074 |
|
| 6075 |
def testCheckNextIncludeOrder_CppThenPossible(self): |
| 6076 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6077 |
assert self.include_state.CheckNextIncludeOrder(cpplint._POSSIBLE_MY_HEADER) == "" |
| 6078 |
|
| 6079 |
def testCheckNextIncludeOrder_CppThenOtherSys(self): |
| 6080 |
assert self.include_state.CheckNextIncludeOrder(cpplint._CPP_SYS_HEADER) == "" |
| 6081 |
assert self.include_state.CheckNextIncludeOrder(cpplint._OTHER_SYS_HEADER) == "" |
| 6082 |
|
| 6083 |
def testCheckNextIncludeOrder_OtherSysThenPossible(self): |
| 6084 |
assert self.include_state.CheckNextIncludeOrder(cpplint._OTHER_SYS_HEADER) == "" |
| 6085 |
assert self.include_state.CheckNextIncludeOrder(cpplint._POSSIBLE_MY_HEADER) == "" |
| 6086 |
|
| 6087 |
def testClassifyInclude(self): |
| 6088 |
file_info = cpplint.FileInfo |
| 6089 |
classify_include = cpplint._ClassifyInclude |
| 6090 |
assert classify_include(file_info("foo/foo.cc"), "stdio.h", True) == cpplint._C_SYS_HEADER |
| 6091 |
assert ( |
| 6092 |
classify_include(file_info("foo/foo.cc"), "sys/time.h", True) == cpplint._C_SYS_HEADER |
| 6093 |
) |
| 6094 |
assert ( |
| 6095 |
classify_include(file_info("foo/foo.cc"), "netipx/ipx.h", True) == cpplint._C_SYS_HEADER |
| 6096 |
) |
| 6097 |
assert ( |
| 6098 |
classify_include(file_info("foo/foo.cc"), "arpa/ftp.h", True) == cpplint._C_SYS_HEADER |
| 6099 |
) |
| 6100 |
assert classify_include(file_info("foo/foo.cc"), "string", True) == cpplint._CPP_SYS_HEADER |
| 6101 |
assert ( |
| 6102 |
classify_include(file_info("foo/foo.cc"), "typeinfo", True) == cpplint._CPP_SYS_HEADER |
| 6103 |
) |
| 6104 |
assert classify_include(file_info("foo/foo.cc"), "foo/foo.h", True) == cpplint._C_SYS_HEADER |
| 6105 |
assert ( |
| 6106 |
classify_include(file_info("foo/foo.cc"), "foo/foo.h", True, "standardcfirst") |
| 6107 |
== cpplint._OTHER_SYS_HEADER |
| 6108 |
) |
| 6109 |
assert classify_include(file_info("foo/foo.cc"), "string", False) == cpplint._OTHER_HEADER |
| 6110 |
assert ( |
| 6111 |
classify_include(file_info("foo/foo.cc"), "boost/any.hpp", True) |
| 6112 |
== cpplint._OTHER_HEADER |
| 6113 |
) |
| 6114 |
assert ( |
| 6115 |
classify_include(file_info("foo/foo.hxx"), "boost/any.hpp", True) |
| 6116 |
== cpplint._OTHER_HEADER |
| 6117 |
) |
| 6118 |
assert ( |
| 6119 |
classify_include(file_info("foo/foo.h++"), "boost/any.hpp", True) |
| 6120 |
== cpplint._OTHER_HEADER |
| 6121 |
) |
| 6122 |
assert ( |
| 6123 |
classify_include(file_info("foo/foo.cc"), "foo/foo-inl.h", False) |
| 6124 |
== cpplint._LIKELY_MY_HEADER |
| 6125 |
) |
| 6126 |
assert ( |
| 6127 |
classify_include(file_info("foo/internal/foo.cc"), "foo/public/foo.h", False) |
| 6128 |
== cpplint._LIKELY_MY_HEADER |
| 6129 |
) |
| 6130 |
assert ( |
| 6131 |
classify_include(file_info("foo/internal/foo.cc"), "foo/other/public/foo.h", False) |
| 6132 |
== cpplint._POSSIBLE_MY_HEADER |
| 6133 |
) |
| 6134 |
assert ( |
| 6135 |
classify_include(file_info("foo/internal/foo.cc"), "foo/other/public/foop.h", False) |
| 6136 |
== cpplint._OTHER_HEADER |
| 6137 |
) |
| 6138 |
|
| 6139 |
def testTryDropCommonSuffixes(self): |
| 6140 |
cpplint._hpp_headers = set() |
| 6141 |
cpplint._valid_extensions = set() |
| 6142 |
assert cpplint._DropCommonSuffixes("foo/foo-inl.h") == "foo/foo" |
| 6143 |
assert cpplint._DropCommonSuffixes("foo/foo-inl.hxx") == "foo/foo" |
| 6144 |
assert cpplint._DropCommonSuffixes("foo/foo-inl.h++") == "foo/foo" |
| 6145 |
assert cpplint._DropCommonSuffixes("foo/foo-inl.hpp") == "foo/foo" |
| 6146 |
assert cpplint._DropCommonSuffixes("foo/bar/foo_inl.h") == "foo/bar/foo" |
| 6147 |
assert cpplint._DropCommonSuffixes("foo/foo.cc") == "foo/foo" |
| 6148 |
assert cpplint._DropCommonSuffixes("foo/foo.cxx") == "foo/foo" |
| 6149 |
assert cpplint._DropCommonSuffixes("foo/foo.c") == "foo/foo" |
| 6150 |
assert cpplint._DropCommonSuffixes("foo/foo_unusualinternal.h") == "foo/foo_unusualinternal" |
| 6151 |
assert ( |
| 6152 |
cpplint._DropCommonSuffixes("foo/foo_unusualinternal.hpp") == "foo/foo_unusualinternal" |
| 6153 |
) |
| 6154 |
assert cpplint._DropCommonSuffixes("_test.cc") == "" |
| 6155 |
assert cpplint._DropCommonSuffixes("_test.c") == "" |
| 6156 |
assert cpplint._DropCommonSuffixes("_test.c++") == "" |
| 6157 |
assert cpplint._DropCommonSuffixes("test.c") == "test" |
| 6158 |
assert cpplint._DropCommonSuffixes("test.cc") == "test" |
| 6159 |
assert cpplint._DropCommonSuffixes("test.c++") == "test" |
| 6160 |
|
| 6161 |
def testRegression(self): |
| 6162 |
def Format(includes): |
| 6163 |
include_list = [] |
| 6164 |
for item in includes: |
| 6165 |
if item.startswith(('"', "<")): |
| 6166 |
include_list.append("#include %s\n" % item) |
| 6167 |
else: |
| 6168 |
include_list.append(item + "\n") |
| 6169 |
return "".join(include_list) |
| 6170 |
|
| 6171 |
|
| 6172 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"foo/foo.h"']), "") |
| 6173 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(["<stdio.h>"]), "") |
| 6174 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(["<string>"]), "") |
| 6175 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"foo/foo-inl.h"']), "") |
| 6176 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"bar/bar-inl.h"']), "") |
| 6177 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"bar/bar.h"']), "") |
| 6178 |
|
| 6179 |
|
| 6180 |
self.TestLanguageRulesCheck( |
| 6181 |
"foo/foo.cc", |
| 6182 |
Format( |
| 6183 |
[ |
| 6184 |
'"foo/foo.h"', |
| 6185 |
'"foo/foo-inl.h"', |
| 6186 |
"<stdio.h>", |
| 6187 |
"<string>", |
| 6188 |
"<unordered_map>", |
| 6189 |
'"bar/bar-inl.h"', |
| 6190 |
'"bar/bar.h"', |
| 6191 |
] |
| 6192 |
), |
| 6193 |
"", |
| 6194 |
) |
| 6195 |
|
| 6196 |
|
| 6197 |
self.TestLanguageRulesCheck( |
| 6198 |
"foo/foo.cc", |
| 6199 |
Format(["<string>", "<stdio.h>"]), |
| 6200 |
"Found C system header after C++ system header." |
| 6201 |
" Should be: foo.h, c system, c++ system, other." |
| 6202 |
" [build/include_order] [4]", |
| 6203 |
) |
| 6204 |
self.TestLanguageRulesCheck( |
| 6205 |
"foo/foo.cc", Format(['"foo/bar-inl.h"', '"foo/foo-inl.h"']), "" |
| 6206 |
) |
| 6207 |
self.TestLanguageRulesCheck( |
| 6208 |
"foo/foo.cc", |
| 6209 |
Format( |
| 6210 |
[ |
| 6211 |
'"foo/e.h"', |
| 6212 |
'"foo/b.h"', |
| 6213 |
'"foo/c.h"', |
| 6214 |
'"foo/d.h"', |
| 6215 |
'"foo/a.h"', |
| 6216 |
] |
| 6217 |
), |
| 6218 |
[ |
| 6219 |
'Include "foo/b.h" not in alphabetical order [build/include_alpha] [4]', |
| 6220 |
'Include "foo/a.h" not in alphabetical order [build/include_alpha] [4]', |
| 6221 |
], |
| 6222 |
) |
| 6223 |
|
| 6224 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"foo/foo-inl.h"', "<string>"]), "") |
| 6225 |
self.TestLanguageRulesCheck("foo/foo.cc", Format(['"foo/bar.h"', '"foo/bar-inl.h"']), "") |
| 6226 |
|
| 6227 |
self.TestLanguageRulesCheck( |
| 6228 |
"foo/internal/foo.cc", Format(['"foo/public/foo.h"', "<string>"]), "" |
| 6229 |
) |
| 6230 |
|
| 6231 |
|
| 6232 |
self.TestLanguageRulesCheck( |
| 6233 |
"foo/internal/foo.cc", Format(['"foo/other/public/foo.h"', "<string>"]), "" |
| 6234 |
) |
| 6235 |
self.TestLanguageRulesCheck( |
| 6236 |
"foo/foo.cc", |
| 6237 |
Format(['"foo/foo.h"', "<string>", '"base/google.h"', '"base/flags.h"']), |
| 6238 |
'Include "base/flags.h" not in alphabetical order [build/include_alpha] [4]', |
| 6239 |
) |
| 6240 |
|
| 6241 |
|
| 6242 |
self.TestLanguageRulesCheck( |
| 6243 |
"foo/foo.cc", |
| 6244 |
Format(['"foo/foo-inl.h"', '"foo/foo.h"', '"base/google.h"', '"base/google-inl.h"']), |
| 6245 |
"", |
| 6246 |
) |
| 6247 |
|
| 6248 |
self.TestLanguageRulesCheck( |
| 6249 |
"a/a.cc", |
| 6250 |
Format( |
| 6251 |
['"a/a.h"', "<string>", '"base/google.h"', "", '"b/c.h"', "", "MACRO", '"a/b.h"'] |
| 6252 |
), |
| 6253 |
"", |
| 6254 |
) |
| 6255 |
self.TestLanguageRulesCheck( |
| 6256 |
"a/a.cc", |
| 6257 |
Format(['"a/a.h"', "<string>", '"base/google.h"', '"a/b.h"']), |
| 6258 |
'Include "a/b.h" not in alphabetical order [build/include_alpha] [4]', |
| 6259 |
) |
| 6260 |
|
| 6261 |
|
| 6262 |
self.TestLanguageRulesCheck( |
| 6263 |
"a/a.cc", |
| 6264 |
"".join( |
| 6265 |
[ |
| 6266 |
"#include <string.h>\n", |
| 6267 |
'#include "base/port.h"\n', |
| 6268 |
"#include <initializer_list>\n", |
| 6269 |
] |
| 6270 |
), |
| 6271 |
( |
| 6272 |
"Found C++ system header after other header. " |
| 6273 |
"Should be: a.h, c system, c++ system, other. " |
| 6274 |
"[build/include_order] [4]" |
| 6275 |
), |
| 6276 |
) |
| 6277 |
self.TestLanguageRulesCheck( |
| 6278 |
"a/a.cc", |
| 6279 |
"".join( |
| 6280 |
[ |
| 6281 |
"#include <string.h>\n", |
| 6282 |
'#include "base/port.h"\n', |
| 6283 |
"#ifdef LANG_CXX11\n", |
| 6284 |
"#include <initializer_list>\n", |
| 6285 |
"#endif // LANG_CXX11\n", |
| 6286 |
] |
| 6287 |
), |
| 6288 |
"", |
| 6289 |
) |
| 6290 |
self.TestLanguageRulesCheck( |
| 6291 |
"a/a.cc", |
| 6292 |
"".join( |
| 6293 |
[ |
| 6294 |
"#include <string.h>\n", |
| 6295 |
"#ifdef LANG_CXX11\n", |
| 6296 |
'#include "base/port.h"\n', |
| 6297 |
"#include <initializer_list>\n", |
| 6298 |
"#endif // LANG_CXX11\n", |
| 6299 |
] |
| 6300 |
), |
| 6301 |
( |
| 6302 |
"Found C++ system header after other header. " |
| 6303 |
"Should be: a.h, c system, c++ system, other. " |
| 6304 |
"[build/include_order] [4]" |
| 6305 |
), |
| 6306 |
) |
| 6307 |
|
| 6308 |
|
| 6309 |
self.TestLanguageRulesCheck( |
| 6310 |
"foo/foo.cc", Format(["<string>", '"Python.h"', "<vector>"]), "" |
| 6311 |
) |
| 6312 |
|
| 6313 |
|
| 6314 |
class TestCheckForFunctionLengths(CpplintTestBase): |
| 6315 |
@pytest.fixture(autouse=True) |
| 6316 |
def setUp(self): |
| 6317 |
|
| 6318 |
CpplintTestBase.setUp(self) |
| 6319 |
self.old_normal_trigger = cpplint._FunctionState._NORMAL_TRIGGER |
| 6320 |
self.old_test_trigger = cpplint._FunctionState._TEST_TRIGGER |
| 6321 |
|
| 6322 |
cpplint._FunctionState._NORMAL_TRIGGER = 10 |
| 6323 |
cpplint._FunctionState._TEST_TRIGGER = 25 |
| 6324 |
|
| 6325 |
def tearDown(self): |
| 6326 |
cpplint._FunctionState._NORMAL_TRIGGER = self.old_normal_trigger |
| 6327 |
cpplint._FunctionState._TEST_TRIGGER = self.old_test_trigger |
| 6328 |
|
| 6329 |
def TestFunctionLengthsCheck(self, code, expected_message): |
| 6330 |
"""Check warnings for long function bodies are as expected. |
| 6331 |
|
| 6332 |
Args: |
| 6333 |
code: C++ source code expected to generate a warning message. |
| 6334 |
expected_message: Message expected to be generated by the C++ code. |
| 6335 |
""" |
| 6336 |
assert expected_message == self.PerformFunctionLengthsCheck(code) |
| 6337 |
|
| 6338 |
def TriggerLines(self, error_level): |
| 6339 |
"""Return number of lines needed to trigger a function length warning. |
| 6340 |
|
| 6341 |
Args: |
| 6342 |
error_level: --v setting for cpplint. |
| 6343 |
|
| 6344 |
Returns: |
| 6345 |
Number of lines needed to trigger a function length warning. |
| 6346 |
""" |
| 6347 |
return cpplint._FunctionState._NORMAL_TRIGGER * 2**error_level |
| 6348 |
|
| 6349 |
def TestLines(self, error_level): |
| 6350 |
"""Return number of lines needed to trigger a test function length warning. |
| 6351 |
|
| 6352 |
Args: |
| 6353 |
error_level: --v setting for cpplint. |
| 6354 |
|
| 6355 |
Returns: |
| 6356 |
Number of lines needed to trigger a test function length warning. |
| 6357 |
""" |
| 6358 |
return cpplint._FunctionState._TEST_TRIGGER * 2**error_level |
| 6359 |
|
| 6360 |
def TestFunctionLengthCheckDefinition(self, lines, error_level): |
| 6361 |
"""Generate long function definition and check warnings are as expected. |
| 6362 |
|
| 6363 |
Args: |
| 6364 |
lines: Number of lines to generate. |
| 6365 |
error_level: --v setting for cpplint. |
| 6366 |
""" |
| 6367 |
trigger_level = self.TriggerLines(cpplint._VerboseLevel()) |
| 6368 |
self.TestFunctionLengthsCheck( |
| 6369 |
"void test(int x)" + self.FunctionBody(lines), |
| 6370 |
( |
| 6371 |
"Small and focused functions are preferred: " |
| 6372 |
"test() has %d non-comment lines " |
| 6373 |
"(error triggered by exceeding %d lines)." |
| 6374 |
" [readability/fn_size] [%d]" % (lines, trigger_level, error_level) |
| 6375 |
), |
| 6376 |
) |
| 6377 |
|
| 6378 |
def TestFunctionLengthCheckDefinitionOK(self, lines): |
| 6379 |
"""Generate shorter function definition and check no warning is produced. |
| 6380 |
|
| 6381 |
Args: |
| 6382 |
lines: Number of lines to generate. |
| 6383 |
""" |
| 6384 |
self.TestFunctionLengthsCheck("void test(int x)" + self.FunctionBody(lines), "") |
| 6385 |
|
| 6386 |
def TestFunctionLengthCheckAtErrorLevel(self, error_level): |
| 6387 |
"""Generate and check function at the trigger level for --v setting. |
| 6388 |
|
| 6389 |
Args: |
| 6390 |
error_level: --v setting for cpplint. |
| 6391 |
""" |
| 6392 |
self.TestFunctionLengthCheckDefinition(self.TriggerLines(error_level), error_level) |
| 6393 |
|
| 6394 |
def TestFunctionLengthCheckBelowErrorLevel(self, error_level): |
| 6395 |
"""Generate and check function just below the trigger level for --v setting. |
| 6396 |
|
| 6397 |
Args: |
| 6398 |
error_level: --v setting for cpplint. |
| 6399 |
""" |
| 6400 |
self.TestFunctionLengthCheckDefinition(self.TriggerLines(error_level) - 1, error_level - 1) |
| 6401 |
|
| 6402 |
def TestFunctionLengthCheckAboveErrorLevel(self, error_level): |
| 6403 |
"""Generate and check function just above the trigger level for --v setting. |
| 6404 |
|
| 6405 |
Args: |
| 6406 |
error_level: --v setting for cpplint. |
| 6407 |
""" |
| 6408 |
self.TestFunctionLengthCheckDefinition(self.TriggerLines(error_level) + 1, error_level) |
| 6409 |
|
| 6410 |
def FunctionBody(self, number_of_lines): |
| 6411 |
return " {\n" + " this_is_just_a_test();\n" * number_of_lines + "}" |
| 6412 |
|
| 6413 |
def FunctionBodyWithBlankLines(self, number_of_lines): |
| 6414 |
return " {\n" + " this_is_just_a_test();\n\n" * number_of_lines + "}" |
| 6415 |
|
| 6416 |
def FunctionBodyWithNoLints(self, number_of_lines): |
| 6417 |
return " {\n" + " this_is_just_a_test(); // NOLINT\n" * number_of_lines + "}" |
| 6418 |
|
| 6419 |
|
| 6420 |
def testFunctionLengthCheckDeclaration(self): |
| 6421 |
self.TestFunctionLengthsCheck( |
| 6422 |
"void test();", |
| 6423 |
"", |
| 6424 |
) |
| 6425 |
|
| 6426 |
def testFunctionLengthCheckDeclarationWithBlockFollowing(self): |
| 6427 |
self.TestFunctionLengthsCheck( |
| 6428 |
("void test();\n" + self.FunctionBody(66)), |
| 6429 |
"", |
| 6430 |
) |
| 6431 |
|
| 6432 |
def testFunctionLengthCheckClassDefinition(self): |
| 6433 |
self.TestFunctionLengthsCheck( |
| 6434 |
"class Test" + self.FunctionBody(66) + ";", "" |
| 6435 |
) |
| 6436 |
|
| 6437 |
def testFunctionLengthCheckTrivial(self): |
| 6438 |
self.TestFunctionLengthsCheck( |
| 6439 |
"void test() {}", |
| 6440 |
"", |
| 6441 |
) |
| 6442 |
|
| 6443 |
def testFunctionLengthCheckEmpty(self): |
| 6444 |
self.TestFunctionLengthsCheck("void test() {\n}", "") |
| 6445 |
|
| 6446 |
def testFunctionLengthCheckDefinitionBelowSeverity0(self): |
| 6447 |
old_verbosity = cpplint._SetVerboseLevel(0) |
| 6448 |
self.TestFunctionLengthCheckDefinitionOK(self.TriggerLines(0) - 1) |
| 6449 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6450 |
|
| 6451 |
def testFunctionLengthCheckDefinitionAtSeverity0(self): |
| 6452 |
old_verbosity = cpplint._SetVerboseLevel(0) |
| 6453 |
self.TestFunctionLengthCheckDefinitionOK(self.TriggerLines(0)) |
| 6454 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6455 |
|
| 6456 |
def testFunctionLengthCheckDefinitionAboveSeverity0(self): |
| 6457 |
old_verbosity = cpplint._SetVerboseLevel(0) |
| 6458 |
self.TestFunctionLengthCheckAboveErrorLevel(0) |
| 6459 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6460 |
|
| 6461 |
def testFunctionLengthCheckDefinitionBelowSeverity1v0(self): |
| 6462 |
old_verbosity = cpplint._SetVerboseLevel(0) |
| 6463 |
self.TestFunctionLengthCheckBelowErrorLevel(1) |
| 6464 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6465 |
|
| 6466 |
def testFunctionLengthCheckDefinitionAtSeverity1v0(self): |
| 6467 |
old_verbosity = cpplint._SetVerboseLevel(0) |
| 6468 |
self.TestFunctionLengthCheckAtErrorLevel(1) |
| 6469 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6470 |
|
| 6471 |
def testFunctionLengthCheckDefinitionBelowSeverity1(self): |
| 6472 |
self.TestFunctionLengthCheckDefinitionOK(self.TriggerLines(1) - 1) |
| 6473 |
|
| 6474 |
def testFunctionLengthCheckDefinitionAtSeverity1(self): |
| 6475 |
self.TestFunctionLengthCheckDefinitionOK(self.TriggerLines(1)) |
| 6476 |
|
| 6477 |
def testFunctionLengthCheckDefinitionAboveSeverity1(self): |
| 6478 |
self.TestFunctionLengthCheckAboveErrorLevel(1) |
| 6479 |
|
| 6480 |
def testFunctionLengthCheckDefinitionSeverity1PlusBlanks(self): |
| 6481 |
error_level = 1 |
| 6482 |
error_lines = self.TriggerLines(error_level) + 1 |
| 6483 |
trigger_level = self.TriggerLines(cpplint._VerboseLevel()) |
| 6484 |
self.TestFunctionLengthsCheck( |
| 6485 |
"void test_blanks(int x)" + self.FunctionBody(error_lines), |
| 6486 |
( |
| 6487 |
"Small and focused functions are preferred: " |
| 6488 |
"test_blanks() has %d non-comment lines " |
| 6489 |
"(error triggered by exceeding %d lines)." |
| 6490 |
" [readability/fn_size] [%d]" |
| 6491 |
) |
| 6492 |
% (error_lines, trigger_level, error_level), |
| 6493 |
) |
| 6494 |
|
| 6495 |
def testFunctionLengthCheckComplexDefinitionSeverity1(self): |
| 6496 |
error_level = 1 |
| 6497 |
error_lines = self.TriggerLines(error_level) + 1 |
| 6498 |
trigger_level = self.TriggerLines(cpplint._VerboseLevel()) |
| 6499 |
self.TestFunctionLengthsCheck( |
| 6500 |
( |
| 6501 |
"my_namespace::my_other_namespace::MyVeryLongTypeName*\n" |
| 6502 |
"my_namespace::my_other_namespace::MyFunction(int arg1, char* arg2)" |
| 6503 |
+ self.FunctionBody(error_lines) |
| 6504 |
), |
| 6505 |
( |
| 6506 |
"Small and focused functions are preferred: " |
| 6507 |
"my_namespace::my_other_namespace::MyFunction()" |
| 6508 |
" has %d non-comment lines " |
| 6509 |
"(error triggered by exceeding %d lines)." |
| 6510 |
" [readability/fn_size] [%d]" |
| 6511 |
) |
| 6512 |
% (error_lines, trigger_level, error_level), |
| 6513 |
) |
| 6514 |
|
| 6515 |
def testFunctionLengthCheckDefinitionSeverity1ForTest(self): |
| 6516 |
error_level = 1 |
| 6517 |
error_lines = self.TestLines(error_level) + 1 |
| 6518 |
trigger_level = self.TestLines(cpplint._VerboseLevel()) |
| 6519 |
self.TestFunctionLengthsCheck( |
| 6520 |
"TEST_F(Test, Mutator)" + self.FunctionBody(error_lines), |
| 6521 |
( |
| 6522 |
"Small and focused functions are preferred: " |
| 6523 |
"TEST_F(Test, Mutator) has %d non-comment lines " |
| 6524 |
"(error triggered by exceeding %d lines)." |
| 6525 |
" [readability/fn_size] [%d]" |
| 6526 |
) |
| 6527 |
% (error_lines, trigger_level, error_level), |
| 6528 |
) |
| 6529 |
|
| 6530 |
def testFunctionLengthCheckDefinitionSeverity1ForSplitLineTest(self): |
| 6531 |
error_level = 1 |
| 6532 |
error_lines = self.TestLines(error_level) + 1 |
| 6533 |
trigger_level = self.TestLines(cpplint._VerboseLevel()) |
| 6534 |
self.TestFunctionLengthsCheck( |
| 6535 |
( |
| 6536 |
"TEST_F(GoogleUpdateRecoveryRegistryProtectedTest,\n" |
| 6537 |
" FixGoogleUpdate_AllValues_MachineApp)" |
| 6538 |
+ self.FunctionBody(error_lines) |
| 6539 |
), |
| 6540 |
( |
| 6541 |
"Small and focused functions are preferred: " |
| 6542 |
"TEST_F(GoogleUpdateRecoveryRegistryProtectedTest, " |
| 6543 |
"FixGoogleUpdate_AllValues_MachineApp) has %d non-comment lines " |
| 6544 |
"(error triggered by exceeding %d lines)." |
| 6545 |
" [readability/fn_size] [%d]" |
| 6546 |
) |
| 6547 |
% (error_lines + 1, trigger_level, error_level), |
| 6548 |
) |
| 6549 |
|
| 6550 |
def testFunctionLengthCheckDefinitionSeverity1ForBadTestDoesntBreak(self): |
| 6551 |
error_level = 1 |
| 6552 |
error_lines = self.TestLines(error_level) + 1 |
| 6553 |
trigger_level = self.TestLines(cpplint._VerboseLevel()) |
| 6554 |
self.TestFunctionLengthsCheck( |
| 6555 |
("TEST_F(" + self.FunctionBody(error_lines)), |
| 6556 |
( |
| 6557 |
"Small and focused functions are preferred: " |
| 6558 |
"TEST_F has %d non-comment lines " |
| 6559 |
"(error triggered by exceeding %d lines)." |
| 6560 |
" [readability/fn_size] [%d]" |
| 6561 |
) |
| 6562 |
% (error_lines, trigger_level, error_level), |
| 6563 |
) |
| 6564 |
|
| 6565 |
def testFunctionLengthCheckDefinitionSeverity1WithEmbeddedNoLints(self): |
| 6566 |
error_level = 1 |
| 6567 |
error_lines = self.TriggerLines(error_level) + 1 |
| 6568 |
trigger_level = self.TriggerLines(cpplint._VerboseLevel()) |
| 6569 |
self.TestFunctionLengthsCheck( |
| 6570 |
"void test(int x)" + self.FunctionBodyWithNoLints(error_lines), |
| 6571 |
( |
| 6572 |
"Small and focused functions are preferred: " |
| 6573 |
"test() has %d non-comment lines " |
| 6574 |
"(error triggered by exceeding %d lines)." |
| 6575 |
" [readability/fn_size] [%d]" |
| 6576 |
) |
| 6577 |
% (error_lines, trigger_level, error_level), |
| 6578 |
) |
| 6579 |
|
| 6580 |
def testFunctionLengthCheckDefinitionSeverity1WithNoLint(self): |
| 6581 |
self.TestFunctionLengthsCheck( |
| 6582 |
( |
| 6583 |
"void test(int x)" |
| 6584 |
+ self.FunctionBody(self.TriggerLines(1)) |
| 6585 |
+ " // NOLINT -- long function" |
| 6586 |
), |
| 6587 |
"", |
| 6588 |
) |
| 6589 |
|
| 6590 |
def testFunctionLengthCheckDefinitionBelowSeverity2(self): |
| 6591 |
self.TestFunctionLengthCheckBelowErrorLevel(2) |
| 6592 |
|
| 6593 |
def testFunctionLengthCheckDefinitionSeverity2(self): |
| 6594 |
self.TestFunctionLengthCheckAtErrorLevel(2) |
| 6595 |
|
| 6596 |
def testFunctionLengthCheckDefinitionAboveSeverity2(self): |
| 6597 |
self.TestFunctionLengthCheckAboveErrorLevel(2) |
| 6598 |
|
| 6599 |
def testFunctionLengthCheckDefinitionBelowSeverity3(self): |
| 6600 |
self.TestFunctionLengthCheckBelowErrorLevel(3) |
| 6601 |
|
| 6602 |
def testFunctionLengthCheckDefinitionSeverity3(self): |
| 6603 |
self.TestFunctionLengthCheckAtErrorLevel(3) |
| 6604 |
|
| 6605 |
def testFunctionLengthCheckDefinitionAboveSeverity3(self): |
| 6606 |
self.TestFunctionLengthCheckAboveErrorLevel(3) |
| 6607 |
|
| 6608 |
def testFunctionLengthCheckDefinitionBelowSeverity4(self): |
| 6609 |
self.TestFunctionLengthCheckBelowErrorLevel(4) |
| 6610 |
|
| 6611 |
def testFunctionLengthCheckDefinitionSeverity4(self): |
| 6612 |
self.TestFunctionLengthCheckAtErrorLevel(4) |
| 6613 |
|
| 6614 |
def testFunctionLengthCheckDefinitionAboveSeverity4(self): |
| 6615 |
self.TestFunctionLengthCheckAboveErrorLevel(4) |
| 6616 |
|
| 6617 |
def testFunctionLengthCheckDefinitionBelowSeverity5(self): |
| 6618 |
self.TestFunctionLengthCheckBelowErrorLevel(5) |
| 6619 |
|
| 6620 |
def testFunctionLengthCheckDefinitionAtSeverity5(self): |
| 6621 |
self.TestFunctionLengthCheckAtErrorLevel(5) |
| 6622 |
|
| 6623 |
def testFunctionLengthCheckDefinitionAboveSeverity5(self): |
| 6624 |
self.TestFunctionLengthCheckAboveErrorLevel(5) |
| 6625 |
|
| 6626 |
def testFunctionLengthCheckDefinitionHugeLines(self): |
| 6627 |
|
| 6628 |
self.TestFunctionLengthCheckDefinition(self.TriggerLines(10), 5) |
| 6629 |
|
| 6630 |
def testFunctionLengthNotDeterminable(self): |
| 6631 |
|
| 6632 |
self.TestFunctionLengthsCheck("MACRO(arg)", "") |
| 6633 |
|
| 6634 |
|
| 6635 |
self.TestFunctionLengthsCheck("MACRO_WITH_UNDERSCORES(arg1, arg2, arg3)", "") |
| 6636 |
|
| 6637 |
self.TestFunctionLengthsCheck( |
| 6638 |
"NonMacro(arg)", |
| 6639 |
"Lint failed to find start of function body. [readability/fn_size] [5]", |
| 6640 |
) |
| 6641 |
|
| 6642 |
def testFunctionLengthCheckWithNamespace(self): |
| 6643 |
old_verbosity = cpplint._SetVerboseLevel(1) |
| 6644 |
self.TestFunctionLengthsCheck( |
| 6645 |
( |
| 6646 |
"namespace {\n" |
| 6647 |
"void CodeCoverageCL35256059() {\n" + (" X++;\n" * 3000) + "}\n" |
| 6648 |
"} // namespace\n" |
| 6649 |
), |
| 6650 |
( |
| 6651 |
"Small and focused functions are preferred: " |
| 6652 |
"CodeCoverageCL35256059() has 3000 non-comment lines " |
| 6653 |
"(error triggered by exceeding 20 lines)." |
| 6654 |
" [readability/fn_size] [5]" |
| 6655 |
), |
| 6656 |
) |
| 6657 |
cpplint._SetVerboseLevel(old_verbosity) |
| 6658 |
|
| 6659 |
|
| 6660 |
def TrimExtraIndent(text_block): |
| 6661 |
"""Trim a uniform amount of whitespace off of each line in a string. |
| 6662 |
|
| 6663 |
Compute the minimum indent on all non blank lines and trim that from each, so |
| 6664 |
that the block of text has no extra indentation. |
| 6665 |
|
| 6666 |
Args: |
| 6667 |
text_block: a multiline string |
| 6668 |
|
| 6669 |
Returns: |
| 6670 |
text_block with the common whitespace indent of each line removed. |
| 6671 |
""" |
| 6672 |
|
| 6673 |
def CountLeadingWhitespace(s): |
| 6674 |
count = 0 |
| 6675 |
for c in s: |
| 6676 |
if not c.isspace(): |
| 6677 |
break |
| 6678 |
count += 1 |
| 6679 |
return count |
| 6680 |
|
| 6681 |
|
| 6682 |
min_indent = min([CountLeadingWhitespace(line) for line in text_block.split("\n") if line]) |
| 6683 |
return "\n".join([line[min_indent:] for line in text_block.split("\n")]) |
| 6684 |
|
| 6685 |
|
| 6686 |
class TestCloseExpression: |
| 6687 |
@pytest.fixture(autouse=True) |
| 6688 |
def setUp(self): |
| 6689 |
self.lines = cpplint.CleansedLines( |
| 6690 |
|
| 6691 |
|
| 6692 |
[ |
| 6693 |
"// Line 0", |
| 6694 |
"inline RCULocked<X>::ReadPtr::ReadPtr(const RCULocked* rcu) {", |
| 6695 |
' DCHECK(!(data & kFlagMask)) << "Error";', |
| 6696 |
"}", |
| 6697 |
"// Line 4", |
| 6698 |
"RCULocked<X>::WritePtr::WritePtr(RCULocked* rcu)", |
| 6699 |
" : lock_(&rcu_->mutex_) {", |
| 6700 |
"}", |
| 6701 |
"// Line 8", |
| 6702 |
"template <typename T, typename... A>", |
| 6703 |
"typename std::enable_if<", |
| 6704 |
" std::is_array<T>::value && (std::extent<T>::value > 0)>::type", |
| 6705 |
"MakeUnique(A&&... a) = delete;", |
| 6706 |
"// Line 13", |
| 6707 |
"auto x = []() {};", |
| 6708 |
"// Line 15", |
| 6709 |
"template <typename U>", |
| 6710 |
"friend bool operator==(const reffed_ptr& a,", |
| 6711 |
" const reffed_ptr<U>& b) {", |
| 6712 |
" return a.get() == b.get();", |
| 6713 |
"}", |
| 6714 |
"// Line 21", |
| 6715 |
] |
| 6716 |
) |
| 6717 |
|
| 6718 |
def testCloseExpression(self): |
| 6719 |
|
| 6720 |
|
| 6721 |
positions = [ |
| 6722 |
(1, 16, 1, 19), |
| 6723 |
(1, 37, 1, 59), |
| 6724 |
(1, 60, 3, 1), |
| 6725 |
(2, 8, 2, 29), |
| 6726 |
(2, 30, 22, -1), |
| 6727 |
(9, 9, 9, 36), |
| 6728 |
(10, 23, 11, 59), |
| 6729 |
(11, 54, 22, -1), |
| 6730 |
(14, 9, 14, 11), |
| 6731 |
(14, 11, 14, 13), |
| 6732 |
(14, 14, 14, 16), |
| 6733 |
(17, 22, 18, 46), |
| 6734 |
(18, 47, 20, 1), |
| 6735 |
] |
| 6736 |
for p in positions: |
| 6737 |
(_, line, column) = cpplint.CloseExpression(self.lines, p[0], p[1]) |
| 6738 |
assert (p[2], p[3]) == (line, column) |
| 6739 |
|
| 6740 |
def testReverseCloseExpression(self): |
| 6741 |
|
| 6742 |
|
| 6743 |
positions = [ |
| 6744 |
(1, 18, 1, 16), |
| 6745 |
(1, 58, 1, 37), |
| 6746 |
(2, 27, 2, 10), |
| 6747 |
(2, 28, 2, 8), |
| 6748 |
(6, 18, 0, -1), |
| 6749 |
(9, 35, 9, 9), |
| 6750 |
(11, 54, 0, -1), |
| 6751 |
(11, 57, 11, 31), |
| 6752 |
(14, 10, 14, 9), |
| 6753 |
(14, 12, 14, 11), |
| 6754 |
(14, 15, 14, 14), |
| 6755 |
(18, 45, 17, 22), |
| 6756 |
(20, 0, 18, 47), |
| 6757 |
] |
| 6758 |
for p in positions: |
| 6759 |
(_, line, column) = cpplint.ReverseCloseExpression(self.lines, p[0], p[1]) |
| 6760 |
assert (p[2], p[3]) == (line, column) |
| 6761 |
|
| 6762 |
|
| 6763 |
class TestNestingState: |
| 6764 |
@pytest.fixture(autouse=True) |
| 6765 |
def setUp(self): |
| 6766 |
self.nesting_state = cpplint.NestingState() |
| 6767 |
self.error_collector = ErrorCollector(self.assertTrue) |
| 6768 |
|
| 6769 |
def assertTrue(self, condition, message=""): |
| 6770 |
assert condition, message |
| 6771 |
|
| 6772 |
def UpdateWithLines(self, lines): |
| 6773 |
clean_lines = cpplint.CleansedLines(lines) |
| 6774 |
for line in range(clean_lines.NumLines()): |
| 6775 |
self.nesting_state.Update("test.cc", clean_lines, line, self.error_collector) |
| 6776 |
|
| 6777 |
def testEmpty(self): |
| 6778 |
self.UpdateWithLines([]) |
| 6779 |
assert self.nesting_state.stack == [] |
| 6780 |
|
| 6781 |
def testNamespace(self): |
| 6782 |
self.UpdateWithLines(["namespace {"]) |
| 6783 |
assert len(self.nesting_state.stack) == 1 |
| 6784 |
assert isinstance(self.nesting_state.stack[0], cpplint._NamespaceInfo) |
| 6785 |
assert self.nesting_state.stack[0].seen_open_brace |
| 6786 |
assert self.nesting_state.stack[0].name == "" |
| 6787 |
|
| 6788 |
self.UpdateWithLines(["namespace outer { namespace inner"]) |
| 6789 |
assert len(self.nesting_state.stack) == 3 |
| 6790 |
assert self.nesting_state.stack[0].seen_open_brace |
| 6791 |
assert self.nesting_state.stack[1].seen_open_brace |
| 6792 |
assert not self.nesting_state.stack[2].seen_open_brace |
| 6793 |
assert self.nesting_state.stack[0].name == "" |
| 6794 |
assert self.nesting_state.stack[1].name == "outer" |
| 6795 |
assert self.nesting_state.stack[2].name == "inner" |
| 6796 |
|
| 6797 |
self.UpdateWithLines(["{"]) |
| 6798 |
assert self.nesting_state.stack[2].seen_open_brace |
| 6799 |
|
| 6800 |
self.UpdateWithLines(["}", "}}"]) |
| 6801 |
assert len(self.nesting_state.stack) == 0 |
| 6802 |
|
| 6803 |
def testDecoratedClass(self): |
| 6804 |
self.UpdateWithLines(["class Decorated_123 API A {"]) |
| 6805 |
assert len(self.nesting_state.stack) == 1 |
| 6806 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6807 |
assert self.nesting_state.stack[0].name == "A" |
| 6808 |
assert not self.nesting_state.stack[0].is_derived |
| 6809 |
assert self.nesting_state.stack[0].class_indent == 0 |
| 6810 |
self.UpdateWithLines(["}"]) |
| 6811 |
assert len(self.nesting_state.stack) == 0 |
| 6812 |
|
| 6813 |
def testInnerClass(self): |
| 6814 |
self.UpdateWithLines(["class A::B::C {"]) |
| 6815 |
assert len(self.nesting_state.stack) == 1 |
| 6816 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6817 |
assert self.nesting_state.stack[0].name == "A::B::C" |
| 6818 |
assert not self.nesting_state.stack[0].is_derived |
| 6819 |
assert self.nesting_state.stack[0].class_indent == 0 |
| 6820 |
self.UpdateWithLines(["}"]) |
| 6821 |
assert len(self.nesting_state.stack) == 0 |
| 6822 |
|
| 6823 |
def testClass(self): |
| 6824 |
self.UpdateWithLines(["class A {"]) |
| 6825 |
assert len(self.nesting_state.stack) == 1 |
| 6826 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6827 |
assert self.nesting_state.stack[0].name == "A" |
| 6828 |
assert not self.nesting_state.stack[0].is_derived |
| 6829 |
assert self.nesting_state.stack[0].class_indent == 0 |
| 6830 |
|
| 6831 |
self.UpdateWithLines(["};", "struct B : public A {"]) |
| 6832 |
assert len(self.nesting_state.stack) == 1 |
| 6833 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6834 |
assert self.nesting_state.stack[0].name == "B" |
| 6835 |
assert self.nesting_state.stack[0].is_derived |
| 6836 |
|
| 6837 |
self.UpdateWithLines(["};", "class C", ": public A {"]) |
| 6838 |
assert len(self.nesting_state.stack) == 1 |
| 6839 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6840 |
assert self.nesting_state.stack[0].name == "C" |
| 6841 |
assert self.nesting_state.stack[0].is_derived |
| 6842 |
|
| 6843 |
self.UpdateWithLines(["};", "template<T>"]) |
| 6844 |
assert len(self.nesting_state.stack) == 0 |
| 6845 |
|
| 6846 |
self.UpdateWithLines(["class D {", " class E {"]) |
| 6847 |
assert len(self.nesting_state.stack) == 2 |
| 6848 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6849 |
assert self.nesting_state.stack[0].name == "D" |
| 6850 |
assert not self.nesting_state.stack[0].is_derived |
| 6851 |
assert isinstance(self.nesting_state.stack[1], cpplint._ClassInfo) |
| 6852 |
assert self.nesting_state.stack[1].name == "E" |
| 6853 |
assert not self.nesting_state.stack[1].is_derived |
| 6854 |
assert self.nesting_state.stack[1].class_indent == 2 |
| 6855 |
assert self.nesting_state.InnermostClass().name == "E" |
| 6856 |
|
| 6857 |
self.UpdateWithLines(["}", "}"]) |
| 6858 |
assert len(self.nesting_state.stack) == 0 |
| 6859 |
|
| 6860 |
def testClassAccess(self): |
| 6861 |
self.UpdateWithLines(["class A {"]) |
| 6862 |
assert len(self.nesting_state.stack) == 1 |
| 6863 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6864 |
assert self.nesting_state.stack[0].access == "private" |
| 6865 |
|
| 6866 |
self.UpdateWithLines([" public:"]) |
| 6867 |
assert self.nesting_state.stack[0].access == "public" |
| 6868 |
self.UpdateWithLines([" protracted:"]) |
| 6869 |
assert self.nesting_state.stack[0].access == "public" |
| 6870 |
self.UpdateWithLines([" protected:"]) |
| 6871 |
assert self.nesting_state.stack[0].access == "protected" |
| 6872 |
self.UpdateWithLines([" private:"]) |
| 6873 |
assert self.nesting_state.stack[0].access == "private" |
| 6874 |
|
| 6875 |
self.UpdateWithLines([" struct B {"]) |
| 6876 |
assert len(self.nesting_state.stack) == 2 |
| 6877 |
assert isinstance(self.nesting_state.stack[1], cpplint._ClassInfo) |
| 6878 |
assert self.nesting_state.stack[1].access == "public" |
| 6879 |
assert self.nesting_state.stack[0].access == "private" |
| 6880 |
|
| 6881 |
self.UpdateWithLines([" protected :"]) |
| 6882 |
assert self.nesting_state.stack[1].access == "protected" |
| 6883 |
assert self.nesting_state.stack[0].access == "private" |
| 6884 |
|
| 6885 |
self.UpdateWithLines([" }", "}"]) |
| 6886 |
assert len(self.nesting_state.stack) == 0 |
| 6887 |
|
| 6888 |
def testStruct(self): |
| 6889 |
self.UpdateWithLines(["struct A {"]) |
| 6890 |
assert len(self.nesting_state.stack) == 1 |
| 6891 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6892 |
assert self.nesting_state.stack[0].name == "A" |
| 6893 |
assert not self.nesting_state.stack[0].is_derived |
| 6894 |
|
| 6895 |
self.UpdateWithLines(["}", "void Func(struct B arg) {"]) |
| 6896 |
assert len(self.nesting_state.stack) == 1 |
| 6897 |
assert not isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6898 |
|
| 6899 |
self.UpdateWithLines(["}"]) |
| 6900 |
assert len(self.nesting_state.stack) == 0 |
| 6901 |
|
| 6902 |
def testPreprocessor(self): |
| 6903 |
assert len(self.nesting_state.pp_stack) == 0 |
| 6904 |
self.UpdateWithLines(["#if MACRO1"]) |
| 6905 |
assert len(self.nesting_state.pp_stack) == 1 |
| 6906 |
self.UpdateWithLines(["#endif"]) |
| 6907 |
assert len(self.nesting_state.pp_stack) == 0 |
| 6908 |
|
| 6909 |
self.UpdateWithLines(["#ifdef MACRO2"]) |
| 6910 |
assert len(self.nesting_state.pp_stack) == 1 |
| 6911 |
self.UpdateWithLines(["#else"]) |
| 6912 |
assert len(self.nesting_state.pp_stack) == 1 |
| 6913 |
self.UpdateWithLines(["#ifdef MACRO3"]) |
| 6914 |
assert len(self.nesting_state.pp_stack) == 2 |
| 6915 |
self.UpdateWithLines(["#elif MACRO4"]) |
| 6916 |
assert len(self.nesting_state.pp_stack) == 2 |
| 6917 |
self.UpdateWithLines(["#endif"]) |
| 6918 |
assert len(self.nesting_state.pp_stack) == 1 |
| 6919 |
self.UpdateWithLines(["#endif"]) |
| 6920 |
assert len(self.nesting_state.pp_stack) == 0 |
| 6921 |
|
| 6922 |
self.UpdateWithLines( |
| 6923 |
[ |
| 6924 |
"#ifdef MACRO5", |
| 6925 |
"class A {", |
| 6926 |
"#elif MACRO6", |
| 6927 |
"class B {", |
| 6928 |
"#else", |
| 6929 |
"class C {", |
| 6930 |
"#endif", |
| 6931 |
] |
| 6932 |
) |
| 6933 |
assert len(self.nesting_state.pp_stack) == 0 |
| 6934 |
assert len(self.nesting_state.stack) == 1 |
| 6935 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6936 |
assert self.nesting_state.stack[0].name == "A" |
| 6937 |
self.UpdateWithLines(["};"]) |
| 6938 |
assert len(self.nesting_state.stack) == 0 |
| 6939 |
|
| 6940 |
self.UpdateWithLines(["class D", "#ifdef MACRO7"]) |
| 6941 |
assert len(self.nesting_state.pp_stack) == 1 |
| 6942 |
assert len(self.nesting_state.stack) == 1 |
| 6943 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6944 |
assert self.nesting_state.stack[0].name == "D" |
| 6945 |
assert not self.nesting_state.stack[0].is_derived |
| 6946 |
|
| 6947 |
self.UpdateWithLines(["#elif MACRO8", ": public E"]) |
| 6948 |
assert len(self.nesting_state.stack) == 1 |
| 6949 |
assert self.nesting_state.stack[0].name == "D" |
| 6950 |
assert self.nesting_state.stack[0].is_derived |
| 6951 |
assert not self.nesting_state.stack[0].seen_open_brace |
| 6952 |
|
| 6953 |
self.UpdateWithLines(["#else", "{"]) |
| 6954 |
assert len(self.nesting_state.stack) == 1 |
| 6955 |
assert self.nesting_state.stack[0].name == "D" |
| 6956 |
assert not self.nesting_state.stack[0].is_derived |
| 6957 |
assert self.nesting_state.stack[0].seen_open_brace |
| 6958 |
|
| 6959 |
self.UpdateWithLines(["#endif"]) |
| 6960 |
assert len(self.nesting_state.pp_stack) == 0 |
| 6961 |
assert len(self.nesting_state.stack) == 1 |
| 6962 |
assert self.nesting_state.stack[0].name == "D" |
| 6963 |
assert not self.nesting_state.stack[0].is_derived |
| 6964 |
assert not self.nesting_state.stack[0].seen_open_brace |
| 6965 |
|
| 6966 |
self.UpdateWithLines([";"]) |
| 6967 |
assert len(self.nesting_state.stack) == 0 |
| 6968 |
|
| 6969 |
def testTemplate(self): |
| 6970 |
self.UpdateWithLines(["template <T,", " class Arg1 = tmpl<T> >"]) |
| 6971 |
assert len(self.nesting_state.stack) == 0 |
| 6972 |
self.UpdateWithLines(["class A {"]) |
| 6973 |
assert len(self.nesting_state.stack) == 1 |
| 6974 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6975 |
assert self.nesting_state.stack[0].name == "A" |
| 6976 |
|
| 6977 |
self.UpdateWithLines( |
| 6978 |
["};", "template <T,", " template <typename, typename> class B>", "class C"] |
| 6979 |
) |
| 6980 |
assert len(self.nesting_state.stack) == 1 |
| 6981 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6982 |
assert self.nesting_state.stack[0].name == "C" |
| 6983 |
self.UpdateWithLines([";"]) |
| 6984 |
assert len(self.nesting_state.stack) == 0 |
| 6985 |
|
| 6986 |
self.UpdateWithLines(["class D : public Tmpl<E>"]) |
| 6987 |
assert len(self.nesting_state.stack) == 1 |
| 6988 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 6989 |
assert self.nesting_state.stack[0].name == "D" |
| 6990 |
|
| 6991 |
self.UpdateWithLines(["{", "};"]) |
| 6992 |
assert len(self.nesting_state.stack) == 0 |
| 6993 |
|
| 6994 |
self.UpdateWithLines( |
| 6995 |
[ |
| 6996 |
"template <class F,", |
| 6997 |
" class G,", |
| 6998 |
" class H,", |
| 6999 |
" typename I>", |
| 7000 |
"static void Func() {", |
| 7001 |
] |
| 7002 |
) |
| 7003 |
assert len(self.nesting_state.stack) == 1 |
| 7004 |
assert not isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 7005 |
self.UpdateWithLines(["}", "template <class J> class K {"]) |
| 7006 |
assert len(self.nesting_state.stack) == 1 |
| 7007 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 7008 |
assert self.nesting_state.stack[0].name == "K" |
| 7009 |
|
| 7010 |
def testTemplateDefaultArg(self): |
| 7011 |
self.UpdateWithLines(["template <class T, class D = default_delete<T>> class unique_ptr {"]) |
| 7012 |
assert len(self.nesting_state.stack) == 1 |
| 7013 |
assert self.nesting_state.stack[0], isinstance( |
| 7014 |
self.nesting_state.stack[0], cpplint._ClassInfo |
| 7015 |
) |
| 7016 |
|
| 7017 |
def testTemplateInnerClass(self): |
| 7018 |
self.UpdateWithLines(["class A {", " public:"]) |
| 7019 |
assert len(self.nesting_state.stack) == 1 |
| 7020 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 7021 |
|
| 7022 |
self.UpdateWithLines([" template <class B>", " class C<alloc<B> >", " : public A {"]) |
| 7023 |
assert len(self.nesting_state.stack) == 2 |
| 7024 |
assert isinstance(self.nesting_state.stack[1], cpplint._ClassInfo) |
| 7025 |
|
| 7026 |
def testArguments(self): |
| 7027 |
self.UpdateWithLines(["class A {"]) |
| 7028 |
assert len(self.nesting_state.stack) == 1 |
| 7029 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 7030 |
assert self.nesting_state.stack[0].name == "A" |
| 7031 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7032 |
|
| 7033 |
self.UpdateWithLines([" void Func(", " struct X arg1,"]) |
| 7034 |
assert len(self.nesting_state.stack) == 1 |
| 7035 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7036 |
self.UpdateWithLines([" struct X *arg2);"]) |
| 7037 |
assert len(self.nesting_state.stack) == 1 |
| 7038 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7039 |
|
| 7040 |
self.UpdateWithLines(["};"]) |
| 7041 |
assert len(self.nesting_state.stack) == 0 |
| 7042 |
|
| 7043 |
self.UpdateWithLines(["struct B {"]) |
| 7044 |
assert len(self.nesting_state.stack) == 1 |
| 7045 |
assert isinstance(self.nesting_state.stack[0], cpplint._ClassInfo) |
| 7046 |
assert self.nesting_state.stack[0].name == "B" |
| 7047 |
|
| 7048 |
self.UpdateWithLines(["#ifdef MACRO", " void Func(", " struct X arg1"]) |
| 7049 |
assert len(self.nesting_state.stack) == 1 |
| 7050 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7051 |
self.UpdateWithLines(["#else"]) |
| 7052 |
|
| 7053 |
assert len(self.nesting_state.stack) == 1 |
| 7054 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7055 |
self.UpdateWithLines([" void Func(", " struct X arg1"]) |
| 7056 |
assert len(self.nesting_state.stack) == 1 |
| 7057 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7058 |
|
| 7059 |
self.UpdateWithLines(["#endif"]) |
| 7060 |
assert len(self.nesting_state.stack) == 1 |
| 7061 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7062 |
self.UpdateWithLines([" struct X *arg2);"]) |
| 7063 |
assert len(self.nesting_state.stack) == 1 |
| 7064 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7065 |
|
| 7066 |
self.UpdateWithLines(["};"]) |
| 7067 |
assert len(self.nesting_state.stack) == 0 |
| 7068 |
|
| 7069 |
def testInlineAssembly(self): |
| 7070 |
self.UpdateWithLines( |
| 7071 |
[ |
| 7072 |
"void CopyRow_SSE2(const uint8_t* src, uint8_t* dst,", |
| 7073 |
" int count) {", |
| 7074 |
] |
| 7075 |
) |
| 7076 |
assert len(self.nesting_state.stack) == 1 |
| 7077 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7078 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._NO_ASM |
| 7079 |
|
| 7080 |
self.UpdateWithLines([" asm volatile ("]) |
| 7081 |
assert len(self.nesting_state.stack) == 1 |
| 7082 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7083 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._INSIDE_ASM |
| 7084 |
|
| 7085 |
self.UpdateWithLines( |
| 7086 |
[ |
| 7087 |
' "sub %0,%1 \\n"', |
| 7088 |
' "1: \\n"', |
| 7089 |
' "movdqa (%0),%%xmm0 \\n"', |
| 7090 |
' "movdqa 0x10(%0),%%xmm1 \\n"', |
| 7091 |
' "movdqa %%xmm0,(%0,%1) \\n"', |
| 7092 |
' "movdqa %%xmm1,0x10(%0,%1) \\n"', |
| 7093 |
' "lea 0x20(%0),%0 \\n"', |
| 7094 |
' "sub $0x20,%2 \\n"', |
| 7095 |
' "jg 1b \\n"', |
| 7096 |
' : "+r"(src), // %0', |
| 7097 |
' "+r"(dst), // %1', |
| 7098 |
' "+r"(count) // %2', |
| 7099 |
" :", |
| 7100 |
' : "memory", "cc"', |
| 7101 |
] |
| 7102 |
) |
| 7103 |
assert len(self.nesting_state.stack) == 1 |
| 7104 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7105 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._INSIDE_ASM |
| 7106 |
|
| 7107 |
self.UpdateWithLines(["#if defined(__SSE2__)", ' , "xmm0", "xmm1"']) |
| 7108 |
assert len(self.nesting_state.stack) == 1 |
| 7109 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7110 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._INSIDE_ASM |
| 7111 |
|
| 7112 |
self.UpdateWithLines(["#endif"]) |
| 7113 |
assert len(self.nesting_state.stack) == 1 |
| 7114 |
assert self.nesting_state.stack[-1].open_parentheses == 1 |
| 7115 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._INSIDE_ASM |
| 7116 |
|
| 7117 |
self.UpdateWithLines([" );"]) |
| 7118 |
assert len(self.nesting_state.stack) == 1 |
| 7119 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7120 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._END_ASM |
| 7121 |
|
| 7122 |
self.UpdateWithLines(["__asm {"]) |
| 7123 |
assert len(self.nesting_state.stack) == 2 |
| 7124 |
assert self.nesting_state.stack[-1].open_parentheses == 0 |
| 7125 |
assert self.nesting_state.stack[-1].inline_asm == cpplint._BLOCK_ASM |
| 7126 |
|
| 7127 |
self.UpdateWithLines(["}"]) |
| 7128 |
assert len(self.nesting_state.stack) == 1 |
| 7129 |
|
| 7130 |
self.UpdateWithLines(["}"]) |
| 7131 |
assert len(self.nesting_state.stack) == 0 |
| 7132 |
|
| 7133 |
|
| 7134 |
class TestQuiet: |
| 7135 |
@pytest.fixture(autouse=True) |
| 7136 |
def setUp(self): |
| 7137 |
self.temp_dir = os.path.realpath(tempfile.mkdtemp()) |
| 7138 |
self.this_dir_path = os.path.abspath(self.temp_dir) |
| 7139 |
self.python_executable = sys.executable or "python" |
| 7140 |
self.cpplint_test_h = os.path.join(self.this_dir_path, "cpplint_test_header.h") |
| 7141 |
open(self.cpplint_test_h, "w").close() |
| 7142 |
|
| 7143 |
def tearDown(self): |
| 7144 |
shutil.rmtree(self.temp_dir) |
| 7145 |
|
| 7146 |
def _runCppLint(self, *args): |
| 7147 |
cpplint_abspath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cpplint.py") |
| 7148 |
|
| 7149 |
cmd_line = [self.python_executable, cpplint_abspath] + list(args) + [self.cpplint_test_h] |
| 7150 |
|
| 7151 |
return_code = 0 |
| 7152 |
try: |
| 7153 |
output = subprocess.check_output(cmd_line, stderr=subprocess.STDOUT) |
| 7154 |
except subprocess.CalledProcessError as err: |
| 7155 |
return_code = err.returncode |
| 7156 |
output = err.output |
| 7157 |
if isinstance(output, bytes): |
| 7158 |
output = output.decode("utf-8") |
| 7159 |
return (return_code, output) |
| 7160 |
|
| 7161 |
def testNonQuietWithErrors(self): |
| 7162 |
|
| 7163 |
(return_code, output) = self._runCppLint() |
| 7164 |
assert return_code == 1 |
| 7165 |
|
| 7166 |
assert "[legal/copyright]" in output |
| 7167 |
assert "[build/header_guard]" in output |
| 7168 |
|
| 7169 |
assert "Done processing" in output |
| 7170 |
assert "Total errors found:" in output |
| 7171 |
|
| 7172 |
def testQuietWithErrors(self): |
| 7173 |
|
| 7174 |
(return_code, output) = self._runCppLint("--quiet") |
| 7175 |
assert return_code == 1 |
| 7176 |
assert "[legal/copyright]" in output |
| 7177 |
assert "[build/header_guard]" in output |
| 7178 |
|
| 7179 |
assert "Done processing" in output |
| 7180 |
assert "Total errors found:" in output |
| 7181 |
|
| 7182 |
def testNonQuietWithoutErrors(self): |
| 7183 |
|
| 7184 |
(return_code, output) = self._runCppLint( |
| 7185 |
"--filter=" + "-legal/copyright," + "-build/header_guard" |
| 7186 |
) |
| 7187 |
assert return_code == 0, output |
| 7188 |
|
| 7189 |
assert "[legal/copyright]" not in output |
| 7190 |
assert "[build/header_guard]" not in output |
| 7191 |
|
| 7192 |
|
| 7193 |
assert "Done processing" in output |
| 7194 |
|
| 7195 |
def testQuietWithoutErrors(self): |
| 7196 |
|
| 7197 |
(return_code, output) = self._runCppLint( |
| 7198 |
"--quiet", "--filter=" + "-legal/copyright," + "-build/header_guard" |
| 7199 |
) |
| 7200 |
assert return_code == 0, output |
| 7201 |
|
| 7202 |
assert "[legal/copyright]" not in output |
| 7203 |
assert "[build/header_guard]" not in output |
| 7204 |
|
| 7205 |
|
| 7206 |
assert "Done processing" not in output |
| 7207 |
assert "Total errors found:" not in output |
| 7208 |
|
| 7209 |
assert output == "" |
| 7210 |
|
| 7211 |
|
| 7212 |
|
| 7213 |
|
| 7214 |
|
| 7215 |
|
| 7216 |
|
| 7217 |
|
| 7218 |
def setUp(): |
| 7219 |
"""Runs before all tests are executed.""" |
| 7220 |
|
| 7221 |
cpplint._DEFAULT_FILTERS = [] |
| 7222 |
cpplint._cpplint_state.SetFilters("") |
| 7223 |
|
| 7224 |
|
| 7225 |
@pytest.fixture(autouse=True, scope="session") |
| 7226 |
def run_around_tests(pytestconfig: pytest.Config): |
| 7227 |
setUp() |
| 7228 |
yield |
| 7229 |
|
| 7230 |
|
| 7231 |
|
| 7232 |
|
| 7233 |
|
| 7234 |
if pytestconfig.getoption("-k", default=None) in {None, ""} and not any( |
| 7235 |
"::" in arg for arg in pytestconfig.args |
| 7236 |
): |
| 7237 |
ErrorCollector(None).VerifyAllCategoriesAreSeen() |
| 7238 |
|
| 7239 |
|
| 7240 |
if __name__ == "__main__": |
| 7241 |
pytest.main([__file__]) |