| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef REMOVE_UNUSED_FUNCTION_H |
| 12 |
#define REMOVE_UNUSED_FUNCTION_H |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <map> |
| 16 |
#include <set> |
| 17 |
#include "llvm/ADT/DenseMap.h" |
| 18 |
#include "llvm/ADT/SmallPtrSet.h" |
| 19 |
#include "llvm/ADT/SmallSet.h" |
| 20 |
#include "Transformation.h" |
| 21 |
#include "clang/Basic/SourceLocation.h" |
| 22 |
|
| 23 |
namespace clang { |
| 24 |
class DeclGroupRef; |
| 25 |
class ASTContext; |
| 26 |
class FunctionDecl; |
| 27 |
class UsingDecl; |
| 28 |
class DeclarationName; |
| 29 |
class DeclContext; |
| 30 |
class CXXDependentScopeMemberExpr; |
| 31 |
class FunctionTemplateDecl; |
| 32 |
class UnresolvedLookupExpr; |
| 33 |
class NestedNameSpecifier; |
| 34 |
class MemberExpr; |
| 35 |
class CXXOperatorCallExpr; |
| 36 |
} |
| 37 |
|
| 38 |
class RUFAnalysisVisitor; |
| 39 |
class ExtraReferenceVisitorWrapper; |
| 40 |
|
| 41 |
class RemoveUnusedFunction : public Transformation { |
| 42 |
friend class RUFAnalysisVisitor; |
| 43 |
friend class ExtraReferenceVisitorWrapper; |
| 44 |
|
| 45 |
public: |
| 46 |
|
| 47 |
RemoveUnusedFunction(const char *TransName, const char *Desc) |
| 48 |
: Transformation(TransName, Desc, true), |
| 49 |
AnalysisVisitor(NULL), |
| 50 |
VisitorWrapper(NULL), |
| 51 |
TheFunctionDecl(NULL) |
| 52 |
{ } |
| 53 |
|
| 54 |
~RemoveUnusedFunction(); |
| 55 |
|
| 56 |
void handleOneUsingDecl(const clang::FunctionDecl *CurrentFD, |
| 57 |
const clang::UsingDecl *D); |
| 58 |
|
| 59 |
void handleOneCXXDependentScopeMemberExpr( |
| 60 |
const clang::FunctionDecl *CurrentFD, |
| 61 |
const clang::CXXDependentScopeMemberExpr *E); |
| 62 |
|
| 63 |
void handleOneUnresolvedLookupExpr( |
| 64 |
const clang::FunctionDecl *CurrentFD, |
| 65 |
const clang::UnresolvedLookupExpr *E); |
| 66 |
|
| 67 |
void handleOneMemberExpr(const clang::MemberExpr *ME); |
| 68 |
|
| 69 |
void handleOneCXXOperatorCallExpr(const clang::CXXOperatorCallExpr *E); |
| 70 |
|
| 71 |
void handleOneCallExpr(const clang::CallExpr *E); |
| 72 |
|
| 73 |
void handleOneFunctionDecl(const clang::FunctionDecl *FD); |
| 74 |
|
| 75 |
private: |
| 76 |
|
| 77 |
typedef llvm::SmallVector<const clang::FunctionDecl *, 1000> |
| 78 |
FunctionDeclVector; |
| 79 |
|
| 80 |
typedef llvm::DenseMap<const clang::UsingDecl *, |
| 81 |
const clang::FunctionDecl *> |
| 82 |
UsingFunctionDeclsMap; |
| 83 |
|
| 84 |
typedef llvm::SmallPtrSet<const clang::FunctionDecl *, 32> |
| 85 |
FunctionDeclsSet; |
| 86 |
|
| 87 |
typedef llvm::SmallPtrSet<const clang::FunctionDecl *, 5> |
| 88 |
MemberSpecializationSet; |
| 89 |
|
| 90 |
typedef llvm::DenseMap<const clang::FunctionDecl *, |
| 91 |
MemberSpecializationSet *> |
| 92 |
MemberToSpecializationMap; |
| 93 |
|
| 94 |
typedef llvm::SmallPtrSet<const clang::UsingDecl *, 32> |
| 95 |
UsingDeclsSet; |
| 96 |
|
| 97 |
typedef std::map<std::string, std::string> |
| 98 |
InlinedSystemFunctionsMap; |
| 99 |
|
| 100 |
typedef std::set<std::string> SystemFunctionsSet; |
| 101 |
|
| 102 |
typedef llvm::SmallSet<clang::SourceLocation, 5> LocSet; |
| 103 |
|
| 104 |
virtual void Initialize(clang::ASTContext &context); |
| 105 |
|
| 106 |
virtual void HandleTranslationUnit(clang::ASTContext &Ctx); |
| 107 |
|
| 108 |
void initializeInlinedSystemFunctions(); |
| 109 |
|
| 110 |
void doRewriting(); |
| 111 |
|
| 112 |
bool hasReferencedSpecialization(const clang::FunctionDecl *FD); |
| 113 |
|
| 114 |
clang::SourceLocation getExtensionLocStart(clang::SourceLocation Loc); |
| 115 |
|
| 116 |
void removeOneFunctionDecl(const clang::FunctionDecl *FD); |
| 117 |
|
| 118 |
void removeMemberSpecializations(const clang::FunctionDecl *FD); |
| 119 |
|
| 120 |
void removeRemainingExplicitSpecs(MemberSpecializationSet *ExplicitSpecs); |
| 121 |
|
| 122 |
clang::SourceLocation getFunctionOuterLocStart(const clang::FunctionDecl *FD); |
| 123 |
|
| 124 |
bool hasValidOuterLocStart(const clang::FunctionTemplateDecl *FTD, |
| 125 |
const clang::FunctionDecl *FD); |
| 126 |
|
| 127 |
void removeOneFunctionDeclGroup(const clang::FunctionDecl *FD); |
| 128 |
|
| 129 |
bool isInReferencedSet(const clang::FunctionDecl *FD); |
| 130 |
|
| 131 |
bool hasAtLeastOneValidLocation(const clang::FunctionDecl *FD); |
| 132 |
|
| 133 |
void addOneFunctionDecl(const clang::FunctionDecl *CanonicalFD); |
| 134 |
|
| 135 |
void addOneMemberSpecialization(const clang::FunctionDecl *FD, |
| 136 |
const clang::FunctionDecl *Member); |
| 137 |
|
| 138 |
void createFuncToExplicitSpecs(const clang::FunctionDecl *FD); |
| 139 |
|
| 140 |
void addFuncToExplicitSpecs(const clang::FunctionDecl *FD); |
| 141 |
|
| 142 |
const clang::FunctionDecl *lookupFunctionDeclShallow( |
| 143 |
const clang::DeclarationName &DName, |
| 144 |
const clang::DeclContext *Ctx); |
| 145 |
|
| 146 |
const clang::FunctionDecl *getFunctionDeclFromSpecifier( |
| 147 |
const clang::DeclarationName &Name, |
| 148 |
const clang::NestedNameSpecifier *NNS); |
| 149 |
|
| 150 |
void addOneReferencedFunction(const clang::FunctionDecl *FD); |
| 151 |
|
| 152 |
clang::SourceLocation getFunctionLocEnd(clang::SourceLocation LocStart, |
| 153 |
clang::SourceLocation LocEnd, |
| 154 |
const clang::FunctionDecl *FD); |
| 155 |
|
| 156 |
bool isTokenOperator(clang::SourceLocation Loc); |
| 157 |
|
| 158 |
void removeFunctionExplicitInstantiations(const clang::FunctionDecl *FD); |
| 159 |
|
| 160 |
void removeOneExplicitInstantiation(const clang::FunctionDecl *Spec); |
| 161 |
|
| 162 |
const clang::FunctionTemplateDecl *getTopDescribedTemplate( |
| 163 |
const clang::FunctionDecl *FD); |
| 164 |
|
| 165 |
const clang::FunctionDecl *getSourceFunctionDecl( |
| 166 |
const clang::FunctionDecl *TheFD); |
| 167 |
|
| 168 |
void setInlinedSystemFunctions(const clang::FunctionDecl *FD); |
| 169 |
|
| 170 |
bool isInlinedSystemFunction(const clang::FunctionDecl *FD); |
| 171 |
|
| 172 |
UsingFunctionDeclsMap UsingFDs; |
| 173 |
|
| 174 |
UsingFunctionDeclsMap UsingParentFDs; |
| 175 |
|
| 176 |
FunctionDeclsSet ReferencedFDs; |
| 177 |
|
| 178 |
FunctionDeclsSet VisitedFDs; |
| 179 |
|
| 180 |
FunctionDeclsSet RemovedFDs; |
| 181 |
|
| 182 |
UsingDeclsSet VisitedUsingDecls; |
| 183 |
|
| 184 |
MemberToSpecializationMap MemberToSpecs; |
| 185 |
|
| 186 |
MemberToSpecializationMap FuncToExplicitSpecs; |
| 187 |
|
| 188 |
MemberToSpecializationMap MemberToInstantiations; |
| 189 |
|
| 190 |
InlinedSystemFunctionsMap InlinedSystemFunctions; |
| 191 |
|
| 192 |
SystemFunctionsSet ExistingSystemFunctions; |
| 193 |
|
| 194 |
LocSet VisitedLocations; |
| 195 |
|
| 196 |
LocSet FDGroupLocations; |
| 197 |
|
| 198 |
FunctionDeclVector AllValidFunctionDecls; |
| 199 |
|
| 200 |
RUFAnalysisVisitor *AnalysisVisitor; |
| 201 |
|
| 202 |
ExtraReferenceVisitorWrapper *VisitorWrapper; |
| 203 |
|
| 204 |
const clang::FunctionDecl *TheFunctionDecl; |
| 205 |
|
| 206 |
|
| 207 |
RemoveUnusedFunction(); |
| 208 |
|
| 209 |
RemoveUnusedFunction(const RemoveUnusedFunction &); |
| 210 |
|
| 211 |
void operator=(const RemoveUnusedFunction &); |
| 212 |
}; |
| 213 |
#endif |