2017-02-22 08:52:00 +00:00
|
|
|
#include "indexer.h"
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
#include "clang_cursor.h"
|
2017-05-20 06:35:14 +00:00
|
|
|
#include "clang_utils.h"
|
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-02-23 08:47:07 +00:00
|
|
|
|
2017-07-30 04:24:02 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-04-22 07:39:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
2017-11-09 03:55:13 +00:00
|
|
|
#include <climits>
|
2017-04-22 07:39:55 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
// TODO: See if we can use clang_indexLoc_getFileLocation to get a type ref on
|
|
|
|
// |Foobar| in DISALLOW_COPY(Foobar)
|
2017-05-21 01:58:54 +00:00
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-12-19 07:05:12 +00:00
|
|
|
constexpr bool kIndexStdDeclarations = true;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
void AddFuncRef(std::vector<IndexFuncRef>* result, IndexFuncRef ref) {
|
|
|
|
if (!result->empty() && (*result)[result->size() - 1] == ref)
|
|
|
|
return;
|
|
|
|
result->push_back(ref);
|
|
|
|
}
|
|
|
|
|
2017-05-21 01:58:54 +00:00
|
|
|
Range Resolve(const CXSourceRange& range, CXFile* cx_file = nullptr) {
|
2017-05-10 06:13:13 +00:00
|
|
|
CXSourceLocation start = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation end = clang_getRangeEnd(range);
|
|
|
|
|
|
|
|
unsigned int start_line, start_column;
|
2017-09-22 01:14:57 +00:00
|
|
|
clang_getSpellingLocation(start, cx_file, &start_line, &start_column,
|
|
|
|
nullptr);
|
2017-05-10 06:13:13 +00:00
|
|
|
unsigned int end_line, end_column;
|
|
|
|
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
return Range(Position((int16_t)start_line, (int16_t)start_column) /*start*/,
|
|
|
|
Position((int16_t)end_line, (int16_t)end_column) /*end*/);
|
2017-05-10 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 01:58:54 +00:00
|
|
|
Range ResolveSpelling(const CXCursor& cx_cursor, CXFile* cx_file = nullptr) {
|
2017-05-10 06:13:13 +00:00
|
|
|
CXSourceRange cx_range = clang_Cursor_getSpellingNameRange(cx_cursor, 0, 0);
|
2017-05-21 01:58:54 +00:00
|
|
|
return Resolve(cx_range, cx_file);
|
2017-05-10 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 01:58:54 +00:00
|
|
|
Range ResolveExtent(const CXCursor& cx_cursor, CXFile* cx_file = nullptr) {
|
2017-05-10 06:13:13 +00:00
|
|
|
CXSourceRange cx_range = clang_getCursorExtent(cx_cursor);
|
2017-05-21 01:58:54 +00:00
|
|
|
return Resolve(cx_range, cx_file);
|
2017-05-10 06:13:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 02:10:23 +00:00
|
|
|
bool IsScopeSemanticContainer(CXCursorKind kind) {
|
2017-12-22 06:46:45 +00:00
|
|
|
switch (kind) {
|
|
|
|
case CXCursor_Namespace:
|
|
|
|
case CXCursor_TranslationUnit:
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
case CXCursor_EnumDecl:
|
2017-12-22 22:48:55 +00:00
|
|
|
// TODO Add more Objective-C containers
|
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
|
|
|
case CXCursor_ObjCImplementationDecl:
|
2017-12-22 06:46:45 +00:00
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 21:45:46 +00:00
|
|
|
struct NamespaceHelper {
|
2017-12-21 03:52:34 +00:00
|
|
|
std::unordered_map<ClangCursor, std::string>
|
|
|
|
container_cursor_to_qualified_name;
|
2017-05-20 21:45:46 +00:00
|
|
|
|
|
|
|
void RegisterQualifiedName(std::string usr,
|
2017-09-22 01:14:57 +00:00
|
|
|
const CXIdxContainerInfo* container,
|
2017-12-23 16:01:43 +00:00
|
|
|
std::string qualified_name) {}
|
2017-05-20 21:45:46 +00:00
|
|
|
|
|
|
|
std::string QualifiedName(const CXIdxContainerInfo* container,
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string unqualified_name) {
|
2017-12-21 03:52:34 +00:00
|
|
|
if (!container)
|
|
|
|
return unqualified_name;
|
|
|
|
// 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.
|
|
|
|
ClangCursor cursor = container->cursor;
|
|
|
|
std::vector<ClangCursor> namespaces;
|
|
|
|
std::string qualifier;
|
2017-12-22 22:48:55 +00:00
|
|
|
while (cursor.get_kind() != CXCursor_TranslationUnit &&
|
2017-12-23 02:10:23 +00:00
|
|
|
!IsScopeSemanticContainer(cursor.get_kind())) {
|
2017-12-21 03:52:34 +00:00
|
|
|
auto it = container_cursor_to_qualified_name.find(cursor);
|
|
|
|
if (it != container_cursor_to_qualified_name.end()) {
|
|
|
|
qualifier = it->second;
|
|
|
|
break;
|
2017-05-20 21:45:46 +00:00
|
|
|
}
|
2017-12-21 03:52:34 +00:00
|
|
|
namespaces.push_back(cursor);
|
|
|
|
cursor = clang_getCursorSemanticParent(cursor.cx_cursor);
|
|
|
|
}
|
2017-12-23 16:01:43 +00:00
|
|
|
for (size_t i = namespaces.size(); i > 0;) {
|
2017-12-21 03:52:34 +00:00
|
|
|
i--;
|
|
|
|
std::string name = namespaces[i].get_spelling();
|
2017-12-22 22:48:55 +00:00
|
|
|
// Empty name indicates unnamed namespace, anonymous struct, anonymous
|
|
|
|
// union, ...
|
2017-12-21 03:52:34 +00:00
|
|
|
qualifier += name.empty() ? "(anon)" : name;
|
|
|
|
qualifier += "::";
|
|
|
|
container_cursor_to_qualified_name[namespaces[i]] = qualifier;
|
2017-05-20 21:45:46 +00:00
|
|
|
}
|
2017-12-21 03:52:34 +00:00
|
|
|
return qualifier + unqualified_name;
|
2017-05-20 21:45:46 +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 {
|
|
|
|
using Usr = std::string;
|
|
|
|
struct Constructor {
|
|
|
|
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
|
|
|
};
|
|
|
|
std::unordered_map<Usr, std::vector<Constructor>> constructors_;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
Constructor ctor{ctor_cursor.get_usr(), build_type_desc(ctor_cursor)};
|
2017-11-09 03:55:13 +00:00
|
|
|
|
|
|
|
// Insert into |constructors_|.
|
|
|
|
std::string type_usr = ctor_cursor.get_semantic_parent().get_usr();
|
|
|
|
auto existing_ctors = constructors_.find(type_usr);
|
|
|
|
if (existing_ctors != constructors_.end()) {
|
|
|
|
existing_ctors->second.push_back(ctor);
|
|
|
|
} else {
|
|
|
|
constructors_[type_usr] = {ctor};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to lookup a constructor in |type_usr| that takes arguments most
|
|
|
|
// closely aligned to |param_type_desc|.
|
|
|
|
optional<std::string> TryFindConstructorUsr(
|
|
|
|
const std::string& type_usr,
|
|
|
|
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())
|
|
|
|
return nullopt;
|
|
|
|
const std::vector<Constructor>& ctors = ctors_it->second;
|
|
|
|
if (ctors.empty())
|
|
|
|
return nullopt;
|
|
|
|
|
|
|
|
std::string best_usr;
|
|
|
|
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 *&&|
|
|
|
|
for (int i = 0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!best_usr.empty());
|
|
|
|
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;
|
2017-12-19 07:05:12 +00:00
|
|
|
std::unordered_map<std::string, FileContentsWithOffsets> file_contents;
|
2017-07-30 04:24:02 +00:00
|
|
|
std::unordered_map<std::string, int64_t> file_modification_times;
|
|
|
|
|
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
|
|
|
|
2017-11-11 19:41:09 +00:00
|
|
|
IndexParam(ClangTranslationUnit* tu, FileConsumer* file_consumer)
|
2017-09-22 01:14:57 +00:00
|
|
|
: tu(tu), file_consumer(file_consumer) {}
|
2017-05-20 21:45:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
|
|
|
|
bool is_first_ownership = false;
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexFile* db =
|
|
|
|
param->file_consumer->TryConsumeFile(file, &is_first_ownership);
|
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);
|
2017-08-16 03:28:52 +00:00
|
|
|
// Sometimes the fill name will be empty. Not sure why. Not much we can do
|
|
|
|
// with it.
|
|
|
|
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.
|
2017-08-16 05:39:50 +00:00
|
|
|
optional<int64_t> modification_time = GetLastModificationTime(file_name);
|
2017-09-22 01:14:57 +00:00
|
|
|
LOG_IF_S(ERROR, !modification_time)
|
|
|
|
<< "Failed fetching modification time for " << file_name;
|
2017-08-16 05:39:50 +00:00
|
|
|
if (modification_time)
|
|
|
|
param->file_modification_times[file_name] = *modification_time;
|
2017-08-16 03:28:52 +00:00
|
|
|
|
|
|
|
// Capture file contents in |param->file_contents| if it was not specified
|
|
|
|
// at the start of indexing.
|
2017-12-23 16:01:43 +00:00
|
|
|
if (db && !param->file_contents.count(file_name)) {
|
2017-08-16 03:28:52 +00:00
|
|
|
optional<std::string> content = ReadContent(file_name);
|
|
|
|
if (content)
|
2017-12-19 07:05:12 +00:00
|
|
|
param->file_contents.emplace(file_name, *content);
|
2017-08-16 03:28:52 +00:00
|
|
|
else
|
2017-09-22 01:14:57 +00:00
|
|
|
LOG_S(ERROR) << "[indexer] Failed to read file content for "
|
|
|
|
<< file_name;
|
2017-08-16 03:28:52 +00:00
|
|
|
}
|
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) {
|
|
|
|
Range range = Resolve(skipped->ranges[i]);
|
|
|
|
// clang_getSkippedRanges reports start one token after the '#', move it
|
|
|
|
// back so it starts at the '#'
|
|
|
|
range.start.column -= 1;
|
|
|
|
db->skipped_by_preprocessor.push_back(range);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
std::string scanning_for) {
|
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]);
|
|
|
|
if (strcmp(clang_getCString(name), scanning_for.c_str()) == 0) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
optional<Range> previous_token_range;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_tokens; ++i) {
|
|
|
|
// Add whitespace between the previous token and this one.
|
|
|
|
Range token_range = Resolve(clang_getTokenExtent(cx_tu, tokens[i]));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-06-15 03:12:52 +00:00
|
|
|
// static
|
2017-12-19 06:15:46 +00:00
|
|
|
int IndexFile::kCurrentVersion = 6;
|
2017-05-20 21:45:46 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFile::IndexFile(const std::string& path) : id_cache(path), path(path) {
|
2017-02-26 19:45:59 +00:00
|
|
|
// TODO: Reconsider if we should still be reusing the same id_cache.
|
2017-02-25 23:59:09 +00:00
|
|
|
// Preallocate any existing resolved ids.
|
2017-02-28 06:41:42 +00:00
|
|
|
for (const auto& entry : id_cache.usr_to_type_id)
|
2017-05-12 06:08:15 +00:00
|
|
|
types.push_back(IndexType(entry.second, entry.first));
|
2017-02-28 06:41:42 +00:00
|
|
|
for (const auto& entry : id_cache.usr_to_func_id)
|
2017-05-12 06:08:15 +00:00
|
|
|
funcs.push_back(IndexFunc(entry.second, entry.first));
|
2017-02-28 06:41:42 +00:00
|
|
|
for (const auto& entry : id_cache.usr_to_var_id)
|
2017-05-12 06:08:15 +00:00
|
|
|
vars.push_back(IndexVar(entry.second, entry.first));
|
2017-02-25 23:59:09 +00:00
|
|
|
}
|
2017-02-19 02:34:51 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
// TODO: Optimize for const char*?
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexTypeId IndexFile::ToTypeId(const std::string& usr) {
|
2017-02-28 06:41:42 +00:00
|
|
|
auto it = id_cache.usr_to_type_id.find(usr);
|
|
|
|
if (it != id_cache.usr_to_type_id.end())
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId id(types.size());
|
2017-05-12 06:08:15 +00:00
|
|
|
types.push_back(IndexType(id, usr));
|
2017-02-28 06:41:42 +00:00
|
|
|
id_cache.usr_to_type_id[usr] = id;
|
|
|
|
id_cache.type_id_to_usr[id] = usr;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFuncId IndexFile::ToFuncId(const std::string& usr) {
|
2017-02-28 06:41:42 +00:00
|
|
|
auto it = id_cache.usr_to_func_id.find(usr);
|
|
|
|
if (it != id_cache.usr_to_func_id.end())
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId id(funcs.size());
|
2017-05-12 06:08:15 +00:00
|
|
|
funcs.push_back(IndexFunc(id, usr));
|
2017-02-28 06:41:42 +00:00
|
|
|
id_cache.usr_to_func_id[usr] = id;
|
|
|
|
id_cache.func_id_to_usr[id] = usr;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexVarId IndexFile::ToVarId(const std::string& usr) {
|
2017-02-28 06:41:42 +00:00
|
|
|
auto it = id_cache.usr_to_var_id.find(usr);
|
|
|
|
if (it != id_cache.usr_to_var_id.end())
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexVarId id(vars.size());
|
2017-05-12 06:08:15 +00:00
|
|
|
vars.push_back(IndexVar(id, usr));
|
2017-02-28 06:41:42 +00:00
|
|
|
id_cache.usr_to_var_id[usr] = id;
|
|
|
|
id_cache.var_id_to_usr[id] = usr;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexTypeId IndexFile::ToTypeId(const CXCursor& cursor) {
|
2017-11-11 19:31:05 +00:00
|
|
|
return ToTypeId(ClangCursor(cursor).get_usr());
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFuncId IndexFile::ToFuncId(const CXCursor& cursor) {
|
2017-11-11 19:31:05 +00:00
|
|
|
return ToFuncId(ClangCursor(cursor).get_usr());
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexVarId IndexFile::ToVarId(const CXCursor& cursor) {
|
2017-11-11 19:31:05 +00:00
|
|
|
return ToVarId(ClangCursor(cursor).get_usr());
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* IndexFile::Resolve(IndexTypeId id) {
|
2017-02-25 23:59:09 +00:00
|
|
|
return &types[id.id];
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFunc* IndexFile::Resolve(IndexFuncId id) {
|
2017-02-25 23:59:09 +00:00
|
|
|
return &funcs[id.id];
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexVar* IndexFile::Resolve(IndexVarId id) {
|
2017-02-25 23:59:09 +00:00
|
|
|
return &vars[id.id];
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::string IndexFile::ToString() {
|
2017-03-07 09:32:29 +00:00
|
|
|
return Serialize(*this);
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType::IndexType(IndexTypeId id, const std::string& usr)
|
2017-12-12 07:56:50 +00:00
|
|
|
: usr(usr), id(id) {
|
2017-02-24 08:39:25 +00:00
|
|
|
assert(usr.size() > 0);
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
void RemoveItem(std::vector<Range>& ranges, Range to_remove) {
|
2017-04-13 07:47:47 +00:00
|
|
|
auto it = std::find(ranges.begin(), ranges.end(), to_remove);
|
|
|
|
if (it != ranges.end())
|
|
|
|
ranges.erase(it);
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:56:39 +00:00
|
|
|
template <typename T>
|
|
|
|
void UniqueAdd(std::vector<T>& values, T value) {
|
|
|
|
if (std::find(values.begin(), values.end(), value) == values.end())
|
|
|
|
values.push_back(value);
|
2017-02-24 08:39:25 +00:00
|
|
|
}
|
2017-02-20 06:40:55 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
IdCache::IdCache(const std::string& primary_file)
|
2017-09-22 01:14:57 +00:00
|
|
|
: primary_file(primary_file) {}
|
2017-02-27 07:32:10 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
template <typename T>
|
2017-02-21 05:16:45 +00:00
|
|
|
bool Contains(const std::vector<T>& vec, const T& element) {
|
|
|
|
for (const T& entry : vec) {
|
|
|
|
if (entry == element)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Skip diagnostics in system headers.
|
|
|
|
CXSourceLocation diag_loc = clang_getDiagnosticLocation(diagnostic);
|
|
|
|
if (clang_Location_isInSystemHeader(diag_loc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
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.
|
2017-07-12 22:02:48 +00:00
|
|
|
optional<lsDiagnostic> ls_diagnostic =
|
|
|
|
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);
|
|
|
|
|
|
|
|
IndexFile* db = ConsumeFile(param, cx_file);
|
|
|
|
if (!db)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
IndexInclude include;
|
|
|
|
include.line = line;
|
|
|
|
include.resolved_path = FileName(file->file);
|
2017-12-16 05:24:31 +00:00
|
|
|
if (!include.resolved_path.empty())
|
|
|
|
db->includes.push_back(include);
|
2017-05-21 03:46:15 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult DumpVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
int* level) {
|
2017-02-20 06:40:55 +00:00
|
|
|
for (int i = 0; i < *level; ++i)
|
2017-03-05 19:48:05 +00:00
|
|
|
std::cerr << " ";
|
2017-11-04 22:29:03 +00:00
|
|
|
std::cerr << ToString(cursor.get_kind()) << " " << cursor.get_spelling()
|
|
|
|
<< std::endl;
|
2017-02-20 06:40:55 +00:00
|
|
|
|
|
|
|
*level += 1;
|
|
|
|
cursor.VisitChildren(&DumpVisitor, level);
|
|
|
|
*level -= 1;
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Continue;
|
2017-02-20 06:40:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
void Dump(ClangCursor cursor) {
|
2017-02-20 06:40:55 +00:00
|
|
|
int level = 0;
|
|
|
|
cursor.VisitChildren(&DumpVisitor, &level);
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:00:58 +00:00
|
|
|
struct FindChildOfKindParam {
|
|
|
|
CXCursorKind target_kind;
|
2017-11-11 19:31:05 +00:00
|
|
|
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 FindChildOfKindVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
FindChildOfKindParam* param) {
|
2017-02-20 02:00:58 +00:00
|
|
|
if (cursor.get_kind() == param->target_kind) {
|
|
|
|
param->result = cursor;
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Break;
|
2017-02-20 02:00:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
return ClangCursor::VisitResult::Recurse;
|
2017-02-20 02:00:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
optional<ClangCursor> FindChildOfKind(ClangCursor cursor, CXCursorKind kind) {
|
2017-02-20 02:00:58 +00:00
|
|
|
FindChildOfKindParam param(kind);
|
|
|
|
cursor.VisitChildren(&FindChildOfKindVisitor, ¶m);
|
|
|
|
return param.result;
|
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor::VisitResult FindTypeVisitor(ClangCursor cursor,
|
|
|
|
ClangCursor parent,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
optional<ClangCursor> FindType(ClangCursor cursor) {
|
|
|
|
optional<ClangCursor> result;
|
2017-02-20 19:08:27 +00:00
|
|
|
cursor.VisitChildren(&FindTypeVisitor, &result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-23 02:10:23 +00:00
|
|
|
bool IsGlobalContainer(const CXIdxContainerInfo* container) {
|
2017-12-23 16:01:43 +00:00
|
|
|
if (!container)
|
|
|
|
return false;
|
2017-12-23 02:10:23 +00:00
|
|
|
|
2017-12-23 16:01:43 +00:00
|
|
|
switch (container->cursor.kind) {
|
2017-12-23 02:10:23 +00:00
|
|
|
case CXCursor_Namespace:
|
|
|
|
case CXCursor_TranslationUnit:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2017-12-23 16:01:43 +00:00
|
|
|
}
|
2017-12-23 02:10:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
bool IsTypeDefinition(const CXIdxContainerInfo* container) {
|
|
|
|
if (!container)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (container->cursor.kind) {
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXCursor_EnumDecl:
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
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;
|
2017-02-20 21:48:46 +00:00
|
|
|
int has_processed_any = false;
|
2017-11-11 19:31:05 +00:00
|
|
|
optional<ClangCursor> previous_cursor;
|
2017-04-07 06:57:26 +00:00
|
|
|
optional<IndexTypeId> initial_type;
|
2017-02-20 21:48:46 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
VisitDeclForTypeUsageParam(IndexFile* db) : db(db) {}
|
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
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
std::string referenced_usr =
|
|
|
|
cursor.get_referenced()
|
|
|
|
.template_specialization_to_template_definition()
|
|
|
|
.get_usr();
|
2017-03-05 23:44:20 +00:00
|
|
|
// TODO: things in STL cause this to be empty. Figure out why and document it.
|
|
|
|
if (referenced_usr == "")
|
|
|
|
return;
|
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId ref_type_id = db->ToTypeId(referenced_usr);
|
2017-03-05 22:49:23 +00:00
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
if (!param->initial_type)
|
|
|
|
param->initial_type = ref_type_id;
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* ref_type_def = db->Resolve(ref_type_id);
|
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.
|
2017-05-10 06:13:13 +00:00
|
|
|
Range loc = ResolveSpelling(cursor.cx_cursor);
|
2017-04-19 05:28:33 +00:00
|
|
|
UniqueAdd(ref_type_def->uses, loc);
|
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
|
|
|
// Finds the cursor associated with the declaration type of |cursor|. This
|
|
|
|
// strips
|
2017-03-05 22:49:23 +00:00
|
|
|
// qualifies from |cursor| (ie, Foo* => Foo) and removes template arguments
|
|
|
|
// (ie, Foo<A,B> => Foo<*,*>).
|
2017-05-12 06:08:15 +00:00
|
|
|
optional<IndexTypeId> ResolveToDeclarationType(IndexFile* db,
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor cursor) {
|
|
|
|
ClangCursor declaration = cursor.get_declaration();
|
2017-03-05 22:49:23 +00:00
|
|
|
declaration = declaration.template_specialization_to_template_definition();
|
|
|
|
std::string usr = declaration.get_usr();
|
|
|
|
if (usr != "")
|
|
|
|
return db->ToTypeId(usr);
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
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.
|
2017-04-07 06:57:26 +00:00
|
|
|
optional<IndexTypeId> AddDeclTypeUsages(
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFile* db,
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor decl_cursor,
|
2017-03-17 07:58:41 +00:00
|
|
|
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
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
VisitDeclForTypeUsageParam param(db);
|
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
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
return param.initial_type;
|
|
|
|
}
|
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
|
|
|
|
// different USR.
|
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
// ClangCursor ref =
|
2017-03-17 07:58:41 +00:00
|
|
|
// cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr();
|
|
|
|
// std::string ref_usr =
|
|
|
|
// cursor.get_referenced().template_specialization_to_template_definition().get_type().strip_qualifiers().get_usr();
|
|
|
|
std::string ref_usr =
|
|
|
|
cursor.get_referenced()
|
|
|
|
.template_specialization_to_template_definition()
|
|
|
|
.get_usr();
|
|
|
|
// std::string ref_usr = ref.get_usr();
|
|
|
|
if (ref_usr == "")
|
|
|
|
break;
|
|
|
|
|
2017-05-10 06:13:13 +00:00
|
|
|
Range loc = ResolveSpelling(cursor.cx_cursor);
|
2017-04-08 23:16:30 +00:00
|
|
|
IndexVarId ref_id = db->ToVarId(ref_usr);
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexVar* ref_def = db->Resolve(ref_id);
|
2017-04-13 07:47:47 +00:00
|
|
|
UniqueAdd(ref_def->uses, loc);
|
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
|
|
|
void AddDeclInitializerUsages(IndexFile* db, ClangCursor decl_cursor) {
|
2017-03-14 04:31:53 +00:00
|
|
|
decl_cursor.VisitChildren(&AddDeclInitializerUsagesVisitor, db);
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
bool AreEqualLocations(CXIdxLoc loc, CXCursor cursor) {
|
|
|
|
// clang_getCursorExtent
|
|
|
|
// clang_Cursor_getSpellingNameRange
|
|
|
|
|
|
|
|
return clang_equalLocations(
|
2017-09-22 01:14:57 +00:00
|
|
|
clang_indexLoc_getCXSourceLocation(loc),
|
|
|
|
// clang_getRangeStart(clang_getCursorExtent(cursor)));
|
|
|
|
clang_getRangeStart(clang_Cursor_getSpellingNameRange(cursor, 0, 0)));
|
2017-04-05 08:06:18 +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
|
|
|
CXSourceLocation start = clang_getRangeStart(cx_source_range);
|
|
|
|
if (clang_Location_isInSystemHeader(start))
|
|
|
|
break;
|
|
|
|
CXFile file;
|
|
|
|
Range decl_loc_spelling = Resolve(cx_source_range, &file);
|
|
|
|
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)
|
|
|
|
|
|
|
|
std::string decl_usr;
|
|
|
|
if (cursor.get_kind() == CXCursor_MacroDefinition)
|
|
|
|
decl_usr = cursor.get_usr();
|
|
|
|
else
|
|
|
|
decl_usr = cursor.get_referenced().get_usr();
|
|
|
|
|
|
|
|
IndexVarId var_id = db->ToVarId(decl_usr);
|
|
|
|
IndexVar* var_def = db->Resolve(var_id);
|
|
|
|
UniqueAdd(var_def->uses, decl_loc_spelling);
|
|
|
|
|
|
|
|
if (cursor.get_kind() == CXCursor_MacroDefinition) {
|
2017-12-13 08:22:02 +00:00
|
|
|
CXSourceRange cx_extent = clang_getCursorExtent(cursor.cx_cursor);
|
2017-05-21 01:58:54 +00:00
|
|
|
var_def->def.short_name = cursor.get_display_name();
|
2017-12-19 05:20:00 +00:00
|
|
|
var_def->def.detailed_name = cursor.get_display_name();
|
|
|
|
var_def->def.hover =
|
|
|
|
"#define " + GetDocumentContentInRange(param->tu->cx_tu, cx_extent);
|
2017-05-21 01:58:54 +00:00
|
|
|
var_def->def.is_local = false;
|
2017-06-29 06:28:29 +00:00
|
|
|
var_def->def.is_macro = true;
|
2017-05-21 01:58:54 +00:00
|
|
|
var_def->def.definition_spelling = decl_loc_spelling;
|
2017-12-13 08:22:02 +00:00
|
|
|
var_def->def.definition_extent = Resolve(cx_extent, nullptr);
|
2017-05-21 01:58:54 +00:00
|
|
|
}
|
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-01 17:57:03 +00:00
|
|
|
void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
|
2017-09-22 01:14:57 +00:00
|
|
|
if (!kIndexStdDeclarations &&
|
|
|
|
clang_Location_isInSystemHeader(
|
|
|
|
clang_indexLoc_getCXSourceLocation(decl->loc)))
|
2017-03-06 06:47:37 +00:00
|
|
|
return;
|
2017-02-21 07:33:05 +00:00
|
|
|
|
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
|
2017-12-01 17:50:39 +00:00
|
|
|
LanguageId decl_lang = [decl]() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-11 05:26:27 +00:00
|
|
|
NamespaceHelper* ns = ¶m->ns;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
switch (decl->entityInfo->kind) {
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXNamespace: {
|
|
|
|
ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer,
|
|
|
|
decl->entityInfo->name);
|
2017-03-05 23:44:20 +00:00
|
|
|
break;
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2017-03-05 23:44:20 +00:00
|
|
|
|
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: {
|
2017-05-10 06:13:13 +00:00
|
|
|
Range decl_loc_spelling = ResolveSpelling(decl->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor decl_cursor = decl->cursor;
|
2017-03-14 04:31:53 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Do not index implicit template instantiations.
|
|
|
|
if (decl_cursor !=
|
|
|
|
decl_cursor.template_specialization_to_template_definition())
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
std::string decl_usr = decl_cursor.get_usr();
|
2017-02-20 19:08:27 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexVarId var_id = db->ToVarId(decl->entityInfo->USR);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexVar* var = db->Resolve(var_id);
|
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) {
|
2017-05-27 17:03:49 +00:00
|
|
|
var->def.short_name = decl->entityInfo->name;
|
2017-04-14 05:18:02 +00:00
|
|
|
|
2017-11-04 22:29:03 +00:00
|
|
|
std::string type_name =
|
|
|
|
ToString(clang_getTypeSpelling(clang_getCursorType(decl->cursor)));
|
2017-12-22 16:59:37 +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";
|
|
|
|
|
2017-12-22 22:48:55 +00:00
|
|
|
{
|
|
|
|
std::string qualified_name =
|
|
|
|
ns->QualifiedName(decl->semanticContainer, var->def.short_name);
|
|
|
|
if (decl->entityInfo->kind == CXIdxEntity_EnumConstant)
|
|
|
|
var->def.detailed_name = std::move(qualified_name);
|
|
|
|
else
|
|
|
|
var->def.detailed_name = type_name + " " + std::move(qualified_name);
|
|
|
|
}
|
2017-05-21 01:26:50 +00:00
|
|
|
|
2017-12-23 02:10:23 +00:00
|
|
|
bool is_system = clang_Location_isInSystemHeader(
|
2017-12-23 16:01:43 +00:00
|
|
|
clang_indexLoc_getCXSourceLocation(decl->loc));
|
2017-12-23 02:10:23 +00:00
|
|
|
var->def.is_global =
|
|
|
|
!is_system && IsGlobalContainer(decl->semanticContainer);
|
|
|
|
var->def.is_member =
|
|
|
|
!is_system && IsTypeDefinition(decl->semanticContainer);
|
2017-12-23 16:01:43 +00:00
|
|
|
var->def.is_local =
|
|
|
|
!is_system && !var->def.is_global && !var->def.is_member;
|
2017-05-21 01:26:50 +00:00
|
|
|
|
2017-03-06 06:23:41 +00:00
|
|
|
//}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
if (decl->isDefinition) {
|
2017-05-27 17:03:49 +00:00
|
|
|
var->def.definition_spelling = ResolveSpelling(decl->cursor);
|
2017-09-22 01:14:57 +00:00
|
|
|
var->def.definition_extent = ResolveExtent(decl->cursor);
|
|
|
|
;
|
|
|
|
} else {
|
2017-05-27 17:03:49 +00:00
|
|
|
var->def.declaration = ResolveSpelling(decl->cursor);
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
2017-05-27 17:03:49 +00:00
|
|
|
UniqueAdd(var->uses, decl_loc_spelling);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
AddDeclInitializerUsages(db, decl_cursor);
|
2017-05-27 17:03:49 +00:00
|
|
|
var = db->Resolve(var_id);
|
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.
|
2017-09-22 01:14:57 +00:00
|
|
|
AddDeclTypeUsages(db, decl_cursor, 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.
|
|
|
|
if (!decl->isRedeclaration) {
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<IndexTypeId> var_type =
|
|
|
|
ResolveToDeclarationType(db, decl_cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
if (var_type.has_value()) {
|
|
|
|
// Don't treat enum definition variables as instantiations.
|
2017-09-22 01:14:57 +00:00
|
|
|
bool is_enum_member =
|
|
|
|
decl->semanticContainer &&
|
|
|
|
decl->semanticContainer->cursor.kind == CXCursor_EnumDecl;
|
2017-04-03 01:34:15 +00:00
|
|
|
if (!is_enum_member)
|
2017-04-21 07:03:33 +00:00
|
|
|
db->Resolve(var_type.value())->instances.push_back(var_id);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-05-27 17:03:49 +00:00
|
|
|
var->def.variable_type = var_type.value();
|
2017-04-03 01:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
// TODO: Refactor handlers so more things are under 'if
|
|
|
|
// (!decl->isRedeclaration)'
|
2017-03-17 07:58:41 +00:00
|
|
|
if (decl->isDefinition && IsTypeDefinition(decl->semanticContainer)) {
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId declaring_type_id =
|
2017-09-22 01:14:57 +00:00
|
|
|
db->ToTypeId(decl->semanticContainer->cursor);
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* declaring_type_def = db->Resolve(declaring_type_id);
|
2017-05-27 17:03:49 +00:00
|
|
|
var->def.declaring_type = declaring_type_id;
|
2017-03-17 07:58:41 +00:00
|
|
|
declaring_type_def->def.vars.push_back(var_id);
|
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: {
|
2017-05-27 20:10:06 +00:00
|
|
|
Range decl_spelling = ResolveSpelling(decl->cursor);
|
2017-05-28 01:53:22 +00:00
|
|
|
Range decl_extent = ResolveExtent(decl->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor decl_cursor = decl->cursor;
|
|
|
|
ClangCursor decl_cursor_resolved =
|
2017-09-22 01:14:57 +00:00
|
|
|
decl_cursor.template_specialization_to_template_definition();
|
2017-05-27 19:56:39 +00:00
|
|
|
bool is_template_specialization = decl_cursor != decl_cursor_resolved;
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-27 19:56:39 +00:00
|
|
|
IndexFuncId func_id = db->ToFuncId(decl_cursor_resolved.cx_cursor);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexFunc* func = db->Resolve(func_id);
|
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.
|
2017-09-22 01:14:57 +00:00
|
|
|
AddDeclTypeUsages(db, decl_cursor, decl->semanticContainer,
|
|
|
|
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) {
|
2017-09-22 01:14:57 +00:00
|
|
|
// assert(!func->def.definition_spelling);
|
|
|
|
// assert(!func->def.definition_extent);
|
2017-05-27 20:10:06 +00:00
|
|
|
func->def.definition_spelling = decl_spelling;
|
2017-05-28 01:53:22 +00:00
|
|
|
func->def.definition_extent = decl_extent;
|
2017-09-22 01:14:57 +00:00
|
|
|
} else {
|
2017-05-27 21:09:20 +00:00
|
|
|
IndexFunc::Declaration declaration;
|
|
|
|
declaration.spelling = decl_spelling;
|
2017-05-28 01:53:22 +00:00
|
|
|
declaration.extent = decl_extent;
|
2017-09-22 01:14:57 +00:00
|
|
|
declaration.content = GetDocumentContentInRange(
|
|
|
|
param->tu->cx_tu, clang_getCursorExtent(decl->cursor));
|
2017-05-27 21:09:20 +00:00
|
|
|
|
2017-05-28 01:53:22 +00:00
|
|
|
// Add parameters.
|
2017-11-11 19:31:05 +00:00
|
|
|
for (ClangCursor arg : decl_cursor.get_arguments()) {
|
2017-05-27 21:09:20 +00:00
|
|
|
switch (arg.get_kind()) {
|
|
|
|
case CXCursor_ParmDecl: {
|
2017-05-28 01:53:22 +00:00
|
|
|
Range param_spelling = ResolveSpelling(arg.cx_cursor);
|
|
|
|
|
|
|
|
// If the name is empty (which is common for parameters), clang
|
|
|
|
// will report a range with length 1, which is not correct.
|
2017-09-22 01:14:57 +00:00
|
|
|
if (param_spelling.start.column ==
|
|
|
|
(param_spelling.end.column - 1) &&
|
2017-05-28 01:53:22 +00:00
|
|
|
arg.get_display_name().empty()) {
|
|
|
|
param_spelling.end.column -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
declaration.param_spellings.push_back(param_spelling);
|
2017-05-27 21:09:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func->declarations.push_back(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) {
|
2017-05-27 17:03:49 +00:00
|
|
|
func->def.short_name = decl->entityInfo->name;
|
2017-04-14 08:21:03 +00:00
|
|
|
|
2017-12-01 17:50:39 +00:00
|
|
|
// Set the |is_operator| flag to true if the function name starts with
|
|
|
|
// "operator"
|
|
|
|
func->def.is_operator =
|
|
|
|
func->def.short_name.compare(0, 8, "operator") == 0;
|
2017-12-01 16:08:19 +00:00
|
|
|
|
2017-04-15 05:02:03 +00:00
|
|
|
// Build detailed name. The type desc looks like void (void *). We
|
|
|
|
// insert the qualified name before the first '('.
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string qualified_name =
|
|
|
|
ns->QualifiedName(decl->semanticContainer, func->def.short_name);
|
2017-04-15 05:02:03 +00:00
|
|
|
std::string type_desc = decl_cursor.get_type_description();
|
2017-12-22 23:48:27 +00:00
|
|
|
{
|
2017-12-23 17:29:13 +00:00
|
|
|
size_t balance = 0, offset;
|
|
|
|
for (offset = type_desc.size(); offset;) {
|
|
|
|
offset--;
|
|
|
|
if (type_desc[offset] == ')')
|
|
|
|
balance++;
|
|
|
|
// Balanced paren pair that may appear before the paren enclosing
|
|
|
|
// function parameters, see clang/lib/AST/TypePrinter.cpp
|
|
|
|
else if (type_desc[offset] == '(' && --balance == 0 &&
|
|
|
|
!((offset >= 5 &&
|
|
|
|
!type_desc.compare(offset - 5, 5, "throw")) ||
|
|
|
|
(offset >= 6 &&
|
|
|
|
!type_desc.compare(offset - 6, 6, "typeof")) ||
|
2017-12-23 17:57:12 +00:00
|
|
|
(offset >= 7 &&
|
|
|
|
!type_desc.compare(offset - 7, 7, "_Atomic")) ||
|
2017-12-23 17:29:13 +00:00
|
|
|
(offset >= 8 &&
|
|
|
|
!type_desc.compare(offset - 8, 8, "decltype")) ||
|
2017-12-23 17:57:12 +00:00
|
|
|
(offset >= 8 &&
|
|
|
|
!type_desc.compare(offset - 8, 8, "noexcept")) ||
|
2017-12-23 17:29:13 +00:00
|
|
|
(offset >= 13 &&
|
|
|
|
!type_desc.compare(offset - 13, 13, "__attribute__"))))
|
|
|
|
break;
|
2017-12-22 23:48:27 +00:00
|
|
|
}
|
|
|
|
if (offset > 0) {
|
|
|
|
type_desc.insert(offset, qualified_name);
|
|
|
|
func->def.detailed_name = type_desc;
|
|
|
|
} else {
|
|
|
|
// type_desc is probably the name of a typedef.
|
|
|
|
func->def.detailed_name = type_desc + " " + qualified_name;
|
|
|
|
}
|
2017-12-12 08:02:27 +00:00
|
|
|
}
|
2017-04-14 05:18:02 +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)) {
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId declaring_type_id =
|
2017-03-17 07:58:41 +00:00
|
|
|
db->ToTypeId(decl->semanticContainer->cursor);
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* declaring_type_def = db->Resolve(declaring_type_id);
|
2017-05-27 17:03:49 +00:00
|
|
|
func->def.declaring_type = declaring_type_id;
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// Mark a type reference at the ctor/dtor location.
|
2017-05-27 19:56:39 +00:00
|
|
|
if (decl->entityInfo->kind == CXIdxEntity_CXXConstructor)
|
2017-05-27 20:10:06 +00:00
|
|
|
UniqueAdd(declaring_type_def->uses, decl_spelling);
|
2017-05-27 19:58:40 +00:00
|
|
|
if (decl->entityInfo->kind == CXIdxEntity_CXXDestructor) {
|
2017-05-27 20:10:06 +00:00
|
|
|
Range dtor_type_range = decl_spelling;
|
2017-09-22 01:14:57 +00:00
|
|
|
dtor_type_range.start.column += 1; // Don't count the leading ~
|
2017-05-27 19:58:40 +00:00
|
|
|
UniqueAdd(declaring_type_def->uses, dtor_type_range);
|
|
|
|
}
|
2017-05-27 19:56:39 +00:00
|
|
|
|
|
|
|
// Add function to declaring type.
|
|
|
|
UniqueAdd(declaring_type_def->def.funcs, func_id);
|
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();
|
2017-05-27 16:57:52 +00:00
|
|
|
IndexFuncId parent_id = db->ToFuncId(parent.get_usr());
|
|
|
|
IndexFunc* parent_def = db->Resolve(parent_id);
|
2017-05-27 17:03:49 +00:00
|
|
|
func = db->Resolve(func_id); // ToFuncId invalidated func_def
|
2017-05-27 16:57:52 +00:00
|
|
|
|
2017-12-19 06:15:46 +00:00
|
|
|
func->def.base.push_back(parent_id);
|
2017-05-27 16:57:52 +00:00
|
|
|
parent_def->derived.push_back(func_id);
|
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: {
|
2017-05-10 06:13:13 +00:00
|
|
|
Range decl_loc_spelling = ResolveSpelling(decl->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// 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.
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<IndexTypeId> alias_of = AddDeclTypeUsages(
|
|
|
|
db, decl->cursor, decl->semanticContainer, decl->lexicalContainer);
|
2017-02-20 02:35:56 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId type_id = db->ToTypeId(decl->entityInfo->USR);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexType* type = db->Resolve(type_id);
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
if (alias_of)
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.alias_of = alias_of.value();
|
2017-12-19 01:21:31 +00:00
|
|
|
|
|
|
|
Range spell = ResolveSpelling(decl->cursor);
|
|
|
|
Range extent = ResolveExtent(decl->cursor);
|
|
|
|
type->def.definition_spelling = spell;
|
|
|
|
type->def.definition_extent = extent;
|
|
|
|
|
2017-12-19 05:20:00 +00:00
|
|
|
type->def.short_name = decl->entityInfo->name;
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.detailed_name =
|
|
|
|
ns->QualifiedName(decl->semanticContainer, type->def.short_name);
|
2017-12-19 05:20:00 +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.
|
|
|
|
// TODO Think how to display multi-line declaration like `typedef struct {
|
|
|
|
// ... } foo;` https://github.com/jacobdufault/cquery/issues/29
|
2017-12-19 07:05:12 +00:00
|
|
|
if (extent.end.line - extent.start.line <
|
|
|
|
kMaxLinesDisplayTypeAliasDeclarations) {
|
|
|
|
FileContentsWithOffsets& fc = param->file_contents[db->path];
|
|
|
|
optional<int> extent_start = fc.ToOffset(extent.start),
|
|
|
|
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) {
|
|
|
|
type->def.hover =
|
|
|
|
fc.contents.substr(*extent_start, *spell_start - *extent_start) +
|
2017-12-19 01:21:31 +00:00
|
|
|
type->def.detailed_name +
|
2017-12-19 07:05:12 +00:00
|
|
|
fc.contents.substr(*spell_end, *extent_end - *spell_end);
|
2017-12-19 01:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-27 17:03:49 +00:00
|
|
|
UniqueAdd(type->uses, decl_loc_spelling);
|
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:
|
|
|
|
case CXIdxEntity_CXXClass: {
|
2017-05-10 06:13:13 +00:00
|
|
|
Range decl_loc_spelling = ResolveSpelling(decl->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId type_id = db->ToTypeId(decl->entityInfo->USR);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexType* type = db->Resolve(type_id);
|
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) {
|
|
|
|
|
|
|
|
// name can be null in an anonymous struct (see
|
|
|
|
// tests/types/anonymous_struct.cc).
|
|
|
|
if (decl->entityInfo->name) {
|
|
|
|
ns->RegisterQualifiedName(decl->entityInfo->USR,
|
|
|
|
decl->semanticContainer,
|
|
|
|
decl->entityInfo->name);
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.short_name = decl->entityInfo->name;
|
2017-03-17 07:58:41 +00:00
|
|
|
} else {
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.short_name = "<anonymous>";
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.detailed_name =
|
|
|
|
ns->QualifiedName(decl->semanticContainer, type->def.short_name);
|
2017-03-17 07:58:41 +00:00
|
|
|
// }
|
|
|
|
|
2017-09-13 05:51:13 +00:00
|
|
|
if (decl->isDefinition) {
|
|
|
|
type->def.definition_spelling = ResolveSpelling(decl->cursor);
|
|
|
|
type->def.definition_extent = ResolveExtent(decl->cursor);
|
|
|
|
}
|
2017-05-27 17:03:49 +00:00
|
|
|
UniqueAdd(type->uses, decl_loc_spelling);
|
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];
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
AddDeclTypeUsages(db, base_class->cursor, decl->semanticContainer,
|
|
|
|
decl->lexicalContainer);
|
2017-04-07 06:57:26 +00:00
|
|
|
optional<IndexTypeId> parent_type_id =
|
2017-03-17 07:58:41 +00:00
|
|
|
ResolveToDeclarationType(db, base_class->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
// type_def ptr could be invalidated by ResolveToDeclarationType.
|
2017-05-27 17:03:49 +00:00
|
|
|
type = db->Resolve(type_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
if (parent_type_id) {
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexType* parent_type_def = db->Resolve(parent_type_id.value());
|
2017-03-17 07:58:41 +00:00
|
|
|
parent_type_def->derived.push_back(type_id);
|
2017-05-27 17:03:49 +00:00
|
|
|
type->def.parents.push_back(parent_type_id.value());
|
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
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
default:
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cerr << "!! Unhandled indexDeclaration: "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(decl->cursor).ToString() << " at "
|
2017-09-22 01:14:57 +00:00
|
|
|
<< ResolveSpelling(decl->cursor).start.ToString() << std::endl;
|
2017-03-17 07:58:41 +00:00
|
|
|
std::cerr << " entityInfo->kind = " << decl->entityInfo->kind
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " entityInfo->USR = " << decl->entityInfo->USR
|
|
|
|
<< std::endl;
|
|
|
|
if (decl->declAsContainer)
|
|
|
|
std::cerr << " declAsContainer = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(decl->declAsContainer->cursor).ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
if (decl->semanticContainer)
|
|
|
|
std::cerr << " semanticContainer = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(decl->semanticContainer->cursor).ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
if (decl->lexicalContainer)
|
|
|
|
std::cerr << " lexicalContainer = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(decl->lexicalContainer->cursor).get_usr()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 18:36:32 +00:00
|
|
|
bool IsFunctionCallContext(CXCursorKind kind) {
|
2017-02-20 00:56:56 +00:00
|
|
|
switch (kind) {
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXCursor_FunctionDecl:
|
2017-04-08 18:36:32 +00:00
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
case CXCursor_Constructor:
|
|
|
|
case CXCursor_Destructor:
|
|
|
|
case CXCursor_ConversionFunction:
|
|
|
|
case CXCursor_FunctionTemplate:
|
|
|
|
case CXCursor_OverloadedDeclRef:
|
|
|
|
// TODO: we need to test lambdas
|
|
|
|
case CXCursor_LambdaExpr:
|
2017-03-17 07:58:41 +00:00
|
|
|
return true;
|
2017-05-21 23:48:21 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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);
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
switch (ref->referencedEntity->kind) {
|
2017-05-27 19:33:18 +00:00
|
|
|
case CXIdxEntity_CXXNamespaceAlias:
|
2017-03-17 07:58:41 +00:00
|
|
|
case CXIdxEntity_CXXNamespace: {
|
|
|
|
// We don't index namespace usages.
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
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: {
|
2017-05-10 06:13:13 +00:00
|
|
|
Range loc_spelling = ResolveSpelling(ref->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor referenced = ref->referencedEntity->cursor;
|
2017-03-17 07:58:41 +00:00
|
|
|
referenced = referenced.template_specialization_to_template_definition();
|
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexVarId var_id = db->ToVarId(referenced.get_usr());
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexVar* var = db->Resolve(var_id);
|
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.
|
2017-12-11 06:36:32 +00:00
|
|
|
if (var->def.short_name.empty()) {
|
2017-12-12 17:49:52 +00:00
|
|
|
CXFile referenced_file;
|
2017-12-23 16:01:43 +00:00
|
|
|
Range spelling =
|
|
|
|
ResolveSpelling(referenced.cx_cursor, &referenced_file);
|
2017-12-12 17:49:52 +00:00
|
|
|
if (file == referenced_file) {
|
|
|
|
var->def.definition_spelling = spelling;
|
|
|
|
var->def.definition_extent = ResolveExtent(referenced.cx_cursor);
|
|
|
|
|
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.
|
2017-12-12 17:49:52 +00:00
|
|
|
var->def.short_name = referenced.get_spelling();
|
|
|
|
std::string type_name = ToString(
|
|
|
|
clang_getTypeSpelling(clang_getCursorType(referenced.cx_cursor)));
|
|
|
|
var->def.detailed_name = type_name + " " + var->def.short_name;
|
|
|
|
var->def.is_local = false;
|
2017-12-23 02:10:23 +00:00
|
|
|
var->def.is_member = true;
|
2017-12-12 17:49:52 +00:00
|
|
|
UniqueAdd(var->uses, ResolveSpelling(referenced.cx_cursor));
|
|
|
|
AddDeclInitializerUsages(db, referenced.cx_cursor);
|
|
|
|
// TODO Use proper semantic_container and lexical_container.
|
|
|
|
AddDeclTypeUsages(db, referenced.cx_cursor, nullptr, nullptr);
|
|
|
|
// TODO Other logic in OnIndexDeclaration may need to be adapted.
|
|
|
|
}
|
2017-12-11 06:36:32 +00:00
|
|
|
}
|
2017-05-27 17:03:49 +00:00
|
|
|
UniqueAdd(var->uses, loc_spelling);
|
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?
|
2017-05-10 06:13:13 +00:00
|
|
|
Range loc_spelling = ResolveSpelling(ref->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId called_id = db->ToFuncId(ref->referencedEntity->USR);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexFunc* called = db->Resolve(called_id);
|
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();`
|
|
|
|
(called->def.short_name.empty() ||
|
|
|
|
// 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`.
|
|
|
|
(called->def.short_name[0] != '~' &&
|
|
|
|
!CursorSpellingContainsString(ref->cursor, param->tu->cx_tu,
|
|
|
|
called->def.short_name)));
|
2017-05-23 06:47:27 +00:00
|
|
|
|
2017-04-08 18:36:32 +00:00
|
|
|
if (IsFunctionCallContext(ref->container->cursor.kind)) {
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId caller_id = db->ToFuncId(ref->container->cursor);
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexFunc* caller = db->Resolve(caller_id);
|
2017-05-23 06:47:27 +00:00
|
|
|
// Calling db->ToFuncId invalidates the FuncDef* ptrs.
|
2017-05-27 17:03:49 +00:00
|
|
|
called = db->Resolve(called_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
AddFuncRef(&caller->def.callees,
|
|
|
|
IndexFuncRef(called_id, loc_spelling, is_implicit));
|
|
|
|
AddFuncRef(&called->callers,
|
|
|
|
IndexFuncRef(caller_id, loc_spelling, is_implicit));
|
2017-03-17 07:58:41 +00:00
|
|
|
} else {
|
2017-05-27 17:03:49 +00:00
|
|
|
AddFuncRef(&called->callers, IndexFuncRef(loc_spelling, is_implicit));
|
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;
|
|
|
|
if (is_template && str_begin("make", ref->referencedEntity->name)) {
|
|
|
|
// Try to find the return type of called function. That type will have
|
|
|
|
// the constructor function we add a usage to.
|
2017-11-11 19:31:05 +00:00
|
|
|
optional<ClangCursor> opt_found_type = FindType(ref->cursor);
|
2017-11-09 03:55:13 +00:00
|
|
|
if (opt_found_type) {
|
|
|
|
std::string ctor_type_usr =
|
|
|
|
opt_found_type->get_referenced().get_usr();
|
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()) {
|
2017-11-09 03:55:13 +00:00
|
|
|
std::string type_desc = type.get_spelling();
|
|
|
|
if (!type_desc.empty())
|
|
|
|
call_type_desc.push_back(type_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the constructor and add a reference.
|
|
|
|
optional<std::string> ctor_usr =
|
|
|
|
param->ctors.TryFindConstructorUsr(ctor_type_usr, call_type_desc);
|
|
|
|
if (ctor_usr) {
|
|
|
|
IndexFunc* ctor = db->Resolve(db->ToFuncId(*ctor_usr));
|
|
|
|
AddFuncRef(&ctor->callers,
|
|
|
|
IndexFuncRef(loc_spelling, true /*is_implicit*/));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
case CXIdxEntity_CXXTypeAlias:
|
|
|
|
case CXIdxEntity_Enum:
|
|
|
|
case CXIdxEntity_Union:
|
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
case CXIdxEntity_CXXClass: {
|
2017-11-11 19:31:05 +00:00
|
|
|
ClangCursor referenced_cursor = ref->referencedEntity->cursor;
|
2017-09-22 01:14:57 +00:00
|
|
|
referenced_cursor =
|
|
|
|
referenced_cursor.template_specialization_to_template_definition();
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexTypeId referenced_id = db->ToTypeId(referenced_cursor.get_usr());
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-27 17:03:49 +00:00
|
|
|
IndexType* referenced = db->Resolve(referenced_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// The following will generate two TypeRefs to Foo, both located at the
|
|
|
|
// same spot (line 3, column 3). One of the parents will be set to
|
|
|
|
// CXIdxEntity_Variable, the other will be CXIdxEntity_Function. There
|
2017-05-27 17:03:49 +00:00
|
|
|
// does not appear to be a good way to disambiguate these references, as
|
|
|
|
// using parent type alone breaks other indexing tasks.
|
2017-03-17 07:58:41 +00:00
|
|
|
//
|
|
|
|
// To work around this, we check to see if the usage location has been
|
|
|
|
// inserted into all_uses previously.
|
|
|
|
//
|
|
|
|
// struct Foo {};
|
|
|
|
// void Make() {
|
|
|
|
// Foo f;
|
|
|
|
// }
|
|
|
|
//
|
2017-05-27 17:03:49 +00:00
|
|
|
UniqueAdd(referenced->uses, ResolveSpelling(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-03-17 07:58:41 +00:00
|
|
|
default:
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cerr << "!! Unhandled indexEntityReference: " << cursor.ToString()
|
|
|
|
<< " at " << ResolveSpelling(ref->cursor).start.ToString()
|
|
|
|
<< std::endl;
|
2017-03-17 07:58:41 +00:00
|
|
|
std::cerr << " ref->referencedEntity->kind = "
|
|
|
|
<< ref->referencedEntity->kind << std::endl;
|
|
|
|
if (ref->parentEntity)
|
|
|
|
std::cerr << " ref->parentEntity->kind = "
|
|
|
|
<< ref->parentEntity->kind << std::endl;
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cerr << " ref->loc = "
|
|
|
|
<< ResolveSpelling(ref->cursor).start.ToString() << std::endl;
|
2017-03-17 07:58:41 +00:00
|
|
|
std::cerr << " ref->kind = " << ref->kind << std::endl;
|
|
|
|
if (ref->parentEntity)
|
|
|
|
std::cerr << " parentEntity = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(ref->parentEntity->cursor).ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
if (ref->referencedEntity)
|
|
|
|
std::cerr << " referencedEntity = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(ref->referencedEntity->cursor).ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
if (ref->container)
|
|
|
|
std::cerr << " container = "
|
2017-11-11 19:31:05 +00:00
|
|
|
<< ClangCursor(ref->container->cursor).ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
FileContents::FileContents(const std::string& path, const std::string& content)
|
|
|
|
: path(path), content(content) {}
|
2017-04-11 05:26:27 +00:00
|
|
|
|
2017-12-19 07:05:12 +00:00
|
|
|
FileContentsWithOffsets::FileContentsWithOffsets() : line_offsets_{0} {}
|
|
|
|
|
|
|
|
FileContentsWithOffsets::FileContentsWithOffsets(std::string s) {
|
|
|
|
contents = s;
|
|
|
|
line_offsets_.push_back(0);
|
|
|
|
for (size_t i = 0; i < s.size(); i++)
|
|
|
|
if (s[i] == '\n')
|
|
|
|
line_offsets_.push_back(i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<int> FileContentsWithOffsets::ToOffset(Position p) const {
|
|
|
|
if (0 < p.line && size_t(p.line) <= line_offsets_.size()) {
|
|
|
|
int ret = line_offsets_[p.line - 1] + p.column - 1;
|
|
|
|
if (size_t(ret) <= contents.size())
|
|
|
|
return {ret};
|
|
|
|
}
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:01:43 +00:00
|
|
|
optional<std::string> FileContentsWithOffsets::ContentsInRange(
|
|
|
|
Range range) const {
|
2017-12-19 07:05:12 +00:00
|
|
|
optional<int> start_offset = ToOffset(range.start),
|
|
|
|
end_offset = ToOffset(range.end);
|
|
|
|
if (start_offset && end_offset && *start_offset < *end_offset)
|
|
|
|
return {contents.substr(*start_offset, *end_offset - *start_offset)};
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> Parse(
|
2017-09-22 01:14:57 +00:00
|
|
|
Config* config,
|
|
|
|
FileConsumer::SharedState* file_consumer_shared,
|
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,
|
2017-05-17 07:08:45 +00:00
|
|
|
PerformanceImportFile* perf,
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangIndex* index,
|
2017-04-20 07:25:38 +00:00
|
|
|
bool dump_ast) {
|
2017-04-19 07:32:59 +00:00
|
|
|
if (!config->enableIndexing)
|
|
|
|
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(
|
2017-10-23 05:07:50 +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)
|
2017-06-14 03:08:31 +00:00
|
|
|
return {};
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-05-17 07:08:45 +00:00
|
|
|
perf->index_parse = timer.ElapsedMicrosecondsAndReset();
|
|
|
|
|
2017-02-27 07:28:33 +00:00
|
|
|
if (dump_ast)
|
2017-11-04 23:11:52 +00:00
|
|
|
Dump(clang_getTranslationUnitCursor(tu->cx_tu));
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-10-23 05:07:50 +00:00
|
|
|
return ParseWithTu(file_consumer_shared, perf, tu.get(), index, file, args,
|
2017-09-27 06:03:43 +00:00
|
|
|
unsaved_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> ParseWithTu(
|
|
|
|
FileConsumer::SharedState* file_consumer_shared,
|
|
|
|
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
|
|
|
|
2017-06-14 03:08:31 +00:00
|
|
|
FileConsumer file_consumer(file_consumer_shared, file);
|
2017-09-27 06:03:43 +00:00
|
|
|
IndexParam param(tu, &file_consumer);
|
|
|
|
for (const CXUnsavedFile& contents : file_contents) {
|
2017-12-19 07:05:12 +00:00
|
|
|
param.file_contents.emplace(
|
|
|
|
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
|
|
|
|
|
|
|
// NOTE: libclang re-enables crash recovery whenever a new index is created.
|
|
|
|
// To have clang crash toggle crash recovery right before calling
|
|
|
|
// clang_indexTranslationUnit.
|
|
|
|
// clang_toggleCrashRecovery(0);
|
|
|
|
|
|
|
|
// |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) {
|
|
|
|
LOG_S(WARNING) << "Indexing " << file
|
|
|
|
<< " failed with errno=" << index_result;
|
|
|
|
return {};
|
|
|
|
}
|
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
|
|
|
|
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;
|
2017-07-30 04:24:02 +00:00
|
|
|
|
|
|
|
// Update file contents and modification time.
|
2017-12-19 07:05:12 +00:00
|
|
|
entry->file_contents_ = param.file_contents[entry->path].contents;
|
2017-07-30 04:24:02 +00:00
|
|
|
entry->last_modification_time = param.file_modification_times[entry->path];
|
|
|
|
|
|
|
|
// Update dependencies for the file. Do not include the file in its own
|
|
|
|
// dependency set.
|
|
|
|
entry->dependencies = param.seen_files;
|
2017-12-11 02:59:32 +00:00
|
|
|
entry->dependencies.erase(
|
|
|
|
std::remove(entry->dependencies.begin(), entry->dependencies.end(),
|
|
|
|
entry->path),
|
|
|
|
entry->dependencies.end());
|
2017-07-30 04:24:02 +00:00
|
|
|
|
|
|
|
// Make sure we are using correct file contents.
|
2017-09-27 06:03:43 +00:00
|
|
|
for (const CXUnsavedFile& contents : file_contents) {
|
|
|
|
if (entry->path == contents.Filename)
|
|
|
|
entry->file_contents_ = std::string(contents.Contents, contents.Length);
|
2017-07-30 04:24:02 +00:00
|
|
|
}
|
2017-04-08 22:54:36 +00:00
|
|
|
}
|
2017-04-19 00:05:14 +00:00
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
return result;
|
2017-03-22 17:16:09 +00:00
|
|
|
}
|
2017-04-16 21:51:47 +00:00
|
|
|
|
|
|
|
void IndexInit() {
|
|
|
|
clang_enableStackTraces();
|
|
|
|
clang_toggleCrashRecovery(1);
|
2017-04-18 23:49:55 +00:00
|
|
|
}
|
2017-12-01 17:45:44 +00:00
|
|
|
|
|
|
|
void ClangSanityCheck() {
|
|
|
|
std::vector<const char*> args = {"clang", "tests/vars/class_member.cc"};
|
|
|
|
unsigned opts = 0;
|
|
|
|
CXIndex index = clang_createIndex(0, 1);
|
|
|
|
CXTranslationUnit tu;
|
|
|
|
clang_parseTranslationUnit2FullArgv(index, nullptr, args.data(), args.size(),
|
|
|
|
nullptr, 0, opts, &tu);
|
|
|
|
assert(tu);
|
|
|
|
|
|
|
|
IndexerCallbacks callback = {0};
|
|
|
|
callback.abortQuery = [](CXClientData client_data, void* reserved) {
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
callback.diagnostic = [](CXClientData client_data,
|
|
|
|
CXDiagnosticSet diagnostics, void* reserved) {};
|
|
|
|
callback.enteredMainFile = [](CXClientData client_data, CXFile mainFile,
|
|
|
|
void* reserved) -> CXIdxClientFile {
|
|
|
|
return nullptr;
|
|
|
|
};
|
2017-12-23 16:01:43 +00:00
|
|
|
callback.ppIncludedFile = [](
|
|
|
|
CXClientData client_data,
|
|
|
|
const CXIdxIncludedFileInfo* file) -> CXIdxClientFile { return nullptr; };
|
|
|
|
callback.importedASTFile = [](
|
|
|
|
CXClientData client_data,
|
|
|
|
const CXIdxImportedASTFileInfo*) -> CXIdxClientASTFile {
|
2017-12-01 17:45:44 +00:00
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
callback.startedTranslationUnit = [](CXClientData client_data,
|
|
|
|
void* reserved) -> CXIdxClientContainer {
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
callback.indexDeclaration = [](CXClientData client_data,
|
|
|
|
const CXIdxDeclInfo* decl) {};
|
|
|
|
callback.indexEntityReference = [](CXClientData client_data,
|
|
|
|
const CXIdxEntityRefInfo* ref) {};
|
|
|
|
|
|
|
|
const unsigned kIndexOpts = 0;
|
|
|
|
CXIndexAction index_action = clang_IndexAction_create(index);
|
|
|
|
int index_param = 0;
|
|
|
|
clang_toggleCrashRecovery(0);
|
|
|
|
clang_indexTranslationUnit(index_action, &index_param, &callback,
|
|
|
|
sizeof(IndexerCallbacks), kIndexOpts, tu);
|
|
|
|
clang_IndexAction_dispose(index_action);
|
|
|
|
|
|
|
|
clang_disposeTranslationUnit(tu);
|
|
|
|
clang_disposeIndex(index);
|
|
|
|
}
|
2017-12-23 15:51:34 +00:00
|
|
|
|
|
|
|
std::string GetClangVersion() {
|
|
|
|
return ToString(clang_getClangVersion());
|
2017-12-23 17:29:13 +00:00
|
|
|
}
|