Ticket #5373: Keep track whether tokens come from a C or C++ file to properly handle %type% in Token::Match with "delete"

This commit is contained in:
Simon Martin 2014-03-08 20:56:39 +01:00
parent 2ac2dcf392
commit a3d1cac79c
5 changed files with 25 additions and 3 deletions

View File

@ -29,6 +29,8 @@
#include <map> #include <map>
#include <stack> #include <stack>
bool Token::_isCPP = true;
Token::Token(Token **t) : Token::Token(Token **t) :
tokensBack(t), tokensBack(t),
_next(0), _next(0),
@ -600,7 +602,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
// Type (%type%) // Type (%type%)
{ {
p += 5; p += 5;
multicompare(p,tok->isName() && tok->varId() == 0 && tok->str() != "delete",ismulticomp); multicompare(p, tok->isName() && tok->varId() == 0 && (tok->str() != "delete" || !Token::isCPP()), ismulticomp);
} }
break; break;
case 'a': case 'a':

View File

@ -679,6 +679,8 @@ private:
// original name like size_t // original name like size_t
std::string _originalName; std::string _originalName;
static bool _isCPP;
public: public:
void astOperand1(Token *tok); void astOperand1(Token *tok);
void astOperand2(Token *tok); void astOperand2(Token *tok);
@ -728,6 +730,8 @@ public:
void printAst(bool verbose) const; void printAst(bool verbose) const;
void printValueFlow() const; void printValueFlow() const;
static void isCPP(bool isCPP) { _isCPP = isCPP; }
static bool isCPP() { return _isCPP; }
}; };
/// @} /// @}

View File

@ -1589,6 +1589,8 @@ bool Tokenizer::tokenize(std::istream &code,
return false; return false;
} }
Token::isCPP(isCPP());
if (simplifyTokenList1()) { if (simplifyTokenList1()) {
createSymbolDatabase(); createSymbolDatabase();

View File

@ -263,6 +263,7 @@ private:
TEST_CASE(varid53); // #4172 - Template instantiation: T<&functionName> list[4]; TEST_CASE(varid53); // #4172 - Template instantiation: T<&functionName> list[4];
TEST_CASE(varid54); // hang TEST_CASE(varid54); // hang
TEST_CASE(varid_cpp_keywords_in_c_code); TEST_CASE(varid_cpp_keywords_in_c_code);
TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete"
TEST_CASE(varidFunctionCall1); TEST_CASE(varidFunctionCall1);
TEST_CASE(varidFunctionCall2); TEST_CASE(varidFunctionCall2);
TEST_CASE(varidFunctionCall3); TEST_CASE(varidFunctionCall3);
@ -3985,6 +3986,19 @@ private:
ASSERT_EQUALS(expected, tokenizeDebugListing(code,false,"test.c")); ASSERT_EQUALS(expected, tokenizeDebugListing(code,false,"test.c"));
} }
void varid_cpp_keywords_in_c_code2() { // #5373
const char code[] = "int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, "
"unsigned long bits, int wake, int delete, struct extent_state **cached_state, "
"gfp_t mask) {\n"
" struct extent_state *state;\n"
"}"
"int clear_extent_dirty() {\n"
" return clear_extent_bit(tree, start, end, EXTENT_DIRTY | EXTENT_DELALLOC | "
" EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);\n"
"}";
tokenizeDebugListing(code, false, "test.c");
}
void varidFunctionCall1() { void varidFunctionCall1() {
const std::string code("void f() {\n" const std::string code("void f() {\n"
" int x;\n" " int x;\n"

View File

@ -113,8 +113,8 @@ class MatchCompiler:
return '(tok->type()==Token::eString)' return '(tok->type()==Token::eString)'
elif tok == '%type%': elif tok == '%type%':
return ( return (
'(tok->isName() && tok->varId()==0U && tok->str() != ' + '(tok->isName() && tok->varId()==0U && (tok->str() != ' +
self._insertMatchStr('delete') + '/* delete */)' self._insertMatchStr('delete') + '/* delete */ || !Token::isCPP()))'
) )
elif tok == '%var%': elif tok == '%var%':
return 'tok->isName()' return 'tok->isName()'