Coverage Report

Created: 2022-07-16 07:03

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