Tokenizer: Avoid wrong simplification of template right angle bracket

This commit is contained in:
Daniel Marjamäki 2020-12-02 20:21:32 +01:00
parent 96e3c0a9e0
commit cbb388d458
2 changed files with 22 additions and 3 deletions

View File

@ -5178,9 +5178,14 @@ static std::string getExpression(const Token *tok)
void Tokenizer::splitTemplateRightAngleBrackets(bool check)
{
std::set<std::string> vars;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %type% %type% [;,=]") && tok->next()->isStandardType())
vars.insert(tok->strAt(2));
// Ticket #6181: normalize C++11 template parameter list closing syntax
if (tok->str() == "<" && mTemplateSimplifier->templateParameters(tok)) {
if (tok->str() == "<" && mTemplateSimplifier->templateParameters(tok) && vars.find(tok->previous()->str()) == vars.end()) {
Token *endTok = tok->findClosingBracket();
if (check) {
if (Token::Match(endTok, ">>|>>="))
@ -5195,7 +5200,7 @@ void Tokenizer::splitTemplateRightAngleBrackets(bool check)
endTok->insertToken("=");
endTok->insertToken(">");
}
} else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <")) {
} else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <") && vars.find(tok->next()->str()) == vars.end()) {
Token *endTok = tok->tokAt(2)->findClosingBracket();
if (check) {
if (Token::simpleMatch(endTok, ">>"))

View File

@ -5274,9 +5274,23 @@ private:
void splitTemplateRightAngleBrackets() {
{
const char *code = "; z = x < 0 ? x >> y : x >> y;";
const char code[] = "; z = x < 0 ? x >> y : x >> y;";
ASSERT_EQUALS("; z = x < 0 ? x >> y : x >> y ;", tokenizeAndStringify(code));
}
{
// ftp://ftp.de.debian.org/debian/pool/main/f/ffmpeg/ffmpeg_4.3.1.orig.tar.xz
// ffmpeg-4.3.1/libavcodec/mpeg4videodec.c:376
const char code[] = "void f ( ) {\n"
" int shift_y = ctx->sprite_shift[0];\n"
" int shift_c = ctx->sprite_shift[1];\n"
" if ( shift_c < 0 || shift_y < 0 ||\n"
" FFABS ( sprite_offset [ 0 ] [ i ] ) >= INT_MAX >> shift_y ||\n"
" FFABS ( sprite_offset [ 1 ] [ i ] ) >= INT_MAX >> shift_c ||\n"
" FFABS ( sprite_delta [ 0 ] [ i ] ) >= INT_MAX >> shift_y ||\n"
" FFABS ( sprite_delta [ 1 ] [ i ] ) >= INT_MAX >> shift_y ) ;\n"
"}";
ASSERT_EQUALS(std::string::npos, tokenizeAndStringify(code).find("> >"));
}
}
void cpp03template1() {