2017-02-22 08:52:00 +00:00
|
|
|
#include "indexer.h"
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
2017-04-08 22:54:36 +00:00
|
|
|
#include "platform.h"
|
2017-02-23 08:47:07 +00:00
|
|
|
#include "serializer.h"
|
2017-05-17 07:08:45 +00:00
|
|
|
#include "timer.h"
|
2017-12-29 09:19:02 +00:00
|
|
|
#include "type_printer.h"
|
2017-02-23 08:47:07 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
#include <assert.h>
|
2018-04-08 06:32:35 +00:00
|
|
|
#include <inttypes.h>
|
2018-03-31 20:59:27 +00:00
|
|
|
#include <limits.h>
|
2017-04-22 07:39:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
2018-05-05 22:29:17 +00:00
|
|
|
#include <unordered_set>
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2018-02-04 22:54:08 +00:00
|
|
|
#if CINDEX_VERSION >= 47
|
|
|
|
#define CINDEX_HAVE_PRETTY 1
|
|
|
|
#endif
|
2018-02-12 18:15:43 +00:00
|
|
|
#if CINDEX_VERSION >= 48
|
|
|
|
#define CINDEX_HAVE_ROLE 1
|
|
|
|
#endif
|
2018-02-04 22:54:08 +00:00
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-12-19 07:05:12 +00:00
|
|
|
// For typedef/using spanning less than or equal to (this number) of lines,
|
|
|
|
// display their declarations on hover.
|
|
|
|
constexpr int kMaxLinesDisplayTypeAliasDeclarations = 3;
|
2017-04-12 07:57:12 +00:00
|
|
|
|
2018-02-20 21:56:56 +00:00
|
|
|
// TODO How to check if a reference to type is a declaration?
|
|
|
|
// This currently also includes constructors/destructors.
|
|
|
|
// It seems declarations in functions are not indexed.
|
|
|
|
bool IsDeclContext(CXIdxEntityKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case CXIdxEntity_CXXClass:
|
|
|
|
case CXIdxEntity_CXXNamespace:
|
|
|
|
case CXIdxEntity_ObjCCategory:
|
|
|
|
case CXIdxEntity_ObjCClass:
|
|
|
|
case CXIdxEntity_ObjCProtocol:
|
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:15:43 +00:00
|
|
|
Role GetRole(const CXIdxEntityRefInfo* ref_info, Role role) {
|
|
|
|
#if CINDEX_HAVE_ROLE
|
|
|
|
return static_cast<Role>(static_cast<int>(ref_info->role));
|
|
|
|
#else
|
|
|
|
return role;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
SymbolKind GetSymbolKind(CXCursorKind kind) {
|
|
|
|
switch (kind) {
|
2018-03-02 18:18:01 +00:00
|
|
|
case CXCursor_TranslationUnit:
|
|
|
|
return SymbolKind::File;
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
case CXCursor_Constructor:
|
|
|
|
case CXCursor_Destructor:
|
|
|
|
case CXCursor_ConversionFunction:
|
|
|
|
case CXCursor_FunctionTemplate:
|
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
case CXCursor_LambdaExpr:
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXCursor_ObjCInstanceMethodDecl:
|
|
|
|
case CXCursor_ObjCClassMethodDecl:
|
2018-02-22 07:34:32 +00:00
|
|
|
return SymbolKind::Func;
|
|
|
|
|
|
|
|
case CXCursor_StructDecl:
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXCursor_UnionDecl:
|
2018-02-22 07:34:32 +00:00
|
|
|
case CXCursor_ClassDecl:
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
|
|
|
case CXCursor_ObjCCategoryDecl:
|
|
|
|
case CXCursor_ObjCImplementationDecl:
|
|
|
|
case CXCursor_Namespace:
|
2018-02-22 07:34:32 +00:00
|
|
|
return SymbolKind::Type;
|
2018-03-02 17:56:44 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return SymbolKind::Invalid;
|
2018-02-10 20:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 16:43:53 +00:00
|
|
|
// Inverse of libclang/CXIndexDataConsumer.cpp getEntityKindFromSymbolKind
|
2018-02-18 19:29:38 +00:00
|
|
|
lsSymbolKind GetSymbolKind(CXIdxEntityKind kind) {
|
2018-01-07 09:07:39 +00:00
|
|
|
switch (kind) {
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXIdxEntity_Unexposed:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Unknown;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_Typedef:
|
2018-02-22 23:43:39 +00:00
|
|
|
return lsSymbolKind::TypeAlias;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_Function:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Function;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_Variable:
|
|
|
|
// Can also be Parameter
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Variable;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_Field:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Field;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_EnumConstant:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::EnumMember;
|
2018-03-02 17:56:44 +00:00
|
|
|
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCClass:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Class;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCProtocol:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Interface;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCCategory:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Interface;
|
2018-03-02 17:56:44 +00:00
|
|
|
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCInstanceMethod:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Method;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCClassMethod:
|
2018-02-19 03:01:22 +00:00
|
|
|
return lsSymbolKind::StaticMethod;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_ObjCProperty:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Property;
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXIdxEntity_ObjCIvar:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Field;
|
2018-03-02 17:56:44 +00:00
|
|
|
|
|
|
|
case CXIdxEntity_Enum:
|
|
|
|
return lsSymbolKind::Enum;
|
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
case CXIdxEntity_Union:
|
|
|
|
return lsSymbolKind::Struct;
|
|
|
|
|
|
|
|
case CXIdxEntity_CXXClass:
|
|
|
|
return lsSymbolKind::Class;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_CXXNamespace:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Namespace;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_CXXNamespaceAlias:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Namespace;
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXIdxEntity_CXXStaticVariable:
|
|
|
|
return lsSymbolKind::Field;
|
|
|
|
case CXIdxEntity_CXXStaticMethod:
|
|
|
|
return lsSymbolKind::StaticMethod;
|
|
|
|
case CXIdxEntity_CXXInstanceMethod:
|
|
|
|
return lsSymbolKind::Method;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_CXXConstructor:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Constructor;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_CXXDestructor:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Method;
|
2018-01-07 16:43:53 +00:00
|
|
|
case CXIdxEntity_CXXConversionFunction:
|
2018-02-18 19:29:38 +00:00
|
|
|
return lsSymbolKind::Constructor;
|
2018-03-02 17:56:44 +00:00
|
|
|
case CXIdxEntity_CXXTypeAlias:
|
|
|
|
return lsSymbolKind::TypeAlias;
|
|
|
|
case CXIdxEntity_CXXInterface:
|
|
|
|
return lsSymbolKind::Struct;
|
2018-01-07 09:07:39 +00:00
|
|
|
}
|
2018-03-22 04:04:41 +00:00
|
|
|
|
|
|
|
return lsSymbolKind::Unknown;
|
2018-01-07 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
StorageClass GetStorageClass(CX_StorageClass storage) {
|
2018-01-26 17:28:29 +00:00
|
|
|
switch (storage) {
|
|
|
|
case CX_SC_Invalid:
|
|
|
|
case CX_SC_OpenCLWorkGroupLocal:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::Invalid;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_None:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::None;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_Extern:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::Extern;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_Static:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::Static;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_PrivateExtern:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::PrivateExtern;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_Auto:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::Auto;
|
2018-01-26 17:28:29 +00:00
|
|
|
case CX_SC_Register:
|
2018-01-28 04:25:14 +00:00
|
|
|
return StorageClass::Register;
|
2018-01-26 17:28:29 +00:00
|
|
|
}
|
2018-03-22 04:04:41 +00:00
|
|
|
|
|
|
|
return StorageClass::None;
|
2018-01-26 17:28:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 03:55:13 +00:00
|
|
|
// Caches all instances of constructors, regardless if they are indexed or not.
|
|
|
|
// The constructor may have a make_unique call associated with it that we need
|
|
|
|
// to export. If we do not capture the parameter type description for the
|
|
|
|
// constructor we will not be able to attribute the constructor call correctly.
|
|
|
|
struct ConstructorCache {
|
|
|
|
struct Constructor {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2017-11-09 03:59:11 +00:00
|
|
|
std::vector<std::string> param_type_desc;
|
2017-11-09 03:55:13 +00:00
|
|
|
};
|
2018-01-13 08:10:39 +00:00
|
|
|
std::unordered_map<Usr, std::vector<Constructor>> constructors_;
|
2017-11-09 03:55:13 +00:00
|
|
|
|
|
|
|
// This should be called whenever there is a constructor declaration.
|
2017-11-11 19:31:05 +00:00
|
|
|
void NotifyConstructor(ClangCursor ctor_cursor) {
|
|
|
|
auto build_type_desc = [](ClangCursor cursor) {
|
2017-11-09 03:59:11 +00:00
|
|
|
std::vector<std::string> type_desc;
|
2017-11-11 19:31:05 +00:00
|
|
|
for (ClangCursor arg : cursor.get_arguments()) {
|
2017-11-09 03:59:11 +00:00
|
|
|
if (arg.get_kind() == CXCursor_ParmDecl)
|
|
|
|
type_desc.push_back(arg.get_type_description());
|
|
|
|
}
|
|
|
|
return type_desc;
|
|
|
|
};
|
|
|
|
|
2018-01-13 06:13:08 +00:00
|
|
|
Constructor ctor{ctor_cursor.get_usr_hash(), build_type_desc(ctor_cursor)};
|
2017-11-09 03:55:13 +00:00
|
|
|
|
|
|
|
// Insert into |constructors_|.
|
2018-01-13 06:13:08 +00:00
|
|
|
auto type_usr_hash = ctor_cursor.get_semantic_parent().get_usr_hash();
|
|
|
|
auto existing_ctors = constructors_.find(type_usr_hash);
|
2017-11-09 03:55:13 +00:00
|
|
|
if (existing_ctors != constructors_.end()) {
|
|
|
|
existing_ctors->second.push_back(ctor);
|
|
|
|
} else {
|
2018-01-13 06:13:08 +00:00
|
|
|
constructors_[type_usr_hash] = {ctor};
|
2017-11-09 03:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to lookup a constructor in |type_usr| that takes arguments most
|
|
|
|
// closely aligned to |param_type_desc|.
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Usr> TryFindConstructorUsr(
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr type_usr,
|
2017-11-09 03:55:13 +00:00
|
|
|
const std::vector<std::string>& param_type_desc) {
|
|
|
|
auto count_matching_prefix_length = [](const char* a, const char* b) {
|
|
|
|
int matched = 0;
|
|
|
|
while (*a && *b) {
|
|
|
|
if (*a != *b)
|
|
|
|
break;
|
|
|
|
++a;
|
|
|
|
++b;
|
|
|
|
++matched;
|
|
|
|
}
|
|
|
|
// Additional score if the strings were the same length, which makes
|
|
|
|
// "a"/"a" match higher than "a"/"a&"
|
|
|
|
if (*a == *b)
|
|
|
|
matched += 1;
|
|
|
|
return matched;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try to find constructors for the type. If there are no constructors
|
|
|
|
// available, return an empty result.
|
|
|
|
auto ctors_it = constructors_.find(type_usr);
|
|
|
|
if (ctors_it == constructors_.end())
|
2018-03-31 03:16:33 +00:00
|
|
|
return std::nullopt;
|
2017-11-09 03:55:13 +00:00
|
|
|
const std::vector<Constructor>& ctors = ctors_it->second;
|
|
|
|
if (ctors.empty())
|
2018-03-31 03:16:33 +00:00
|
|
|
return std::nullopt;
|
2017-11-09 03:55:13 +00:00
|
|
|
|
2018-01-15 16:57:47 +00:00
|
|
|
Usr best_usr = ctors[0].usr;
|
2017-11-09 03:55:13 +00:00
|
|
|
int best_score = INT_MIN;
|
|
|
|
|
|
|
|
// Scan constructors for the best possible match.
|
|
|
|
for (const Constructor& ctor : ctors) {
|
|
|
|
// If |param_type_desc| is empty and the constructor is as well, we don't
|
|
|
|
// need to bother searching, as this is the match.
|
|
|
|
if (param_type_desc.empty() && ctor.param_type_desc.empty()) {
|
|
|
|
best_usr = ctor.usr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weight matching parameter length heavily, as it is more accurate than
|
|
|
|
// the fuzzy type matching approach.
|
|
|
|
int score = 0;
|
|
|
|
if (param_type_desc.size() == ctor.param_type_desc.size())
|
|
|
|
score += param_type_desc.size() * 1000;
|
|
|
|
|
|
|
|
// Do prefix-based match on parameter type description. This works well in
|
|
|
|
// practice because clang appends qualifiers to the end of the type, ie,
|
|
|
|
// |foo *&&|
|
2018-01-13 06:13:08 +00:00
|
|
|
for (size_t i = 0;
|
2017-11-09 03:55:13 +00:00
|
|
|
i < std::min(param_type_desc.size(), ctor.param_type_desc.size());
|
|
|
|
++i) {
|
|
|
|
score += count_matching_prefix_length(param_type_desc[i].c_str(),
|
|
|
|
ctor.param_type_desc[i].c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (score > best_score) {
|
|
|
|
best_usr = ctor.usr;
|
|
|
|
best_score = score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_usr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-20 21:45:46 +00:00
|
|
|
struct IndexParam {
|
2017-07-30 04:24:02 +00:00
|
|
|
std::unordered_set<CXFile> seen_cx_files;
|
|
|
|
std::vector<std::string> seen_files;
|
2018-04-08 06:32:35 +00:00
|
|
|
std::unordered_map<std::string, FileContents> file_contents;
|
2018-05-05 22:29:17 +00:00
|
|
|
std::unordered_map<std::string, int64_t> file2write_time;
|
2017-07-30 04:24:02 +00:00
|
|
|
|
2017-05-20 21:45:46 +00:00
|
|
|
// Only use this when strictly needed (ie, primary translation unit is
|
|
|
|
// needed). Most logic should get the IndexFile instance via
|
|
|
|
// |file_consumer|.
|
|
|
|
//
|
|
|
|
// This can be null if we're not generating an index for the primary
|
|
|
|
// translation unit.
|
|
|
|
IndexFile* primary_file = nullptr;
|
|
|
|
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangTranslationUnit* tu = nullptr;
|
2017-05-20 21:45:46 +00:00
|
|
|
|
|
|
|
FileConsumer* file_consumer = nullptr;
|
|
|
|
NamespaceHelper ns;
|
2017-11-09 03:55:13 +00:00
|
|
|
ConstructorCache ctors;
|
2017-05-20 21:45:46 +00:00
|
|
|
|
2018-04-04 06:05:41 +00:00
|
|
|
IndexParam(ClangTranslationUnit* tu,
|
2018-02-22 07:34:32 +00:00
|
|
|
FileConsumer* file_consumer)
|
2018-04-04 06:05:41 +00:00
|
|
|
: tu(tu), file_consumer(file_consumer) {}
|
2018-02-04 22:54:08 +00:00
|
|
|
|
|
|
|
#if CINDEX_HAVE_PRETTY
|
|
|
|
CXPrintingPolicy print_policy = nullptr;
|
|
|
|
CXPrintingPolicy print_policy_more = nullptr;
|
|
|
|
~IndexParam() {
|
|
|
|
clang_PrintingPolicy_dispose(print_policy);
|
|
|
|
clang_PrintingPolicy_dispose(print_policy_more);
|
|
|
|
}
|
|
|
|
|
2018-04-06 00:00:07 +00:00
|
|
|
std::tuple<std::string, int16_t, int16_t, int16_t> PrettyPrintCursor(
|
|
|
|
CXCursor cursor,
|
|
|
|
std::string_view short_name) {
|
2018-02-04 22:54:08 +00:00
|
|
|
if (!print_policy) {
|
|
|
|
print_policy = clang_getCursorPrintingPolicy(cursor);
|
|
|
|
clang_PrintingPolicy_setProperty(print_policy,
|
|
|
|
CXPrintingPolicy_TerseOutput, 1);
|
2018-02-13 03:13:21 +00:00
|
|
|
clang_PrintingPolicy_setProperty(print_policy,
|
|
|
|
CXPrintingPolicy_FullyQualifiedName, 1);
|
2018-02-22 07:34:32 +00:00
|
|
|
clang_PrintingPolicy_setProperty(
|
|
|
|
print_policy, CXPrintingPolicy_SuppressInitializers, 1);
|
2018-02-04 22:54:08 +00:00
|
|
|
print_policy_more = clang_getCursorPrintingPolicy(cursor);
|
2018-02-13 03:13:21 +00:00
|
|
|
clang_PrintingPolicy_setProperty(print_policy_more,
|
|
|
|
CXPrintingPolicy_FullyQualifiedName, 1);
|
2018-02-04 22:54:08 +00:00
|
|
|
clang_PrintingPolicy_setProperty(print_policy_more,
|
|
|
|
CXPrintingPolicy_TerseOutput, 1);
|
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
std::string name =
|
|
|
|
ToString(clang_getCursorPrettyPrinted(cursor, print_policy_more));
|
|
|
|
for (std::string::size_type i = 0;;) {
|
|
|
|
if ((i = name.find("(anonymous ", i)) == std::string::npos)
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
if (name.size() > 10 + 9 && name.compare(10, 9, "namespace"))
|
|
|
|
name.replace(i, 10 + 9, "anon ns");
|
|
|
|
else
|
|
|
|
name.replace(i, 10, "anon");
|
|
|
|
}
|
|
|
|
auto i = name.find(short_name);
|
|
|
|
assert(i != std::string::npos);
|
|
|
|
int16_t short_name_offset = i, short_name_size = short_name.size();
|
|
|
|
for (int paren = 0; i; i--) {
|
|
|
|
// Skip parentheses in "(anon struct)::name"
|
|
|
|
if (name[i - 1] == ')')
|
|
|
|
paren++;
|
|
|
|
else if (name[i - 1] == '(')
|
|
|
|
paren--;
|
|
|
|
else if (!(paren > 0 || isalnum(name[i - 1]) ||
|
|
|
|
name[i - 1] == '_' || name[i - 1] == ':'))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return {name, i, short_name_offset, short_name_size};
|
2018-02-04 22:54:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-05-20 21:45:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
|
2018-04-30 02:51:25 +00:00
|
|
|
if (!file)
|
|
|
|
return nullptr;
|
2017-05-20 21:45:46 +00:00
|
|
|
bool is_first_ownership = false;
|
2018-01-11 05:16:46 +00:00
|
|
|
IndexFile* db = param->file_consumer->TryConsumeFile(
|
|
|
|
file, &is_first_ownership, ¶m->file_contents);
|
2017-05-21 01:26:50 +00:00
|
|
|
|
2017-07-30 04:24:02 +00:00
|
|
|
// If this is the first time we have seen the file (ignoring if we are
|
|
|
|
// generating an index for it):
|
|
|
|
if (param->seen_cx_files.insert(file).second) {
|
|
|
|
std::string file_name = FileName(file);
|
2018-02-20 00:19:57 +00:00
|
|
|
// file_name may be empty when it contains .. and is outside of WorkingDir.
|
2018-02-22 07:34:32 +00:00
|
|
|
// https://reviews.llvm.org/D42893
|
|
|
|
// https://github.com/cquery-project/cquery/issues/413
|
2017-08-16 03:28:52 +00:00
|
|
|
if (!file_name.empty()) {
|
|
|
|
// Add to all files we have seen so we can generate proper dependency
|
|
|
|
// graph.
|
|
|
|
param->seen_files.push_back(file_name);
|
|
|
|
|
|
|
|
// Set modification time.
|
2018-05-05 22:29:17 +00:00
|
|
|
std::optional<int64_t> write_time = LastWriteTime(file_name);
|
|
|
|
LOG_IF_S(ERROR, !write_time) << "failed to fetch write time for "
|
|
|
|
<< file_name;
|
|
|
|
if (write_time)
|
|
|
|
param->file2write_time[file_name] = *write_time;
|
2017-07-30 04:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 21:45:46 +00:00
|
|
|
|
|
|
|
if (is_first_ownership) {
|
|
|
|
// Report skipped source range list.
|
|
|
|
CXSourceRangeList* skipped = clang_getSkippedRanges(param->tu->cx_tu, file);
|
|
|
|
for (unsigned i = 0; i < skipped->count; ++i) {
|
2018-04-06 00:00:07 +00:00
|
|
|
db->skipped_by_preprocessor.push_back(
|
|
|
|
ResolveCXSourceRange(skipped->ranges[i]));
|
2017-05-20 21:45:46 +00:00
|
|
|
}
|
|
|
|
clang_disposeSourceRangeList(skipped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2017-05-23 06:47:27 +00:00
|
|
|
// Returns true if the given entity kind can be called implicitly, ie, without
|
|
|
|
// actually being written in the source code.
|
|
|
|
bool CanBeCalledImplicitly(CXIdxEntityKind kind) {
|
|
|
|
switch (kind) {
|
2017-09-22 01:14:57 +00:00
|
|
|
case CXIdxEntity_CXXConstructor:
|
|
|
|
case CXIdxEntity_CXXConversionFunction:
|
|
|
|
case CXIdxEntity_CXXDestructor:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2017-05-23 06:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the cursor spelling contains the given string. This is
|
|
|
|
// useful to check for implicit function calls.
|
2017-09-22 01:14:57 +00:00
|
|
|
bool CursorSpellingContainsString(CXCursor cursor,
|
|
|
|
CXTranslationUnit cx_tu,
|
2018-01-31 06:39:39 +00:00
|
|
|
std::string_view needle) {
|
2017-05-23 06:47:27 +00:00
|
|
|
CXSourceRange range = clang_Cursor_getSpellingNameRange(cursor, 0, 0);
|
|
|
|
CXToken* tokens;
|
2017-05-28 01:53:22 +00:00
|
|
|
unsigned num_tokens;
|
2017-05-23 06:47:27 +00:00
|
|
|
clang_tokenize(cx_tu, range, &tokens, &num_tokens);
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
2017-05-28 01:53:22 +00:00
|
|
|
for (unsigned i = 0; i < num_tokens; ++i) {
|
2017-05-23 06:47:27 +00:00
|
|
|
CXString name = clang_getTokenSpelling(cx_tu, tokens[i]);
|
2018-01-31 06:39:39 +00:00
|
|
|
if (needle == clang_getCString(name)) {
|
2017-05-23 06:47:27 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clang_disposeString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_disposeTokens(cx_tu, tokens, num_tokens);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-28 01:53:22 +00:00
|
|
|
// Returns the document content for the given range. May not work perfectly
|
|
|
|
// when there are tabs instead of spaces.
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string GetDocumentContentInRange(CXTranslationUnit cx_tu,
|
|
|
|
CXSourceRange range) {
|
2017-05-28 01:53:22 +00:00
|
|
|
std::string result;
|
|
|
|
|
|
|
|
CXToken* tokens;
|
|
|
|
unsigned num_tokens;
|
|
|
|
clang_tokenize(cx_tu, range, &tokens, &num_tokens);
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Range> previous_token_range;
|
2017-05-28 01:53:22 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_tokens; ++i) {
|
|
|
|
// Add whitespace between the previous token and this one.
|
2018-01-11 02:43:01 +00:00
|
|
|
Range token_range =
|
|
|
|
ResolveCXSourceRange(clang_getTokenExtent(cx_tu, tokens[i]));
|
2017-05-28 01:53:22 +00:00
|
|
|
if (previous_token_range) {
|
|
|
|
// Insert newlines.
|
2017-09-22 01:14:57 +00:00
|
|
|
int16_t line_delta =
|
|
|
|
token_range.start.line - previous_token_range->end.line;
|
2017-05-28 01:53:22 +00:00
|
|
|
assert(line_delta >= 0);
|
|
|
|
if (line_delta > 0) {
|
|
|
|
result.append((size_t)line_delta, '\n');
|
|
|
|
// Reset column so we insert starting padding.
|
|
|
|
previous_token_range->end.column = 0;
|
|
|
|
}
|
|
|
|
// Insert spaces.
|
2017-09-22 01:14:57 +00:00
|
|
|
int16_t column_delta =
|
|
|
|
token_range.start.column - previous_token_range->end.column;
|
2017-05-28 01:53:22 +00:00
|
|
|
assert(column_delta >= 0);
|
|
|
|
result.append((size_t)column_delta, ' ');
|
|
|
|
}
|
|
|
|
previous_token_range = token_range;
|
|
|
|
|
|
|
|
// Add token content.
|
|
|
|
CXString spelling = clang_getTokenSpelling(cx_tu, tokens[i]);
|
|
|
|
result += clang_getCString(spelling);
|
|
|
|
clang_disposeString(spelling);
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_disposeTokens(cx_tu, tokens, num_tokens);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-15 04:58:42 +00:00
|
|
|
// |parent| should be resolved before using |SetUsePreflight| so that |def| will
|
|
|
|
// not be invalidated by |To{Func,Type,Var}Id|.
|
2018-02-18 18:07:13 +00:00
|
|
|
Use SetUse(IndexFile* db, Range range, ClangCursor parent, Role role) {
|
2018-02-15 04:58:42 +00:00
|
|
|
switch (GetSymbolKind(parent.get_kind())) {
|
2018-02-18 19:29:38 +00:00
|
|
|
case SymbolKind::Func:
|
2018-04-30 04:49:03 +00:00
|
|
|
return Use{{range, db->ToFunc(parent).usr, SymbolKind::Func, role}};
|
2018-02-18 19:29:38 +00:00
|
|
|
case SymbolKind::Type:
|
2018-04-30 04:49:03 +00:00
|
|
|
return Use{{range, db->ToType(parent).usr, SymbolKind::Type, role}};
|
2018-02-18 19:29:38 +00:00
|
|
|
case SymbolKind::Var:
|
2018-04-30 04:49:03 +00:00
|
|
|
return Use{{range, db->ToVar(parent).usr, SymbolKind::Var, role}};
|
2018-02-18 18:07:13 +00:00
|
|
|
default:
|
2018-04-30 04:49:03 +00:00
|
|
|
return Use{{range, 0, SymbolKind::File, role}};
|
2018-02-11 04:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 04:31:08 +00:00
|
|
|
const char* GetAnonName(CXCursorKind kind) {
|
|
|
|
switch (kind) {
|
2018-03-20 02:51:42 +00:00
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
return "(anon class)";
|
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
return "(anon enum)";
|
2018-04-06 00:00:07 +00:00
|
|
|
case CXCursor_Namespace:
|
|
|
|
return "(anon ns)";
|
2018-03-20 02:51:42 +00:00
|
|
|
case CXCursor_StructDecl:
|
|
|
|
return "(anon struct)";
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
return "(anon union)";
|
|
|
|
default:
|
|
|
|
return "(anon)";
|
2018-02-27 04:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void SetTypeName(IndexType& type,
|
2018-01-26 06:20:28 +00:00
|
|
|
const ClangCursor& cursor,
|
2018-01-26 05:29:24 +00:00
|
|
|
const CXIdxContainerInfo* container,
|
|
|
|
const char* name,
|
2018-02-04 22:54:08 +00:00
|
|
|
IndexParam* param) {
|
2018-01-26 06:20:28 +00:00
|
|
|
CXIdxContainerInfo parent;
|
2018-01-26 05:29:24 +00:00
|
|
|
// |name| can be null in an anonymous struct (see
|
|
|
|
// tests/types/anonymous_struct.cc).
|
2018-01-31 07:01:45 +00:00
|
|
|
if (!name)
|
2018-02-27 04:31:08 +00:00
|
|
|
name = GetAnonName(cursor.get_kind());
|
2018-01-26 06:20:28 +00:00
|
|
|
if (!container)
|
|
|
|
parent.cursor = cursor.get_semantic_parent().cx_cursor;
|
2018-02-22 07:34:32 +00:00
|
|
|
// Investigate why clang_getCursorPrettyPrinted gives `struct A {}` `namespace
|
|
|
|
// ns {}` which are not qualified.
|
|
|
|
// type->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor);
|
2018-04-06 00:00:07 +00:00
|
|
|
int short_name_offset, short_name_size;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::tie(type.def.detailed_name, short_name_offset, short_name_size) =
|
2018-02-22 07:34:32 +00:00
|
|
|
param->ns.QualifiedName(container ? container : &parent, name);
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.qual_name_offset = 0;
|
|
|
|
type.def.short_name_offset = short_name_offset;
|
|
|
|
type.def.short_name_size = short_name_size;
|
2018-01-26 05:29:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:54:04 +00:00
|
|
|
// Finds the cursor associated with the declaration type of |cursor|. This
|
|
|
|
// strips
|
|
|
|
// qualifies from |cursor| (ie, Foo* => Foo) and removes template arguments
|
|
|
|
// (ie, Foo<A,B> => Foo<*,*>).
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* ResolveToDeclarationType(IndexFile* db,
|
|
|
|
ClangCursor cursor,
|
|
|
|
IndexParam* param) {
|
2018-01-21 06:34:41 +00:00
|
|
|
ClangType type = cursor.get_type();
|
|
|
|
|
|
|
|
// auto x = new Foo() will not be deduced to |Foo| if we do not use the
|
|
|
|
// canonical type. However, a canonical type will look past typedefs so we
|
|
|
|
// will not accurately report variables on typedefs if we always do this.
|
|
|
|
if (type.cx_type.kind == CXType_Auto)
|
|
|
|
type = type.get_canonical();
|
|
|
|
|
|
|
|
type = type.strip_qualifiers();
|
|
|
|
|
2018-02-27 01:23:45 +00:00
|
|
|
if (type.is_builtin()) {
|
2018-01-24 06:54:54 +00:00
|
|
|
// For builtin types, use type kinds as USR hash.
|
2018-04-30 04:49:03 +00:00
|
|
|
return &db->ToType(static_cast<Usr>(type.cx_type.kind));
|
2018-01-21 06:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClangCursor declaration =
|
2018-01-21 06:55:29 +00:00
|
|
|
type.get_declaration().template_specialization_to_template_definition();
|
2018-05-09 05:01:58 +00:00
|
|
|
std::optional<Usr> usr = declaration.get_opt_usr_hash();
|
|
|
|
if (!usr)
|
2018-04-30 04:49:03 +00:00
|
|
|
return nullptr;
|
2018-05-09 05:01:58 +00:00
|
|
|
IndexType& typ = db->ToType(*usr);
|
2018-04-30 04:49:03 +00:00
|
|
|
if (typ.def.detailed_name.empty()) {
|
2018-02-18 18:07:13 +00:00
|
|
|
std::string name = declaration.get_spell_name();
|
2018-02-04 22:54:08 +00:00
|
|
|
SetTypeName(typ, declaration, nullptr, name.c_str(), param);
|
2018-01-26 05:29:24 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
return &typ;
|
2018-01-03 07:54:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
void SetVarDetail(IndexVar& var,
|
2018-01-31 04:59:31 +00:00
|
|
|
std::string_view short_name,
|
2018-01-03 06:52:35 +00:00
|
|
|
const ClangCursor& cursor,
|
|
|
|
const CXIdxContainerInfo* semanticContainer,
|
2018-01-03 07:54:04 +00:00
|
|
|
bool is_first_seen,
|
|
|
|
IndexFile* db,
|
2018-01-03 06:52:35 +00:00
|
|
|
IndexParam* param) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar::Def& def = var.def;
|
2018-01-08 08:19:32 +00:00
|
|
|
const CXType cx_type = clang_getCursorType(cursor.cx_cursor);
|
|
|
|
std::string type_name = ToString(clang_getTypeSpelling(cx_type));
|
2018-01-03 06:52:35 +00:00
|
|
|
// clang may report "(lambda at foo.cc)" which end up being a very long
|
|
|
|
// string. Shorten it to just "lambda".
|
|
|
|
if (type_name.find("(lambda at") != std::string::npos)
|
|
|
|
type_name = "lambda";
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.comments)
|
2018-02-22 21:50:46 +00:00
|
|
|
def.comments = cursor.get_comments();
|
2018-01-26 17:28:29 +00:00
|
|
|
def.storage = GetStorageClass(clang_Cursor_getStorageClass(cursor.cx_cursor));
|
2018-01-03 06:52:35 +00:00
|
|
|
|
2018-02-13 03:13:21 +00:00
|
|
|
// TODO how to make PrettyPrint'ed variable name qualified?
|
|
|
|
#if 0 && CINDEX_HAVE_PRETTY
|
2018-02-04 22:54:08 +00:00
|
|
|
cursor.get_kind() != CXCursor_EnumConstantDecl
|
|
|
|
? param->PrettyPrintCursor(cursor.cx_cursor)
|
|
|
|
:
|
|
|
|
#endif
|
2018-04-06 00:00:07 +00:00
|
|
|
std::string qualified_name;
|
|
|
|
std::tie(qualified_name, def.short_name_offset, def.short_name_size) =
|
|
|
|
param->ns.QualifiedName(semanticContainer, short_name);
|
2018-01-17 03:09:29 +00:00
|
|
|
|
2018-01-18 10:59:51 +00:00
|
|
|
if (cursor.get_kind() == CXCursor_EnumConstantDecl && semanticContainer) {
|
2018-05-09 05:01:58 +00:00
|
|
|
CXTypeKind k = clang_getCanonicalType(
|
|
|
|
clang_getEnumDeclIntegerType(semanticContainer->cursor))
|
|
|
|
.kind;
|
2018-01-17 04:01:47 +00:00
|
|
|
std::string hover = qualified_name + " = ";
|
2018-05-09 05:01:58 +00:00
|
|
|
if (k == CXType_Char_U || k == CXType_UChar || k == CXType_UShort ||
|
|
|
|
k == CXType_UInt || k == CXType_ULong || k == CXType_ULongLong)
|
2018-01-17 04:01:47 +00:00
|
|
|
hover += std::to_string(
|
|
|
|
clang_getEnumConstantDeclUnsignedValue(cursor.cx_cursor));
|
2018-02-01 20:21:58 +00:00
|
|
|
else
|
|
|
|
hover += std::to_string(clang_getEnumConstantDeclValue(cursor.cx_cursor));
|
2018-01-03 07:54:04 +00:00
|
|
|
def.detailed_name = std::move(qualified_name);
|
2018-04-08 17:32:08 +00:00
|
|
|
def.qual_name_offset = 0;
|
2018-01-17 04:30:45 +00:00
|
|
|
def.hover = hover;
|
2018-01-17 04:01:47 +00:00
|
|
|
} else {
|
2018-02-13 03:13:21 +00:00
|
|
|
#if 0 && CINDEX_HAVE_PRETTY
|
|
|
|
//def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor, false);
|
2018-02-04 22:54:08 +00:00
|
|
|
#else
|
2018-04-06 00:00:07 +00:00
|
|
|
int offset = type_name.size();
|
|
|
|
offset += ConcatTypeAndName(type_name, qualified_name);
|
2018-04-08 17:32:08 +00:00
|
|
|
def.detailed_name = type_name;
|
2018-04-06 00:00:07 +00:00
|
|
|
def.qual_name_offset = offset;
|
|
|
|
def.short_name_offset += offset;
|
2018-01-18 05:02:09 +00:00
|
|
|
// Append the textual initializer, bit field, constructor to |hover|.
|
|
|
|
// Omit |hover| for these types:
|
|
|
|
// int (*a)(); int (&a)(); int (&&a)(); int a[1]; auto x = ...
|
|
|
|
// We can take these into consideration after we have better support for
|
|
|
|
// inside-out syntax.
|
2018-01-17 08:28:32 +00:00
|
|
|
CXType deref = cx_type;
|
2018-01-18 05:02:09 +00:00
|
|
|
while (deref.kind == CXType_Pointer || deref.kind == CXType_MemberPointer ||
|
|
|
|
deref.kind == CXType_LValueReference ||
|
|
|
|
deref.kind == CXType_RValueReference)
|
2018-01-17 08:28:32 +00:00
|
|
|
deref = clang_getPointeeType(deref);
|
2018-01-18 05:02:09 +00:00
|
|
|
if (deref.kind != CXType_Unexposed && deref.kind != CXType_Auto &&
|
2018-01-17 08:28:32 +00:00
|
|
|
clang_getResultType(deref).kind == CXType_Invalid &&
|
2018-01-17 08:17:04 +00:00
|
|
|
clang_getElementType(deref).kind == CXType_Invalid) {
|
|
|
|
const FileContents& fc = param->file_contents[db->path];
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<int> spell_end = fc.ToOffset(cursor.get_spell().end);
|
|
|
|
std::optional<int> extent_end = fc.ToOffset(cursor.get_extent().end);
|
2018-01-17 08:17:04 +00:00
|
|
|
if (extent_end && *spell_end < *extent_end)
|
2018-02-11 05:36:15 +00:00
|
|
|
def.hover = std::string(def.detailed_name.c_str()) +
|
2018-01-30 00:27:43 +00:00
|
|
|
fc.content.substr(*spell_end, *extent_end - *spell_end);
|
2018-01-17 08:17:04 +00:00
|
|
|
}
|
2018-02-04 22:54:08 +00:00
|
|
|
#endif
|
2018-01-03 07:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_first_seen) {
|
2018-04-30 04:49:03 +00:00
|
|
|
if (IndexType* var_type =
|
2018-04-15 17:21:26 +00:00
|
|
|
ResolveToDeclarationType(db, cursor, param)) {
|
2018-01-03 07:54:04 +00:00
|
|
|
// Don't treat enum definition variables as instantiations.
|
|
|
|
bool is_enum_member = semanticContainer &&
|
|
|
|
semanticContainer->cursor.kind == CXCursor_EnumDecl;
|
|
|
|
if (!is_enum_member)
|
2018-04-30 04:49:03 +00:00
|
|
|
var_type->instances.push_back(var.usr);
|
2018-01-03 07:54:04 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
def.type = var_type->usr;
|
2018-01-03 07:54:04 +00:00
|
|
|
}
|
2018-01-03 06:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 18:27:17 +00:00
|
|
|
void OnIndexReference_Function(IndexFile* db,
|
2017-12-30 23:33:58 +00:00
|
|
|
Range loc,
|
2018-02-10 20:53:18 +00:00
|
|
|
ClangCursor parent_cursor,
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& called,
|
2018-02-11 04:01:10 +00:00
|
|
|
Role role) {
|
2018-02-10 20:53:18 +00:00
|
|
|
switch (GetSymbolKind(parent_cursor.get_kind())) {
|
|
|
|
case SymbolKind::Func: {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& parent = db->ToFunc(parent_cursor.cx_cursor);
|
|
|
|
parent.def.callees.push_back(
|
|
|
|
SymbolRef{loc, called.usr, SymbolKind::Func, role});
|
|
|
|
called.uses.push_back(Use{{loc, parent.usr, SymbolKind::Func, role}});
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Type: {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& parent = db->ToType(parent_cursor.cx_cursor);
|
|
|
|
called.uses.push_back(Use{{loc, parent.usr, SymbolKind::Type, role}});
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-04-30 04:49:03 +00:00
|
|
|
called.uses.push_back(Use{{loc, 0, SymbolKind::File, role}});
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-24 08:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-06-15 03:12:52 +00:00
|
|
|
// static
|
2018-02-26 02:45:46 +00:00
|
|
|
const int IndexFile::kMajorVersion = 15;
|
2018-02-12 04:22:47 +00:00
|
|
|
const int IndexFile::kMinorVersion = 0;
|
2017-05-20 21:45:46 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
IndexFile::IndexFile(const std::string& path, const std::string& contents)
|
2018-04-30 04:49:03 +00:00
|
|
|
: path(path), file_contents(contents) {}
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& IndexFile::ToFunc(Usr usr) {
|
|
|
|
auto ret = usr2func.try_emplace(usr);
|
|
|
|
if (ret.second)
|
|
|
|
ret.first->second.usr = usr;
|
|
|
|
return ret.first->second;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& IndexFile::ToType(Usr usr) {
|
|
|
|
auto ret = usr2type.try_emplace(usr);
|
|
|
|
if (ret.second)
|
|
|
|
ret.first->second.usr = usr;
|
|
|
|
return ret.first->second;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar& IndexFile::ToVar(Usr usr) {
|
|
|
|
auto ret = usr2var.try_emplace(usr);
|
|
|
|
if (ret.second)
|
|
|
|
ret.first->second.usr = usr;
|
|
|
|
return ret.first->second;
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::string IndexFile::ToString() {
|
2018-01-07 02:01:32 +00:00
|
|
|
return Serialize(SerializeFormat::Json, *this);
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
void Uniquify(std::vector<Usr>& usrs) {
|
2018-04-30 04:49:03 +00:00
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-23 18:14:54 +00:00
|
|
|
size_t n = 0;
|
2018-05-05 03:40:52 +00:00
|
|
|
for (size_t i = 0; i < usrs.size(); i++)
|
|
|
|
if (seen.insert(usrs[i]).second)
|
|
|
|
usrs[n++] = usrs[i];
|
|
|
|
usrs.resize(n);
|
2017-04-13 07:47:47 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 18:14:54 +00:00
|
|
|
void Uniquify(std::vector<Use>& uses) {
|
2018-05-05 03:40:52 +00:00
|
|
|
std::unordered_set<Range> seen;
|
2018-02-23 18:14:54 +00:00
|
|
|
size_t n = 0;
|
2018-04-08 06:32:35 +00:00
|
|
|
for (size_t i = 0; i < uses.size(); i++) {
|
2018-05-05 03:40:52 +00:00
|
|
|
if (seen.insert(uses[i].range).second)
|
2018-02-23 18:14:54 +00:00
|
|
|
uses[n++] = uses[i];
|
2018-04-08 06:32:35 +00:00
|
|
|
}
|
2018-02-23 18:14:54 +00:00
|
|
|
uses.resize(n);
|
2017-02-24 08:39:25 +00:00
|
|
|
}
|
2017-02-20 06:40:55 +00:00
|
|
|
|
2018-02-10 20:53:18 +00:00
|
|
|
void AddUse(IndexFile* db,
|
|
|
|
std::vector<Use>& uses,
|
|
|
|
Range range,
|
|
|
|
ClangCursor parent,
|
2018-02-11 04:01:10 +00:00
|
|
|
Role role = Role::Reference) {
|
2018-02-10 20:53:18 +00:00
|
|
|
switch (GetSymbolKind(parent.get_kind())) {
|
|
|
|
case SymbolKind::Func:
|
2018-04-30 04:49:03 +00:00
|
|
|
uses.push_back(Use{
|
|
|
|
{range, db->ToFunc(parent.cx_cursor).usr, SymbolKind::Func, role}});
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
2018-04-30 04:49:03 +00:00
|
|
|
uses.push_back(Use{
|
|
|
|
{range, db->ToType(parent.cx_cursor).usr, SymbolKind::Type, role}});
|
2018-02-18 18:07:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-04-30 04:49:03 +00:00
|
|
|
uses.push_back(Use{{range, 0, SymbolKind::File, role}});
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CXCursor fromContainer(const CXIdxContainerInfo* parent) {
|
|
|
|
return parent ? parent->cursor : clang_getNullCursor();
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:34:32 +00:00
|
|
|
void AddUseSpell(IndexFile* db, std::vector<Use>& uses, ClangCursor cursor) {
|
2018-02-23 18:14:54 +00:00
|
|
|
AddUse(db, uses, cursor.get_spell(), cursor.get_lexical_parent().cx_cursor);
|
2018-02-08 18:38:27 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:57:03 +00:00
|
|
|
void OnIndexDiagnostic(CXClientData client_data,
|
|
|
|
CXDiagnosticSet diagnostics,
|
|
|
|
void* reserved) {
|
2017-06-20 01:52:25 +00:00
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < clang_getNumDiagnosticsInSet(diagnostics); ++i) {
|
|
|
|
CXDiagnostic diagnostic = clang_getDiagnosticInSet(diagnostics, i);
|
2017-07-12 22:02:48 +00:00
|
|
|
|
2017-06-20 01:52:25 +00:00
|
|
|
CXSourceLocation diag_loc = clang_getDiagnosticLocation(diagnostic);
|
2018-01-06 17:28:55 +00:00
|
|
|
// Skip diagnostics in system headers.
|
|
|
|
// if (clang_Location_isInSystemHeader(diag_loc))
|
|
|
|
// continue;
|
2017-06-20 01:52:25 +00:00
|
|
|
|
|
|
|
// Get db so we can attribute diagnostic to the right indexed file.
|
|
|
|
CXFile file;
|
|
|
|
unsigned int line, column;
|
|
|
|
clang_getSpellingLocation(diag_loc, &file, &line, &column, nullptr);
|
2018-01-06 17:28:55 +00:00
|
|
|
// Skip empty diagnostic.
|
2018-01-11 02:43:01 +00:00
|
|
|
if (!line && !column)
|
|
|
|
continue;
|
2017-06-20 01:52:25 +00:00
|
|
|
IndexFile* db = ConsumeFile(param, file);
|
|
|
|
if (!db)
|
|
|
|
continue;
|
2017-07-12 22:02:48 +00:00
|
|
|
|
2017-06-20 01:52:25 +00:00
|
|
|
// Build diagnostic.
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<lsDiagnostic> ls_diagnostic =
|
2017-07-12 22:02:48 +00:00
|
|
|
BuildAndDisposeDiagnostic(diagnostic, db->path);
|
2017-06-20 01:52:25 +00:00
|
|
|
if (ls_diagnostic)
|
|
|
|
db->diagnostics_.push_back(*ls_diagnostic);
|
|
|
|
}
|
2017-03-11 02:24:51 +00:00
|
|
|
}
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-12-01 17:57:03 +00:00
|
|
|
CXIdxClientFile OnIndexIncludedFile(CXClientData client_data,
|
|
|
|
const CXIdxIncludedFileInfo* file) {
|
2017-05-21 03:46:15 +00:00
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
|
|
|
|
|
|
|
// file->hashLoc only has the position of the hash. We don't have the full
|
|
|
|
// range for the include.
|
|
|
|
CXSourceLocation hash_loc = clang_indexLoc_getCXSourceLocation(file->hashLoc);
|
|
|
|
CXFile cx_file;
|
|
|
|
unsigned int line;
|
|
|
|
clang_getSpellingLocation(hash_loc, &cx_file, &line, nullptr, nullptr);
|
2018-01-17 20:52:37 +00:00
|
|
|
line--;
|
2017-05-21 03:46:15 +00:00
|
|
|
|
|
|
|
IndexFile* db = ConsumeFile(param, cx_file);
|
|
|
|
if (!db)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
IndexInclude include;
|
|
|
|
include.line = line;
|
|
|
|
include.resolved_path = FileName(file->file);
|
2018-03-31 20:59:27 +00:00
|
|
|
if (include.resolved_path.size())
|
2017-12-16 05:24:31 +00:00
|
|
|
db->includes.push_back(include);
|
2017-05-21 03:46:15 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:00:58 +00:00
|
|
|
struct FindChildOfKindParam {
|
|
|
|
CXCursorKind target_kind;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<ClangCursor> result;
|
2017-02-20 02:00:58 +00:00
|
|
|
|
|
|
|
FindChildOfKindParam(CXCursorKind target_kind) : target_kind(target_kind) {}
|
|
|
|
};
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<ClangCursor>* result) {
|
2017-02-20 19:08:27 +00:00
|
|
|
switch (cursor.get_kind()) {
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXCursor_TypeRef:
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
*result = cursor;
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Break;
|
2017-05-21 23:48:21 +00:00
|
|
|
default:
|
|
|
|
break;
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Recurse;
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<ClangCursor> FindType(ClangCursor cursor) {
|
|
|
|
std::optional<ClangCursor> result;
|
2017-02-20 19:08:27 +00:00
|
|
|
cursor.VisitChildren(&FindTypeVisitor, &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
bool IsTypeDefinition(const CXIdxContainerInfo* container) {
|
|
|
|
if (!container)
|
|
|
|
return false;
|
2018-02-10 20:53:18 +00:00
|
|
|
return GetSymbolKind(container->cursor.kind) == SymbolKind::Type;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
struct VisitDeclForTypeUsageParam {
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFile* db;
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* toplevel_type;
|
2017-02-20 21:48:46 +00:00
|
|
|
int has_processed_any = false;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<ClangCursor> previous_cursor;
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* initial_type = nullptr;
|
2017-02-20 21:48:46 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
VisitDeclForTypeUsageParam(IndexFile* db, IndexType* toplevel_type)
|
2018-01-25 20:19:24 +00:00
|
|
|
: db(db), toplevel_type(toplevel_type) {}
|
2017-02-20 21:48:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor,
|
2017-03-17 07:58:41 +00:00
|
|
|
VisitDeclForTypeUsageParam* param) {
|
2017-02-20 21:48:46 +00:00
|
|
|
param->has_processed_any = true;
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFile* db = param->db;
|
2017-02-20 21:48:46 +00:00
|
|
|
|
2018-01-25 20:19:24 +00:00
|
|
|
// For |A<int> a| where there is a specialization for |A<int>|,
|
|
|
|
// the |referenced_usr| below resolves to the primary template and
|
|
|
|
// attributes the use to the primary template instead of the specialization.
|
2018-01-30 00:27:43 +00:00
|
|
|
// |toplevel_type| is retrieved |clang_getCursorType| which can be a
|
|
|
|
// specialization. If its name is the same as the primary template's, we
|
|
|
|
// assume the use should be attributed to the specialization. This heuristic
|
|
|
|
// fails when a member class bears the same name with its container.
|
2018-01-25 20:19:24 +00:00
|
|
|
//
|
|
|
|
// template<class T>
|
|
|
|
// struct C { struct C {}; };
|
|
|
|
// C<int>::C a;
|
|
|
|
//
|
|
|
|
// We will attribute |::C| to the parent class.
|
|
|
|
if (param->toplevel_type) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ref_type = *param->toplevel_type;
|
2018-02-18 18:07:13 +00:00
|
|
|
std::string name = cursor.get_referenced().get_spell_name();
|
2018-04-30 04:49:03 +00:00
|
|
|
if (name == ref_type.def.Name(false)) {
|
|
|
|
AddUseSpell(db, ref_type.uses, cursor);
|
|
|
|
param->toplevel_type = nullptr;
|
2018-01-25 20:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:01:58 +00:00
|
|
|
std::optional<Usr> referenced_usr =
|
2018-01-30 00:27:43 +00:00
|
|
|
cursor.get_referenced()
|
|
|
|
.template_specialization_to_template_definition()
|
2018-05-09 05:01:58 +00:00
|
|
|
.get_opt_usr_hash();
|
|
|
|
// In STL this may be empty.
|
|
|
|
if (!referenced_usr)
|
2017-03-05 23:44:20 +00:00
|
|
|
return;
|
|
|
|
|
2018-05-09 05:01:58 +00:00
|
|
|
IndexType& ref_type = db->ToType(*referenced_usr);
|
2017-03-05 22:49:23 +00:00
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
if (!param->initial_type)
|
2018-04-30 04:49:03 +00:00
|
|
|
param->initial_type = &ref_type;
|
2017-02-20 21:48:46 +00:00
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
// TODO: Should we even be visiting this if the file is not from the main
|
|
|
|
// def? Try adding assert on |loc| later.
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_type.uses, cursor);
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult VisitDeclForTypeUsageVisitor(
|
|
|
|
ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
2017-03-17 07:58:41 +00:00
|
|
|
VisitDeclForTypeUsageParam* param) {
|
2017-02-20 21:48:46 +00:00
|
|
|
switch (cursor.get_kind()) {
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
if (param->previous_cursor) {
|
|
|
|
VisitDeclForTypeUsageVisitorHandler(param->previous_cursor.value(),
|
|
|
|
param);
|
|
|
|
}
|
2017-03-05 23:44:20 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
param->previous_cursor = cursor;
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Continue;
|
2017-03-05 23:44:20 +00:00
|
|
|
|
|
|
|
// We do not want to recurse for everything, since if we do that we will end
|
|
|
|
// up visiting method definition bodies/etc. Instead, we only recurse for
|
2017-03-17 07:58:41 +00:00
|
|
|
// things that can logically appear as part of an inline variable
|
|
|
|
// initializer,
|
2017-03-05 23:44:20 +00:00
|
|
|
// ie,
|
|
|
|
//
|
|
|
|
// class Foo {
|
|
|
|
// int x = (Foo)3;
|
|
|
|
// }
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXCursor_CallExpr:
|
|
|
|
case CXCursor_CStyleCastExpr:
|
|
|
|
case CXCursor_CXXStaticCastExpr:
|
|
|
|
case CXCursor_CXXReinterpretCastExpr:
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Recurse;
|
2017-05-21 23:48:21 +00:00
|
|
|
|
|
|
|
default:
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Continue;
|
2017-02-20 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Continue;
|
2017-02-20 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Add usages to any seen TypeRef or TemplateRef under the given |decl_cursor|.
|
2017-04-03 01:34:15 +00:00
|
|
|
// This returns the first seen TypeRef or TemplateRef value, which can be
|
|
|
|
// useful if trying to figure out ie, what a using statement refers to. If
|
|
|
|
// trying to generally resolve a cursor to a type, use
|
|
|
|
// ResolveToDeclarationType, which works in more scenarios.
|
2018-01-25 20:19:24 +00:00
|
|
|
// If |decl_cursor| is a variable of a template type, clang_getCursorType
|
|
|
|
// may return a specialized template which is preciser than the primary
|
|
|
|
// template.
|
|
|
|
// We use |toplevel_type| to attribute the use to the specialized template
|
|
|
|
// instead of the primary template.
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* AddDeclTypeUsages(IndexFile* db,
|
|
|
|
ClangCursor decl_cursor,
|
|
|
|
IndexType* toplevel_type,
|
|
|
|
const CXIdxContainerInfo* semantic_container,
|
|
|
|
const CXIdxContainerInfo* lexical_container) {
|
2017-02-20 21:48:46 +00:00
|
|
|
//
|
|
|
|
// The general AST format for definitions follows this pattern:
|
|
|
|
//
|
|
|
|
// template<typename A, typename B>
|
|
|
|
// struct Container;
|
|
|
|
//
|
|
|
|
// struct S1;
|
|
|
|
// struct S2;
|
|
|
|
//
|
|
|
|
// Container<Container<S1, S2>, S2> foo;
|
|
|
|
//
|
|
|
|
// =>
|
|
|
|
//
|
|
|
|
// VarDecl
|
|
|
|
// TemplateRef Container
|
|
|
|
// TemplateRef Container
|
|
|
|
// TypeRef struct S1
|
|
|
|
// TypeRef struct S2
|
|
|
|
// TypeRef struct S2
|
|
|
|
//
|
2017-03-05 22:49:23 +00:00
|
|
|
//
|
|
|
|
// Here is another example:
|
|
|
|
//
|
|
|
|
// enum A {};
|
|
|
|
// enum B {};
|
2017-03-14 04:31:53 +00:00
|
|
|
//
|
2017-03-05 22:49:23 +00:00
|
|
|
// template<typename T>
|
|
|
|
// struct Foo {
|
|
|
|
// struct Inner {};
|
|
|
|
// };
|
2017-03-14 04:31:53 +00:00
|
|
|
//
|
2017-03-05 22:49:23 +00:00
|
|
|
// Foo<A>::Inner a;
|
|
|
|
// Foo<B> b;
|
2017-03-14 04:31:53 +00:00
|
|
|
//
|
2017-03-05 22:49:23 +00:00
|
|
|
// =>
|
|
|
|
//
|
|
|
|
// EnumDecl A
|
|
|
|
// EnumDecl B
|
|
|
|
// ClassTemplate Foo
|
|
|
|
// TemplateTypeParameter T
|
|
|
|
// StructDecl Inner
|
|
|
|
// VarDecl a
|
|
|
|
// TemplateRef Foo
|
|
|
|
// TypeRef enum A
|
|
|
|
// TypeRef struct Foo<enum A>::Inner
|
|
|
|
// CallExpr Inner
|
|
|
|
// VarDecl b
|
|
|
|
// TemplateRef Foo
|
|
|
|
// TypeRef enum B
|
|
|
|
// CallExpr Foo
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Determining the actual type of the variable/declaration from just the
|
|
|
|
// children is tricky. Doing so would require looking up the template
|
|
|
|
// definition associated with a TemplateRef, figuring out how many children
|
|
|
|
// it has, and then skipping that many TypeRef values. This also has to work
|
|
|
|
// with the example below (skipping the last TypeRef). As a result, we
|
|
|
|
// determine variable types using |ResolveToDeclarationType|.
|
|
|
|
//
|
|
|
|
//
|
2017-02-20 21:48:46 +00:00
|
|
|
// We skip the last type reference for methods/variables which are defined
|
|
|
|
// out-of-line w.r.t. the parent type.
|
|
|
|
//
|
|
|
|
// S1* Foo::foo() {}
|
2017-02-22 01:06:43 +00:00
|
|
|
//
|
2017-02-20 21:48:46 +00:00
|
|
|
// The above example looks like this in the AST:
|
|
|
|
//
|
|
|
|
// CXXMethod foo
|
|
|
|
// TypeRef struct S1
|
|
|
|
// TypeRef class Foo
|
|
|
|
// CompoundStmt
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// The second TypeRef is an uninteresting usage.
|
|
|
|
bool process_last_type_ref = true;
|
2017-03-17 07:58:41 +00:00
|
|
|
if (IsTypeDefinition(semantic_container) &&
|
|
|
|
!IsTypeDefinition(lexical_container)) {
|
2017-03-06 01:09:08 +00:00
|
|
|
//
|
2017-03-17 07:58:41 +00:00
|
|
|
// In some code, such as the following example, we receive a cursor which is
|
|
|
|
// not
|
|
|
|
// a definition and is not associated with a definition due to an error
|
|
|
|
// condition.
|
2017-03-06 01:09:08 +00:00
|
|
|
// In this case, it is the Foo::Foo constructor.
|
|
|
|
//
|
|
|
|
// struct Foo {};
|
|
|
|
//
|
|
|
|
// template<class T>
|
|
|
|
// Foo::Foo() {}
|
|
|
|
//
|
|
|
|
if (!decl_cursor.is_definition()) {
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor def = decl_cursor.get_definition();
|
2017-12-02 01:04:39 +00:00
|
|
|
if (def.get_kind() != CXCursor_FirstInvalid)
|
2017-03-06 01:09:08 +00:00
|
|
|
decl_cursor = def;
|
|
|
|
}
|
2017-02-20 21:48:46 +00:00
|
|
|
process_last_type_ref = false;
|
|
|
|
}
|
2017-02-20 19:08:27 +00:00
|
|
|
|
2018-01-25 20:19:24 +00:00
|
|
|
VisitDeclForTypeUsageParam param(db, toplevel_type);
|
2017-02-20 21:48:46 +00:00
|
|
|
decl_cursor.VisitChildren(&VisitDeclForTypeUsageVisitor, ¶m);
|
|
|
|
|
|
|
|
// VisitDeclForTypeUsageVisitor guarantees that if there are multiple TypeRef
|
|
|
|
// children, the first one will always be visited.
|
|
|
|
if (param.previous_cursor && process_last_type_ref) {
|
|
|
|
VisitDeclForTypeUsageVisitorHandler(param.previous_cursor.value(), ¶m);
|
2017-03-17 07:58:41 +00:00
|
|
|
} else {
|
2017-03-06 06:23:41 +00:00
|
|
|
// If we are not processing the last type ref, it *must* be a TypeRef or
|
|
|
|
// TemplateRef.
|
2017-03-05 22:49:23 +00:00
|
|
|
//
|
2017-03-17 07:58:41 +00:00
|
|
|
// We will not visit every child if the is_interseting is false, so
|
|
|
|
// previous_cursor
|
2017-03-05 22:49:23 +00:00
|
|
|
// may not point to the last TemplateRef.
|
2017-04-19 05:28:33 +00:00
|
|
|
assert(param.previous_cursor.has_value() == false ||
|
2017-03-17 07:58:41 +00:00
|
|
|
(param.previous_cursor.value().get_kind() == CXCursor_TypeRef ||
|
|
|
|
param.previous_cursor.value().get_kind() == CXCursor_TemplateRef));
|
2017-02-20 21:48:46 +00:00
|
|
|
}
|
2017-02-20 19:08:27 +00:00
|
|
|
|
2018-02-26 08:07:01 +00:00
|
|
|
if (param.initial_type)
|
|
|
|
return param.initial_type;
|
|
|
|
CXType cx_under = clang_getTypedefDeclUnderlyingType(decl_cursor.cx_cursor);
|
|
|
|
if (cx_under.kind == CXType_Invalid)
|
2018-04-30 04:49:03 +00:00
|
|
|
return nullptr;
|
|
|
|
return &db->ToType(ClangType(cx_under).strip_qualifiers().get_usr_hash());
|
2017-02-20 21:48:46 +00:00
|
|
|
}
|
2017-02-20 19:08:27 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Various versions of LLVM (ie, 4.0) will not visit inline variable references
|
|
|
|
// for template arguments.
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult AddDeclInitializerUsagesVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
IndexFile* db) {
|
2017-03-14 04:31:53 +00:00
|
|
|
/*
|
2017-03-17 07:58:41 +00:00
|
|
|
We need to index the |DeclRefExpr| below (ie, |var| inside of
|
|
|
|
Foo<int>::var).
|
2017-03-14 04:31:53 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Foo {
|
|
|
|
static constexpr int var = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
int a = Foo<int>::var;
|
|
|
|
|
|
|
|
=>
|
|
|
|
|
|
|
|
VarDecl a
|
|
|
|
UnexposedExpr var
|
|
|
|
DeclRefExpr var
|
|
|
|
TemplateRef Foo
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (cursor.get_kind()) {
|
2017-05-21 23:48:21 +00:00
|
|
|
case CXCursor_DeclRefExpr: {
|
2017-03-17 07:58:41 +00:00
|
|
|
if (cursor.get_referenced().get_kind() != CXCursor_VarDecl)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: when we resolve the template type to the definition, we get a
|
2018-01-13 08:10:39 +00:00
|
|
|
// different Usr.
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
// ClangCursor ref =
|
2018-01-13 06:13:08 +00:00
|
|
|
// cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr_hash();
|
2017-03-17 07:58:41 +00:00
|
|
|
// std::string ref_usr =
|
2018-01-13 06:13:08 +00:00
|
|
|
// cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr_hash();
|
|
|
|
auto ref_usr = cursor.get_referenced()
|
|
|
|
.template_specialization_to_template_definition()
|
2018-05-09 05:01:58 +00:00
|
|
|
.get_opt_usr_hash();
|
|
|
|
if (!ref_usr)
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2018-05-09 05:01:58 +00:00
|
|
|
IndexVar& ref_var = db->ToVar(*ref_usr);
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_var.uses, cursor);
|
2017-03-14 04:31:53 +00:00
|
|
|
break;
|
2017-05-21 23:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-03-14 04:31:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Recurse;
|
2017-03-14 04:31:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult VisitMacroDefinitionAndExpansions(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
IndexParam* param) {
|
2017-05-21 01:58:54 +00:00
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
case CXCursor_MacroDefinition:
|
2017-05-21 23:48:21 +00:00
|
|
|
case CXCursor_MacroExpansion: {
|
2017-05-21 01:58:54 +00:00
|
|
|
// Resolve location, find IndexFile instance.
|
2017-09-22 01:14:57 +00:00
|
|
|
CXSourceRange cx_source_range =
|
|
|
|
clang_Cursor_getSpellingNameRange(cursor.cx_cursor, 0, 0);
|
2017-05-21 01:58:54 +00:00
|
|
|
CXFile file;
|
2018-01-01 07:56:35 +00:00
|
|
|
Range decl_loc_spelling = ResolveCXSourceRange(cx_source_range, &file);
|
2017-05-21 01:58:54 +00:00
|
|
|
IndexFile* db = ConsumeFile(param, file);
|
|
|
|
if (!db)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: Considering checking clang_Cursor_isMacroFunctionLike, but the
|
|
|
|
// only real difference will be that we show 'callers' instead of 'refs'
|
|
|
|
// (especially since macros cannot have overrides)
|
|
|
|
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr decl_usr;
|
2017-05-21 01:58:54 +00:00
|
|
|
if (cursor.get_kind() == CXCursor_MacroDefinition)
|
2018-01-13 06:13:08 +00:00
|
|
|
decl_usr = cursor.get_usr_hash();
|
2017-05-21 01:58:54 +00:00
|
|
|
else
|
2018-01-13 06:13:08 +00:00
|
|
|
decl_usr = cursor.get_referenced().get_usr_hash();
|
2017-05-21 01:58:54 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar& var_def = db->ToVar(decl_usr);
|
2017-05-21 01:58:54 +00:00
|
|
|
if (cursor.get_kind() == CXCursor_MacroDefinition) {
|
2017-12-13 08:22:02 +00:00
|
|
|
CXSourceRange cx_extent = clang_getCursorExtent(cursor.cx_cursor);
|
2018-04-30 04:49:03 +00:00
|
|
|
var_def.def.detailed_name = cursor.get_display_name();
|
|
|
|
var_def.def.qual_name_offset = 0;
|
|
|
|
var_def.def.short_name_offset = 0;
|
|
|
|
var_def.def.short_name_size =
|
|
|
|
int16_t(strlen(var_def.def.detailed_name.c_str()));
|
|
|
|
var_def.def.hover =
|
2017-12-19 05:20:00 +00:00
|
|
|
"#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent);
|
2018-04-30 04:49:03 +00:00
|
|
|
var_def.def.kind = lsSymbolKind::Macro;
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.comments)
|
2018-04-30 04:49:03 +00:00
|
|
|
var_def.def.comments = cursor.get_comments();
|
|
|
|
var_def.def.spell =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, decl_loc_spelling, parent, Role::Definition);
|
2018-04-30 04:49:03 +00:00
|
|
|
var_def.def.extent = SetUse(
|
2018-02-18 18:07:13 +00:00
|
|
|
db, ResolveCXSourceRange(cx_extent, nullptr), parent, Role::None);
|
2018-01-28 19:42:40 +00:00
|
|
|
} else
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, var_def.uses, decl_loc_spelling, parent);
|
2017-04-11 05:26:27 +00:00
|
|
|
|
2017-05-21 23:48:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2017-05-21 01:58:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Continue;
|
2017-05-21 01:58:54 +00:00
|
|
|
}
|
2017-04-11 05:26:27 +00:00
|
|
|
|
2017-12-24 18:27:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-12-28 18:53:13 +00:00
|
|
|
// TODO Move to another file and use clang C++ API
|
2017-12-24 18:27:17 +00:00
|
|
|
struct TemplateVisitorData {
|
|
|
|
IndexFile* db;
|
2018-01-03 06:52:35 +00:00
|
|
|
IndexParam* param;
|
2017-12-24 18:27:17 +00:00
|
|
|
ClangCursor container;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
TemplateVisitorData* data) {
|
2018-01-07 08:36:12 +00:00
|
|
|
IndexFile* db = data->db;
|
2018-02-04 22:54:08 +00:00
|
|
|
IndexParam* param = data->param;
|
2017-12-24 18:27:17 +00:00
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
default:
|
2017-12-24 21:28:53 +00:00
|
|
|
break;
|
2017-12-28 07:42:56 +00:00
|
|
|
case CXCursor_DeclRefExpr: {
|
|
|
|
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
|
|
|
|
if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar& ref_var = db->ToVar(ref_cursor);
|
|
|
|
if (ref_var.def.detailed_name.empty()) {
|
2018-02-18 18:07:13 +00:00
|
|
|
ClangCursor sem_parent = ref_cursor.get_semantic_parent();
|
|
|
|
ClangCursor lex_parent = ref_cursor.get_lexical_parent();
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_var.def.spell =
|
2018-02-22 07:34:32 +00:00
|
|
|
SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_var.def.extent =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_var.def.kind = lsSymbolKind::TypeParameter;
|
2018-02-18 18:07:13 +00:00
|
|
|
SetVarDetail(ref_var, ref_cursor.get_spell_name(), ref_cursor,
|
2018-02-04 22:54:08 +00:00
|
|
|
nullptr, true, db, param);
|
2018-01-07 08:36:12 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
ClangType ref_type_c = clang_getCursorType(ref_cursor.cx_cursor);
|
2018-01-13 06:13:08 +00:00
|
|
|
// TODO optimize
|
2018-04-30 04:49:03 +00:00
|
|
|
if (ref_type_c.get_usr().size()) {
|
|
|
|
IndexType& ref_type = db->ToType(ref_type_c.get_usr_hash());
|
2018-01-07 18:53:44 +00:00
|
|
|
// The cursor extent includes `type name`, not just `name`. There
|
|
|
|
// seems no way to extract the spelling range of `type` and we do
|
|
|
|
// not want to do subtraction here.
|
2018-03-31 03:16:33 +00:00
|
|
|
// See https://github.com/cquery-project/cquery/issues/252
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, ref_type.uses, ref_cursor.get_extent(),
|
2018-02-23 18:14:54 +00:00
|
|
|
ref_cursor.get_lexical_parent());
|
2018-01-07 09:19:57 +00:00
|
|
|
}
|
2017-12-28 08:37:10 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_var.uses, cursor);
|
2018-01-01 06:54:52 +00:00
|
|
|
}
|
2017-12-28 07:42:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-24 18:27:17 +00:00
|
|
|
case CXCursor_OverloadedDeclRef: {
|
|
|
|
unsigned num_overloaded = clang_getNumOverloadedDecls(cursor.cx_cursor);
|
|
|
|
for (unsigned i = 0; i != num_overloaded; i++) {
|
|
|
|
ClangCursor overloaded = clang_getOverloadedDecl(cursor.cx_cursor, i);
|
|
|
|
switch (overloaded.get_kind()) {
|
|
|
|
default:
|
|
|
|
break;
|
2017-12-24 22:23:58 +00:00
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
case CXCursor_FunctionTemplate: {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& called = db->ToFunc(overloaded.get_usr_hash());
|
2018-02-22 07:34:32 +00:00
|
|
|
OnIndexReference_Function(db, cursor.get_spell(), data->container,
|
2018-04-30 04:49:03 +00:00
|
|
|
called, Role::Call);
|
2017-12-24 18:27:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-24 21:28:53 +00:00
|
|
|
break;
|
2017-12-24 18:27:17 +00:00
|
|
|
}
|
2018-01-01 06:54:52 +00:00
|
|
|
case CXCursor_TemplateRef: {
|
|
|
|
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
|
|
|
|
if (ref_cursor.get_kind() == CXCursor_TemplateTemplateParameter) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ref_type = db->ToType(ref_cursor);
|
2018-01-01 06:54:52 +00:00
|
|
|
// TODO It seems difficult to get references to template template
|
|
|
|
// parameters.
|
|
|
|
// CXCursor_TemplateTemplateParameter can be visited by visiting
|
|
|
|
// CXCursor_TranslationUnit, but not (confirm this) by visiting
|
|
|
|
// {Class,Function}Template. Thus we need to initialize it here.
|
2018-04-30 04:49:03 +00:00
|
|
|
if (ref_type.def.detailed_name.empty()) {
|
2018-02-18 18:07:13 +00:00
|
|
|
ClangCursor sem_parent = ref_cursor.get_semantic_parent();
|
|
|
|
ClangCursor lex_parent = ref_cursor.get_lexical_parent();
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.spell =
|
2018-02-22 07:34:32 +00:00
|
|
|
SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.extent =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
|
2018-02-22 07:34:32 +00:00
|
|
|
#if 0 && CINDEX_HAVE_PRETTY
|
2018-02-15 04:58:42 +00:00
|
|
|
ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
|
2018-02-04 22:54:08 +00:00
|
|
|
#else
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.detailed_name = ref_cursor.get_spell_name();
|
2018-02-04 22:54:08 +00:00
|
|
|
#endif
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.short_name_offset = 0;
|
|
|
|
ref_type.def.short_name_size =
|
|
|
|
int16_t(strlen(ref_type.def.detailed_name.c_str()));
|
|
|
|
ref_type.def.kind = lsSymbolKind::TypeParameter;
|
2018-01-01 06:54:52 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_type.uses, cursor);
|
2018-01-01 06:54:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-12-28 06:58:01 +00:00
|
|
|
case CXCursor_TypeRef: {
|
|
|
|
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
|
|
|
|
if (ref_cursor.get_kind() == CXCursor_TemplateTypeParameter) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ref_type = db->ToType(ref_cursor);
|
2017-12-28 17:22:26 +00:00
|
|
|
// TODO It seems difficult to get a FunctionTemplate's template
|
|
|
|
// parameters.
|
2017-12-28 08:37:10 +00:00
|
|
|
// CXCursor_TemplateTypeParameter can be visited by visiting
|
|
|
|
// CXCursor_TranslationUnit, but not (confirm this) by visiting
|
2018-01-01 06:54:52 +00:00
|
|
|
// {Class,Function}Template. Thus we need to initialize it here.
|
2018-04-30 04:49:03 +00:00
|
|
|
if (ref_type.def.detailed_name.empty()) {
|
2018-02-18 18:07:13 +00:00
|
|
|
ClangCursor sem_parent = ref_cursor.get_semantic_parent();
|
|
|
|
ClangCursor lex_parent = ref_cursor.get_lexical_parent();
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.spell =
|
2018-02-22 07:34:32 +00:00
|
|
|
SetUse(db, ref_cursor.get_spell(), sem_parent, Role::Definition);
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.extent =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, ref_cursor.get_extent(), lex_parent, Role::None);
|
2018-02-22 07:34:32 +00:00
|
|
|
#if 0 && CINDEX_HAVE_PRETTY
|
2018-02-21 01:50:48 +00:00
|
|
|
// template<class T> void f(T t){} // weird, the name is empty
|
2018-02-15 04:58:42 +00:00
|
|
|
ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
|
2018-02-04 22:54:08 +00:00
|
|
|
#else
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.detailed_name = ref_cursor.get_spell_name();
|
2018-02-04 22:54:08 +00:00
|
|
|
#endif
|
2018-04-30 04:49:03 +00:00
|
|
|
ref_type.def.short_name_offset = 0;
|
|
|
|
ref_type.def.short_name_size =
|
|
|
|
int16_t(strlen(ref_type.def.detailed_name.c_str()));
|
|
|
|
ref_type.def.kind = lsSymbolKind::TypeParameter;
|
2017-12-28 08:37:10 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_type.uses, cursor);
|
2018-01-01 06:54:52 +00:00
|
|
|
}
|
2017-12-28 06:58:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-24 18:27:17 +00:00
|
|
|
}
|
2018-01-01 06:54:52 +00:00
|
|
|
return ClangCursor::VisitResult::Recurse;
|
2017-12-24 18:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 09:19:02 +00:00
|
|
|
} // namespace
|
2017-12-27 23:49:26 +00:00
|
|
|
|
2018-04-06 00:00:07 +00:00
|
|
|
std::tuple<std::string, int16_t, int16_t> NamespaceHelper::QualifiedName(
|
2018-03-31 16:25:58 +00:00
|
|
|
const CXIdxContainerInfo* container,
|
|
|
|
std::string_view unqualified_name) {
|
2017-12-29 09:19:02 +00:00
|
|
|
if (!container)
|
2018-04-06 00:00:07 +00:00
|
|
|
return {std::string(unqualified_name), 0, 0};
|
2017-12-29 09:19:02 +00:00
|
|
|
// Anonymous namespaces are not processed by indexDeclaration. We trace
|
|
|
|
// nested namespaces bottom-up through clang_getCursorSemanticParent until
|
|
|
|
// one that we know its qualified name. Then do another trace top-down and
|
|
|
|
// put their names into a map of USR -> qualified_name.
|
2018-01-26 06:20:28 +00:00
|
|
|
ClangCursor cursor(container->cursor);
|
2017-12-29 09:19:02 +00:00
|
|
|
std::vector<ClangCursor> namespaces;
|
|
|
|
std::string qualifier;
|
|
|
|
while (cursor.get_kind() != CXCursor_TranslationUnit &&
|
2018-03-02 18:18:01 +00:00
|
|
|
GetSymbolKind(cursor.get_kind()) == SymbolKind::Type) {
|
2018-05-09 05:01:58 +00:00
|
|
|
auto it = usr2qualified_name.find(cursor.get_usr_hash());
|
|
|
|
if (it != usr2qualified_name.end()) {
|
2017-12-29 09:19:02 +00:00
|
|
|
qualifier = it->second;
|
2017-12-27 23:49:26 +00:00
|
|
|
break;
|
2017-12-28 02:42:08 +00:00
|
|
|
}
|
2017-12-29 09:19:02 +00:00
|
|
|
namespaces.push_back(cursor);
|
|
|
|
cursor = clang_getCursorSemanticParent(cursor.cx_cursor);
|
2017-12-27 23:49:26 +00:00
|
|
|
}
|
2017-12-29 09:19:02 +00:00
|
|
|
for (size_t i = namespaces.size(); i > 0;) {
|
|
|
|
i--;
|
2018-02-18 18:07:13 +00:00
|
|
|
std::string name = namespaces[i].get_spell_name();
|
2017-12-29 09:19:02 +00:00
|
|
|
// Empty name indicates unnamed namespace, anonymous struct, anonymous
|
|
|
|
// union, ...
|
|
|
|
if (name.size())
|
|
|
|
qualifier += name;
|
|
|
|
else
|
2018-02-27 04:31:08 +00:00
|
|
|
qualifier += GetAnonName(namespaces[i].get_kind());
|
2017-12-29 09:19:02 +00:00
|
|
|
qualifier += "::";
|
2018-05-09 05:01:58 +00:00
|
|
|
usr2qualified_name[namespaces[i].get_usr_hash()] = qualifier;
|
2017-12-27 23:49:26 +00:00
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
int16_t pos = qualifier.size();
|
2018-03-31 16:25:58 +00:00
|
|
|
qualifier.append(unqualified_name);
|
2018-04-06 00:00:07 +00:00
|
|
|
return {qualifier, pos, int16_t(unqualified_name.size())};
|
2017-12-27 23:49:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:57:03 +00:00
|
|
|
void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
|
2017-11-09 03:55:13 +00:00
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
|
|
|
|
|
|
|
// Track all constructor declarations, as we may need to use it to manually
|
|
|
|
// associate std::make_unique and the like as constructor invocations.
|
|
|
|
if (decl->entityInfo->kind == CXIdxEntity_CXXConstructor) {
|
|
|
|
param->ctors.NotifyConstructor(decl->cursor);
|
|
|
|
}
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
CXFile file;
|
2017-09-22 01:14:57 +00:00
|
|
|
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(decl->loc),
|
|
|
|
&file, nullptr, nullptr, nullptr);
|
2017-05-20 21:45:46 +00:00
|
|
|
IndexFile* db = ConsumeFile(param, file);
|
2017-04-08 22:54:36 +00:00
|
|
|
if (!db)
|
|
|
|
return;
|
|
|
|
|
2017-11-30 21:56:55 +00:00
|
|
|
// The language of this declaration
|
2018-02-11 04:01:10 +00:00
|
|
|
LanguageId decl_lang = [&decl]() {
|
2017-12-01 17:50:39 +00:00
|
|
|
switch (clang_getCursorLanguage(decl->cursor)) {
|
2017-11-30 21:56:55 +00:00
|
|
|
case CXLanguage_C:
|
|
|
|
return LanguageId::C;
|
|
|
|
case CXLanguage_CPlusPlus:
|
|
|
|
return LanguageId::Cpp;
|
|
|
|
case CXLanguage_ObjC:
|
|
|
|
return LanguageId::ObjC;
|
|
|
|
default:
|
|
|
|
return LanguageId::Unknown;
|
2017-12-01 17:50:39 +00:00
|
|
|
};
|
|
|
|
}();
|
2017-11-30 21:56:55 +00:00
|
|
|
|
|
|
|
// Only update the file language if the new language is "greater" than the old
|
2017-11-30 22:09:29 +00:00
|
|
|
if (decl_lang > db->language) {
|
|
|
|
db->language = decl_lang;
|
2017-11-30 03:47:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 04:58:42 +00:00
|
|
|
ClangCursor sem_parent(fromContainer(decl->semanticContainer));
|
2018-02-11 04:01:10 +00:00
|
|
|
ClangCursor lex_parent(fromContainer(decl->lexicalContainer));
|
2018-02-21 04:26:17 +00:00
|
|
|
ClangCursor cursor = decl->cursor;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
switch (decl->entityInfo->kind) {
|
2018-02-21 04:26:17 +00:00
|
|
|
case CXIdxEntity_Unexposed:
|
|
|
|
LOG_S(INFO) << "CXIdxEntity_Unexposed " << cursor.get_spell_name();
|
|
|
|
break;
|
|
|
|
|
2018-01-24 08:19:42 +00:00
|
|
|
case CXIdxEntity_CXXNamespace: {
|
2018-02-21 04:26:17 +00:00
|
|
|
Range spell = cursor.get_spell();
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ns = db->ToType(HashUsr(decl->entityInfo->USR));
|
|
|
|
ns.def.kind = GetSymbolKind(decl->entityInfo->kind);
|
|
|
|
if (ns.def.detailed_name.empty()) {
|
2018-02-22 07:34:32 +00:00
|
|
|
SetTypeName(ns, cursor, decl->semanticContainer, decl->entityInfo->name,
|
|
|
|
param);
|
2018-04-30 04:49:03 +00:00
|
|
|
ns.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
ns.def.extent =
|
2018-02-21 04:26:17 +00:00
|
|
|
SetUse(db, cursor.get_extent(), lex_parent, Role::None);
|
2018-01-24 08:19:42 +00:00
|
|
|
if (decl->semanticContainer) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& parent = db->ToType(decl->semanticContainer->cursor);
|
|
|
|
parent.derived.push_back(ns.usr);
|
|
|
|
ns.def.bases.push_back(parent.usr);
|
2018-01-24 08:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, ns.uses, spell, lex_parent);
|
2018-01-03 16:05:52 +00:00
|
|
|
break;
|
2018-01-24 08:19:42 +00:00
|
|
|
}
|
2018-01-03 16:05:52 +00:00
|
|
|
|
2018-02-21 04:26:17 +00:00
|
|
|
case CXIdxEntity_CXXNamespaceAlias:
|
|
|
|
assert(false && "CXXNamespaceAlias");
|
|
|
|
break;
|
|
|
|
|
2017-12-18 20:06:13 +00:00
|
|
|
case CXIdxEntity_ObjCProperty:
|
|
|
|
case CXIdxEntity_ObjCIvar:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_EnumConstant:
|
|
|
|
case CXIdxEntity_Field:
|
|
|
|
case CXIdxEntity_Variable:
|
|
|
|
case CXIdxEntity_CXXStaticVariable: {
|
2018-02-18 18:07:13 +00:00
|
|
|
Range spell = cursor.get_spell();
|
2017-03-14 04:31:53 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Do not index implicit template instantiations.
|
2018-02-18 18:07:13 +00:00
|
|
|
if (cursor != cursor.template_specialization_to_template_definition())
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar& var = db->ToVar(HashUsr(decl->entityInfo->USR));
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// TODO: Eventually run with this if. Right now I want to iron out bugs
|
|
|
|
// this may shadow.
|
|
|
|
// TODO: Verify this gets called multiple times
|
|
|
|
// if (!decl->isRedeclaration) {
|
2018-01-31 04:59:31 +00:00
|
|
|
SetVarDetail(var, std::string(decl->entityInfo->name), decl->cursor,
|
|
|
|
decl->semanticContainer, !decl->isRedeclaration, db, param);
|
2017-05-21 01:26:50 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
var.def.kind = GetSymbolKind(decl->entityInfo->kind);
|
|
|
|
if (var.def.kind == lsSymbolKind::Variable &&
|
2018-01-07 16:43:53 +00:00
|
|
|
decl->cursor.kind == CXCursor_ParmDecl)
|
2018-04-30 04:49:03 +00:00
|
|
|
var.def.kind = lsSymbolKind::Parameter;
|
2017-03-06 06:23:41 +00:00
|
|
|
//}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
if (decl->isDefinition) {
|
2018-04-30 04:49:03 +00:00
|
|
|
var.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
var.def.extent =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, cursor.get_extent(), lex_parent, Role::None);
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
2018-04-30 04:49:03 +00:00
|
|
|
var.declarations.push_back(
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, spell, lex_parent, Role::Declaration));
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2018-02-18 18:07:13 +00:00
|
|
|
cursor.VisitChildren(&AddDeclInitializerUsagesVisitor, db);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// Declaring variable type information. Note that we do not insert an
|
|
|
|
// interesting reference for parameter declarations - that is handled when
|
|
|
|
// the function declaration is encountered since we won't receive ParmDecl
|
|
|
|
// declarations for unnamed parameters.
|
2017-04-19 05:28:33 +00:00
|
|
|
// TODO: See if we can remove this function call.
|
2018-04-30 04:49:03 +00:00
|
|
|
AddDeclTypeUsages(
|
|
|
|
db, cursor,
|
|
|
|
var.def.type ? &db->ToType(var.def.type) : nullptr,
|
|
|
|
decl->semanticContainer, decl->lexicalContainer);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
// We don't need to assign declaring type multiple times if this variable
|
|
|
|
// has already been seen.
|
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
if (decl->isDefinition && decl->semanticContainer) {
|
2018-02-10 20:53:18 +00:00
|
|
|
switch (GetSymbolKind(decl->semanticContainer->cursor.kind)) {
|
2018-02-27 01:23:45 +00:00
|
|
|
case SymbolKind::Func: {
|
2018-04-30 04:49:03 +00:00
|
|
|
db->ToFunc(decl->semanticContainer->cursor)
|
|
|
|
.def.vars.push_back(var.usr);
|
2018-02-10 20:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-27 01:23:45 +00:00
|
|
|
case SymbolKind::Type:
|
|
|
|
if (decl->semanticContainer->cursor.kind != CXCursor_EnumDecl) {
|
2018-04-30 04:49:03 +00:00
|
|
|
db->ToType(decl->semanticContainer->cursor)
|
|
|
|
.def.vars.push_back(var.usr);
|
2018-02-27 01:23:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-01-28 04:25:14 +00:00
|
|
|
}
|
2017-02-21 05:16:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-02-21 05:16:45 +00:00
|
|
|
|
2017-12-15 20:14:57 +00:00
|
|
|
case CXIdxEntity_ObjCInstanceMethod:
|
|
|
|
case CXIdxEntity_ObjCClassMethod:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_Function:
|
|
|
|
case CXIdxEntity_CXXConstructor:
|
|
|
|
case CXIdxEntity_CXXDestructor:
|
|
|
|
case CXIdxEntity_CXXInstanceMethod:
|
|
|
|
case CXIdxEntity_CXXStaticMethod:
|
|
|
|
case CXIdxEntity_CXXConversionFunction: {
|
2018-02-28 16:27:14 +00:00
|
|
|
Range spell = cursor.get_spell();
|
|
|
|
Range extent = cursor.get_extent();
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor decl_cursor_resolved =
|
2018-02-28 16:27:14 +00:00
|
|
|
cursor.template_specialization_to_template_definition();
|
|
|
|
bool is_template_specialization = cursor != decl_cursor_resolved;
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& func = db->ToFunc(decl_cursor_resolved);
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.comments)
|
2018-04-30 04:49:03 +00:00
|
|
|
func.def.comments = cursor.get_comments();
|
|
|
|
func.def.kind = GetSymbolKind(decl->entityInfo->kind);
|
|
|
|
func.def.storage =
|
2018-01-26 17:28:29 +00:00
|
|
|
GetStorageClass(clang_Cursor_getStorageClass(decl->cursor));
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// We don't actually need to know the return type, but we need to mark it
|
|
|
|
// as an interesting usage.
|
2018-04-30 04:49:03 +00:00
|
|
|
AddDeclTypeUsages(db, cursor, nullptr, decl->semanticContainer,
|
2017-09-22 01:14:57 +00:00
|
|
|
decl->lexicalContainer);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-27 19:56:39 +00:00
|
|
|
// Add definition or declaration. This is a bit tricky because we treat
|
|
|
|
// template specializations as declarations, even though they are
|
|
|
|
// technically definitions.
|
|
|
|
// TODO: Support multiple function definitions, which is common for
|
|
|
|
// template specializations.
|
|
|
|
if (decl->isDefinition && !is_template_specialization) {
|
2018-02-11 04:01:10 +00:00
|
|
|
// assert(!func->def.spell);
|
|
|
|
// assert(!func->def.extent);
|
2018-04-30 04:49:03 +00:00
|
|
|
func.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
func.def.extent = SetUse(db, extent, lex_parent, Role::None);
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
2018-04-30 04:49:03 +00:00
|
|
|
func.declarations.push_back(
|
|
|
|
SetUse(db, spell, lex_parent, Role::Declaration));
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-27 19:56:39 +00:00
|
|
|
// Emit definition data for the function. We do this even if it isn't a
|
|
|
|
// definition because there can be, for example, interfaces, or a class
|
|
|
|
// declaration that doesn't have a definition yet. If we never end up
|
|
|
|
// indexing the definition, then there will not be any (ie) outline
|
|
|
|
// information.
|
|
|
|
if (!is_template_specialization) {
|
2018-02-04 22:54:08 +00:00
|
|
|
#if CINDEX_HAVE_PRETTY
|
2018-04-30 04:49:03 +00:00
|
|
|
std::tie(func.def.detailed_name, func.def.qual_name_offset,
|
|
|
|
func.def.short_name_offset, func.def.short_name_size) =
|
2018-04-06 00:00:07 +00:00
|
|
|
param->PrettyPrintCursor(decl->cursor, decl->entityInfo->name);
|
2018-02-04 22:54:08 +00:00
|
|
|
#else
|
2018-04-30 04:49:03 +00:00
|
|
|
std::tie(func.def.detailed_name, func.def.qual_name_offset,
|
|
|
|
func.def.short_name_offset, func.def.short_name_size) =
|
|
|
|
GetFunctionSignature(db, ¶m->ns, decl);
|
2018-02-04 22:54:08 +00:00
|
|
|
#endif
|
2017-04-14 05:18:02 +00:00
|
|
|
|
2017-12-24 08:35:38 +00:00
|
|
|
// CXCursor_OverloadedDeclRef in templates are not processed by
|
|
|
|
// OnIndexReference, thus we use TemplateVisitor to collect function
|
|
|
|
// references.
|
|
|
|
if (decl->entityInfo->templateKind == CXIdxEntity_Template) {
|
2017-12-24 18:27:17 +00:00
|
|
|
TemplateVisitorData data;
|
|
|
|
data.db = db;
|
2018-01-17 04:30:45 +00:00
|
|
|
data.param = param;
|
2018-02-28 16:27:14 +00:00
|
|
|
data.container = cursor;
|
|
|
|
cursor.VisitChildren(&TemplateVisitor, &data);
|
2017-12-24 08:35:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Add function usage information. We only want to do it once per
|
|
|
|
// definition/declaration. Do it on definition since there should only
|
2017-05-27 16:57:52 +00:00
|
|
|
// ever be one of those in the entire program.
|
2017-03-17 07:58:41 +00:00
|
|
|
if (IsTypeDefinition(decl->semanticContainer)) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& declaring_type =
|
|
|
|
db->ToType(decl->semanticContainer->cursor);
|
|
|
|
func.def.declaring_type = declaring_type.usr;
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// Mark a type reference at the ctor/dtor location.
|
2018-02-18 21:57:16 +00:00
|
|
|
if (decl->entityInfo->kind == CXIdxEntity_CXXConstructor)
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, declaring_type.uses, spell,
|
2018-02-23 18:14:54 +00:00
|
|
|
fromContainer(decl->lexicalContainer));
|
2017-05-27 19:56:39 +00:00
|
|
|
|
|
|
|
// Add function to declaring type.
|
2018-04-30 04:49:03 +00:00
|
|
|
declaring_type.def.funcs.push_back(func.usr);
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
|
2017-05-27 16:57:52 +00:00
|
|
|
// Process inheritance.
|
|
|
|
if (clang_CXXMethod_isVirtual(decl->cursor)) {
|
|
|
|
CXCursor* overridden;
|
|
|
|
unsigned int num_overridden;
|
|
|
|
clang_getOverriddenCursors(decl->cursor, &overridden,
|
2017-09-22 01:14:57 +00:00
|
|
|
&num_overridden);
|
2017-05-27 16:57:52 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_overridden; ++i) {
|
2017-12-21 07:18:48 +00:00
|
|
|
ClangCursor parent =
|
|
|
|
ClangCursor(overridden[i])
|
|
|
|
.template_specialization_to_template_definition();
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& parent_def = db->ToFunc(parent);
|
|
|
|
func.def.bases.push_back(parent_def.usr);
|
|
|
|
parent_def.derived.push_back(func.usr);
|
2017-03-06 06:23:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
|
2017-05-27 16:57:52 +00:00
|
|
|
clang_disposeOverriddenCursors(overridden);
|
2017-03-06 06:23:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-02-20 02:35:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_Typedef:
|
|
|
|
case CXIdxEntity_CXXTypeAlias: {
|
|
|
|
// Note we want to fetch the first TypeRef. Running
|
|
|
|
// ResolveCursorType(decl->cursor) would return
|
|
|
|
// the type of the typedef/using, not the type of the referenced type.
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* alias_of = AddDeclTypeUsages(
|
|
|
|
db, cursor, nullptr, decl->semanticContainer, decl->lexicalContainer);
|
2017-02-20 02:35:56 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& type = db->ToType(HashUsr(decl->entityInfo->USR));
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
if (alias_of)
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.alias_of = alias_of->usr;
|
2017-12-19 01:21:31 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
Range spell = cursor.get_spell();
|
|
|
|
Range extent = cursor.get_extent();
|
|
|
|
type.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
type.def.extent = SetUse(db, extent, lex_parent, Role::None);
|
2017-12-19 01:21:31 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
SetTypeName(type, cursor, decl->semanticContainer,
|
2018-02-04 22:54:08 +00:00
|
|
|
decl->entityInfo->name, param);
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.kind = GetSymbolKind(decl->entityInfo->kind);
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.comments)
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.comments = cursor.get_comments();
|
2017-12-24 02:22:22 +00:00
|
|
|
|
2017-12-23 16:01:43 +00:00
|
|
|
// For Typedef/CXXTypeAlias spanning a few lines, display the declaration
|
|
|
|
// line, with spelling name replaced with qualified name.
|
2017-12-19 07:05:12 +00:00
|
|
|
if (extent.end.line - extent.start.line <
|
|
|
|
kMaxLinesDisplayTypeAliasDeclarations) {
|
2018-01-11 05:16:46 +00:00
|
|
|
FileContents& fc = param->file_contents[db->path];
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<int> extent_start = fc.ToOffset(extent.start),
|
2017-12-19 07:05:12 +00:00
|
|
|
spell_start = fc.ToOffset(spell.start),
|
|
|
|
spell_end = fc.ToOffset(spell.end),
|
|
|
|
extent_end = fc.ToOffset(extent.end);
|
|
|
|
if (extent_start && spell_start && spell_end && extent_end) {
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.hover =
|
2018-01-11 05:16:46 +00:00
|
|
|
fc.content.substr(*extent_start, *spell_start - *extent_start) +
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.detailed_name.c_str() +
|
2018-01-11 05:16:46 +00:00
|
|
|
fc.content.substr(*spell_end, *extent_end - *spell_end);
|
2017-12-19 01:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, type.uses, spell, fromContainer(decl->lexicalContainer));
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-21 06:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 20:14:57 +00:00
|
|
|
case CXIdxEntity_ObjCProtocol:
|
|
|
|
case CXIdxEntity_ObjCCategory:
|
|
|
|
case CXIdxEntity_ObjCClass:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_Enum:
|
|
|
|
case CXIdxEntity_Union:
|
|
|
|
case CXIdxEntity_Struct:
|
2018-02-21 04:26:17 +00:00
|
|
|
case CXIdxEntity_CXXInterface:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXClass: {
|
2018-03-02 18:18:01 +00:00
|
|
|
Range spell = cursor.get_spell();
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& type = db->ToType(HashUsr(decl->entityInfo->USR));
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// TODO: Eventually run with this if. Right now I want to iron out bugs
|
|
|
|
// this may shadow.
|
|
|
|
// TODO: For type section, verify if this ever runs for non definitions?
|
|
|
|
// if (!decl->isRedeclaration) {
|
|
|
|
|
2018-03-20 02:51:42 +00:00
|
|
|
SetTypeName(type, cursor, decl->semanticContainer, decl->entityInfo->name,
|
|
|
|
param);
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.kind = GetSymbolKind(decl->entityInfo->kind);
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.comments)
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.comments = cursor.get_comments();
|
2017-03-17 07:58:41 +00:00
|
|
|
// }
|
|
|
|
|
2017-09-13 05:51:13 +00:00
|
|
|
if (decl->isDefinition) {
|
2018-04-30 04:49:03 +00:00
|
|
|
type.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
type.def.extent =
|
2018-03-02 18:18:01 +00:00
|
|
|
SetUse(db, cursor.get_extent(), lex_parent, Role::None);
|
2018-01-25 02:22:06 +00:00
|
|
|
|
2018-03-02 18:18:01 +00:00
|
|
|
if (cursor.get_kind() == CXCursor_EnumDecl) {
|
2018-01-25 02:22:06 +00:00
|
|
|
ClangType enum_type = clang_getEnumDeclIntegerType(decl->cursor);
|
2018-02-27 01:23:45 +00:00
|
|
|
if (!enum_type.is_builtin()) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& int_type = db->ToType(enum_type.get_usr_hash());
|
|
|
|
AddUse(db, int_type.uses, spell,
|
2018-02-22 07:34:32 +00:00
|
|
|
fromContainer(decl->lexicalContainer));
|
2018-01-25 02:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-29 01:30:41 +00:00
|
|
|
} else
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, type.declarations, spell,
|
2018-02-23 18:14:54 +00:00
|
|
|
fromContainer(decl->lexicalContainer), Role::Declaration);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2018-01-29 01:30:41 +00:00
|
|
|
switch (decl->entityInfo->templateKind) {
|
2018-01-30 00:27:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case CXIdxEntity_TemplateSpecialization:
|
|
|
|
case CXIdxEntity_TemplatePartialSpecialization: {
|
|
|
|
// TODO Use a different dimension
|
|
|
|
ClangCursor origin_cursor =
|
2018-03-02 18:18:01 +00:00
|
|
|
cursor.template_specialization_to_template_definition();
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& origin = db->ToType(origin_cursor);
|
2018-01-30 00:27:43 +00:00
|
|
|
// template<class T> class function; // not visited by
|
|
|
|
// OnIndexDeclaration template<> class function<int> {}; // current
|
|
|
|
// cursor
|
2018-04-30 04:49:03 +00:00
|
|
|
if (origin.def.detailed_name.empty()) {
|
2018-01-30 00:27:43 +00:00
|
|
|
SetTypeName(origin, origin_cursor, nullptr,
|
2018-04-30 04:49:03 +00:00
|
|
|
&type.def.Name(false)[0], param);
|
|
|
|
origin.def.kind = type.def.kind;
|
2018-01-30 00:27:43 +00:00
|
|
|
}
|
|
|
|
// TODO The name may be assigned in |ResolveToDeclarationType| but
|
2018-03-31 03:16:33 +00:00
|
|
|
// |spell| is std::nullopt.
|
2018-02-15 04:58:42 +00:00
|
|
|
CXFile origin_file;
|
2018-02-18 18:07:13 +00:00
|
|
|
Range origin_spell = origin_cursor.get_spell(&origin_file);
|
2018-04-30 04:49:03 +00:00
|
|
|
if (!origin.def.spell && file == origin_file) {
|
2018-02-18 18:07:13 +00:00
|
|
|
ClangCursor origin_sem = origin_cursor.get_semantic_parent();
|
|
|
|
ClangCursor origin_lex = origin_cursor.get_lexical_parent();
|
2018-04-30 04:49:03 +00:00
|
|
|
origin.def.spell =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, origin_spell, origin_sem, Role::Definition);
|
2018-04-30 04:49:03 +00:00
|
|
|
origin.def.extent =
|
2018-02-18 18:07:13 +00:00
|
|
|
SetUse(db, origin_cursor.get_extent(), origin_lex, Role::None);
|
2018-01-30 00:27:43 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
origin.derived.push_back(type.usr);
|
|
|
|
type.def.bases.push_back(origin.usr);
|
|
|
|
[[fallthrough]];
|
2018-01-29 06:22:13 +00:00
|
|
|
}
|
2018-01-30 00:27:43 +00:00
|
|
|
case CXIdxEntity_Template: {
|
|
|
|
TemplateVisitorData data;
|
|
|
|
data.db = db;
|
2018-03-02 18:18:01 +00:00
|
|
|
data.container = cursor;
|
2018-01-30 00:27:43 +00:00
|
|
|
data.param = param;
|
2018-03-02 18:18:01 +00:00
|
|
|
cursor.VisitChildren(&TemplateVisitor, &data);
|
2018-01-30 00:27:43 +00:00
|
|
|
break;
|
2018-01-29 01:30:41 +00:00
|
|
|
}
|
2017-12-28 18:53:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// type_def->alias_of
|
|
|
|
// type_def->funcs
|
|
|
|
// type_def->types
|
|
|
|
// type_def->uses
|
|
|
|
// type_def->vars
|
|
|
|
|
|
|
|
// Add type-level inheritance information.
|
|
|
|
CXIdxCXXClassDeclInfo const* class_info =
|
|
|
|
clang_index_getCXXClassDeclInfo(decl);
|
|
|
|
if (class_info) {
|
|
|
|
for (unsigned int i = 0; i < class_info->numBases; ++i) {
|
|
|
|
const CXIdxBaseClassInfo* base_class = class_info->bases[i];
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
AddDeclTypeUsages(db, base_class->cursor, nullptr,
|
2018-01-25 20:19:24 +00:00
|
|
|
decl->semanticContainer, decl->lexicalContainer);
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType* parent_type =
|
2018-02-04 22:54:08 +00:00
|
|
|
ResolveToDeclarationType(db, base_class->cursor, param);
|
2018-04-30 04:49:03 +00:00
|
|
|
if (parent_type) {
|
|
|
|
parent_type->derived.push_back(type.usr);
|
|
|
|
type.def.bases.push_back(parent_type->usr);
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2017-02-21 05:32:40 +00:00
|
|
|
}
|
2017-02-20 22:38:32 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 05:01:31 +00:00
|
|
|
// Type-dependent member access expressions do not have accurate spelling
|
|
|
|
// ranges.
|
|
|
|
//
|
|
|
|
// Not type dependent
|
|
|
|
// C<int> f; f.x // .x produces a MemberRefExpr which has a spelling range
|
|
|
|
// of `x`.
|
|
|
|
//
|
|
|
|
// Type dependent
|
|
|
|
// C<T> e; e.x // .x produces a MemberRefExpr which has a spelling range
|
|
|
|
// of `e` (weird) and an empty spelling name.
|
|
|
|
//
|
|
|
|
// To attribute the use of `x` in `e.x`, we use cursor extent `e.x`
|
|
|
|
// minus cursor spelling `e` minus the period.
|
2018-02-01 05:40:40 +00:00
|
|
|
void CheckTypeDependentMemberRefExpr(Range* spell,
|
2018-02-01 05:01:31 +00:00
|
|
|
const ClangCursor& cursor,
|
|
|
|
IndexParam* param,
|
|
|
|
const IndexFile* db) {
|
|
|
|
if (cursor.get_kind() == CXCursor_MemberRefExpr &&
|
2018-02-18 18:07:13 +00:00
|
|
|
cursor.get_spell_name().empty()) {
|
2018-02-01 05:40:40 +00:00
|
|
|
*spell = cursor.get_extent().RemovePrefix(spell->end);
|
2018-02-01 05:01:31 +00:00
|
|
|
const FileContents& fc = param->file_contents[db->path];
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<int> maybe_period = fc.ToOffset(spell->start);
|
2018-02-01 05:01:31 +00:00
|
|
|
if (maybe_period) {
|
|
|
|
int i = *maybe_period;
|
|
|
|
if (fc.content[i] == '.')
|
2018-02-01 05:40:40 +00:00
|
|
|
spell->start.column++;
|
2018-02-01 05:01:31 +00:00
|
|
|
// -> is likely unexposed.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:57:03 +00:00
|
|
|
void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
|
2017-04-08 22:54:36 +00:00
|
|
|
// TODO: Use clang_getFileUniqueID
|
|
|
|
CXFile file;
|
2017-09-22 01:14:57 +00:00
|
|
|
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(ref->loc), &file,
|
|
|
|
nullptr, nullptr, nullptr);
|
2017-04-08 18:36:32 +00:00
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
2017-05-20 21:45:46 +00:00
|
|
|
IndexFile* db = ConsumeFile(param, file);
|
2017-04-08 22:54:36 +00:00
|
|
|
if (!db)
|
|
|
|
return;
|
2017-04-08 18:36:32 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor cursor(ref->cursor);
|
2018-02-11 04:01:10 +00:00
|
|
|
ClangCursor lex_parent(fromContainer(ref->container));
|
2018-02-21 04:26:17 +00:00
|
|
|
ClangCursor referenced;
|
|
|
|
if (ref->referencedEntity)
|
|
|
|
referenced = ref->referencedEntity->cursor;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
switch (ref->referencedEntity->kind) {
|
2018-02-21 04:26:17 +00:00
|
|
|
case CXIdxEntity_Unexposed:
|
|
|
|
LOG_S(INFO) << "CXIdxEntity_Unexposed " << cursor.get_spell_name();
|
|
|
|
break;
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXNamespace: {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ns = db->ToType(referenced.get_usr_hash());
|
|
|
|
AddUse(db, ns.uses, cursor.get_spell(), fromContainer(ref->container));
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2018-02-21 04:26:17 +00:00
|
|
|
case CXIdxEntity_CXXNamespaceAlias: {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ns = db->ToType(referenced.get_usr_hash());
|
|
|
|
AddUse(db, ns.uses, cursor.get_spell(), fromContainer(ref->container));
|
|
|
|
if (!ns.def.spell) {
|
2018-02-21 04:26:17 +00:00
|
|
|
ClangCursor sem_parent = referenced.get_semantic_parent();
|
|
|
|
ClangCursor lex_parent = referenced.get_lexical_parent();
|
2018-05-08 07:35:32 +00:00
|
|
|
CXFile referenced_file;
|
|
|
|
Range spell = referenced.get_spell(&referenced_file);
|
|
|
|
if (file == referenced_file) {
|
|
|
|
ns.def.spell = SetUse(db, spell, sem_parent, Role::Definition);
|
|
|
|
ns.def.extent =
|
|
|
|
SetUse(db, referenced.get_extent(), lex_parent, Role::None);
|
|
|
|
std::string name = referenced.get_spell_name();
|
|
|
|
SetTypeName(ns, referenced, nullptr, name.c_str(), param);
|
|
|
|
}
|
2018-02-21 04:26:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-15 20:14:57 +00:00
|
|
|
case CXIdxEntity_ObjCProperty:
|
|
|
|
case CXIdxEntity_ObjCIvar:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_EnumConstant:
|
|
|
|
case CXIdxEntity_CXXStaticVariable:
|
|
|
|
case CXIdxEntity_Variable:
|
|
|
|
case CXIdxEntity_Field: {
|
2018-03-06 04:46:45 +00:00
|
|
|
Range loc = cursor.get_spell();
|
|
|
|
CheckTypeDependentMemberRefExpr(&loc, cursor, param, db);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
referenced = referenced.template_specialization_to_template_definition();
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexVar& var = db->ToVar(referenced);
|
2017-12-11 06:36:32 +00:00
|
|
|
// Lambda paramaters are not processed by OnIndexDeclaration and
|
2017-12-12 17:49:52 +00:00
|
|
|
// may not have a short_name yet. Note that we only process the lambda
|
|
|
|
// parameter as a definition if it is in the same file as the reference,
|
|
|
|
// as lambdas cannot be split across files.
|
2018-04-30 04:49:03 +00:00
|
|
|
if (var.def.detailed_name.empty()) {
|
2017-12-12 17:49:52 +00:00
|
|
|
CXFile referenced_file;
|
2018-02-18 18:07:13 +00:00
|
|
|
Range spell = referenced.get_spell(&referenced_file);
|
2017-12-12 17:49:52 +00:00
|
|
|
if (file == referenced_file) {
|
2018-04-30 04:49:03 +00:00
|
|
|
var.def.spell = SetUse(db, spell, lex_parent, Role::Definition);
|
|
|
|
var.def.extent =
|
2018-02-22 07:34:32 +00:00
|
|
|
SetUse(db, referenced.get_extent(), lex_parent, Role::None);
|
2017-12-12 17:49:52 +00:00
|
|
|
|
2017-12-23 16:01:43 +00:00
|
|
|
// TODO Some of the logic here duplicates CXIdxEntity_Variable branch
|
|
|
|
// of OnIndexDeclaration. But there `decl` is of type CXIdxDeclInfo
|
|
|
|
// and has more information, thus not easy to reuse the code.
|
2018-02-18 18:07:13 +00:00
|
|
|
SetVarDetail(var, referenced.get_spell_name(), referenced, nullptr,
|
2018-01-31 04:59:31 +00:00
|
|
|
true, db, param);
|
2018-04-30 04:49:03 +00:00
|
|
|
var.def.kind = lsSymbolKind::Parameter;
|
2017-12-12 17:49:52 +00:00
|
|
|
}
|
2017-12-11 06:36:32 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUse(db, var.uses, loc, fromContainer(ref->container),
|
2018-02-23 18:14:54 +00:00
|
|
|
GetRole(ref, Role::Reference));
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
2017-02-21 03:03:01 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXConversionFunction:
|
|
|
|
case CXIdxEntity_CXXStaticMethod:
|
|
|
|
case CXIdxEntity_CXXInstanceMethod:
|
2017-12-15 20:14:57 +00:00
|
|
|
case CXIdxEntity_ObjCInstanceMethod:
|
|
|
|
case CXIdxEntity_ObjCClassMethod:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_Function:
|
|
|
|
case CXIdxEntity_CXXConstructor:
|
|
|
|
case CXIdxEntity_CXXDestructor: {
|
|
|
|
// TODO: Redirect container to constructor for the following example, ie,
|
|
|
|
// we should be inserting an outgoing function call from the Foo
|
|
|
|
// ctor.
|
|
|
|
//
|
|
|
|
// int Gen() { return 5; }
|
|
|
|
// class Foo {
|
|
|
|
// int x = Gen();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TODO: search full history?
|
2018-03-06 04:46:45 +00:00
|
|
|
Range loc = cursor.get_spell();
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& called = db->ToFunc(HashUsr(ref->referencedEntity->USR));
|
2017-05-23 06:47:27 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
std::string_view short_name = called.def.Name(false);
|
2017-05-23 06:47:27 +00:00
|
|
|
// libclang doesn't provide a nice api to check if the given function
|
|
|
|
// call is implicit. ref->kind should probably work (it's either direct
|
|
|
|
// or implicit), but libclang only supports implicit for objective-c.
|
2017-09-22 01:14:57 +00:00
|
|
|
bool is_implicit =
|
|
|
|
CanBeCalledImplicitly(ref->referencedEntity->kind) &&
|
2017-12-20 06:20:44 +00:00
|
|
|
// Treats empty short_name as an implicit call like implicit move
|
|
|
|
// constructor in `vector<int> a = f();`
|
2018-01-31 06:39:39 +00:00
|
|
|
(short_name.empty() ||
|
2017-12-20 06:20:44 +00:00
|
|
|
// For explicit destructor call, ref->cursor may be "~" while
|
|
|
|
// called->def.short_name is "~A"
|
|
|
|
// "~A" is not a substring of ref->cursor, but we should take this
|
|
|
|
// case as not `is_implicit`.
|
2018-01-31 06:39:39 +00:00
|
|
|
(short_name[0] != '~' &&
|
2017-12-20 06:20:44 +00:00
|
|
|
!CursorSpellingContainsString(ref->cursor, param->tu->cx_tu,
|
2018-01-31 06:39:39 +00:00
|
|
|
short_name)));
|
2017-05-23 06:47:27 +00:00
|
|
|
|
2018-01-11 02:43:01 +00:00
|
|
|
// Extents have larger ranges and thus less specific, and will be
|
|
|
|
// overriden by other functions if exist.
|
2018-01-03 18:42:23 +00:00
|
|
|
//
|
2018-02-01 05:40:40 +00:00
|
|
|
// Type-dependent member access expressions do not have useful spelling
|
2018-01-11 02:43:01 +00:00
|
|
|
// ranges. See the comment above for the CXIdxEntity_Field case.
|
2018-02-01 05:01:31 +00:00
|
|
|
if (is_implicit)
|
2018-03-06 04:46:45 +00:00
|
|
|
loc = cursor.get_extent();
|
2018-02-01 05:01:31 +00:00
|
|
|
else
|
2018-03-06 04:46:45 +00:00
|
|
|
CheckTypeDependentMemberRefExpr(&loc, cursor, param, db);
|
2017-12-30 23:33:58 +00:00
|
|
|
|
2018-02-08 03:14:44 +00:00
|
|
|
OnIndexReference_Function(
|
2018-04-30 04:49:03 +00:00
|
|
|
db, loc, ref->container->cursor, called,
|
2018-02-12 18:15:43 +00:00
|
|
|
GetRole(ref, Role::Call) |
|
2018-02-11 04:01:10 +00:00
|
|
|
(is_implicit ? Role::Implicit : Role::None));
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-11-09 03:55:13 +00:00
|
|
|
// Checks if |str| starts with |start|. Ignores case.
|
|
|
|
auto str_begin = [](const char* start, const char* str) {
|
|
|
|
while (*start && *str) {
|
|
|
|
char a = tolower(*start);
|
|
|
|
char b = tolower(*str);
|
|
|
|
if (a != b)
|
|
|
|
return false;
|
|
|
|
++start;
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
return !*start;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool is_template = ref->referencedEntity->templateKind !=
|
|
|
|
CXIdxEntityCXXTemplateKind::CXIdxEntity_NonTemplate;
|
2018-04-04 06:05:41 +00:00
|
|
|
if (g_config->index.attributeMakeCallsToCtor && is_template &&
|
2018-01-31 19:10:20 +00:00
|
|
|
str_begin("make", ref->referencedEntity->name)) {
|
2017-11-09 03:55:13 +00:00
|
|
|
// Try to find the return type of called function. That type will have
|
|
|
|
// the constructor function we add a usage to.
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<ClangCursor> opt_found_type = FindType(ref->cursor);
|
2017-11-09 03:55:13 +00:00
|
|
|
if (opt_found_type) {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr ctor_type_usr = opt_found_type->get_referenced().get_usr_hash();
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor call_cursor = ref->cursor;
|
2017-11-09 03:55:13 +00:00
|
|
|
|
|
|
|
// Build a type description from the parameters of the call, so we
|
|
|
|
// can try to find a constructor with the same type description.
|
|
|
|
std::vector<std::string> call_type_desc;
|
2017-11-11 19:31:05 +00:00
|
|
|
for (ClangType type : call_cursor.get_type().get_arguments()) {
|
2018-02-18 18:07:13 +00:00
|
|
|
std::string type_desc = type.get_spell_name();
|
2017-11-09 03:55:13 +00:00
|
|
|
if (!type_desc.empty())
|
|
|
|
call_type_desc.push_back(type_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the constructor and add a reference.
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Usr> ctor_usr =
|
2017-11-09 03:55:13 +00:00
|
|
|
param->ctors.TryFindConstructorUsr(ctor_type_usr, call_type_desc);
|
|
|
|
if (ctor_usr) {
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexFunc& ctor = db->ToFunc(*ctor_usr);
|
|
|
|
ctor.uses.push_back(
|
|
|
|
Use{{loc, 0, SymbolKind::File, Role::Call | Role::Implicit}});
|
2017-11-09 03:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-21 03:03:01 +00:00
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-12-15 20:14:57 +00:00
|
|
|
case CXIdxEntity_ObjCCategory:
|
|
|
|
case CXIdxEntity_ObjCProtocol:
|
|
|
|
case CXIdxEntity_ObjCClass:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_Typedef:
|
2018-02-22 07:34:32 +00:00
|
|
|
case CXIdxEntity_CXXInterface: // MSVC __interface
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXTypeAlias:
|
|
|
|
case CXIdxEntity_Enum:
|
|
|
|
case CXIdxEntity_Union:
|
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
case CXIdxEntity_CXXClass: {
|
2018-03-06 04:46:45 +00:00
|
|
|
referenced = referenced.template_specialization_to_template_definition();
|
2018-04-30 04:49:03 +00:00
|
|
|
IndexType& ref_type = db->ToType(referenced);
|
2018-02-20 21:56:56 +00:00
|
|
|
if (!ref->parentEntity || IsDeclContext(ref->parentEntity->kind))
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_type.declarations, ref->cursor);
|
2018-02-20 21:56:56 +00:00
|
|
|
else
|
2018-04-30 04:49:03 +00:00
|
|
|
AddUseSpell(db, ref_type.uses, ref->cursor);
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-26 01:08:05 +00:00
|
|
|
}
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2018-05-09 05:01:58 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> ClangIndexer::Index(
|
2018-05-05 22:29:17 +00:00
|
|
|
VFS* vfs,
|
2017-04-24 01:01:51 +00:00
|
|
|
std::string file,
|
2017-09-27 06:03:43 +00:00
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<FileContents>& file_contents,
|
2018-05-09 05:01:58 +00:00
|
|
|
PerformanceImportFile* perf) {
|
2018-04-04 06:05:41 +00:00
|
|
|
if (!g_config->index.enabled)
|
2018-04-02 07:22:12 +00:00
|
|
|
return {};
|
2017-04-19 00:05:14 +00:00
|
|
|
|
2017-04-24 01:01:51 +00:00
|
|
|
file = NormalizePath(file);
|
2017-03-11 02:24:51 +00:00
|
|
|
|
2017-05-17 07:08:45 +00:00
|
|
|
Timer timer;
|
|
|
|
|
2017-04-14 22:58:07 +00:00
|
|
|
std::vector<CXUnsavedFile> unsaved_files;
|
2017-07-30 04:24:02 +00:00
|
|
|
for (const FileContents& contents : file_contents) {
|
2017-05-16 07:38:15 +00:00
|
|
|
CXUnsavedFile unsaved;
|
2017-07-30 04:24:02 +00:00
|
|
|
unsaved.Filename = contents.path.c_str();
|
|
|
|
unsaved.Contents = contents.content.c_str();
|
|
|
|
unsaved.Length = (unsigned long)contents.content.size();
|
2017-05-16 07:38:15 +00:00
|
|
|
unsaved_files.push_back(unsaved);
|
|
|
|
}
|
2017-07-30 04:24:02 +00:00
|
|
|
|
2017-11-11 19:41:09 +00:00
|
|
|
std::unique_ptr<ClangTranslationUnit> tu = ClangTranslationUnit::Create(
|
2018-05-09 05:01:58 +00:00
|
|
|
&index, file, args, unsaved_files,
|
2017-10-31 19:49:19 +00:00
|
|
|
CXTranslationUnit_KeepGoing |
|
|
|
|
CXTranslationUnit_DetailedPreprocessingRecord);
|
2017-10-23 05:07:50 +00:00
|
|
|
if (!tu)
|
2018-04-02 07:22:12 +00:00
|
|
|
return {};
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-05-17 07:08:45 +00:00
|
|
|
perf->index_parse = timer.ElapsedMicrosecondsAndReset();
|
|
|
|
|
2018-05-09 05:01:58 +00:00
|
|
|
return ParseWithTu(vfs, perf, tu.get(), &index, file, args, unsaved_files);
|
2017-09-27 06:03:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 07:22:12 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> ParseWithTu(
|
2018-05-05 22:29:17 +00:00
|
|
|
VFS* vfs,
|
2017-09-27 06:03:43 +00:00
|
|
|
PerformanceImportFile* perf,
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangTranslationUnit* tu,
|
|
|
|
ClangIndex* index,
|
2017-09-27 06:03:43 +00:00
|
|
|
const std::string& file,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<CXUnsavedFile>& file_contents) {
|
|
|
|
Timer timer;
|
|
|
|
|
2017-12-01 17:57:03 +00:00
|
|
|
IndexerCallbacks callback = {0};
|
|
|
|
// Available callbacks:
|
|
|
|
// - abortQuery
|
|
|
|
// - enteredMainFile
|
|
|
|
// - ppIncludedFile
|
|
|
|
// - importedASTFile
|
|
|
|
// - startedTranslationUnit
|
|
|
|
callback.diagnostic = &OnIndexDiagnostic;
|
|
|
|
callback.ppIncludedFile = &OnIndexIncludedFile;
|
|
|
|
callback.indexDeclaration = &OnIndexDeclaration;
|
|
|
|
callback.indexEntityReference = &OnIndexReference;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
FileConsumer file_consumer(vfs, file);
|
2018-04-04 06:05:41 +00:00
|
|
|
IndexParam param(tu, &file_consumer);
|
2017-09-27 06:03:43 +00:00
|
|
|
for (const CXUnsavedFile& contents : file_contents) {
|
2018-01-11 05:16:46 +00:00
|
|
|
param.file_contents[contents.Filename] = FileContents(
|
2017-12-19 07:05:12 +00:00
|
|
|
contents.Filename, std::string(contents.Contents, contents.Length));
|
2017-07-30 18:31:41 +00:00
|
|
|
}
|
2017-03-06 06:47:37 +00:00
|
|
|
|
2017-09-27 06:03:43 +00:00
|
|
|
CXFile cx_file = clang_getFile(tu->cx_tu, file.c_str());
|
2017-05-20 21:45:46 +00:00
|
|
|
param.primary_file = ConsumeFile(¶m, cx_file);
|
2017-04-10 00:08:54 +00:00
|
|
|
|
2017-05-25 02:04:19 +00:00
|
|
|
CXIndexAction index_action = clang_IndexAction_create(index->cx_index);
|
2017-12-01 17:57:03 +00:00
|
|
|
|
|
|
|
// |index_result| is a CXErrorCode instance.
|
|
|
|
int index_result = clang_indexTranslationUnit(
|
|
|
|
index_action, ¶m, &callback, sizeof(IndexerCallbacks),
|
|
|
|
CXIndexOpt_IndexFunctionLocalSymbols |
|
|
|
|
CXIndexOpt_SkipParsedBodiesInSession |
|
|
|
|
CXIndexOpt_IndexImplicitTemplateInstantiations,
|
|
|
|
tu->cx_tu);
|
|
|
|
if (index_result != CXError_Success) {
|
2018-01-20 07:56:49 +00:00
|
|
|
LOG_S(ERROR) << "Indexing " << file
|
|
|
|
<< " failed with errno=" << index_result;
|
2018-04-02 07:22:12 +00:00
|
|
|
return {};
|
2017-12-01 17:57:03 +00:00
|
|
|
}
|
2017-07-30 04:24:02 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
clang_IndexAction_dispose(index_action);
|
2017-05-17 07:08:45 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor(clang_getTranslationUnitCursor(tu->cx_tu))
|
2017-11-04 23:11:52 +00:00
|
|
|
.VisitChildren(&VisitMacroDefinitionAndExpansions, ¶m);
|
2017-05-21 01:58:54 +00:00
|
|
|
|
2017-05-17 07:08:45 +00:00
|
|
|
perf->index_build = timer.ElapsedMicrosecondsAndReset();
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2018-01-06 17:28:55 +00:00
|
|
|
std::unordered_map<std::string, int> inc_to_line;
|
2018-01-06 22:47:24 +00:00
|
|
|
// TODO
|
|
|
|
if (param.primary_file)
|
2018-01-11 02:43:01 +00:00
|
|
|
for (auto& inc : param.primary_file->includes)
|
2018-01-06 22:47:24 +00:00
|
|
|
inc_to_line[inc.resolved_path] = inc.line;
|
2018-01-06 17:28:55 +00:00
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
auto result = param.file_consumer->TakeLocalState();
|
2017-07-30 04:24:02 +00:00
|
|
|
for (std::unique_ptr<IndexFile>& entry : result) {
|
2017-04-24 01:01:51 +00:00
|
|
|
entry->import_file = file;
|
2017-04-21 04:06:15 +00:00
|
|
|
entry->args = args;
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it : entry->usr2func) {
|
2018-03-07 23:08:50 +00:00
|
|
|
// e.g. declaration + out-of-line definition
|
2018-04-30 04:49:03 +00:00
|
|
|
Uniquify(it.second.derived);
|
|
|
|
Uniquify(it.second.uses);
|
2018-03-07 23:08:50 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it : entry->usr2type) {
|
|
|
|
Uniquify(it.second.derived);
|
|
|
|
Uniquify(it.second.uses);
|
2018-02-23 18:14:54 +00:00
|
|
|
// e.g. declaration + out-of-line definition
|
2018-04-30 04:49:03 +00:00
|
|
|
Uniquify(it.second.def.funcs);
|
2018-02-23 18:14:54 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
for (auto& it : entry->usr2var)
|
|
|
|
Uniquify(it.second.uses);
|
2018-01-06 17:28:55 +00:00
|
|
|
|
2018-01-06 22:47:24 +00:00
|
|
|
if (param.primary_file) {
|
|
|
|
// If there are errors, show at least one at the include position.
|
|
|
|
auto it = inc_to_line.find(entry->path);
|
|
|
|
if (it != inc_to_line.end()) {
|
|
|
|
int line = it->second;
|
|
|
|
for (auto ls_diagnostic : entry->diagnostics_) {
|
|
|
|
if (ls_diagnostic.severity != lsDiagnosticSeverity::Error)
|
|
|
|
continue;
|
|
|
|
ls_diagnostic.range =
|
2018-04-08 00:10:54 +00:00
|
|
|
lsRange{lsPosition{line, 10}, lsPosition{line, 10}};
|
2018-01-06 22:47:24 +00:00
|
|
|
param.primary_file->diagnostics_.push_back(ls_diagnostic);
|
|
|
|
break;
|
|
|
|
}
|
2018-01-06 17:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 04:24:02 +00:00
|
|
|
|
|
|
|
// Update file contents and modification time.
|
2018-05-05 22:29:17 +00:00
|
|
|
entry->last_write_time = param.file2write_time[entry->path];
|
2017-07-30 04:24:02 +00:00
|
|
|
|
|
|
|
// Update dependencies for the file. Do not include the file in its own
|
|
|
|
// dependency set.
|
2018-05-05 22:29:17 +00:00
|
|
|
for (const std::string& path : param.seen_files)
|
|
|
|
if (path != entry->path && path != entry->import_file)
|
|
|
|
entry->dependencies[path] = param.file2write_time[path];
|
2017-04-08 22:54:36 +00:00
|
|
|
}
|
2017-04-19 00:05:14 +00:00
|
|
|
|
2018-04-02 07:22:12 +00:00
|
|
|
return result;
|
2017-03-22 17:16:09 +00:00
|
|
|
}
|
2017-04-16 21:51:47 +00:00
|
|
|
|
2018-04-06 00:00:07 +00:00
|
|
|
bool ConcatTypeAndName(std::string& type, const std::string& name) {
|
|
|
|
bool ret = false;
|
2017-12-30 21:11:55 +00:00
|
|
|
if (type.size() &&
|
2018-04-06 00:00:07 +00:00
|
|
|
(type.back() != ' ' && type.back() != '*' && type.back() != '&')) {
|
2017-12-30 21:11:55 +00:00
|
|
|
type.push_back(' ');
|
2018-04-06 00:00:07 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
2017-12-30 21:11:55 +00:00
|
|
|
type.append(name);
|
2018-04-06 00:00:07 +00:00
|
|
|
return ret;
|
2017-12-30 21:11:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 21:51:47 +00:00
|
|
|
void IndexInit() {
|
|
|
|
clang_enableStackTraces();
|
2018-02-07 07:55:02 +00:00
|
|
|
if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
|
2018-01-25 07:32:40 +00:00
|
|
|
clang_toggleCrashRecovery(1);
|
2017-04-18 23:49:55 +00:00
|
|
|
}
|
2017-12-01 17:45:44 +00:00
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
// |SymbolRef| is serialized this way.
|
|
|
|
// |Use| also uses this though it has an extra field |file|,
|
|
|
|
// which is not used by Index* so it does not need to be serialized.
|
2018-02-08 18:38:27 +00:00
|
|
|
void Reflect(Reader& visitor, Reference& value) {
|
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-02-10 20:53:18 +00:00
|
|
|
std::string t = visitor.GetString();
|
|
|
|
char* s = const_cast<char*>(t.c_str());
|
2018-04-08 06:32:35 +00:00
|
|
|
value.range = Range::FromString(s);
|
2018-02-10 20:53:18 +00:00
|
|
|
s = strchr(s, '|');
|
2018-04-30 04:49:03 +00:00
|
|
|
value.usr = strtoull(s + 1, &s, 10);
|
2018-02-10 20:53:18 +00:00
|
|
|
value.kind = static_cast<SymbolKind>(strtol(s + 1, &s, 10));
|
2018-02-11 04:01:10 +00:00
|
|
|
value.role = static_cast<Role>(strtol(s + 1, &s, 10));
|
2018-02-08 18:38:27 +00:00
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.range);
|
2018-04-30 04:49:03 +00:00
|
|
|
Reflect(visitor, value.usr);
|
2018-02-09 17:42:10 +00:00
|
|
|
Reflect(visitor, value.kind);
|
2018-02-08 18:38:27 +00:00
|
|
|
Reflect(visitor, value.role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Reflect(Writer& visitor, Reference& value) {
|
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-04-08 06:32:35 +00:00
|
|
|
char buf[99];
|
2018-04-30 04:49:03 +00:00
|
|
|
snprintf(buf, sizeof buf, "%s|%" PRIu64 "|%d|%d",
|
|
|
|
value.range.ToString().c_str(), value.usr, int(value.kind),
|
|
|
|
int(value.role));
|
2018-04-08 06:32:35 +00:00
|
|
|
std::string s(buf);
|
2018-02-10 20:53:18 +00:00
|
|
|
Reflect(visitor, s);
|
2018-02-08 18:38:27 +00:00
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.range);
|
2018-04-30 04:49:03 +00:00
|
|
|
Reflect(visitor, value.usr);
|
2018-02-09 17:42:10 +00:00
|
|
|
Reflect(visitor, value.kind);
|
2018-02-08 18:38:27 +00:00
|
|
|
Reflect(visitor, value.role);
|
|
|
|
}
|
|
|
|
}
|