summaryrefslogtreecommitdiff
path: root/kythe/cxx/indexer/cxx/IndexerASTHooks.h
blob: 0f7132343f37fa3502faf1b11815eb6a0eec4036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
/*
 * Copyright 2014 The Kythe Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Defines AST visitors and consumers used by the indexer tool.

#ifndef KYTHE_CXX_INDEXER_CXX_INDEXER_AST_HOOKS_H_
#define KYTHE_CXX_INDEXER_CXX_INDEXER_AST_HOOKS_H_

#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>

#include "GraphObserver.h"
#include "IndexerLibrarySupport.h"
#include "absl/container/flat_hash_map.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Index/USRGeneration.h"
#include "clang/Sema/SemaConsumer.h"
#include "clang/Sema/Template.h"
#include "glog/logging.h"
#include "indexed_parent_map.h"
#include "indexer_worklist.h"
#include "kythe/cxx/indexer/cxx/node_set.h"
#include "kythe/cxx/indexer/cxx/recursive_type_visitor.h"
#include "kythe/cxx/indexer/cxx/semantic_hash.h"
#include "marked_source.h"
#include "re2/re2.h"
#include "type_map.h"

namespace kythe {

/// \brief Specifies whether uncommonly-used data should be dropped.
enum Verbosity : bool {
  Classic = true,  ///< Emit all data.
  Lite = false     ///< Emit only common data.
};

/// \brief Specifies what the indexer should do if it encounters a case it
/// doesn't understand.
enum BehaviorOnUnimplemented : bool {
  Abort = false,   ///< Stop indexing and exit with an error.
  Continue = true  ///< Continue indexing, possibly emitting less data.
};

/// \brief Specifies what the indexer should do with template instantiations.
enum BehaviorOnTemplates : bool {
  SkipInstantiations = false,  ///< Don't visit template instantiations.
  VisitInstantiations = true   ///< Visit template instantiations.
};

/// \brief Specifies if the indexer should emit documentation nodes for comments
/// associated with forward declarations.
enum BehaviorOnFwdDeclComments : bool { Emit = true, Ignore = false };

/// \brief A byte range that links to some node.
struct MiniAnchor {
  size_t Begin;
  size_t End;
  GraphObserver::NodeId AnchoredTo;
};

/// \brief Specifies whether dataflow edges should be emitted.
enum EmitDataflowEdges : bool {
  No = false,  ///< Don't emit dataflow edges.
  Yes = true   ///< Emit dataflow edges.
};

/// Adds brackets to Text to define anchor locations (escaping existing ones)
/// and sorts Anchors such that the ith Anchor corresponds to the ith opening
/// bracket. Drops empty or negative-length spans.
void InsertAnchorMarks(std::string& Text, std::vector<MiniAnchor>& Anchors);

/// \brief Used internally to check whether parts of the AST can be ignored.
class PruneCheck;

/// \brief An AST visitor that extracts information for a translation unit and
/// writes it to a `GraphObserver`.
class IndexerASTVisitor : public RecursiveTypeVisitor<IndexerASTVisitor> {
  using Base = RecursiveTypeVisitor;

 public:
  IndexerASTVisitor(clang::ASTContext& C, BehaviorOnUnimplemented B,
                    BehaviorOnTemplates T, Verbosity V,
                    BehaviorOnFwdDeclComments ObjC,
                    BehaviorOnFwdDeclComments Cpp, const LibrarySupports& S,
                    clang::Sema& Sema, std::function<bool()> ShouldStopIndexing,
                    GraphObserver* GO = nullptr, int UsrByteSize = 0,
                    EmitDataflowEdges EDE = EmitDataflowEdges::No,
                    std::shared_ptr<re2::RE2> TIEPP = nullptr)
      : IgnoreUnimplemented(B),
        TemplateMode(T),
        Verbosity(V),
        ObjCFwdDocs(ObjC),
        CppFwdDocs(Cpp),
        Observer(GO ? *GO : NullObserver),
        Context(C),
        Supports(S),
        Sema(Sema),
        MarkedSources(&Sema, &Observer),
        ShouldStopIndexing(std::move(ShouldStopIndexing)),
        UsrByteSize(UsrByteSize),
        DataflowEdges(EDE),
        TemplateInstanceExcludePathPattern(TIEPP) {}

  bool VisitDecl(const clang::Decl* Decl);
  bool TraverseFieldDecl(clang::FieldDecl* Decl);
  bool VisitFieldDecl(const clang::FieldDecl* Decl);
  bool TraverseVarDecl(clang::VarDecl* Decl);
  bool VisitVarDecl(const clang::VarDecl* Decl);
  bool VisitNamespaceDecl(const clang::NamespaceDecl* Decl);
  bool VisitBindingDecl(const clang::BindingDecl* Decl);
  bool VisitSizeOfPackExpr(const clang::SizeOfPackExpr* Expr);
  bool VisitDeclRefExpr(const clang::DeclRefExpr* DRE);

  bool TraverseCallExpr(clang::CallExpr* CE);
  bool TraverseReturnStmt(clang::ReturnStmt* RS);
  bool TraverseBinaryOperator(clang::BinaryOperator* BO);
  bool TraverseUnaryOperator(clang::UnaryOperator* BO);
  bool TraverseCompoundAssignOperator(clang::CompoundAssignOperator* CAO);

  bool TraverseInitListExpr(clang::InitListExpr* ILE);
  bool VisitInitListExpr(const clang::InitListExpr* ILE);
  bool VisitDesignatedInitExpr(const clang::DesignatedInitExpr* DIE);

  bool VisitCXXConstructExpr(const clang::CXXConstructExpr* E);
  bool VisitCXXDeleteExpr(const clang::CXXDeleteExpr* E);
  bool VisitCXXNewExpr(const clang::CXXNewExpr* E);
  bool VisitCXXPseudoDestructorExpr(const clang::CXXPseudoDestructorExpr* E);
  bool VisitCXXUnresolvedConstructExpr(
      const clang::CXXUnresolvedConstructExpr* E);
  bool VisitCallExpr(const clang::CallExpr* Expr);
  bool VisitMemberExpr(const clang::MemberExpr* Expr);
  bool VisitCXXDependentScopeMemberExpr(
      const clang::CXXDependentScopeMemberExpr* Expr);

  bool TraverseNestedNameSpecifierLoc(clang::NestedNameSpecifierLoc NNS);

  // Visitors for leaf TypeLoc types.
  bool VisitBuiltinTypeLoc(clang::BuiltinTypeLoc TL);
  bool VisitEnumTypeLoc(clang::EnumTypeLoc TL);
  bool VisitRecordTypeLoc(clang::RecordTypeLoc TL);
  bool VisitTemplateTypeParmTypeLoc(clang::TemplateTypeParmTypeLoc TL);
  bool VisitSubstTemplateTypeParmTypeLoc(
      clang::SubstTemplateTypeParmTypeLoc TL);

  template <typename TypeLoc, typename Type>
  bool VisitTemplateSpecializationTypePairHelper(TypeLoc Written,
                                                 const Type* Resolved);

  bool VisitTemplateSpecializationTypeLoc(
      clang::TemplateSpecializationTypeLoc TL);
  bool VisitDeducedTemplateSpecializationTypePair(
      clang::DeducedTemplateSpecializationTypeLoc TL,
      const clang::DeducedTemplateSpecializationType* T);

  bool VisitAutoTypePair(clang::AutoTypeLoc TL, const clang::AutoType* T);
  bool VisitDecltypeTypeLoc(clang::DecltypeTypeLoc TL);
  bool VisitElaboratedTypeLoc(clang::ElaboratedTypeLoc TL);
  bool VisitTypedefTypeLoc(clang::TypedefTypeLoc TL);
  bool VisitInjectedClassNameTypeLoc(clang::InjectedClassNameTypeLoc TL);
  bool VisitDependentNameTypeLoc(clang::DependentNameTypeLoc TL);
  bool VisitPackExpansionTypeLoc(clang::PackExpansionTypeLoc TL);
  bool VisitObjCObjectTypeLoc(clang::ObjCObjectTypeLoc TL);
  bool VisitObjCTypeParamTypeLoc(clang::ObjCTypeParamTypeLoc TL);

  bool TraverseAttributedTypeLoc(clang::AttributedTypeLoc TL);
  bool TraverseDependentAddressSpaceTypeLoc(
      clang::DependentAddressSpaceTypeLoc TL);

  bool TraverseMemberPointerTypeLoc(clang::MemberPointerTypeLoc TL);

  // Emit edges for an anchor pointing to the indicated type.
  NodeSet RecordTypeLocSpellingLocation(clang::TypeLoc TL);
  NodeSet RecordTypeLocSpellingLocation(clang::TypeLoc Written,
                                        const clang::Type* Resolved);

  bool TraverseDeclarationNameInfo(clang::DeclarationNameInfo NameInfo);

  // Visit the subtypes of TypedefNameDecl individually because we want to do
  // something different with ObjCTypeParamDecl.
  bool VisitTypedefDecl(const clang::TypedefDecl* Decl);
  bool VisitTypeAliasDecl(const clang::TypeAliasDecl* Decl);
  bool VisitObjCTypeParamDecl(const clang::ObjCTypeParamDecl* Decl);
  bool VisitUsingShadowDecl(const clang::UsingShadowDecl* Decl);

  bool VisitRecordDecl(const clang::RecordDecl* Decl);
  bool VisitEnumDecl(const clang::EnumDecl* Decl);
  bool VisitEnumConstantDecl(const clang::EnumConstantDecl* Decl);
  bool VisitFunctionDecl(clang::FunctionDecl* Decl);
  bool TraverseDecl(clang::Decl* Decl);
  bool TraverseCXXConstructorDecl(clang::CXXConstructorDecl* CD);

  bool TraverseConstructorInitializer(clang::CXXCtorInitializer* Init);
  bool TraverseCXXNewExpr(clang::CXXNewExpr* E);
  bool TraverseCXXFunctionalCastExpr(clang::CXXFunctionalCastExpr* E);
  bool TraverseCXXTemporaryObjectExpr(clang::CXXTemporaryObjectExpr* E);

  bool IndexConstructExpr(const clang::CXXConstructExpr* E,
                          const clang::TypeSourceInfo* TSI);

  // Objective C specific nodes
  bool VisitObjCPropertyImplDecl(const clang::ObjCPropertyImplDecl* Decl);
  bool VisitObjCCompatibleAliasDecl(const clang::ObjCCompatibleAliasDecl* Decl);
  bool VisitObjCCategoryDecl(const clang::ObjCCategoryDecl* Decl);
  bool VisitObjCImplementationDecl(
      const clang::ObjCImplementationDecl* ImplDecl);
  bool VisitObjCCategoryImplDecl(const clang::ObjCCategoryImplDecl* ImplDecl);
  bool VisitObjCInterfaceDecl(const clang::ObjCInterfaceDecl* Decl);
  bool VisitObjCProtocolDecl(const clang::ObjCProtocolDecl* Decl);
  bool VisitObjCMethodDecl(const clang::ObjCMethodDecl* Decl);
  bool VisitObjCPropertyDecl(const clang::ObjCPropertyDecl* Decl);
  bool VisitObjCIvarRefExpr(const clang::ObjCIvarRefExpr* IRE);
  bool VisitObjCMessageExpr(const clang::ObjCMessageExpr* Expr);
  bool VisitObjCPropertyRefExpr(const clang::ObjCPropertyRefExpr* Expr);

  // TODO(salguarnieri) We could link something here (the square brackets?) to
  // the setter and getter methods
  //   bool VisitObjCSubscriptRefExpr(const clang::ObjCSubscriptRefExpr *Expr);

  // TODO(salguarnieri) delete this comment block when we have more objective-c
  // support implemented.
  //
  //  Visitors that are left to their default behavior because we do not need
  //  to take any action while visiting them.
  //   bool VisitObjCDictionaryLiteral(const clang::ObjCDictionaryLiteral *D);
  //   bool VisitObjCArrayLiteral(const clang::ObjCArrayLiteral *D);
  //   bool VisitObjCBoolLiteralExpr(const clang::ObjCBoolLiteralExpr *D);
  //   bool VisitObjCStringLiteral(const clang::ObjCStringLiteral *D);
  //   bool VisitObjCEncodeExpr(const clang::ObjCEncodeExpr *Expr);
  //   bool VisitObjCBoxedExpr(const clang::ObjCBoxedExpr *Expr);
  //   bool VisitObjCSelectorExpr(const clang::ObjCSelectorExpr *Expr);
  //   bool VisitObjCIndirectCopyRestoreExpr(
  //    const clang::ObjCIndirectCopyRestoreExpr *Expr);
  //   bool VisitObjCIsaExpr(const clang::ObjCIsaExpr *Expr);
  //
  //  We visit the subclasses of ObjCContainerDecl so there is nothing to do.
  //   bool VisitObjCContainerDecl(const clang::ObjCContainerDecl *D);
  //
  //  We visit the subclasses of ObjCImpleDecl so there is nothing to do.
  //   bool VisitObjCImplDecl(const clang::ObjCImplDecl *D);
  //
  //  There is nothing specific we need to do for blocks. The recursive visitor
  //  will visit the components of them correctly.
  //   bool VisitBlockDecl(const clang::BlockDecl *Decl);
  //   bool VisitBlockExpr(const clang::BlockExpr *Expr);

  /// \brief For functions that support it, controls the emission of range
  /// information.
  enum class EmitRanges {
    No,  ///< Don't emit range information.
    Yes  ///< Emit range information when it's available.
  };

  // Objective C methods don't have TypeSourceInfo so we must construct a type
  // for the methods to be used in the graph.
  absl::optional<GraphObserver::NodeId> CreateObjCMethodTypeNode(
      const clang::ObjCMethodDecl* MD);

  /// \brief Builds a stable node ID for a compile-time expression.
  /// \param Expr The expression to represent.
  /// \param ER Whether to notify the `GraphObserver` about source text
  /// ranges for expressions.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForExpr(
      const clang::Expr* Expr, EmitRanges ER);

  /// \brief Builds a stable node ID for a special template argument.
  /// \param Id A string representing the special argument.
  GraphObserver::NodeId BuildNodeIdForSpecialTemplateArgument(
      llvm::StringRef Id);

  /// \brief Builds a stable node ID for a template expansion template argument.
  /// \param Name The template pattern being expanded.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForTemplateExpansion(
      clang::TemplateName Name);

  /// \brief Builds a stable NodeSet for the given TypeLoc.
  /// \param TL The TypeLoc for which to build a NodeSet.
  /// \returns NodeSet instance indicating claimability of the contained
  /// NodeIds.
  NodeSet BuildNodeSetForType(const clang::QualType& QT);
  NodeSet BuildNodeSetForType(const clang::Type* T);
  NodeSet BuildNodeSetForType(const clang::TypeLoc& TL);

  NodeSet BuildNodeSetForTypeInternal(const clang::Type& T);
  NodeSet BuildNodeSetForTypeInternal(const clang::QualType& QT);

  NodeSet BuildNodeSetForBuiltin(const clang::BuiltinType& T) const;
  NodeSet BuildNodeSetForEnum(const clang::EnumType& T);
  NodeSet BuildNodeSetForRecord(const clang::RecordType& T);
  NodeSet BuildNodeSetForInjectedClassName(
      const clang::InjectedClassNameType& T);
  NodeSet BuildNodeSetForTemplateTypeParm(const clang::TemplateTypeParmType& T);
  NodeSet BuildNodeSetForPointer(const clang::PointerType& T);
  NodeSet BuildNodeSetForMemberPointer(const clang::MemberPointerType& T);
  NodeSet BuildNodeSetForLValueReference(const clang::LValueReferenceType& T);
  NodeSet BuildNodeSetForRValueReference(const clang::RValueReferenceType& T);

  NodeSet BuildNodeSetForAuto(const clang::AutoType& TL);
  NodeSet BuildNodeSetForDeducedTemplateSpecialization(
      const clang::DeducedTemplateSpecializationType& TL);
  // Helper used for Auto and DeducedTemplateSpecialization.
  NodeSet BuildNodeSetForDeduced(const clang::DeducedType& T);

  NodeSet BuildNodeSetForConstantArray(const clang::ConstantArrayType& TL);
  NodeSet BuildNodeSetForIncompleteArray(const clang::IncompleteArrayType& TL);
  NodeSet BuildNodeSetForDependentSizedArray(
      const clang::DependentSizedArrayType& T);
  NodeSet BuildNodeSetForExtInt(const clang::ExtIntType& T);
  NodeSet BuildNodeSetForDependentExtInt(const clang::DependentExtIntType& T);
  NodeSet BuildNodeSetForFunctionProto(const clang::FunctionProtoType& T);
  NodeSet BuildNodeSetForFunctionNoProto(const clang::FunctionNoProtoType& T);
  NodeSet BuildNodeSetForParen(const clang::ParenType& T);
  NodeSet BuildNodeSetForDecltype(const clang::DecltypeType& T);
  NodeSet BuildNodeSetForElaborated(const clang::ElaboratedType& T);
  NodeSet BuildNodeSetForTypedef(const clang::TypedefType& T);

  NodeSet BuildNodeSetForSubstTemplateTypeParm(
      const clang::SubstTemplateTypeParmType& T);
  NodeSet BuildNodeSetForDependentName(const clang::DependentNameType& T);
  NodeSet BuildNodeSetForTemplateSpecialization(
      const clang::TemplateSpecializationType& T);
  NodeSet BuildNodeSetForPackExpansion(const clang::PackExpansionType& T);
  NodeSet BuildNodeSetForBlockPointer(const clang::BlockPointerType& T);
  NodeSet BuildNodeSetForObjCObjectPointer(
      const clang::ObjCObjectPointerType& T);
  NodeSet BuildNodeSetForObjCObject(const clang::ObjCObjectType& T);
  NodeSet BuildNodeSetForObjCTypeParam(const clang::ObjCTypeParamType& T);
  NodeSet BuildNodeSetForObjCInterface(const clang::ObjCInterfaceType& T);
  NodeSet BuildNodeSetForAttributed(const clang::AttributedType& T);
  NodeSet BuildNodeSetForDependentAddressSpace(
      const clang::DependentAddressSpaceType& T);

  // Helper function which constructs marked source and records
  // a tnominal node for the given `Decl`.
  GraphObserver::NodeId BuildNominalNodeIdForDecl(const clang::NamedDecl* Decl);

  // Helper used by BuildNodeSetForRecord and BuildNodeSetForInjectedClassName.
  NodeSet BuildNodeSetForNonSpecializedRecordDecl(
      const clang::RecordDecl* Decl);

  const clang::TemplateTypeParmDecl* FindTemplateTypeParmTypeDecl(
      const clang::TemplateTypeParmType& T) const;

  absl::optional<GraphObserver::NodeId> BuildNodeIdForObjCProtocols(
      const clang::ObjCObjectType& T);
  GraphObserver::NodeId BuildNodeIdForObjCProtocols(
      absl::Span<const GraphObserver::NodeId> ProtocolIds);

  std::vector<GraphObserver::NodeId> BuildNodeIdsForObjCProtocols(
      GraphObserver::NodeId BaseType, const clang::ObjCObjectType& T);
  std::vector<GraphObserver::NodeId> BuildNodeIdsForObjCProtocols(
      const clang::ObjCObjectType& T);

  /// \brief Builds a stable node ID for `Type`.
  /// \param TypeLoc The type that is being identified.
  /// \return The Node ID for `Type`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForType(
      const clang::TypeLoc& TypeLoc);

  /// \brief Builds a stable node ID for `QT`.
  /// \param QT The type that is being identified.
  /// \return The Node ID for `QT`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForType(
      const clang::QualType& QT);

  /// \brief Builds a stable node ID for `T`.
  /// \param T The type that is being identified.
  /// \return The Node ID for `T`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForType(
      const clang::Type* T);

  /// \brief Builds a stable node ID for the given `TemplateName`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForTemplateName(
      const clang::TemplateName& Name);

  /// \brief Builds a stable node ID for the given `TemplateArgumentLoc`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForTemplateArgument(
      const clang::TemplateArgumentLoc& ArgLoc);

  /// \brief Builds a stable node ID for the given `TemplateArgument`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForTemplateArgument(
      const clang::TemplateArgument& Arg);

  /// \brief Builds a stable node ID for `Stmt`.
  ///
  /// This mechanism for generating IDs should only be used in contexts where
  /// identifying statements through source locations/wraith contexts is not
  /// possible (e.g., in implicit code).
  ///
  /// \param Decl The statement that is being identified
  /// \return The node for `Stmt` if the statement was implicit; otherwise,
  /// None.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForImplicitStmt(
      const clang::Stmt* Stmt);

  /// \brief Builds a stable node ID for `Decl`'s tapp if it's an implicit
  /// template instantiation.
  absl::optional<GraphObserver::NodeId>
  BuildNodeIdForImplicitTemplateInstantiation(const clang::Decl* Decl);

  /// \brief Builds a stable node ID for `Decl`'s tapp if it's an implicit
  /// function template instantiation.
  absl::optional<GraphObserver::NodeId>
  BuildNodeIdForImplicitFunctionTemplateInstantiation(
      const clang::FunctionDecl* FD);

  /// \brief Builds a stable node ID for `Decl`'s tapp if it's an implicit
  /// class template instantiation.
  absl::optional<GraphObserver::NodeId>
  BuildNodeIdForImplicitClassTemplateInstantiation(
      const clang::ClassTemplateSpecializationDecl* CTSD);

  /// \brief Builds a stable node ID for an external reference to `Decl`.
  ///
  /// This is equivalent to BuildNodeIdForDecl for Decls that are not
  /// implicit template instantiations; otherwise, it returns the `NodeId`
  /// for the tapp node for the instantiation.
  ///
  /// \param Decl The declaration that is being identified.
  /// \return The node for `Decl`.
  GraphObserver::NodeId BuildNodeIdForRefToDecl(const clang::Decl* Decl);

  /// \brief Builds a stable node ID for `Decl`.
  ///
  /// \param Decl The declaration that is being identified.
  /// \return The node for `Decl`.
  GraphObserver::NodeId BuildNodeIdForDecl(const clang::Decl* Decl);

  /// \brief Builds a stable node ID for the definition of `Decl`, if
  /// `Decl` is not already a definition and its definition can be found.
  ///
  /// \param Decl The declaration that is being identified.
  /// \return The node for the definition `Decl` if `Decl` isn't a definition
  /// and its definition can be found; or None.
  template <typename D>
  absl::optional<GraphObserver::NodeId> BuildNodeIdForDefnOfDecl(
      const D* Decl) {
    if (const auto* Defn = Decl->getDefinition()) {
      if (Defn != Decl) {
        return BuildNodeIdForDecl(Defn);
      }
    }
    return absl::nullopt;
  }

  /// \brief Builds a stable node ID for `TND`.
  ///
  /// \param Decl The declaration that is being identified.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForTypedefNameDecl(
      const clang::TypedefNameDecl* Decl);

  /// \brief Builds a stable node ID for `Decl`.
  ///
  /// There is not a one-to-one correspondence between `Decl`s and nodes.
  /// Certain `Decl`s, like `TemplateTemplateParmDecl`, are split into a
  /// node representing the parameter and a node representing the kind of
  /// the abstraction. The primary node is returned by the normal
  /// `BuildNodeIdForDecl` function.
  ///
  /// \param Decl The declaration that is being identified.
  /// \param Index The index of the sub-id to generate.
  ///
  /// \return A stable node ID for `Decl`'s `Index`th subnode.
  GraphObserver::NodeId BuildNodeIdForDecl(const clang::Decl* Decl,
                                           unsigned Index);

  /// \brief Categorizes the name of `Decl` according to the equivalence classes
  /// defined by `GraphObserver::NameId::NameEqClass`.
  GraphObserver::NameId::NameEqClass BuildNameEqClassForDecl(
      const clang::Decl* Decl) const;

  /// \brief Builds a stable name ID for the name of `Decl`.
  ///
  /// \param Decl The declaration that is being named.
  /// \return The name for `Decl`.
  GraphObserver::NameId BuildNameIdForDecl(const clang::Decl* Decl);

  /// \brief Records parameter and lookup edges for the given dependent name.
  ///
  /// \param NNS The qualifier on the name.
  /// \param Id The name itself.
  /// \param IdLoc The name's location.
  /// \param Root If provided, the primary NodeId is morally prepended to `NNS`
  /// such that the dependent name is lookup(lookup*(Root, NNS), Id).
  /// \return The NodeId for the dependent name.
  absl::optional<GraphObserver::NodeId> RecordEdgesForDependentName(
      const clang::NestedNameSpecifierLoc& NNS,
      const clang::DeclarationName& Id, const clang::SourceLocation IdLoc,
      const absl::optional<GraphObserver::NodeId>& Root = absl::nullopt);

  /// \brief Records parameter edges for the given dependent name.
  /// Also records Lookup edges for any nested Identifiers.
  /// \param DId The NodeId of the dependent name to use.
  /// \param NNSLoc The qualifier prefix of the dependent name, if any.
  /// \param Root If provided, the primary NodeId to prepend to `NNS`.
  /// \return The provided NodeId or nullopt if an unhandled element is
  /// encountered.
  absl::optional<GraphObserver::NodeId> RecordParamEdgesForDependentName(
      const GraphObserver::NodeId& DId, clang::NestedNameSpecifierLoc NNSLoc,
      const absl::optional<GraphObserver::NodeId>& Root = absl::nullopt);

  /// \brief Records the lookup edge for a dependent name.
  ///
  /// \param DId The NodeId of the name being looked up.
  /// \param Name The kind of name being looked up.
  /// \return The provided NodeId or absl::nullopt if Name is unsupported.
  absl::optional<GraphObserver::NodeId> RecordLookupEdgeForDependentName(
      const GraphObserver::NodeId& DId, const clang::DeclarationName& Name);

  /// \brief Builds a NodeId for the DependentName.
  ///
  /// \param Prefix The qualifier preceding the name.
  /// \param Identifier The identifier in question.
  GraphObserver::NodeId BuildNodeIdForDependentIdentifier(
      const clang::NestedNameSpecifier* Prefix,
      const clang::IdentifierInfo* Identifier);

  /// \brief Builds a NodeId for the DependentName.
  ///
  /// \param Prefix The qualifier preceding the name.
  /// \param Identifier The DeclarationName in question.
  GraphObserver::NodeId BuildNodeIdForDependentName(
      const clang::NestedNameSpecifier* Prefix,
      const clang::DeclarationName& Identifier);

  /// \brief Builds a NodeId for the provided NestedNameSpecifier, depending on
  /// its type.
  ///
  /// \param NNSLoc The NestedNameSpecifierLoc from which to construct a NodeId.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForNestedNameSpecifier(
      const clang::NestedNameSpecifier* NNS);

  /// \brief Builds a NodeId for the provided NestedNameSpecifier, depending on
  /// its type.
  ///
  /// \param NNSLoc The NestedNameSpecifierLoc from which to construct a NodeId.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForNestedNameSpecifierLoc(
      const clang::NestedNameSpecifierLoc& NNSLoc);

  /// \brief Is `VarDecl` a definition?
  ///
  /// A parameter declaration is considered a definition if it appears as part
  /// of a function definition; otherwise it is always a declaration. This
  /// differs from the C++ Standard, which treats these parameters as
  /// definitions (basic.scope.proto).
  static bool IsDefinition(const clang::VarDecl* VD);

  /// \brief Is `FunctionDecl` a definition?
  static bool IsDefinition(const clang::FunctionDecl* FunctionDecl);

  /// \brief Gets a range for the name of a declaration, whether that name is a
  /// single token or otherwise.
  ///
  /// The returned range is a best-effort attempt to cover the "name" of
  /// the entity as written in the source code.
  clang::SourceRange RangeForNameOfDeclaration(
      const clang::NamedDecl* Decl) const;

  bool TraverseClassTemplateDecl(clang::ClassTemplateDecl* TD);
  bool TraverseClassTemplateSpecializationDecl(
      clang::ClassTemplateSpecializationDecl* TD);
  bool TraverseClassTemplatePartialSpecializationDecl(
      clang::ClassTemplatePartialSpecializationDecl* TD);

  bool TraverseVarTemplateDecl(clang::VarTemplateDecl* TD);
  bool TraverseVarTemplateSpecializationDecl(
      clang::VarTemplateSpecializationDecl* TD);
  bool ForceTraverseVarTemplateSpecializationDecl(
      clang::VarTemplateSpecializationDecl* TD);
  bool TraverseVarTemplatePartialSpecializationDecl(
      clang::VarTemplatePartialSpecializationDecl* TD);

  bool TraverseFunctionDecl(clang::FunctionDecl* FD);
  bool TraverseFunctionTemplateDecl(clang::FunctionTemplateDecl* FTD);

  bool TraverseTypeAliasTemplateDecl(clang::TypeAliasTemplateDecl* TATD);

  bool shouldVisitTemplateInstantiations() const {
    return TemplateMode == BehaviorOnTemplates::VisitInstantiations;
  }
  bool shouldEmitObjCForwardClassDeclDocumentation() const {
    return ObjCFwdDocs == BehaviorOnFwdDeclComments::Emit;
  }
  bool shouldEmitCppForwardDeclDocumentation() const {
    return CppFwdDocs == BehaviorOnFwdDeclComments::Emit;
  }
  bool shouldVisitImplicitCode() const { return true; }
  // Disables data recursion. We intercept Traverse* methods in the RAV, which
  // are not triggered during data recursion.
  bool shouldUseDataRecursionFor(clang::Stmt* S) const { return false; }

  /// \brief Records the range of a definition. If the range cannot be placed
  /// somewhere inside a source file, no record is made.
  void MaybeRecordDefinitionRange(
      const absl::optional<GraphObserver::Range>& R,
      const GraphObserver::NodeId& Id,
      const absl::optional<GraphObserver::NodeId>& DeclId);

  /// \brief Records the full range of a definition. If the range cannot be
  /// placed somewhere inside a source file, no record is made.
  void MaybeRecordFullDefinitionRange(
      const absl::optional<GraphObserver::Range>& R,
      const GraphObserver::NodeId& Id,
      const absl::optional<GraphObserver::NodeId>& DeclId);

  /// \brief Returns the attached GraphObserver.
  GraphObserver& getGraphObserver() { return Observer; }

  /// \brief Returns the ASTContext.
  const clang::ASTContext& getASTContext() { return Context; }

  /// Returns `SR` as a `Range` in this `RecursiveASTVisitor`'s current
  /// RangeContext.
  absl::optional<GraphObserver::Range> ExplicitRangeInCurrentContext(
      const clang::SourceRange& SR);

  /// Returns `SR` as a `Range` in this `RecursiveASTVisitor`'s current
  /// RangeContext. If SR is in a macro, the returned Range will be mapped
  /// to a file first. If the range would be zero-width, it will be expanded
  /// via RangeForASTEntityFromSourceLocation.
  absl::optional<GraphObserver::Range> ExpandedRangeInCurrentContext(
      clang::SourceRange SR);
  /// Returns `SR` as a character-based file range.
  clang::SourceRange NormalizeRange(clang::SourceRange SR) const;

  /// If `Implicit` is true, returns `Id` as an implicit Range; otherwise,
  /// returns `SR` as a `Range` in this `RecursiveASTVisitor`'s current
  /// RangeContext.
  absl::optional<GraphObserver::Range> RangeInCurrentContext(
      bool Implicit, const GraphObserver::NodeId& Id,
      const clang::SourceRange& SR);

  /// If `Id` is some NodeId, returns it as an implicit Range; otherwise,
  /// returns `SR` as a `Range` in this `RecursiveASTVisitor`'s current
  /// RangeContext.
  absl::optional<GraphObserver::Range> RangeInCurrentContext(
      const absl::optional<GraphObserver::NodeId>& Id,
      const clang::SourceRange& SR);

  void RunJob(std::unique_ptr<IndexJob> JobToRun) {
    Job = std::move(JobToRun);
    if (Job->IsDeclJob) {
      TraverseDecl(Job->Decl);
    } else {
      // There is no declaration attached to a top-level file comment.
      HandleFileLevelComments(Job->FileId, Job->FileNode);
    }
  }

  const IndexJob& getCurrentJob() {
    CHECK(Job != nullptr);
    return *Job;
  }

  /// \brief Call after sema but before traversal. Applies semantic metadata
  /// (e.g., write tags, alias tags).
  void PrepareAlternateSemanticCache();

  void Work(clang::Decl* InitialDecl,
            std::unique_ptr<IndexerWorklist> NewWorklist) {
    Worklist = std::move(NewWorklist);
    Worklist->EnqueueJob(absl::make_unique<IndexJob>(InitialDecl));
    while (!ShouldStopIndexing() && Worklist->DoWork())
      ;
    Observer.iterateOverClaimedFiles(
        [this, InitialDecl](clang::FileID Id,
                            const GraphObserver::NodeId& FileNode) {
          RunJob(absl::make_unique<IndexJob>(InitialDecl, Id, FileNode));
          return !ShouldStopIndexing();
        });
    Worklist.reset();
  }

  /// \brief Provides execute-only access to ShouldStopIndexing. Should be used
  /// from the same thread that's walking the AST.
  bool shouldStopIndexing() const { return ShouldStopIndexing(); }

  /// Blames a call to `Callee` at `Range` on everything at the top of
  /// `BlameStack` (or does nothing if there's nobody to blame).
  void RecordCallEdges(const GraphObserver::Range& Range,
                       const GraphObserver::NodeId& Callee);

  // Blames the use of a `Decl` at a particular `Range` on everything at the
  // top of `BlameStack`. If there is nothing at the top of `BlameStack`,
  // blames the use on the file.
  void RecordBlame(const clang::Decl* Decl, const GraphObserver::Range& Range);

  /// \return whether `range` should be considered to be implicit under the
  /// current context.
  GraphObserver::Implicit IsImplicit(const GraphObserver::Range& range);

 private:
  friend class PruneCheck;

  /// Whether we should stop on missing cases or continue on.
  BehaviorOnUnimplemented IgnoreUnimplemented;

  /// Should we visit template instantiations?
  BehaviorOnTemplates TemplateMode;

  /// Should we emit all data?
  enum Verbosity Verbosity;

  /// Should we emit documentation for forward class decls in ObjC?
  BehaviorOnFwdDeclComments ObjCFwdDocs;

  /// Should we emit documentation for forward decls in C++?
  BehaviorOnFwdDeclComments CppFwdDocs;

  NullGraphObserver NullObserver;
  GraphObserver& Observer;
  clang::ASTContext& Context;

  /// \brief The result of calling into the lexer.
  enum class LexerResult {
    Failure,  ///< The operation failed.
    Success   ///< The operation completed.
  };

  /// \brief Using the `Observer`'s preprocessor, relexes the token at the
  /// specified location. Ignores whitespace.
  /// \param StartLocation Where to begin lexing.
  /// \param Token The token to overwrite.
  /// \return `Failure` if there was a failure, `Success` on success.
  LexerResult getRawToken(clang::SourceLocation StartLocation,
                          clang::Token& Token) const {
    return Observer.getPreprocessor()->getRawToken(StartLocation, Token,
                                                   true /* ignoreWhiteSpace */)
               ? LexerResult::Failure
               : LexerResult::Success;
  }

  /// \brief Handle the file-level comments for `Id` with node ID `FileId`.
  void HandleFileLevelComments(clang::FileID Id,
                               const GraphObserver::NodeId& FileNode);

  /// \brief Emit data for `Comment` that documents `DocumentedNode`, using
  /// `DC` for lookups.
  void VisitComment(const clang::RawComment* Comment,
                    const clang::DeclContext* DC,
                    const GraphObserver::NodeId& DocumentedNode);

  /// \brief Emit data for attributes attached to `Decl`, whose `NodeId`
  /// is `TargetNode`.
  void VisitAttributes(const clang::Decl* Decl,
                       const GraphObserver::NodeId& TargetNode);

  /// \brief Attempts to find the ID of the first parent of `Decl` for
  /// attaching a `childof` relationship.
  absl::optional<GraphObserver::NodeId> GetDeclChildOf(const clang::Decl* D);

  /// \brief Attempts to add some representation of `ND` to `Ostream`.
  /// \return true on success; false on failure.
  bool AddNameToStream(llvm::raw_string_ostream& Ostream,
                       const clang::NamedDecl* ND);

  /// \brief Assign `ND` (whose node ID is `TargetNode`) a USR if USRs are
  /// enabled.
  ///
  /// USRs are added only for NamedDecls that:
  ///   * are not under implicit template instantiations
  ///   * are not in a DeclContext inside a function body
  ///   * can actually be assigned USRs from Clang
  ///
  /// Similar to the way we deal with JVM names, the corpus, path,
  /// and root fields of a usr vname are cleared. Clients are permitted
  /// to write their own USR tickets. The USR value itself is encoded
  /// in capital hex (to match Clang's own internal USR stringification,
  /// modulo the configurable size of the SHA1 prefix).
  void AssignUSR(const GraphObserver::NodeId& TargetNode,
                 const clang::NamedDecl* ND);

  /// Assigns a USR to an alias.
  void AssignUSR(const GraphObserver::NameId& TargetName,
                 const GraphObserver::NodeId& AliasedType,
                 const clang::NamedDecl* ND) {
    AssignUSR(Observer.nodeIdForTypeAliasNode(TargetName, AliasedType), ND);
  }

  GraphObserver::NodeId ApplyBuiltinTypeConstructor(
      const char* BuiltinName, const GraphObserver::NodeId& Param);

  /// \brief Returns the parent of the given node, along with the index
  /// at which the node appears underneath each parent.
  ///
  /// Note that this will lazily compute the parents of all nodes
  /// and store them for later retrieval. Thus, the first call is O(n)
  /// in the number of AST nodes.
  ///
  /// Caveats and FIXMEs:
  /// Calculating the parent map over all AST nodes will need to load the
  /// full AST. This can be undesirable in the case where the full AST is
  /// expensive to create (for example, when using precompiled header
  /// preambles). Thus, there are good opportunities for optimization here.
  /// One idea is to walk the given node downwards, looking for references
  /// to declaration contexts - once a declaration context is found, compute
  /// the parent map for the declaration context; if that can satisfy the
  /// request, loading the whole AST can be avoided. Note that this is made
  /// more complex by statements in templates having multiple parents - those
  /// problems can be solved by building closure over the templated parts of
  /// the AST, which also avoids touching large parts of the AST.
  /// Additionally, we will want to add an interface to already give a hint
  /// where to search for the parents, for example when looking at a statement
  /// inside a certain function.
  ///
  /// 'NodeT' can be one of Decl, Stmt, Type, TypeLoc,
  /// NestedNameSpecifier or NestedNameSpecifierLoc.
  template <typename NodeT>
  const IndexedParent* getIndexedParent(const NodeT& Node) {
    return getIndexedParent(clang::DynTypedNode::create(Node));
  }

  /// \return true if `Decl` and all of the nodes underneath it are prunable.
  ///
  /// A subtree is prunable if it's "the same" in all possible indexer runs.
  /// This excludes, for example, certain template instantiations.
  bool declDominatesPrunableSubtree(const clang::Decl* Decl);

  const IndexedParent* getIndexedParent(const clang::DynTypedNode& Node);

  /// Initializes AllParents, if necessary, and then returns a pointer to it.
  const IndexedParentMap* getAllParents();

  /// A map from memoizable DynTypedNodes to their parent nodes
  /// and their child indices with respect to those parents.
  /// Filled on the first call to `getIndexedParents`.
  std::unique_ptr<IndexedParentMap> AllParents;

  /// Records information about the template `Template` wrapping the node
  /// `BodyId`, including the edge linking the template and its body. Returns
  /// the `NodeId` for the dominating template.
  template <typename TemplateDeclish>
  GraphObserver::NodeId RecordTemplate(
      const TemplateDeclish* Decl, const GraphObserver::NodeId& BodyDeclNode);

  /// Records information about the generic class by wrapping the node
  /// `BodyId`. Returns the `NodeId` for the dominating generic type.
  GraphObserver::NodeId RecordGenericClass(
      const clang::ObjCInterfaceDecl* IDecl,
      const clang::ObjCTypeParamList* TPL, const GraphObserver::NodeId& BodyId);

  /// \brief Returns a vector of NodeId for each template argument.
  absl::optional<std::vector<GraphObserver::NodeId>> BuildTemplateArgumentList(
      llvm::ArrayRef<clang::TemplateArgument> Args);
  absl::optional<std::vector<GraphObserver::NodeId>> BuildTemplateArgumentList(
      llvm::ArrayRef<clang::TemplateArgumentLoc> Args);

  /// Dumps information about `TypeContext` to standard error when looking for
  /// an entry at (`Depth`, `Index`).
  void DumpTypeContext(unsigned Depth, unsigned Index);

  /// \brief Attempts to add a childof edge between DeclNode and its parent.
  /// \param Decl The (outer, in the case of a template) decl.
  /// \param DeclNode The (outer) `NodeId` for `Decl`.
  void AddChildOfEdgeToDeclContext(const clang::Decl* Decl,
                                   const GraphObserver::NodeId& DeclNode);

  /// Points at the inner node of the DeclContext, if it's a template.
  /// Otherwise points at the DeclContext as a Decl.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForDeclContext(
      const clang::DeclContext* DC);

  /// Points at the tapp node for a DeclContext, if it's an implicit template
  /// instantiation. Otherwise behaves as `BuildNodeIdForDeclContext`.
  absl::optional<GraphObserver::NodeId> BuildNodeIdForRefToDeclContext(
      const clang::DeclContext* DC);

  /// Avoid regenerating type node IDs and keep track of where we are while
  /// generating node IDs for recursive types. The key is opaque and
  /// makes sense only within the implementation of this class.
  TypeMap<NodeSet> TypeNodes;

  /// \brief Visit an Expr that refers to some NamedDecl.
  ///
  /// DeclRefExpr and ObjCIvarRefExpr are similar entities and can be processed
  /// in the same way but do not have a useful common ancestry.
  bool VisitDeclRefOrIvarRefExpr(const clang::Expr* Expr,
                                 const clang::NamedDecl* const FoundDecl,
                                 clang::SourceLocation SL,
                                 bool IsImplicit = false);

  /// \brief Connect a NodeId to the super and implemented protocols for a
  /// ObjCInterfaceDecl.
  ///
  /// Helper method used to connect an interface to the super and protocols it
  /// implements. It is also used to connect the interface implementation to
  /// these nodes as well. In that case, the interface implementation node is
  /// passed in as the first argument and the interface decl is passed in as the
  /// second.
  ///
  /// \param BodyDeclNode The node to connect to the super and protocols for
  /// the interface. This may be a interface decl node or an interface impl
  /// node.
  /// \param IFace The interface decl to use to look up the super and
  //// protocols.
  void ConnectToSuperClassAndProtocols(const GraphObserver::NodeId BodyDeclNode,
                                       const clang::ObjCInterfaceDecl* IFace);
  void ConnectToProtocols(const GraphObserver::NodeId BodyDeclNode,
                          clang::ObjCProtocolList::loc_iterator locStart,
                          clang::ObjCProtocolList::loc_iterator locEnd,
                          clang::ObjCProtocolList::iterator itStart,
                          clang::ObjCProtocolList::iterator itEnd);

  /// \brief Connect a parameter to a function decl.
  ///
  /// For FunctionDecl and ObjCMethodDecl, this connects the parameters to the
  /// function/method decl.
  /// \param Decl This should be a FunctionDecl or ObjCMethodDecl.
  void ConnectParam(const clang::Decl* Decl,
                    const GraphObserver::NodeId& FuncNode,
                    bool IsFunctionDefinition, const unsigned int ParamNumber,
                    const clang::ParmVarDecl* Param, bool DeclIsImplicit);

  /// \brief Draw the completes edge from a Decl to each of its redecls.
  void RecordCompletesForRedecls(const clang::Decl* Decl,
                                 const clang::SourceRange& NameRange,
                                 const GraphObserver::NodeId& DeclNode);

  /// \brief Draw an extends/category edge from the category to the class the
  /// category is extending.
  ///
  /// For example, @interface A (Cat) ... We draw an extends edge from the
  /// ObjCCategoryDecl for Cat to the ObjCInterfaceDecl for A.
  ///
  /// \param DeclNode The node for the category (impl or decl).
  /// \param IFace The class interface for class we are adding a category to.
  void ConnectCategoryToBaseClass(const GraphObserver::NodeId& DeclNode,
                                  const clang::ObjCInterfaceDecl* IFace);

  void LogErrorWithASTDump(absl::string_view msg,
                           const clang::Decl* Decl) const;
  void LogErrorWithASTDump(absl::string_view msg,
                           const clang::Expr* Expr) const;

  /// \brief This is used to handle the visitation of a clang::TypedefDecl
  /// or a clang::TypeAliasDecl.
  bool VisitCTypedef(const clang::TypedefNameDecl* Decl);

  /// \brief Find the implementation for `MD`. If `MD` is a definition, `MD` is
  /// returned. Otherwise, the method tries to find the implementation by
  /// looking through the interface and its implementation. If a method
  /// implementation is found, it is returned otherwise `MD` is returned.
  const clang::ObjCMethodDecl* FindMethodDefn(
      const clang::ObjCMethodDecl* MD, const clang::ObjCInterfaceDecl* I);

  void VisitObjCInterfaceDeclComment(
      const clang::ObjCInterfaceDecl* Decl, const clang::RawComment* Comment,
      const clang::DeclContext* DCxt,
      absl::optional<GraphObserver::NodeId> DCID);

  void VisitRecordDeclComment(const clang::RecordDecl* Decl,
                              const clang::RawComment* Comment,
                              const clang::DeclContext* DCxt,
                              absl::optional<GraphObserver::NodeId> DCID);

  /// \brief Returns whether `Decl` should be indexed.
  bool ShouldIndex(const clang::Decl* Decl);

  GraphObserver::UseKind UseKindFor(const clang::Stmt* stmt) {
    auto i = use_kinds_.find(stmt);
    return i == use_kinds_.end() ? GraphObserver::UseKind::kUnknown : i->second;
  }

  /// \brief Marks that `stmt` was used as a write target.
  /// \return `stmt` as passed.
  const clang::Stmt* UsedAsWrite(const clang::Stmt* stmt) {
    if (stmt != nullptr) use_kinds_[stmt] = GraphObserver::UseKind::kWrite;
    return stmt;
  }

  /// \brief Marks that `stmt` was used as a read+write target.
  /// \return `stmt` as passed.
  const clang::Stmt* UsedAsReadWrite(const clang::Stmt* stmt) {
    if (stmt != nullptr) use_kinds_[stmt] = GraphObserver::UseKind::kReadWrite;
    return stmt;
  }

  /// \brief Maps known Decls to their NodeIds.
  llvm::DenseMap<const clang::Decl*, GraphObserver::NodeId> DeclToNodeId;

  /// \brief Used for calculating semantic hashes.
  SemanticHash Hash{
      [this](const clang::Decl* Decl) {
        return BuildNameIdForDecl(Decl).ToString();
      },
      // These enums are intentionally compatible.
      static_cast<SemanticHash::OnUnimplemented>(IgnoreUnimplemented)};

  /// \brief Enabled library-specific callbacks.
  const LibrarySupports& Supports;

  /// \brief The `Sema` instance to use.
  clang::Sema& Sema;

  /// \brief The cache to use to generate signatures.
  MarkedSourceCache MarkedSources;

  /// \return true if we should stop indexing.
  std::function<bool()> ShouldStopIndexing;

  /// \brief The active indexing job.
  std::unique_ptr<IndexJob> Job;

  /// \brief The controlling worklist.
  std::unique_ptr<IndexerWorklist> Worklist;

  /// \brief Comments we've already visited.
  std::unordered_set<const clang::RawComment*> VisitedComments;

  /// \brief The number of (raw) bytes to use to represent a USR. If 0,
  /// no USRs will be recorded.
  int UsrByteSize = 0;

  /// \brief Controls whether dataflow edges are emitted.
  EmitDataflowEdges DataflowEdges;

  /// \brief if nonempty, the pattern to match a path against to see whether
  /// it should be excluded from template instance indexing.
  std::shared_ptr<re2::RE2> TemplateInstanceExcludePathPattern = nullptr;

  /// \brief AST nodes we know are used in specific ways.
  absl::flat_hash_map<const clang::Stmt*, GraphObserver::UseKind> use_kinds_;

  struct AlternateSemantic {
    GraphObserver::UseKind use_kind;
    std::optional<GraphObserver::NodeId> node;
  };

  /// \return the alternate semantic for `decl` or null.
  AlternateSemantic* AlternateSemanticForDecl(const clang::Decl* decl);

  /// \brief Maps from declaring token (begin, end) locations (as pairs of
  /// encoded clang::SourceLocations) to alternate semantics.
  absl::flat_hash_map<std::pair<unsigned, unsigned>, AlternateSemantic>
      alternate_semantic_cache_;

  /// \brief Decls known to have alternate semantics.
  absl::flat_hash_map<const clang::Decl*, AlternateSemantic*>
      alternate_semantics_;
};

