diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1a6d4a4e53f..05434da3341 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1187,21 +1187,23 @@ void Tokenizer::simplifyTypedef() simplifyTypedefCpp(); } -static Token* simplifyTypedefCopyTokens(Token* to, const Token* fromStart, const Token* toEnd, const Token* location) { +static Token* simplifyTypedefCopyTokens(Token* to, const Token* fromStart, const Token* toEnd, const Token* location, const std::string &originalName) { Token* ret = TokenList::copyTokens(to, fromStart, toEnd); for (Token* tok = to->next(); tok != ret->next(); tok = tok->next()) { tok->linenr(location->linenr()); tok->column(location->column()); tok->isSimplifiedTypedef(true); + tok->originalName(originalName); } return ret; } -static Token* simplifyTypedefInsertToken(Token* tok, const std::string& str, const Token* location) { +static Token* simplifyTypedefInsertToken(Token* tok, const std::string& str, const Token* location, const std::string &originalName) { tok = tok->insertToken(str); tok->linenr(location->linenr()); tok->column(location->column()); tok->isSimplifiedTypedef(true); + tok->originalName(originalName); return tok; } @@ -2037,12 +2039,13 @@ void Tokenizer::simplifyTypedefCpp() // start substituting at the typedef name by replacing it with the type const Token* location = tok2; + const std::string originalName = tok2->str(); for (Token* tok3 = typeStart; tok3 && (tok3->str() != ";"); tok3 = tok3->next()) tok3->isSimplifiedTypedef(true); if (isPointerTypeCall) { tok2->deleteThis(); - tok2 = simplifyTypedefInsertToken(tok2, "0", location); - simplifyTypedefInsertToken(tok2->next(), "0", location); + tok2 = simplifyTypedefInsertToken(tok2, "0", location, originalName); + simplifyTypedefInsertToken(tok2->next(), "0", location, originalName); } if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str()) tok2->deletePrevious(); @@ -2051,7 +2054,7 @@ void Tokenizer::simplifyTypedefCpp() tok2->previous()->str("typedef"); tok2->insertToken(tok2->str()); } - tok2->originalName(tok2->str()); + tok2->originalName(originalName); tok2->str(typeStart->str()); // restore qualification if it was removed @@ -2060,12 +2063,12 @@ void Tokenizer::simplifyTypedefCpp() tok2 = tok2->previous(); if (globalScope) { - tok2 = simplifyTypedefInsertToken(tok2, "::", location); + tok2 = simplifyTypedefInsertToken(tok2, "::", location, originalName); } for (std::size_t i = classLevel; i < spaceInfo.size(); ++i) { - tok2 = simplifyTypedefInsertToken(tok2, spaceInfo[i].className, location); - tok2 = simplifyTypedefInsertToken(tok2, "::", location); + tok2 = simplifyTypedefInsertToken(tok2, spaceInfo[i].className, location, originalName); + tok2 = simplifyTypedefInsertToken(tok2, "::", location, originalName); } } @@ -2082,11 +2085,11 @@ void Tokenizer::simplifyTypedefCpp() std::string::size_type spaceIdx = 0; std::string::size_type startIdx = 0; while ((spaceIdx = removed1.find(' ', startIdx)) != std::string::npos) { - simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx, spaceIdx - startIdx), location); + simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx, spaceIdx - startIdx), location, originalName); startIdx = spaceIdx + 1; } - simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx), location); - simplifyTypedefInsertToken(tok2->previous(), "::", location); + simplifyTypedefInsertToken(tok2->previous(), removed1.substr(startIdx), location, originalName); + simplifyTypedefInsertToken(tok2->previous(), "::", location, originalName); break; } idx = removed1.rfind(" ::"); @@ -2098,23 +2101,23 @@ void Tokenizer::simplifyTypedefCpp() } Token* constTok = Token::simpleMatch(tok2->previous(), "const") ? tok2->previous() : nullptr; // add remainder of type - tok2 = simplifyTypedefCopyTokens(tok2, typeStart->next(), typeEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, typeStart->next(), typeEnd, location, originalName); if (!pointers.empty()) { for (const std::string &p : pointers) // cppcheck-suppress useStlAlgorithm - tok2 = simplifyTypedefInsertToken(tok2, p, location); + tok2 = simplifyTypedefInsertToken(tok2, p, location, originalName); if (constTok && !functionPtr) { - tok2 = simplifyTypedefInsertToken(tok2, "const", location); + tok2 = simplifyTypedefInsertToken(tok2, "const", location, originalName); constTok->deleteThis(); location = constTok; } } if (funcStart && funcEnd) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); Token *paren = tok2; - tok2 = simplifyTypedefCopyTokens(tok2, funcStart, funcEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, funcStart, funcEnd, location, originalName); if (!inCast) tok2 = processFunc(tok2, inOperator); @@ -2125,17 +2128,17 @@ void Tokenizer::simplifyTypedefCpp() while (Token::Match(tok2, "%name%|] [")) tok2 = tok2->linkAt(1); - tok2 = simplifyTypedefInsertToken(tok2, ")", location); + tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2, paren); - tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location, originalName); if (specStart) { Token *spec = specStart; - tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location, originalName); while (spec != specEnd) { spec = spec->next(); - tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location, originalName); } } } @@ -2147,20 +2150,20 @@ void Tokenizer::simplifyTypedefCpp() if (!inTemplate && function && tok2->next() && tok2->strAt(1) != "*") needParen = false; if (needParen) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); } Token *tok3 = tok2; if (namespaceStart) { const Token *tok4 = namespaceStart; while (tok4 != namespaceEnd) { - tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location, originalName); tok4 = tok4->next(); } - tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location, originalName); } if (functionPtr) { - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); } if (!inCast) @@ -2170,35 +2173,35 @@ void Tokenizer::simplifyTypedefCpp() if (!tok2) syntaxError(nullptr); - tok2 = simplifyTypedefInsertToken(tok2, ")", location); + tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2, tok3); } if (!tok2) syntaxError(nullptr); - tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location, originalName); if (inTemplate) { tok2 = tok2->next(); } if (specStart) { Token *spec = specStart; - tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location, originalName); while (spec != specEnd) { spec = spec->next(); - tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, spec->str(), location, originalName); } } } else if (functionRetFuncPtr || functionPtrRetFuncPtr) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); Token *tok3 = tok2; - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); Token * tok4 = nullptr; if (functionPtrRetFuncPtr) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); tok4 = tok2; - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); } // skip over variable name if there @@ -2211,24 +2214,24 @@ void Tokenizer::simplifyTypedefCpp() } if (tok4 && functionPtrRetFuncPtr) { - tok2 = simplifyTypedefInsertToken(tok2,")", location); + tok2 = simplifyTypedefInsertToken(tok2,")", location, originalName); Token::createMutualLinks(tok2, tok4); } - tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location, originalName); - tok2 = simplifyTypedefInsertToken(tok2, ")", location); + tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2, tok3); - tok2 = simplifyTypedefCopyTokens(tok2, argFuncRetStart, argFuncRetEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, argFuncRetStart, argFuncRetEnd, location, originalName); } else if (ptrToArray || refToArray) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); Token *tok3 = tok2; if (ptrToArray) - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); else - tok2 = simplifyTypedefInsertToken(tok2, "&", location); + tok2 = simplifyTypedefInsertToken(tok2, "&", location, originalName); bool hasName = false; // skip over name @@ -2247,14 +2250,14 @@ void Tokenizer::simplifyTypedefCpp() tok2 = tok2->linkAt(1); } - simplifyTypedefInsertToken(tok2, ")", location); + simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2->next(), tok3); if (!hasName) tok2 = tok2->next(); } else if (ptrMember) { if (Token::simpleMatch(tok2, "* (")) { - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); } else { // This is the case of casting operator. // Name is not available, and () should not be @@ -2263,7 +2266,7 @@ void Tokenizer::simplifyTypedefCpp() Token *openParenthesis = nullptr; if (!castOperator) { - tok2 = simplifyTypedefInsertToken(tok2, "(", location); + tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName); openParenthesis = tok2; } @@ -2271,25 +2274,25 @@ void Tokenizer::simplifyTypedefCpp() const Token *tok4 = namespaceStart; while (tok4 != namespaceEnd) { - tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, tok4->str(), location, originalName); tok4 = tok4->next(); } - tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location); + tok2 = simplifyTypedefInsertToken(tok2, namespaceEnd->str(), location, originalName); - tok2 = simplifyTypedefInsertToken(tok2, "*", location); + tok2 = simplifyTypedefInsertToken(tok2, "*", location, originalName); if (openParenthesis) { // Skip over name, if any if (Token::Match(tok2->next(), "%name%")) tok2 = tok2->next(); - tok2 = simplifyTypedefInsertToken(tok2, ")", location); + tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2, openParenthesis); } } } else if (typeOf) { - tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, argStart, argEnd, location, originalName); } else if (Token::Match(tok2, "%name% [")) { while (Token::Match(tok2, "%name%|] [")) { tok2 = tok2->linkAt(1); @@ -2311,7 +2314,7 @@ void Tokenizer::simplifyTypedefCpp() // reference or pointer to array? if (Token::Match(tok2, "&|*|&&")) { tok2 = tok2->previous(); - Token *tok3 = simplifyTypedefInsertToken(tok2, "(", location); + Token *tok3 = simplifyTypedefInsertToken(tok2, "(", location, originalName); // handle missing variable name if (Token::Match(tok3, "( *|&|&& *|&|&& %name%")) @@ -2349,7 +2352,7 @@ void Tokenizer::simplifyTypedefCpp() tok2 = tok2->tokAt(3); } - tok2 = simplifyTypedefInsertToken(tok2, ")", location); + tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName); Token::createMutualLinks(tok2, tok3); } @@ -2360,7 +2363,7 @@ void Tokenizer::simplifyTypedefCpp() while (tok2->strAt(1) == "[") tok2 = tok2->linkAt(1); - tok2 = simplifyTypedefCopyTokens(tok2, arrayStart, arrayEnd, location); + tok2 = simplifyTypedefCopyTokens(tok2, arrayStart, arrayEnd, location, originalName); if (!tok2->next()) syntaxError(tok2); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index f8e3eb1cdf6..2afd362371c 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -257,6 +257,8 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedefOriginalName1); TEST_CASE(simplifyTypedefOriginalName2); TEST_CASE(simplifyTypedefOriginalName3); + TEST_CASE(simplifyTypedefOriginalName4); + TEST_CASE(simplifyTypedefOriginalName5); TEST_CASE(simplifyTypedefTokenColumn1); TEST_CASE(simplifyTypedefTokenColumn2); @@ -4619,6 +4621,36 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS("A", token->originalName()); } + void simplifyTypedefOriginalName4() { + const char code[] = "void f(void) {\n" + " typedef unsigned int A;\n" + " A a;\n" + "}\n"; + TokenList tokenlist{ settings1, Standards::Language::C }; + ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c")); + TokenizerTest tokenizer(std::move(tokenlist), *this); + tokenizer.createLinks(); + tokenizer.simplifyTypedef(); + ASSERT_NO_THROW(tokenizer.validate()); + const Token* token = Token::findsimplematch(tokenizer.list.front(), "int"); + ASSERT_EQUALS("A", token->originalName()); + } + + void simplifyTypedefOriginalName5() { + const char code[] = "void f(void) {\n" + " typedef void (*A)(int);\n" + " A a;\n" + "}\n"; + TokenList tokenlist{ settings1, Standards::Language::C }; + ASSERT(TokenListHelper::createTokensFromString(tokenlist, code, "file.c")); + TokenizerTest tokenizer(std::move(tokenlist), *this); + tokenizer.createLinks(); + tokenizer.simplifyTypedef(); + ASSERT_NO_THROW(tokenizer.validate()); + const Token* token = Token::findsimplematch(tokenizer.list.front(), "int"); + ASSERT_EQUALS("A", token->originalName()); + } + void simplifyTypedefTokenColumn1() { // #13155 const char code[] = "void foo(void) {\n" " typedef signed int MY_INT;\n"