Debian Codegraph @

clang_delta/SimplifyStructUnionDecl.cpp

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Copyright (c) 2012, 2013, 2014, 2015, 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 "SimplifyStructUnionDecl.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 "This pass combines the declaration of global vars declared \
27 as struct/union with the corresponding struct/union \
28 declaration. \n";
29
30 static RegisterTransformation<SimplifyStructUnionDecl>
31 Trans("simplify-struct-union-decl", DescriptionMsg);
32
33 class SimplifyStructUnionDeclVisitor : public
34 RecursiveASTVisitor<SimplifyStructUnionDeclVisitor> {
35
36 public:
37 explicit SimplifyStructUnionDeclVisitor(SimplifyStructUnionDecl *Instance)
38 : ConsumerInstance(Instance)
39 { }
40
41 bool VisitFunctionDecl(FunctionDecl *FD);
42
43 bool VisitVarDecl(VarDecl *VD);
44
45 bool VisitFieldDecl(FieldDecl *FD);
46
47 private:
48
49 SimplifyStructUnionDecl *ConsumerInstance;
50 };
51
52 bool SimplifyStructUnionDeclVisitor::VisitFunctionDecl(FunctionDecl *FD)
53 {
54 const Type *T = FD->getReturnType().getTypePtr();
55 return ConsumerInstance->handleOneDeclarator(T);
56 }
57
58 bool SimplifyStructUnionDeclVisitor::VisitVarDecl(VarDecl *VD)
59 {
60 if (ConsumerInstance->CombinedVars.count(VD))
61 return true;
62
63 const Type *T = VD->getType().getTypePtr();
64 return ConsumerInstance->handleOneDeclarator(T);
65 }
66
67 bool SimplifyStructUnionDeclVisitor::VisitFieldDecl(FieldDecl *FD)
68 {
69 const Type *T = FD->getType().getTypePtr();
70 return ConsumerInstance->handleOneDeclarator(T);
71 }
72
73 void SimplifyStructUnionDecl::Initialize(ASTContext &context)
74 {
75 Transformation::Initialize(context);
76 AnalysisVisitor = new SimplifyStructUnionDeclVisitor(this);
77 }
78
79 bool SimplifyStructUnionDecl::HandleTopLevelDecl(DeclGroupRef DGR)
80 {
81 DeclGroupRef::iterator DI = DGR.begin();
82 if (isInIncludedFile(*DI))
83 return true;
84
85 const RecordDecl *RD = dyn_cast<RecordDecl>(*DI);
86 if (RD) {
87 addOneRecordDecl(RD, DGR);
88 return true;
89 }
90
91 VarDecl *VD = dyn_cast<VarDecl>(*DI);
92 if (!VD)
93 return true;
94
95 const Type *T = VD->getType().getTypePtr();
96
97 RD = getBaseRecordDecl(T);
98 if (!RD)
99 return true;
100
101 const Decl *CanonicalD = RD->getCanonicalDecl();
102 void *DGRPointer = RecordDeclToDeclGroup[CanonicalD];
103 if (!DGRPointer)
104 return true;
105
106 ValidInstanceNum++;
107 if (ValidInstanceNum != TransformationCounter)
108 return true;
109
110 TheRecordDecl = dyn_cast<RecordDecl>(CanonicalD);
111 TheDeclGroupRefs.push_back(DGRPointer);
112 TheDeclGroupRefs.push_back(DGR.getAsOpaquePtr());
113
114 for (DeclGroupRef::iterator I = DGR.begin(), E = DGR.end(); I != E; ++I) {
115 VarDecl *VD = dyn_cast<VarDecl>(*I);
116 TransAssert(VD && "Bad VarDecl!");
117 CombinedVars.insert(VD);
118 }
119
120 DeclGroupRef DefDGR = DeclGroupRef::getFromOpaquePtr(DGRPointer);
121 for (DeclGroupRef::iterator I = DefDGR.begin(),
122 E = DefDGR.end(); I != E; ++I) {
123 VarDecl *VD = dyn_cast<VarDecl>(*I);
124 if (VD)
125 CombinedVars.insert(VD);
126 }
127 return true;
128 }
129
130 void SimplifyStructUnionDecl::HandleTranslationUnit(ASTContext &Ctx)
131 {
132 if (QueryInstanceOnly) {
133 return;
134 }
135
136 if (TransformationCounter > ValidInstanceNum) {
137 TransError = TransMaxInstanceError;
138 return;
139 }
140
141 Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
142
143 TransAssert(AnalysisVisitor && "NULL AnalysisVisitor!");
144 TransAssert(TheRecordDecl && "NULL RecordDecl!");
145 AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());
146 doCombination();
147
148 if (Ctx.getDiagnostics().hasErrorOccurred() ||
149 Ctx.getDiagnostics().hasFatalErrorOccurred())
150 TransError = TransInternalError;
151 }
152
153 void SimplifyStructUnionDecl::doCombination(void)
154 {
155 TransAssert(TheDeclGroupRefs.size() == 2);
156 void *P2 = TheDeclGroupRefs.pop_back_val();
157 void *P1 = TheDeclGroupRefs.pop_back_val();
158
159 DeclGroupRef FirstDGR = DeclGroupRef::getFromOpaquePtr(P1);
160 DeclGroupRef SecondDGR = DeclGroupRef::getFromOpaquePtr(P2);
161
162 SourceLocation EndLoc =
163 RewriteHelper->getDeclGroupRefEndLoc(FirstDGR);
164
165 std::string DStr;
166 RewriteHelper->getDeclGroupStrAndRemove(SecondDGR, DStr);
167
168 // it's a single RecordDecl
169 if (FirstDGR.isSingleDecl())
170 TheRewriter.InsertText(EndLoc, DStr, /*InsertAfter=*/false);
171 else
172 TheRewriter.InsertText(EndLoc, ", " + DStr, /*InsertAfter=*/false);
173
174 if (isSafeToRemoveName()) {
175 SourceLocation NameLocStart = TheRecordDecl->getLocation();
176 std::string Name = TheRecordDecl->getNameAsString();
177 TheRewriter.RemoveText(NameLocStart, Name.size());
178 }
179 }
180
181 bool SimplifyStructUnionDecl::isSafeToRemoveName(void)
182 {
183 if (!SafeToRemoveName)
184 return false;
185
186 const RecordDecl *RD =
187 dyn_cast<RecordDecl>(TheRecordDecl->getCanonicalDecl());
188 RecordDecl::redecl_iterator I = RD->redecls_begin();
189 RecordDecl::redecl_iterator E = RD->redecls_end();
190 ++I;
191 return (I == E);
192 }
193
194 void SimplifyStructUnionDecl::addOneRecordDecl(const RecordDecl *RD,
195 DeclGroupRef DGR)
196 {
197 const Decl *CanonicalD = RD->getCanonicalDecl();
198
199 const RecordDecl *RDDef = RD->getDefinition();
200 if (!RDDef || (RD != RDDef))
201 return;
202
203 if (!RecordDeclToDeclGroup[CanonicalD])
204 RecordDeclToDeclGroup[CanonicalD] = (DGR.getAsOpaquePtr());
205 }
206
207 const RecordDecl *SimplifyStructUnionDecl::getBaseRecordDecl(const Type *T)
208 {
209 const ArrayType *ArrayTy = dyn_cast<ArrayType>(T);
210 if (ArrayTy)
211 T = getArrayBaseElemType(ArrayTy);
212 if (T->isPointerType())
213 T = getBasePointerElemType(T);
214
215 const RecordType *RT = NULL;
216 if (T->isUnionType())
217 RT = T->getAsUnionType();
218 else if (T->isStructureType())
219 RT = T->getAsStructureType();
220 else
221 return NULL;
222
223 return RT->getDecl();
224 }
225
226 bool SimplifyStructUnionDecl::handleOneDeclarator(const Type *Ty)
227 {
228 const RecordDecl *RD = getBaseRecordDecl(Ty);
229 if (!RD)
230 return true;
231
232 const RecordDecl *CanonicalRD = dyn_cast<RecordDecl>(RD->getCanonicalDecl());
233 if (CanonicalRD == TheRecordDecl) {
234 SafeToRemoveName = false;
235 }
236 return SafeToRemoveName;
237 }
238
239 SimplifyStructUnionDecl::~SimplifyStructUnionDecl(void)
240 {
241 delete AnalysisVisitor;
242 }
243