Skip to content

Commit

Permalink
iwyu: pass source root into fix_includes
Browse files Browse the repository at this point in the history
fix_includes tries to identify the "main compilation unit header" for
each file using some heuristics. For example, "foo-test.cc" is
associated with "foo.h".

This was previously broken due to the fact that the tool runs from the
build/ directory instead of the source directory, so it saw the CC file
as "../../src/kudu/.../foo.cc" while it saw the header file as
"kudu/.../foo.h".

This adds a --source_root parameter to fix_includes so that the source
files can be relativized to this path prior to fixing.

Change-Id: Iac9f0b47b9a3c8180e178ed59863d33840434433
Reviewed-on: http://gerrit.cloudera.org:8080/10159
Tested-by: Todd Lipcon <[email protected]>
Reviewed-by: Alexey Serbin <[email protected]>
  • Loading branch information
toddlipcon committed Apr 24, 2018
1 parent a6280bc commit 138284e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
8 changes: 6 additions & 2 deletions build-support/iwyu.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def crash(output):

try:
output = check_output(cmdline, env=env, stderr=subprocess.STDOUT)
if '\nFATAL ERROR: ' in output or _RE_CLANG_ERROR.search(output):
if '\nFATAL ERROR: ' in output or \
'Assertion failed: ' in output or \
_RE_CLANG_ERROR.search(output):
crash(output)
return output
except subprocess.CalledProcessError, e:
Expand All @@ -161,7 +163,9 @@ def _get_thirdparty_include_dirs():


def _get_fixer_flags(flags):
args = ['--quiet', '--nosafe_headers']
args = ['--quiet',
'--nosafe_headers',
'--source_root=%s' % os.path.join(ROOT, 'src')]
if flags.dry_run:
args.append("--dry_run")
for d in _get_thirdparty_include_dirs():
Expand Down
30 changes: 18 additions & 12 deletions build-support/iwyu/fix_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,31 +1503,32 @@ def _IsSameProject(line_info, edited_file, project):
return (included_root and edited_root and included_root == edited_root)


def _GetLineKind(file_line, filename, separate_project_includes, thirdparty_include_dirs):
def _GetLineKind(file_line, filename, flags):
"""Given a file_line + file being edited, return best *_KIND value or None.
Arguments:
file_line: the LineInfo structure to be analyzed
filename: the file which contains the line to be analyzed
separate_project_includes: see 'project' parameter of '_IsSameProject()'
thirdparty_include_dirs: see help output for the corresponding flag
flags: the program flags. Uses 'separate_project_includes' and 'thirdparty_include_dirs'
"""
if flags.source_root:
filename = os.path.relpath(filename, flags.source_root)
line_without_coments = _COMMENT_RE.sub('', file_line.line)
if file_line.deleted:
return None
elif _IsMainCUInclude(file_line, filename):
return _MAIN_CU_INCLUDE_KIND
elif _IsSystemInclude(file_line) and \
not _IsThirdpartyInclude(file_line, thirdparty_include_dirs):
not _IsThirdpartyInclude(file_line, flags.thirdparty_include_dirs):
if '.' in line_without_coments:
# e.g. <string.h>
return _C_SYSTEM_INCLUDE_KIND
else:
# e.g. <string>
return _CXX_SYSTEM_INCLUDE_KIND
elif file_line.type == _INCLUDE_RE:
if (separate_project_includes and
_IsSameProject(file_line, filename, separate_project_includes)):
if (flags.separate_project_includes and
_IsSameProject(file_line, filename, flags.separate_project_includes)):
return _PROJECT_INCLUDE_KIND
return _NONSYSTEM_INCLUDE_KIND
elif file_line.type == _FORWARD_DECLARE_RE:
Expand Down Expand Up @@ -1606,9 +1607,8 @@ class Bang;
last_reorder_spans = {}
for reorder_span in good_reorder_spans:
for line_number in range(*reorder_span):
line_kind = _GetLineKind(file_lines[line_number], filename,
flags.separate_project_includes,
flags.thirdparty_include_dirs)
line_kind = _GetLineKind(file_lines[line_number], filename, flags)

# Ignore forward-declares that come after 'contentful' code; we
# never want to insert new forward-declares there.
if (line_kind == _FORWARD_DECLARE_KIND and
Expand Down Expand Up @@ -1787,9 +1787,7 @@ def _DecoratedMoveSpanLines(iwyu_record, file_lines, move_span_lines, flags):
sort_key = sort_key.replace('-inl.h', '_inl.h')

# Next figure out the kind.
kind = _GetLineKind(firstline, iwyu_record.filename,
flags.separate_project_includes,
flags.thirdparty_include_dirs)
kind = _GetLineKind(firstline, iwyu_record.filename, flags)

# All we're left to do is the reorder-span we're in. Hopefully it's easy.
reorder_span = firstline.reorder_span
Expand Down Expand Up @@ -2270,6 +2268,14 @@ def ParseArgs(args):
help=('Any includes which are found in <dir> are considered '
'"other-project" includes rather than "system" includes.'))

parser.add_option('--source_root', type='str',
metavar="dir",
help=('The path to the root of the source tree. When processing '
'changes to a file /project/src/bar/baz.cc, setting '
'--source_root to /project/src will ensure that an include '
'of "bar/baz.h" is properly identified as the corresponding '
'main-compilation-unit include.'))

parser.add_option('--invoking_command_line', default=None,
help=('Internal flag used by iwyu.py, It should be the'
' command line used to invoke iwyu.py'))
Expand Down

0 comments on commit 138284e

Please sign in to comment.