Preprocessor: provide suppressions separately from settings (#4878)

This commit is contained in:
Oliver Stöneberg 2023-03-09 20:15:53 +01:00 committed by GitHub
parent 2c05281a31
commit 901b2ab838
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 69 additions and 66 deletions

View File

@ -663,7 +663,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
CheckUnusedFunctions checkUnusedFunctions(nullptr, nullptr, nullptr);
try {
Preprocessor preprocessor(mSettings, this);
Preprocessor preprocessor(mSettings, mSettings.nomsg, this);
std::set<std::string> configurations;
simplecpp::OutputList outputList;

View File

@ -64,7 +64,7 @@ Directive::Directive(std::string _file, const int _linenr, const std::string &_s
char Preprocessor::macroChar = char(1);
Preprocessor::Preprocessor(Settings& settings, ErrorLogger *errorLogger) : mSettings(settings), mErrorLogger(errorLogger)
Preprocessor::Preprocessor(const Settings& settings, Suppressions &suppressions, ErrorLogger *errorLogger) : mSettings(settings), mSuppressions(suppressions), mErrorLogger(errorLogger)
{}
Preprocessor::~Preprocessor()
@ -129,7 +129,7 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
return true;
}
static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &mSettings, std::list<BadInlineSuppression> &bad)
static void addinlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, Suppressions &suppressions, std::list<BadInlineSuppression> &bad)
{
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
if (!tok->comment)
@ -155,8 +155,8 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &
// Relative filename
std::string relativeFilename(tok->location.file());
if (mSettings.relativePaths) {
for (const std::string & basePath : mSettings.basePaths) {
if (settings.relativePaths) {
for (const std::string & basePath : settings.basePaths) {
const std::string bp = basePath + "/";
if (relativeFilename.compare(0,bp.size(),bp)==0) {
relativeFilename = relativeFilename.substr(bp.size());
@ -179,7 +179,7 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, Settings &
suppr.fileName = relativeFilename;
suppr.lineNumber = tok->location.line;
suppr.thisAndNextLine = thisAndNextLine;
mSettings.nomsg.addSuppression(suppr);
suppressions.addSuppression(suppr);
}
}
}
@ -189,10 +189,10 @@ void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens)
if (!mSettings.inlineSuppressions)
return;
std::list<BadInlineSuppression> err;
::addinlineSuppressions(tokens, mSettings, err);
::addinlineSuppressions(tokens, mSettings, mSuppressions, err);
for (std::map<std::string,simplecpp::TokenList*>::const_iterator it = mTokenLists.cbegin(); it != mTokenLists.cend(); ++it) {
if (it->second)
::addinlineSuppressions(*it->second, mSettings, err);
::addinlineSuppressions(*it->second, mSettings, mSuppressions, err);
}
for (const BadInlineSuppression &bad : err) {
error(bad.location.file(), bad.location.line, bad.errmsg);
@ -842,7 +842,7 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
errorMessage.errorId = errorId;
errorMessage.setFileName(std::move(fname));
errorMessage.lineNumber = linenr;
if (mSettings.nomsg.isSuppressed(errorMessage))
if (mSuppressions.isSuppressed(errorMessage))
return;
if (mErrorLogger) {
@ -866,7 +866,8 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line
void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
Settings settings2(*settings);
Preprocessor preprocessor(settings2, errorLogger);
Suppressions supressions2;
Preprocessor preprocessor(settings2, supressions2, errorLogger);
preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader);
preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader);
preprocessor.error(emptyString, 1, "#error message"); // #error ..

View File

@ -35,6 +35,7 @@
class ErrorLogger;
class Settings;
class Suppressions;
/**
* @brief A preprocessor directive
@ -83,7 +84,7 @@ public:
/** character that is inserted in expanded macros */
static char macroChar;
explicit Preprocessor(Settings& settings, ErrorLogger *errorLogger = nullptr);
explicit Preprocessor(const Settings& settings, Suppressions &suppressions, ErrorLogger *errorLogger = nullptr);
virtual ~Preprocessor();
void inlineSuppressions(const simplecpp::TokenList &tokens);
@ -187,7 +188,8 @@ private:
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
Settings& mSettings;
const Settings& mSettings;
Suppressions &mSuppressions;
ErrorLogger *mErrorLogger;
/** list of all directives met while preprocessing file */

View File

@ -260,7 +260,7 @@ private:
Settings settings;
settings.severity.enable(Severity::warning);
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -366,7 +366,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -520,7 +520,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings1, nullptr);
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings1, this, &preprocessor);
@ -683,7 +683,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -1132,7 +1132,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -1608,7 +1608,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings1, nullptr);
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings1, this, &preprocessor);
@ -2572,7 +2572,7 @@ private:
settings0.certainty.setEnabled(Certainty::inconclusive, inconclusive);
settings0.severity.enable(Severity::warning);
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -2895,7 +2895,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -3528,7 +3528,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings1, nullptr);
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings1, this, &preprocessor);
@ -3568,7 +3568,7 @@ private:
s = &settings0;
s->certainty.setEnabled(Certainty::inconclusive, inconclusive);
Preprocessor preprocessor(*s, nullptr);
Preprocessor preprocessor(*s, s->nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(s, this, &preprocessor);
@ -7185,7 +7185,7 @@ private:
// Check..
settings0.certainty.setEnabled(Certainty::inconclusive, true);
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -7223,7 +7223,7 @@ private:
Settings settings;
settings.severity.enable(Severity::performance);
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -7437,7 +7437,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings0, this, &preprocessor);
@ -7555,7 +7555,7 @@ private:
settings.severity.enable(Severity::warning);
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -7904,7 +7904,7 @@ private:
Settings settings;
settings.severity.enable(Severity::style);
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -8082,7 +8082,7 @@ private:
settings.safeChecks.classes = true;
settings.severity.enable(Severity::warning);
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);
@ -8105,7 +8105,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings1, nullptr);
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings1, this, &preprocessor);
@ -8304,7 +8304,7 @@ private:
// Clear the error log
errout.str("");
Preprocessor preprocessor(settings1, nullptr);
Preprocessor preprocessor(settings1, settings1.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings1, this, &preprocessor);

