Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11169 fix "delete" handling in "%type%" matching #6111

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7091,7 +7091,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% matches keywords, although it probably shouldn't
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
Loading