Changed heuristics to detect variable constructor initialization syntax (#6071)

This commit is contained in:
PKEuS 2014-08-19 11:06:52 +02:00
parent 96c5983409
commit 5d50e7e9ae
2 changed files with 65 additions and 48 deletions

View File

@ -2281,6 +2281,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
if (!tok2 || tok2->str() != "::") if (!tok2 || tok2->str() != "::")
break; break;
} else { } else {
if (tok2->str() != "void" || Token::Match(tok2, "void const| *|(")) // just "void" cannot be a variable type
++typeCount; ++typeCount;
++singleNameCount; ++singleNameCount;
} }
@ -2545,7 +2546,7 @@ void Tokenizer::setVarId()
} }
if (tok == list.front() || Token::Match(tok, "[;{}]") || if (tok == list.front() || Token::Match(tok, "[;{}]") ||
(Token::Match(tok,"[(,]") && (!executableScope.top() || Token::simpleMatch(tok->link(), ") {"))) || (Token::Match(tok, "[(,]") && (!executableScope.top() || Token::simpleMatch(tok->link(), ") {"))) ||
(tok->isName() && tok->str().at(tok->str().length()-1U) == ':')) { (tok->isName() && tok->str().at(tok->str().length()-1U) == ':')) {
// No variable declarations in sizeof // No variable declarations in sizeof
@ -2570,46 +2571,46 @@ void Tokenizer::setVarId()
if (notstart.find(tok2->str()) != notstart.end()) if (notstart.find(tok2->str()) != notstart.end())
continue; continue;
const bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top(), isCPP()); bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top(), isCPP());
if (decl) {
if (decl && Token::Match(tok2->previous(), "%type% [;[=,)]") && tok2->previous()->str() != "const") { const Token* prev2 = tok2->previous();
variableId[tok2->previous()->str()] = ++_varId; if (Token::Match(prev2, "%type% [;[=,)]") && tok2->previous()->str() != "const")
tok = tok2->previous(); ;
} else if (Token::Match(prev2, "%type% ( !!)") && Token::simpleMatch(tok2->link(), ") ;")) {
else if (decl && Token::Match(tok2->previous(), "%type% ( !!)") && Token::simpleMatch(tok2->link(), ") ;")) {
// In C++ , a variable can't be called operator+ or something like that. // In C++ , a variable can't be called operator+ or something like that.
if (isCPP() && if (isCPP() &&
tok2->previous()->str().size() >= 9 && prev2->str().size() >= 9 &&
tok2->previous()->str().compare(0, 8, "operator") == 0 && prev2->str().compare(0, 8, "operator") == 0 &&
tok2->previous()->str()[8] != '_' && prev2->str()[8] != '_' &&
!std::isalnum(tok2->previous()->str()[8])) !std::isalnum(prev2->str()[8]))
continue; continue;
const Token *tok3 = tok2->next(); const Token *tok3 = tok2->next();
if (!tok3->isStandardType() && tok3->str() != "void" && !Token::Match(tok3, "struct|union|class %type%") && tok3->str() != "." && !Token::Match(tok2->link()->previous(), "[&*]")) { if (!tok3->isStandardType() && tok3->str() != "void" && !Token::Match(tok3, "struct|union|class %type%") && tok3->str() != "." && !Token::Match(tok2->link()->previous(), "[&*]")) {
bool isDecl = true; if (!executableScope.top()) {
// Detecting initializations with () in non-executable scope is hard and often impossible to be done safely. Thus, only treat code as a variable that definitly is one.
decl = false;
for (; tok3; tok3 = tok3->nextArgument()) { for (; tok3; tok3 = tok3->nextArgument()) {
if (tok3->strAt(-2) == "&" || tok3->strAt(-2) == "*" || (notstart.find(tok3->str()) == notstart.end() && setVarIdParseDeclaration(&tok3, variableId, executableScope.top(), isCPP()))) { if (tok3->isLiteral() || (tok3->isName() && (variableId.find(tok3->str()) != variableId.end())) || tok3->isOp() || (tok3->next()->isOp() && !Token::Match(tok3->next(), "*|&")) || notstart.find(tok3->str()) != notstart.end()) {
isDecl = false; decl = true;
break; break;
} }
} }
if (isDecl) {
variableId[tok2->previous()->str()] = ++_varId;
tok = tok2->previous();
} }
} } else
} decl = false;
} else if (isCPP() && Token::Match(prev2, "%type% {") && Token::simpleMatch(tok2->link(), "} ;")) { // C++11 initialization style
else if (decl && isCPP() && Token::Match(tok2->previous(), "%type% {") && Token::simpleMatch(tok2->link(), "} ;")) { // C++11 initialization style if (Token::Match(prev2, "do|try|else"))
if (Token::Match(tok2->previous(), "do|try|else"))
continue; continue;
} else
decl = false;
variableId[tok2->previous()->str()] = ++_varId; if (decl) {
variableId[prev2->str()] = ++_varId;
tok = tok2->previous(); tok = tok2->previous();
} }
} }
}
if (tok->isName()) { if (tok->isName()) {
// don't set variable id after a struct|enum|union // don't set variable id after a struct|enum|union

View File

@ -4633,13 +4633,15 @@ private:
} }
void varid_in_class17() { // #6056 - Set no varid for member functions void varid_in_class17() { // #6056 - Set no varid for member functions
const char code[] = "class Fred {\n" const char code1[] = "class Fred {\n"
" int method_with_internal(X&);\n" " int method_with_internal(X&);\n"
" int method_with_internal(X*);\n" " int method_with_internal(X*);\n"
" int method_with_internal(int&);\n" " int method_with_internal(int&);\n"
" int method_with_internal(A* b, X&);\n" " int method_with_internal(A* b, X&);\n"
" int method_with_internal(X&, A* b);\n" " int method_with_internal(X&, A* b);\n"
" int method_with_internal(const B &, int);\n" " int method_with_internal(const B &, int);\n"
" void Set(BAR);\n"
" FOO Set(BAR);\n"
"};"; "};";
ASSERT_EQUALS("\n\n##file 0\n" ASSERT_EQUALS("\n\n##file 0\n"
"1: class Fred {\n" "1: class Fred {\n"
@ -4649,7 +4651,21 @@ private:
"5: int method_with_internal ( A * b@1 , X & ) ;\n" "5: int method_with_internal ( A * b@1 , X & ) ;\n"
"6: int method_with_internal ( X & , A * b@2 ) ;\n" "6: int method_with_internal ( X & , A * b@2 ) ;\n"
"7: int method_with_internal ( const B & , int ) ;\n" "7: int method_with_internal ( const B & , int ) ;\n"
"8: } ;\n", tokenizeDebugListing(code, false, "test.cpp")); "8: void Set ( BAR ) ;\n"
"9: FOO Set ( BAR ) ;\n"
"10: } ;\n", tokenizeDebugListing(code1, false, "test.cpp"));
const char code2[] = "int i;\n"
"SomeType someVar1(i, i);\n"
"SomeType someVar2(j, j);\n"
"SomeType someVar3(j, 1);\n"
"SomeType someVar4(new bar);";
ASSERT_EQUALS("\n\n##file 0\n"
"1: int i@1 ;\n"
"2: SomeType someVar1@2 ( i@1 , i@1 ) ;\n"
"3: SomeType someVar2 ( j , j ) ;\n" // This one could be a function
"4: SomeType someVar3@3 ( j , 1 ) ;\n"
"5: SomeType someVar4@4 ( new bar ) ;\n", tokenizeDebugListing(code2, false, "test.cpp"));
} }
void varid_initList() { void varid_initList() {
@ -7297,12 +7313,12 @@ private:
void functionpointer6() { void functionpointer6() {
const char code1[] = ";void (*fp(f))(int);"; const char code1[] = ";void (*fp(f))(int);";
const char expected1[] = "\n\n##file 0\n" const char expected1[] = "\n\n##file 0\n"
"1: ; void * fp@1 ( f ) ;\n"; "1: ; void * fp ( f ) ;\n"; // No varId - it could be a function
ASSERT_EQUALS(expected1, tokenizeDebugListing(code1, false)); ASSERT_EQUALS(expected1, tokenizeDebugListing(code1, false));
const char code2[] = ";std::string (*fp(f))(int);"; const char code2[] = ";std::string (*fp(f))(int);";
const char expected2[] = "\n\n##file 0\n" const char expected2[] = "\n\n##file 0\n"
"1: ; std :: string * fp@1 ( f ) ;\n"; "1: ; std :: string * fp ( f ) ;\n";
ASSERT_EQUALS(expected2, tokenizeDebugListing(code2, false)); ASSERT_EQUALS(expected2, tokenizeDebugListing(code2, false));
} }