Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXIndexDataConsumer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CXIndexDataConsumer.cpp - Index data consumer for libclang----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "CXIndexDataConsumer.h"
10
#include "CIndexDiagnostic.h"
11
#include "CXTranslationUnit.h"
12
#include "clang/AST/Attr.h"
13
#include "clang/AST/DeclCXX.h"
14
#include "clang/AST/DeclTemplate.h"
15
#include "clang/AST/DeclVisitor.h"
16
#include "clang/Frontend/ASTUnit.h"
17
18
using namespace clang;
19
using namespace clang::index;
20
using namespace cxindex;
21
using namespace cxcursor;
22
23
namespace {
24
class IndexingDeclVisitor : public ConstDeclVisitor<IndexingDeclVisitor, bool> {
25
  CXIndexDataConsumer &DataConsumer;
26
  SourceLocation DeclLoc;
27
  const DeclContext *LexicalDC;
28
29
public:
30
  IndexingDeclVisitor(CXIndexDataConsumer &dataConsumer, SourceLocation Loc,
31
                      const DeclContext *lexicalDC)
32
365
    : DataConsumer(dataConsumer), DeclLoc(Loc), LexicalDC(lexicalDC) { }
33
34
89
  bool VisitFunctionDecl(const FunctionDecl *D) {
35
89
    DataConsumer.handleFunction(D);
36
89
    return true;
37
89
  }
38
39
55
  bool VisitVarDecl(const VarDecl *D) {
40
55
    DataConsumer.handleVar(D);
41
55
    return true;
42
55
  }
43
44
13
  bool VisitFieldDecl(const FieldDecl *D) {
45
13
    DataConsumer.handleField(D);
46
13
    return true;
47
13
  }
48
49
0
  bool VisitMSPropertyDecl(const MSPropertyDecl *D) {
50
0
    return true;
51
0
  }
52
53
2
  bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
54
2
    DataConsumer.handleEnumerator(D);
55
2
    return true;
56
2
  }
57
58
12
  bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
59
12
    DataConsumer.handleTypedefName(D);
60
12
    return true;
61
12
  }
62
63
39
  bool VisitTagDecl(const TagDecl *D) {
64
39
    DataConsumer.handleTagDecl(D);
65
39
    return true;
66
39
  }
67
68
27
  bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
69
27
    DataConsumer.handleObjCInterface(D);
70
27
    return true;
71
27
  }
72
73
5
  bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
74
5
    DataConsumer.handleObjCProtocol(D);
75
5
    return true;
76
5
  }
77
78
4
  bool VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
79
4
    DataConsumer.handleObjCImplementation(D);
80
4
    return true;
81
4
  }
82
83
5
  bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
84
5
    DataConsumer.handleObjCCategory(D);
85
5
    return true;
86
5
  }
87
88
0
  bool VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
89
0
    DataConsumer.handleObjCCategoryImpl(D);
90
0
    return true;
91
0
  }
92
93
48
  bool VisitObjCMethodDecl(const ObjCMethodDecl *D) {
94
48
    if (isa<ObjCImplDecl>(LexicalDC) && 
!D->isThisDeclarationADefinition()9
)
95
6
      DataConsumer.handleSynthesizedObjCMethod(D, DeclLoc, LexicalDC);
96
42
    else
97
42
      DataConsumer.handleObjCMethod(D, DeclLoc);
98
48
    return true;
99
48
  }
100
101
8
  bool VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
102
8
    DataConsumer.handleObjCProperty(D);
103
8
    return true;
104
8
  }
105
106
4
  bool VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
107
4
    DataConsumer.handleSynthesizedObjCProperty(D);
108
4
    return true;
109
4
  }
110
111
8
  bool VisitNamespaceDecl(const NamespaceDecl *D) {
112
8
    DataConsumer.handleNamespace(D);
113
8
    return true;
114
8
  }
115
116
1
  bool VisitUsingDecl(const UsingDecl *D) {
117
1
    return true;
118
1
  }
119
120
0
  bool VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
121
0
    return true;
122
0
  }
123
124
0
  bool VisitClassTemplateDecl(const ClassTemplateDecl *D) {
125
0
    DataConsumer.handleClassTemplate(D);
126
0
    return true;
127
0
  }
128
129
  bool VisitClassTemplateSpecializationDecl(const
130
2
                                           ClassTemplateSpecializationDecl *D) {
131
2
    DataConsumer.handleTagDecl(D);
132
2
    return true;
133
2
  }
134
135
0
  bool VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
136
0
    DataConsumer.handleFunctionTemplate(D);
137
0
    return true;
138
0
  }
139
140
0
  bool VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
141
0
    DataConsumer.handleTypeAliasTemplate(D);
142
0
    return true;
143
0
  }
144
145
43
  bool VisitImportDecl(const ImportDecl *D) {
146
43
    DataConsumer.importedModule(D);
147
43
    return true;
148
43
  }
149
};
150
151
1.91k
CXSymbolRole getSymbolRole(SymbolRoleSet Role) {
152
  // CXSymbolRole mirrors low 9 bits of clang::index::SymbolRole.
153
1.91k
  return CXSymbolRole(static_cast<uint32_t>(Role) & ((1 << 9) - 1));
154
1.91k
}
155
}
156
157
bool CXIndexDataConsumer::handleDeclOccurrence(
158
    const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations,
159
2.23k
    SourceLocation Loc, ASTNodeInfo ASTNode) {
160
2.23k
  Loc = getASTContext().getSourceManager().getFileLoc(Loc);
161
162
2.23k
  if (Roles & (unsigned)SymbolRole::Reference) {
163
1.91k
    const NamedDecl *ND = dyn_cast<NamedDecl>(D);
164
1.91k
    if (!ND)
165
0
      return true;
166
167
1.91k
    if (auto *ObjCID = dyn_cast_or_null<ObjCInterfaceDecl>(ASTNode.OrigD)) {
168
27
      if (!ObjCID->isThisDeclarationADefinition() &&
169
9
          ObjCID->getLocation() == Loc) {
170
        // The libclang API treats this as ObjCClassRef declaration.
171
6
        IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCID);
172
6
        return true;
173
6
      }
174
1.91k
    }
175
1.91k
    if (auto *ObjCPD = dyn_cast_or_null<ObjCProtocolDecl>(ASTNode.OrigD)) {
176
5
      if (!ObjCPD->isThisDeclarationADefinition() &&
177
1
          ObjCPD->getLocation() == Loc) {
178
        // The libclang API treats this as ObjCProtocolRef declaration.
179
1
        IndexingDeclVisitor(*this, Loc, nullptr).Visit(ObjCPD);
180
1
        return true;
181
1
      }
182
1.91k
    }
183
184
1.91k
    CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct;
185
1.91k
    if (Roles & (unsigned)SymbolRole::Implicit) {
186
8
      Kind = CXIdxEntityRef_Implicit;
187
8
    }
188
1.91k
    CXSymbolRole CXRole = getSymbolRole(Roles);
189
190
1.91k
    CXCursor Cursor;
191
1.91k
    if (ASTNode.OrigE) {
192
1.81k
      Cursor = cxcursor::MakeCXCursor(ASTNode.OrigE,
193
1.81k
                                      cast<Decl>(ASTNode.ContainerDC),
194
1.81k
                                      getCXTU());
195
94
    } else {
196
94
      if (ASTNode.OrigD) {
197
94
        if (auto *OrigND = dyn_cast<NamedDecl>(ASTNode.OrigD))
198
94
          Cursor = getRefCursor(OrigND, Loc);
199
0
        else
200
0
          Cursor = MakeCXCursor(ASTNode.OrigD, CXTU);
201
0
      } else {
202
0
        Cursor = getRefCursor(ND, Loc);
203
0
      }
204
94
    }
205
1.91k
    handleReference(ND, Loc, Cursor,
206
1.91k
                    dyn_cast_or_null<NamedDecl>(ASTNode.Parent),
207
1.91k
                    ASTNode.ContainerDC, ASTNode.OrigE, Kind, CXRole);
208
209
315
  } else {
210
315
    const DeclContext *LexicalDC = ASTNode.ContainerDC;
211
315
    if (!LexicalDC) {
212
0
      for (const auto &SymRel : Relations) {
213
0
        if (SymRel.Roles & (unsigned)SymbolRole::RelationChildOf)
214
0
          LexicalDC = dyn_cast<DeclContext>(SymRel.RelatedSymbol);
215
0
      }
216
0
    }
217
315
    IndexingDeclVisitor(*this, Loc, LexicalDC).Visit(ASTNode.OrigD);
218
315
  }
