Skip to content

Commit c8031b4

Browse files
authored
Fixup #14021 (Manual: document improved path matching for suppressions, file filters, exclusions) (danmar#7680)
1 parent 18d4d9f commit c8031b4

File tree

2 files changed

+82
-42
lines changed

2 files changed

+82
-42
lines changed

man/manual-premium.md

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,46 @@ need to use both approaches. Later chapters will describe this in more detail.
102102

103103
### Check files matching a given file filter
104104

105-
With `--file-filter=<str>` you can set a file filter and only those files matching the filter will be checked.
105+
With `--file-filter=<str>` you can configure file filter(s) and then only those files matching the filter will be checked.
106106

107-
For example: if you want to check only those files and folders starting from a subfolder src/ that start with "test"
108-
you have to type:
107+
For example, this command below means that `src/test1.cpp` and `src/test/file1.cpp` could be checked, but `src/file2.cpp` will not be checked:
109108

110109
cppcheck src/ --file-filter=src/test*
111110

112-
Cppcheck first collects all files in src/ and will apply the filter after that. So the filter must start with the given
113-
start folder.
111+
You can use `**`, `*` and `?` in the file filter pattern.
112+
`**`: matches zero or more characters, including path separators
113+
`*`: matches zero or more characters, excluding path separators
114+
`?`: matches any single character except path separators
114115

115-
### Excluding a file or folder from checking
116+
A common use case for `--file-filter` is to check a project, but only check certain files:
116117

