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
11 changes: 6 additions & 5 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3943,14 +3943,15 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum, nesting_state,
else:
constructor_args = explicit_constructor_match.group(2).split(",")

# collapse arguments so that commas in template parameter lists and function
# argument parameter lists don't split arguments in two
# Collapse commas inside template and function parameter lists.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this change?

i = 0
while i < len(constructor_args):
constructor_arg = constructor_args[i]
while constructor_arg.count("<") > constructor_arg.count(">") or constructor_arg.count(
"("
) > constructor_arg.count(")"):
# Ignore `<`-prefixed operators when balancing template brackets.
while i + 1 < len(constructor_args) and (
re.sub(r"<<=?|<=", "", constructor_arg).count("<") > constructor_arg.count(">")
or constructor_arg.count("(") > constructor_arg.count(")")
Comment on lines +3952 to +3953

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very inefficient. We were already using .count to do four optimized single-character passes, and now we add a regex pass on top of that. Not to mention we're sort of simulating regex with the consuming (del) of tokens below, and adding onto our misery by going over parts of constructor_arg we've already searched again with each loop. Surely we can simplify at least this part to O(n).

):
constructor_arg += "," + constructor_args[i + 1]
del constructor_args[i + 1]
constructor_args[i] = constructor_arg
Expand Down
31 changes: 31 additions & 0 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,37 @@ class Foo {
"Constructors callable with one argument should be marked explicit."
" [runtime/explicit] [4]",
)
# `<`-containing operators are not confused with template brackets.
self.TestMultiLineLint(
"""
class A {
A(int a, int b, int c = 1 << 1);
};""",
"",
)
self.TestMultiLineLint(
"""
class A {
A(int a = (value <<= 1), int b = 0, int c = 0);
};""",
"Constructors callable with one argument should be marked explicit."
" [runtime/explicit] [4]",
)
self.TestMultiLineLint(
"""
class A {
A(int a, bool b = 1 <= 1, int c = 0);
};""",
"Constructors callable with one argument should be marked explicit."
" [runtime/explicit] [4]",
)
self.TestMultiLineLint(
"""
class A {
A(int a, int b, bool c = 1 < 2);
};""",
"",
)
# explicit no-argument constructors are just fine
self.TestMultiLineLint(
"""
Expand Down