219
220
2.22k
  return !shouldAbort();
221
2.23k
}
222
223
bool CXIndexDataConsumer::handleModuleOccurrence(const ImportDecl *ImportD,
224
                                                 const Module *Mod,
225
                                                 SymbolRoleSet Roles,
226
45
                                                 SourceLocation Loc) {
227
45
  if (Roles & (SymbolRoleSet)SymbolRole::Declaration)
228
43
    IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
229
45
  return !shouldAbort();
230
45
}
231
232
48
void CXIndexDataConsumer::finish() {
233
48
  indexDiagnostics();
234
48
}
235
236
237
CXIndexDataConsumer::ObjCProtocolListInfo::ObjCProtocolListInfo(
238
                                    const ObjCProtocolList &ProtList,
239
                                    CXIndexDataConsumer &IdxCtx,
240
30
                                    ScratchAlloc &SA) {
241
30
  ObjCInterfaceDecl::protocol_loc_iterator LI = ProtList.loc_begin();
242
30
  for (ObjCInterfaceDecl::protocol_iterator
243
32
         I = ProtList.begin(), E = ProtList.end(); I != E; 
++I, ++LI2
) {
244
2
    SourceLocation Loc = *LI;
245
2
    ObjCProtocolDecl *PD = *I;
246
2
    ProtEntities.push_back(EntityInfo());
247
2
    IdxCtx.getEntityInfo(PD, ProtEntities.back(), SA);
248
2
    CXIdxObjCProtocolRefInfo ProtInfo = { nullptr,
249
2
                                MakeCursorObjCProtocolRef(PD, Loc, IdxCtx.CXTU),
250
2
                                IdxCtx.getIndexLoc(Loc) };
251
2
    ProtInfos.push_back(ProtInfo);
252
253
2
    if (IdxCtx.shouldSuppressRefs())
254
1
      IdxCtx.markEntityOccurrenceInFile(PD, Loc);
255
2
  }
256
257
32
  for (unsigned i = 0, e = ProtInfos.size(); i != e; 
++i2
)
258
2
    ProtInfos[i].protocol = &ProtEntities[i];
259
260
32
  for (unsigned i = 0, e = ProtInfos.size(); i != e; 
++i2
)
261
2
    Prots.push_back(&ProtInfos[i]);
262
30
}
263
264
265
IBOutletCollectionInfo::IBOutletCollectionInfo(
266
                                          const IBOutletCollectionInfo &other)
267
3
  : AttrInfo(CXIdxAttr_IBOutletCollection, other.cursor, other.loc, other.A) {
268
269
3
  IBCollInfo.attrInfo = this;
270
3
  IBCollInfo.classCursor = other.IBCollInfo.classCursor;
271
3
  IBCollInfo.classLoc = other.IBCollInfo.classLoc;
272
3
  if (other.IBCollInfo.objcClass) {
273
0
    ClassInfo = other.ClassInfo;
274
0
    IBCollInfo.objcClass = &ClassInfo;
275
0
  } else
276
3
    IBCollInfo.objcClass = nullptr;
277
3
}
278
279
AttrListInfo::AttrListInfo(const Decl *D, CXIndexDataConsumer &IdxCtx)
280
42
  : SA(IdxCtx), ref_cnt(0) {
281
282
42
  if (!D->hasAttrs())
283
0
    return;
284
285
45
  
for (const auto *A : D->attrs())42
{
286
45
    CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU);
287
45
    CXIdxLoc Loc =  IdxCtx.getIndexLoc(A->getLocation());
288
45
    switch (C.kind) {
289
42
    default:
290
42
      Attrs.push_back(AttrInfo(CXIdxAttr_Unexposed, C, Loc, A));
291
42
      break;
292
0
    case CXCursor_IBActionAttr:
293
0
      Attrs.push_back(AttrInfo(CXIdxAttr_IBAction, C, Loc, A));
294
0
      break;
295
0
    case CXCursor_IBOutletAttr:
296
0
      Attrs.push_back(AttrInfo(CXIdxAttr_IBOutlet, C, Loc, A));
297
0
      break;
298
3
    case CXCursor_IBOutletCollectionAttr:
299
3
      IBCollAttrs.push_back(IBOutletCollectionInfo(C, Loc, A));
300
3
      break;
301
45
    }
302
45
  }
303
304
45
  
for (unsigned i = 0, e = IBCollAttrs.size(); 42
i != e;
++i3
) {
305
3
    IBOutletCollectionInfo &IBInfo = IBCollAttrs[i];
306
3
    CXAttrs.push_back(&IBInfo);
307
308
3
    const IBOutletCollectionAttr *
309
3
      IBAttr = cast<IBOutletCollectionAttr>(IBInfo.A);
310
3
    SourceLocation InterfaceLocStart =
311
3
        IBAttr->getInterfaceLoc()->getTypeLoc().getBeginLoc();
312
3
    IBInfo.IBCollInfo.attrInfo = &IBInfo;
313
3
    IBInfo.IBCollInfo.classLoc = IdxCtx.getIndexLoc(InterfaceLocStart);
314
3
    IBInfo.IBCollInfo.objcClass = nullptr;
315
3
    IBInfo.IBCollInfo.classCursor = clang_getNullCursor();
316
3
    QualType Ty = IBAttr->getInterface();
317
3
    if (const ObjCObjectType *ObjectTy = Ty->getAs<ObjCObjectType>()) {
318
3
      if (const ObjCInterfaceDecl *InterD = ObjectTy->getInterface()) {
319
3
        IdxCtx.getEntityInfo(InterD, IBInfo.ClassInfo, SA);
320
3
        IBInfo.IBCollInfo.objcClass = &IBInfo.ClassInfo;
321
3
        IBInfo.IBCollInfo.classCursor =
322
3
            MakeCursorObjCClassRef(InterD, InterfaceLocStart, IdxCtx.CXTU);
323
3
      }
324
3
    }
325
3
  }
326
327
84
  for (unsigned i = 0, e = Attrs.size(); i != e; 
++i42
)
328
42
    CXAttrs.push_back(&Attrs[i]);