/// \brief An `ASTConsumer` that passes events to a `GraphObserver`.
class IndexerASTConsumer : public clang::SemaConsumer {
 public:
  explicit IndexerASTConsumer(
      GraphObserver* GO, BehaviorOnUnimplemented B, BehaviorOnTemplates T,
      Verbosity V, BehaviorOnFwdDeclComments ObjC,
      BehaviorOnFwdDeclComments Cpp, const LibrarySupports& S,
      std::function<bool()> ShouldStopIndexing,
      std::function<std::unique_ptr<IndexerWorklist>(IndexerASTVisitor*)>
          CreateWorklist,
      int UsrByteSize, EmitDataflowEdges EDE, std::shared_ptr<re2::RE2> TIEPP)
      : Observer(GO),
        IgnoreUnimplemented(B),
        TemplateMode(T),
        Verbosity(V),
        ObjCFwdDocs(ObjC),
        CppFwdDocs(Cpp),
        Supports(S),
        ShouldStopIndexing(std::move(ShouldStopIndexing)),
        CreateWorklist(std::move(CreateWorklist)),
        UsrByteSize(UsrByteSize),
        DataflowEdges(EDE),
        TemplateInstanceExcludePathPattern(TIEPP) {}

  void HandleTranslationUnit(clang::ASTContext& Context) override {
    CHECK(Sema != nullptr);
    IndexerASTVisitor Visitor(
        Context, IgnoreUnimplemented, TemplateMode, Verbosity, ObjCFwdDocs,
        CppFwdDocs, Supports, *Sema, ShouldStopIndexing, Observer, UsrByteSize,
        DataflowEdges, TemplateInstanceExcludePathPattern);
    {
      ProfileBlock block(Observer->getProfilingCallback(), "traverse_tu");
      Visitor.PrepareAlternateSemanticCache();
      Visitor.Work(Context.getTranslationUnitDecl(), CreateWorklist(&Visitor));
    }
  }

