Skip to content

Commit 41d53da

Browse files
committed
Improved docs on includeGuardRoots and improved include guard generation
Made include guard generation use longest matching root and fixed duplicated underscores in include guards.
1 parent 045d863 commit 41d53da

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

.styleguide

+2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ licenseUpdateExclude {
2020
Excluded\.h$
2121
}
2222

23+
# Ordered this way to ensure tests find longest match
2324
includeGuardRoots {
2425
wpiformat/
26+
wpiformat/Test/
2527
}
2628

2729
# Used by unit tests

wpiformat/README.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ File names matching regexes in the group ``licenseUpdateExclude`` will be skippe
4545

4646
Empty config groups can be omitted. Directory separators must be "/", not "\". During processing, they will be replaced internally with an os.sep that is automatically escaped for regexes.
4747

48-
Valid include guard patterns use capital letters, start with the repository name, include the path to the file and the file name itself, and have directory separators and hyphens replaced with underscores. The path to the file starts from the repository root by default. Other paths, such as include directories, can be specified in the group ``includeGuardRoots``. If a path matches, that string will be truncated from the include guard pattern.
48+
Valid include guard patterns use capital letters, start with the repository name, include the path to the file and the file name itself, have directory separators and hyphens replaced with underscores, and have a trailing underscore. The path to the file starts from the repository root by default. Other paths, such as include directories, can be specified in the group ``includeGuardRoots``. If a path matches, that string will be truncated from the include guard pattern.
49+
50+
For example, given a file at `allwpilib/src/main/native/include/wpiutil/support/ConcurrentQueue.h` and an include path of `src/main/native/include/`, the resulting include guard would be `ALLWPILIB_WPIUTIL_SUPPORT_CONCURRENTQUEUE_H_`.
4951

5052
The group ``repoRootNameOverride`` allows one to override the repository name used in include guards. This is useful for giving subprojects within one repository different repository roots in their include guards. Only specify one name in this group because subsequent names will be ignored.
5153

wpiformat/test/test_includeguard.py

+13
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,17 @@ def test_includeguard():
6666
os.linesep + \
6767
"#endif // STYLEGUIDE_TEST_H_" + os.linesep, True, True)
6868

69+
# Ensure leading underscores are removed (this occurs if the user doesn't
70+
# include a trailing "/" in the include guard root)
71+
test.add_input("./Test/Test.h",
72+
"#ifndef STYLEGUIDE_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
73+
"#define STYLEGUIDE_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
74+
os.linesep + \
75+
"#endif // STYLEGUIDE_WPIFORMAT_TEST_TEST_H_" + os.linesep)
76+
test.add_output(
77+
"#ifndef STYLEGUIDE_TEST_H_" + os.linesep + \
78+
"#define STYLEGUIDE_TEST_H_" + os.linesep + \
79+
os.linesep + \
80+
"#endif // STYLEGUIDE_TEST_H_" + os.linesep, True, True)
81+
6982
test.run(OutputType.FILE)

wpiformat/wpiformat/includeguard.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ def make_include_guard(self, config_file, name):
9090
include_roots = config_file.group("includeGuardRoots")
9191

9292
if include_roots:
93+
prefix = ""
9394
for include_root in include_roots:
94-
if guard_root.startswith(include_root):
95-
guard_path += guard_root[len(include_root):]
96-
return regex.sub("[^a-zA-Z0-9]", "_",
97-
guard_path).upper() + "_"
95+
if guard_root.startswith(
96+
include_root) and len(include_root) > len(prefix):
97+
prefix = include_root
98+
guard_path += guard_root[len(prefix):]
99+
return (regex.sub("[^a-zA-Z0-9]", "_", guard_path).upper() +
100+
"_").lstrip("_")
98101

99102
# No include guard roots matched, so append full name
100103
guard_path += guard_root

0 commit comments

Comments
 (0)