329
42
}
330
331
IntrusiveRefCntPtr<AttrListInfo>
332
42
AttrListInfo::create(const Decl *D, CXIndexDataConsumer &IdxCtx) {
333
42
  ScratchAlloc SA(IdxCtx);
334
42
  AttrListInfo *attrs = SA.allocate<AttrListInfo>();
335
42
  return new (attrs) AttrListInfo(D, IdxCtx);
336
42
}
337
338
CXIndexDataConsumer::CXXBasesListInfo::CXXBasesListInfo(const CXXRecordDecl *D,
339
                                   CXIndexDataConsumer &IdxCtx,
340
34
                                   ScratchAlloc &SA) {
341
3
  for (const auto &Base : D->bases()) {
342
3
    BaseEntities.push_back(EntityInfo());
343
3
    const NamedDecl *BaseD = nullptr;
344
3
    QualType T = Base.getType();
345
3
    SourceLocation Loc = getBaseLoc(Base);
346
347
3
    if (const TypedefType *TDT = T->getAs<TypedefType>()) {
348
0
      BaseD = TDT->getDecl();
349
3
    } else if (const TemplateSpecializationType *
350
3
          TST = T->getAs<TemplateSpecializationType>()) {
351
1
      BaseD = TST->getTemplateName().getAsTemplateDecl();
352
2
    } else if (const RecordType *RT = T->getAs<RecordType>()) {
353
2
      BaseD = RT->getDecl();
354
2
    }
355
356
3
    if (BaseD)
357
3
      IdxCtx.getEntityInfo(BaseD, BaseEntities.back(), SA);
358
3
    CXIdxBaseClassInfo BaseInfo = { nullptr,
359
3
                         MakeCursorCXXBaseSpecifier(&Base, IdxCtx.CXTU),
360
3
                         IdxCtx.getIndexLoc(Loc) };
361
3
    BaseInfos.push_back(BaseInfo);
362
3
  }
363
364
37
  for (unsigned i = 0, e = BaseInfos.size(); i != e; 
++i3
) {
365
3
    if (BaseEntities[i].name && BaseEntities[i].USR)
366
3
      BaseInfos[i].base = &BaseEntities[i];
367
3
  }
368
369
37
  for (unsigned i = 0, e = BaseInfos.size(); i != e; 
++i3
)
370
3
    CXBases.push_back(&BaseInfos[i]);
371
34
}
372
373
SourceLocation CXIndexDataConsumer::CXXBasesListInfo::getBaseLoc(
374
3
                                           const CXXBaseSpecifier &Base) const {
375
3
  SourceLocation Loc = Base.getSourceRange().getBegin();
376
3
  TypeLoc TL;
377
3
  if (Base.getTypeSourceInfo())
378
3
    TL = Base.getTypeSourceInfo()->getTypeLoc();
379
3
  if (TL.isNull())
380
0
    return Loc;
381
382
3
  if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>())
383
0
    TL = QL.getUnqualifiedLoc();
384
385
3
  if (ElaboratedTypeLoc EL = TL.getAs<ElaboratedTypeLoc>())
386
0
    return EL.getNamedTypeLoc().getBeginLoc();
387
3
  if (DependentNameTypeLoc DL = TL.getAs<DependentNameTypeLoc>())
388
0
    return DL.getNameLoc();
389
3
  if (DependentTemplateSpecializationTypeLoc DTL =
390
0
          TL.getAs<DependentTemplateSpecializationTypeLoc>())
391
0
    return DTL.getTemplateNameLoc();
392
393
3
  return Loc;
394
3
}
395
396
2.34k
const char *ScratchAlloc::toCStr(StringRef Str) {
397
2.34k
  if (Str.empty())
398
0
    return "";
399
2.34k
  if (Str.data()[Str.size()] == '\0')
400
2.32k
    return Str.data();
401
19
  return copyCStr(Str);
402
19
}
403
404
6.00k
const char *ScratchAlloc::copyCStr(StringRef Str) {
405
6.00k
  char *buf = IdxCtx.StrScratch.Allocate<char>(Str.size() + 1);
406
6.00k
  std::uninitialized_copy(Str.begin(), Str.end(), buf);
407
6.00k
  buf[Str.size()] = '\0';
408
6.00k
  return buf;
409
6.00k
}
410
411
87
void CXIndexDataConsumer::setASTContext(ASTContext &ctx) {
412
87
  Ctx = &ctx;
413
87
  cxtu::getASTUnit(CXTU)->setASTContext(&ctx);
414
87
}
415
416
87
void CXIndexDataConsumer::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
417
87
  cxtu::getASTUnit(CXTU)->setPreprocessor(std::move(PP));
418
87
}
419
420
1.87k
bool CXIndexDataConsumer::isFunctionLocalDecl(const Decl *D) {
421
1.87k
  assert(D);
422
423
1.87k
  if (!D->getParentFunctionOrMethod())
424
1.87k
    return false;
425
426
0
  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
427
0
    switch (ND->getFormalLinkage()) {
428
0
    case NoLinkage:
429
0
    case InternalLinkage:
430
0
      return true;
431
0
    case VisibleNoLinkage:
432
0
    case ModuleInternalLinkage:
433
0
    case UniqueExternalLinkage:
434
0
      llvm_unreachable("Not a sema linkage");
435
0
    case ModuleLinkage:
436
0
    case ExternalLinkage:
437
0
      return false;
438
0
    }
439
0
  }
440
441
0
  return true;
442
0
}
443
444
2.44k
bool CXIndexDataConsumer::shouldAbort() {
445
2.44k
  if (!CB.abortQuery)
446
0
    return false;
447
2.44k
  return CB.abortQuery(ClientData, nullptr);
448
2.44k
}
449
450
48
void CXIndexDataConsumer::enteredMainFile(const FileEntry *File) {
451
48
  if (File && 
CB.enteredMainFile42
) {
452
42
    CXIdxClientFile idxFile =
453
42
      CB.enteredMainFile(ClientData,
454
42
                         static_cast<CXFile>(const_cast<FileEntry *>(File)),
455
42
                         nullptr);
456
42
    FileMap[File] = idxFile;
457
42
  }
458
48
}
459
460
void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc,
461
                                     StringRef filename,
462
                                     const FileEntry *File,
463
                                     bool isImport, bool isAngled,
464
45
                                     bool isModuleImport) {
465
45
  if (!CB.ppIncludedFile)
466
0
    return;
467
468
45
  ScratchAlloc SA(*this);
469
45
  CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
470
45
                                 SA.toCStr(filename),
471
45
                                 static_cast<CXFile>(
472
45
                                   const_cast<FileEntry *>(File)),
473
45
                                 isImport, isAngled, isModuleImport };
474
45
  CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
475
45
  FileMap[File] = idxFile;
476
45
}
477
478
43
void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) {
479
43
  if (!CB.importedASTFile)
480
0
    return;
481
482
43
  Module *Mod = ImportD->getImportedModule();
483
43
  if (!Mod)
484
0
    return;
485
486
  // If the imported module is part of the top-level module that we're
487
  // indexing, it doesn't correspond to an imported AST file.
488
  // FIXME: This assumes that AST files and top-level modules directly
489
  // correspond, which is unlikely to remain true forever.
490
43
  if (Module *SrcMod = ImportD->getImportedOwningModule())
491
26
    if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule())
492
23
      return;
493
494
20
  FileEntry *FE = nullptr;
495
20
  if (auto File = Mod->getASTFile())
496
20
    FE = const_cast<FileEntry *>(&File->getFileEntry());
497
20
  CXIdxImportedASTFileInfo Info = {static_cast<CXFile>(FE), Mod,
498
20
                                   getIndexLoc(ImportD->getLocation()),
499
20
                                   ImportD->isImplicit()};
500
20
  CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
501
20
  (void)astFile;
