blob: 3aa0152ec996f04301ed9455154aac9a0e518de8 [file] [log] [blame]
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +00001//===- IndexBody.cpp - Indexing statements --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "IndexingContext.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12
13using namespace clang;
14using namespace clang::index;
15
16namespace {
17
18class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
19 IndexingContext &IndexCtx;
20 const NamedDecl *Parent;
21 const DeclContext *ParentDC;
22 SmallVector<Stmt*, 16> StmtStack;
23
24 typedef RecursiveASTVisitor<BodyIndexer> base;
25public:
26 BodyIndexer(IndexingContext &indexCtx,
27 const NamedDecl *Parent, const DeclContext *DC)
28 : IndexCtx(indexCtx), Parent(Parent), ParentDC(DC) { }
29
30 bool shouldWalkTypesOfTypeLocs() const { return false; }
31
Argyrios Kyrtzidisa36dd122016-02-13 01:24:19 +000032 bool dataTraverseStmtPre(Stmt *S) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000033 StmtStack.push_back(S);
Argyrios Kyrtzidisa36dd122016-02-13 01:24:19 +000034 return true;
35 }
36
37 bool dataTraverseStmtPost(Stmt *S) {
38 assert(StmtStack.back() == S);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000039 StmtStack.pop_back();
Argyrios Kyrtzidisa36dd122016-02-13 01:24:19 +000040 return true;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000041 }
42
43 bool TraverseTypeLoc(TypeLoc TL) {
44 IndexCtx.indexTypeLoc(TL, Parent, ParentDC);
45 return true;
46 }
47
48 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
49 IndexCtx.indexNestedNameSpecifierLoc(NNS, Parent, ParentDC);
50 return true;
51 }
52
53 SymbolRoleSet getRolesForRef(const Expr *E,
54 SmallVectorImpl<SymbolRelation> &Relations) {
55 SymbolRoleSet Roles{};
56 assert(!StmtStack.empty() && E == StmtStack.back());
57 if (StmtStack.size() == 1)
58 return Roles;
59 auto It = StmtStack.end()-2;
60 while (isa<CastExpr>(*It) || isa<ParenExpr>(*It)) {
61 if (auto ICE = dyn_cast<ImplicitCastExpr>(*It)) {
62 if (ICE->getCastKind() == CK_LValueToRValue)
63 Roles |= (unsigned)(unsigned)SymbolRole::Read;
64 }
65 if (It == StmtStack.begin())
66 break;
67 --It;
68 }
69 const Stmt *Parent = *It;
70
71 if (auto BO = dyn_cast<BinaryOperator>(Parent)) {
72 if (BO->getOpcode() == BO_Assign && BO->getLHS()->IgnoreParenCasts() == E)
73 Roles |= (unsigned)SymbolRole::Write;
74
75 } else if (auto UO = dyn_cast<UnaryOperator>(Parent)) {
76 if (UO->isIncrementDecrementOp()) {
77 Roles |= (unsigned)SymbolRole::Read;
78 Roles |= (unsigned)SymbolRole::Write;
79 } else if (UO->getOpcode() == UO_AddrOf) {
80 Roles |= (unsigned)SymbolRole::AddressOf;
81 }
82
83 } else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
84 if (CA->getLHS()->IgnoreParenCasts() == E) {
85 Roles |= (unsigned)SymbolRole::Read;
86 Roles |= (unsigned)SymbolRole::Write;
87 }
88
89 } else if (auto CE = dyn_cast<CallExpr>(Parent)) {
90 if (CE->getCallee()->IgnoreParenCasts() == E) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +000091 addCallRole(Roles, Relations);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000092 if (auto *ME = dyn_cast<MemberExpr>(E)) {
93 if (auto *CXXMD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
94 if (CXXMD->isVirtual() && !ME->hasQualifier()) {
95 Roles |= (unsigned)SymbolRole::Dynamic;
96 auto BaseTy = ME->getBase()->IgnoreImpCasts()->getType();
97 if (!BaseTy.isNull())
98 if (auto *CXXRD = BaseTy->getPointeeCXXRecordDecl())
99 Relations.emplace_back((unsigned)SymbolRole::RelationReceivedBy,
100 CXXRD);
101 }
102 }
103 } else if (auto CXXOp = dyn_cast<CXXOperatorCallExpr>(CE)) {
104 if (CXXOp->getNumArgs() > 0 && CXXOp->getArg(0)->IgnoreParenCasts() == E) {
105 OverloadedOperatorKind Op = CXXOp->getOperator();
106 if (Op == OO_Equal) {
107 Roles |= (unsigned)SymbolRole::Write;
108 } else if ((Op >= OO_PlusEqual && Op <= OO_PipeEqual) ||
109 Op == OO_LessLessEqual || Op == OO_GreaterGreaterEqual ||
110 Op == OO_PlusPlus || Op == OO_MinusMinus) {
111 Roles |= (unsigned)SymbolRole::Read;
112 Roles |= (unsigned)SymbolRole::Write;
113 } else if (Op == OO_Amp) {
114 Roles |= (unsigned)SymbolRole::AddressOf;
115 }
116 }
117 }
118 }
119
120 return Roles;
121 }
122
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000123 void addCallRole(SymbolRoleSet &Roles,
124 SmallVectorImpl<SymbolRelation> &Relations) {
125 Roles |= (unsigned)SymbolRole::Call;
126 if (auto *FD = dyn_cast<FunctionDecl>(ParentDC))
127 Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, FD);
128 else if (auto *MD = dyn_cast<ObjCMethodDecl>(ParentDC))
129 Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, MD);
130 }
131
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000132 bool VisitDeclRefExpr(DeclRefExpr *E) {
133 SmallVector<SymbolRelation, 4> Relations;
134 SymbolRoleSet Roles = getRolesForRef(E, Relations);
135 return IndexCtx.handleReference(E->getDecl(), E->getLocation(),
136 Parent, ParentDC, Roles, Relations, E);
137 }
138
139 bool VisitMemberExpr(MemberExpr *E) {
140 SourceLocation Loc = E->getMemberLoc();
141 if (Loc.isInvalid())
142 Loc = E->getLocStart();
143 SmallVector<SymbolRelation, 4> Relations;
144 SymbolRoleSet Roles = getRolesForRef(E, Relations);
145 return IndexCtx.handleReference(E->getMemberDecl(), Loc,
146 Parent, ParentDC, Roles, Relations, E);
147 }
148
149 bool VisitDesignatedInitExpr(DesignatedInitExpr *E) {
David Majnemerf7e36092016-06-23 00:15:04 +0000150 for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) {
Argyrios Kyrtzidisf60c3952016-08-03 05:38:53 +0000151 if (D.isFieldDesignator() && D.getField())
David Majnemerf7e36092016-06-23 00:15:04 +0000152 return IndexCtx.handleReference(D.getField(), D.getFieldLoc(), Parent,
153 ParentDC, SymbolRoleSet(), {}, E);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000154 }
155 return true;
156 }
157
158 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
159 SmallVector<SymbolRelation, 4> Relations;
160 SymbolRoleSet Roles = getRolesForRef(E, Relations);
161 return IndexCtx.handleReference(E->getDecl(), E->getLocation(),
162 Parent, ParentDC, Roles, Relations, E);
163 }
164
165 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
166 auto isDynamic = [](const ObjCMessageExpr *MsgE)->bool {
167 if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
168 return false;
169 if (auto *RecE = dyn_cast<ObjCMessageExpr>(
170 MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
171 if (RecE->getMethodFamily() == OMF_alloc)
172 return false;
173 }
174 return true;
175 };
176
177 if (ObjCMethodDecl *MD = E->getMethodDecl()) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000178 SymbolRoleSet Roles{};
179 SmallVector<SymbolRelation, 2> Relations;
180 addCallRole(Roles, Relations);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000181 if (E->isImplicit())
182 Roles |= (unsigned)SymbolRole::Implicit;
183
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000184 if (isDynamic(E)) {
185 Roles |= (unsigned)SymbolRole::Dynamic;
186 if (auto *RecD = E->getReceiverInterface())
187 Relations.emplace_back((unsigned)SymbolRole::RelationReceivedBy, RecD);
188 }
189
190 return IndexCtx.handleReference(MD, E->getSelectorStartLoc(),
191 Parent, ParentDC, Roles, Relations, E);
192 }
193 return true;
194 }
195
196 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
197 if (E->isExplicitProperty())
198 return IndexCtx.handleReference(E->getExplicitProperty(), E->getLocation(),
199 Parent, ParentDC, SymbolRoleSet(), {}, E);
200
201 // No need to do a handleReference for the objc method, because there will
202 // be a message expr as part of PseudoObjectExpr.
203 return true;
204 }
205
206 bool VisitMSPropertyRefExpr(MSPropertyRefExpr *E) {
207 return IndexCtx.handleReference(E->getPropertyDecl(), E->getMemberLoc(),
208 Parent, ParentDC, SymbolRoleSet(), {}, E);
209 }
210
211 bool VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
212 return IndexCtx.handleReference(E->getProtocol(), E->getProtocolIdLoc(),
213 Parent, ParentDC, SymbolRoleSet(), {}, E);
214 }
215
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000216 bool passObjCLiteralMethodCall(const ObjCMethodDecl *MD, const Expr *E) {
217 SymbolRoleSet Roles{};
218 SmallVector<SymbolRelation, 2> Relations;
219 addCallRole(Roles, Relations);
220 Roles |= (unsigned)SymbolRole::Implicit;
221 return IndexCtx.handleReference(MD, E->getLocStart(),
222 Parent, ParentDC, Roles, Relations, E);
223 }
224
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000225 bool VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
226 if (ObjCMethodDecl *MD = E->getBoxingMethod()) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000227 return passObjCLiteralMethodCall(MD, E);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000228 }
229 return true;
230 }
231
232 bool VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
233 if (ObjCMethodDecl *MD = E->getDictWithObjectsMethod()) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000234 return passObjCLiteralMethodCall(MD, E);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000235 }
236 return true;
237 }
238
239 bool VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
240 if (ObjCMethodDecl *MD = E->getArrayWithObjectsMethod()) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000241 return passObjCLiteralMethodCall(MD, E);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000242 }
243 return true;
244 }
245
246 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000247 SymbolRoleSet Roles{};
248 SmallVector<SymbolRelation, 2> Relations;
249 addCallRole(Roles, Relations);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000250 return IndexCtx.handleReference(E->getConstructor(), E->getLocation(),
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000251 Parent, ParentDC, Roles, Relations, E);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000252 }
253
254 bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E,
255 DataRecursionQueue *Q = nullptr) {
256 if (E->getOperatorLoc().isInvalid())
257 return true; // implicit.
Argyrios Kyrtzidisd7511c82016-03-01 02:46:32 +0000258 return base::TraverseCXXOperatorCallExpr(E, Q);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000259 }
260
261 bool VisitDeclStmt(DeclStmt *S) {
262 if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
263 IndexCtx.indexDeclGroupRef(S->getDeclGroup());
264 return true;
265 }
266
267 DeclGroupRef DG = S->getDeclGroup();
268 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
269 const Decl *D = *I;
270 if (!D)
271 continue;
272 if (!IndexCtx.isFunctionLocalDecl(D))
273 IndexCtx.indexTopLevelDecl(D);
274 }
275
276 return true;
277 }
278
Martin Bohmed5f94a62016-08-17 14:59:53 +0000279 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
280 Expr *Init) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000281 if (C->capturesThis() || C->capturesVLAType())
282 return true;
283
284 if (C->capturesVariable() && IndexCtx.shouldIndexFunctionLocalSymbols())
285 return IndexCtx.handleReference(C->getCapturedVar(), C->getLocation(),
286 Parent, ParentDC, SymbolRoleSet());
287
288 // FIXME: Lambda init-captures.
289 return true;
290 }
291
292 // RecursiveASTVisitor visits both syntactic and semantic forms, duplicating
293 // the things that we visit. Make sure to only visit the semantic form.
294 // Also visit things that are in the syntactic form but not the semantic one,
295 // for example the indices in DesignatedInitExprs.
296 bool TraverseInitListExpr(InitListExpr *S, DataRecursionQueue *Q = nullptr) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000297 auto visitForm = [&](InitListExpr *Form) {
298 for (Stmt *SubStmt : Form->children()) {
Argyrios Kyrtzidisd7511c82016-03-01 02:46:32 +0000299 if (!TraverseStmt(SubStmt, Q))
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000300 return false;
301 }
302 return true;
303 };
304
Argyrios Kyrtzidis4db30af2016-11-01 04:29:39 +0000305 auto visitSyntacticDesignatedInitExpr = [&](DesignatedInitExpr *E) -> bool {
306 for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) {
307 if (D.isFieldDesignator())
308 return IndexCtx.handleReference(D.getField(), D.getFieldLoc(),
309 Parent, ParentDC, SymbolRoleSet(),
310 {}, E);
311 }
312 return true;
313 };
314
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000315 InitListExpr *SemaForm = S->isSemanticForm() ? S : S->getSemanticForm();
316 InitListExpr *SyntaxForm = S->isSemanticForm() ? S->getSyntacticForm() : S;
317
318 if (SemaForm) {
319 // Visit things present in syntactic form but not the semantic form.
320 if (SyntaxForm) {
Argyrios Kyrtzidis4db30af2016-11-01 04:29:39 +0000321 for (Expr *init : SyntaxForm->inits()) {
322 if (auto *DIE = dyn_cast<DesignatedInitExpr>(init))
323 visitSyntacticDesignatedInitExpr(DIE);
324 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000325 }
326 return visitForm(SemaForm);
327 }
328
329 // No semantic, try the syntactic.
330 if (SyntaxForm) {
331 return visitForm(SyntaxForm);
332 }
333
334 return true;
335 }
336};
337
338} // anonymous namespace
339
340void IndexingContext::indexBody(const Stmt *S, const NamedDecl *Parent,
341 const DeclContext *DC) {
342 if (!S)
343 return;
344
345 if (!DC)
346 DC = Parent->getLexicalDeclContext();
347 BodyIndexer(*this, Parent, DC).TraverseStmt(const_cast<Stmt*>(S));
348}