MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / replace

Method replace

lib/tokenize.cpp:675–884  ·  view source on GitHub ↗

* @throws InternalError thrown if simplification failed */

Source from the content-addressed store, hash-verified

673 * @throws InternalError thrown if simplification failed
674 */
675 void replace(Token* tok, const std::string &originalname) {
676 if (tok == mNameToken)
677 return;
678
679 mUsed = true;
680 const bool isFunctionPointer = Tokenizer::isFunctionPointer(mNameToken);
681
682 // Special handling for T(...) when T is a pointer
683 if (Token::Match(tok, "%name% [({]") && !isFunctionPointer && !Token::simpleMatch(tok->linkAt(1), ") (")) {
684 bool pointerType = false;
685 for (const Token* type = mRangeType.first; type != mRangeType.second; type = type->next()) {
686 if (type->str() == "*" || type->str() == "&") {
687 pointerType = true;
688 break;
689 }
690 }
691 for (const Token* type = mRangeTypeQualifiers.first; type != mRangeTypeQualifiers.second; type = type->next()) {
692 if (type->str() == "*" || type->str() == "&") {
693 pointerType = true;
694 break;
695 }
696 }
697 if (pointerType) {
698 tok->tokAt(1)->str("(");
699 tok->linkAt(1)->str(")");
700 if (tok->linkAt(1) == tok->tokAt(2)) { // T() or T{}
701 tok->deleteThis();
702 tok->next()->insertToken("0");
703 Token* tok2 = insertTokens(tok, mRangeType);
704 insertTokens(tok2, mRangeTypeQualifiers);
705 }
706 else { // functional-style cast
707 tok->originalName(originalname);
708 tok->isSimplifiedTypedef(true);
709 tok->str("(");
710 Token* tok2 = insertTokens(tok, mRangeType);
711 tok2 = insertTokens(tok2, mRangeTypeQualifiers);
712 Token* tok3 = tok2->insertToken(")");
713 Token::createMutualLinks(tok, tok3);
714 tok->insertTokenBefore("(");
715 tok3 = tok3->linkAt(1);
716 tok3 = tok3->insertToken(")");
717 Token::createMutualLinks(tok->tokAt(-1), tok3);
718 }
719 return;
720 }
721 }
722
723 // Special handling of function pointer cast
724 if (isFunctionPointer && isCast(tok->previous())) {
725 tok->insertToken("*");
726 Token* const tok_1 = insertTokens(tok, std::pair<Token*, Token*>(mRangeType.first, mNameToken->linkAt(1)));
727 tok_1->originalName(originalname);
728 tok->deleteThis();
729 return;
730 }
731
732 // Inherited type => skip "struct" / "class"

Callers 2

simplifyTypedefMethod · 0.45
simplifyStringMethod · 0.45

Calls 13

isFunctionPointerFunction · 0.85
linkAtMethod · 0.80
nextMethod · 0.80
deleteThisMethod · 0.80
insertTokenMethod · 0.80
columnMethod · 0.80
deletePreviousMethod · 0.80
deleteNextMethod · 0.80
simpleMatchFunction · 0.70
isCastFunction · 0.70
strMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected