Skip to content

Commit 9ac4623

Browse files
authored
ignore all other suppressions when UNUSEDFUNCTION_ONLY hack is active / selfcheck.yml: enabled information messages (#7938)
1 parent 5f34b1c commit 9ac4623

18 files changed

+59
-62
lines changed

.github/workflows/selfcheck.yml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ permissions:
1616
contents: read
1717

1818
jobs:
19-
# TODO: enable information
2019
build:
2120

2221
runs-on: ubuntu-22.04
@@ -77,11 +76,10 @@ jobs:
7776
# make sure the auto-generated GUI dependencies exist
7877
make -C cmake.output gui-build-deps
7978
80-
# TODO: find a way to report unmatched suppressions without need to add information checks
8179
- name: Self check (unusedFunction)
8280
if: false # TODO: fails with preprocessorErrorDirective - see #10667
8381
run: |
84-
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction --exception-handling -rp=. --project=cmake.output/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
82+
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
8583
env:
8684
DISABLE_VALUEFLOW: 1
8785
UNUSEDFUNCTION_ONLY: 1
@@ -103,10 +101,9 @@ jobs:
103101
# make sure the auto-generated GUI dependencies exist
104102
make -C cmake.output.notest gui-build-deps
105103
106-
# TODO: find a way to report unmatched suppressions without need to add information checks
107104
- name: Self check (unusedFunction / no test)
108105
run: |
109-
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction --exception-handling -rp=. --project=cmake.output.notest/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
106+
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
110107
env:
111108
DISABLE_VALUEFLOW: 1
112109
UNUSEDFUNCTION_ONLY: 1
@@ -122,10 +119,10 @@ jobs:
122119
# make sure the precompiled headers exist
123120
make -C cmake.output.notest_nogui lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx
124121
125-
# TODO: find a way to report unmatched suppressions without need to add information checks
126122
- name: Self check (unusedFunction / no test / no gui)
127123
run: |
128-
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction --exception-handling -rp=. --project=cmake.output.notest_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
124+
supprs="--suppress=unusedFunction:lib/errorlogger.h:193 --suppress=unusedFunction:lib/importproject.cpp:1508 --suppress=unusedFunction:lib/importproject.cpp:1532"
125+
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr $supprs
129126
env:
130127
DISABLE_VALUEFLOW: 1
131128
UNUSEDFUNCTION_ONLY: 1
@@ -145,11 +142,10 @@ jobs:
145142
# make sure the auto-generated GUI dependencies exist
146143
make -C cmake.output.notest_nocli gui-build-deps
147144
148-
# TODO: find a way to report unmatched suppressions without need to add information checks
149145
- name: Self check (unusedFunction / no test / no cli)
150146
if: false # TODO: the findings are currently too intrusive
151147
run: |
152-
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction --exception-handling -rp=. --project=cmake.output.notest_nocli/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
148+
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nocli/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
153149
env:
154150
DISABLE_VALUEFLOW: 1
155151
UNUSEDFUNCTION_ONLY: 1
@@ -165,11 +161,10 @@ jobs:
165161
# make sure the precompiled headers exist
166162
make -C cmake.output.notest_nocli_nogui lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx
167163
168-
# TODO: find a way to report unmatched suppressions without need to add information checks
169164
- name: Self check (unusedFunction / no test / no cli / no gui)
170165
if: false # TODO: the findings are currently too intrusive
171166
run: |
172-
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction --exception-handling -rp=. --project=cmake.output.notest_nocli_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
167+
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib --library=qt -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nocli_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr
173168
env:
174169
DISABLE_VALUEFLOW: 1
175170
UNUSEDFUNCTION_ONLY: 1
@@ -193,11 +188,10 @@ jobs:
193188
# make sure the auto-generated GUI dependencies exist
194189
make -C cmake.output.corpus gui-build-deps
195190
196-
# TODO: find a way to report unmatched suppressions without need to add information checks
197191
- name: Self check (unusedFunction / corpus / no test / callgrind)
198192
run: |
199193
# TODO: fix -rp so the suppressions actually work
200-
valgrind --tool=callgrind ./cppcheck --template=selfcheck --error-exitcode=0 --library=cppcheck-lib --library=qt -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction --exception-handling -rp=. --project=cmake.output.corpus/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr 2>callgrind.log || (cat callgrind.log && false)
194+
valgrind --tool=callgrind ./cppcheck --template=selfcheck --error-exitcode=0 --library=cppcheck-lib --library=qt -D__GNUC__ -DQT_VERSION=0x060000 -DQ_MOC_OUTPUT_REVISION=69 -DQT_CHARTS_LIB -DQT_MOC_HAS_STRINGDATA --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.corpus/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr 2>callgrind.log || (cat callgrind.log && false)
201195
cat callgrind.log
202196
callgrind_annotate --auto=no > callgrind.annotated.log
203197
head -50 callgrind.annotated.log

.selfcheck_unused_suppressions

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
# should not be reported - see #13387
2+
checkersReport
3+
14
# we are not using all methods of their interfaces
25
unusedFunction:externals/*/*
36

4-
# usage is disabled
5-
unusedFunction:lib/symboldatabase.cpp
6-
77
# Q_OBJECT functions which are not called in our code
88
unusedFunction:cmake.output.notest/gui/cppcheck-gui_autogen/*/moc_aboutdialog.cpp

cli/cppcheckexecutor.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "errorlogger.h"
3333
#include "errortypes.h"
3434
#include "filesettings.h"
35-
#include "json.h"
3635
#include "path.h"
3736
#include "sarifreport.h"
3837
#include "settings.h"
@@ -50,7 +49,6 @@
5049

5150
#include <algorithm>
5251
#include <cassert>
53-
#include <cstdint>
5452
#include <cstdio>
5553
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
5654
#include <ctime>
@@ -342,27 +340,39 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
342340
assert(!(!files.empty() && !fileSettings.empty()));
343341

344342
// bail out if there is a suppression of unmatchedSuppression which matches any file
345-
const auto suppr = suppressions.getSuppressions();
343+
auto suppr = suppressions.getSuppressions();
346344
if (std::any_of(suppr.cbegin(), suppr.cend(), [](const SuppressionList::Suppression& s) {
347345
return s.errorId == "unmatchedSuppression" && (s.fileName.empty() || s.fileName == "*") && s.lineNumber == SuppressionList::Suppression::NO_LINE;
348346
}))
349347
return false;
350348

349+
SuppressionList supprlist;
350+
351+
const bool doUnusedFunctionOnly = Settings::unusedFunctionOnly();
352+
// ignore all other suppressions if we use the unusedFunction hack
353+
for (auto&& s : suppr)
354+
{
355+
// TODO: checkersReport should not be reported - see #13387
356+
if (doUnusedFunctionOnly && s.errorId != "unusedFunction" && s.errorId != "checkersReport")
357+
continue;
358+
supprlist.addSuppression(std::move(s));
359+
}
360+
351361
bool err = false;
352362

353363
for (auto i = files.cbegin(); i != files.cend(); ++i) {
354-
err |= ::reportUnmatchedSuppressions(suppressions.getUnmatchedLocalSuppressions(*i), errorLogger, settings.unmatchedSuppressionFilters);
364+
err |= ::reportUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(*i), errorLogger, settings.unmatchedSuppressionFilters);
355365
}
356366