502
20
}
503
504
4
void CXIndexDataConsumer::importedPCH(const FileEntry *File) {
505
4
  if (!CB.importedASTFile)
506
0
    return;
507
508
4
  CXIdxImportedASTFileInfo Info = {
509
4
                                    static_cast<CXFile>(
510
4
                                      const_cast<FileEntry *>(File)),
511
4
                                    /*module=*/nullptr,
512
4
                                    getIndexLoc(SourceLocation()),
513
4
                                    /*isImplicit=*/false
514
4
                                  };
515
4
  CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
516
4
  (void)astFile;
517
4
}
518
519
48
void CXIndexDataConsumer::startedTranslationUnit() {
520
48
  CXIdxClientContainer idxCont = nullptr;
521
48
  if (CB.startedTranslationUnit)
522
48
    idxCont = CB.startedTranslationUnit(ClientData, nullptr);
523
48
  addContainerInMap(Ctx->getTranslationUnitDecl(), idxCont);
524
48
}
525
526
57
void CXIndexDataConsumer::indexDiagnostics() {
527
57
  if (!hasDiagnosticCallback())
528
0
    return;
529
530
57
  CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(getCXTU());
531
57
  handleDiagnosticSet(DiagSet);
532
57
}
533
534
57
void CXIndexDataConsumer::handleDiagnosticSet(CXDiagnostic CXDiagSet) {
535
57
  if (!CB.diagnostic)
536
0
    return;
537
538
57
  CB.diagnostic(ClientData, CXDiagSet, nullptr);
539
57
}
540
541
bool CXIndexDataConsumer::handleDecl(const NamedDecl *D,
542
                                 SourceLocation Loc, CXCursor Cursor,
543
                                 DeclInfo &DInfo,
544
                                 const DeclContext *LexicalDC,
545
316
                                 const DeclContext *SemaDC) {
546
316
  if (!CB.indexDeclaration || !D)
547
0
    return false;
548
316
  if (D->isImplicit() && 
shouldIgnoreIfImplicit(D)14
)
549
0
    return false;
550
551
316
  ScratchAlloc SA(*this);
552
316
  getEntityInfo(D, DInfo.EntInfo, SA);
553
316
  if ((!shouldIndexFunctionLocalSymbols() && 
!DInfo.EntInfo.USR295
)
554
316
      || Loc.isInvalid())
555
0
    return false;
556
557
316
  if (!LexicalDC)
558
310
    LexicalDC = D->getLexicalDeclContext();
559
560
316
  if (shouldSuppressRefs())
561
15
    markEntityOccurrenceInFile(D, Loc);
562
  
563
316
  DInfo.entityInfo = &DInfo.EntInfo;
564
316
  DInfo.cursor = Cursor;
565
316
  DInfo.loc = getIndexLoc(Loc);
566
316
  DInfo.isImplicit = D->isImplicit();
567
568
316
  DInfo.attributes = DInfo.EntInfo.attributes;
569
316
  DInfo.numAttributes = DInfo.EntInfo.numAttributes;
570
571
316
  if (!SemaDC)
572
310
    SemaDC = D->getDeclContext();
573
316
  getContainerInfo(SemaDC, DInfo.SemanticContainer);
574
316
  DInfo.semanticContainer = &DInfo.SemanticContainer;
575
576
316
  if (LexicalDC == SemaDC) {
577
303
    DInfo.lexicalContainer = &DInfo.SemanticContainer;
578
13
  } else if (isTemplateImplicitInstantiation(D)) {
579
    // Implicit instantiations have the lexical context of where they were
580
    // instantiated first. We choose instead the semantic context because:
581
    // 1) at the time that we see the instantiation we have not seen the
582
    //   function where it occurred yet.
583
    // 2) the lexical context of the first instantiation is not useful
584
    //   information anyway.
585
0
    DInfo.lexicalContainer = &DInfo.SemanticContainer;
586
13
  } else {
587
13
    getContainerInfo(LexicalDC, DInfo.LexicalContainer);
588
13
    DInfo.lexicalContainer = &DInfo.LexicalContainer;
589
13
  }
590
591
316
  if (DInfo.isContainer) {
592
125
    getContainerInfo(getEntityContainer(D), DInfo.DeclAsContainer);
593
125
    DInfo.declAsContainer = &DInfo.DeclAsContainer;
594
125
  }
595
596
316
  CB.indexDeclaration(ClientData, &DInfo);
597
316
  return true;
598
316
}
599
600
bool CXIndexDataConsumer::handleObjCContainer(const ObjCContainerDecl *D,
601
                                          SourceLocation Loc, CXCursor Cursor,
602
40
                                          ObjCContainerDeclInfo &ContDInfo) {
603
40
  ContDInfo.ObjCContDeclInfo.declInfo = &ContDInfo;
604
40
  return handleDecl(D, Loc, Cursor, ContDInfo);
605
40
}
606
607
89
bool CXIndexDataConsumer::handleFunction(const FunctionDecl *D) {
608
89
  bool isDef = D->isThisDeclarationADefinition();
609
89
  bool isContainer = isDef;
610
89
  bool isSkipped = false;
611
89
  if (D->hasSkippedBody()) {
612
9
    isSkipped = true;
613
9
    isDef = true;
614
9
    isContainer = false;
615
9
  }
616
617
89
  DeclInfo DInfo(!D->isFirstDecl(), isDef, isContainer);
618
89
  if (isSkipped)
619
9
    DInfo.flags |= CXIdxDeclFlag_Skipped;
620
89
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
621
89
}
622
623
55
bool CXIndexDataConsumer::handleVar(const VarDecl *D) {
624
55
  DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
625
55
                 /*isContainer=*/false);
626
55
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
627
55
}
628
629
13
bool CXIndexDataConsumer::handleField(const FieldDecl *D) {
630
13
  DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
631
13
                 /*isContainer=*/false);
632
13
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
633
13
}
634
635
2
bool CXIndexDataConsumer::handleEnumerator(const EnumConstantDecl *D) {
636
2
  DeclInfo DInfo(/*isRedeclaration=*/false, /*isDefinition=*/true,
637
2
                 /*isContainer=*/false);
638
2
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
639
2
}
640
641
41
bool CXIndexDataConsumer::handleTagDecl(const TagDecl *D) {
642
41
  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(D))
643
35
    return handleCXXRecordDecl(CXXRD, D);
644
645
6
  DeclInfo DInfo(!D->isFirstDecl(), D->isThisDeclarationADefinition(),
646
6
                 D->isThisDeclarationADefinition());
647
6
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
648
6
}
649
650
12
bool CXIndexDataConsumer::handleTypedefName(const TypedefNameDecl *D) {
651
12
  DeclInfo DInfo(!D->isFirstDecl(), /*isDefinition=*/true,
652
12
                 /*isContainer=*/false);
653
12
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
654
12
}
655
656
27
bool CXIndexDataConsumer::handleObjCInterface(const ObjCInterfaceDecl *D) {
657
  // For @class forward declarations, suppress them the same way as references.
658
27
  if (!D->isThisDeclarationADefinition()) {
659
6
    if (shouldSuppressRefs() && 
markEntityOccurrenceInFile(D, D->getLocation())2
)
660
1
      return false; // already occurred.
661
662
    // FIXME: This seems like the wrong definition for redeclaration.
663
5
    bool isRedeclaration = D->hasDefinition() || 
D->getPreviousDecl()4
;
664
5
    ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true, isRedeclaration,
665
5
                                    /*isImplementation=*/false);
666
5
    return handleObjCContainer(D, D->getLocation(),
667
5
                               MakeCursorObjCClassRef(D, D->getLocation(),
668
5
                                                      CXTU), 
669
5
                               ContDInfo);
670
5
  }
671
672
21
  ScratchAlloc SA(*this);
673
674
21
  CXIdxBaseClassInfo BaseClass;
675
21
  EntityInfo BaseEntity;
676
21
  BaseClass.cursor = clang_getNullCursor();
677
21
  if (ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
678
3
    getEntityInfo(SuperD, BaseEntity, SA);
679
3
    SourceLocation SuperLoc = D->getSuperClassLoc();
680
3
    BaseClass.base = &BaseEntity;
681
3
    BaseClass.cursor = MakeCursorObjCSuperClassRef(SuperD, SuperLoc, CXTU);
682
3
    BaseClass.loc = getIndexLoc(SuperLoc);
683
684
3
    if (shouldSuppressRefs())
685
1
      markEntityOccurrenceInFile(SuperD, SuperLoc);
686
3
  }
687
  
688
21
  ObjCProtocolList EmptyProtoList;
689
21
  ObjCProtocolListInfo ProtInfo(D->isThisDeclarationADefinition() 
690
21
                                  ? D->getReferencedProtocols()
691
0
                                  : EmptyProtoList, 
692
21
                                *this, SA);
693
  
694
21
  ObjCInterfaceDeclInfo InterInfo(D);
695
21
  InterInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
696
21
  InterInfo.ObjCInterDeclInfo.containerInfo = &InterInfo.ObjCContDeclInfo;
697
3
  InterInfo.ObjCInterDeclInfo.superInfo = D->getSuperClass() ? &BaseClass
