Tokenizer: some changes to line numbers of some tokens:

simplifyAsm: change line number of newly added 'asm ( )' in order to be the same as next ';'.
simplifyIfAddBraces: Change line number of newly added '}' in order to be the same as next 'else', except for '{ ; } else'.
This commit is contained in:
Edoardo Prezioso 2011-12-12 20:50:49 +01:00
parent dc59fc4391
commit 79b82f115f
2 changed files with 42 additions and 0 deletions

View File

@ -4953,6 +4953,13 @@ bool Tokenizer::simplifyIfAddBraces()
if (tempToken) { if (tempToken) {
tempToken->insertToken("}"); tempToken->insertToken("}");
Token::createMutualLinks(tok, tempToken->next()); Token::createMutualLinks(tok, tempToken->next());
// move '}' in the same line as 'else' if there's it after the new token,
// except for '}' which is after '{ ; }'
tempToken = tempToken->next();
if (!Token::simpleMatch(tempToken->link(), "{ ; }") && tempToken->next() && tempToken->next()->str() == "else" &&
tempToken->next()->linenr() != tempToken->linenr())
tempToken->linenr(tempToken->next()->linenr());
} else { } else {
// Can't insert matching "}" so give up. This is fatal because it // Can't insert matching "}" so give up. This is fatal because it
// causes unbalanced braces. // causes unbalanced braces.
@ -9360,6 +9367,18 @@ void Tokenizer::simplifyAsm()
tok->insertToken("asm"); tok->insertToken("asm");
Token::createMutualLinks(tok->tokAt(2), tok->tokAt(3)); Token::createMutualLinks(tok->tokAt(2), tok->tokAt(3));
//move the new tokens in the same line as ";" if available
tok = tok->tokAt(3);
if (tok->next() && tok->next()->str() == ";" &&
tok->next()->linenr() != tok->linenr()) {
unsigned int endposition = tok->next()->linenr();
tok = tok->tokAt(-3);
for (int i = 0; i < 4; ++i) {
tok = tok->next();
tok->linenr(endposition);
}
}
} }
} }

View File

@ -86,6 +86,7 @@ private:
TEST_CASE(ifAddBraces13); TEST_CASE(ifAddBraces13);
TEST_CASE(ifAddBraces14); // #2610 - segfault: if()<{} TEST_CASE(ifAddBraces14); // #2610 - segfault: if()<{}
TEST_CASE(ifAddBraces15); // #2616 - unknown macro before if TEST_CASE(ifAddBraces15); // #2616 - unknown macro before if
TEST_CASE(ifAddBraces16); // '} else' should be in the same line
TEST_CASE(whileAddBraces); TEST_CASE(whileAddBraces);
TEST_CASE(doWhileAddBraces); TEST_CASE(doWhileAddBraces);
@ -750,6 +751,9 @@ private:
ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm__ (\"fnstcw %0\" : \"= m\" (old_cw));")); ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm__ (\"fnstcw %0\" : \"= m\" (old_cw));"));
ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm __volatile__ (\"ddd\") ;")); ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm __volatile__ (\"ddd\") ;"));
ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(";__asm__ volatile ( \"mov ax,bx\" );")); ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(";__asm__ volatile ( \"mov ax,bx\" );"));
// 'asm ( ) ;' should be in the same line
ASSERT_EQUALS(";\n\nasm ( ) ;", tokenizeAndStringify(";\n\n__asm__ volatile ( \"mov ax,bx\" );", true));
} }
@ -941,6 +945,25 @@ private:
ASSERT_EQUALS("{ A if ( x ) { y ( ) ; } }", tokenizeAndStringify("{A if(x)y();}", false)); ASSERT_EQUALS("{ A if ( x ) { y ( ) ; } }", tokenizeAndStringify("{A if(x)y();}", false));
} }
void ifAddBraces16() {
const char code[] = "void f()\n"
"{\n"
" if (a)\n"
" bar1 ();\n"
" \n"
" else\n"
" bar2 ();\n"
"}\n";
ASSERT_EQUALS("void f ( )\n"
"{\n"
"if ( a ) {\n"
"bar1 ( ) ;\n\n"
"} else {\n"
"bar2 ( ) ; }\n"
"}", tokenizeAndStringify(code, true));
}
void whileAddBraces() { void whileAddBraces() {
const char code[] = ";while(a);"; const char code[] = ";while(a);";