|
2 | 2 | # |
3 | 3 | # Tweaks a compile_commands.json file: for every build command that has a |
4 | 4 | # --sysroot argument, each existing -isystem argument gets a matching extra |
5 | | -# -isystem argument pointing into the sysroot. This is useful when a |
6 | | -# compiler resolves -isystem paths relative to --sysroot internally (as |
7 | | -# part of its built-in search path handling) but a tool consuming |
8 | | -# compile_commands.json (such as Cppcheck) does not, so the sysroot-relative |
9 | | -# path needs to be spelled out explicitly. |
| 5 | +# -isystem argument pointing into the sysroot, and the --sysroot argument |
| 6 | +# itself is then removed. This is useful when a compiler resolves -isystem |
| 7 | +# paths relative to --sysroot internally (as part of its built-in search |
| 8 | +# path handling) but a tool consuming compile_commands.json (such as |
| 9 | +# Cppcheck) does not, so the sysroot-relative path needs to be spelled out |
| 10 | +# explicitly instead. |
10 | 11 | # |
11 | 12 | # Example: |
12 | 13 | # --sysroot /a/b -isystem /opt/x |
13 | 14 | # => |
14 | | -# --sysroot /a/b -isystem /opt/x -isystem /a/b/opt/x |
| 15 | +# -isystem /opt/x -isystem /a/b/opt/x |
15 | 16 | # |
16 | 17 | # Optionally, --isystem-to-i converts -isystem arguments to -I, which can be |
17 | 18 | # useful since Cppcheck otherwise treats -isystem headers as "system" |
@@ -55,6 +56,23 @@ def join_sysroot(sysroot, path): |
55 | 56 | return sysroot.rstrip('/') + '/' + path.lstrip('/') |
56 | 57 |
|
57 | 58 |
|
| 59 | +def remove_sysroot_arg(tokens): |
| 60 | + result = [] |
| 61 | + i = 0 |
| 62 | + n = len(tokens) |
| 63 | + while i < n: |
| 64 | + tok = tokens[i] |
| 65 | + if tok == '--sysroot' and i + 1 < n: |
| 66 | + i += 2 |
| 67 | + continue |
| 68 | + if tok.startswith('--sysroot='): |
| 69 | + i += 1 |
| 70 | + continue |
| 71 | + result.append(tok) |
| 72 | + i += 1 |
| 73 | + return result |
| 74 | + |
| 75 | + |
58 | 76 | def add_isystem_sysroot(tokens, sysroot): |
59 | 77 | result = [] |
60 | 78 | i = 0 |
@@ -153,6 +171,7 @@ def tweak_entry(entry, isystem_to_i, exclude_folders, remove_include_path): |
153 | 171 | sysroot = find_sysroot(tokens) |
154 | 172 | if sysroot is not None: |
155 | 173 | tokens = add_isystem_sysroot(tokens, sysroot) |
| 174 | + tokens = remove_sysroot_arg(tokens) |
156 | 175 | changed = True |
157 | 176 |
|
158 | 177 | if isystem_to_i: |
|
0 commit comments