698
18
                                                             : nullptr;
699
21
  InterInfo.ObjCInterDeclInfo.protocols = &InterInfo.ObjCProtoListInfo;
700
701
21
  return handleObjCContainer(D, D->getLocation(), getCursor(D), InterInfo);
702
21
}
703
704
bool CXIndexDataConsumer::handleObjCImplementation(
705
4
                                              const ObjCImplementationDecl *D) {
706
4
  ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/false,
707
4
                      /*isRedeclaration=*/true,
708
4
                      /*isImplementation=*/true);
709
4
  return handleObjCContainer(D, D->getLocation(), getCursor(D), ContDInfo);
710
4
}
711
712
5
bool CXIndexDataConsumer::handleObjCProtocol(const ObjCProtocolDecl *D) {
713
5
  if (!D->isThisDeclarationADefinition()) {
714
1
    if (shouldSuppressRefs() && 
markEntityOccurrenceInFile(D, D->getLocation())0
)
715
0
      return false; // already occurred.
716
    
717
    // FIXME: This seems like the wrong definition for redeclaration.
718
1
    bool isRedeclaration = D->hasDefinition() || D->getPreviousDecl();
719
1
    ObjCContainerDeclInfo ContDInfo(/*isForwardRef=*/true,
720
1
                                    isRedeclaration,
721
1
                                    /*isImplementation=*/false);
722
1
    return handleObjCContainer(D, D->getLocation(), 
723
1
                               MakeCursorObjCProtocolRef(D, D->getLocation(),
724
1
                                                         CXTU),
725
1
                               ContDInfo);    
726
1
  }
727
  
728
4
  ScratchAlloc SA(*this);
729
4
  ObjCProtocolList EmptyProtoList;
730
4
  ObjCProtocolListInfo ProtListInfo(D->isThisDeclarationADefinition()
731
4
                                      ? D->getReferencedProtocols()
732
0
                                      : EmptyProtoList,
733
4
                                    *this, SA);
734
  
735
4
  ObjCProtocolDeclInfo ProtInfo(D);
736
4
  ProtInfo.ObjCProtoRefListInfo = ProtListInfo.getListInfo();
737
738
4
  return handleObjCContainer(D, D->getLocation(), getCursor(D), ProtInfo);
739
4
}
740
741
5
bool CXIndexDataConsumer::handleObjCCategory(const ObjCCategoryDecl *D) {
742
5
  ScratchAlloc SA(*this);
743
744
5
  ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/false);
745
5
  EntityInfo ClassEntity;
746
5
  const ObjCInterfaceDecl *IFaceD = D->getClassInterface();
747
5
  SourceLocation ClassLoc = D->getLocation();
748
1
  SourceLocation CategoryLoc = D->IsClassExtension() ? ClassLoc
749
4
                                                     : D->getCategoryNameLoc();
750
5
  getEntityInfo(IFaceD, ClassEntity, SA);
751
752
5
  if (shouldSuppressRefs())
753
1
    markEntityOccurrenceInFile(IFaceD, ClassLoc);
754
755
5
  ObjCProtocolListInfo ProtInfo(D->getReferencedProtocols(), *this, SA);
756
  
757
5
  CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
758
5
  if (IFaceD) {
759
5
    CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
760
5
    CatDInfo.ObjCCatDeclInfo.classCursor =
761
5
        MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
762
0
  } else {
763
0
    CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
764
0
    CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
765
0
  }
766
5
  CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
767
5
  CatDInfo.ObjCProtoListInfo = ProtInfo.getListInfo();
768
5
  CatDInfo.ObjCCatDeclInfo.protocols = &CatDInfo.ObjCProtoListInfo;
769
770
5
  return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
771
5
}
772
773
0
bool CXIndexDataConsumer::handleObjCCategoryImpl(const ObjCCategoryImplDecl *D) {
774
0
  ScratchAlloc SA(*this);
775
776
0
  const ObjCCategoryDecl *CatD = D->getCategoryDecl();
777
0
  ObjCCategoryDeclInfo CatDInfo(/*isImplementation=*/true);
778
0
  EntityInfo ClassEntity;
779
0
  const ObjCInterfaceDecl *IFaceD = CatD->getClassInterface();
780
0
  SourceLocation ClassLoc = D->getLocation();
781
0
  SourceLocation CategoryLoc = D->getCategoryNameLoc();
782
0
  getEntityInfo(IFaceD, ClassEntity, SA);
783
784
0
  if (shouldSuppressRefs())
785
0
    markEntityOccurrenceInFile(IFaceD, ClassLoc);
786
787
0
  CatDInfo.ObjCCatDeclInfo.containerInfo = &CatDInfo.ObjCContDeclInfo;
788
0
  if (IFaceD) {
789
0
    CatDInfo.ObjCCatDeclInfo.objcClass = &ClassEntity;
790
0
    CatDInfo.ObjCCatDeclInfo.classCursor =
791
0
        MakeCursorObjCClassRef(IFaceD, ClassLoc, CXTU);
792
0
  } else {
793
0
    CatDInfo.ObjCCatDeclInfo.objcClass = nullptr;
794
0
    CatDInfo.ObjCCatDeclInfo.classCursor = clang_getNullCursor();
795
0
  }
796
0
  CatDInfo.ObjCCatDeclInfo.classLoc = getIndexLoc(ClassLoc);
797
0
  CatDInfo.ObjCCatDeclInfo.protocols = nullptr;
798
799
0
  return handleObjCContainer(D, CategoryLoc, getCursor(D), CatDInfo);
800
0
}
801
802
bool CXIndexDataConsumer::handleObjCMethod(const ObjCMethodDecl *D,
803
42
                                           SourceLocation Loc) {
804
42
  bool isDef = D->isThisDeclarationADefinition();
805
42
  bool isContainer = isDef;
806
42
  bool isSkipped = false;
807
42
  if (D->hasSkippedBody()) {
808
0
    isSkipped = true;
809
0
    isDef = true;
810
0
    isContainer = false;
811
0
  }
812
813
42
  DeclInfo DInfo(!D->isCanonicalDecl(), isDef, isContainer);
814
42
  if (isSkipped)
815
0
    DInfo.flags |= CXIdxDeclFlag_Skipped;
816
42
  return handleDecl(D, Loc, getCursor(D), DInfo);
817
42
}
818
819
bool CXIndexDataConsumer::handleSynthesizedObjCProperty(
820
4
                                                const ObjCPropertyImplDecl *D) {
821
4
  ObjCPropertyDecl *PD = D->getPropertyDecl();
822
4
  auto *DC = D->getDeclContext();
823
4
  return handleReference(PD, D->getLocation(), getCursor(D),
824
4
                         dyn_cast<NamedDecl>(DC), DC);
825
4
}
826
827
bool CXIndexDataConsumer::handleSynthesizedObjCMethod(const ObjCMethodDecl *D,
828
                                                  SourceLocation Loc,
829
6
                                                 const DeclContext *LexicalDC) {
830
6
  DeclInfo DInfo(/*isRedeclaration=*/true, /*isDefinition=*/true,
831
6
                 /*isContainer=*/false);
832
6
  return handleDecl(D, Loc, getCursor(D), DInfo, LexicalDC, D->getDeclContext());
833
6
}
834
835
8
bool CXIndexDataConsumer::handleObjCProperty(const ObjCPropertyDecl *D) {
836
8
  ScratchAlloc SA(*this);
837
838
8
  ObjCPropertyDeclInfo DInfo;
839
8
  EntityInfo GetterEntity;
840
8
  EntityInfo SetterEntity;
841
842
8
  DInfo.ObjCPropDeclInfo.declInfo = &DInfo;
843
844
8
  if (ObjCMethodDecl *Getter = D->getGetterMethodDecl()) {
845
8
    getEntityInfo(Getter, GetterEntity, SA);
846
8
    DInfo.ObjCPropDeclInfo.getter = &GetterEntity;
847
0
  } else {
848
0
    DInfo.ObjCPropDeclInfo.getter = nullptr;
849
0
  }
850
8
  if (ObjCMethodDecl *Setter = D->getSetterMethodDecl()) {
851
7
    getEntityInfo(Setter, SetterEntity, SA);
852
7
    DInfo.ObjCPropDeclInfo.setter = &SetterEntity;
853
1
  } else {
854
1
    DInfo.ObjCPropDeclInfo.setter = nullptr;
855
1
  }
856
857
8
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
858
8
}
859
860
8
bool CXIndexDataConsumer::handleNamespace(const NamespaceDecl *D) {
861
8
  DeclInfo DInfo(/*isRedeclaration=*/!D->isOriginalNamespace(),
862
8
                 /*isDefinition=*/true,
863
8
                 /*isContainer=*/true);
864
8
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
865
8
}
866
867
0
bool CXIndexDataConsumer::handleClassTemplate(const ClassTemplateDecl *D) {
868
0
  return handleCXXRecordDecl(D->getTemplatedDecl(), D);
869
0
}
870
871
0
bool CXIndexDataConsumer::handleFunctionTemplate(const FunctionTemplateDecl *D) {
872
0
  DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
873
0
                 /*isDefinition=*/D->isThisDeclarationADefinition(),
874
0
                 /*isContainer=*/D->isThisDeclarationADefinition());
