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
22 changes: 19 additions & 3 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@
# Match string that indicates we're working on a Linux Kernel file.
_SEARCH_KERNEL_FILE = re.compile(r"\b(?:LINT_KERNEL_FILE)")

# Operator sequences beginning with '<' (used to strip before counting angle brackets).
# Longer sequences must come first so '<<=' is not partially matched as '<<'.
_LANGLE_OPS_RE = re.compile(r"<<=|<=|<<")
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.

What's "LANGLE"?

If this variable is necessary, it should not be all the way up here for more global things; instead should be before the function and named _RE_PATTERN_SOMETHING_DESCRIPTIVE .


# Commands for sed to fix the problem
_SED_FIXUPS = {
"Remove spaces around =": r"s/ = /=/",
Expand Down Expand Up @@ -3972,15 +3976,27 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum, nesting_state,
constructor_args = explicit_constructor_match.group(2).split(",")
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.

Why not make one regex replacement here instead of needing to define a variable?


# collapse arguments so that commas in template parameter lists and function
# argument parameter lists don't split arguments in two
# argument parameter lists don't split arguments in two.
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.

If you're making this a sentence, please also capitalize.

# Strip operator sequences that begin with '<' before counting angle
# brackets, to avoid confusing operators with unmatched template
Comment on lines +3980 to +3981
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.

could be terser

# brackets. Longer sequences must come first so that '<<=' is not
# partially matched as '<<' leaving a stray '='.
# Note: '>>' and '>=' are intentionally left alone because extra '>'
# characters do not trigger the joining loop (condition is
# count('<') > count('>')), and stripping '>>' would break nested
# template types such as vector<vector<int>>.
Comment on lines +3982 to +3987
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.

These important comments about the design of the compiled regex should not be separated from the compiled regex.

i = 0
while i < len(constructor_args):
constructor_arg = constructor_args[i]
while constructor_arg.count("<") > constructor_arg.count(">") or constructor_arg.count(
cleaned_arg = _LANGLE_OPS_RE.sub("", constructor_arg)
while cleaned_arg.count("<") > cleaned_arg.count(">") or cleaned_arg.count(
"("
) > constructor_arg.count(")"):
) > cleaned_arg.count(")"):
if i + 1 >= len(constructor_args):
break
constructor_arg += "," + constructor_args[i + 1]
del constructor_args[i + 1]
cleaned_arg = _LANGLE_OPS_RE.sub("", constructor_arg)
constructor_args[i] = constructor_arg
i += 1

Expand Down
26 changes: 26 additions & 0 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,32 @@ class Foo {
"""
class Foo {
explicit Foo(int f, int g);
};""",
"",
)
# No crash or warning for constructors with '<'-containing operators
# in default parameter values (regression test for issue #223).
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.

Suggested change
# in default parameter values (regression test for issue #223).
# `<`-containing operators in constructors are not confused with improper templates

# left shift <<
self.TestMultiLineLint(
"""
class A {
A(int b, int c, int a = 1 << 1) {}
};""",
"",
)
# compound left-shift-assign <<=
self.TestMultiLineLint(
"""
class A {
A(int b, int c, int a = (x <<= 1)) {}
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.

Since the error involves comma-joining logic, we should rotate the parameter order in tests, even though I suspect doing so won't catch any real bugs.

};""",
"",
)
# less-or-equal <=
self.TestMultiLineLint(
"""
class A {
A(int b, int c, int a = b <= c ? 1 : 0) {}
};""",
"",
)
Expand Down