Tokenizer::setVarIdNew: use the new setVarId with a few more test cases. Improved setVarId: references in executable scopes must be initialized.

This commit is contained in:
Daniel Marjamäki 2012-04-15 14:30:46 +02:00
parent 27c37896a0
commit 3c1dfc658c
2 changed files with 17 additions and 8 deletions

View File

@ -2804,10 +2804,12 @@ std::string Tokenizer::getNameForFunctionParams(const Token *start)
} }
static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::string,unsigned int> &variableId) static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::string,unsigned int> &variableId, bool executableScope)
{ {
const Token *tok2 = *tok; const Token *tok2 = *tok;
bool ref = false;
if (!tok2->isName()) if (!tok2->isName())
return false; return false;
@ -2840,15 +2842,22 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
} }
if (bad || !tok2) if (bad || !tok2)
break; break;
} else if (tok2->str() != "*" && tok2->str() != "&" && tok2->str() != "::") { } else if (tok2->str() == "&") {
ref = true;
} else if (tok2->str() != "*" && tok2->str() != "::") {
break; break;
} }
tok2 = tok2->next(); tok2 = tok2->next();
} }
if (tok2) if (tok2) {
*tok = tok2; *tok = tok2;
// In executable scopes, references must be assigned
if (executableScope && ref && tok2->str() != "=")
return false;
}
return bool(typeCount >= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%")); return bool(typeCount >= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%"));
} }
@ -2902,7 +2911,7 @@ void Tokenizer::setVarIdNew()
if (tok2->str() == "return") if (tok2->str() == "return")
continue; continue;
const bool decl = setVarIdParseDeclaration(&tok2, variableId); const bool decl = setVarIdParseDeclaration(&tok2, variableId, executableScope.top());
if (decl && Token::Match(tok2->previous(), "%type% [;[=,)]")) { if (decl && Token::Match(tok2->previous(), "%type% [;[=,)]")) {
variableId[tok2->previous()->str()] = ++_varId; variableId[tok2->previous()->str()] = ++_varId;
@ -2911,7 +2920,7 @@ void Tokenizer::setVarIdNew()
else if (decl && Token::Match(tok2->previous(), "%type% ( !!)")) { else if (decl && Token::Match(tok2->previous(), "%type% ( !!)")) {
const Token *tok3 = tok2->next(); const Token *tok3 = tok2->next();
if (!setVarIdParseDeclaration(&tok3,variableId)) { if (!setVarIdParseDeclaration(&tok3,variableId,executableScope.top())) {
variableId[tok2->previous()->str()] = ++_varId; variableId[tok2->previous()->str()] = ++_varId;
tok = tok2->previous(); tok = tok2->previous();
} }

View File

@ -3213,7 +3213,7 @@ private:
const std::string code = "static int const SZ = 22;\n"; const std::string code = "static int const SZ = 22;\n";
ASSERT_EQUALS("\n\n##file 0\n" ASSERT_EQUALS("\n\n##file 0\n"
"1: static int const SZ@1 = 22 ;\n", "1: static int const SZ@1 = 22 ;\n",
tokenizeDebugListing(code)); tokenizeDebugListing(code, false, "test.c"));
} }
} }
@ -3255,7 +3255,7 @@ private:
const std::string code("int main(int flag) { if(a & flag) { return 1; } }"); const std::string code("int main(int flag) { if(a & flag) { return 1; } }");
ASSERT_EQUALS("\n\n##file 0\n" ASSERT_EQUALS("\n\n##file 0\n"
"1: int main ( int flag@1 ) { if ( a & flag@1 ) { return 1 ; } }\n", "1: int main ( int flag@1 ) { if ( a & flag@1 ) { return 1 ; } }\n",
tokenizeDebugListing(code)); tokenizeDebugListing(code, false, "test.c"));
} }
void varid44() { void varid44() {
@ -3275,7 +3275,7 @@ private:
"2: int x@1 ;\n" "2: int x@1 ;\n"
"3: x@1 = a ( y * x@1 , 10 ) ;\n" "3: x@1 = a ( y * x@1 , 10 ) ;\n"
"4: }\n"); "4: }\n");
ASSERT_EQUALS(expected, tokenizeDebugListing(code)); ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c"));
} }
void varidFunctionCall2() { void varidFunctionCall2() {