Skip to content

Commit ad464c4

Browse files
authored
Preprocessor: relaxed dependency on Suppressions (#4983)
* Preprocessor: cleaned up `missingInclude()` * Preprocessor: relaxed dependency on `Suppressions` / adjusted `TestPreProcessor::inline_suppression_for_missing_include()` which was not testing production behavior * test/cli/test-other.py: added test for `missingInclude` and `missingIncludeSystem` inline suppressions * fixed `constParameterReference` selfcheck warning
1 parent a4e224b commit ad464c4

File tree

11 files changed

+110
-106
lines changed

11 files changed

+110
-106
lines changed

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
662662
CheckUnusedFunctions checkUnusedFunctions(nullptr, nullptr, nullptr);
663663

664664
try {
665-
Preprocessor preprocessor(mSettings, mSettings.nomsg, this);
665+
Preprocessor preprocessor(mSettings, this);
666666
std::set<std::string> configurations;
667667

668668
simplecpp::OutputList outputList;
@@ -739,7 +739,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
739739
}
740740

741741
// Parse comments and then remove them
742-
preprocessor.inlineSuppressions(tokens1);
742+
preprocessor.inlineSuppressions(tokens1, mSettings.nomsg);
743743
if (mSettings.dump || !mSettings.addons.empty()) {
744744
mSettings.nomsg.dump(dumpProlog);
745745
}

lib/preprocessor.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Directive::Directive(std::string _file, const int _linenr, const std::string &_s
6464

6565
char Preprocessor::macroChar = char(1);
6666

67-
Preprocessor::Preprocessor(const Settings& settings, Suppressions &suppressions, ErrorLogger *errorLogger) : mSettings(settings), mSuppressions(suppressions), mErrorLogger(errorLogger)
67+
Preprocessor::Preprocessor(const Settings& settings, ErrorLogger *errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
6868
{}
6969

7070
Preprocessor::~Preprocessor()
@@ -184,15 +184,15 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, const Sett
184184
}
185185
}
186186

187-
void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens)
187+
void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, Suppressions &suppressions)
188188
{
189189
if (!mSettings.inlineSuppressions)
190190
return;
191191
std::list<BadInlineSuppression> err;
192-
::addinlineSuppressions(tokens, mSettings, mSuppressions, err);
192+
::addinlineSuppressions(tokens, mSettings, suppressions, err);
193193
for (std::map<std::string,simplecpp::TokenList*>::const_iterator it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
194194
if (it->second)
195-
::addinlineSuppressions(*it->second, mSettings, mSuppressions, err);
195+
::addinlineSuppressions(*it->second, mSettings, suppressions, err);
196196
}
197197
for (const BadInlineSuppression &bad : err) {
198198
error(bad.location.file(), bad.location.line, bad.errmsg);
@@ -752,14 +752,15 @@ std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std
752752
return ret.str();
753753
}
754754

755-
std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename)
755+
std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression)
756756
{
757757
simplecpp::OutputList outputList;
758758
std::vector<std::string> files;
759759

760760
std::istringstream istr(filedata);
761761
simplecpp::TokenList tokens1(istr, files, Path::simplifyPath(filename), &outputList);
762-
inlineSuppressions(tokens1);
762+
if (inlineSuppression)
763+
inlineSuppressions(tokens1, *inlineSuppression);
763764
tokens1.removeComments();
764765
removeComments();
765766
setDirectives(tokens1);
@@ -833,41 +834,26 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
833834
// Report that include is missing
834835
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
835836
{
836-
if (!mSettings.checks.isEnabled(Checks::missingInclude))
837+
if (!mSettings.checks.isEnabled(Checks::missingInclude) || !mErrorLogger)
837838
return;
838839

839-
std::string fname = Path::fromNativeSeparators(filename);
840-
std::string errorId = (headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude";
841-
Suppressions::ErrorMessage errorMessage;
842-
errorMessage.errorId = errorId;
843-
errorMessage.setFileName(std::move(fname));
844-
errorMessage.lineNumber = linenr;
845-
if (mSuppressions.isSuppressed(errorMessage))
846-
return;
847-
848-
if (mErrorLogger) {
849-
std::list<ErrorMessage::FileLocation> locationList;
850-
if (!filename.empty()) {
851-
ErrorMessage::FileLocation loc;
852-
loc.line = linenr;
853-
loc.setfile(Path::toNativeSeparators(filename));
854-
locationList.push_back(std::move(loc));
855-
}
856-
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
857-
(headerType==SystemHeader) ?
858-
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
859-
"Include file: \"" + header + "\" not found.",
860-
std::move(errorId),
861-
Certainty::normal);
862-
mErrorLogger->reportErr(errmsg);
840+
std::list<ErrorMessage::FileLocation> locationList;
841+
if (!filename.empty()) {
842+
locationList.emplace_back(filename, linenr);
863843
}
844+
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
845+
(headerType==SystemHeader) ?
846+
"Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
847+
"Include file: \"" + header + "\" not found.",
848+
(headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
849+
Certainty::normal);
850+
mErrorLogger->reportErr(errmsg);
864851
}
865852

866853
void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
867854
{
868855
Settings settings2(*settings);
869-
Suppressions supressions2;
870-
Preprocessor preprocessor(settings2, supressions2, errorLogger);
856+
Preprocessor preprocessor(settings2, errorLogger);
871857
preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader);
872858
preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader);
873859
preprocessor.error(emptyString, 1, "#error message"); // #error ..

lib/preprocessor.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ class CPPCHECKLIB Preprocessor {
8484
/** character that is inserted in expanded macros */
8585
static char macroChar;
8686

87-
explicit Preprocessor(const Settings& settings, Suppressions &suppressions, ErrorLogger *errorLogger = nullptr);
87+
explicit Preprocessor(const Settings& settings, ErrorLogger *errorLogger = nullptr);
8888
virtual ~Preprocessor();
8989

90-
void inlineSuppressions(const simplecpp::TokenList &tokens);
90+
void inlineSuppressions(const simplecpp::TokenList &tokens, Suppressions &suppressions);
9191

9292
void setDirectives(const simplecpp::TokenList &tokens);
9393
void setDirectives(const std::list<Directive> &directives) {
@@ -147,11 +147,15 @@ class CPPCHECKLIB Preprocessor {
147147

148148
/**
149149
* Get preprocessed code for a given configuration
150+
*
151+
* Note: for testing only.
152+
*
150153
* @param filedata file data including preprocessing 'if', 'define', etc
151154
* @param cfg configuration to read out
152155
* @param filename name of source file
156+
* @param inlineSuppression the inline suppressions
153157
*/
154-
std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename);
158+
std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression = nullptr);
155159

156160
/**
157161
* Calculate HASH. Using toolinfo, tokens1, filedata.
@@ -189,7 +193,6 @@ class CPPCHECKLIB Preprocessor {
189193
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
190194

191195
const Settings& mSettings;
192-
Suppressions &mSuppressions;
193196
ErrorLogger *mErrorLogger;
194197

195198
/** list of all directives met while preprocessing file */

test/cli/test-other.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ def test_missing_include_check_config(tmpdir):
4545
__test_missing_include_check_config(tmpdir, False)
4646

4747
def test_missing_include_check_config_j(tmpdir):
48-
__test_missing_include_check_config(tmpdir, True)
48+
__test_missing_include_check_config(tmpdir, True)
49+
50+
def test_missing_include_inline_suppr(tmpdir):
51+
test_file = os.path.join(tmpdir, 'test.c')
52+
with open(test_file, 'wt') as f:
53+
f.write("""
54+
// cppcheck-suppress missingInclude
55+
#include "missing.h"
56+
// cppcheck-suppress missingIncludeSystem
57+
#include <missing2.h>
58+
""")
59+
60+
args = ['--enable=missingInclude', '--inline-suppr', test_file]
61+
62+
_, _, stderr = cppcheck(args)
63+
assert stderr == ''

test/testclass.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class TestClass : public TestFixture {
265265
Settings settings;
266266
settings.severity.enable(Severity::warning);
267267

268-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
268+
Preprocessor preprocessor(settings);
269269

270270
// Tokenize..
271271
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -371,7 +371,7 @@ class TestClass : public TestFixture {
371371
// Clear the error log
372372
errout.str("");
373373

374-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
374+
Preprocessor preprocessor(settings0);
375375

376376
// Tokenize..
377377
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -525,7 +525,7 @@ class TestClass : public TestFixture {
525525
// Clear the error log
526526
errout.str("");
527527

528-
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
528+
Preprocessor preprocessor(settings1);
529529

530530
// Tokenize..
531531
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -688,7 +688,7 @@ class TestClass : public TestFixture {
688688
// Clear the error log
689689
errout.str("");
690690

691-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
691+
Preprocessor preprocessor(settings0);
692692

693693
// Tokenize..
694694
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -1137,7 +1137,7 @@ class TestClass : public TestFixture {
11371137
// Clear the error log
11381138
errout.str("");
11391139

1140-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
1140+
Preprocessor preprocessor(settings0);
11411141

11421142
// Tokenize..
11431143
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -1613,7 +1613,7 @@ class TestClass : public TestFixture {
16131613
// Clear the error log
16141614
errout.str("");
16151615

1616-
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
1616+
Preprocessor preprocessor(settings1);
16171617

16181618
// Tokenize..
16191619
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -2577,7 +2577,7 @@ class TestClass : public TestFixture {
25772577
settings0.certainty.setEnabled(Certainty::inconclusive, inconclusive);
25782578
settings0.severity.enable(Severity::warning);
25792579

2580-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
2580+
Preprocessor preprocessor(settings0);
25812581

25822582
// Tokenize..
25832583
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -2896,11 +2896,11 @@ class TestClass : public TestFixture {
28962896
checkNoMemset_(file, line, code, settings);
28972897
}
28982898

2899-
void checkNoMemset_(const char* file, int line, const char code[], Settings &settings) {
2899+
void checkNoMemset_(const char* file, int line, const char code[], const Settings &settings) {
29002900
// Clear the error log
29012901
errout.str("");
29022902

2903-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
2903+
Preprocessor preprocessor(settings);
29042904

29052905
// Tokenize..
29062906
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -3533,7 +3533,7 @@ class TestClass : public TestFixture {
35333533
// Clear the error log
35343534
errout.str("");
35353535

3536-
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
3536+
Preprocessor preprocessor(settings1);
35373537

35383538
// Tokenize..
35393539
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -3573,7 +3573,7 @@ class TestClass : public TestFixture {
35733573
s = &settings0;
35743574
s->certainty.setEnabled(Certainty::inconclusive, inconclusive);
35753575

3576-
Preprocessor preprocessor(*s, s->nomsg, nullptr);
3576+
Preprocessor preprocessor(*s);
35773577

35783578
// Tokenize..
35793579
Tokenizer tokenizer(s, this, &preprocessor);
@@ -7254,7 +7254,7 @@ class TestClass : public TestFixture {
72547254
// Check..
72557255
settings0.certainty.setEnabled(Certainty::inconclusive, true);
72567256

7257-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
7257+
Preprocessor preprocessor(settings0);
72587258

72597259
// Tokenize..
72607260
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -7292,7 +7292,7 @@ class TestClass : public TestFixture {
72927292
Settings settings;
72937293
settings.severity.enable(Severity::performance);
72947294

7295-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
7295+
Preprocessor preprocessor(settings);
72967296

72977297
// Tokenize..
72987298
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -7506,7 +7506,7 @@ class TestClass : public TestFixture {
75067506
// Clear the error log
75077507
errout.str("");
75087508

7509-
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
7509+
Preprocessor preprocessor(settings0);
75107510

75117511
// Tokenize..
75127512
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -7624,7 +7624,7 @@ class TestClass : public TestFixture {
76247624
settings.severity.enable(Severity::warning);
76257625
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
76267626

7627-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
7627+
Preprocessor preprocessor(settings);
76287628

76297629
// Tokenize..
76307630
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -7973,7 +7973,7 @@ class TestClass : public TestFixture {
79737973
Settings settings;
79747974
settings.severity.enable(Severity::style);
79757975

7976-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
7976+
Preprocessor preprocessor(settings);
79777977

79787978
// Tokenize..
79797979
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -8151,7 +8151,7 @@ class TestClass : public TestFixture {
81518151
settings.safeChecks.classes = true;
81528152
settings.severity.enable(Severity::warning);
81538153

8154-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
8154+
Preprocessor preprocessor(settings);
81558155

81568156
// Tokenize..
81578157
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -8174,7 +8174,7 @@ class TestClass : public TestFixture {
81748174
// Clear the error log
81758175
errout.str("");
81768176

8177-
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
8177+
Preprocessor preprocessor(settings1);
81788178

81798179
// Tokenize..
81808180
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -8373,7 +8373,7 @@ class TestClass : public TestFixture {
83738373
// Clear the error log
83748374
errout.str("");
83758375

8376-
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
8376+
Preprocessor preprocessor(settings1);
83778377

83788378
// Tokenize..
83798379
Tokenizer tokenizer(&settings1, this, &preprocessor);

test/testcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class TestCondition : public TestFixture {
155155
std::map<std::string, simplecpp::TokenList*> filedata;
156156
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
157157

158-
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
158+
Preprocessor preprocessor(*settings);
159159
preprocessor.setDirectives(tokens1);
160160

161161
// Tokenizer..

test/testgarbage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class TestGarbage : public TestFixture {
288288
std::string checkCodeInternal_(const std::string &code, const char* filename, const char* file, int line) {
289289
errout.str("");
290290

291-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
291+
Preprocessor preprocessor(settings);
292292

293293
// tokenize..
294294
Tokenizer tokenizer(&settings, this, &preprocessor);

test/testother.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class TestOther : public TestFixture {
307307
settings->certainty.setEnabled(Certainty::inconclusive, inconclusive);
308308
settings->verbose = verbose;
309309

310-
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
310+
Preprocessor preprocessor(*settings);
311311

312312
// Tokenize..
313313
Tokenizer tokenizer(settings, this, &preprocessor);
@@ -347,7 +347,7 @@ class TestOther : public TestFixture {
347347
std::map<std::string, simplecpp::TokenList*> filedata;
348348
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
349349

350-
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
350+
Preprocessor preprocessor(*settings);
351351
preprocessor.setDirectives(tokens1);
352352

353353
// Tokenizer..
@@ -1572,7 +1572,7 @@ class TestOther : public TestFixture {
15721572
settings.severity.enable(Severity::style);
15731573
settings.standards.cpp = Standards::CPP03; // #5560
15741574

1575-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
1575+
Preprocessor preprocessor(settings);
15761576

15771577
// Tokenize..
15781578
Tokenizer tokenizerCpp(&settings, this, &preprocessor);
@@ -1778,7 +1778,7 @@ class TestOther : public TestFixture {
17781778
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
17791779
settings.platform.defaultSign = 's';
17801780

1781-
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
1781+
Preprocessor preprocessor(settings);
17821782

17831783
// Tokenize..
17841784
Tokenizer tokenizer(&settings, this, &preprocessor);

0 commit comments

Comments
 (0)