Skip to content

Commit

Permalink
Fix #11169 fix "delete" handling in "%type%" matching (#6111)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 12, 2024
1 parent 84f0028 commit 1fe7485
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ int multiComparePercent(const Token *tok, const char*& haystack, nonneg int vari
// Type (%type%)
{
haystack += 5;
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
if (tok->isName() && tok->varId() == 0)
return 1;
}
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,18 @@ class CPPCHECKLIB Token {
* - "%char%" Any token enclosed in '-character.
* - "%comp%" Any token such that isComparisonOp() returns true.
* - "%cop%" Any token such that isConstOp() returns true.
* - "%name%" any token which is a name, variable or type e.g. "hello" or "int"
* - "%name%" any token which is a name, variable or type e.g. "hello" or "int". Also matches keywords.
* - "%num%" Any numeric token, e.g. "23"
* - "%op%" Any token such that isOp() returns true.
* - "%or%" A bitwise-or operator '|'
* - "%oror%" A logical-or operator '||'
* - "%type%" Anything that can be a variable type, e.g. "int", but not "delete".
* - "%type%" Anything that can be a variable type, e.g. "int". Also matches keywords.
* - "%str%" Any token starting with "-character (C-string).
* - "%var%" Match with token with varId > 0
* - "%varid%" Match with parameter varid
* - "[abc]" Any of the characters 'a' or 'b' or 'c'
* - "int|void|char" Any of the strings, int, void or char
* - "int|void|char|" Any of the strings, int, void or char or empty string
* - "int|void|char|" Any of the strings, int, void or char or no token
* - "!!else" No tokens or any token that is not "else".
* - "someRandomText" If token contains "someRandomText".
*
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7090,7 +7090,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
continue;
if (isCPP11 && type0->str() == "using")
continue;
if (type0->isCpp() && type0->str() == "namespace")
if (type0->isCpp() && Token::Match(type0, "namespace|delete"))
continue;

bool isconst = false;
Expand Down
5 changes: 3 additions & 2 deletions test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,15 @@ class TestToken : public TestFixture {
ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %name%"));
ASSERT_EQUALS(false, Token::Match(isVar.tokens(), "%type% %type%"));

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

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

const SimpleTokenList noType2("void delete");
ASSERT_EQUALS(false, Token::Match(noType2.front(), "!!foo %type%"));
ASSERT_EQUALS(true, Token::Match(noType2.front(), "!!foo %type%"));
}

void matchChar() const {
Expand Down
2 changes: 1 addition & 1 deletion tools/matchcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _compileCmd(tok):
elif tok == '%str%':
return '(tok->tokType() == Token::eString)'
elif tok == '%type%':
return '(tok->isName() && tok->varId() == 0U && (tok->str() != MatchCompiler::makeConstString("delete") || !tok->isKeyword()))'
return '(tok->isName() && tok->varId() == 0U)'
elif tok == '%name%':
return 'tok->isName()'
elif tok == '%var%':
Expand Down

0 comments on commit 1fe7485

Please sign in to comment.