View File

@ -155,7 +155,7 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(*settings, nullptr);
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..

View File

@ -287,7 +287,7 @@ private:
std::string checkCodeInternal_(const std::string &code, const char* filename, const char* file, int line) {
errout.str("");
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);

View File

@ -306,7 +306,7 @@ private:
settings->certainty.setEnabled(Certainty::experimental, experimental);
settings->verbose = verbose;
Preprocessor preprocessor(*settings, nullptr);
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(settings, this, &preprocessor);
@ -347,7 +347,7 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(*settings, nullptr);
Preprocessor preprocessor(*settings, settings->nomsg, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..
@ -1548,7 +1548,7 @@ private:
settings.severity.enable(Severity::style);
settings.standards.cpp = Standards::CPP03; // #5560
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizerCpp(&settings, this, &preprocessor);
@ -1754,7 +1754,7 @@ private:
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
settings.platform.defaultSign = 's';
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
// Tokenize..
Tokenizer tokenizer(&settings, this, &preprocessor);

View File

@ -46,7 +46,7 @@ class TestPreprocessor : public TestFixture {
public:
TestPreprocessor()
: TestFixture("TestPreprocessor")
, preprocessor0(settings0, this) {
, preprocessor0(settings0, settings0.nomsg, this) {
settings0.severity.enable(Severity::information);
}
@ -64,7 +64,7 @@ public:
if (errorLogger) {
Settings settings;
Preprocessor p(settings, errorLogger);
Preprocessor p(settings, settings.nomsg, errorLogger);
p.reportOutput(outputList, true);
}
@ -296,7 +296,7 @@ private:
settings.userDefines = arg + 2;
if (arg && std::strncmp(arg,"-U",2)==0)
settings.userUndefs.insert(arg+2);
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens(istr,files);
@ -362,7 +362,7 @@ private:
errout.str("");
Settings settings;
settings.userDefines = "__cplusplus";
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
@ -375,7 +375,7 @@ private:
errout.str("");
Settings settings;
settings.userDefines = "TEST";
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile");
preprocessor.getcode(code, "TEST", "test.c");
ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout.str());
@ -386,7 +386,7 @@ private:
errout.str("");
Settings settings;
settings.userDefines = "TEST";
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa");
preprocessor.getcode(code, "TEST", "test.c");
ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout.str());
@ -398,7 +398,7 @@ private:
Settings settings;
settings.userDefines = "FOO";
settings.force = true; // No message if --force is given
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "test.c");
ASSERT_EQUALS("", errout.str());
@ -463,7 +463,7 @@ private:
void setPlatformInfo() {
Settings settings;
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
// read code with simplecpp..
const char filedata[] = "#if sizeof(long) == 4\n"
@ -1950,7 +1950,7 @@ private:
settings.inlineSuppressions = true;
settings.severity.clear();
settings.checks.enable(Checks::missingInclude);
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("// cppcheck-suppress missingInclude\n"
"#include \"missing.h\"\n"
@ -2306,7 +2306,7 @@ private:
errout.str("");
Settings settings;
settings.userDefines = "foo";
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "./././test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
@ -2344,7 +2344,7 @@ private:
" </directivelist>\n";
std::ostringstream ostr;
Preprocessor preprocessor(settings0, this);
Preprocessor preprocessor(settings0, settings0.nomsg, this);
preprocessor.getcode(filedata, "", "test.c");
preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str());
@ -2371,7 +2371,7 @@ private:
" </directivelist>\n";
std::ostringstream ostr;
Preprocessor preprocessor(settings0, this);
Preprocessor preprocessor(settings0, settings0.nomsg, this);
preprocessor.getcode(filedata, "", "test.c");
preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str());
@ -2388,7 +2388,7 @@ private:
" </directivelist>\n";
std::ostringstream ostr;
Preprocessor preprocessor(settings0, this);
Preprocessor preprocessor(settings0, settings0.nomsg, this);
preprocessor.getcode(filedata, "", "test.c");
preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str());
@ -2402,7 +2402,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "");
@ -2421,7 +2421,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
std::string code("#include \"header.h\"");
errout.str("");
@ -2438,7 +2438,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "", "inc");
@ -2458,7 +2458,7 @@ private:
settings.includePaths.emplace_back("inc");
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "", "inc");
@ -2478,7 +2478,7 @@ private:
settings.includePaths.emplace_back("inc");
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "", Path::getCurrentPath());
@ -2497,7 +2497,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string header = Path::join(Path::getCurrentPath(), "header.h");
@ -2516,7 +2516,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "");
@ -2535,7 +2535,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
std::string code("#include <header.h>");
errout.str("");
@ -2554,7 +2554,7 @@ private:
setTemplateFormat("simple");
settings.includePaths.emplace_back("system");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "", "system");
@ -2574,7 +2574,7 @@ private:
settings.includePaths.emplace_back("inc");
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "", Path::getCurrentPath());
@ -2593,7 +2593,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
const std::string header = Path::join(Path::getCurrentPath(), "header.h");
@ -2612,7 +2612,7 @@ private:
settings.checks.enable(Checks::missingInclude);
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "");
ScopedFile header2("header2.h", "");
@ -2638,7 +2638,7 @@ private:
settings.templateFormat = "simple"; // has no effect
setTemplateFormat("simple");
Preprocessor preprocessor(settings, this);
Preprocessor preprocessor(settings, settings.nomsg, this);
ScopedFile header("header.h", "");
ScopedFile header2("header2.h", "");

View File

@ -6586,7 +6586,7 @@ private:
"int PTR4 q4_var RBR4 = 0;\n";
// Preprocess file..
Preprocessor preprocessor(settings0);
Preprocessor preprocessor(settings0, settings0.nomsg);
std::list<std::string> configurations;
std::string filedata;
std::istringstream fin(raw_code);
@ -7332,7 +7332,7 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(settings0, nullptr);
Preprocessor preprocessor(settings0, settings0.nomsg, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..

View File

@ -252,7 +252,7 @@ private:
// Clear the error buffer..
errout.str("");
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
if (directives)
preprocessor.setDirectives(*directives);
@ -277,7 +277,7 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(settings, nullptr);
Preprocessor preprocessor(settings, settings.nomsg, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..