875
0
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
876
0
}
877
878
0
bool CXIndexDataConsumer::handleTypeAliasTemplate(const TypeAliasTemplateDecl *D) {
879
0
  DeclInfo DInfo(/*isRedeclaration=*/!D->isCanonicalDecl(),
880
0
                 /*isDefinition=*/true, /*isContainer=*/false);
881
0
  return handleDecl(D, D->getLocation(), getCursor(D), DInfo);
882
0
}
883
884
bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc,
885
                                      CXCursor Cursor,
886
                                      const NamedDecl *Parent,
887
                                      const DeclContext *DC,
888
                                      const Expr *E,
889
                                      CXIdxEntityRefKind Kind,
890
1.91k
                                      CXSymbolRole Role) {
891
1.91k
  if (!CB.indexEntityReference)
892
0
    return false;
893
894
1.91k
  if (!D || !DC)
895
0
    return false;
896
1.91k
  if (Loc.isInvalid())
897
2
    return false;
898
1.91k
  if (!shouldIndexFunctionLocalSymbols() && 
isFunctionLocalDecl(D)1.87k
)
899
0
    return false;
900
1.91k
  if (isNotFromSourceFile(D->getLocation()))
901
0
    return false;
902
1.91k
  if (D->isImplicit() && 
shouldIgnoreIfImplicit(D)0
)
903
0
    return false;
904
905
1.91k
  if (shouldSuppressRefs()) {
906
11
    if (markEntityOccurrenceInFile(D, Loc))
907
11
      return false; // already occurred.
908
1.90k
  }
909
910
1.90k
  ScratchAlloc SA(*this);
911
1.90k
  EntityInfo RefEntity, ParentEntity;
912
1.90k
  getEntityInfo(D, RefEntity, SA);
913
1.90k
  if (!RefEntity.USR)
914
0
    return false;
915
916
1.90k
  getEntityInfo(Parent, ParentEntity, SA);
917
918
1.90k
  ContainerInfo Container;
919
1.90k
  getContainerInfo(DC, Container);
920
921
1.90k
  CXIdxEntityRefInfo Info = { Kind,
922
1.90k
                              Cursor,
923
1.90k
                              getIndexLoc(Loc),
924
1.90k
                              &RefEntity,
925
1.89k
                              Parent ? &ParentEntity : 
nullptr7
,
926
1.90k
                              &Container,
927
1.90k
                              Role };
928
1.90k
  CB.indexEntityReference(ClientData, &Info);
929
1.90k
  return true;
930
1.90k
}
931
932
1.91k
bool CXIndexDataConsumer::isNotFromSourceFile(SourceLocation Loc) const {
933
1.91k
  if (Loc.isInvalid())
934
0
    return true;
935
1.91k
  SourceManager &SM = Ctx->getSourceManager();
936
1.91k
  SourceLocation FileLoc = SM.getFileLoc(Loc);
937
1.91k
  FileID FID = SM.getFileID(FileLoc);
938
1.91k
  return SM.getFileEntryForID(FID) == nullptr;
939
1.91k
}
940
941
void CXIndexDataConsumer::addContainerInMap(const DeclContext *DC,
942
173
                                        CXIdxClientContainer container) {
943
173
  if (!DC)
944
0
    return;
945
946
173
  ContainerMapTy::iterator I = ContainerMap.find(DC);
947
173
  if (I == ContainerMap.end()) {
948
173
    if (container)
949
173
      ContainerMap[DC] = container;
950
173
    return;
951
173
  }
952
  // Allow changing the container of a previously seen DeclContext so we
953
  // can handle invalid user code, like a function re-definition.
954
0
  if (container)
955
0
    I->second = container;
956
0
  else
957
0
    ContainerMap.erase(I);
958
0
}
959
960
0
CXIdxClientEntity CXIndexDataConsumer::getClientEntity(const Decl *D) const {
961
0
  if (!D)
962
0
    return nullptr;
963
0
  EntityMapTy::const_iterator I = EntityMap.find(D);
964
0
  if (I == EntityMap.end())
965
0
    return nullptr;
966
0
  return I->second;
967
0
}
968
969
0
void CXIndexDataConsumer::setClientEntity(const Decl *D, CXIdxClientEntity client) {
970
0
  if (!D)
971
0
    return;
972
0
  EntityMap[D] = client;
973
0
}
974
975
bool CXIndexDataConsumer::handleCXXRecordDecl(const CXXRecordDecl *RD,
976
35
                                          const NamedDecl *OrigD) {
977
35
  if (RD->isThisDeclarationADefinition()) {
978
34
    ScratchAlloc SA(*this);
979
34
    CXXClassDeclInfo CXXDInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
980
34
                           /*isDefinition=*/RD->isThisDeclarationADefinition());
981
34
    CXXBasesListInfo BaseList(RD, *this, SA);
982
34
    CXXDInfo.CXXClassInfo.declInfo = &CXXDInfo;
983
34
    CXXDInfo.CXXClassInfo.bases = BaseList.getBases();
984
34
    CXXDInfo.CXXClassInfo.numBases = BaseList.getNumBases();
985
986
34
    if (shouldSuppressRefs()) {
987
      // Go through bases and mark them as referenced.
988
5
      for (unsigned i = 0, e = BaseList.getNumBases(); i != e; 
++i2
) {
989
2
        const CXIdxBaseClassInfo *baseInfo = BaseList.getBases()[i];
990
2
        if (baseInfo->base) {
991
2
          const NamedDecl *BaseD = BaseList.BaseEntities[i].Dcl;
992
2
          SourceLocation
993
2
            Loc = SourceLocation::getFromRawEncoding(baseInfo->loc.int_data);
994
2
          markEntityOccurrenceInFile(BaseD, Loc);
995
2
        }
996
2
      }
997
3
    }
998
999
34
    return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), CXXDInfo);
1000
34
  }
1001
1002
1
  DeclInfo DInfo(/*isRedeclaration=*/!OrigD->isCanonicalDecl(),
1003
1
                 /*isDefinition=*/RD->isThisDeclarationADefinition(),
1004
1
                 /*isContainer=*/RD->isThisDeclarationADefinition());
1005
1
  return handleDecl(OrigD, OrigD->getLocation(), getCursor(OrigD), DInfo);
1006
1
}
1007
1008
bool CXIndexDataConsumer::markEntityOccurrenceInFile(const NamedDecl *D,
1009
33
                                                 SourceLocation Loc) {
1010
33
  if (!D || Loc.isInvalid())
1011
0
    return true;
1012
1013
33
  SourceManager &SM = Ctx->getSourceManager();
1014
33
  D = getEntityDecl(D);
1015
  
1016
33
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SM.getFileLoc(Loc));
1017
33
  FileID FID = LocInfo.first;
