diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 6849f4e34a8..22d2e234fb0 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1000,11 +1000,11 @@ void TemplateSimplifier::getTemplateInstantiations() std::string::size_type offset = 0; std::string::size_type pos = 0; while ((pos = nameSpace.find(' ', offset)) != std::string::npos) { - qualificationTok->insertToken(nameSpace.substr(offset, pos - offset), emptyString, true); + qualificationTok->insertTokenBefore(nameSpace.substr(offset, pos - offset)); offset = pos + 1; } - qualificationTok->insertToken(nameSpace.substr(offset), emptyString, true); - qualificationTok->insertToken("::", emptyString, true); + qualificationTok->insertTokenBefore(nameSpace.substr(offset)); + qualificationTok->insertTokenBefore("::"); addInstantiation(tok, it1->scope()); found = true; break; @@ -1635,9 +1635,9 @@ void TemplateSimplifier::expandTemplate( // add forward declarations if (copy && isClass) { - templateDeclaration.token()->insertToken(templateDeclarationToken->strAt(1), emptyString, true); - templateDeclaration.token()->insertToken(newName, emptyString, true); - templateDeclaration.token()->insertToken(";", emptyString, true); + templateDeclaration.token()->insertTokenBefore(templateDeclarationToken->strAt(1)); + templateDeclaration.token()->insertTokenBefore(newName); + templateDeclaration.token()->insertTokenBefore(";"); } else if ((isFunction && (copy || isSpecialization)) || (isVariable && !isSpecialization) || (isClass && isSpecialization && mTemplateSpecializationMap.find(templateDeclaration.token()) != mTemplateSpecializationMap.end())) { @@ -1701,7 +1701,7 @@ void TemplateSimplifier::expandTemplate( } if (isStatic) { - dst->insertToken("static", emptyString, true); + dst->insertTokenBefore("static"); if (start) { dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); @@ -1742,7 +1742,7 @@ void TemplateSimplifier::expandTemplate( ++typeindentlevel; else if (typetok->str() == ")") --typeindentlevel; - dst->insertToken(typetok->str(), typetok->originalName(), true); + dst->insertToken(typetok->str(), typetok->originalName(), typetok->getMacroName(), true); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); Token *previous = dst->previous(); @@ -1770,7 +1770,7 @@ void TemplateSimplifier::expandTemplate( } } if (pointerType && Token::simpleMatch(dst1, "const")) { - dst->insertToken("const", dst1->originalName(), true); + dst->insertToken("const", dst1->originalName(), dst1->getMacroName(), true); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst1->deleteThis(); @@ -1784,13 +1784,13 @@ void TemplateSimplifier::expandTemplate( !(templateDeclaration.isFunction() && templateDeclaration.scope().empty() && (start->strAt(-1) == "." || Token::simpleMatch(start->tokAt(-2), ". template")))) { if (start->strAt(1) != "<" || Token::Match(start, newName.c_str()) || !inAssignment) { - dst->insertToken(newName, emptyString, true); + dst->insertTokenBefore(newName); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); if (start->strAt(1) == "<") start = start->next()->findClosingBracket(); } else { - dst->insertToken(start->str(), emptyString, true); + dst->insertTokenBefore(start->str()); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); newInstantiations.emplace_back(dst->previous(), templateDeclaration.scope()); @@ -1814,7 +1814,7 @@ void TemplateSimplifier::expandTemplate( return Token::simpleMatch(inst.token(), name.c_str(), name.size()); })) { // use the instantiated name - dst->insertToken(name, "", true); + dst->insertTokenBefore(name); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); start = closing; @@ -1822,7 +1822,7 @@ void TemplateSimplifier::expandTemplate( } // just copy the token if it wasn't instantiated if (start != closing) { - dst->insertToken(start->str(), start->originalName(), true); + dst->insertToken(start->str(), start->originalName(), start->getMacroName(), true); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst->previous()->isSigned(start->isSigned()); @@ -1830,7 +1830,7 @@ void TemplateSimplifier::expandTemplate( dst->previous()->isLong(start->isLong()); } } else { - dst->insertToken(start->str(), start->originalName(), true); + dst->insertToken(start->str(), start->originalName(), start->getMacroName(), true); dst->previous()->linenr(start->linenr()); dst->previous()->column(start->column()); dst->previous()->isSigned(start->isSigned()); @@ -1858,7 +1858,7 @@ void TemplateSimplifier::expandTemplate( start = start->next(); } - dst->insertToken(";", emptyString, true); + dst->insertTokenBefore(";"); dst->previous()->linenr(dst->tokAt(-2)->linenr()); dst->previous()->column(dst->tokAt(-2)->column() + 1); diff --git a/lib/token.cpp b/lib/token.cpp index 4d2baa12e82..7e35af1e2d0 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1129,7 +1129,7 @@ void Token::function(const Function *f) tokType(eName); } -Token* Token::insertToken(const std::string& tokenStr, const std::string& originalNameStr, bool prepend) +Token* Token::insertToken(const std::string& tokenStr, const std::string& originalNameStr, const std::string& macroNameStr, bool prepend) { Token *newToken; if (mStr.empty()) @@ -1137,8 +1137,8 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin else newToken = new Token(mTokensFrontBack); newToken->str(tokenStr); - if (!originalNameStr.empty()) - newToken->originalName(originalNameStr); + newToken->originalName(originalNameStr); + newToken->setMacroName(macroNameStr); if (newToken != this) { newToken->mImpl->mLineNumber = mImpl->mLineNumber; diff --git a/lib/token.h b/lib/token.h index cb0beaf51c8..0578a903a93 100644 --- a/lib/token.h +++ b/lib/token.h @@ -850,11 +850,11 @@ class CPPCHECKLIB Token { * @param prepend Insert the new token before this token when it's not * the first one on the tokens list. */ - Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, bool prepend = false); + Token* insertToken(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString, bool prepend = false); - Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr = emptyString) + Token* insertTokenBefore(const std::string& tokenStr, const std::string& originalNameStr = emptyString, const std::string& macroNameStr = emptyString) { - return insertToken(tokenStr, originalNameStr, true); + return insertToken(tokenStr, originalNameStr, macroNameStr, true); } Token* previous() { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5326cff8c45..a549f0b80f3 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7389,11 +7389,11 @@ void Tokenizer::simplifyStaticConst() // Move the qualifier to the left-most position in the declaration tok->deleteNext(); if (!leftTok) { - list.front()->insertToken(qualifiers[i], emptyString, false); + list.front()->insertToken(qualifiers[i]); list.front()->swapWithNext(); tok = list.front(); } else if (leftTok->next()) { - leftTok->next()->insertToken(qualifiers[i], emptyString, true); + leftTok->next()->insertTokenBefore(qualifiers[i]); tok = leftTok->next(); } else { leftTok->insertToken(qualifiers[i]); @@ -10312,7 +10312,7 @@ void Tokenizer::prepareTernaryOpForAST() } if (parenthesesNeeded && tok2 && tok2->str() == ":") { tok->insertToken("("); - tok2->insertToken(")", emptyString, true); + tok2->insertTokenBefore(")"); Token::createMutualLinks(tok->next(), tok2->previous()); } } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 14b560193f6..4e0914d6757 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -223,13 +223,13 @@ void TokenList::addtoken(const Token *tok) return; if (mTokensFrontBack.back) { - mTokensFrontBack.back->insertToken(tok->str(), tok->originalName()); + mTokensFrontBack.back->insertToken(tok->str(), tok->originalName(), tok->getMacroName()); } else { mTokensFrontBack.front = new Token(&mTokensFrontBack); mTokensFrontBack.back = mTokensFrontBack.front; mTokensFrontBack.back->str(tok->str()); - if (!tok->originalName().empty()) - mTokensFrontBack.back->originalName(tok->originalName()); + mTokensFrontBack.back->originalName(tok->originalName()); + mTokensFrontBack.back->setMacroName(tok->getMacroName()); } mTokensFrontBack.back->flags(tok->flags()); diff --git a/test/testother.cpp b/test/testother.cpp index 7c61270d108..71a284d499d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7379,6 +7379,16 @@ class TestOther : public TestFixture { " if (std::is_same_v || std::is_same_v) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkP("#define F(v) (v) != 0\n" // #12392 + "template\n" + "void f() {\n" + " if (F(0)) {}\n" + "}\n" + "void g() {\n" + " f();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateExpressionCompareWithZero() {