Skip to content

Commit 256e7de

Browse files
committed
Allow suppressing all warnings (using *) for specified file or files
1 parent 1ec6a64 commit 256e7de

4 files changed

Lines changed: 45 additions & 10 deletions

File tree

lib/settings.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,18 @@ std::string Settings::Suppressions::addSuppression(const std::string &errorId, c
281281
{
282282
return "Failed to add suppression. No id.";
283283
}
284-
for (std::string::size_type pos = 0; pos < errorId.length(); ++pos)
284+
if (errorId != "*")
285285
{
286-
if (errorId[pos] < 0 || !std::isalnum(errorId[pos]))
286+
for (std::string::size_type pos = 0; pos < errorId.length(); ++pos)
287287
{
288-
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
289-
}
290-
if (pos == 0 && std::isdigit(errorId[pos]))
291-
{
292-
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
288+
if (errorId[pos] < 0 || !std::isalnum(errorId[pos]))
289+
{
290+
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
291+
}
292+
if (pos == 0 && std::isdigit(errorId[pos]))
293+
{
294+
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
295+
}
293296
}
294297
}
295298

@@ -298,6 +301,10 @@ std::string Settings::Suppressions::addSuppression(const std::string &errorId, c
298301

299302
bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line)
300303
{
304+
if (errorId != "unmatchedSuppression" && _suppressions.find("*") != _suppressions.end())
305+
if (_suppressions["*"].isSuppressed(file, line))
306+
return true;
307+
301308
if (_suppressions.find(errorId) == _suppressions.end())
302309
return false;
303310

@@ -306,6 +313,10 @@ bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std:
306313

307314
bool Settings::Suppressions::isSuppressedLocal(const std::string &errorId, const std::string &file, unsigned int line)
308315
{
316+
if (errorId != "unmatchedSuppression" && _suppressions.find("*") != _suppressions.end())
317+
if (_suppressions["*"].isSuppressedLocal(file, line))
318+
return true;
319+
309320
if (_suppressions.find(errorId) == _suppressions.end())
310321
return false;
311322

man/cppcheck.1.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Directory name is matched to all parts of the path.</para>
309309
<listitem>
310310
<para>Suppress a specific warning. The format of &lt;spec&gt; is: [error id]:[filename]:[line].
311311
The [filename] and [line] are optional.
312+
[error id] may be * to suppress all warnings (for a specified file or files).
312313
[filename] may contain the wildcard characters * or ?.</para>
313314
</listitem>
314315
</varlistentry>

man/manual.docbook

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ gui/test.cpp,16,error,mismatchAllocDealloc,Mismatching allocation and deallocati
384384
<para>The <literal>error id</literal> is the id that you want to suppress.
385385
The easiest way to get it is to use the <literal>--xml</literal> command
386386
line flag. Copy and paste the <literal>id</literal> string from the XML
387-
output.</para>
387+
output. This may be * to suppress all warnings (for a specified file or
388+
files).</para>
388389

389390
<para>The <literal>filename</literal> may include the wildcard characters
390391
* or ?, which match any sequence of characters or any single character

test/testsuppressions.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ class TestSuppressions : public TestFixture
4848
Settings settings;
4949
settings._inlineSuppressions = true;
5050
if (!suppression.empty())
51-
settings.nomsg.addSuppressionLine(suppression);
51+
{
52+
std::string r = settings.nomsg.addSuppressionLine(suppression);
53+
ASSERT_EQUALS("", r);
54+
}
5255

5356
CppCheck cppCheck(*this, true);
5457
cppCheck.settings(settings);
@@ -75,7 +78,10 @@ class TestSuppressions : public TestFixture
7578
settings._jobs = 1;
7679
settings._inlineSuppressions = true;
7780
if (!suppression.empty())
78-
settings.nomsg.addSuppressionLine(suppression);
81+
{
82+
std::string r = settings.nomsg.addSuppressionLine(suppression);
83+
ASSERT_EQUALS("", r);
84+
}
7985
ThreadExecutor executor(filenames, settings, *this);
8086
for (unsigned int i = 0; i < filenames.size(); ++i)
8187
executor.addFileContent(filenames[i], code);
@@ -147,6 +153,22 @@ class TestSuppressions : public TestFixture
147153
"uninitvar:test.cpp");
148154
ASSERT_EQUALS("[test.cpp]: (information) Unmatched suppression: uninitvar\n", errout.str());
149155

156+
// suppress all for this file only
157+
(this->*check)("void f() {\n"
158+
" int a;\n"
159+
" a++;\n"
160+
"}\n",
161+
"*:test.cpp");
162+
ASSERT_EQUALS("", errout.str());
163+
164+
// suppress all for this file only, without error present
165+
(this->*check)("void f() {\n"
166+
" int a;\n"
167+
" b++;\n"
168+
"}\n",
169+
"*:test.cpp");
170+
ASSERT_EQUALS("[test.cpp]: (information) Unmatched suppression: *\n", errout.str());
171+
150172
// suppress uninitvar for this file and line
151173
(this->*check)("void f() {\n"
152174
" int a;\n"

0 commit comments

Comments
 (0)