1018
33
  if (FID.isInvalid())
1019
0
    return true;
1020
  
1021
33
  const FileEntry *FE = SM.getFileEntryForID(FID);
1022
33
  if (!FE)
1023
0
    return true;
1024
33
  RefFileOccurrence RefOccur(FE, D);
1025
33
  std::pair<llvm::DenseSet<RefFileOccurrence>::iterator, bool>
1026
33
  res = RefFileOccurrences.insert(RefOccur);
1027
33
  return !res.second; // already in map
1028
33
}
1029
1030
4.20k
const NamedDecl *CXIndexDataConsumer::getEntityDecl(const NamedDecl *D) const {
1031
4.20k
  assert(D);
1032
4.20k
  D = cast<NamedDecl>(D->getCanonicalDecl());
1033
1034
4.20k
  if (const ObjCImplementationDecl *
1035
4.20k
               ImplD = dyn_cast<ObjCImplementationDecl>(D)) {
1036
6
    return getEntityDecl(ImplD->getClassInterface());
1037
1038
4.20k
  } else if (const ObjCCategoryImplDecl *
1039
4.20k
               CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) {
1040
0
    return getEntityDecl(CatImplD->getCategoryDecl());
1041
4.20k
  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1042
3.69k
    if (FunctionTemplateDecl *TemplD = FD->getDescribedFunctionTemplate())
1043
2
      return getEntityDecl(TemplD);
1044
511
  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1045
86
    if (ClassTemplateDecl *TemplD = RD->getDescribedClassTemplate())
1046
20
      return getEntityDecl(TemplD);
1047
4.17k
  }
1048
1049
4.17k
  return D;
1050
4.17k
}
1051
1052
const DeclContext *
1053
125
CXIndexDataConsumer::getEntityContainer(const Decl *D) const {
1054
125
  const DeclContext *DC = dyn_cast<DeclContext>(D);
1055
125
  if (DC)
1056
125
    return DC;
1057
1058
0
  if (const ClassTemplateDecl *ClassTempl = dyn_cast<ClassTemplateDecl>(D)) {
1059
0
    DC = ClassTempl->getTemplatedDecl();
1060
0
  } else if (const FunctionTemplateDecl *
1061
0
          FuncTempl = dyn_cast<FunctionTemplateDecl>(D)) {
1062
0
    DC = FuncTempl->getTemplatedDecl();
1063
0
  }
1064
1065
0
  return DC;
1066
0
}
1067
1068
CXIdxClientContainer
1069
2.53k
CXIndexDataConsumer::getClientContainerForDC(const DeclContext *DC) const {
1070
2.53k
  if (!DC)
1071
0
    return nullptr;
1072
1073
2.53k
  ContainerMapTy::const_iterator I = ContainerMap.find(DC);
1074
2.53k
  if (I == ContainerMap.end())
1075
28
    return nullptr;
1076
1077
2.50k
  return I->second;
1078
2.50k
}
1079
1080
2.40k
CXIdxClientFile CXIndexDataConsumer::getIndexFile(const FileEntry *File) {
1081
2.40k
  if (!File)
1082
0
    return nullptr;
1083
1084
2.40k
  FileMapTy::iterator FI = FileMap.find(File);
1085
2.40k
  if (FI != FileMap.end())
1086
2.40k
    return FI->second;
1087
1088
0
  return nullptr;
1089
0
}
1090
1091
2.34k
CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const {
1092
2.34k
  CXIdxLoc idxLoc =  { {nullptr, nullptr}, 0 };
1093
2.34k
  if (Loc.isInvalid())
1094
22
    return idxLoc;
1095
1096
2.32k
  idxLoc.ptr_data[0] = const_cast<CXIndexDataConsumer *>(this);
1097
2.32k
  idxLoc.int_data = Loc.getRawEncoding();
1098
2.32k
  return idxLoc;
1099
2.32k
}
1100
1101
void CXIndexDataConsumer::translateLoc(SourceLocation Loc,
1102
                                   CXIdxClientFile *indexFile, CXFile *file,
1103
                                   unsigned *line, unsigned *column,
1104
2.40k
                                   unsigned *offset) {
1105
2.40k
  if (Loc.isInvalid())
1106
0
    return;
1107
1108
2.40k
  SourceManager &SM = Ctx->getSourceManager();
1109
2.40k
  Loc = SM.getFileLoc(Loc);
1110
1111
2.40k
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
1112
2.40k
  FileID FID = LocInfo.first;
1113
2.40k
  unsigned FileOffset = LocInfo.second;
1114
1115
2.40k
  if (FID.isInvalid())
1116
0
    return;
1117
  
1118
2.40k
  const FileEntry *FE = SM.getFileEntryForID(FID);
1119
2.40k
  if (indexFile)
1120
2.40k
    *indexFile = getIndexFile(FE);
1121
2.40k
  if (file)
1122
0
    *file = const_cast<FileEntry *>(FE);
1123
2.40k
  if (line)
1124
2.40k
    *line = SM.getLineNumber(FID, FileOffset);
1125
2.40k
  if (column)
1126
2.40k
    *column = SM.getColumnNumber(FID, FileOffset);
1127
2.40k
  if (offset)
1128
0
    *offset = FileOffset;
1129
2.40k
}
1130
1131
static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage L);
1132
static CXIdxEntityCXXTemplateKind
1133
getEntityKindFromSymbolProperties(SymbolPropertySet K);
1134
static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L);
1135
1136
void CXIndexDataConsumer::getEntityInfo(const NamedDecl *D,
1137
                                    EntityInfo &EntityInfo,
1138
4.15k
                                    ScratchAlloc &SA) {
1139
4.15k
  if (!D)
1140
7
    return;
1141
1142
4.14k
  D = getEntityDecl(D);
1143
4.14k
  EntityInfo.cursor = getCursor(D);
1144
4.14k
  EntityInfo.Dcl = D;
1145
4.14k
  EntityInfo.IndexCtx = this;
1146
1147
4.14k
  SymbolInfo SymInfo = getSymbolInfo(D);
1148
4.14k
  EntityInfo.kind = getEntityKindFromSymbolKind(SymInfo.Kind, SymInfo.Lang);
1149
4.14k
  EntityInfo.templateKind = getEntityKindFromSymbolProperties(SymInfo.Properties);
1150
4.14k
  EntityInfo.lang = getEntityLangFromSymbolLang(SymInfo.Lang);
1151
1152
4.14k
  if (D->hasAttrs()) {
1153
42
    EntityInfo.AttrList = AttrListInfo::create(D, *this);
1154
42
    EntityInfo.attributes = EntityInfo.AttrList->getAttrs();
1155
42
    EntityInfo.numAttributes = EntityInfo.AttrList->getNumAttrs();
1156
42
  }
1157
1158
4.14k
  if (EntityInfo.kind == CXIdxEntity_Unexposed)
1159
0
    return;
1160
1161
4.14k
  if (IdentifierInfo *II = D->getIdentifier()) {
1162
2.30k
    EntityInfo.name = SA.toCStr(II->getName());
1163
1164
1.84k
  } else if (isa<TagDecl>(D) || 
isa<FieldDecl>(D)1.84k
||
isa<NamespaceDecl>(D)1.84k
) {
1165
3
    EntityInfo.name = nullptr; // anonymous tag/field/namespace.
1166
1167
1.84k
  } else {
1168
1.84k
    SmallString<256> StrBuf;
1169
1.84k
    {
1170
1.84k
      llvm::raw_svector_ostream OS(StrBuf);
1171
1.84k
      D->printName(OS);
1172
1.84k
    }
1173
1.84k
    EntityInfo.name = SA.copyCStr(StrBuf.str());
1174
1.84k
  }
1175
1176
4.14k
  {
1177
4.14k
    SmallString<512> StrBuf;
1178
4.14k
    bool Ignore = getDeclCursorUSR(D, StrBuf);
1179
4.14k
    if (Ignore) {
1180
0
      EntityInfo.USR = nullptr;
1181
4.14k
    } else {
1182
4.14k
      EntityInfo.USR = SA.copyCStr(StrBuf.str());
1183
4.14k
    }
1184
4.14k
  }
1185
4.14k
}
1186
1187
void CXIndexDataConsumer::getContainerInfo(const DeclContext *DC,
1188
2.35k
                                       ContainerInfo &ContInfo) {
1189
2.35k
  ContInfo.cursor = getCursor(cast<Decl>(DC));
1190
2.35k
  ContInfo.DC = DC;
1191
2.35k
  ContInfo.IndexCtx = this;
1192
2.35k
}
1193
1194
94
CXCursor CXIndexDataConsumer::getRefCursor(const NamedDecl *D, SourceLocation Loc) {
1195
94
  if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1196
50
    return MakeCursorTypeRef(TD, Loc, CXTU);
1197
44
  if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
1198
21
    return MakeCursorObjCClassRef(ID, Loc, CXTU);
1199
23
  if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
1200
3
    return MakeCursorObjCProtocolRef(PD, Loc, CXTU);
1201
20
  if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D))
