1 #include "indexer.h"
2 
3 #include "clang_cursor.h"
4 #include "clang_utils.h"
5 #include "platform.h"
6 #include "serializer.h"
7 #include "timer.h"
8 #include "type_printer.h"
9 
10 #include <loguru.hpp>
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <chrono>
15 #include <climits>
16 #include <iostream>
17 
18 // TODO: See if we can use clang_indexLoc_getFileLocation to get a type ref on
19 // |Foobar| in DISALLOW_COPY(Foobar)
20 
21 #if CINDEX_VERSION >= 47
22 #define CINDEX_HAVE_PRETTY 1
23 #endif
24 #if CINDEX_VERSION >= 48
25 #define CINDEX_HAVE_ROLE 1
26 #endif
27 
28 namespace {
29 
30 // For typedef/using spanning less than or equal to (this number) of lines,
31 // display their declarations on hover.
32 constexpr int kMaxLinesDisplayTypeAliasDeclarations = 3;
33 
34 // TODO How to check if a reference to type is a declaration?
35 // This currently also includes constructors/destructors.
36 // It seems declarations in functions are not indexed.
IsDeclContext(CXIdxEntityKind kind)37 bool IsDeclContext(CXIdxEntityKind kind) {
38   switch (kind) {
39     case CXIdxEntity_CXXClass:
40     case CXIdxEntity_CXXNamespace:
41     case CXIdxEntity_ObjCCategory:
42     case CXIdxEntity_ObjCClass:
43     case CXIdxEntity_ObjCProtocol:
44     case CXIdxEntity_Struct:
45       return true;
46     default:
47       return false;
48   }
49 }
50 
GetRole(const CXIdxEntityRefInfo * ref_info,Role role)51 Role GetRole(const CXIdxEntityRefInfo* ref_info, Role role) {
52 #if CINDEX_HAVE_ROLE
53   return static_cast<Role>(static_cast<int>(ref_info->role));
54 #else
55   return role;
56 #endif
57 }
58 
GetSymbolKind(CXCursorKind kind)59 SymbolKind GetSymbolKind(CXCursorKind kind) {
60   switch (kind) {
61     case CXCursor_TranslationUnit:
62       return SymbolKind::File;
63 
64     case CXCursor_FunctionDecl:
65     case CXCursor_CXXMethod:
66     case CXCursor_Constructor:
67     case CXCursor_Destructor:
68     case CXCursor_ConversionFunction:
69     case CXCursor_FunctionTemplate:
70     case CXCursor_OverloadedDeclRef:
71     case CXCursor_LambdaExpr:
72     case CXCursor_ObjCInstanceMethodDecl:
73     case CXCursor_ObjCClassMethodDecl:
74       return SymbolKind::Func;
75 
76     case CXCursor_StructDecl:
77     case CXCursor_UnionDecl:
78     case CXCursor_ClassDecl:
79     case CXCursor_EnumDecl:
80     case CXCursor_ObjCInterfaceDecl:
81     case CXCursor_ObjCCategoryDecl:
82     case CXCursor_ObjCImplementationDecl:
83     case CXCursor_Namespace:
84       return SymbolKind::Type;
85 
86     default:
87       return SymbolKind::Invalid;
88   }
89 }
90 
91 // Inverse of libclang/CXIndexDataConsumer.cpp getEntityKindFromSymbolKind
GetSymbolKind(CXIdxEntityKind kind)92 lsSymbolKind GetSymbolKind(CXIdxEntityKind kind) {
93   switch (kind) {
94     case CXIdxEntity_Unexposed:
95       return lsSymbolKind::Unknown;
96     case CXIdxEntity_Typedef:
97       return lsSymbolKind::TypeAlias;
98     case CXIdxEntity_Function:
99       return lsSymbolKind::Function;
100     case CXIdxEntity_Variable:
101       // Can also be Parameter
102       return lsSymbolKind::Variable;
103     case CXIdxEntity_Field:
104       return lsSymbolKind::Field;
105     case CXIdxEntity_EnumConstant:
106       return lsSymbolKind::EnumMember;
107 
108     case CXIdxEntity_ObjCClass:
109       return lsSymbolKind::Class;
110     case CXIdxEntity_ObjCProtocol:
111       return lsSymbolKind::Interface;
112     case CXIdxEntity_ObjCCategory:
113       return lsSymbolKind::Interface;
114 
115     case CXIdxEntity_ObjCInstanceMethod:
116       return lsSymbolKind::Method;
117     case CXIdxEntity_ObjCClassMethod:
118       return lsSymbolKind::StaticMethod;
119     case CXIdxEntity_ObjCProperty:
120       return lsSymbolKind::Property;
121     case CXIdxEntity_ObjCIvar:
122       return lsSymbolKind::Field;
123 
124     case CXIdxEntity_Enum:
125       return lsSymbolKind::Enum;
126     case CXIdxEntity_Struct:
127     case CXIdxEntity_Union:
128       return lsSymbolKind::Struct;
129 
130     case CXIdxEntity_CXXClass:
131       return lsSymbolKind::Class;
132     case CXIdxEntity_CXXNamespace:
133       return lsSymbolKind::Namespace;
134     case CXIdxEntity_CXXNamespaceAlias:
135       return lsSymbolKind::Namespace;
136     case CXIdxEntity_CXXStaticVariable:
137       return lsSymbolKind::Field;
138     case CXIdxEntity_CXXStaticMethod:
139       return lsSymbolKind::StaticMethod;
140     case CXIdxEntity_CXXInstanceMethod:
141       return lsSymbolKind::Method;
142     case CXIdxEntity_CXXConstructor:
143       return lsSymbolKind::Constructor;
144     case CXIdxEntity_CXXDestructor:
145       return lsSymbolKind::Method;
146     case CXIdxEntity_CXXConversionFunction:
147       return lsSymbolKind::Constructor;
148     case CXIdxEntity_CXXTypeAlias:
149       return lsSymbolKind::TypeAlias;
150     case CXIdxEntity_CXXInterface:
151       return lsSymbolKind::Struct;
152   }
153 
154   return lsSymbolKind::Unknown;
155 }
156 
GetStorageClass(CX_StorageClass storage)157 StorageClass GetStorageClass(CX_StorageClass storage) {
158   switch (storage) {
159     case CX_SC_Invalid:
160     case CX_SC_OpenCLWorkGroupLocal:
161       return StorageClass::Invalid;
162     case CX_SC_None:
163       return StorageClass::None;
164     case CX_SC_Extern:
165       return StorageClass::Extern;
166     case CX_SC_Static:
167       return StorageClass::Static;
168     case CX_SC_PrivateExtern:
169       return StorageClass::PrivateExtern;
170     case CX_SC_Auto:
171       return StorageClass::Auto;
172     case CX_SC_Register:
173       return StorageClass::Register;
174   }
175 
176   return StorageClass::None;
177 }
178 
179 // Caches all instances of constructors, regardless if they are indexed or not.
180 // The constructor may have a make_unique call associated with it that we need
181 // to export. If we do not capture the parameter type description for the
182 // constructor we will not be able to attribute the constructor call correctly.
183 struct ConstructorCache {
184   struct Constructor {
185     Usr usr;
186     std::vector<std::string> param_type_desc;
187   };
188   std::unordered_map<Usr, std::vector<Constructor>> constructors_;
189 
190   // This should be called whenever there is a constructor declaration.
NotifyConstructor__anon89644b3f0111::ConstructorCache191   void NotifyConstructor(ClangCursor ctor_cursor) {
192     auto build_type_desc = [](ClangCursor cursor) {
193       std::vector<std::string> type_desc;
194       for (ClangCursor arg : cursor.get_arguments()) {
195         if (arg.get_kind() == CXCursor_ParmDecl)
196           type_desc.push_back(arg.get_type_description());
197       }
198       return type_desc;
199     };
200 
201     Constructor ctor{ctor_cursor.get_usr_hash(), build_type_desc(ctor_cursor)};
202 
203     // Insert into |constructors_|.
204     auto type_usr_hash = ctor_cursor.get_semantic_parent().get_usr_hash();
205     auto existing_ctors = constructors_.find(type_usr_hash);
206     if (existing_ctors != constructors_.end()) {
207       existing_ctors->second.push_back(ctor);
208     } else {
209       constructors_[type_usr_hash] = {ctor};
210     }
211   }
212 
213   // Tries to lookup a constructor in |type_usr| that takes arguments most
214   // closely aligned to |param_type_desc|.
TryFindConstructorUsr__anon89644b3f0111::ConstructorCache215   optional<Usr> TryFindConstructorUsr(
216       Usr type_usr,
217       const std::vector<std::string>& param_type_desc) {
218     auto count_matching_prefix_length = [](const char* a, const char* b) {
219       int matched = 0;
220       while (*a && *b) {
221         if (*a != *b)
222           break;
223         ++a;
224         ++b;
225         ++matched;
226       }
227       // Additional score if the strings were the same length, which makes
228       // "a"/"a" match higher than "a"/"a&"
229       if (*a == *b)
230         matched += 1;
231       return matched;
232     };
233 
234     // Try to find constructors for the type. If there are no constructors
235     // available, return an empty result.
236     auto ctors_it = constructors_.find(type_usr);
237     if (ctors_it == constructors_.end())
238       return nullopt;
239     const std::vector<Constructor>& ctors = ctors_it->second;
240     if (ctors.empty())
241       return nullopt;
242 
243     Usr best_usr = ctors[0].usr;
244     int best_score = INT_MIN;
245 
246     // Scan constructors for the best possible match.
247     for (const Constructor& ctor : ctors) {
248       // If |param_type_desc| is empty and the constructor is as well, we don't
249       // need to bother searching, as this is the match.
250       if (param_type_desc.empty() && ctor.param_type_desc.empty()) {
251         best_usr = ctor.usr;
252         break;
253       }
254 
255       // Weight matching parameter length heavily, as it is more accurate than
256       // the fuzzy type matching approach.
257       int score = 0;
258       if (param_type_desc.size() == ctor.param_type_desc.size())
259         score += param_type_desc.size() * 1000;
260 
261       // Do prefix-based match on parameter type description. This works well in
262       // practice because clang appends qualifiers to the end of the type, ie,
263       // |foo *&&|
264       for (size_t i = 0;
265            i < std::min(param_type_desc.size(), ctor.param_type_desc.size());
266            ++i) {
267         score += count_matching_prefix_length(param_type_desc[i].c_str(),
268                                               ctor.param_type_desc[i].c_str());
269       }
270 
271       if (score > best_score) {
272         best_usr = ctor.usr;
273         best_score = score;
274       }
275     }
276 
277     return best_usr;
278   }
279 };
280 
281 struct IndexParam {
282   std::unordered_set<CXFile> seen_cx_files;
283   std::vector<AbsolutePath> seen_files;
284   FileContentsMap file_contents;
285   std::unordered_map<AbsolutePath, int64_t> file_modification_times;
286 
287   // Only use this when strictly needed (ie, primary translation unit is
288   // needed). Most logic should get the IndexFile instance via
289   // |file_consumer|.
290   //
291   // This can be null if we're not generating an index for the primary
292   // translation unit.
293   IndexFile* primary_file = nullptr;
294 
295   ClangTranslationUnit* tu = nullptr;
296 
297   FileConsumer* file_consumer = nullptr;
298   NamespaceHelper ns;
299   ConstructorCache ctors;
300 
IndexParam__anon89644b3f0111::IndexParam301   IndexParam(ClangTranslationUnit* tu, FileConsumer* file_consumer)
302       : tu(tu), file_consumer(file_consumer) {}
303 
304 #if CINDEX_HAVE_PRETTY
305   CXPrintingPolicy print_policy = nullptr;
306   CXPrintingPolicy print_policy_more = nullptr;
~IndexParam__anon89644b3f0111::IndexParam307   ~IndexParam() {
308     clang_PrintingPolicy_dispose(print_policy);
309     clang_PrintingPolicy_dispose(print_policy_more);
310   }
311 
PrettyPrintCursor__anon89644b3f0111::IndexParam312   std::string PrettyPrintCursor(CXCursor cursor, bool initializer = true) {
313     if (!print_policy) {
314       print_policy = clang_getCursorPrintingPolicy(cursor);
315       clang_PrintingPolicy_setProperty(print_policy,
316                                        CXPrintingPolicy_TerseOutput, 1);
317       clang_PrintingPolicy_setProperty(print_policy,
318                                        CXPrintingPolicy_FullyQualifiedName, 1);
319       clang_PrintingPolicy_setProperty(
320           print_policy, CXPrintingPolicy_SuppressInitializers, 1);
321       print_policy_more = clang_getCursorPrintingPolicy(cursor);
322       clang_PrintingPolicy_setProperty(print_policy_more,
323                                        CXPrintingPolicy_FullyQualifiedName, 1);
324       clang_PrintingPolicy_setProperty(print_policy_more,
325                                        CXPrintingPolicy_TerseOutput, 1);
326     }
327     return ToString(clang_getCursorPrettyPrinted(
328         cursor, initializer ? print_policy_more : print_policy));
329   }
330 #endif
331 };
332 
ConsumeFile(IndexParam * param,CXFile file)333 IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
334   bool is_first_ownership = false;
335   IndexFile* db = param->file_consumer->TryConsumeFile(
336       file, &is_first_ownership, &param->file_contents);
337 
338   // If this is the first time we have seen the file (ignoring if we are
339   // generating an index for it):
340   if (param->seen_cx_files.insert(file).second) {
341     optional<AbsolutePath> file_name = FileName(file);
342     // file_name may be empty when it contains .. and is outside of WorkingDir.
343     // https://reviews.llvm.org/D42893
344     // https://github.com/cquery-project/cquery/issues/413
345     if (file_name && !file_name->path.empty()) {
346       // Add to all files we have seen so we can generate proper dependency
347       // graph.
348       param->seen_files.push_back(*file_name);
349 
350       // Set modification time.
351       optional<int64_t> modification_time = GetLastModificationTime(*file_name);
352       LOG_IF_S(ERROR, !modification_time)
353           << "Failed fetching modification time for " << *file_name;
354       if (modification_time)
355         param->file_modification_times[file_name->path] = *modification_time;
356     }
357   }
358 
359   if (is_first_ownership) {
360     // Report skipped source range list.
361     CXSourceRangeList* skipped = clang_getSkippedRanges(param->tu->cx_tu, file);
362     for (unsigned i = 0; i < skipped->count; ++i) {
363       Range range = ResolveCXSourceRange(skipped->ranges[i]);
364 #if CINDEX_VERSION < 45  // Before clang 6.0.0
365       // clang_getSkippedRanges reports start one token after the '#', move it
366       // back so it starts at the '#'
367       range.start.column -= 1;
368 #endif
369       db->skipped_by_preprocessor.push_back(range);
370     }
371     clang_disposeSourceRangeList(skipped);
372   }
373 
374   return db;
375 }
376 
377 // Returns true if the given entity kind can be called implicitly, ie, without
378 // actually being written in the source code.
CanBeCalledImplicitly(CXIdxEntityKind kind)379 bool CanBeCalledImplicitly(CXIdxEntityKind kind) {
380   switch (kind) {
381     case CXIdxEntity_CXXConstructor:
382     case CXIdxEntity_CXXConversionFunction:
383     case CXIdxEntity_CXXDestructor:
384       return true;
385     default:
386       return false;
387   }
388 }
389 
390 // Returns true if the cursor spelling contains the given string. This is
391 // useful to check for implicit function calls.
CursorSpellingContainsString(CXCursor cursor,CXTranslationUnit cx_tu,std::string_view needle)392 bool CursorSpellingContainsString(CXCursor cursor,
393                                   CXTranslationUnit cx_tu,
394                                   std::string_view needle) {
395   CXSourceRange range = clang_Cursor_getSpellingNameRange(cursor, 0, 0);
396   CXToken* tokens;
397   unsigned num_tokens;
398   clang_tokenize(cx_tu, range, &tokens, &num_tokens);
399 
400   bool result = false;
401 
402   for (unsigned i = 0; i < num_tokens; ++i) {
403     CXString name = clang_getTokenSpelling(cx_tu, tokens[i]);
404     if (needle == clang_getCString(name)) {
405       result = true;
406       break;
407     }
408     clang_disposeString(name);
409   }
410 
411   clang_disposeTokens(cx_tu, tokens, num_tokens);
412   return result;
413 }
414 
415 // Returns the document content for the given range. May not work perfectly
416 // when there are tabs instead of spaces.
GetDocumentContentInRange(CXTranslationUnit cx_tu,CXSourceRange range)417 std::string GetDocumentContentInRange(CXTranslationUnit cx_tu,
418                                       CXSourceRange range) {
419   std::string result;
420 
421   CXToken* tokens;
422   unsigned num_tokens;
423   clang_tokenize(cx_tu, range, &tokens, &num_tokens);
424 
425   optional<Range> previous_token_range;
426 
427   for (unsigned i = 0; i < num_tokens; ++i) {
428     // Add whitespace between the previous token and this one.
429     Range token_range =
430         ResolveCXSourceRange(clang_getTokenExtent(cx_tu, tokens[i]));
431     if (previous_token_range) {
432       // Insert newlines.
433       int16_t line_delta =
434           token_range.start.line - previous_token_range->end.line;
435       assert(line_delta >= 0);
436       if (line_delta > 0) {
437         result.append((size_t)line_delta, '\n');
438         // Reset column so we insert starting padding.
439         previous_token_range->end.column = 0;
440       }
441       // Insert spaces.
442       int16_t column_delta =
443           token_range.start.column - previous_token_range->end.column;
444       assert(column_delta >= 0);
445       result.append((size_t)column_delta, ' ');
446     }
447     previous_token_range = token_range;
448 
449     // Add token content.
450     CXString spelling = clang_getTokenSpelling(cx_tu, tokens[i]);
451     result += clang_getCString(spelling);
452     clang_disposeString(spelling);
453   }
454 
455   clang_disposeTokens(cx_tu, tokens, num_tokens);
456 
457   return result;
458 }
459 
SetUsePreflight(IndexFile * db,ClangCursor parent)460 void SetUsePreflight(IndexFile* db, ClangCursor parent) {
461   switch (GetSymbolKind(parent.get_kind())) {
462     case SymbolKind::Func:
463       (void)db->ToFuncId(parent.cx_cursor);
464       break;
465     case SymbolKind::Type:
466       (void)db->ToTypeId(parent.cx_cursor);
467       break;
468     case SymbolKind::Var:
469       (void)db->ToVarId(parent.cx_cursor);
470       break;
471     default:
472       break;
473   }
474 }
475 
476 // |parent| should be resolved before using |SetUsePreflight| so that |def| will
477 // not be invalidated by |To{Func,Type,Var}Id|.
SetUse(IndexFile * db,Range range,ClangCursor parent,Role role)478 IndexId::LexicalRef SetUse(IndexFile* db,
479                            Range range,
480                            ClangCursor parent,
481                            Role role) {
482   switch (GetSymbolKind(parent.get_kind())) {
483     case SymbolKind::Func:
484       return IndexId::LexicalRef(range, db->ToFuncId(parent.cx_cursor),
485                                  SymbolKind::Func, role);
486     case SymbolKind::Type:
487       return IndexId::LexicalRef(range, db->ToTypeId(parent.cx_cursor),
488                                  SymbolKind::Type, role);
489     case SymbolKind::Var:
490       return IndexId::LexicalRef(range, db->ToVarId(parent.cx_cursor),
491                                  SymbolKind::Var, role);
492     default:
493       return IndexId::LexicalRef(range, AnyId(), SymbolKind::File, role);
494   }
495 }
496 // |parent| should be resolved before using |SetUsePreflight| so that |def| will
497 // not be invalidated by |To{Func,Type,Var}Id|.
SetRef(IndexFile * db,Range range,ClangCursor parent,Role role)498 IndexId::LexicalRef SetRef(IndexFile* db,
499                            Range range,
500                            ClangCursor parent,
501                            Role role) {
502   switch (GetSymbolKind(parent.get_kind())) {
503     case SymbolKind::Func:
504       return IndexId::LexicalRef(range, db->ToFuncId(parent.cx_cursor),
505                                  SymbolKind::Func, role);
506     case SymbolKind::Type:
507       return IndexId::LexicalRef(range, db->ToTypeId(parent.cx_cursor),
508                                  SymbolKind::Type, role);
509     case SymbolKind::Var:
510       return IndexId::LexicalRef(range, db->ToVarId(parent.cx_cursor),
511                                  SymbolKind::Var, role);
512     default:
513       return IndexId::LexicalRef(range, AnyId(), SymbolKind::File, role);
514   }
515 }
516 
GetAnonName(CXCursorKind kind)517 const char* GetAnonName(CXCursorKind kind) {
518   switch (kind) {
519     case CXCursor_ClassDecl:
520       return "(anon class)";
521     case CXCursor_EnumDecl:
522       return "(anon enum)";
523     case CXCursor_StructDecl:
524       return "(anon struct)";
525     case CXCursor_UnionDecl:
526       return "(anon union)";
527     default:
528       return "(anon)";
529   }
530 }
531 
SetTypeName(IndexType * type,const ClangCursor & cursor,const CXIdxContainerInfo * container,const char * name,IndexParam * param)532 void SetTypeName(IndexType* type,
533                  const ClangCursor& cursor,
534                  const CXIdxContainerInfo* container,
535                  const char* name,
536                  IndexParam* param) {
537   CXIdxContainerInfo parent;
538   // |name| can be null in an anonymous struct (see
539   // tests/types/anonymous_struct.cc).
540   if (!name)
541     name = GetAnonName(cursor.get_kind());
542   if (!container)
543     parent.cursor = cursor.get_semantic_parent().cx_cursor;
544   // Investigate why clang_getCursorPrettyPrinted gives `struct A {}` `namespace
545   // ns {}` which are not qualified.
546   // type->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor);
547   type->def.detailed_name =
548       param->ns.QualifiedName(container ? container : &parent, name);
549   auto idx = type->def.detailed_name.rfind(name);
550   assert(idx != std::string::npos);
551   type->def.short_name_offset = idx;
552   type->def.short_name_size = strlen(name);
553 }
554 
555 // Finds the cursor associated with the declaration type of |cursor|. This
556 // strips
557 // qualifies from |cursor| (ie, Foo* => Foo) and removes template arguments
558 // (ie, Foo<A,B> => Foo<*,*>).
ResolveToDeclarationType(IndexFile * db,ClangCursor cursor,IndexParam * param)559 optional<IndexId::Type> ResolveToDeclarationType(IndexFile* db,
560                                                  ClangCursor cursor,
561                                                  IndexParam* param) {
562   ClangType type = cursor.get_type();
563 
564   // auto x = new Foo() will not be deduced to |Foo| if we do not use the
565   // canonical type. However, a canonical type will look past typedefs so we
566   // will not accurately report variables on typedefs if we always do this.
567   if (type.cx_type.kind == CXType_Auto)
568     type = type.get_canonical();
569 
570   type = type.strip_qualifiers();
571 
572   if (type.is_builtin()) {
573     // For builtin types, use type kinds as USR hash.
574     return db->ToTypeId(type.cx_type.kind);
575   }
576 
577   ClangCursor declaration =
578       type.get_declaration().template_specialization_to_template_definition();
579   optional<Usr> usr = declaration.get_opt_usr_hash();
580   if (!usr)
581     return nullopt;
582   IndexId::Type type_id = db->ToTypeId(*usr);
583   IndexType* typ = db->Resolve(type_id);
584   if (typ->def.detailed_name.empty()) {
585     std::string name = declaration.get_spell_name();
586     SetTypeName(typ, declaration, nullptr, name.c_str(), param);
587   }
588   return type_id;
589 }
590 
SetVarDetail(IndexVar * var,std::string_view short_name,const ClangCursor & cursor,const CXIdxContainerInfo * semanticContainer,bool is_first_seen,IndexFile * db,IndexParam * param)591 void SetVarDetail(IndexVar* var,
592                   std::string_view short_name,
593                   const ClangCursor& cursor,
594                   const CXIdxContainerInfo* semanticContainer,
595                   bool is_first_seen,
596                   IndexFile* db,
597                   IndexParam* param) {
598   IndexVar::Def& def = var->def;
599   const CXType cx_type = clang_getCursorType(cursor.cx_cursor);
600   std::string type_name = ToString(clang_getTypeSpelling(cx_type));
601   // clang may report "(lambda at foo.cc)" which end up being a very long
602   // string. Shorten it to just "lambda".
603   if (type_name.find("(lambda at") != std::string::npos)
604     type_name = "lambda";
605   if (g_config->index.comments)
606     def.comments = cursor.get_comments();
607   def.storage = GetStorageClass(clang_Cursor_getStorageClass(cursor.cx_cursor));
608 
609   // TODO how to make PrettyPrint'ed variable name qualified?
610   std::string qualified_name =
611 #if 0 && CINDEX_HAVE_PRETTY
612       cursor.get_kind() != CXCursor_EnumConstantDecl
613           ? param->PrettyPrintCursor(cursor.cx_cursor)
614           :
615 #endif
616       param->ns.QualifiedName(semanticContainer, short_name);
617 
618   if (cursor.get_kind() == CXCursor_EnumConstantDecl && semanticContainer) {
619     CXType enum_type = clang_getCanonicalType(
620         clang_getEnumDeclIntegerType(semanticContainer->cursor));
621     std::string hover = qualified_name + " = ";
622     if (enum_type.kind == CXType_UInt || enum_type.kind == CXType_ULong ||
623         enum_type.kind == CXType_ULongLong)
624       hover += std::to_string(
625           clang_getEnumConstantDeclUnsignedValue(cursor.cx_cursor));
626     else
627       hover += std::to_string(clang_getEnumConstantDeclValue(cursor.cx_cursor));
628     def.detailed_name = std::move(qualified_name);
629     def.hover = hover;
630   } else {
631 #if 0 && CINDEX_HAVE_PRETTY
632     //def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor, false);
633 #else
634     ConcatTypeAndName(type_name, qualified_name);
635     def.detailed_name = type_name;
636     // Append the textual initializer, bit field, constructor to |hover|.
637     // Omit |hover| for these types:
638     // int (*a)(); int (&a)(); int (&&a)(); int a[1]; auto x = ...
639     // We can take these into consideration after we have better support for
640     // inside-out syntax.
641     CXType deref = cx_type;
642     while (deref.kind == CXType_Pointer || deref.kind == CXType_MemberPointer ||
643            deref.kind == CXType_LValueReference ||
644            deref.kind == CXType_RValueReference)
645       deref = clang_getPointeeType(deref);
646     if (deref.kind != CXType_Unexposed && deref.kind != CXType_Auto &&
647         clang_getResultType(deref).kind == CXType_Invalid &&
648         clang_getElementType(deref).kind == CXType_Invalid) {
649       const FileContents& fc = param->file_contents[db->path];
650       optional<int> spell_end = fc.ToOffset(cursor.get_spell().end);
651       optional<int> extent_end = fc.ToOffset(cursor.get_extent().end);
652       if (extent_end && *spell_end < *extent_end)
653         def.hover = std::string(def.detailed_name.c_str()) +
654                     fc.content.substr(*spell_end, *extent_end - *spell_end);
655     }
656 #endif
657   }
658   // FIXME QualifiedName should return index
659   auto idx = def.detailed_name.rfind(short_name.begin(), std::string::npos,
660                                      short_name.size());
661   assert(idx != std::string::npos);
662   def.short_name_offset = idx;
663   def.short_name_size = short_name.size();
664 
665   if (is_first_seen) {
666     optional<IndexId::Type> var_type =
667         ResolveToDeclarationType(db, cursor, param);
668     if (var_type) {
669       // Don't treat enum definition variables as instantiations.
670       bool is_enum_member = semanticContainer &&
671                             semanticContainer->cursor.kind == CXCursor_EnumDecl;
672       if (!is_enum_member)
673         db->Resolve(var_type.value())->instances.push_back(var->id);
674 
675       def.type = *var_type;
676     }
677   }
678 }
679 
OnIndexReference_Function(IndexFile * db,Range loc,ClangCursor parent_cursor,IndexId::Func called_id,Role role)680 void OnIndexReference_Function(IndexFile* db,
681                                Range loc,
682                                ClangCursor parent_cursor,
683                                IndexId::Func called_id,
684                                Role role) {
685   switch (GetSymbolKind(parent_cursor.get_kind())) {
686     case SymbolKind::Func: {
687       IndexFunc* parent = db->Resolve(db->ToFuncId(parent_cursor.cx_cursor));
688       IndexFunc* called = db->Resolve(called_id);
689       parent->def.callees.push_back(
690           IndexId::SymbolRef(loc, called->id, SymbolKind::Func, role));
691       called->uses.push_back(
692           IndexId::LexicalRef(loc, parent->id, SymbolKind::Func, role));
693       break;
694     }
695     case SymbolKind::Type: {
696       IndexType* parent = db->Resolve(db->ToTypeId(parent_cursor.cx_cursor));
697       IndexFunc* called = db->Resolve(called_id);
698       called = db->Resolve(called_id);
699       called->uses.push_back(
700           IndexId::LexicalRef(loc, parent->id, SymbolKind::Type, role));
701       break;
702     }
703     default: {
704       IndexFunc* called = db->Resolve(called_id);
705       called->uses.push_back(
706           IndexId::LexicalRef(loc, AnyId(), SymbolKind::File, role));
707       break;
708     }
709   }
710 }
711 
712 template <typename T>
Uniquify(std::vector<Id<T>> & ids)713 void Uniquify(std::vector<Id<T>>& ids) {
714   std::unordered_set<Id<T>> seen;
715   size_t n = 0;
716   for (size_t i = 0; i < ids.size(); i++)
717     if (seen.insert(ids[i]).second)
718       ids[n++] = ids[i];
719   ids.resize(n);
720 }
721 
Uniquify(std::vector<IndexId::LexicalRef> & refs)722 void Uniquify(std::vector<IndexId::LexicalRef>& refs) {
723   std::unordered_set<Range> seen;
724   size_t n = 0;
725   for (size_t i = 0; i < refs.size(); i++)
726     if (seen.insert(refs[i].range).second)
727       refs[n++] = refs[i];
728   refs.resize(n);
729 }
730 
731 }  // namespace
732 
733 // static
734 const int IndexFile::kMajorVersion = 15;
735 // static
736 const int IndexFile::kMinorVersion = 0;
737 
IndexFile(const AbsolutePath & path,const std::string & contents)738 IndexFile::IndexFile(const AbsolutePath& path, const std::string& contents)
739     : id_cache(path), path(path), file_contents(contents) {}
740 
ToTypeId(Usr usr)741 IndexId::Type IndexFile::ToTypeId(Usr usr) {
742   auto it = id_cache.usr_to_type_id.find(usr);
743   if (it != id_cache.usr_to_type_id.end())
744     return it->second;
745 
746   IndexId::Type id(types.size());
747   types.push_back(IndexType(id, usr));
748   id_cache.usr_to_type_id[usr] = id;
749   id_cache.type_id_to_usr[id] = usr;
750   return id;
751 }
ToFuncId(Usr usr)752 IndexId::Func IndexFile::ToFuncId(Usr usr) {
753   auto it = id_cache.usr_to_func_id.find(usr);
754   if (it != id_cache.usr_to_func_id.end())
755     return it->second;
756 
757   IndexId::Func id(funcs.size());
758   funcs.push_back(IndexFunc(id, usr));
759   id_cache.usr_to_func_id[usr] = id;
760   id_cache.func_id_to_usr[id] = usr;
761   return id;
762 }
ToVarId(Usr usr)763 IndexId::Var IndexFile::ToVarId(Usr usr) {
764   auto it = id_cache.usr_to_var_id.find(usr);
765   if (it != id_cache.usr_to_var_id.end())
766     return it->second;
767 
768   IndexId::Var id(vars.size());
769   vars.push_back(IndexVar(id, usr));
770   id_cache.usr_to_var_id[usr] = id;
771   id_cache.var_id_to_usr[id] = usr;
772   return id;
773 }
774 
ToTypeId(const CXCursor & cursor)775 IndexId::Type IndexFile::ToTypeId(const CXCursor& cursor) {
776   return ToTypeId(ClangCursor(cursor).get_usr_hash());
777 }
778 
ToFuncId(const CXCursor & cursor)779 IndexId::Func IndexFile::ToFuncId(const CXCursor& cursor) {
780   return ToFuncId(ClangCursor(cursor).get_usr_hash());
781 }
782 
ToVarId(const CXCursor & cursor)783 IndexId::Var IndexFile::ToVarId(const CXCursor& cursor) {
784   return ToVarId(ClangCursor(cursor).get_usr_hash());
785 }
786 
Resolve(IndexId::Type id)787 IndexType* IndexFile::Resolve(IndexId::Type id) {
788   return &types[id.id];
789 }
Resolve(IndexId::Func id)790 IndexFunc* IndexFile::Resolve(IndexId::Func id) {
791   return &funcs[id.id];
792 }
Resolve(IndexId::Var id)793 IndexVar* IndexFile::Resolve(IndexId::Var id) {
794   return &vars[id.id];
795 }
796 
ToString()797 std::string IndexFile::ToString() {
798   return Serialize(SerializeFormat::Json, *this);
799 }
800 
IndexType(IndexId::Type id,Usr usr)801 IndexType::IndexType(IndexId::Type id, Usr usr) : usr(usr), id(id) {}
802 
AddRef(IndexFile * db,std::vector<IndexId::LexicalRef> & refs,Range range,ClangCursor parent,Role role=Role::Reference)803 void AddRef(IndexFile* db,
804             std::vector<IndexId::LexicalRef>& refs,
805             Range range,
806             ClangCursor parent,
807             Role role = Role::Reference) {
808   switch (GetSymbolKind(parent.get_kind())) {
809     case SymbolKind::Func:
810       refs.push_back(IndexId::LexicalRef(range, db->ToFuncId(parent.cx_cursor),
811                                          SymbolKind::Func, role));
812       break;
813     case SymbolKind::Type:
814       refs.push_back(IndexId::LexicalRef(range, db->ToTypeId(parent.cx_cursor),
815                                          SymbolKind::Type, role));
816       break;
817     default:
818       refs.push_back(
819           IndexId::LexicalRef(range, AnyId(), SymbolKind::File, role));
820       break;
821   }
822 }
823 
AddRefSpell(IndexFile * db,std::vector<IndexId::LexicalRef> & refs,ClangCursor cursor)824 void AddRefSpell(IndexFile* db,
825                  std::vector<IndexId::LexicalRef>& refs,
826                  ClangCursor cursor) {
827   AddRef(db, refs, cursor.get_spell(), cursor.get_lexical_parent().cx_cursor);
828 }
829 
FromContainer(const CXIdxContainerInfo * parent)830 CXCursor FromContainer(const CXIdxContainerInfo* parent) {
831   return parent ? parent->cursor : clang_getNullCursor();
832 }
833 
IdCache(const AbsolutePath & primary_file)834 IdCache::IdCache(const AbsolutePath& primary_file)
835     : primary_file(primary_file) {}
836 
OnIndexDiagnostic(CXClientData client_data,CXDiagnosticSet diagnostics,void * reserved)837 void OnIndexDiagnostic(CXClientData client_data,
838                        CXDiagnosticSet diagnostics,
839                        void* reserved) {
840   IndexParam* param = static_cast<IndexParam*>(client_data);
841 
842   for (unsigned i = 0; i < clang_getNumDiagnosticsInSet(diagnostics); ++i) {
843     CXDiagnostic diagnostic = clang_getDiagnosticInSet(diagnostics, i);
844 
845     CXSourceLocation diag_loc = clang_getDiagnosticLocation(diagnostic);
846     // Skip diagnostics in system headers.
847     // if (clang_Location_isInSystemHeader(diag_loc))
848     //   continue;
849 
850     // Get db so we can attribute diagnostic to the right indexed file.
851     CXFile file;
852     unsigned int line, column;
853     clang_getSpellingLocation(diag_loc, &file, &line, &column, nullptr);
854     // Skip empty diagnostic.
855     if (!line && !column)
856       continue;
857     IndexFile* db = ConsumeFile(param, file);
858     if (!db)
859       continue;
860 
861     // Build diagnostic.
862     optional<lsDiagnostic> ls_diagnostic =
863         BuildAndDisposeDiagnostic(diagnostic, db->path);
864     // Check to see if we have already reported this diagnostic, as sometimes
865     // libclang will report the same diagnostic twice. See
866     // https://github.com/cquery-project/cquery/issues/594 for a repro.
867     if (ls_diagnostic && !ContainsValue(db->diagnostics_, *ls_diagnostic))
868       db->diagnostics_.push_back(*ls_diagnostic);
869   }
870 }
871 
OnIndexIncludedFile(CXClientData client_data,const CXIdxIncludedFileInfo * file)872 CXIdxClientFile OnIndexIncludedFile(CXClientData client_data,
873                                     const CXIdxIncludedFileInfo* file) {
874   IndexParam* param = static_cast<IndexParam*>(client_data);
875 
876   // file->hashLoc only has the position of the hash. We don't have the full
877   // range for the include.
878   CXSourceLocation hash_loc = clang_indexLoc_getCXSourceLocation(file->hashLoc);
879   CXFile cx_file;
880   unsigned int line;
881   clang_getSpellingLocation(hash_loc, &cx_file, &line, nullptr, nullptr);
882   line--;
883 
884   IndexFile* db = ConsumeFile(param, cx_file);
885   if (!db)
886     return nullptr;
887 
888   optional<AbsolutePath> include_path = FileName(file->file);
889   if (!include_path)
890     return nullptr;
891 
892   IndexInclude include;
893   include.line = line;
894   include.resolved_path = include_path->path;
895   if (!include.resolved_path.empty())
896     db->includes.push_back(include);
897 
898   return nullptr;
899 }
900 
DumpVisitor(ClangCursor cursor,ClangCursor parent,int * level)901 ClangCursor::VisitResult DumpVisitor(ClangCursor cursor,
902                                      ClangCursor parent,
903                                      int* level) {
904   for (int i = 0; i < *level; ++i)
905     std::cerr << "  ";
906   std::cerr << ToString(cursor.get_kind()) << " " << cursor.get_spell_name()
907             << std::endl;
908 
909   *level += 1;
910   cursor.VisitChildren(&DumpVisitor, level);
911   *level -= 1;
912 
913   return ClangCursor::VisitResult::Continue;
914 }
915 
Dump(ClangCursor cursor)916 void Dump(ClangCursor cursor) {
917   int level = 0;
918   cursor.VisitChildren(&DumpVisitor, &level);
919 }
920 
921 struct FindChildOfKindParam {
922   CXCursorKind target_kind;
923   optional<ClangCursor> result;
924 
FindChildOfKindParamFindChildOfKindParam925   FindChildOfKindParam(CXCursorKind target_kind) : target_kind(target_kind) {}
926 };
927 
FindTypeVisitor(ClangCursor cursor,ClangCursor parent,optional<ClangCursor> * result)928 ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor,
929                                          ClangCursor parent,
930                                          optional<ClangCursor>* result) {
931   switch (cursor.get_kind()) {
932     case CXCursor_TypeRef:
933     case CXCursor_TemplateRef:
934       *result = cursor;
935       return ClangCursor::VisitResult::Break;
936     default:
937       break;
938   }
939 
940   return ClangCursor::VisitResult::Recurse;
941 }
942 
FindType(ClangCursor cursor)943 optional<ClangCursor> FindType(ClangCursor cursor) {
944   optional<ClangCursor> result;
945   cursor.VisitChildren(&FindTypeVisitor, &result);
946   return result;
947 }
948 
IsTypeDefinition(const CXIdxContainerInfo * container)949 bool IsTypeDefinition(const CXIdxContainerInfo* container) {
950   if (!container)
951     return false;
952   return GetSymbolKind(container->cursor.kind) == SymbolKind::Type;
953 }
954 
955 struct VisitDeclForTypeUsageParam {
956   IndexFile* db;
957   optional<IndexId::Type> toplevel_type;
958   int has_processed_any = false;
959   optional<ClangCursor> previous_cursor;
960   optional<IndexId::Type> initial_type;
961 
VisitDeclForTypeUsageParamVisitDeclForTypeUsageParam962   VisitDeclForTypeUsageParam(IndexFile* db,
963                              optional<IndexId::Type> toplevel_type)
964       : db(db), toplevel_type(toplevel_type) {}
965 };
966 
VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor,VisitDeclForTypeUsageParam * param)967 void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor,
968                                          VisitDeclForTypeUsageParam* param) {
969   param->has_processed_any = true;
970   IndexFile* db = param->db;
971 
972   // For |A<int> a| where there is a specialization for |A<int>|,
973   // the |referenced_usr| below resolves to the primary template and
974   // attributes the use to the primary template instead of the specialization.
975   // |toplevel_type| is retrieved |clang_getCursorType| which can be a
976   // specialization. If its name is the same as the primary template's, we
977   // assume the use should be attributed to the specialization. This heuristic
978   // fails when a member class bears the same name with its container.
979   //
980   // template<class T>
981   // struct C { struct C {}; };
982   // C<int>::C a;
983   //
984   // We will attribute |::C| to the parent class.
985   if (param->toplevel_type) {
986     IndexType* ref_type = db->Resolve(*param->toplevel_type);
987     std::string name = cursor.get_referenced().get_spell_name();
988     if (name == ref_type->def.ShortName()) {
989       AddRefSpell(db, ref_type->uses, cursor);
990       param->toplevel_type = nullopt;
991       return;
992     }
993   }
994 
995   optional<Usr> referenced_usr =
996       cursor.get_referenced()
997           .template_specialization_to_template_definition()
998           .get_opt_usr_hash();
999   // May be empty, happens in STL.
1000   if (!referenced_usr)
1001     return;
1002 
1003   IndexId::Type ref_type_id = db->ToTypeId(*referenced_usr);
1004 
1005   if (!param->initial_type)
1006     param->initial_type = ref_type_id;
1007 
1008   IndexType* ref_type_def = db->Resolve(ref_type_id);
1009   // TODO: Should we even be visiting this if the file is not from the main
1010   // def? Try adding assert on |loc| later.
1011   AddRefSpell(db, ref_type_def->uses, cursor);
1012 }
1013 
VisitDeclForTypeUsageVisitor(ClangCursor cursor,ClangCursor parent,VisitDeclForTypeUsageParam * param)1014 ClangCursor::VisitResult VisitDeclForTypeUsageVisitor(
1015     ClangCursor cursor,
1016     ClangCursor parent,
1017     VisitDeclForTypeUsageParam* param) {
1018   switch (cursor.get_kind()) {
1019     case CXCursor_TemplateRef:
1020     case CXCursor_TypeRef:
1021       if (param->previous_cursor) {
1022         VisitDeclForTypeUsageVisitorHandler(param->previous_cursor.value(),
1023                                             param);
1024       }
1025 
1026       param->previous_cursor = cursor;
1027       return ClangCursor::VisitResult::Continue;
1028 
1029     // We do not want to recurse for everything, since if we do that we will end
1030     // up visiting method definition bodies/etc. Instead, we only recurse for
1031     // things that can logically appear as part of an inline variable
1032     // initializer,
1033     // ie,
1034     //
1035     //  class Foo {
1036     //   int x = (Foo)3;
1037     //  }
1038     case CXCursor_CallExpr:
1039     case CXCursor_CStyleCastExpr:
1040     case CXCursor_CXXStaticCastExpr:
1041     case CXCursor_CXXReinterpretCastExpr:
1042       return ClangCursor::VisitResult::Recurse;
1043 
1044     default:
1045       return ClangCursor::VisitResult::Continue;
1046   }
1047 
1048   return ClangCursor::VisitResult::Continue;
1049 }
1050 
1051 // Add usages to any seen TypeRef or TemplateRef under the given |decl_cursor|.
1052 // This returns the first seen TypeRef or TemplateRef value, which can be
1053 // useful if trying to figure out ie, what a using statement refers to. If
1054 // trying to generally resolve a cursor to a type, use
1055 // ResolveToDeclarationType, which works in more scenarios.
1056 // If |decl_cursor| is a variable of a template type, clang_getCursorType
1057 // may return a specialized template which is preciser than the primary
1058 // template.
1059 // We use |toplevel_type| to attribute the use to the specialized template
1060 // instead of the primary template.
AddDeclTypeUsages(IndexFile * db,ClangCursor decl_cursor,optional<IndexId::Type> toplevel_type,const CXIdxContainerInfo * semantic_container,const CXIdxContainerInfo * lexical_container)1061 optional<IndexId::Type> AddDeclTypeUsages(
1062     IndexFile* db,
1063     ClangCursor decl_cursor,
1064     optional<IndexId::Type> toplevel_type,
1065     const CXIdxContainerInfo* semantic_container,
1066     const CXIdxContainerInfo* lexical_container) {
1067   //
1068   // The general AST format for definitions follows this pattern:
1069   //
1070   //  template<typename A, typename B>
1071   //  struct Container;
1072   //
1073   //  struct S1;
1074   //  struct S2;
1075   //
1076   //  Container<Container<S1, S2>, S2> foo;
1077   //
1078   //  =>
1079   //
1080   //  VarDecl
1081   //    TemplateRef Container
1082   //    TemplateRef Container
1083   //    TypeRef struct S1
1084   //    TypeRef struct S2
1085   //    TypeRef struct S2
1086   //
1087   //
1088   // Here is another example:
1089   //
1090   //  enum A {};
1091   //  enum B {};
1092   //
1093   //  template<typename T>
1094   //  struct Foo {
1095   //    struct Inner {};
1096   //  };
1097   //
1098   //  Foo<A>::Inner a;
1099   //  Foo<B> b;
1100   //
1101   //  =>
1102   //
1103   //  EnumDecl A
1104   //  EnumDecl B
1105   //  ClassTemplate Foo
1106   //    TemplateTypeParameter T
1107   //    StructDecl Inner
1108   //  VarDecl a
1109   //    TemplateRef Foo
1110   //    TypeRef enum A
1111   //    TypeRef struct Foo<enum A>::Inner
1112   //    CallExpr Inner
1113   //  VarDecl b
1114   //    TemplateRef Foo
1115   //    TypeRef enum B
1116   //    CallExpr Foo
1117   //
1118   //
1119   // Determining the actual type of the variable/declaration from just the
1120   // children is tricky. Doing so would require looking up the template
1121   // definition associated with a TemplateRef, figuring out how many children
1122   // it has, and then skipping that many TypeRef values. This also has to work
1123   // with the example below (skipping the last TypeRef). As a result, we
1124   // determine variable types using |ResolveToDeclarationType|.
1125   //
1126   //
1127   // We skip the last type reference for methods/variables which are defined
1128   // out-of-line w.r.t. the parent type.
1129   //
1130   //  S1* Foo::foo() {}
1131   //
1132   // The above example looks like this in the AST:
1133   //
1134   //  CXXMethod foo
1135   //    TypeRef struct S1
1136   //    TypeRef class Foo
1137   //    CompoundStmt
1138   //      ...
1139   //
1140   //  The second TypeRef is an uninteresting usage.
1141   bool process_last_type_ref = true;
1142   if (IsTypeDefinition(semantic_container) &&
1143       !IsTypeDefinition(lexical_container)) {
1144     //
1145     // In some code, such as the following example, we receive a cursor which is
1146     // not
1147     // a definition and is not associated with a definition due to an error
1148     // condition.
1149     // In this case, it is the Foo::Foo constructor.
1150     //
1151     //  struct Foo {};
1152     //
1153     //  template<class T>
1154     //  Foo::Foo() {}
1155     //
1156     if (!decl_cursor.is_definition()) {
1157       ClangCursor def = decl_cursor.get_definition();
1158       if (def.get_kind() != CXCursor_FirstInvalid)
1159         decl_cursor = def;
1160     }
1161     process_last_type_ref = false;
1162   }
1163 
1164   VisitDeclForTypeUsageParam param(db, toplevel_type);
1165   decl_cursor.VisitChildren(&VisitDeclForTypeUsageVisitor, &param);
1166 
1167   // VisitDeclForTypeUsageVisitor guarantees that if there are multiple TypeRef
1168   // children, the first one will always be visited.
1169   if (param.previous_cursor && process_last_type_ref) {
1170     VisitDeclForTypeUsageVisitorHandler(param.previous_cursor.value(), &param);
1171   } else {
1172     // If we are not processing the last type ref, it *must* be a TypeRef or
1173     // TemplateRef.
1174     //
1175     // We will not visit every child if the is_interseting is false, so
1176     // previous_cursor
1177     // may not point to the last TemplateRef.
1178     assert(param.previous_cursor.has_value() == false ||
1179            (param.previous_cursor.value().get_kind() == CXCursor_TypeRef ||
1180             param.previous_cursor.value().get_kind() == CXCursor_TemplateRef));
1181   }
1182 
1183   if (param.initial_type)
1184     return param.initial_type;
1185   CXType cx_under = clang_getTypedefDeclUnderlyingType(decl_cursor.cx_cursor);
1186   if (cx_under.kind == CXType_Invalid)
1187     return nullopt;
1188   return db->ToTypeId(ClangType(cx_under).strip_qualifiers().get_usr_hash());
1189 }
1190 
1191 // Various versions of LLVM (ie, 4.0) will not visit inline variable references
1192 // for template arguments.
AddDeclInitializerUsagesVisitor(ClangCursor cursor,ClangCursor parent,IndexFile * db)1193 ClangCursor::VisitResult AddDeclInitializerUsagesVisitor(ClangCursor cursor,
1194                                                          ClangCursor parent,
1195                                                          IndexFile* db) {
1196   /*
1197     We need to index the |DeclRefExpr| below (ie, |var| inside of
1198     Foo<int>::var).
1199 
1200       template<typename T>
1201       struct Foo {
1202         static constexpr int var = 3;
1203       };
1204 
1205       int a = Foo<int>::var;
1206 
1207       =>
1208 
1209       VarDecl a
1210         UnexposedExpr var
1211           DeclRefExpr var
1212             TemplateRef Foo
1213 
1214   */
1215 
1216   switch (cursor.get_kind()) {
1217     case CXCursor_DeclRefExpr: {
1218       if (cursor.get_referenced().get_kind() != CXCursor_VarDecl)
1219         break;
1220 
1221       // TODO: when we resolve the template type to the definition, we get a
1222       // different Usr.
1223 
1224       // ClangCursor ref =
1225       // cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr_hash();
1226       // std::string ref_usr =
1227       // cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr_hash();
1228       optional<Usr> ref_usr =
1229           cursor.get_referenced()
1230               .template_specialization_to_template_definition()
1231               .get_usr_hash();
1232       // std::string ref_usr = ref.get_usr_hash();
1233       if (!ref_usr)
1234         break;
1235 
1236       IndexVar* ref_var = db->Resolve(db->ToVarId(*ref_usr));
1237       AddRefSpell(db, ref_var->uses, cursor);
1238       break;
1239     }
1240 
1241     default:
1242       break;
1243   }
1244 
1245   return ClangCursor::VisitResult::Recurse;
1246 }
1247 
VisitMacroDefinitionAndExpansions(ClangCursor cursor,ClangCursor parent,IndexParam * param)1248 ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor,
1249                                                            ClangCursor parent,
1250                                                            IndexParam* param) {
1251   switch (cursor.get_kind()) {
1252     case CXCursor_MacroDefinition:
1253     case CXCursor_MacroExpansion: {
1254       // Resolve location, find IndexFile instance.
1255       CXSourceRange cx_source_range =
1256           clang_Cursor_getSpellingNameRange(cursor.cx_cursor, 0, 0);
1257       CXFile file;
1258       Range decl_loc_spelling = ResolveCXSourceRange(cx_source_range, &file);
1259       IndexFile* db = ConsumeFile(param, file);
1260       if (!db)
1261         break;
1262 
1263       // TODO: Considering checking clang_Cursor_isMacroFunctionLike, but the
1264       // only real difference will be that we show 'callers' instead of 'refs'
1265       // (especially since macros cannot have overrides)
1266 
1267       Usr decl_usr;
1268       if (cursor.get_kind() == CXCursor_MacroDefinition)
1269         decl_usr = cursor.get_usr_hash();
1270       else
1271         decl_usr = cursor.get_referenced().get_usr_hash();
1272 
1273       SetUsePreflight(db, parent);
1274       IndexVar* var_def = db->Resolve(db->ToVarId(decl_usr));
1275       if (cursor.get_kind() == CXCursor_MacroDefinition) {
1276         CXSourceRange cx_extent = clang_getCursorExtent(cursor.cx_cursor);
1277         var_def->def.detailed_name = cursor.get_display_name();
1278         var_def->def.short_name_offset = 0;
1279         var_def->def.short_name_size =
1280             int16_t(strlen(var_def->def.detailed_name.c_str()));
1281         var_def->def.hover =
1282             "#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent);
1283         var_def->def.kind = lsSymbolKind::Macro;
1284         if (g_config->index.comments)
1285           var_def->def.comments = cursor.get_comments();
1286         var_def->def.spell =
1287             SetUse(db, decl_loc_spelling, parent, Role::Definition);
1288         var_def->def.extent = SetUse(
1289             db, ResolveCXSourceRange(cx_extent, nullptr), parent, Role::None);
1290       } else
1291         AddRef(db, var_def->uses, decl_loc_spelling, parent);
1292 
1293       break;
1294     }
1295     default:
1296       break;
1297   }
1298 
1299   return ClangCursor::VisitResult::Continue;
1300 }
1301 
1302 namespace {
1303 
1304 // TODO Move to another file and use clang C++ API
1305 struct TemplateVisitorData {
1306   IndexFile* db;
1307   IndexParam* param;
1308   ClangCursor container;
1309 };
1310 
TemplateVisitor(ClangCursor cursor,ClangCursor parent,TemplateVisitorData * data)1311 ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
1312                                          ClangCursor parent,
1313                                          TemplateVisitorData* data) {
1314   IndexFile* db = data->db;
1315   IndexParam* param = data->param;
1316   switch (cursor.get_kind()) {
1317     default:
1318       break;
1319     case CXCursor_DeclRefExpr: {
1320       ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
1321       if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) {
1322         IndexId::Var ref_var_id = db->ToVarId(ref_cursor.get_usr_hash());
1323         IndexVar* ref_var = db->Resolve(ref_var_id);
1324         if (ref_var->def.detailed_name.empty()) {
1325           ClangCursor sem_parent = ref_cursor.get_semantic_parent();
1326           ClangCursor lex_parent = ref_cursor.get_lexical_parent();
1327           SetUsePreflight(db, sem_parent);
1328           SetUsePreflight(db, lex_parent);
1329           ref_var = db->Resolve(ref_var_id);
1330           ref_var->def.spell =
1331               SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
1332           ref_var->def.extent =
1333               SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
1334           ref_var = db->Resolve(ref_var_id);
1335           ref_var->def.kind = lsSymbolKind::TypeParameter;
1336           SetVarDetail(ref_var, ref_cursor.get_spell_name(), ref_cursor,
1337                        nullptr, true, db, param);
1338 
1339           ClangType ref_type = clang_getCursorType(ref_cursor.cx_cursor);
1340           // TODO optimize
1341           if (ref_type.get_usr().size()) {
1342             IndexType* ref_type_index =
1343                 db->Resolve(db->ToTypeId(ref_type.get_usr_hash()));
1344             // The cursor extent includes `type name`, not just `name`. There
1345             // seems no way to extract the spelling range of `type` and we do
1346             // not want to do subtraction here.
1347             // See https://github.com/jacobdufault/cquery/issues/252
1348             AddRef(db, ref_type_index->uses, ref_cursor.get_extent(),
1349                    ref_cursor.get_lexical_parent());
1350           }
1351         }
1352         AddRefSpell(db, ref_var->uses, cursor);
1353       }
1354       break;
1355     }
1356     case CXCursor_OverloadedDeclRef: {
1357       unsigned num_overloaded = clang_getNumOverloadedDecls(cursor.cx_cursor);
1358       for (unsigned i = 0; i != num_overloaded; i++) {
1359         ClangCursor overloaded = clang_getOverloadedDecl(cursor.cx_cursor, i);
1360         switch (overloaded.get_kind()) {
1361           default:
1362             break;
1363           case CXCursor_FunctionDecl:
1364           case CXCursor_FunctionTemplate: {
1365             IndexId::Func called_id = db->ToFuncId(overloaded.get_usr_hash());
1366             OnIndexReference_Function(db, cursor.get_spell(), data->container,
1367                                       called_id, Role::Call);
1368             break;
1369           }
1370         }
1371       }
1372       break;
1373     }
1374     case CXCursor_TemplateRef: {
1375       ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
1376       if (ref_cursor.get_kind() == CXCursor_TemplateTemplateParameter) {
1377         IndexId::Type ref_type_id = db->ToTypeId(ref_cursor.get_usr_hash());
1378         IndexType* ref_type = db->Resolve(ref_type_id);
1379         // TODO It seems difficult to get references to template template
1380         // parameters.
1381         // CXCursor_TemplateTemplateParameter can be visited by visiting
1382         // CXCursor_TranslationUnit, but not (confirm this) by visiting
1383         // {Class,Function}Template. Thus we need to initialize it here.
1384         if (ref_type->def.detailed_name.empty()) {
1385           ClangCursor sem_parent = ref_cursor.get_semantic_parent();
1386           ClangCursor lex_parent = ref_cursor.get_lexical_parent();
1387           SetUsePreflight(db, sem_parent);
1388           SetUsePreflight(db, lex_parent);
1389           ref_type = db->Resolve(ref_type_id);
1390           ref_type->def.spell =
1391               SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
1392           ref_type->def.extent =
1393               SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
1394 #if 0 && CINDEX_HAVE_PRETTY
1395           ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
1396 #else
1397           ref_type->def.detailed_name = ref_cursor.get_spell_name();
1398 #endif
1399           ref_type->def.short_name_offset = 0;
1400           ref_type->def.short_name_size =
1401               int16_t(strlen(ref_type->def.detailed_name.c_str()));
1402           ref_type->def.kind = lsSymbolKind::TypeParameter;
1403         }
1404         AddRefSpell(db, ref_type->uses, cursor);
1405       }
1406       break;
1407     }
1408     case CXCursor_TypeRef: {
1409       ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
1410       if (ref_cursor.get_kind() == CXCursor_TemplateTypeParameter) {
1411         IndexId::Type ref_type_id = db->ToTypeId(ref_cursor.get_usr_hash());
1412         IndexType* ref_type = db->Resolve(ref_type_id);
1413         // TODO It seems difficult to get a FunctionTemplate's template
1414         // parameters.
1415         // CXCursor_TemplateTypeParameter can be visited by visiting
1416         // CXCursor_TranslationUnit, but not (confirm this) by visiting
1417         // {Class,Function}Template. Thus we need to initialize it here.
1418         if (ref_type->def.detailed_name.empty()) {
1419           ClangCursor sem_parent = ref_cursor.get_semantic_parent();
1420           ClangCursor lex_parent = ref_cursor.get_lexical_parent();
1421           SetUsePreflight(db, sem_parent);
1422           SetUsePreflight(db, lex_parent);
1423           ref_type = db->Resolve(ref_type_id);
1424           ref_type->def.spell =
1425               SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
1426           ref_type->def.extent =
1427               SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
1428 #if 0 && CINDEX_HAVE_PRETTY
1429           // template<class T> void f(T t){} // weird, the name is empty
1430           ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
1431 #else
1432           ref_type->def.detailed_name = ref_cursor.get_spell_name();
1433 #endif
1434           ref_type->def.short_name_offset = 0;
1435           ref_type->def.short_name_size =
1436               int16_t(strlen(ref_type->def.detailed_name.c_str()));
1437           ref_type->def.kind = lsSymbolKind::TypeParameter;
1438         }
1439         AddRefSpell(db, ref_type->uses, cursor);
1440       }
1441       break;
1442     }
1443   }
1444   return ClangCursor::VisitResult::Recurse;
1445 }
1446 
1447 }  // namespace
1448 
QualifiedName(const CXIdxContainerInfo * container,std::string_view unqualified_name)1449 std::string NamespaceHelper::QualifiedName(const CXIdxContainerInfo* container,
1450                                            std::string_view unqualified_name) {
1451   if (!container)
1452     return std::string(unqualified_name);
1453   // Anonymous namespaces are not processed by indexDeclaration. We trace
1454   // nested namespaces bottom-up through clang_getCursorSemanticParent until
1455   // one that we know its qualified name. Then do another trace top-down and
1456   // put their names into a map of USR -> qualified_name.
1457   ClangCursor cursor(container->cursor);
1458   std::vector<ClangCursor> namespaces;
1459   std::string qualifier;
1460   while (cursor.get_kind() != CXCursor_TranslationUnit &&
1461          GetSymbolKind(cursor.get_kind()) == SymbolKind::Type) {
1462     auto it = container_cursor_to_qualified_name.find(cursor);
1463     if (it != container_cursor_to_qualified_name.end()) {
1464       qualifier = it->second;
1465       break;
1466     }
1467     namespaces.push_back(cursor);
1468     cursor = clang_getCursorSemanticParent(cursor.cx_cursor);
1469   }
1470   for (size_t i = namespaces.size(); i > 0;) {
1471     i--;
1472     std::string name = namespaces[i].get_spell_name();
1473     // Empty name indicates unnamed namespace, anonymous struct, anonymous
1474     // union, ...
1475     if (name.size())
1476       qualifier += name;
1477     else
1478       qualifier += GetAnonName(namespaces[i].get_kind());
1479     qualifier += "::";
1480     container_cursor_to_qualified_name[namespaces[i]] = qualifier;
1481   }
1482   // C++17 string::append
1483   return qualifier + std::string(unqualified_name);
1484 }
1485 
OnIndexDeclaration(CXClientData client_data,const CXIdxDeclInfo * decl)1486 void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
1487   IndexParam* param = static_cast<IndexParam*>(client_data);
1488 
1489   // Track all constructor declarations, as we may need to use it to manually
1490   // associate std::make_unique and the like as constructor invocations.
1491   if (decl->entityInfo->kind == CXIdxEntity_CXXConstructor) {
1492     param->ctors.NotifyConstructor(decl->cursor);
1493   }
1494 
1495   CXFile file;
1496   clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(decl->loc),
1497                             &file, nullptr, nullptr, nullptr);
1498   IndexFile* db = ConsumeFile(param, file);
1499   if (!db)
1500     return;
1501 
1502   // The language of this declaration
1503   LanguageId decl_lang = [&decl]() {
1504     switch (clang_getCursorLanguage(decl->cursor)) {
1505       case CXLanguage_C:
1506         return LanguageId::C;
1507       case CXLanguage_CPlusPlus:
1508         return LanguageId::Cpp;
1509       case CXLanguage_ObjC:
1510         return LanguageId::ObjC;
1511       default:
1512         return LanguageId::Unknown;
1513     };
1514   }();
1515 
1516   // Only update the file language if the new language is "greater" than the old
1517   if (decl_lang > db->language) {
1518     db->language = decl_lang;
1519   }
1520 
1521   ClangCursor sem_parent(FromContainer(decl->semanticContainer));
1522   ClangCursor lex_parent(FromContainer(decl->lexicalContainer));
1523   SetUsePreflight(db, sem_parent);
1524   SetUsePreflight(db, lex_parent);
1525   ClangCursor cursor = decl->cursor;
1526 
1527   switch (decl->entityInfo->kind) {
1528     case CXIdxEntity_Unexposed:
1529       LOG_S(INFO) << "CXIdxEntity_Unexposed " << cursor.get_spell_name();
1530       break;
1531 
1532     case CXIdxEntity_CXXNamespace: {
1533       Range spell = cursor.get_spell();
1534       IndexId::Type ns_id = db->ToTypeId(HashUsr(decl->entityInfo->USR));
1535       IndexType* ns = db->Resolve(ns_id);
1536       ns->def.kind = GetSymbolKind(decl->entityInfo->kind);
1537       if (ns->def.detailed_name.empty()) {
1538         SetTypeName(ns, cursor, decl->semanticContainer, decl->entityInfo->name,
1539                     param);
1540         ns->def.spell = SetUse(db, spell, sem_parent, Role::Definition);
1541         ns->def.extent =
1542             SetUse(db, cursor.get_extent(), lex_parent, Role::None);
1543         if (decl->semanticContainer) {
1544           IndexId::Type parent_id = db->ToTypeId(
1545               ClangCursor(decl->semanticContainer->cursor).get_usr_hash());
1546           db->Resolve(parent_id)->derived.push_back(ns_id);
1547           // |ns| may be invalidated.
1548           ns = db->Resolve(ns_id);
1549           ns->def.bases.push_back(parent_id);
1550         }
1551       }
1552       AddRef(db, ns->uses, spell, lex_parent);
1553       break;
1554     }
1555 
1556     case CXIdxEntity_CXXNamespaceAlias:
1557       assert(false && "CXXNamespaceAlias");
1558       break;
1559 
1560     case CXIdxEntity_ObjCProperty:
1561     case CXIdxEntity_ObjCIvar:
1562     case CXIdxEntity_EnumConstant:
1563     case CXIdxEntity_Field:
1564     case CXIdxEntity_Variable:
1565     case CXIdxEntity_CXXStaticVariable: {
1566       Range spell = cursor.get_spell();
1567 
1568       // Do not index implicit template instantiations.
1569       if (cursor != cursor.template_specialization_to_template_definition())
1570         break;
1571 
1572       IndexId::Var var_id = db->ToVarId(HashUsr(decl->entityInfo->USR));
1573       IndexVar* var = db->Resolve(var_id);
1574 
1575       // TODO: Eventually run with this if. Right now I want to iron out bugs
1576       // this may shadow.
1577       // TODO: Verify this gets called multiple times
1578       // if (!decl->isRedeclaration) {
1579       SetVarDetail(var, std::string(decl->entityInfo->name), decl->cursor,
1580                    decl->semanticContainer, !decl->isRedeclaration, db, param);
1581 
1582       // FIXME https://github.com/jacobdufault/cquery/issues/239
1583       var->def.kind = GetSymbolKind(decl->entityInfo->kind);
1584       if (var->def.kind == lsSymbolKind::Variable &&
1585           decl->cursor.kind == CXCursor_ParmDecl)
1586         var->def.kind = lsSymbolKind::Parameter;
1587       //}
1588 
1589       if (decl->isDefinition) {
1590         var->def.spell = SetUse(db, spell, sem_parent, Role::Definition);
1591         var->def.extent =
1592             SetUse(db, cursor.get_extent(), lex_parent, Role::None);
1593       } else {
1594         var->declarations.push_back(
1595             SetRef(db, spell, lex_parent, Role::Declaration));
1596       }
1597 
1598       cursor.VisitChildren(&AddDeclInitializerUsagesVisitor, db);
1599       var = db->Resolve(var_id);
1600 
1601       // Declaring variable type information. Note that we do not insert an
1602       // interesting reference for parameter declarations - that is handled when
1603       // the function declaration is encountered since we won't receive ParmDecl
1604       // declarations for unnamed parameters.
1605       // TODO: See if we can remove this function call.
1606       AddDeclTypeUsages(db, cursor, var->def.type, decl->semanticContainer,
1607                         decl->lexicalContainer);
1608 
1609       // We don't need to assign declaring type multiple times if this variable
1610       // has already been seen.
1611 
1612       if (decl->isDefinition && decl->semanticContainer) {
1613         switch (GetSymbolKind(decl->semanticContainer->cursor.kind)) {
1614           case SymbolKind::Func: {
1615             db->Resolve(db->ToFuncId(decl->semanticContainer->cursor))
1616                 ->def.vars.push_back(var_id);
1617             break;
1618           }
1619           case SymbolKind::Type:
1620             if (decl->semanticContainer->cursor.kind != CXCursor_EnumDecl) {
1621               db->Resolve(db->ToTypeId(decl->semanticContainer->cursor))
1622                   ->def.vars.push_back(var_id);
1623             }
1624             break;
1625           default:
1626             break;
1627         }
1628       }
1629 
1630       break;
1631     }
1632 
1633     case CXIdxEntity_ObjCInstanceMethod:
1634     case CXIdxEntity_ObjCClassMethod:
1635     case CXIdxEntity_Function:
1636     case CXIdxEntity_CXXConstructor:
1637     case CXIdxEntity_CXXDestructor:
1638     case CXIdxEntity_CXXInstanceMethod:
1639     case CXIdxEntity_CXXStaticMethod:
1640     case CXIdxEntity_CXXConversionFunction: {
1641       Range spell = cursor.get_spell();
1642       Range extent = cursor.get_extent();
1643 
1644       ClangCursor decl_cursor_resolved =
1645           cursor.template_specialization_to_template_definition();
1646       bool is_template_specialization = cursor != decl_cursor_resolved;
1647 
1648       IndexId::Func func_id = db->ToFuncId(decl_cursor_resolved.cx_cursor);
1649       IndexFunc* func = db->Resolve(func_id);
1650       if (g_config->index.comments)
1651         func->def.comments = cursor.get_comments();
1652       func->def.kind = GetSymbolKind(decl->entityInfo->kind);
1653       func->def.storage =
1654           GetStorageClass(clang_Cursor_getStorageClass(decl->cursor));
1655 
1656       // We don't actually need to know the return type, but we need to mark it
1657       // as an interesting usage.
1658       AddDeclTypeUsages(db, cursor, nullopt, decl->semanticContainer,
1659                         decl->lexicalContainer);
1660 
1661       // Add definition or declaration. This is a bit tricky because we treat
1662       // template specializations as declarations, even though they are
1663       // technically definitions.
1664       // TODO: Support multiple function definitions, which is common for
1665       //       template specializations.
1666       if (decl->isDefinition && !is_template_specialization) {
1667         // assert(!func->def.spell);
1668         // assert(!func->def.extent);
1669         func->def.spell = SetUse(db, spell, sem_parent, Role::Definition);
1670         func->def.extent = SetUse(db, extent, lex_parent, Role::None);
1671       } else {
1672         IndexFunc::Declaration declaration;
1673         declaration.spell = SetUse(db, spell, lex_parent, Role::Declaration);
1674 
1675         // Add parameters.
1676         for (ClangCursor arg : cursor.get_arguments()) {
1677           switch (arg.get_kind()) {
1678             case CXCursor_ParmDecl: {
1679               Range param_spelling = arg.get_spell();
1680 
1681               // If the name is empty (which is common for parameters), clang
1682               // will report a range with length 1, which is not correct.
1683               if (param_spelling.start.column ==
1684                       (param_spelling.end.column - 1) &&
1685                   arg.get_display_name().empty()) {
1686                 param_spelling.end.column -= 1;
1687               }
1688 
1689               declaration.param_spellings.push_back(param_spelling);
1690               break;
1691             }
1692             default:
1693               break;
1694           }
1695         }
1696 
1697         func->declarations.push_back(declaration);
1698       }
1699 
1700       // Emit definition data for the function. We do this even if it isn't a
1701       // definition because there can be, for example, interfaces, or a class
1702       // declaration that doesn't have a definition yet. If we never end up
1703       // indexing the definition, then there will not be any (ie) outline
1704       // information.
1705       if (!is_template_specialization) {
1706         // Build detailed name. The type desc looks like void (void *). We
1707         // insert the qualified name before the first '('.
1708         // FIXME GetFunctionSignature should set index
1709 #if CINDEX_HAVE_PRETTY
1710         func->def.detailed_name = param->PrettyPrintCursor(decl->cursor);
1711 #else
1712         func->def.detailed_name = GetFunctionSignature(db, &param->ns, decl);
1713 #endif
1714         auto idx = func->def.detailed_name.find(decl->entityInfo->name);
1715         assert(idx != std::string::npos);
1716         func->def.short_name_offset = idx;
1717         func->def.short_name_size = strlen(decl->entityInfo->name);
1718 
1719         // CXCursor_OverloadedDeclRef in templates are not processed by
1720         // OnIndexReference, thus we use TemplateVisitor to collect function
1721         // references.
1722         if (decl->entityInfo->templateKind == CXIdxEntity_Template) {
1723           TemplateVisitorData data;
1724           data.db = db;
1725           data.param = param;
1726           data.container = cursor;
1727           cursor.VisitChildren(&TemplateVisitor, &data);
1728           // TemplateVisitor calls ToFuncId which invalidates func
1729           func = db->Resolve(func_id);
1730         }
1731 
1732         // Add function usage information. We only want to do it once per
1733         // definition/declaration. Do it on definition since there should only
1734         // ever be one of those in the entire program.
1735         if (IsTypeDefinition(decl->semanticContainer)) {
1736           IndexId::Type declaring_type_id =
1737               db->ToTypeId(decl->semanticContainer->cursor);
1738           IndexType* declaring_type_def = db->Resolve(declaring_type_id);
1739           func->def.declaring_type = declaring_type_id;
1740 
1741           // Mark a type reference at the ctor/dtor location.
1742           if (decl->entityInfo->kind == CXIdxEntity_CXXConstructor)
1743             AddRef(db, declaring_type_def->uses, spell,
1744                    FromContainer(decl->lexicalContainer));
1745 
1746           // Add function to declaring type.
1747           declaring_type_def->def.funcs.push_back(func_id);
1748         }
1749 
1750         // Process inheritance.
1751         if (clang_CXXMethod_isVirtual(decl->cursor)) {
1752           CXCursor* overridden;
1753           unsigned int num_overridden;
1754           clang_getOverriddenCursors(decl->cursor, &overridden,
1755                                      &num_overridden);
1756 
1757           for (unsigned i = 0; i < num_overridden; ++i) {
1758             ClangCursor parent =
1759                 ClangCursor(overridden[i])
1760                     .template_specialization_to_template_definition();
1761             IndexId::Func parent_id = db->ToFuncId(parent.get_usr_hash());
1762             IndexFunc* parent_def = db->Resolve(parent_id);
1763             func = db->Resolve(func_id);  // ToFuncId invalidated func_def
1764 
1765             func->def.bases.push_back(parent_id);
1766             parent_def->derived.push_back(func_id);
1767           }
1768 
1769           clang_disposeOverriddenCursors(overridden);
1770         }
1771       }
1772       break;
1773     }
1774 
1775     case CXIdxEntity_Typedef:
1776     case CXIdxEntity_CXXTypeAlias: {
1777       // Note we want to fetch the first TypeRef. Running
1778       // ResolveCursorType(decl->cursor) would return
1779       // the type of the typedef/using, not the type of the referenced type.
1780       optional<IndexId::Type> alias_of = AddDeclTypeUsages(
1781           db, cursor, nullopt, decl->semanticContainer, decl->lexicalContainer);
1782 
1783       IndexId::Type type_id = db->ToTypeId(HashUsr(decl->entityInfo->USR));
1784       IndexType* type = db->Resolve(type_id);
1785 
1786       if (alias_of)
1787         type->def.alias_of = alias_of.value();
1788 
1789       ClangCursor decl_cursor = decl->cursor;
1790       Range spell = decl_cursor.get_spell();
1791       Range extent = decl_cursor.get_extent();
1792       type->def.spell = SetUse(db, spell, sem_parent, Role::Definition);
1793       type->def.extent = SetUse(db, extent, lex_parent, Role::None);
1794 
1795       SetTypeName(type, decl_cursor, decl->semanticContainer,
1796                   decl->entityInfo->name, param);
1797       type->def.kind = GetSymbolKind(decl->entityInfo->kind);
1798       if (g_config->index.comments)
1799         type->def.comments = decl_cursor.get_comments();
1800 
1801       // For Typedef/CXXTypeAlias spanning a few lines, display the declaration
1802       // line, with spelling name replaced with qualified name.
1803       // TODO Think how to display multi-line declaration like `typedef struct {
1804       // ... } foo;` https://github.com/jacobdufault/cquery/issues/29
1805       if (extent.end.line - extent.start.line <
1806           kMaxLinesDisplayTypeAliasDeclarations) {
1807         FileContents& fc = param->file_contents[db->path];
1808         optional<int> extent_start = fc.ToOffset(extent.start),
1809                       spell_start = fc.ToOffset(spell.start),
1810                       spell_end = fc.ToOffset(spell.end),
1811                       extent_end = fc.ToOffset(extent.end);
1812         if (extent_start && spell_start && spell_end && extent_end) {
1813           type->def.hover =
1814               fc.content.substr(*extent_start, *spell_start - *extent_start) +
1815               type->def.detailed_name.c_str() +
1816               fc.content.substr(*spell_end, *extent_end - *spell_end);
1817         }
1818       }
1819 
1820       AddRef(db, type->uses, spell, FromContainer(decl->lexicalContainer));
1821       break;
1822     }
1823 
1824     case CXIdxEntity_ObjCProtocol:
1825     case CXIdxEntity_ObjCCategory:
1826     case CXIdxEntity_ObjCClass:
1827     case CXIdxEntity_Enum:
1828     case CXIdxEntity_Union:
1829     case CXIdxEntity_Struct:
1830     case CXIdxEntity_CXXInterface:
1831     case CXIdxEntity_CXXClass: {
1832       Range spell = cursor.get_spell();
1833 
1834       IndexId::Type type_id = db->ToTypeId(HashUsr(decl->entityInfo->USR));
1835       IndexType* type = db->Resolve(type_id);
1836 
1837       // TODO: Eventually run with this if. Right now I want to iron out bugs
1838       // this may shadow.
1839       // TODO: For type section, verify if this ever runs for non definitions?
1840       // if (!decl->isRedeclaration) {
1841 
1842       SetTypeName(type, cursor, decl->semanticContainer, decl->entityInfo->name,
1843                   param);
1844       type->def.kind = GetSymbolKind(decl->entityInfo->kind);
1845       if (g_config->index.comments)
1846         type->def.comments = cursor.get_comments();
1847       // }
1848 
1849       if (decl->isDefinition) {
1850         type->def.spell = SetUse(db, spell, sem_parent, Role::Definition);
1851         type->def.extent =
1852             SetUse(db, cursor.get_extent(), lex_parent, Role::None);
1853 
1854         if (cursor.get_kind() == CXCursor_EnumDecl) {
1855           ClangType enum_type = clang_getEnumDeclIntegerType(decl->cursor);
1856           if (!enum_type.is_builtin()) {
1857             IndexType* int_type =
1858                 db->Resolve(db->ToTypeId(enum_type.get_usr_hash()));
1859             AddRef(db, int_type->uses, spell,
1860                    FromContainer(decl->lexicalContainer));
1861             // type is invalidated.
1862             type = db->Resolve(type_id);
1863           }
1864         }
1865       } else
1866         AddRef(db, type->declarations, spell,
1867                FromContainer(decl->lexicalContainer), Role::Declaration);
1868 
1869       switch (decl->entityInfo->templateKind) {
1870         default:
1871           break;
1872         case CXIdxEntity_TemplateSpecialization:
1873         case CXIdxEntity_TemplatePartialSpecialization: {
1874           // TODO Use a different dimension
1875           ClangCursor origin_cursor =
1876               cursor.template_specialization_to_template_definition();
1877           IndexId::Type origin_id = db->ToTypeId(origin_cursor.get_usr_hash());
1878           IndexType* origin = db->Resolve(origin_id);
1879           // |type| may be invalidated.
1880           type = db->Resolve(type_id);
1881           // template<class T> class function; // not visited by
1882           // OnIndexDeclaration template<> class function<int> {}; // current
1883           // cursor
1884           if (origin->def.detailed_name.empty()) {
1885             SetTypeName(origin, origin_cursor, nullptr,
1886                         &type->def.ShortName()[0], param);
1887             origin->def.kind = type->def.kind;
1888           }
1889           // TODO The name may be assigned in |ResolveToDeclarationType| but
1890           // |spell| is nullopt.
1891           CXFile origin_file;
1892           Range origin_spell = origin_cursor.get_spell(&origin_file);
1893           if (!origin->def.spell && file == origin_file) {
1894             ClangCursor origin_sem = origin_cursor.get_semantic_parent();
1895             ClangCursor origin_lex = origin_cursor.get_lexical_parent();
1896             SetUsePreflight(db, origin_sem);
1897             SetUsePreflight(db, origin_lex);
1898             origin = db->Resolve(origin_id);
1899             type = db->Resolve(type_id);
1900             origin->def.spell =
1901                 SetUse(db, origin_spell, origin_sem, Role::Definition);
1902             origin->def.extent =
1903                 SetUse(db, origin_cursor.get_extent(), origin_lex, Role::None);
1904           }
1905           origin->derived.push_back(type_id);
1906           type->def.bases.push_back(origin_id);
1907         }
1908           // fallthrough
1909         case CXIdxEntity_Template: {
1910           TemplateVisitorData data;
1911           data.db = db;
1912           data.container = cursor;
1913           data.param = param;
1914           cursor.VisitChildren(&TemplateVisitor, &data);
1915           break;
1916         }
1917       }
1918 
1919       // type_def->alias_of
1920       // type_def->funcs
1921       // type_def->types
1922       // type_def->uses
1923       // type_def->vars
1924 
1925       // Add type-level inheritance information.
1926       CXIdxCXXClassDeclInfo const* class_info =
1927           clang_index_getCXXClassDeclInfo(decl);
1928       if (class_info) {
1929         for (unsigned int i = 0; i < class_info->numBases; ++i) {
1930           const CXIdxBaseClassInfo* base_class = class_info->bases[i];
1931 
1932           AddDeclTypeUsages(db, base_class->cursor, nullopt,
1933                             decl->semanticContainer, decl->lexicalContainer);
1934           optional<IndexId::Type> parent_type_id =
1935               ResolveToDeclarationType(db, base_class->cursor, param);
1936           // type_def ptr could be invalidated by ResolveToDeclarationType and
1937           // TemplateVisitor.
1938           type = db->Resolve(type_id);
1939           if (parent_type_id) {
1940             IndexType* parent_type_def = db->Resolve(parent_type_id.value());
1941             parent_type_def->derived.push_back(type_id);
1942             type->def.bases.push_back(*parent_type_id);
1943           }
1944         }
1945       }
1946       break;
1947     }
1948   }
1949 }
1950 
1951 // https://github.com/jacobdufault/cquery/issues/174
1952 // Type-dependent member access expressions do not have accurate spelling
1953 // ranges.
1954 //
1955 // Not type dependent
1956 // C<int> f; f.x // .x produces a MemberRefExpr which has a spelling range
1957 // of `x`.
1958 //
1959 // Type dependent
1960 // C<T> e; e.x // .x produces a MemberRefExpr which has a spelling range
1961 // of `e` (weird) and an empty spelling name.
1962 //
1963 // To attribute the use of `x` in `e.x`, we use cursor extent `e.x`
1964 // minus cursor spelling `e` minus the period.
CheckTypeDependentMemberRefExpr(Range * spell,const ClangCursor & cursor,IndexParam * param,const IndexFile * db)1965 void CheckTypeDependentMemberRefExpr(Range* spell,
1966                                      const ClangCursor& cursor,
1967                                      IndexParam* param,
1968                                      const IndexFile* db) {
1969   if (cursor.get_kind() == CXCursor_MemberRefExpr &&
1970       cursor.get_spell_name().empty()) {
1971     *spell = cursor.get_extent().RemovePrefix(spell->end);
1972     const FileContents& fc = param->file_contents[db->path];
1973     optional<int> maybe_period = fc.ToOffset(spell->start);
1974     if (maybe_period) {
1975       int i = *maybe_period;
1976       if (fc.content[i] == '.')
1977         spell->start.column++;
1978       // -> is likely unexposed.
1979     }
1980   }
1981 }
1982 
OnIndexReference(CXClientData client_data,const CXIdxEntityRefInfo * ref)1983 void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
1984   // TODO: Use clang_getFileUniqueID
1985   CXFile file;
1986   clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(ref->loc), &file,
1987                             nullptr, nullptr, nullptr);
1988   IndexParam* param = static_cast<IndexParam*>(client_data);
1989   IndexFile* db = ConsumeFile(param, file);
1990   if (!db)
1991     return;
1992 
1993   ClangCursor cursor(ref->cursor);
1994   ClangCursor lex_parent(FromContainer(ref->container));
1995   ClangCursor referenced;
1996   if (ref->referencedEntity)
1997     referenced = ref->referencedEntity->cursor;
1998   SetUsePreflight(db, lex_parent);
1999 
2000   switch (ref->referencedEntity->kind) {
2001     case CXIdxEntity_Unexposed:
2002       LOG_S(INFO) << "CXIdxEntity_Unexposed " << cursor.get_spell_name();
2003       break;
2004 
2005     case CXIdxEntity_CXXNamespace: {
2006       IndexType* ns = db->Resolve(db->ToTypeId(referenced.get_usr_hash()));
2007       AddRef(db, ns->uses, cursor.get_spell(), FromContainer(ref->container));
2008       break;
2009     }
2010 
2011     case CXIdxEntity_CXXNamespaceAlias: {
2012       IndexType* ns = db->Resolve(db->ToTypeId(referenced.get_usr_hash()));
2013       AddRef(db, ns->uses, cursor.get_spell(), FromContainer(ref->container));
2014       if (!ns->def.spell) {
2015         ClangCursor sem_parent = referenced.get_semantic_parent();
2016         ClangCursor lex_parent = referenced.get_lexical_parent();
2017         SetUsePreflight(db, sem_parent);
2018         SetUsePreflight(db, lex_parent);
2019         ns->def.spell =
2020             SetUse(db, referenced.get_spell(), sem_parent, Role::Definition);
2021         ns->def.extent =
2022             SetUse(db, referenced.get_extent(), lex_parent, Role::None);
2023         std::string name = referenced.get_spell_name();
2024         SetTypeName(ns, referenced, nullptr, name.c_str(), param);
2025       }
2026       break;
2027     }
2028 
2029     case CXIdxEntity_ObjCProperty:
2030     case CXIdxEntity_ObjCIvar:
2031     case CXIdxEntity_EnumConstant:
2032     case CXIdxEntity_CXXStaticVariable:
2033     case CXIdxEntity_Variable:
2034     case CXIdxEntity_Field: {
2035       Range loc = cursor.get_spell();
2036       CheckTypeDependentMemberRefExpr(&loc, cursor, param, db);
2037 
2038       referenced = referenced.template_specialization_to_template_definition();
2039 
2040       IndexId::Var var_id = db->ToVarId(referenced.get_usr_hash());
2041       IndexVar* var = db->Resolve(var_id);
2042       // Lambda paramaters are not processed by OnIndexDeclaration and
2043       // may not have a short_name yet. Note that we only process the lambda
2044       // parameter as a definition if it is in the same file as the reference,
2045       // as lambdas cannot be split across files.
2046       if (var->def.detailed_name.empty()) {
2047         CXFile referenced_file;
2048         Range spell = referenced.get_spell(&referenced_file);
2049         if (file == referenced_file) {
2050           var->def.spell = SetUse(db, spell, lex_parent, Role::Definition);
2051           var->def.extent =
2052               SetUse(db, referenced.get_extent(), lex_parent, Role::None);
2053 
2054           // TODO Some of the logic here duplicates CXIdxEntity_Variable branch
2055           // of OnIndexDeclaration. But there `decl` is of type CXIdxDeclInfo
2056           // and has more information, thus not easy to reuse the code.
2057           SetVarDetail(var, referenced.get_spell_name(), referenced, nullptr,
2058                        true, db, param);
2059           var->def.kind = lsSymbolKind::Parameter;
2060         }
2061       }
2062       AddRef(db, var->uses, loc, FromContainer(ref->container),
2063              GetRole(ref, Role::Reference));
2064       break;
2065     }
2066 
2067     case CXIdxEntity_CXXConversionFunction:
2068     case CXIdxEntity_CXXStaticMethod:
2069     case CXIdxEntity_CXXInstanceMethod:
2070     case CXIdxEntity_ObjCInstanceMethod:
2071     case CXIdxEntity_ObjCClassMethod:
2072     case CXIdxEntity_Function:
2073     case CXIdxEntity_CXXConstructor:
2074     case CXIdxEntity_CXXDestructor: {
2075       // TODO: Redirect container to constructor for the following example, ie,
2076       //       we should be inserting an outgoing function call from the Foo
2077       //       ctor.
2078       //
2079       //  int Gen() { return 5; }
2080       //  class Foo {
2081       //    int x = Gen();
2082       //  }
2083 
2084       // TODO: search full history?
2085       Range loc = cursor.get_spell();
2086 
2087       IndexId::Func called_id =
2088           db->ToFuncId(HashUsr(ref->referencedEntity->USR));
2089       IndexFunc* called = db->Resolve(called_id);
2090 
2091       std::string_view short_name = called->def.ShortName();
2092       // libclang doesn't provide a nice api to check if the given function
2093       // call is implicit. ref->kind should probably work (it's either direct
2094       // or implicit), but libclang only supports implicit for objective-c.
2095       bool is_implicit = CanBeCalledImplicitly(ref->referencedEntity->kind) &&
2096                          // Treats empty short_name as an implicit call like
2097                          // implicit move constructor in `vector<int> a = f();`
2098                          (short_name.empty() ||
2099                           // For explicit destructor call, ref->cursor may be
2100                           // "~" while called->def.short_name is "~A"
2101                           // "~A" is not a substring of ref->cursor, but we
2102                           // should take this case as not `is_implicit`.
2103                           (short_name[0] != '~' &&
2104                            !CursorSpellingContainsString(
2105                                ref->cursor, param->tu->cx_tu, short_name)));
2106 
2107       // Extents have larger ranges and thus less specific, and will be
2108       // overriden by other functions if exist.
2109       //
2110       // Type-dependent member access expressions do not have useful spelling
2111       // ranges. See the comment above for the CXIdxEntity_Field case.
2112       if (is_implicit)
2113         loc = cursor.get_extent();
2114       else
2115         CheckTypeDependentMemberRefExpr(&loc, cursor, param, db);
2116 
2117       OnIndexReference_Function(
2118           db, loc, ref->container->cursor, called_id,
2119           GetRole(ref, Role::Call) |
2120               (is_implicit ? Role::Implicit : Role::None));
2121 
2122       // Checks if |str| starts with |start|. Ignores case.
2123       auto str_begin = [](const char* start, const char* str) {
2124         while (*start && *str) {
2125           char a = tolower(*start);
2126           char b = tolower(*str);
2127           if (a != b)
2128             return false;
2129           ++start;
2130           ++str;
2131         }
2132         return !*start;
2133       };
2134 
2135       bool is_template = ref->referencedEntity->templateKind !=
2136                          CXIdxEntityCXXTemplateKind::CXIdxEntity_NonTemplate;
2137       if (g_config->index.attributeMakeCallsToCtor && is_template &&
2138           str_begin("make", ref->referencedEntity->name)) {
2139         // Try to find the return type of called function. That type will have
2140         // the constructor function we add a usage to.
2141         optional<ClangCursor> opt_found_type = FindType(ref->cursor);
2142         if (opt_found_type) {
2143           Usr ctor_type_usr = opt_found_type->get_referenced().get_usr_hash();
2144           ClangCursor call_cursor = ref->cursor;
2145 
2146           // Build a type description from the parameters of the call, so we
2147           // can try to find a constructor with the same type description.
2148           std::vector<std::string> call_type_desc;
2149           for (ClangType type : call_cursor.get_type().get_arguments()) {
2150             std::string type_desc = type.get_spell_name();
2151             if (!type_desc.empty())
2152               call_type_desc.push_back(type_desc);
2153           }
2154 
2155           // Try to find the constructor and add a reference.
2156           optional<Usr> ctor_usr =
2157               param->ctors.TryFindConstructorUsr(ctor_type_usr, call_type_desc);
2158           if (ctor_usr) {
2159             IndexFunc* ctor = db->Resolve(db->ToFuncId(*ctor_usr));
2160             ctor->uses.push_back(IndexId::LexicalRef(
2161                 loc, AnyId(), SymbolKind::File, Role::Call | Role::Implicit));
2162           }
2163         }
2164       }
2165 
2166       break;
2167     }
2168 
2169     case CXIdxEntity_ObjCCategory:
2170     case CXIdxEntity_ObjCProtocol:
2171     case CXIdxEntity_ObjCClass:
2172     case CXIdxEntity_Typedef:
2173     case CXIdxEntity_CXXInterface:  // MSVC __interface
2174     case CXIdxEntity_CXXTypeAlias:
2175     case CXIdxEntity_Enum:
2176     case CXIdxEntity_Union:
2177     case CXIdxEntity_Struct:
2178     case CXIdxEntity_CXXClass: {
2179       referenced = referenced.template_specialization_to_template_definition();
2180       IndexType* ref_type =
2181           db->Resolve(db->ToTypeId(referenced.get_usr_hash()));
2182       if (!ref->parentEntity || IsDeclContext(ref->parentEntity->kind))
2183         AddRefSpell(db, ref_type->declarations, ref->cursor);
2184       else
2185         AddRefSpell(db, ref_type->uses, ref->cursor);
2186       break;
2187     }
2188   }
2189 }
2190 
Parse(FileConsumerSharedState * file_consumer_shared,const std::string & file0,const std::vector<std::string> & args,const std::vector<FileContents> & file_contents,ClangIndex * index,bool dump_ast)2191 optional<std::vector<std::unique_ptr<IndexFile>>> Parse(
2192     FileConsumerSharedState* file_consumer_shared,
2193     const std::string& file0,
2194     const std::vector<std::string>& args,
2195     const std::vector<FileContents>& file_contents,
2196     ClangIndex* index,
2197     bool dump_ast) {
2198   if (!g_config->index.enabled)
2199     return nullopt;
2200 
2201   optional<AbsolutePath> file = NormalizePath(file0);
2202   if (!file) {
2203     LOG_S(WARNING) << "Cannot index " << file0
2204                    << " because it can not be found";
2205     return nullopt;
2206   }
2207 
2208   std::vector<CXUnsavedFile> unsaved_files;
2209   for (const FileContents& contents : file_contents) {
2210     CXUnsavedFile unsaved;
2211     unsaved.Filename = contents.path.path.c_str();
2212     unsaved.Contents = contents.content.c_str();
2213     unsaved.Length = (unsigned long)contents.content.size();
2214     unsaved_files.push_back(unsaved);
2215   }
2216 
2217   std::unique_ptr<ClangTranslationUnit> tu = ClangTranslationUnit::Create(
2218       index, file->path, args, unsaved_files,
2219       CXTranslationUnit_KeepGoing |
2220           CXTranslationUnit_DetailedPreprocessingRecord);
2221   if (!tu)
2222     return nullopt;
2223 
2224   if (dump_ast)
2225     Dump(clang_getTranslationUnitCursor(tu->cx_tu));
2226 
2227   return ParseWithTu(file_consumer_shared, tu.get(), index, *file, args,
2228                      unsaved_files);
2229 }
2230 
ParseWithTu(FileConsumerSharedState * file_consumer_shared,ClangTranslationUnit * tu,ClangIndex * index,const AbsolutePath & file,const std::vector<std::string> & args,const std::vector<CXUnsavedFile> & file_contents)2231 optional<std::vector<std::unique_ptr<IndexFile>>> ParseWithTu(
2232     FileConsumerSharedState* file_consumer_shared,
2233     ClangTranslationUnit* tu,
2234     ClangIndex* index,
2235     const AbsolutePath& file,
2236     const std::vector<std::string>& args,
2237     const std::vector<CXUnsavedFile>& file_contents) {
2238   IndexerCallbacks callback = {0};
2239   // Available callbacks:
2240   // - abortQuery
2241   // - enteredMainFile
2242   // - ppIncludedFile
2243   // - importedASTFile
2244   // - startedTranslationUnit
2245   callback.diagnostic = &OnIndexDiagnostic;
2246   callback.ppIncludedFile = &OnIndexIncludedFile;
2247   callback.indexDeclaration = &OnIndexDeclaration;
2248   callback.indexEntityReference = &OnIndexReference;
2249 
2250   FileConsumer file_consumer(file_consumer_shared, file);
2251   IndexParam param(tu, &file_consumer);
2252   for (const CXUnsavedFile& contents : file_contents) {
2253     AbsolutePath contents_path(contents.Filename);
2254     param.file_contents[contents_path] = FileContents(
2255         contents_path, std::string(contents.Contents, contents.Length));
2256   }
2257 
2258   CXFile cx_file = clang_getFile(tu->cx_tu, file.path.c_str());
2259   param.primary_file = ConsumeFile(&param, cx_file);
2260 
2261   CXIndexAction index_action = clang_IndexAction_create(index->cx_index);
2262 
2263   // |index_result| is a CXErrorCode instance.
2264   int index_result = clang_indexTranslationUnit(
2265       index_action, &param, &callback, sizeof(IndexerCallbacks),
2266       CXIndexOpt_IndexFunctionLocalSymbols |
2267           CXIndexOpt_SkipParsedBodiesInSession |
2268           CXIndexOpt_IndexImplicitTemplateInstantiations,
2269       tu->cx_tu);
2270   if (index_result != CXError_Success) {
2271     LOG_S(ERROR) << "Indexing " << file
2272                  << " failed with errno=" << index_result;
2273     return nullopt;
2274   }
2275 
2276   clang_IndexAction_dispose(index_action);
2277 
2278   ClangCursor(clang_getTranslationUnitCursor(tu->cx_tu))
2279       .VisitChildren(&VisitMacroDefinitionAndExpansions, &param);
2280 
2281   std::unordered_map<AbsolutePath, int> inc_to_line;
2282   // TODO
2283   if (param.primary_file)
2284     for (auto& inc : param.primary_file->includes)
2285       inc_to_line[inc.resolved_path] = inc.line;
2286 
2287   auto result = param.file_consumer->TakeLocalState();
2288   for (std::unique_ptr<IndexFile>& entry : result) {
2289     entry->import_file = file;
2290     entry->args = args;
2291     for (IndexFunc& func : entry->funcs) {
2292       // e.g. declaration + out-of-line definition
2293       Uniquify(func.derived);
2294       Uniquify(func.uses);
2295     }
2296     for (IndexType& type : entry->types) {
2297       Uniquify(type.derived);
2298       Uniquify(type.uses);
2299       // e.g. declaration + out-of-line definition
2300       Uniquify(type.def.funcs);
2301     }
2302     for (IndexVar& var : entry->vars)
2303       Uniquify(var.uses);
2304 
2305     if (param.primary_file) {
2306       // If there are errors, show at least one at the include position.
2307       auto it = inc_to_line.find(entry->path);
2308       if (it != inc_to_line.end()) {
2309         int line = it->second;
2310         for (auto ls_diagnostic : entry->diagnostics_) {
2311           if (ls_diagnostic.severity != lsDiagnosticSeverity::Error)
2312             continue;
2313           ls_diagnostic.range =
2314               lsRange(lsPosition(line, 10), lsPosition(line, 10));
2315           param.primary_file->diagnostics_.push_back(ls_diagnostic);
2316           break;
2317         }
2318       }
2319     }
2320 
2321     // Update file contents and modification time.
2322     entry->last_modification_time = param.file_modification_times[entry->path];
2323 
2324     // Update dependencies for the file. Do not include the file in its own
2325     // dependency set.
2326     entry->dependencies = param.seen_files;
2327     entry->dependencies.erase(
2328         std::remove(entry->dependencies.begin(), entry->dependencies.end(),
2329                     entry->path),
2330         entry->dependencies.end());
2331   }
2332 
2333   return std::move(result);
2334 }
2335 
ConcatTypeAndName(std::string & type,const std::string & name)2336 void ConcatTypeAndName(std::string& type, const std::string& name) {
2337   if (type.size() &&
2338       (type.back() != ' ' && type.back() != '*' && type.back() != '&'))
2339     type.push_back(' ');
2340   type.append(name);
2341 }
2342 
IndexInit()2343 void IndexInit() {
2344   clang_enableStackTraces();
2345   if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
2346     clang_toggleCrashRecovery(1);
2347 }
2348 
ClangSanityCheck(const Project::Entry & entry)2349 void ClangSanityCheck(const Project::Entry& entry) {
2350   std::vector<const char*> args;
2351   ClangIndex index;
2352   std::vector<CXUnsavedFile> unsaved;
2353   std::unique_ptr<ClangTranslationUnit> tu = ClangTranslationUnit::Create(
2354       &index, entry.filename, entry.args, unsaved, 0);
2355   if (!tu)
2356     ABORT_S() << "Creating translation unit failed";
2357 
2358   struct ClientData {
2359     int num_diagnostics = 0;
2360   };
2361 
2362   IndexerCallbacks callback = {0};
2363   callback.abortQuery = [](CXClientData client_data, void* reserved) {
2364     return 0;
2365   };
2366   callback.diagnostic = [](CXClientData client_data,
2367                            CXDiagnosticSet diagnostics, void* reserved) {
2368     ((ClientData*)client_data)->num_diagnostics +=
2369         clang_getNumDiagnosticsInSet(diagnostics);
2370     for (unsigned i = 0; i < clang_getNumDiagnosticsInSet(diagnostics); ++i) {
2371       CXDiagnostic diagnostic = clang_getDiagnosticInSet(diagnostics, i);
2372 
2373       // Get db so we can attribute diagnostic to the right indexed file.
2374       CXFile file;
2375       unsigned int line, column;
2376       CXSourceLocation diag_loc = clang_getDiagnosticLocation(diagnostic);
2377       clang_getSpellingLocation(diag_loc, &file, &line, &column, nullptr);
2378       std::string file_name;
2379       if (FileName(file))
2380         file_name = FileName(file)->path + ":";
2381       LOG_S(WARNING) << file_name << line << ":" << column << " "
2382                      << ToString(clang_getDiagnosticSpelling(diagnostic));
2383       clang_disposeDiagnostic(diagnostic);
2384     }
2385   };
2386   callback.enteredMainFile = [](CXClientData client_data, CXFile mainFile,
2387                                 void* reserved) -> CXIdxClientFile {
2388     return nullptr;
2389   };
2390   callback.ppIncludedFile =
2391       [](CXClientData client_data,
2392          const CXIdxIncludedFileInfo* file) -> CXIdxClientFile {
2393     return nullptr;
2394   };
2395   callback.importedASTFile =
2396       [](CXClientData client_data,
2397          const CXIdxImportedASTFileInfo*) -> CXIdxClientASTFile {
2398     return nullptr;
2399   };
2400   callback.startedTranslationUnit = [](CXClientData client_data,
2401                                        void* reserved) -> CXIdxClientContainer {
2402     return nullptr;
2403   };
2404   callback.indexDeclaration = [](CXClientData client_data,
2405                                  const CXIdxDeclInfo* decl) {};
2406   callback.indexEntityReference = [](CXClientData client_data,
2407                                      const CXIdxEntityRefInfo* ref) {};
2408 
2409   const unsigned kIndexOpts = 0;
2410   CXIndexAction index_action = clang_IndexAction_create(tu->cx_tu);
2411   ClientData index_param;
2412   clang_toggleCrashRecovery(0);
2413   clang_indexTranslationUnit(index_action, &index_param, &callback,
2414                              sizeof(IndexerCallbacks), kIndexOpts, tu->cx_tu);
2415   clang_IndexAction_dispose(index_action);
2416 
2417   LOG_S(INFO) << "Got " << index_param.num_diagnostics << " diagnostics";
2418 }
2419 
GetClangVersion()2420 std::string GetClangVersion() {
2421   return ToString(clang_getClangVersion());
2422 }
2423 
2424 // |SymbolRef| is serialized this way.
2425 // |Use| also uses this though it has an extra field |file|,
2426 // which is not used by Index* so it does not need to be serialized.
Reflect(Reader & visitor,Reference & value)2427 void Reflect(Reader& visitor, Reference& value) {
2428   if (visitor.Format() == SerializeFormat::Json) {
2429     std::string t = visitor.GetString();
2430     char* s = const_cast<char*>(t.c_str());
2431     value.range = Range(s);
2432     s = strchr(s, '|');
2433     value.id.id = RawId(strtol(s + 1, &s, 10));
2434     value.kind = static_cast<SymbolKind>(strtol(s + 1, &s, 10));
2435     value.role = static_cast<Role>(strtol(s + 1, &s, 10));
2436   } else {
2437     Reflect(visitor, value.range);
2438     Reflect(visitor, value.id);
2439     Reflect(visitor, value.kind);
2440     Reflect(visitor, value.role);
2441   }
2442 }
Reflect(Writer & visitor,Reference & value)2443 void Reflect(Writer& visitor, Reference& value) {
2444   if (visitor.Format() == SerializeFormat::Json) {
2445     std::string s = value.range.ToString();
2446     // RawId(-1) -> "-1"
2447     s += '|' + std::to_string(
2448                    static_cast<std::make_signed<RawId>::type>(value.id.id));
2449     s += '|' + std::to_string(int(value.kind));
2450     s += '|' + std::to_string(int(value.role));
2451     Reflect(visitor, s);
2452   } else {
2453     Reflect(visitor, value.range);
2454     Reflect(visitor, value.id);
2455     Reflect(visitor, value.kind);
2456     Reflect(visitor, value.role);
2457   }
2458 }
2459