GeezerVison is showing github.com — fix: handle multiline function templates in namespaces · cpplint/cpplint@cd67aa7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.rst +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,8 @@ Changelog | 5 | 5 | TBA | 6 | 6 | === | 7 | 7 |
| | 8 | +* Function template declarations with multiline template parameters no longer trigger ``whitespace/indent_namespace`` on their continuation lines. | | 9 | + | 8 | 10 | 2.0.2 (2025-04-08) | 9 | 11 | =========== | 10 | 12 |
| |
Collapse file Copy file name to clipboardExpand all lines: cpplint.py +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4161,6 +4161,9 @@ def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line, err | 4161 | 4161 | or (isinstance(nesting_state.previous_stack_top, _NamespaceInfo)) | 4162 | 4162 | ) | 4163 | 4163 |
| | 4164 | + if IsMultilineFunctionTemplateDeclaration(clean_lines, line): | | 4165 | + return | | 4166 | + | 4164 | 4167 | if ShouldCheckNamespaceIndentation( | 4165 | 4168 | nesting_state, is_namespace_indent_item, clean_lines.elided, line | 4166 | 4169 | ): | @@ -7384,6 +7387,36 @@ def IsBlockInNameSpace(nesting_state: NestingState, is_forward_declaration: bool | 7384 | 7387 | return False | 7385 | 7388 |
| 7386 | 7389 |
| | 7390 | +def IsMultilineFunctionTemplateDeclaration(clean_lines, linenum): | | 7391 | + """Checks whether a line continues a function template declaration.""" | | 7392 | + for start_line in range(linenum - 1, -1, -1): | | 7393 | + line = clean_lines.elided[start_line] | | 7394 | + if re.search(r"[;{}]", line): | | 7395 | + return False | | 7396 | + | | 7397 | + if template := re.search(r"\btemplate\s*<", line): | | 7398 | + _, end_line, end_pos = CloseExpression(clean_lines, start_line, template.end() - 1) | | 7399 | + if not start_line < linenum <= end_line or end_pos < 0: | | 7400 | + return False | | 7401 | + | | 7402 | + declaration = clean_lines.elided[end_line][end_pos:] | | 7403 | + for next_line in range(end_line + 1, clean_lines.NumLines()): | | 7404 | + declaration += " " + clean_lines.elided[next_line].strip() | | 7405 | + if re.search(r"[;{}]", declaration): | | 7406 | + break | | 7407 | + | | 7408 | + declaration = declaration.strip() | | 7409 | + if not declaration or "{" in declaration.split(";", 1)[0]: | | 7410 | + return False | | 7411 | + if re.match(r"(?:class|enum|struct|using)\b", declaration): | | 7412 | + return False | | 7413 | + | | 7414 | + function = re.search(r"\b(?:operator\s*\S+|~?[A-Za-z_]\w*)\s*\(", declaration) | | 7415 | + return function is not None and "=" not in declaration[: function.start()] | | 7416 | + | | 7417 | + return False | | 7418 | + | | 7419 | + | 7387 | 7420 | def ShouldCheckNamespaceIndentation( | 7388 | 7421 | nesting_state: NestingState, is_namespace_indent_item, raw_lines_no_comments, linenum | 7389 | 7422 | ): | |
Collapse file Copy file name to clipboardExpand all lines: cpplint_unittest.py +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -323,6 +323,38 @@ def testNamespaceIndentationIndentedParameter(self): | 323 | 323 | results = self.GetNamespaceResults(lines) | 324 | 324 | assert results == "" | 325 | 325 |
| | 326 | + def testNamespaceIndentationMultilineFunctionTemplateDeclaration(self): | | 327 | + lines = [ | | 328 | + "namespace Test {", | | 329 | + "template <typename Type1,", | | 330 | + " typename Type2>", | | 331 | + "void TestFunc(const Type1 &var1, Type2 &var2);", | | 332 | + "} // namespace Test", | | 333 | + ] | | 334 | + assert self.GetNamespaceResults(lines) == "" | | 335 | + | | 336 | + lines = [ | | 337 | + "namespace Test {", | | 338 | + "template <typename T,", | | 339 | + " typename Callback = void (*)(T)>", | | 340 | + "void Register(Callback callback);", | | 341 | + "} // namespace Test", | | 342 | + ] | | 343 | + assert self.GetNamespaceResults(lines) == "" | | 344 | + | | 345 | + def testNamespaceIndentationIndentedMultilineFunctionTemplateDeclaration(self): | | 346 | + lines = [ | | 347 | + "namespace Test {", | | 348 | + " template <typename Type1,", | | 349 | + " typename Type2>", | | 350 | + " void TestFunc(const Type1 &var1, Type2 &var2);", | | 351 | + "} // namespace Test", | | 352 | + ] | | 353 | + assert self.GetNamespaceResults(lines) == [ | | 354 | + "Do not indent within a namespace. [whitespace/indent_namespace] [4]", | | 355 | + "Do not indent within a namespace. [whitespace/indent_namespace] [4]", | | 356 | + ] | | 357 | + | 326 | 358 | def testNamespaceIndentationMemberInitializerList(self): | 327 | 359 | lines = [ | 328 | 360 | "namespace Opossum {", | |
Collapse file Copy file name to clipboardExpand all lines: samples/boost-sample/simple.def +1-3Lines changed: 1 addition & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ include/boost/math/* | 2 | 2 | 1 | 3 | 3 | 3 | 4 | 4 | Done processing include/boost/math/octonion.hpp | 5 | | -Total errors found: 2900 | | 5 | +Total errors found: 2898 | 6 | 6 |
| 7 | 7 | include/boost/math/octonion.hpp:11: #ifndef header guard has wrong style, please use: SAMPLES_BOOST_SAMPLE_INCLUDE_BOOST_MATH_OCTONION_HPP_ [build/header_guard] [5] | 8 | 8 | include/boost/math/octonion.hpp:4250: #endif line should be "#endif // SAMPLES_BOOST_SAMPLE_INCLUDE_BOOST_MATH_OCTONION_HPP_" [build/header_guard] [5] | @@ -290,8 +290,6 @@ include/boost/math/octonion.hpp:672: { should almost always be at the end of th | 290 | 290 | include/boost/math/octonion.hpp:673: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 291 | 291 | include/boost/math/octonion.hpp:673: Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4] | 292 | 292 | include/boost/math/octonion.hpp:674: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 293 | | -include/boost/math/octonion.hpp:675: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 294 | | -include/boost/math/octonion.hpp:676: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 295 | 293 | include/boost/math/octonion.hpp:677: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 296 | 294 | include/boost/math/octonion.hpp:678: Do not indent within a namespace. [whitespace/indent_namespace] [4] | 297 | 295 | include/boost/math/octonion.hpp:679: Do not indent within a namespace. [whitespace/indent_namespace] [4] | |
|