Visitar URL original
fix: allow braces after multiline preprocessor directives by PNHD · Pull Request #480 · cpplint/cpplint · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: allow braces after multiline preprocessor directives
Trace continued lines to their directive before applying the standalone brace warning. Preserve the existing checks for ordinary continued code.

Fixes #294.
  • Loading branch information
PNHD committed Oct 26, 2026
commit d3f400388027d35d17715dc4a24e522abd5a9fea
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
TBA
===

* Fixed a whitespace/braces false positive after multiline preprocessor directives. (#294)
* Fixed a whitespace/newline false positive for control conditions containing lambdas. (#410)
* We now error on relative include paths (``./``, ``../``). (#432)
* This makes ``#include "./foo.h"`` produce two separate errors: that foo.cpp should include foo.h and that relative paths are not allowed.
Expand Down
8 changes: 6 additions & 2 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4909,10 +4909,14 @@ def CheckBraces(filename, clean_lines, linenum, error):
# previous line starts a preprocessor block. We also allow a brace on the
# following line if it is part of an array initialization and would not fit
# within the 80 character limit of the preceding line.
prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
prevline, prevlinenum = GetPreviousNonBlankLine(clean_lines, linenum)
# Follow continuations back to the start of the preprocessor directive.
while prevlinenum > 0 and clean_lines.raw_lines[prevlinenum - 1].endswith("\\"):
prevlinenum -= 1
is_preprocessor = prevlinenum >= 0 and re.match(r"\s*#", clean_lines.elided[prevlinenum])
if (
not re.search(r"[,;:}{(]\s*$", prevline)
and not re.match(r"\s*#", prevline)
and not is_preprocessor
and not (GetLineWidth(prevline) > _line_length - 2 and "[]" in prevline)
):
error(
Expand Down
33 changes: 33 additions & 0 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,39 @@ def testBraceAtBeginOfLine(self):
"",
)

@pytest.mark.parametrize(
"directive",
[
"#pragma omp parallel num_threads(thread_count)",
(
"#pragma omp parallel num_threads(thread_count) \\\n"
" default(none) shared(thread_count)"
),
(
"#pragma omp parallel \\\n"
" num_threads(thread_count) \\\n"
" default(none) shared(thread_count)"
),
"#pragma omp parallel \\\n num_threads(thread_count) // Comment\n\n",
],
)
def testBraceAfterPreprocessorDirective(self, directive):
self.TestMultiLineLint(directive + "\n{\n}", "")

@pytest.mark.parametrize(
"code",
[
"if (condition \\\n && other_condition)",
"#pragma omp parallel\nif (condition)",
"#pragma omp parallel \\\n\nif (condition)",
],
)
def testBraceAfterPreprocessorDirectiveDoesNotSuppressOtherCode(self, code):
self.TestMultiLineLint(
code + "\n{\n Work();\n}",
"{ should almost always be at the end of the previous line [whitespace/braces] [4]",
)

def testInlineControlBodyWithLambda(self):
self.TestLint("if (all_of(v, [](auto b) { return b == 0; })) {", "")
self.TestLint(
Expand Down
Loading