  void InitializeSema(clang::Sema& S) override { Sema = &S; }

  void ForgetSema() override { Sema = nullptr; }

 private:
  GraphObserver* const Observer;
  /// Whether we should stop on missing cases or continue on.
  BehaviorOnUnimplemented IgnoreUnimplemented;
  /// Whether we should visit template instantiations.
  BehaviorOnTemplates TemplateMode;
  /// Whether we should emit all data.
  enum Verbosity Verbosity;
  /// Should we emit documentation for forward class decls in ObjC?
  BehaviorOnFwdDeclComments ObjCFwdDocs;
  /// Should we emit documentation for forward decls in C++?
  BehaviorOnFwdDeclComments CppFwdDocs;
  /// Which library supports are enabled.
  const LibrarySupports& Supports;
  /// The active Sema instance.
  clang::Sema* Sema;
  /// \return true if we should stop indexing.
  std::function<bool()> ShouldStopIndexing;
  /// \return a new worklist for the given visitor.
  std::function<std::unique_ptr<IndexerWorklist>(IndexerASTVisitor*)>
      CreateWorklist;
  /// \brief The number of (raw) bytes to use to represent a USR. If 0,
  /// no USRs will be recorded.
  int UsrByteSize = 0;
  /// \brief Controls whether dataflow edges are emitted.
  EmitDataflowEdges DataflowEdges;
  /// \brief if nonempty, the pattern to match a path against to see whether
  /// it should be excluded from template instance indexing.
  std::shared_ptr<re2::RE2> TemplateInstanceExcludePathPattern;
};

}  // namespace kythe

#endif  // KYTHE_CXX_INDEXER_CXX_INDEXER_AST_HOOKS_H_