Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 53 additions & 50 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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(" ::");
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -2263,33 +2266,33 @@ void Tokenizer::simplifyTypedefCpp()
Token *openParenthesis = nullptr;

if (!castOperator) {
tok2 = simplifyTypedefInsertToken(tok2, "(", location);
tok2 = simplifyTypedefInsertToken(tok2, "(", location, originalName);

openParenthesis = tok2;
}

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);
Expand All @@ -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%"))
Expand Down Expand Up @@ -2349,7 +2352,7 @@ void Tokenizer::simplifyTypedefCpp()
tok2 = tok2->tokAt(3);
}

tok2 = simplifyTypedefInsertToken(tok2, ")", location);
tok2 = simplifyTypedefInsertToken(tok2, ")", location, originalName);
Token::createMutualLinks(tok2, tok3);
}

Expand All @@ -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);

Expand Down
Loading
Loading