| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016 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 "RemoveUnusedVar.h" |
| 16 | |
| 17 | #include <cctype> |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | |
| 22 | #include "TransformationManager.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const char *DescriptionMsg = |
| 27 | "Remove unused local/global variable declarations. \n"; |
| 28 | |
| 29 | static RegisterTransformation<RemoveUnusedVar> |
| 30 | Trans("remove-unused-var", DescriptionMsg); |
| 31 | |
| 32 | class RemoveUnusedVarAnalysisVisitor : public |
| 33 | RecursiveASTVisitor<RemoveUnusedVarAnalysisVisitor> { |
| 34 | public: |
| 35 | |
| 36 | explicit RemoveUnusedVarAnalysisVisitor(RemoveUnusedVar *Instance) |
| 37 | : ConsumerInstance(Instance) |
| 38 | { } |
| 39 | |
| 40 | bool VisitVarDecl(VarDecl *VD); |
| 41 | |
| 42 | bool VisitDeclStmt(DeclStmt *DS); |
| 43 | |
| 44 | bool VisitCXXCatchStmt(CXXCatchStmt *DS); |
| 45 | |
| 46 | private: |
| 47 | |
| 48 | RemoveUnusedVar *ConsumerInstance; |
| 49 | }; |
| 50 | |
| 51 | bool RemoveUnusedVarAnalysisVisitor::VisitVarDecl(VarDecl *VD) |
| 52 | { |
| 53 | if (ConsumerInstance->isInIncludedFile(VD)) |
| 54 | return true; |
| 55 | |
| 56 | if (VD->isReferenced() || dyn_cast<ParmVarDecl>(VD) || |
| 57 | VD->isStaticDataMember()) |
| 58 | return true; |
| 59 | |
| 60 | SourceRange VarRange = VD->getSourceRange(); |
| 61 | if (VarRange.getEnd().isInvalid()) |
| 62 | return true; |
| 63 | |
| 64 | if (ConsumerInstance->SkippedVars.count(VD->getCanonicalDecl())) |
| 65 | return true; |
| 66 | |
| 67 | ConsumerInstance->ValidInstanceNum++; |
| 68 | if (ConsumerInstance->ToCounter > 0) { |
| 69 | ConsumerInstance->AllValidVarDecls.push_back(VD); |
| 70 | return true; |
| 71 | } |
| 72 | if (ConsumerInstance->ValidInstanceNum == |
| 73 | ConsumerInstance->TransformationCounter) { |
| 74 | ConsumerInstance->TheVarDecl = VD; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool RemoveUnusedVarAnalysisVisitor::VisitDeclStmt(DeclStmt *DS) |
| 80 | { |
| 81 | for (DeclStmt::decl_iterator I = DS->decl_begin(), |
| 82 | E = DS->decl_end(); I != E; ++I) { |
| 83 | VarDecl *CurrDecl = dyn_cast<VarDecl>(*I); |
| 84 | if (CurrDecl) { |
| 85 | DeclGroupRef DGR = DS->getDeclGroup(); |
| 86 | ConsumerInstance->VarToDeclGroup[CurrDecl] = DGR; |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool RemoveUnusedVarAnalysisVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) |
| 93 | { |
| 94 | const VarDecl *VD = S->getExceptionDecl(); |
| 95 | if (VD) { |
| 96 | ConsumerInstance->SkippedVars.insert(VD->getCanonicalDecl()); |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void RemoveUnusedVar::Initialize(ASTContext &context) |
| 102 | { |
| 103 | Transformation::Initialize(context); |
| 104 | AnalysisVisitor = new RemoveUnusedVarAnalysisVisitor(this); |
| 105 | } |
| 106 | |
| 107 | bool RemoveUnusedVar::HandleTopLevelDecl(DeclGroupRef D) |
| 108 | { |
| 109 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 110 | VarDecl *VD = dyn_cast<VarDecl>(*I); |
| 111 | if (VD) |
| 112 | VarToDeclGroup[VD] = D; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void RemoveUnusedVar::HandleTranslationUnit(ASTContext &Ctx) |
| 118 | { |
| 119 | AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 120 | |
| 121 | if (QueryInstanceOnly) |
| 122 | return; |
| 123 | |
| 124 | if (TransformationCounter > ValidInstanceNum) { |
| 125 | TransError = TransMaxInstanceError; |
| 126 | return; |
| 127 | } |
| 128 | if (ToCounter > ValidInstanceNum) { |
| 129 | TransError = TransToCounterTooBigError; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 134 | |
| 135 | doRewriting(); |
| 136 | |
| 137 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 138 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 139 | TransError = TransInternalError; |
| 140 | } |
| 141 | |
| 142 | void RemoveUnusedVar::doRewriting() |
| 143 | { |
| 144 | if (ToCounter <= 0) { |
| 145 | TransAssert(TheVarDecl && "NULL TheVarDecl!"); |
| 146 | removeVarDecl(TheVarDecl); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | TransAssert((TransformationCounter <= |
| 151 | static_cast<int>(AllValidVarDecls.size())) && |
| 152 | "TransformationCounter is larger than the number of decls!"); |
| 153 | TransAssert((ToCounter <= static_cast<int>(AllValidVarDecls.size())) && |
| 154 | "ToCounter is larger than the number of decls!"); |
| 155 | for (int I = ToCounter; I >= TransformationCounter; --I) { |
| 156 | TransAssert((I >= 1) && "Invalid Index!"); |
| 157 | const VarDecl *VD = AllValidVarDecls[I-1]; |
| 158 | TransAssert(VD && "NULL FunctionDecl!"); |
| 159 | removeVarDecl(VD); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void RemoveUnusedVar::removeVarDeclFromLinkageSpecDecl( |
| 164 | const LinkageSpecDecl *LinkageD, const VarDecl *VD) |
| 165 | { |
| 166 | const DeclContext *Ctx = LinkageSpecDecl::castToDeclContext(LinkageD); |
| 167 | unsigned NumDecls = 0; |
| 168 | for (DeclContext::decl_iterator I = Ctx->decls_begin(), E = Ctx->decls_end(); |
| 169 | I != E; ++I) { |
| 170 | NumDecls++; |
| 171 | if (NumDecls > 1) |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | if (NumDecls <= 1) { |
| 176 | RewriteHelper->removeDecl(LinkageD); |
| 177 | return; |
| 178 | } |
| 179 | else { |
| 180 | RewriteHelper->removeVarDecl(VD); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void RemoveUnusedVar::removeVarDecl(const VarDecl *VD) |
| 185 | { |
| 186 | const DeclContext *Ctx = VD->getDeclContext(); |
| 187 | if (const LinkageSpecDecl *LinkageDecl = dyn_cast<LinkageSpecDecl>(Ctx)) { |
| 188 | removeVarDeclFromLinkageSpecDecl(LinkageDecl, VD); |
| 189 | return; |
| 190 | } |
| 191 | else if (dyn_cast<NamespaceDecl>(Ctx)) { |
| 192 | // if a var is declared inside a namespace, we don't know |
| 193 | // which declaration group it belongs to. |
| 194 | RewriteHelper->removeVarDecl(VD); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | llvm::DenseMap<const VarDecl *, DeclGroupRef>::iterator DI = |
| 199 | VarToDeclGroup.find(VD); |
| 200 | if (DI == VarToDeclGroup.end()) { |
| 201 | // We don't know the decl group that the var decl belongs to. |
| 202 | RewriteHelper->removeVarDecl(VD); |
| 203 | } |
| 204 | else { |
| 205 | RewriteHelper->removeVarDecl(VD, (*DI).second); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | RemoveUnusedVar::~RemoveUnusedVar(void) |
| 210 | { |
| 211 | delete AnalysisVisitor; |
| 212 | } |
| 213 |