1202
2
    return MakeCursorTemplateRef(Template, Loc, CXTU);
1203
18
  if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(D))
1204
10
    return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1205
8
  if (const NamespaceAliasDecl *Namespace = dyn_cast<NamespaceAliasDecl>(D))
1206
0
    return MakeCursorNamespaceRef(Namespace, Loc, CXTU);
1207
8
  if (const FieldDecl *Field = dyn_cast<FieldDecl>(D))
1208
0
    return MakeCursorMemberRef(Field, Loc, CXTU);
1209
8
  if (const VarDecl *Var = dyn_cast<VarDecl>(D))
1210
8
    return MakeCursorVariableRef(Var, Loc, CXTU);
1211
  
1212
0
  return clang_getNullCursor();
1213
0
}
1214
1215
14
bool CXIndexDataConsumer::shouldIgnoreIfImplicit(const Decl *D) {
1216
14
  if (isa<ObjCInterfaceDecl>(D))
1217
0
    return false;
1218
14
  if (isa<ObjCCategoryDecl>(D))
1219
0
    return false;
1220
14
  if (isa<ObjCIvarDecl>(D))
1221
0
    return false;
1222
14
  if (isa<ObjCMethodDecl>(D))
1223
14
    return false;
1224
0
  if (isa<ImportDecl>(D))
1225
0
    return false;
1226
0
  return true;
1227
0
}
1228
1229
13
bool CXIndexDataConsumer::isTemplateImplicitInstantiation(const Decl *D) {
1230
13
  if (const ClassTemplateSpecializationDecl *
1231
13
        SD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
1232
0
    return SD->getSpecializationKind() == TSK_ImplicitInstantiation;
1233
0
  }
1234
13
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1235
5
    return FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation;
1236
5
  }
1237
8
  return false;
1238
8
}
1239
1240
4.14k
static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage Lang) {
1241
4.14k
  switch (K) {
1242
0
  case SymbolKind::Unknown:
1243
0
  case SymbolKind::Module:
1244
0
  case SymbolKind::Macro:
1245
0
  case SymbolKind::ClassProperty:
1246
0
  case SymbolKind::Using:
1247
0
  case SymbolKind::TemplateTypeParm:
1248
0
  case SymbolKind::TemplateTemplateParm:
1249
0
  case SymbolKind::NonTypeTemplateParm:
1250
0
    return CXIdxEntity_Unexposed;
1251
1252
2
  case SymbolKind::Enum: return CXIdxEntity_Enum;
1253
32
  case SymbolKind::Struct: return CXIdxEntity_Struct;
1254
1
  case SymbolKind::Union: return CXIdxEntity_Union;
1255
27
  case SymbolKind::TypeAlias:
1256
27
    if (Lang == SymbolLanguage::CXX)
1257
1
      return CXIdxEntity_CXXTypeAlias;
1258
26
    return CXIdxEntity_Typedef;
1259
1.86k
  case SymbolKind::Function: return CXIdxEntity_Function;
1260
122
  case SymbolKind::Variable: return CXIdxEntity_Variable;
1261
20
  case SymbolKind::Field:
1262
20
    if (Lang == SymbolLanguage::ObjC)
1263
5
      return CXIdxEntity_ObjCIvar;
1264
15
    return CXIdxEntity_Field;
1265
6
  case SymbolKind::EnumConstant: return CXIdxEntity_EnumConstant;
1266
113
  case SymbolKind::Class:
1267
113
    if (Lang == SymbolLanguage::ObjC)
1268
62
      return CXIdxEntity_ObjCClass;
1269
51
    return CXIdxEntity_CXXClass;
1270
9
  case SymbolKind::Protocol:
1271
9
    if (Lang == SymbolLanguage::ObjC)
1272
9
      return CXIdxEntity_ObjCProtocol;
1273
0
    return CXIdxEntity_CXXInterface;
1274
10
  case SymbolKind::Extension: return CXIdxEntity_ObjCCategory;
1275
1.87k
  case SymbolKind::InstanceMethod:
1276
1.87k
    if (Lang == SymbolLanguage::ObjC)
1277
59
      return CXIdxEntity_ObjCInstanceMethod;
1278
1.81k
    return CXIdxEntity_CXXInstanceMethod;
1279
15
  case SymbolKind::ClassMethod: return CXIdxEntity_ObjCClassMethod;
1280
0
  case SymbolKind::StaticMethod: return CXIdxEntity_CXXStaticMethod;
1281
12
  case SymbolKind::InstanceProperty: return CXIdxEntity_ObjCProperty;
1282
0
  case SymbolKind::StaticProperty: return CXIdxEntity_CXXStaticVariable;
1283
18
  case SymbolKind::Namespace: return CXIdxEntity_CXXNamespace;
1284
0
  case SymbolKind::NamespaceAlias: return CXIdxEntity_CXXNamespaceAlias;
1285
11
  case SymbolKind::Constructor: return CXIdxEntity_CXXConstructor;
1286
0
  case SymbolKind::Destructor: return CXIdxEntity_CXXDestructor;
1287
0
  case SymbolKind::ConversionFunction: return CXIdxEntity_CXXConversionFunction;
1288
10
  case SymbolKind::Parameter: return CXIdxEntity_Variable;
1289
0
  }
1290
0
  llvm_unreachable("invalid symbol kind");
1291
0
}
1292
1293
static CXIdxEntityCXXTemplateKind
1294
4.14k
getEntityKindFromSymbolProperties(SymbolPropertySet K) {
1295
4.14k
  if (K & (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization)
1296
2
    return CXIdxEntity_TemplatePartialSpecialization;
1297
4.14k
  if (K & (SymbolPropertySet)SymbolProperty::TemplateSpecialization)
1298
3
    return CXIdxEntity_TemplateSpecialization;
1299
4.14k
  if (K & (SymbolPropertySet)SymbolProperty::Generic)
1300
23
    return CXIdxEntity_Template;
1301
4.11k
  return CXIdxEntity_NonTemplate;
1302
4.11k
}
1303
1304
4.14k
static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L) {
1305
4.14k
  switch (L) {
1306
2.05k
  case SymbolLanguage::C: return CXIdxEntityLang_C;
1307
172
  case SymbolLanguage::ObjC: return CXIdxEntityLang_ObjC;
1308
1.91k
  case SymbolLanguage::CXX: return CXIdxEntityLang_CXX;
1309
0
  case SymbolLanguage::Swift: return CXIdxEntityLang_Swift;
1310
0
  }
1311
0
  llvm_unreachable("invalid symbol language");
1312
0
}