Skip to content

Commit

Permalink
Support relative includes through the parent directory
Browse files Browse the repository at this point in the history
  • Loading branch information
martis42 committed Feb 4, 2023
1 parent a31db72 commit 4ecd0bf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Examples for this can be seen at the [implementation_deps test cases](test/aspec
- Defines are ignored.
No matter if they are defined directly inside the header under inspection, headers from the dependencies or injected
through the `defines = [...]` attribute of the `cc_` rules.
- Include statements utilizing `..` to go up the directory tree are not resolved.
- Include statements utilizing `..` are not recognized if they are used on virtual or system include paths.

## Applying automatic fixes

Expand Down
13 changes: 5 additions & 8 deletions src/analyze_includes/evaluate_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ def _check_for_invalid_includes(
# Might be a relative include
curr_dir = inc.file.parent
for hf in all_header_files:
try:
rel_path = Path(hf.path).relative_to(curr_dir)
if rel_path == Path(inc.include):
legal = True
hf.usage.update(usage)
break
except ValueError:
pass
path_matching_include_statement = (curr_dir / inc.include).resolve()
if path_matching_include_statement == Path(hf.path).resolve():
legal = True
hf.usage.update(usage)
break
if not legal:
invalid_includes.append(inc)
return invalid_includes
Expand Down
2 changes: 2 additions & 0 deletions src/analyze_includes/test/evaluate_includes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def test_success_for_internal_relative_includes_with_nested_structure(self):
public_includes=[
Include(file=Path("nested/dir/foo.h"), include="bar.h"),
Include(file=Path("nested/dir/foo.h"), include="sub/tick.h"),
Include(file=Path("nested/dir/foo.h"), include="../../nested/dir/sub/tick.h"),
Include(file=Path("nested/dir/sub/tick.h"), include="tock.h"),
],
private_includes=[],
Expand All @@ -325,6 +326,7 @@ def test_success_for_relative_includes_to_dependency(self):
public_includes=[
Include(file=Path("foo.h"), include="bar/dir/bar.h"),
Include(file=Path("bar/dir/bar.h"), include="sub/tick.h"),
Include(file=Path("bar/dir/bar.h"), include="../dir/sub/tick.h"),
Include(file=Path("bar/dir/sub/tick.h"), include="tock.h"),
],
private_includes=[],
Expand Down
4 changes: 4 additions & 0 deletions test/aspect/relative_includes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cc_library(
"normal_include.h",
"some/sub/dir/bar.h",
"some/sub/dir/foo.h",
"some/sub/dir2/baz.h",
],
)

Expand All @@ -14,6 +15,7 @@ cc_library(
hdrs = [
"some/sub/dir/bar.h",
"some/sub/dir/foo.h",
"some/sub/dir2/baz.h",
"system_include.h",
],
includes = ["some"],
Expand All @@ -25,6 +27,7 @@ cc_library(
hdrs = [
"some/sub/dir/bar.h",
"some/sub/dir/foo.h",
"some/sub/dir2/baz.h",
"some/virtual_strip.h",
],
strip_include_prefix = "some",
Expand All @@ -36,6 +39,7 @@ cc_library(
hdrs = [
"some/sub/dir/bar.h",
"some/sub/dir/foo.h",
"some/sub/dir2/baz.h",
"virtual_prefix.h",
],
include_prefix = "custom/prefix",
Expand Down
9 changes: 8 additions & 1 deletion test/aspect/relative_includes/some/sub/dir/foo.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#ifndef FOO_H
#define FOO_H

// normal relative include to file in same directory
#include "bar.h"
// Enter parent directory and descend again into current directory
#include "../dir/bar.h"
// Relative include to a file in another directory
#include "../dir2/baz.h"
// Example for complex relative path
#include "../../sub/dir2/../dir/bar.h"

inline int doFoo() {
return doBar() + 13;
return doBar() + doBaz();
}

#endif
8 changes: 8 additions & 0 deletions test/aspect/relative_includes/some/sub/dir2/baz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef BAZ_H
#define BAZ_H

inline int doBaz() {
return 1;
}

#endif
3 changes: 2 additions & 1 deletion test/aspect/relative_includes/use_normal_include.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// relative includes to headers from dependencies
#include "normal_include.h"
#include "some/sub/dir/foo.h"
#include "some/sub/dir/../dir2/baz.h"

int main() {
return useNormalInclude() + doBar();
return useNormalInclude() + doBar() + doBaz();
}
3 changes: 2 additions & 1 deletion test/aspect/relative_includes/use_system_include.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// relatively including header from own target
#include "system_include.h"
#include "some/sub/dir/foo.h"
#include "some/sub/dir/../dir2/baz.h"
// include from virtually prefixed path
#include <sub/dir/bar.h>

int main() {
return useSystemInclude() + doBar();
return useSystemInclude() + doBar() + doBaz();
}
3 changes: 2 additions & 1 deletion test/aspect/relative_includes/use_virtual_prefix.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// relatively including header from own target
#include "virtual_prefix.h"
#include "some/sub/dir/bar.h"
#include "some/sub/dir/../dir2/baz.h"
// include from virtually prefixed path
#include "custom/prefix/some/sub/dir/foo.h"

int main() {
return useVirtualPrefix() + doBar();
return useVirtualPrefix() + doBar() + doBaz();
}
3 changes: 2 additions & 1 deletion test/aspect/relative_includes/use_virtual_strip.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// relatively including header from own target
#include "some/virtual_strip.h"
#include "some/sub/dir/bar.h"
#include "some/sub/dir/../dir2/baz.h"
// include from virtually stripped path
#include "sub/dir/foo.h"

int main() {
return useVirtualStrip() + doBar();
return useVirtualStrip() + doBar() + doBaz();
}

0 comments on commit 4ecd0bf

Please sign in to comment.