| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2019 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 "ReturnVoid.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 | "Make a function return void. \ |
| 27 | Only change the prototype of the function and \ |
| 28 | delete all return statements in the function, \ |
| 29 | but skip the call sites of this function.\n"; |
| 30 | |
| 31 | static RegisterTransformation<ReturnVoid> |
| 32 | Trans("return-void", DescriptionMsg); |
| 33 | |
| 34 | class RVASTVisitor : public RecursiveASTVisitor<RVASTVisitor> { |
| 35 | public: |
| 36 | explicit RVASTVisitor(ReturnVoid *Instance) |
| 37 | : ConsumerInstance(Instance) |
| 38 | { } |
| 39 | |
| 40 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 41 | |
| 42 | bool VisitReturnStmt(ReturnStmt *RS); |
| 43 | |
| 44 | private: |
| 45 | |
| 46 | ReturnVoid *ConsumerInstance; |
| 47 | |
| 48 | bool rewriteFuncDecl(FunctionDecl *FP); |
| 49 | |
| 50 | bool rewriteReturnStmt(ReturnStmt *RS); |
| 51 | |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | class RVCollectionVisitor : public RecursiveASTVisitor<RVCollectionVisitor> { |
| 56 | public: |
| 57 | explicit RVCollectionVisitor(ReturnVoid *Instance) |
| 58 | : ConsumerInstance(Instance) |
| 59 | { } |
| 60 | |
| 61 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 62 | |
| 63 | private: |
| 64 | |
| 65 | ReturnVoid *ConsumerInstance; |
| 66 | }; |
| 67 | |
| 68 | bool RVCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 69 | { |
| 70 | if (ConsumerInstance->isInIncludedFile(FD)) |
| 71 | return true; |
| 72 | |
| 73 | FunctionDecl *CanonicalDecl = FD->getCanonicalDecl(); |
| 74 | if (ConsumerInstance->isNonVoidReturnFunction(CanonicalDecl)) { |
| 75 | ConsumerInstance->ValidInstanceNum++; |
| 76 | ConsumerInstance->ValidFuncDecls.push_back(CanonicalDecl); |
| 77 | |
| 78 | if (ConsumerInstance->ValidInstanceNum == |
| 79 | ConsumerInstance->TransformationCounter) |
| 80 | ConsumerInstance->TheFuncDecl = CanonicalDecl; |
| 81 | } |
| 82 | |
| 83 | if ((ConsumerInstance->TheFuncDecl == CanonicalDecl) && |
| 84 | FD->isThisDeclarationADefinition()) |
| 85 | ConsumerInstance->keepFuncDefRange(FD); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void ReturnVoid::Initialize(ASTContext &context) |
| 91 | { |
| 92 | Transformation::Initialize(context); |
| 93 | CollectionVisitor = new RVCollectionVisitor(this); |
| 94 | TransformationASTVisitor = new RVASTVisitor(this); |
| 95 | } |
| 96 | |
| 97 | bool ReturnVoid::isNonVoidReturnFunction(FunctionDecl *FD) |
| 98 | { |
| 99 | // Avoid duplications |
| 100 | if (std::find(ValidFuncDecls.begin(), |
| 101 | ValidFuncDecls.end(), FD) != |
| 102 | ValidFuncDecls.end()) |
| 103 | return false; |
| 104 | |
| 105 | // this function happen to have a library function, e.g. strcpy, |
| 106 | // then the type source info won't be available, let's try to |
| 107 | // get one from the one which is in the source |
| 108 | if (!FD->getTypeSourceInfo()) { |
| 109 | const FunctionDecl *FirstFD = FD->getCanonicalDecl(); |
| 110 | FD = NULL; |
| 111 | for (FunctionDecl::redecl_iterator I = FirstFD->redecls_begin(), |
| 112 | E = FirstFD->redecls_end(); I != E; ++I) { |
| 113 | if ((*I)->getTypeSourceInfo()) { |
| 114 | FD = (*I); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | if (!FD) |
| 119 | return false; |
| 120 | } |
| 121 | TypeLoc TLoc = FD->getTypeSourceInfo()->getTypeLoc(); |
| 122 | SourceLocation SLoc = TLoc.getBeginLoc(); |
| 123 | if (SLoc.isInvalid()) |
| 124 | return false; |
| 125 | QualType RVType = FD->getReturnType(); |
| 126 | return !(RVType.getTypePtr()->isVoidType()); |
| 127 | } |
| 128 | |
| 129 | void ReturnVoid::keepFuncDefRange(FunctionDecl *FD) |
| 130 | { |
| 131 | TransAssert(!FuncDefStartPos && !FuncDefEndPos && |
| 132 | "Duplicated function definition?"); |
| 133 | |
| 134 | SourceRange FuncDefRange = FD->getSourceRange(); |
| 135 | |
| 136 | SourceLocation StartLoc = FuncDefRange.getBegin(); |
| 137 | if (StartLoc.isMacroID()) { |
| 138 | StartLoc = SrcManager->getExpansionLoc(StartLoc); |
| 139 | } |
| 140 | FuncDefStartPos = |
| 141 | SrcManager->getCharacterData(StartLoc); |
| 142 | |
| 143 | SourceLocation EndLoc = FuncDefRange.getEnd(); |
| 144 | FuncDefEndPos = |
| 145 | SrcManager->getCharacterData(EndLoc); |
| 146 | } |
| 147 | |
| 148 | bool ReturnVoid::isInTheFuncDef(ReturnStmt *RS) |
| 149 | { |
| 150 | // The candidate function doesn't have a body |
| 151 | if (!FuncDefStartPos) |
| 152 | return false; |
| 153 | |
| 154 | SourceRange RSRange = RS->getSourceRange(); |
| 155 | |
| 156 | SourceLocation StartLoc = RSRange.getBegin(); |
| 157 | if (StartLoc.isMacroID()) { |
| 158 | StartLoc = SrcManager->getExpansionLoc(StartLoc); |
| 159 | } |
| 160 | SourceLocation EndLoc = RSRange.getEnd(); |
| 161 | if (EndLoc.isMacroID()) { |
| 162 | EndLoc = SrcManager->getExpansionLoc(EndLoc); |
| 163 | } |
| 164 | const char *StartPos = |
| 165 | SrcManager->getCharacterData(StartLoc); |
| 166 | const char *EndPos = |
| 167 | SrcManager->getCharacterData(EndLoc); |
| 168 | (void)EndPos; |
| 169 | |
| 170 | if ((StartPos > FuncDefStartPos) && (StartPos < FuncDefEndPos)) { |
| 171 | TransAssert((EndPos > FuncDefStartPos) && (EndPos < FuncDefEndPos) && |
| 172 | "Bad return statement range!"); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | void ReturnVoid::HandleTranslationUnit(ASTContext &Ctx) |
| 180 | { |
| 181 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 182 | |
| 183 | if (QueryInstanceOnly) |
| 184 | return; |
| 185 | |
| 186 | if (TransformationCounter > ValidInstanceNum) { |
| 187 | TransError = TransMaxInstanceError; |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | TransAssert(TransformationASTVisitor && "NULL TransformationASTVisitor!"); |
| 192 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 193 | TransAssert(TheFuncDecl && "NULL TheFuncDecl!"); |
| 194 | |
| 195 | TransformationASTVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 196 | |
| 197 | if (!Rewritten) { |
| 198 | TransError = TransNoTextModificationError; |
| 199 | return; |
| 200 | } |
| 201 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 202 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 203 | TransError = TransInternalError; |
| 204 | } |
| 205 | |
| 206 | bool RVASTVisitor::rewriteFuncDecl(FunctionDecl *FD) |
| 207 | { |
| 208 | DeclarationNameInfo NameInfo = FD->getNameInfo(); |
| 209 | SourceLocation NameInfoStartLoc = NameInfo.getBeginLoc(); |
| 210 | |
| 211 | SourceRange FuncDefRange = FD->getSourceRange(); |
| 212 | SourceLocation FuncStartLoc = FuncDefRange.getBegin(); |
| 213 | |
| 214 | if (FuncStartLoc.isMacroID()) { |
| 215 | FuncStartLoc = ConsumerInstance->SrcManager->getExpansionLoc(FuncStartLoc); |
| 216 | } |
| 217 | |
| 218 | const char *FuncStartBuf = |
| 219 | ConsumerInstance->SrcManager->getCharacterData(FuncStartLoc); |
| 220 | const char *NameInfoStartBuf = |
| 221 | ConsumerInstance->SrcManager->getCharacterData(NameInfoStartLoc); |
| 222 | if (FuncStartBuf == NameInfoStartBuf) { |
| 223 | ConsumerInstance->Rewritten = true; |
| 224 | return !(ConsumerInstance->TheRewriter.InsertText(FuncStartLoc, "void ")); |
| 225 | } |
| 226 | |
| 227 | int Offset = NameInfoStartBuf - FuncStartBuf; |
| 228 | |
| 229 | NameInfoStartBuf--; |
| 230 | while ((*NameInfoStartBuf == '(') || (*NameInfoStartBuf == ' ') || |
| 231 | (*NameInfoStartBuf == '\t') || (*NameInfoStartBuf == '\n')) { |
| 232 | Offset--; |
| 233 | NameInfoStartBuf--; |
| 234 | } |
| 235 | |
| 236 | TransAssert(Offset >= 0); |
| 237 | ConsumerInstance->Rewritten = true; |
| 238 | return !(ConsumerInstance->TheRewriter.ReplaceText(FuncStartLoc, |
| 239 | Offset, "void ")); |
| 240 | } |
| 241 | |
| 242 | bool RVASTVisitor::rewriteReturnStmt(ReturnStmt *RS) |
| 243 | { |
| 244 | // SourceRange RSRange = RS->getSourceRange(); |
| 245 | |
| 246 | // return !(ConsumerInstance->TheRewriter.ReplaceText(RSRange, "return")); |
| 247 | |
| 248 | // Instead of replace an entire ReturnStmt with return, let's keep Ret Expr. |
| 249 | // The reason is that RetExpr could have side-effects and these side-effects |
| 250 | // could cause bugs. But we still could remove "return" keyword |
| 251 | |
| 252 | ConsumerInstance->Rewritten = true; |
| 253 | SourceLocation Loc = RS->getReturnLoc(); |
| 254 | return !(ConsumerInstance->TheRewriter.RemoveText(Loc, 6)); |
| 255 | } |
| 256 | |
| 257 | bool RVASTVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 258 | { |
| 259 | FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 260 | |
| 261 | if (CanonicalFD == ConsumerInstance->TheFuncDecl) |
| 262 | return rewriteFuncDecl(FD); |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | bool RVASTVisitor::VisitReturnStmt(ReturnStmt *RS) |
| 268 | { |
| 269 | if (ConsumerInstance->isInTheFuncDef(RS)) |
| 270 | return rewriteReturnStmt(RS); |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | ReturnVoid::~ReturnVoid(void) |
| 276 | { |
| 277 | delete CollectionVisitor; |
| 278 | delete TransformationASTVisitor; |
| 279 | } |
| 280 |