Skip to content

Commit 8285510

Browse files
committed
Rules: Make it possible to write rules that check #define macros
1 parent cfd697d commit 8285510

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/cppcheck.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,28 @@ unsigned int CppCheck::processFile(const std::string& filename)
162162
preprocessor.preprocess(fin, filedata, configurations, filename, _settings._includePaths);
163163
}
164164

165+
// Run rules on this code
166+
for (std::list<Settings::Rule>::const_iterator it = _settings.rules.begin(); it != _settings.rules.end(); ++it) {
167+
if (it->tokenlist == "define") {
168+
Tokenizer tokenizer2(&_settings, this);
169+
std::istringstream istr2(filedata);
170+
tokenizer2.list.createTokens(istr2, filename);
171+
172+
for (const Token *tok = tokenizer2.list.front(); tok; tok = tok->next()) {
173+
if (tok->str() == "#define") {
174+
std::string code = std::string(tok->linenr()-1U, '\n');
175+
for (const Token *tok2 = tok; tok2 && tok2->linenr() == tok->linenr(); tok2 = tok2->next())
176+
code += " " + tok2->str();
177+
Tokenizer tokenizer3(&_settings, this);
178+
std::istringstream istr3(code);
179+
tokenizer3.list.createTokens(istr3, tokenizer2.list.file(tok));
180+
executeRules("define", tokenizer3);
181+
}
182+
}
183+
break;
184+
}
185+
}
186+
165187
if (_settings.checkConfiguration) {
166188
return 0;
167189
}

rules/show-all-defines.rule

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<rule version="1.0">
3+
<tokenlist>define</tokenlist>
4+
<pattern>.*</pattern>
5+
<message>
6+
<id>showalldefines</id>
7+
<severity>information</severity>
8+
<summary/>
9+
</message>
10+
</rule>
11+

0 commit comments

Comments
 (0)