diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a0f7abdce..d4db60b95 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3337,6 +3337,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) createLinks(); + // Remove __asm.. + simplifyAsm(); + // Bail out if code is garbage if (const Token *garbage = findGarbageCode()) syntaxError(garbage); @@ -3488,9 +3491,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // syntax error: enum with typedef in it checkForEnumsWithTypedef(); - // Remove __asm.. - simplifyAsm(); - // Add parentheses to ternary operator where necessary prepareTernaryOpForAST(); @@ -8835,14 +8835,31 @@ void Tokenizer::simplifyAsm() } else if (Token::Match(tok, "_asm|__asm")) { - const Token *tok2 = tok; - while (tok2 && (tok2->isNumber() || tok2->isName() || tok2->str() == "," || tok2->str() == ":")) - tok2 = tok2->next(); - if (!tok2 || tok2->str() == ";" || tok2->linenr() != tok->linenr()) { - instruction = tok->next()->stringifyList(tok2); - Token::eraseTokens(tok, tok2); - if (!tok2 || tok2->str() != ";") + Token *endasm = tok->next(); + const Token *firstSemiColon = nullptr; + unsigned int comment = 0; + while (Token::Match(endasm, "%num%|%name%|,|:|;") || (endasm && endasm->linenr() == comment)) { + if (Token::Match(endasm, "_asm|__asm|__endasm")) + break; + if (endasm->str() == ";") { + comment = endasm->linenr(); + if (!firstSemiColon) + firstSemiColon = endasm; + } + endasm = endasm->next(); + } + if (Token::simpleMatch(endasm, "__endasm")) { + instruction = tok->next()->stringifyList(endasm); + Token::eraseTokens(tok, endasm->next()); + if (!Token::simpleMatch(tok->next(), ";")) tok->insertToken(";"); + } else if (firstSemiColon) { + instruction = tok->next()->stringifyList(firstSemiColon); + Token::eraseTokens(tok, firstSemiColon); + } else if (!endasm) { + instruction = tok->next()->stringifyList(endasm); + Token::eraseTokens(tok, endasm); + tok->insertToken(";"); } else continue; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 68f72737f..0eb171137 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1011,7 +1011,8 @@ private: ASSERT_EQUALS("asm ( \"\"ddd\"\" ) ;", tokenizeAndStringify(" __asm __volatile (\"ddd\") ;")); ASSERT_EQUALS("asm ( \"\"mov ax,bx\"\" ) ;", tokenizeAndStringify("__asm__ volatile ( \"mov ax,bx\" );")); ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ; int a ;", tokenizeAndStringify("asm { mov ax,bx } int a;")); - ASSERT_EQUALS("asm\n\n( \"mov ax , bx __endasm\" ) ;", tokenizeAndStringify("__asm\nmov ax,bx\n__endasm;")); + ASSERT_EQUALS("asm\n\n( \"mov ax , bx\" ) ;", tokenizeAndStringify("__asm\nmov ax,bx\n__endasm;")); + ASSERT_EQUALS("asm\n\n( \"push b ; for if\" ) ;", tokenizeAndStringify("__asm\npush b ; for if\n__endasm;")); // 'asm ( ) ;' should be in the same line ASSERT_EQUALS(";\n\nasm ( \"\"mov ax,bx\"\" ) ;", tokenizeAndStringify(";\n\n__asm__ volatile ( \"mov ax,bx\" );", true));