simplifyIfAddBraces : Further fixes so that braces will be added to an "else" statement too
This commit is contained in:
parent
e7d6809cf4
commit
5a91a2ea12
|
@ -98,7 +98,7 @@ private:
|
||||||
|
|
||||||
{
|
{
|
||||||
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } ";
|
const char code1[] = " void f() { int a; bool use = false; if( use ) a=0; else a=1; int c=1; } ";
|
||||||
const char code2[] = " void f() { int a; bool use = false; a=1; int c=1; } ";
|
const char code2[] = " void f() { int a; bool use = false; { a=1; } int c=1; } ";
|
||||||
ASSERT_EQUALS( tok(code2), tok(code1) );
|
ASSERT_EQUALS( tok(code2), tok(code1) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -234,6 +234,7 @@ private:
|
||||||
const char code[] = "void f()\n"
|
const char code[] = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" if (a);\n"
|
" if (a);\n"
|
||||||
|
" else ;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
// tokenize..
|
// tokenize..
|
||||||
|
@ -246,7 +247,7 @@ private:
|
||||||
std::ostringstream ostr;
|
std::ostringstream ostr;
|
||||||
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
|
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
|
||||||
ostr << " " << tok->str();
|
ostr << " " << tok->str();
|
||||||
ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { ; } }"), ostr.str() );
|
ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { ; } else { ; } }"), ostr.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
void ifAddBraces2()
|
void ifAddBraces2()
|
||||||
|
|
20
tokenize.cpp
20
tokenize.cpp
|
@ -1224,9 +1224,8 @@ bool Tokenizer::simplifyIfAddBraces()
|
||||||
|
|
||||||
for ( TOKEN *tok = _tokens; tok; tok = tok ? tok->next() : NULL )
|
for ( TOKEN *tok = _tokens; tok; tok = tok ? tok->next() : NULL )
|
||||||
{
|
{
|
||||||
if ( ! TOKEN::Match(tok, "if|for|while (") )
|
if ( TOKEN::Match(tok, "if|for|while (") )
|
||||||
continue;
|
{
|
||||||
|
|
||||||
// Goto the ending ')'
|
// Goto the ending ')'
|
||||||
int parlevel = 1;
|
int parlevel = 1;
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
@ -1241,6 +1240,19 @@ bool Tokenizer::simplifyIfAddBraces()
|
||||||
// ')' should be followed by '{'
|
// ')' should be followed by '{'
|
||||||
if (!tok || TOKEN::Match(tok, ") {"))
|
if (!tok || TOKEN::Match(tok, ") {"))
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if ( tok->str() == "else" )
|
||||||
|
{
|
||||||
|
// An else followed by an if or brace don't need to be processed further
|
||||||
|
if ( TOKEN::Match( tok, "else if|{" ) )
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// insert open brace..
|
// insert open brace..
|
||||||
tok->insertToken("{");
|
tok->insertToken("{");
|
||||||
|
@ -1251,7 +1263,7 @@ bool Tokenizer::simplifyIfAddBraces()
|
||||||
// But here are special cases..
|
// But here are special cases..
|
||||||
// * if (cond) for (;;) break;
|
// * if (cond) for (;;) break;
|
||||||
// * if (cond1) if (cond2) { }
|
// * if (cond1) if (cond2) { }
|
||||||
parlevel = 0;
|
int parlevel = 0;
|
||||||
int indentlevel = 0;
|
int indentlevel = 0;
|
||||||
while ( (tok = tok->next()) != NULL )
|
while ( (tok = tok->next()) != NULL )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue