From 79b82f115f699db476b87de68f8830b1584fafbe Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Mon, 12 Dec 2011 20:50:49 +0100 Subject: [PATCH] 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'. --- lib/tokenize.cpp | 19 +++++++++++++++++++ test/testtokenize.cpp | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ee8ca09d3..9f81211e3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4953,6 +4953,13 @@ bool Tokenizer::simplifyIfAddBraces() if (tempToken) { tempToken->insertToken("}"); 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 { // Can't insert matching "}" so give up. This is fatal because it // causes unbalanced braces. @@ -9360,6 +9367,18 @@ void Tokenizer::simplifyAsm() tok->insertToken("asm"); 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); + } + } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 62f9c7a2a..6be3ceb95 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -86,6 +86,7 @@ private: TEST_CASE(ifAddBraces13); TEST_CASE(ifAddBraces14); // #2610 - segfault: if()<{} TEST_CASE(ifAddBraces15); // #2616 - unknown macro before if + TEST_CASE(ifAddBraces16); // '} else' should be in the same line TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); @@ -750,6 +751,9 @@ private: ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm__ (\"fnstcw %0\" : \"= m\" (old_cw));")); ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify("; __asm __volatile__ (\"ddd\") ;")); 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)); } + 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() { const char code[] = ";while(a);";