117-
To exclude a file or folder, there are two options. The first option is to only provide the paths and files you want to
118-
check:
118+
cppcheck --project=compile_commands.json --file-filter=src/*.c
119119

120-
cppcheck src/a src/b
120+
Typically a `compile_commands.json` contains absolute paths. However no matter if `compile_commands.json` contains absolute paths or relative paths, the option `--file-filter=src/*.c` would mean that:
121+
* a file with relative path `test1.c` is not checked.
122+
* a file with relative path `src/test2.c` can be checked.
123+
* a file with relative path `src/test3.cpp` is not checked.
121124

122-
All files under src/a and src/b are then checked.
125+
### Excluding a file or folder from checking
123126

124-
The second option is to use -i, which specifies the files/paths to ignore. With this command no files in src/c are
125-
checked:
127+
The option `-i` specifies a pattern to files/folders to exclude. With this command no files in `src/c` are checked:
126128

127129
cppcheck -isrc/c src
128130

129-
This option is only valid when supplying an input directory. To ignore multiple directories supply the -i flag for each
130-
directory individually. The following command ignores both the src/b and src/c directories:
131+
The `-i` option is not used during preprocessing, it can't be used to exclude headers that are included.
132+
133+
You can use `**`, `*` and `?` in the pattern to specify excluded folders/files.
134+
`**`: matches zero or more characters, including path separators
135+
`*`: matches zero or more characters, excluding path separators
136+
`?`: matches any single character except path separators
137+
138+
A use case for `-i` is to check a project, but exclude certain files/folders:
131139

132-
cppcheck -isrc/b -isrc/c
140+
cppcheck --project=compile_commands.json -itest
141+
142+
Typically a `compile_commands.json` contains absolute paths. However no matter if `compile_commands.json` contains absolute paths or relative paths, the option `-itest` would mean that:
143+
* a file with relative path `test1.cpp` can be checked.
144+
* a file with relative path `test/somefile.cpp` is not checked
133145

134146
### Clang parser (experimental)
135147

@@ -474,19 +486,22 @@ The format for an error suppression is one of:
474486
[error id]:[filename2]
475487
[error id]
476488

477-
The `error id` is the id that you want to suppress. The id of a warning is shown in brackets in the normal cppcheck text output. The suppression `error id` may contain \* to match any sequence of tokens.
489+
The `error id` is the id that you want to suppress. The id of a warning is shown in brackets in the normal cppcheck text output.
490+
491+
The `error id` and `filename` patterns may contain `**`, `*` or `?`.
492+
`**`: matches zero or more characters, including path separators
493+
`*`: matches zero or more characters, excluding path separators
494+
`?`: matches any single character except path separators
478495

479-
The filename may include the wildcard characters \* or ?, which matches any sequence of characters or any single character respectively.
480-
It is recommended to use forward-slash `/` as path separator on all operating systems. The filename must match the filename in the reported warning exactly.
481-
For instance, if the warning contains a relative path, then the suppression must match that relative path.
496+
It is recommended to use forward-slash `/` in the filename pattern as path separator on all operating systems.
482497

483-
## Command line suppression
498+
### Command line suppression
484499

485500
The `--suppress=` command line option is used to specify suppressions on the command line. Example:
486501

487502
cppcheck --suppress=memleak:src/file1.cpp src/
488503

489-
## Suppressions in a file
504+
### Suppressions in a file
490505

491506
You can create a suppressions file for example as follows:
492507

@@ -517,6 +532,11 @@ You can specify suppressions in a XML file, for example as follows:
517532
</suppress>
518533
</suppressions>
519534

535+
The `id` and `fileName` patterns may contain `**`, `*` or `?`.
536+
`**`: matches zero or more characters, including path separators
537+
`*`: matches zero or more characters, excluding path separators
538+
`?`: matches any single character except path separators
539+
520540
The XML format is extensible and may be extended with further attributes in the future.
521541

522542
The usage of the suppressions file is as follows:

man/manual.md

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,46 @@ need to use both approaches. Later chapters will describe this in more detail.
103103

104104
### Check files matching a given file filter
105105

106-
With `--file-filter=<str>` you can set a file filter and only those files matching the filter will be checked.
106+
With `--file-filter=<str>` you can configure file filter(s) and then only those files matching the filter will be checked.
107107

108-
For example: if you want to check only those files and folders starting from a subfolder src/ that start with "test"
109-
you have to type:
108+
For example, this command below means that `src/test1.cpp` and `src/test/file1.cpp` could be checked, but `src/file2.cpp` will not be checked:
110109

111110
cppcheck src/ --file-filter=src/test*
112111

113-
Cppcheck first collects all files in src/ and will apply the filter after that. So the filter must start with the given
114-
start folder.
112+
You can use `**`, `*` and `?` in the file filter pattern.
113+
`**`: matches zero or more characters, including path separators
114+
`*`: matches zero or more characters, excluding path separators
115+
`?`: matches any single character except path separators
115116

116-
### Excluding a file or folder from checking
117+
A common use case for `--file-filter` is to check a project, but only check certain files:
117118

118-
To exclude a file or folder, there are two options. The first option is to only provide the paths and files you want to
119-
check:
119+
cppcheck --project=compile_commands.json --file-filter=src/*.c
120120

121-
cppcheck src/a src/b
121+
Typically a `compile_commands.json` contains absolute paths. However no matter if `compile_commands.json` contains absolute paths or relative paths, the option `--file-filter=src/*.c` would mean that:
122+
* a file with relative path `test1.c` is not checked.
123+
* a file with relative path `src/test2.c` can be checked.
124+
* a file with relative path `src/test3.cpp` is not checked.
122125

123-
All files under src/a and src/b are then checked.
126+
### Excluding a file or folder from checking
124127

125-
The second option is to use -i, which specifies the files/paths to ignore. With this command no files in src/c are
126-
checked:
128+
The option `-i` specifies a pattern to files/folders to exclude. With this command no files in `src/c` are checked:
127129

128130
cppcheck -isrc/c src
129131

130-
This option is only valid when supplying an input directory. To ignore multiple directories supply the -i flag for each
131-
directory individually. The following command ignores both the src/b and src/c directories:
132+
The `-i` option is not used during preprocessing, it can't be used to exclude headers that are included.
133+
134+
You can use `**`, `*` and `?` in the pattern to specify excluded folders/files.
135+
`**`: matches zero or more characters, including path separators
136+
`*`: matches zero or more characters, excluding path separators
137+
`?`: matches any single character except path separators
138+
139+
A use case for `-i` is to check a project, but exclude certain files/folders:
132140

133-
cppcheck -isrc/b -isrc/c
141+
cppcheck --project=compile_commands.json -itest
142+
143+
Typically a `compile_commands.json` contains absolute paths. However no matter if `compile_commands.json` contains absolute paths or relative paths, the option `-itest` would mean that:
144+
* a file with relative path `test1.cpp` can be checked.
145+
* a file with relative path `test/somefile.cpp` is not checked
134146

135147
### Clang parser (experimental)
136148

@@ -475,19 +487,22 @@ The format for an error suppression is one of:
475487
[error id]:[filename2]
476488
[error id]
477489

478-
The `error id` is the id that you want to suppress. The id of a warning is shown in brackets in the normal cppcheck text output. The suppression `error id` may contain \* to match any sequence of tokens.
490+
The `error id` is the id that you want to suppress. The id of a warning is shown in brackets in the normal cppcheck text output.
491+
492+
The `error id` and `filename` patterns may contain `**`, `*` or `?`.
493+
`**`: matches zero or more characters, including path separators
494+
`*`: matches zero or more characters, excluding path separators
495+
`?`: matches any single character except path separators
479496

480-
The filename may include the wildcard characters \* or ?, which matches any sequence of characters or any single character respectively.
481-
It is recommended to use forward-slash `/` as path separator on all operating systems. The filename must match the filename in the reported warning exactly.
482-
For instance, if the warning contains a relative path, then the suppression must match that relative path.
497+
It is recommended to use forward-slash `/` in the filename pattern as path separator on all operating systems.
483498

484-
## Command line suppression
499+
### Command line suppression
485500

486501
The `--suppress=` command line option is used to specify suppressions on the command line. Example:
487502

488503
cppcheck --suppress=memleak:src/file1.cpp src/
489504

490-
## Suppressions in a file
505+
### Suppressions in a file
491506

492507
You can create a suppressions file for example as follows:
493508

@@ -518,6 +533,11 @@ You can specify suppressions in a XML file, for example as follows:
518533
</suppress>
519534
</suppressions>
520535

536+
The `id` and `fileName` patterns may contain `**`, `*` or `?`.
537+
`**`: matches zero or more characters, including path separators
538+
`*`: matches zero or more characters, excluding path separators
539+
`?`: matches any single character except path separators
540+
521541
The XML format is extensible and may be extended with further attributes in the future.
522542

523543
The usage of the suppressions file is as follows:

0 commit comments

Comments
 (0)