| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017 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 "RemovePointer.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 | "Only reduce the level of a pointer var if this pointer is \ |
| 27 | not referenced or dereferenced except being used as an operand of \ |
| 28 | a comparison operator where another operand is 0. We only change \ |
| 29 | the definition of this pointer. \n"; |
| 30 | |
| 31 | static RegisterTransformation<RemovePointer> |
| 32 | Trans("remove-pointer", DescriptionMsg); |
| 33 | |
| 34 | class RemovePointerCollectionVisitor : public |
| 35 | RecursiveASTVisitor<RemovePointerCollectionVisitor> { |
| 36 | |
| 37 | public: |
| 38 | |
| 39 | explicit RemovePointerCollectionVisitor(RemovePointer *Instance) |
| 40 | : ConsumerInstance(Instance), |
| 41 | CurrDeclRefExpr(NULL) |
| 42 | { } |
| 43 | |
| 44 | bool VisitVarDecl(VarDecl *VD); |
| 45 | |
| 46 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 47 | |
| 48 | bool VisitBinaryOperator(BinaryOperator *BO); |
| 49 | |
| 50 | private: |
| 51 | |
| 52 | RemovePointer *ConsumerInstance; |
| 53 | |
| 54 | const DeclRefExpr *CurrDeclRefExpr; |
| 55 | }; |
| 56 | |
| 57 | bool RemovePointerCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 58 | { |
| 59 | ConsumerInstance->handleOneVarDecl(VD); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool RemovePointerCollectionVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) |
| 64 | { |
| 65 | if (CurrDeclRefExpr != DRE) |
| 66 | ConsumerInstance->invalidateOneVarDecl(DRE); |
| 67 | |
| 68 | CurrDeclRefExpr = NULL; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool RemovePointerCollectionVisitor::VisitBinaryOperator(BinaryOperator *BO) |
| 73 | { |
| 74 | if (!BO->isComparisonOp()) |
| 75 | return true; |
| 76 | |
| 77 | const Expr *E = NULL; |
| 78 | if (dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 79 | E = BO->getRHS()->IgnoreParenCasts(); |
| 80 | } |
| 81 | else if (dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParenCasts())) { |
| 82 | E = BO->getLHS()->IgnoreParenCasts(); |
| 83 | } |
| 84 | |
| 85 | if (!E) |
| 86 | return true; |
| 87 | |
| 88 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 89 | if (!DRE) |
| 90 | return true; |
| 91 | |
| 92 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 93 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 94 | if (!VD || dyn_cast<ParmVarDecl>(VD)) |
| 95 | return true; |
| 96 | |
| 97 | if (ConsumerInstance->AllPointerVarDecls.count(VD) && |
| 98 | !(ConsumerInstance->AllInvalidPointerVarDecls.count(VD))) |
| 99 | CurrDeclRefExpr = DRE; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | void RemovePointer::Initialize(ASTContext &context) |
| 104 | { |
| 105 | Transformation::Initialize(context); |
| 106 | CollectionVisitor = new RemovePointerCollectionVisitor(this); |
| 107 | } |
| 108 | |
| 109 | void RemovePointer::HandleTranslationUnit(ASTContext &Ctx) |
| 110 | { |
| 111 | TransAssert(CollectionVisitor && "NULL CollectionVisitor!"); |
| 112 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 113 | doAnalysis(); |
| 114 | |
| 115 | if (QueryInstanceOnly) |
| 116 | return; |
| 117 | |
| 118 | if (TransformationCounter > ValidInstanceNum) { |
| 119 | TransError = TransMaxInstanceError; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 124 | TransAssert(TheVarDecl && "NULL TheVarDecl!"); |
| 125 | |
| 126 | doRewriting(); |
| 127 | |
| 128 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 129 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 130 | TransError = TransInternalError; |
| 131 | } |
| 132 | |
| 133 | void RemovePointer::doAnalysis(void) |
| 134 | { |
| 135 | for (VarDeclSetVector::iterator I = AllPointerVarDecls.begin(), |
| 136 | E = AllPointerVarDecls.end(); I != E; ++I) { |
| 137 | const VarDecl *VD = (*I); |
| 138 | if (AllInvalidPointerVarDecls.count(VD)) |
| 139 | continue; |
| 140 | |
| 141 | ValidInstanceNum++; |
| 142 | if (TransformationCounter == ValidInstanceNum) |
| 143 | TheVarDecl = VD; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void RemovePointer::doRewriting(void) |
| 148 | { |
| 149 | const VarDecl *FirstVD = TheVarDecl->getCanonicalDecl(); |
| 150 | for(VarDecl::redecl_iterator RI = FirstVD->redecls_begin(), |
| 151 | RE = FirstVD->redecls_end(); RI != RE; ++RI) { |
| 152 | RewriteHelper->removeAStarBefore(*RI); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void RemovePointer::invalidateOneVarDecl(const DeclRefExpr *DRE) |
| 157 | { |
| 158 | const ValueDecl *OrigDecl = DRE->getDecl(); |
| 159 | const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 160 | if (!VD || dyn_cast<ParmVarDecl>(VD)) |
| 161 | return; |
| 162 | |
| 163 | const Type *Ty = VD->getType().getTypePtr(); |
| 164 | if (!Ty->isPointerType()) |
| 165 | return; |
| 166 | const VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 167 | AllInvalidPointerVarDecls.insert(CanonicalVD); |
| 168 | } |
| 169 | |
| 170 | void RemovePointer::handleOneVarDecl(const VarDecl *VD) |
| 171 | { |
| 172 | if (isInIncludedFile(VD)) |
| 173 | return; |
| 174 | |
| 175 | if (dyn_cast<ParmVarDecl>(VD)) |
| 176 | return; |
| 177 | |
| 178 | if (VD->getAnyInitializer()) |
| 179 | return; |
| 180 | |
| 181 | const Type *Ty = VD->getType().getTypePtr(); |
| 182 | if (!Ty->isPointerType()) |
| 183 | return; |
| 184 | |
| 185 | AllPointerVarDecls.insert(VD->getCanonicalDecl()); |
| 186 | } |
| 187 | |
| 188 | RemovePointer::~RemovePointer(void) |
| 189 | { |
| 190 | delete CollectionVisitor; |
| 191 | } |
| 192 |