357367
for (auto i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) {
358-
err |= ::reportUnmatchedSuppressions(suppressions.getUnmatchedLocalSuppressions(i->file), errorLogger, settings.unmatchedSuppressionFilters);
368+
err |= ::reportUnmatchedSuppressions(supprlist.getUnmatchedLocalSuppressions(i->file), errorLogger, settings.unmatchedSuppressionFilters);
359369
}
360370

361371
if (settings.inlineSuppressions) {
362-
err |= ::reportUnmatchedSuppressions(suppressions.getUnmatchedInlineSuppressions(), errorLogger, settings.unmatchedSuppressionFilters);
372+
err |= ::reportUnmatchedSuppressions(supprlist.getUnmatchedInlineSuppressions(), errorLogger, settings.unmatchedSuppressionFilters);
363373
}
364374

365-
err |= ::reportUnmatchedSuppressions(suppressions.getUnmatchedGlobalSuppressions(), errorLogger, settings.unmatchedSuppressionFilters);
375+
err |= ::reportUnmatchedSuppressions(supprlist.getUnmatchedGlobalSuppressions(), errorLogger, settings.unmatchedSuppressionFilters);
366376
return err;
367377
}
368378

gui/checkthread.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ void CheckThread::analyseWholeProgram(const std::list<FileWithDetails> &files, c
124124
start();
125125
}
126126

