| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2019, 2020 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "ReplaceUndefinedFunction.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "If there are more than one undefined function with the same type,\ |
| 27 | pick up one and then replace all others with the selected one. \n"; |
| 28 | |
| 29 | static RegisterTransformation<ReplaceUndefinedFunction> |
| 30 | Trans("replace-undefined-function", DescriptionMsg); |
| 31 | |
| 32 | class ReplaceUndefFuncCollectionVisitor : public |
| 33 | RecursiveASTVisitor<ReplaceUndefFuncCollectionVisitor> { |
| 34 | |
| 35 | public: |
| 36 | |
| 37 | explicit ReplaceUndefFuncCollectionVisitor(ReplaceUndefinedFunction *Instance) |
| 38 | : ConsumerInstance(Instance) |
| 39 | { } |
| 40 | |
| 41 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 42 | |
| 43 | private: |
| 44 | |
| 45 | ReplaceUndefinedFunction *ConsumerInstance; |
| 46 | }; |
| 47 | |
| 48 | class ReplaceUndefFuncRewriteVisitor : public |
| 49 | RecursiveASTVisitor<ReplaceUndefFuncRewriteVisitor> { |
| 50 | |
| 51 | public: |
| 52 | |
| 53 | explicit ReplaceUndefFuncRewriteVisitor(ReplaceUndefinedFunction *Instance) |
| 54 | : ConsumerInstance(Instance) |
| 55 | { } |
| 56 | |
| 57 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 58 | |
| 59 | bool VisitCallExpr(CallExpr *CE); |
| 60 | |
| 61 | private: |
| 62 | |
| 63 | ReplaceUndefinedFunction *ConsumerInstance; |
| 64 | }; |
| 65 | |
| 66 | bool ReplaceUndefFuncCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 67 | { |
| 68 | if (ConsumerInstance->isInIncludedFile(FD) || FD->hasBody()) |
| 69 | return true; |
| 70 | ConsumerInstance->handleOneFunctionDecl(FD->getCanonicalDecl()); |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool ReplaceUndefFuncRewriteVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 76 | { |
| 77 | if (FD->getCanonicalDecl() != ConsumerInstance->ReplacedFunctionDecl) |
| 78 | return true; |
| 79 | |
| 80 | TransAssert(!FD->hasBody() && "FD cannot have any definition!"); |
| 81 | ConsumerInstance->RewriteHelper->removeDecl(FD); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool ReplaceUndefFuncRewriteVisitor::VisitCallExpr(CallExpr *CE) |
| 86 | { |
| 87 | FunctionDecl *FD = CE->getDirectCallee(); |
| 88 | // skip CXXMethodDecl for now |
| 89 | if (!FD || dyn_cast<CXXMethodDecl>(FD)) |
| 90 | return true; |
| 91 | |
| 92 | if (FD->getCanonicalDecl() == ConsumerInstance->ReplacedFunctionDecl) { |
| 93 | ConsumerInstance->TheRewriter.ReplaceText(CE->getBeginLoc(), |
| 94 | ConsumerInstance->ReplacedFunctionDecl->getNameAsString().size(), |
| 95 | ConsumerInstance->ReplacingFunctionDecl->getNameAsString()); |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void ReplaceUndefinedFunction::Initialize(ASTContext &context) |
| 101 | { |
| 102 | Transformation::Initialize(context); |
| 103 | CollectionVisitor = new ReplaceUndefFuncCollectionVisitor(this); |
| 104 | RewriteVisitor = new ReplaceUndefFuncRewriteVisitor(this); |
| 105 | } |
| 106 | |
| 107 | void ReplaceUndefinedFunction::HandleTranslationUnit(ASTContext &Ctx) |
| 108 | { |
| 109 | TransAssert(CollectionVisitor && "NULL CollectionVisitor!"); |
| 110 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 111 | doAnalysis(); |
| 112 | |
| 113 | if (QueryInstanceOnly) |
| 114 | return; |
| 115 | |
| 116 | if (TransformationCounter > ValidInstanceNum) { |
| 117 | TransError = TransMaxInstanceError; |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 122 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 123 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 124 | |
| 125 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 126 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 127 | TransError = TransInternalError; |
| 128 | } |
| 129 | |
| 130 | void ReplaceUndefinedFunction::doAnalysis(void) |
| 131 | { |
| 132 | for (FunctionSetMap::iterator I = ReplaceableFunctions.begin(), |
| 133 | E = ReplaceableFunctions.end(); I != E; ++I) { |
| 134 | const FunctionDeclSet *FDSet = (*I).second; |
| 135 | if (!FDSet) |
| 136 | continue; |
| 137 | for (FunctionDeclSet::iterator FI = FDSet->begin(), |
| 138 | FE = FDSet->end(); FI != FE; ++FI) { |
| 139 | ValidInstanceNum++; |
| 140 | if (ValidInstanceNum == TransformationCounter) { |
| 141 | ReplacingFunctionDecl = (*I).first; |
| 142 | ReplacedFunctionDecl = (*FI); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void ReplaceUndefinedFunction::handleOneFunctionDecl(const FunctionDecl *FD) |
| 149 | { |
| 150 | QualType FDQualTy = FD->getType(); |
| 151 | for (FunctionSetMap::iterator I = ReplaceableFunctions.begin(), |
| 152 | E = ReplaceableFunctions.end(); I != E; ++I) { |
| 153 | const FunctionDecl *ReplaceableFD = (*I).first; |
| 154 | FunctionDeclSet *FDSet = (*I).second; |
| 155 | QualType QualTy = ReplaceableFD->getType(); |
| 156 | |
| 157 | if (!Context->hasSameType(FDQualTy, QualTy)) |
| 158 | continue; |
| 159 | |
| 160 | TransAssert(FDSet && "NULL FDSet"); |
| 161 | FDSet->insert(FD); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | FunctionDeclSet *FDSet = new FunctionDeclSet(); |
| 166 | ReplaceableFunctions[FD] = FDSet; |
| 167 | } |
| 168 | |
| 169 | ReplaceUndefinedFunction::~ReplaceUndefinedFunction(void) |
| 170 | { |
| 171 | delete CollectionVisitor; |
| 172 | delete RewriteVisitor; |
| 173 | |
| 174 | for (FunctionSetMap::iterator I = ReplaceableFunctions.begin(), |
| 175 | E = ReplaceableFunctions.end(); I != E; ++I) { |
| 176 | const FunctionDeclSet *FDSet = (*I).second; |
| 177 | delete FDSet; |
| 178 | } |
| 179 | } |
| 180 |