Skip to content

Commit 901b2ab

Browse files
authored
Preprocessor: provide suppressions separately from settings (#4878)
1 parent 2c05281 commit 901b2ab

File tree

10 files changed

+69
-66
lines changed

10 files changed

+69
-66
lines changed

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
663663
CheckUnusedFunctions checkUnusedFunctions(nullptr, nullptr, nullptr);
664664

665665
try {
666-
Preprocessor preprocessor(mSettings, this);
666+
Preprocessor preprocessor(mSettings, mSettings.nomsg, this);
667667
std::set<std::string> configurations;
668668

669669
simplecpp::OutputList outputList;

lib/preprocessor.cpp

Lines changed: 10 additions & 9 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(Settings& settings, ErrorLogger *errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
67+
Preprocessor::Preprocessor(const Settings& settings, Suppressions &suppressions, ErrorLogger *errorLogger) : mSettings(settings), mSuppressions(suppressions), mErrorLogger(errorLogger)
6868
{}
6969

7070
Preprocessor::~Preprocessor()
@@ -129,7 +129,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
129129
return true;
130130
}
131131

132-
static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &mSettings, std::list<BadInlineSuppression> &bad)
132+
static void addinlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, Suppressions &suppressions, std::list<BadInlineSuppression> &bad)
133133
{
134134
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
135135
if (!tok->comment)
@@ -155,8 +155,8 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &
155155

156156
// Relative filename
157157
std::string relativeFilename(tok->location.file());
158-
if (mSettings.relativePaths) {
159-
for (const std::string & basePath : mSettings.basePaths) {
158+
if (settings.relativePaths) {
159+
for (const std::string & basePath : settings.basePaths) {
160160
const std::string bp = basePath + "/";
161161
if (relativeFilename.compare(0,bp.size(),bp)==0) {
162162
relativeFilename = relativeFilename.substr(bp.size());
@@ -179,7 +179,7 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &
179179
suppr.fileName = relativeFilename;
180180
suppr.lineNumber = tok->location.line;
181181
suppr.thisAndNextLine = thisAndNextLine;
182-
mSettings.nomsg.addSuppression(suppr);
182+
suppressions.addSuppression(suppr);
183183
}
184184
}
185185
}
@@ -189,10 +189,10 @@ void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens)
189189
if (!mSettings.inlineSuppressions)
190190
return;
191191
std::list<BadInlineSuppression> err;
192-
::addinlineSuppressions(tokens, mSettings, err);
192+
::addinlineSuppressions(tokens, mSettings, mSuppressions, 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, err);
195+
::addinlineSuppressions(*it->second, mSettings, mSuppressions, err);
196196
}
197197
for (const BadInlineSuppression &bad : err) {
198198
error(bad.location.file(), bad.location.line, bad.errmsg);
@@ -842,7 +842,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
842842
errorMessage.errorId = errorId;
843843
errorMessage.setFileName(std::move(fname));
844844
errorMessage.lineNumber = linenr;
845-
if (mSettings.nomsg.isSuppressed(errorMessage))
845+
if (mSuppressions.isSuppressed(errorMessage))
846846
return;
847847

848848
if (mErrorLogger) {
@@ -866,7 +866,8 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
866866
void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
867867
{
868868
Settings settings2(*settings);
869-
Preprocessor preprocessor(settings2, errorLogger);
869+
Suppressions supressions2;
870+
Preprocessor preprocessor(settings2, supressions2, errorLogger);
870871
preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader);
871872
preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader);
872873
preprocessor.error(emptyString, 1, "#error message"); // #error ..

lib/preprocessor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
class ErrorLogger;
3737
class Settings;
38+
class Suppressions;
3839

3940
/**
4041
* @brief A preprocessor directive
@@ -83,7 +84,7 @@ class CPPCHECKLIB Preprocessor {
8384
/** character that is inserted in expanded macros */
8485
static char macroChar;
8586

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

8990
void inlineSuppressions(const simplecpp::TokenList &tokens);
@@ -187,7 +188,8 @@ class CPPCHECKLIB Preprocessor {
187188
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
188189
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
189190

190-
Settings& mSettings;
191+
const Settings& mSettings;
192+
Suppressions &mSuppressions;
191193
ErrorLogger *mErrorLogger;
192194

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

test/testclass.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class TestClass : public TestFixture {
260260
Settings settings;
261261
settings.severity.enable(Severity::warning);
262262

263-
Preprocessor preprocessor(settings, nullptr);
263+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
264264

265265
// Tokenize..
266266
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -366,7 +366,7 @@ class TestClass : public TestFixture {
366366
// Clear the error log
367367
errout.str("");
368368

369-
Preprocessor preprocessor(settings0, nullptr);
369+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
370370

371371
// Tokenize..
372372
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -520,7 +520,7 @@ class TestClass : public TestFixture {
520520
// Clear the error log
521521
errout.str("");
522522

523-
Preprocessor preprocessor(settings1, nullptr);
523+
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
524524

525525
// Tokenize..
526526
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -683,7 +683,7 @@ class TestClass : public TestFixture {
683683
// Clear the error log
684684
errout.str("");
685685

686-
Preprocessor preprocessor(settings0, nullptr);
686+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
687687

688688
// Tokenize..
689689
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -1132,7 +1132,7 @@ class TestClass : public TestFixture {
11321132
// Clear the error log
11331133
errout.str("");
11341134

1135-
Preprocessor preprocessor(settings0, nullptr);
1135+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
11361136

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

1611-
Preprocessor preprocessor(settings1, nullptr);
1611+
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
16121612

16131613
// Tokenize..
16141614
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -2572,7 +2572,7 @@ class TestClass : public TestFixture {
25722572
settings0.certainty.setEnabled(Certainty::inconclusive, inconclusive);
25732573
settings0.severity.enable(Severity::warning);
25742574

2575-
Preprocessor preprocessor(settings0, nullptr);
2575+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
25762576

25772577
// Tokenize..
25782578
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -2895,7 +2895,7 @@ class TestClass : public TestFixture {
28952895
// Clear the error log
28962896
errout.str("");
28972897

2898-
Preprocessor preprocessor(settings, nullptr);
2898+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
28992899

29002900
// Tokenize..
29012901
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -3528,7 +3528,7 @@ class TestClass : public TestFixture {
35283528
// Clear the error log
35293529
errout.str("");
35303530

3531-
Preprocessor preprocessor(settings1, nullptr);
3531+
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
35323532

35333533
// Tokenize..
35343534
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -3568,7 +3568,7 @@ class TestClass : public TestFixture {
35683568
s = &settings0;
35693569
s->certainty.setEnabled(Certainty::inconclusive, inconclusive);
35703570

3571-
Preprocessor preprocessor(*s, nullptr);
3571+
Preprocessor preprocessor(*s, s->nomsg, nullptr);
35723572

35733573
// Tokenize..
35743574
Tokenizer tokenizer(s, this, &preprocessor);
@@ -7185,7 +7185,7 @@ class TestClass : public TestFixture {
71857185
// Check..
71867186
settings0.certainty.setEnabled(Certainty::inconclusive, true);
71877187

7188-
Preprocessor preprocessor(settings0, nullptr);
7188+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
71897189

71907190
// Tokenize..
71917191
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -7223,7 +7223,7 @@ class TestClass : public TestFixture {
72237223
Settings settings;
72247224
settings.severity.enable(Severity::performance);
72257225

7226-
Preprocessor preprocessor(settings, nullptr);
7226+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
72277227

72287228
// Tokenize..
72297229
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -7437,7 +7437,7 @@ class TestClass : public TestFixture {
74377437
// Clear the error log
74387438
errout.str("");
74397439

7440-
Preprocessor preprocessor(settings0, nullptr);
7440+
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
74417441

74427442
// Tokenize..
74437443
Tokenizer tokenizer(&settings0, this, &preprocessor);
@@ -7555,7 +7555,7 @@ class TestClass : public TestFixture {
75557555
settings.severity.enable(Severity::warning);
75567556
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
75577557

7558-
Preprocessor preprocessor(settings, nullptr);
7558+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
75597559

75607560
// Tokenize..
75617561
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -7904,7 +7904,7 @@ class TestClass : public TestFixture {
79047904
Settings settings;
79057905
settings.severity.enable(Severity::style);
79067906

7907-
Preprocessor preprocessor(settings, nullptr);
7907+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
79087908

79097909
// Tokenize..
79107910
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -8082,7 +8082,7 @@ class TestClass : public TestFixture {
80828082
settings.safeChecks.classes = true;
80838083
settings.severity.enable(Severity::warning);
80848084

8085-
Preprocessor preprocessor(settings, nullptr);
8085+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
80868086

80878087
// Tokenize..
80888088
Tokenizer tokenizer(&settings, this, &preprocessor);
@@ -8105,7 +8105,7 @@ class TestClass : public TestFixture {
81058105
// Clear the error log
81068106
errout.str("");
81078107

8108-
Preprocessor preprocessor(settings1, nullptr);
8108+
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
81098109

81108110
// Tokenize..
81118111
Tokenizer tokenizer(&settings1, this, &preprocessor);
@@ -8304,7 +8304,7 @@ class TestClass : public TestFixture {
83048304
// Clear the error log
83058305
errout.str("");
83068306

8307-
Preprocessor preprocessor(settings1, nullptr);
8307+
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
83088308

83098309
// Tokenize..
83108310
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, nullptr);
158+
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
159159
preprocessor.setDirectives(tokens1);
160160

161161
// Tokenizer..

test/testgarbage.cpp

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

290-
Preprocessor preprocessor(settings, nullptr);
290+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
291291

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

test/testother.cpp

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

309-
Preprocessor preprocessor(*settings, nullptr);
309+
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
310310

311311
// Tokenize..
312312
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, nullptr);
350+
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
351351
preprocessor.setDirectives(tokens1);
352352

353353
// Tokenizer..
@@ -1548,7 +1548,7 @@ class TestOther : public TestFixture {
15481548
settings.severity.enable(Severity::style);
15491549
settings.standards.cpp = Standards::CPP03; // #5560
15501550

1551-
Preprocessor preprocessor(settings, nullptr);
1551+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
15521552

15531553
// Tokenize..
15541554
Tokenizer tokenizerCpp(&settings, this, &preprocessor);
@@ -1754,7 +1754,7 @@ class TestOther : public TestFixture {
17541754
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
17551755
settings.platform.defaultSign = 's';
17561756

1757-
Preprocessor preprocessor(settings, nullptr);
1757+
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
17581758

17591759
// Tokenize..
17601760
Tokenizer tokenizer(&settings, this, &preprocessor);

0 commit comments

Comments
 (0)