Skip to content

Commit

Permalink
Fix #11169 fix "delete" handling in "%type%" matching
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 10, 2024
1 parent f1d8dd5 commit 0ff92fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 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 @@ -7088,7 +7088,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
continue;
if (isCPP11 && type0->str() == "using")
continue;
if (isCPP() && type0->str() == "namespace")
if (isCPP() && Token::Match(type0, "namespace|delete"))
continue;

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

givenACodeSampleToTokenize noType1_cpp("delete", true, true);
ASSERT_EQUALS(false, Token::Match(noType1_cpp.tokens(), "%type%"));
givenACodeSampleToTokenize noType1_cpp("delete", true, /*cpp*/ true);
ASSERT_EQUALS(true, Token::Match(noType1_cpp.tokens(), "%type%"));

givenACodeSampleToTokenize noType1_c("delete", true, false);
givenACodeSampleToTokenize noType1_c("delete", true, /*cpp*/ false);
ASSERT_EQUALS(true, Token::Match(noType1_c.tokens(), "%type%"));

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

void matchChar() const {
Expand Down

0 comments on commit 0ff92fd

Please sign in to comment.