* @throws InternalError thrown if simplification failed */
| 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" |
no test coverage detected