Coverage Report

Created: 2020-02-15 09:57

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