Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: lint for inline headers #21521

Closed
wants to merge 4 commits into from
Closed
Changes from 2 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
21 changes: 21 additions & 0 deletions tools/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,25 @@ def CheckForBadCharacters(filename, lines, error):
error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')


def CheckInlineHeader(filename, include_state, error):
"""Logs an error if both a header and its inline variant are included."""

# Iterate over all headers, looking for any inline headers
for section_list in include_state.include_list:
for header in section_list:
# Proceed further for only inline headers
m = Match(r'^(.+)-inl.h$', header[0])
if m:
name = '%s.h' % m.group(1)
# Look for the corresponding header
for sl in include_state.include_list:
for h in section_list:
if h[0] == name:
err = '%s includes both %s and %s' % (filename, header[0], h[0])
error(filename, h[1], 'build/include', 5, err)
print err
Copy link
Member

Choose a reason for hiding this comment

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

More succinct of way of doing it:

all_headers = dict(item for sublist in include_state.include_list
                   for item in sublist)
bad_headers = set('%s.h' % name[:-6] for name in all_headers.keys()
                  if name.endswith('-inl.h'))
bad_headers &= set(all_headers.keys())  # compute intersection

for name in bad_headers:
  # lineno = all_headers[name]

It's also slightly more idiomatic vis-a-vis other include checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnoordhuis okay, will try this. That said, I'm more interested at this point to find out what's wrong with my solution? It might not be idiomatic, but it should work. Why does error not work?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, is that your question? :-)

I assume you're testing it from the command line like this: python tools/cpplint.py src/*.cc?

Look for _DEFAULT_FILTERS: build/include is off by default. Pass --filter=build/include to test it, or --filter=+build/include to enable it in addition to the other rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnoordhuis I was! That helped.



def CheckForNewlineAtEOF(filename, lines, error):
"""Logs an error if there is no newline char at the end of the file.

Expand Down Expand Up @@ -5861,6 +5880,8 @@ def ProcessFileData(filename, file_extension, lines, error,

CheckForNewlineAtEOF(filename, lines, error)

CheckInlineHeader(filename, include_state, error)

def ProcessConfigOverrides(filename):
""" Loads the configuration files and processes the config overrides.

Expand Down