Debian Codegraph @

clang_delta/CommonParameterRewriteVisitor.h

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2014 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 #ifndef COMMON_PARAMETER_REWRITE_VISITOR_H
12 #define COMMON_PARAMETER_REWRITE_VISITOR_H
13
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "clang/AST/RecursiveASTVisitor.h"
17
18 template<typename T, typename Trans>
19 class CommonParameterRewriteVisitor : public clang::RecursiveASTVisitor<T> {
20
21 public:
22 explicit CommonParameterRewriteVisitor(Trans *Instance)
23 : ConsumerInstance(Instance)
24 { }
25
26 bool VisitCallExpr(clang::CallExpr *E);
27
28 bool VisitFunctionDecl(clang::FunctionDecl *FD);
29
30 bool VisitCXXConstructExpr(clang::CXXConstructExpr *CE);
31
32 void rewriteAllExprs(void);
33
34 protected:
35
36 clang::SmallVector<clang::CallExpr *, 10> AllCallExprs;
37
38 clang::SmallVector<const clang::CXXConstructExpr *, 5> AllConstructExprs;
39
40 typedef llvm::SmallPtrSet<const clang::DeclContext *, 20> DeclContextSet;
41
42 bool rewriteOneCallExpr(clang::CallExpr *E);
43
44 bool rewriteOneConstructExpr(const clang::CXXConstructExpr *CE);
45
46 Trans *ConsumerInstance;
47 };
48
49 template<typename T, typename Trans>
50 bool CommonParameterRewriteVisitor<T, Trans>::VisitCXXConstructExpr(
51 clang::CXXConstructExpr *CE)
52 {
53 const clang::CXXConstructorDecl *CtorD = CE->getConstructor();
54 if (CtorD->getCanonicalDecl() == ConsumerInstance->TheFuncDecl)
55 AllConstructExprs.push_back(CE);
56 return true;
57 }
58
59 template<typename T, typename Trans>
60 bool CommonParameterRewriteVisitor<T, Trans>::VisitFunctionDecl(
61 clang::FunctionDecl *FD)
62 {
63 clang::FunctionDecl *CanonicalFD = FD->getCanonicalDecl();
64
65 if (CanonicalFD == ConsumerInstance->TheFuncDecl)
66 return ConsumerInstance->rewriteFuncDecl(FD);
67
68 return true;
69 }
70
71 template<typename T, typename Trans>
72 bool CommonParameterRewriteVisitor<T, Trans>::rewriteOneCallExpr(
73 clang::CallExpr *E)
74 {
75 return ConsumerInstance->RewriteHelper->
76 removeArgFromCallExpr(E, ConsumerInstance->TheParamPos);
77 }
78
79 template<typename T, typename Trans>
80 bool CommonParameterRewriteVisitor<T, Trans>::rewriteOneConstructExpr(
81 const clang::CXXConstructExpr *CE)
82 {
83 return ConsumerInstance->RewriteHelper->
84 removeArgFromCXXConstructExpr(CE, ConsumerInstance->TheParamPos);
85 }
86
87 template<typename T, typename Trans>
88 void CommonParameterRewriteVisitor<T, Trans>::rewriteAllExprs(void)
89 {
90 while (!AllCallExprs.empty()) {
91 clang::CallExpr *CallE = AllCallExprs.pop_back_val();
92 rewriteOneCallExpr(CallE);
93 }
94
95 while (!AllConstructExprs.empty()) {
96 const clang::CXXConstructExpr *CE = AllConstructExprs.pop_back_val();
97 rewriteOneConstructExpr(CE);
98 }
99 }
100
101 template<typename T, typename Trans>
102 bool CommonParameterRewriteVisitor<T, Trans>::VisitCallExpr(
103 clang::CallExpr *CallE)
104 {
105 const clang::FunctionDecl *CalleeDecl = NULL;
106 const clang::Expr *E = CallE->getCallee();
107 if (const clang::UnresolvedLookupExpr *UE =
108 llvm::dyn_cast<clang::UnresolvedLookupExpr>(E)) {
109 clang::DeclarationName DName = UE->getName();
110 TransAssert(((DName.getNameKind() == clang::DeclarationName::Identifier) ||
111 (DName.getNameKind() ==
112 clang::DeclarationName::CXXOperatorName)) &&
113 "Not an indentifier!");
114 if (const clang::NestedNameSpecifier *NNS = UE->getQualifier()) {
115 if (const clang::DeclContext *Ctx =
116 ConsumerInstance->getDeclContextFromSpecifier(NNS)) {
117 DeclContextSet VisitedCtxs;
118 CalleeDecl =
119 ConsumerInstance->lookupFunctionDecl(DName, Ctx, VisitedCtxs);
120 }
121 }
122 if (!CalleeDecl) {
123 DeclContextSet VisitedCtxs;
124 CalleeDecl = ConsumerInstance->lookupFunctionDecl(DName,
125 ConsumerInstance->TheFuncDecl->getLookupParent(),
126 VisitedCtxs);
127 }
128 if (!CalleeDecl)
129 return true;
130 }
131 else {
132 CalleeDecl = CallE->getDirectCallee();
133 if (!CalleeDecl) {
134 return true;
135 }
136 }
137
138 if (clang::FunctionTemplateDecl *TheTmplFuncD =
139 ConsumerInstance->TheFuncDecl->getDescribedFunctionTemplate()) {
140 clang::FunctionTemplateDecl *TmplFuncD;
141 if (CalleeDecl->isTemplateInstantiation())
142 TmplFuncD = CalleeDecl->getPrimaryTemplate();
143 else
144 TmplFuncD = CalleeDecl->getDescribedFunctionTemplate();
145 if (!TmplFuncD ||
146 (TmplFuncD->getCanonicalDecl() != TheTmplFuncD->getCanonicalDecl()))
147 return true;
148 }
149
150 if (clang::FunctionDecl *InstFuncDecl =
151 CalleeDecl->getInstantiatedFromMemberFunction()) {
152 CalleeDecl = InstFuncDecl;
153 }
154
155 if (CalleeDecl->getCanonicalDecl() != ConsumerInstance->TheFuncDecl) {
156 return true;
157 }
158
159 // We now have a correct CallExpr
160 // Here we only collect these valid CallExprs, and
161 // will rewrite them later in a reverse order.
162 // The reason is that if we have code like below:
163 // foo(foo(1));
164 // we want to rewrite the nested foo(1) first.
165 // If we rewrite the outside foo first, we will
166 // end up with bad transformation when we try to
167 // rewrite foo(1), which has been removed.
168 AllCallExprs.push_back(CallE);
169 return true;
170 }
171
172 #endif