127-
// cppcheck-suppress unusedFunction - TODO: false positive
128127
void CheckThread::run()
129128
{
130129
mState = Running;

gui/projectfiledialog.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ static const std::array<Platform::Type, 6> builtinPlatforms = {
9090
Platform::Type::Unix64
9191
};
9292

93+
static std::string suppressionAsText(const SuppressionList::Suppression& s)
94+
{
95+
std::string ret;
96+
if (!s.errorId.empty())
97+
ret = s.errorId;
98+
if (!s.fileName.empty())
99+
ret += " fileName=" + s.fileName;
100+
if (s.lineNumber != SuppressionList::Suppression::NO_LINE)
101+
ret += " lineNumber=" + std::to_string(s.lineNumber);
102+
if (!s.symbolName.empty())
103+
ret += " symbolName=" + s.symbolName;
104+
if (s.hash > 0)
105+
ret += " hash=" + std::to_string(s.hash);
106+
if (startsWith(ret," "))
107+
return ret.substr(1);
108+
return ret;
109+
}
110+
93111
QStringList ProjectFileDialog::getProjectConfigs(const QString &fileName)
94112
{
95113
if (!fileName.endsWith(".sln") && !fileName.endsWith(".vcxproj"))
@@ -808,7 +826,7 @@ void ProjectFileDialog::setLibraries(const QStringList &libraries)
808826
void ProjectFileDialog::addSingleSuppression(const SuppressionList::Suppression &suppression)
809827
{
810828
mSuppressions += suppression;
811-
mUI->mListSuppressions->addItem(QString::fromStdString(suppression.getText()));
829+
mUI->mListSuppressions->addItem(QString::fromStdString(suppressionAsText(suppression)));
812830
}
813831

814832
void ProjectFileDialog::setSuppressions(const QList<SuppressionList::Suppression> &suppressions)
@@ -949,7 +967,7 @@ int ProjectFileDialog::getSuppressionIndex(const QString &shortText) const
949967
{
950968
const std::string s = shortText.toStdString();
951969
auto it = std::find_if(mSuppressions.cbegin(), mSuppressions.cend(), [&](const SuppressionList::Suppression& sup) {
952-
return sup.getText() == s;
970+
return suppressionAsText(sup) == s;
953971
});
954972
return it == mSuppressions.cend() ? -1 : static_cast<int>(std::distance(mSuppressions.cbegin(), it));
955973
}

lib/cppcheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
13401340

13411341
// TODO: this should actually be the behavior if only "--enable=unusedFunction" is specified - see #10648
13421342
// TODO: log message when this is active?
1343-
const char* unusedFunctionOnly = std::getenv("UNUSEDFUNCTION_ONLY");
1344-
const bool doUnusedFunctionOnly = unusedFunctionOnly && (std::strcmp(unusedFunctionOnly, "1") == 0);
1343+
const bool doUnusedFunctionOnly = Settings::unusedFunctionOnly();
13451344

13461345
if (!doUnusedFunctionOnly) {
13471346
const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;

lib/errorlogger.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ class CPPCHECKLIB ErrorMessage {
190190
}
191191

192192
/** Verbose message (may be the same as the short message) */
193-
// cppcheck-suppress unusedFunction - used by GUI only
194193
const std::string &verboseMessage() const {
195194
return mVerboseMessage;
196195
}

lib/importproject.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,6 @@ void ImportProject::selectOneVsConfig(Platform::Type platform)
15051505
}
15061506
}
15071507

1508-
// cppcheck-suppress unusedFunction - used by GUI only
15091508
void ImportProject::selectVsConfigurations(Platform::Type platform, const std::vector<std::string> &configurations)
15101509
{
15111510
for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
@@ -1530,7 +1529,6 @@ void ImportProject::selectVsConfigurations(Platform::Type platform, const std::v
15301529
}
15311530
}
15321531

1533-
// cppcheck-suppress unusedFunction - used by GUI only
15341532
std::list<std::string> ImportProject::getVSConfigs()
15351533
{
15361534
return std::list<std::string>(mAllVSConfigs.cbegin(), mAllVSConfigs.cend());

lib/keywords.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ static const std::unordered_set<std::string> cpp26_keywords_all = {
173173
CPP03_KEYWORDS, CPP11_KEYWORDS, CPP20_KEYWORDS
174174
};
175175

176-
// cppcheck-suppress unusedFunction
177176
const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd)
178177
{
179178
switch (cStd) {
@@ -193,7 +192,6 @@ const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd)
193192
cppcheck::unreachable();
194193
}
195194

196-
// cppcheck-suppress unusedFunction
197195
const std::unordered_set<std::string>& Keywords::getAll(Standards::cppstd_t cppStd) {
198196
switch (cppStd) {
199197
case Standards::cppstd_t::CPP03:
@@ -234,7 +232,6 @@ const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd)
234232
cppcheck::unreachable();
235233
}
236234

237-
// cppcheck-suppress unusedFunction
238235
const std::unordered_set<std::string>& Keywords::getOnly(Standards::cppstd_t cppStd)
239236
{
240237
switch (cppStd) {

lib/mathlib.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ static bool isValidIntegerSuffixIt(std::string::const_iterator it, std::string::
789789
(state == Status::SUFFIX_LITERAL));
790790
}
791791

792-
// cppcheck-suppress unusedFunction
793792
bool MathLib::isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions)
794793
{
795794
return isValidIntegerSuffixIt(str.cbegin(), str.cend(), supportMicrosoftExtensions);

0 commit comments

Comments
 (0)