Skip to content

Commit 1fe7485

Browse files
Fix #11169 fix "delete" handling in "%type%" matching (danmar#6111)
1 parent 84f0028 commit 1fe7485

5 files changed

Lines changed: 9 additions & 8 deletions

File tree

lib/token.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ int multiComparePercent(const Token *tok, const char*& haystack, nonneg int vari
491491
// Type (%type%)
492492
{
493493
haystack += 5;
494-
if (tok->isName() && tok->varId() == 0 && (tok->str() != "delete" || !tok->isKeyword())) // HACK: this is legacy behaviour, it should return false for all keywords, except types
494+
if (tok->isName() && tok->varId() == 0)
495495
return 1;
496496
}
497497
break;

lib/token.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,18 @@ class CPPCHECKLIB Token {
265265
* - "%char%" Any token enclosed in '-character.
266266
* - "%comp%" Any token such that isComparisonOp() returns true.
267267
* - "%cop%" Any token such that isConstOp() returns true.
268-
* - "%name%" any token which is a name, variable or type e.g. "hello" or "int"
268+
* - "%name%" any token which is a name, variable or type e.g. "hello" or "int". Also matches keywords.
269269
* - "%num%" Any numeric token, e.g. "23"
270270
* - "%op%" Any token such that isOp() returns true.
271271
* - "%or%" A bitwise-or operator '|'
272272
* - "%oror%" A logical-or operator '||'
273-
* - "%type%" Anything that can be a variable type, e.g. "int", but not "delete".
273+
* - "%type%" Anything that can be a variable type, e.g. "int". Also matches keywords.
274274
* - "%str%" Any token starting with "-character (C-string).
275275
* - "%var%" Match with token with varId > 0
276276
* - "%varid%" Match with parameter varid
277277
* - "[abc]" Any of the characters 'a' or 'b' or 'c'
278278
* - "int|void|char" Any of the strings, int, void or char
279-
* - "int|void|char|" Any of the strings, int, void or char or empty string
279+
* - "int|void|char|" Any of the strings, int, void or char or no token
280280
* - "!!else" No tokens or any token that is not "else".
281281
* - "someRandomText" If token contains "someRandomText".
282282
*

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7090,7 +7090,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
70907090
continue;
70917091
if (isCPP11 && type0->str() == "using")
70927092
continue;
7093-
if (type0->isCpp() && type0->str() == "namespace")
7093+
if (type0->isCpp() && Token::Match(type0, "namespace|delete"))
70947094
continue;
70957095

70967096
bool isconst = false;

test/testtoken.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,15 @@ class TestToken : public TestFixture {
613613
ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %name%"));
614614
ASSERT_EQUALS(false, Token::Match(isVar.tokens(), "%type% %type%"));
615615

616+
// TODO: %type% should not match keywords other than fundamental types
616617
const SimpleTokenList noType1_cpp("delete");
617-
ASSERT_EQUALS(false, Token::Match(noType1_cpp.front(), "%type%"));
618+
ASSERT_EQUALS(true, Token::Match(noType1_cpp.front(), "%type%"));
618619

619620
const SimpleTokenList noType1_c("delete", Standards::Language::C);
620621
ASSERT_EQUALS(true, Token::Match(noType1_c.front(), "%type%"));
621622

622623
const SimpleTokenList noType2("void delete");
623-
ASSERT_EQUALS(false, Token::Match(noType2.front(), "!!foo %type%"));
624+
ASSERT_EQUALS(true, Token::Match(noType2.front(), "!!foo %type%"));
624625
}
625626

626627
void matchChar() const {

tools/matchcompiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _compileCmd(tok):
179179
elif tok == '%str%':
180180
return '(tok->tokType() == Token::eString)'
181181
elif tok == '%type%':
182-
return '(tok->isName() && tok->varId() == 0U && (tok->str() != MatchCompiler::makeConstString("delete") || !tok->isKeyword()))'
182+
return '(tok->isName() && tok->varId() == 0U)'
183183
elif tok == '%name%':
184184
return 'tok->isName()'
185185
elif tok == '%var%':

0 commit comments

Comments
 (0)