Tokenizer: More specific bail outs in simplifyKnownVariables

This commit is contained in:
Daniel Marjamäki 2010-11-07 11:07:56 +01:00
parent b881718d9f
commit cd94fa89e0
3 changed files with 37 additions and 3 deletions

View File

@ -6029,6 +6029,19 @@ bool Tokenizer::simplifyKnownVariables()
if (Token::Match(tok3->next(), ". %var% (")) if (Token::Match(tok3->next(), ". %var% ("))
break; break;
// suppress debug-warning when assignment
if (Token::simpleMatch(tok3->next(), "="))
break;
// taking address of variable..
if (Token::Match(tok3->tokAt(-2), "return|= & %var% ;"))
break;
// parameter in function call..
if (Token::Match(tok3->tokAt(-2), "%var% ( %var% ,|)") ||
Token::Match(tok3->previous(), ", %var% ,|)"))
break;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok3->linenr(); loc.line = tok3->linenr();

View File

@ -38,7 +38,9 @@ private:
void check(const char code[]) void check(const char code[])
{ {
// Tokenize.. // Tokenize..
Tokenizer tokenizer; Settings settings;
settings.debugwarnings = true;
Tokenizer tokenizer(&settings, 0);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
@ -53,7 +55,6 @@ private:
errout.str(""); errout.str("");
// Check auto variables // Check auto variables
Settings settings;
CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this); CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this);
checkAutoVariables.autoVariables(); checkAutoVariables.autoVariables();
checkAutoVariables.returnPointerToLocalArray(); checkAutoVariables.returnPointerToLocalArray();

View File

@ -123,6 +123,7 @@ private:
TEST_CASE(simplifyKnownVariables30); TEST_CASE(simplifyKnownVariables30);
TEST_CASE(simplifyKnownVariables31); TEST_CASE(simplifyKnownVariables31);
TEST_CASE(simplifyKnownVariables32); // const TEST_CASE(simplifyKnownVariables32); // const
TEST_CASE(simplifyKnownVariablesBailOutAssign);
TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor1);
TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutFor2);
TEST_CASE(simplifyKnownVariablesBailOutMemberFunction); TEST_CASE(simplifyKnownVariablesBailOutMemberFunction);
@ -299,8 +300,12 @@ private:
std::string tokenizeAndStringify(const char code[], bool simplify = false) std::string tokenizeAndStringify(const char code[], bool simplify = false)
{ {
errout.str("");
// tokenize.. // tokenize..
Tokenizer tokenizer(0, this); Settings settings;
settings.debugwarnings = true;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
if (simplify) if (simplify)
@ -1871,6 +1876,21 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
} }
void simplifyKnownVariablesBailOutAssign()
{
const char code[] = "int foo() {\n"
" int i; i = 0;\n"
" if (x) { i = 10; }\n"
" return i;\n"
"}\n";
const char expected[] = "int foo ( ) {\n"
"int i ; i = 0 ;\n"
"if ( x ) { i = 10 ; }\n"
"return i ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutFor1() void simplifyKnownVariablesBailOutFor1()
{ {
const char code[] = "void foo() {\n" const char code[] = "void foo() {\n"