2017-02-22 08:52:00 +00:00
|
|
|
#include "indexer.h"
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-05-20 06:35:14 +00:00
|
|
|
#include "clang_utils.h"
|
2017-04-22 07:39:55 +00:00
|
|
|
#include "libclangmm/Cursor.h"
|
|
|
|
#include "libclangmm/Index.h"
|
|
|
|
#include "libclangmm/TranslationUnit.h"
|
|
|
|
#include "libclangmm/Utility.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-04-22 07:39:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
|
|
|
|
2017-04-12 07:36:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-04-12 07:57:12 +00:00
|
|
|
const bool kIndexStdDeclarations = true;
|
|
|
|
|
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-10 06:13:13 +00:00
|
|
|
|
|
|
|
Range Resolve(const CXSourceRange& range) {
|
|
|
|
CXSourceLocation start = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation end = clang_getRangeEnd(range);
|
|
|
|
|
|
|
|
unsigned int start_line, start_column;
|
|
|
|
clang_getSpellingLocation(start, nullptr, &start_line, &start_column, nullptr);
|
|
|
|
unsigned int end_line, end_column;
|
|
|
|
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
|
|
|
|
|
|
|
|
return Range(
|
|
|
|
Position(start_line, start_column) /*start*/,
|
|
|
|
Position(end_line, end_column) /*end*/);
|
|
|
|
}
|
|
|
|
|
|
|
|
Range ResolveSpelling(const CXCursor& cx_cursor) {
|
|
|
|
CXSourceRange cx_range = clang_Cursor_getSpellingNameRange(cx_cursor, 0, 0);
|
|
|
|
return Resolve(cx_range);
|
|
|
|
}
|
|
|
|
|
|
|
|
Range ResolveExtent(const CXCursor& cx_cursor) {
|
|
|
|
CXSourceRange cx_range = clang_getCursorExtent(cx_cursor);
|
|
|
|
return Resolve(cx_range);
|
|
|
|
}
|
|
|
|
|
2017-05-20 21:45:46 +00:00
|
|
|
|
|
|
|
struct NamespaceHelper {
|
|
|
|
std::unordered_map<std::string, std::string> container_usr_to_qualified_name;
|
|
|
|
|
|
|
|
void RegisterQualifiedName(std::string usr,
|
|
|
|
const CXIdxContainerInfo* container,
|
|
|
|
std::string qualified_name) {
|
|
|
|
if (container) {
|
|
|
|
std::string container_usr = clang::Cursor(container->cursor).get_usr();
|
|
|
|
auto it = container_usr_to_qualified_name.find(container_usr);
|
|
|
|
if (it != container_usr_to_qualified_name.end()) {
|
|
|
|
container_usr_to_qualified_name[usr] =
|
|
|
|
it->second + qualified_name + "::";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container_usr_to_qualified_name[usr] = qualified_name + "::";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string QualifiedName(const CXIdxContainerInfo* container,
|
|
|
|
std::string unqualified_name) {
|
|
|
|
if (container) {
|
|
|
|
std::string container_usr = clang::Cursor(container->cursor).get_usr();
|
|
|
|
auto it = container_usr_to_qualified_name.find(container_usr);
|
|
|
|
if (it != container_usr_to_qualified_name.end())
|
|
|
|
return it->second + unqualified_name;
|
|
|
|
|
|
|
|
// Anonymous namespaces are not processed by indexDeclaration. If we
|
|
|
|
// encounter one insert it into map.
|
|
|
|
if (container->cursor.kind == CXCursor_Namespace) {
|
|
|
|
// assert(clang::Cursor(container->cursor).get_spelling() == "");
|
|
|
|
container_usr_to_qualified_name[container_usr] = "::";
|
|
|
|
return "::" + unqualified_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unqualified_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IndexParam {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
clang::TranslationUnit* tu = nullptr;
|
|
|
|
|
|
|
|
FileConsumer* file_consumer = nullptr;
|
|
|
|
NamespaceHelper ns;
|
|
|
|
|
|
|
|
IndexParam(clang::TranslationUnit* tu, FileConsumer* file_consumer) : tu(tu), file_consumer(file_consumer) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
IndexFile* ConsumeFile(IndexParam* param, CXFile file) {
|
|
|
|
bool is_first_ownership = false;
|
|
|
|
IndexFile* db = param->file_consumer->TryConsumeFile(file, &is_first_ownership);
|
|
|
|
|
|
|
|
// Mark dependency in primary file. If primary_file is null that means we're
|
|
|
|
// doing a re-index in which case the dependency has already been established
|
|
|
|
// in a previous index run.
|
|
|
|
if (is_first_ownership && param->primary_file)
|
|
|
|
param->primary_file->dependencies.push_back(db->path);
|
|
|
|
|
|
|
|
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-04-12 07:36:17 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
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-02-20 00:56:56 +00:00
|
|
|
return ToTypeId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFuncId IndexFile::ToFuncId(const CXCursor& cursor) {
|
2017-02-20 00:56:56 +00:00
|
|
|
return ToFuncId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexVarId IndexFile::ToVarId(const CXCursor& cursor) {
|
2017-02-20 00:56:56 +00:00
|
|
|
return ToVarId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
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-03-22 17:16:09 +00:00
|
|
|
: def(usr), id(id) {
|
2017-02-24 08:39:25 +00:00
|
|
|
assert(usr.size() > 0);
|
2017-03-17 07:58:41 +00:00
|
|
|
// std::cerr << "Creating type with usr " << usr << std::endl;
|
2017-02-24 08:39:25 +00:00
|
|
|
}
|
|
|
|
|
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-04-19 05:28:33 +00:00
|
|
|
void UniqueAdd(std::vector<IndexFuncRef>& refs, IndexFuncRef ref) {
|
2017-04-13 07:47:47 +00:00
|
|
|
if (std::find(refs.begin(), refs.end(), ref) != refs.end())
|
|
|
|
refs.push_back(ref);
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
void UniqueAdd(std::vector<Range>& uses, Range loc) {
|
|
|
|
if (std::find(uses.begin(), uses.end(), loc) == uses.end())
|
2017-02-24 08:39:25 +00:00
|
|
|
uses.push_back(loc);
|
|
|
|
}
|
2017-02-20 06:40:55 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
IdCache::IdCache(const std::string& primary_file)
|
|
|
|
: 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-03-17 07:58:41 +00:00
|
|
|
int abortQuery(CXClientData client_data, void* reserved) {
|
2017-02-20 00:56:56 +00:00
|
|
|
// 0 -> continue
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
void diagnostic(CXClientData client_data,
|
|
|
|
CXDiagnosticSet diagnostics,
|
|
|
|
void* reserved) {
|
2017-03-11 02:24:51 +00:00
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
|
|
|
|
2017-03-11 08:07:32 +00:00
|
|
|
// Print any diagnostics to std::cerr
|
2017-03-11 02:24:51 +00:00
|
|
|
for (unsigned i = 0; i < clang_getNumDiagnosticsInSet(diagnostics); ++i) {
|
|
|
|
CXDiagnostic diagnostic = clang_getDiagnosticInSet(diagnostics, i);
|
2017-03-14 04:31:53 +00:00
|
|
|
|
2017-05-10 06:13:13 +00:00
|
|
|
// Skip diagnostics in system headers.
|
|
|
|
CXSourceLocation diag_loc = clang_getDiagnosticLocation(diagnostic);
|
|
|
|
if (clang_Location_isInSystemHeader(diag_loc))
|
|
|
|
continue;
|
2017-03-11 02:24:51 +00:00
|
|
|
|
2017-05-10 06:13:13 +00:00
|
|
|
// Get db so we can attribute diagnostic to the right indexed file.
|
2017-04-05 08:06:18 +00:00
|
|
|
CXFile file;
|
|
|
|
unsigned int line, column;
|
2017-05-10 06:13:13 +00:00
|
|
|
clang_getSpellingLocation(diag_loc, &file, &line, &column, nullptr);
|
2017-05-20 21:45:46 +00:00
|
|
|
IndexFile* db = ConsumeFile(param, file);
|
2017-05-10 06:13:13 +00:00
|
|
|
if (!db)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Build diagnostic.
|
2017-05-20 06:35:14 +00:00
|
|
|
optional<lsDiagnostic> ls_diagnostic = BuildDiagnostic(diagnostic);
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
CXIdxClientFile enteredMainFile(CXClientData client_data,
|
|
|
|
CXFile mainFile,
|
|
|
|
void* reserved) {
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
CXIdxClientFile ppIncludedFile(CXClientData client_data,
|
2017-04-11 05:26:27 +00:00
|
|
|
const CXIdxIncludedFileInfo* file) {
|
|
|
|
// Clang include logic is broken. This function is never
|
|
|
|
// called and clang_findIncludesInFile doesn't work.
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
CXIdxClientASTFile importedASTFile(CXClientData client_data,
|
|
|
|
const CXIdxImportedASTFileInfo*) {
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
CXIdxClientContainer startedTranslationUnit(CXClientData client_data,
|
|
|
|
void* reserved) {
|
2017-02-20 00:56:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
clang::VisiterResult DumpVisitor(clang::Cursor cursor,
|
|
|
|
clang::Cursor 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-03-17 07:58:41 +00:00
|
|
|
std::cerr << clang::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;
|
|
|
|
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dump(clang::Cursor cursor) {
|
|
|
|
int level = 0;
|
|
|
|
cursor.VisitChildren(&DumpVisitor, &level);
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:00:58 +00:00
|
|
|
struct FindChildOfKindParam {
|
|
|
|
CXCursorKind target_kind;
|
2017-02-22 01:06:43 +00:00
|
|
|
optional<clang::Cursor> result;
|
2017-02-20 02:00:58 +00:00
|
|
|
|
|
|
|
FindChildOfKindParam(CXCursorKind target_kind) : target_kind(target_kind) {}
|
|
|
|
};
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
clang::VisiterResult FindChildOfKindVisitor(clang::Cursor cursor,
|
|
|
|
clang::Cursor parent,
|
|
|
|
FindChildOfKindParam* param) {
|
2017-02-20 02:00:58 +00:00
|
|
|
if (cursor.get_kind() == param->target_kind) {
|
|
|
|
param->result = cursor;
|
|
|
|
return clang::VisiterResult::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Recurse;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
optional<clang::Cursor> FindChildOfKind(clang::Cursor cursor,
|
|
|
|
CXCursorKind kind) {
|
2017-02-20 02:00:58 +00:00
|
|
|
FindChildOfKindParam param(kind);
|
|
|
|
cursor.VisitChildren(&FindChildOfKindVisitor, ¶m);
|
|
|
|
return param.result;
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
clang::VisiterResult FindTypeVisitor(clang::Cursor cursor,
|
|
|
|
clang::Cursor parent,
|
|
|
|
optional<clang::Cursor>* 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;
|
|
|
|
return clang::VisiterResult::Break;
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Recurse;
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:06:43 +00:00
|
|
|
optional<clang::Cursor> FindType(clang::Cursor cursor) {
|
|
|
|
optional<clang::Cursor> 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;
|
|
|
|
|
|
|
|
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-02-22 01:06:43 +00:00
|
|
|
optional<clang::Cursor> 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-03-17 07:58:41 +00:00
|
|
|
void VisitDeclForTypeUsageVisitorHandler(clang::Cursor cursor,
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
clang::VisiterResult VisitDeclForTypeUsageVisitor(
|
|
|
|
clang::Cursor cursor,
|
|
|
|
clang::Cursor parent,
|
|
|
|
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;
|
|
|
|
break;
|
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:
|
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-20 21:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
clang::Cursor cursor) {
|
2017-03-17 07:58:41 +00:00
|
|
|
clang::Cursor declaration =
|
|
|
|
cursor.get_type().strip_qualifiers().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-03-17 07:58:41 +00:00
|
|
|
clang::Cursor decl_cursor,
|
|
|
|
const CXIdxContainerInfo* semantic_container,
|
|
|
|
const CXIdxContainerInfo* lexical_container) {
|
2017-04-19 05:28:33 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// std::cerr << std::endl << "AddDeclUsages " << decl_cursor.get_spelling() <<
|
|
|
|
// std::endl;
|
|
|
|
// Dump(decl_cursor);
|
2017-03-05 23:44:20 +00:00
|
|
|
|
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()) {
|
|
|
|
// TODO: I don't think this resolution ever works.
|
|
|
|
clang::Cursor def = decl_cursor.get_definition();
|
|
|
|
if (def.get_kind() != CXCursor_FirstInvalid) {
|
2017-03-17 07:58:41 +00:00
|
|
|
std::cerr << "Successful resolution of decl usage to definition"
|
|
|
|
<< std::endl;
|
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.
|
|
|
|
clang::VisiterResult AddDeclInitializerUsagesVisitor(clang::Cursor cursor,
|
|
|
|
clang::Cursor parent,
|
2017-05-12 06:08:15 +00:00
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
case CXCursor_DeclRefExpr:
|
|
|
|
CXCursorKind referenced_kind = cursor.get_referenced().get_kind();
|
|
|
|
if (cursor.get_referenced().get_kind() != CXCursor_VarDecl)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// TODO: when we resolve the template type to the definition, we get a
|
|
|
|
// different USR.
|
|
|
|
|
|
|
|
// clang::Cursor ref =
|
|
|
|
// 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-03-17 07:58:41 +00:00
|
|
|
// std::cerr << "Adding usage to id=" << ref_id.id << " usr=" << ref_usr
|
|
|
|
// << " at " << loc.ToString() << std::endl;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Recurse;
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
void AddDeclInitializerUsages(IndexFile* db, clang::Cursor 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(
|
|
|
|
clang_indexLoc_getCXSourceLocation(loc),
|
|
|
|
//clang_getRangeStart(clang_getCursorExtent(cursor)));
|
|
|
|
clang_getRangeStart(clang_Cursor_getSpellingNameRange(cursor, 0, 0)));
|
|
|
|
}
|
|
|
|
|
2017-04-11 05:26:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
|
2017-04-12 07:57:12 +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-04-05 08:06:18 +00:00
|
|
|
assert(AreEqualLocations(decl->loc, decl->cursor));
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
CXFile file;
|
|
|
|
clang_getSpellingLocation(clang_indexLoc_getCXSourceLocation(decl->loc), &file, nullptr, nullptr, nullptr);
|
2017-02-20 00:56:56 +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-11 05:26:27 +00:00
|
|
|
NamespaceHelper* ns = ¶m->ns;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-29 06:33:38 +00:00
|
|
|
|
|
|
|
//std::cerr << "DECL kind=" << decl->entityInfo->kind << " at " << db->id_cache.Resolve(decl->cursor, false).ToPrettyString(&db->id_cache) << std::endl;
|
2017-03-14 04:31:53 +00:00
|
|
|
|
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-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-03-17 07:58:41 +00:00
|
|
|
clang::Cursor 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-12 06:08:15 +00:00
|
|
|
IndexVar* var_def = 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) {
|
|
|
|
var_def->def.short_name = decl->entityInfo->name;
|
2017-04-14 05:18:02 +00:00
|
|
|
|
2017-04-15 05:02:03 +00:00
|
|
|
std::string type_name = clang::ToString(clang_getTypeSpelling(clang_getCursorType(decl->cursor)));
|
|
|
|
var_def->def.detailed_name = type_name + " " + ns->QualifiedName(decl->semanticContainer, var_def->def.short_name);
|
2017-03-06 06:23:41 +00:00
|
|
|
//}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
if (decl->isDefinition) {
|
2017-05-10 06:13:13 +00:00
|
|
|
var_def->def.definition_spelling = ResolveSpelling(decl->cursor);
|
|
|
|
var_def->def.definition_extent = ResolveExtent(decl->cursor);;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-10 06:13:13 +00:00
|
|
|
var_def->def.declaration = ResolveSpelling(decl->cursor);
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
2017-04-18 23:49:55 +00:00
|
|
|
UniqueAdd(var_def->uses, decl_loc_spelling);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// std::cerr << std::endl << "Visiting declaration" << std::endl;
|
|
|
|
// Dump(decl_cursor);
|
|
|
|
|
|
|
|
AddDeclInitializerUsages(db, decl_cursor);
|
|
|
|
var_def = db->Resolve(var_id);
|
|
|
|
|
|
|
|
// 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-03-17 07:58:41 +00:00
|
|
|
AddDeclTypeUsages(
|
|
|
|
db, decl_cursor,
|
|
|
|
decl->semanticContainer, decl->lexicalContainer);
|
|
|
|
|
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-04-07 06:57:26 +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.
|
|
|
|
bool is_enum_member = decl->semanticContainer && decl->semanticContainer->cursor.kind == CXCursor_EnumDecl;
|
|
|
|
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
|
|
|
|
|
|
|
var_def->def.variable_type = var_type.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-03 01:34:15 +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-03-17 07:58:41 +00:00
|
|
|
var_def->def.declaring_type = declaring_type_id;
|
|
|
|
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-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-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
|
|
|
clang::Cursor decl_cursor = decl->cursor;
|
|
|
|
clang::Cursor resolved =
|
|
|
|
decl_cursor.template_specialization_to_template_definition();
|
|
|
|
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId func_id = db->ToFuncId(resolved.cx_cursor);
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFunc* func_def = 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-04-19 05:28:33 +00:00
|
|
|
AddDeclTypeUsages(db, decl_cursor,
|
2017-03-17 07:58:41 +00:00
|
|
|
decl->semanticContainer, decl->lexicalContainer);
|
|
|
|
|
|
|
|
// TODO: support multiple definitions per function; right now we are
|
|
|
|
// hacking the 'declarations' field by
|
|
|
|
// adding a definition when we really don't have one.
|
2017-04-05 08:06:18 +00:00
|
|
|
if (decl->isDefinition && !func_def->def.definition_extent.has_value()) {
|
2017-05-10 06:13:13 +00:00
|
|
|
func_def->def.definition_spelling = ResolveSpelling(decl->cursor);
|
|
|
|
func_def->def.definition_extent = ResolveExtent(decl->cursor);
|
2017-04-13 07:47:47 +00:00
|
|
|
|
|
|
|
RemoveItem(func_def->declarations, *func_def->def.definition_spelling);
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-05-10 06:13:13 +00:00
|
|
|
Range decl_spelling = ResolveSpelling(decl->cursor);
|
2017-04-13 07:47:47 +00:00
|
|
|
// Only add the declaration if it's not already a definition.
|
|
|
|
if (!func_def->def.definition_spelling || *func_def->def.definition_spelling != decl_spelling)
|
|
|
|
UniqueAdd(func_def->declarations, decl_spelling);
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
2017-03-17 07:58:41 +00:00
|
|
|
|
|
|
|
// If decl_cursor != resolved, then decl_cursor is a template
|
|
|
|
// specialization. We
|
|
|
|
// don't want to override a lot of the function definition information in
|
|
|
|
// that
|
|
|
|
// scenario.
|
|
|
|
if (decl_cursor == resolved) {
|
|
|
|
// TODO: Eventually run with this if. Right now I want to iron out bugs
|
|
|
|
// this may shadow.
|
|
|
|
// if (!decl->isRedeclaration) {
|
|
|
|
func_def->def.short_name = decl->entityInfo->name;
|
2017-04-14 08:21:03 +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 '('.
|
|
|
|
std::string qualified_name = ns->QualifiedName(decl->semanticContainer, func_def->def.short_name);
|
|
|
|
std::string type_desc = decl_cursor.get_type_description();
|
|
|
|
size_t offset = type_desc.find('(');
|
|
|
|
type_desc.insert(offset, qualified_name);
|
|
|
|
func_def->def.detailed_name = type_desc;
|
|
|
|
//}
|
2017-04-14 08:21:03 +00:00
|
|
|
|
2017-04-14 05:18:02 +00:00
|
|
|
// TODO: return type
|
|
|
|
//decl_cursor.get_type_description()
|
2017-04-18 23:49:55 +00:00
|
|
|
//func_def->def.return_type =
|
2017-04-14 05:18:02 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
bool is_pure_virtual = clang_CXXMethod_isPureVirtual(decl->cursor);
|
|
|
|
bool is_ctor_or_dtor =
|
|
|
|
decl->entityInfo->kind == CXIdxEntity_CXXConstructor ||
|
|
|
|
decl->entityInfo->kind == CXIdxEntity_CXXDestructor;
|
|
|
|
// bool process_declaring_type = is_pure_virtual || is_ctor_or_dtor;
|
|
|
|
|
|
|
|
// Add function usage information. We only want to do it once per
|
|
|
|
// definition/declaration. Do it on definition since there should only
|
|
|
|
// ever
|
|
|
|
// be one of those in the entire program.
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
func_def->def.declaring_type = declaring_type_id;
|
|
|
|
|
|
|
|
// Mark a type reference at the ctor/dtor location.
|
|
|
|
if (is_ctor_or_dtor) {
|
2017-04-18 23:49:55 +00:00
|
|
|
Range type_usage_loc = decl_loc_spelling;
|
2017-04-13 07:47:47 +00:00
|
|
|
UniqueAdd(declaring_type_def->uses, type_usage_loc);
|
2017-03-06 06:23:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Register function in declaring type if it hasn't been registered
|
|
|
|
// yet.
|
|
|
|
if (!Contains(declaring_type_def->def.funcs, func_id))
|
|
|
|
declaring_type_def->def.funcs.push_back(func_id);
|
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
if (decl->isDefinition || is_pure_virtual) {
|
|
|
|
// Mark type usage for parameters as interesting. We handle this here
|
|
|
|
// instead of inside var declaration because clang will not emit a var
|
|
|
|
// declaration for an unnamed parameter, but we still want to mark the
|
|
|
|
// usage as interesting.
|
|
|
|
// TODO: Do a similar thing for function decl parameter usages. Mark
|
|
|
|
// prototype params as interesting type usages but also relate
|
|
|
|
// mark
|
|
|
|
// them as as usages on the primary variable - requires USR to
|
|
|
|
// be
|
|
|
|
// the same. We can work around it by declaring which variables
|
|
|
|
// a
|
|
|
|
// parameter has declared and update the USR in the definition.
|
|
|
|
clang::Cursor cursor = decl->cursor;
|
|
|
|
for (clang::Cursor arg : cursor.get_arguments()) {
|
|
|
|
switch (arg.get_kind()) {
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
// We don't need to know the arg type, but we do want to mark it
|
|
|
|
// as
|
|
|
|
// an interesting usage. Note that we use semanticContainer
|
|
|
|
// twice
|
|
|
|
// because a parameter is not really part of the lexical
|
|
|
|
// container.
|
2017-04-19 05:28:33 +00:00
|
|
|
AddDeclTypeUsages(db, arg,
|
2017-03-17 07:58:41 +00:00
|
|
|
decl->semanticContainer,
|
|
|
|
decl->semanticContainer);
|
|
|
|
|
|
|
|
// TypeResolution arg_type = ResolveToType(db, arg.get_type());
|
|
|
|
// if (arg_type.resolved_type)
|
|
|
|
// AddInterestingUsageToType(db, arg_type,
|
|
|
|
// FindLocationOfTypeSpecifier(arg));
|
|
|
|
break;
|
|
|
|
}
|
2017-03-06 06:23:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// Process inheritance.
|
|
|
|
// void clang_getOverriddenCursors(CXCursor cursor, CXCursor
|
|
|
|
// **overridden, unsigned *num_overridden);
|
|
|
|
// void clang_disposeOverriddenCursors(CXCursor *overridden);
|
|
|
|
if (clang_CXXMethod_isVirtual(decl->cursor)) {
|
|
|
|
CXCursor* overridden;
|
|
|
|
unsigned int num_overridden;
|
|
|
|
clang_getOverriddenCursors(decl->cursor, &overridden,
|
|
|
|
&num_overridden);
|
|
|
|
|
|
|
|
// TODO: How to handle multiple parent overrides??
|
|
|
|
for (unsigned int i = 0; i < num_overridden; ++i) {
|
|
|
|
clang::Cursor parent = overridden[i];
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId parent_id = db->ToFuncId(parent.get_usr());
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFunc* parent_def = db->Resolve(parent_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
func_def = db->Resolve(func_id); // ToFuncId invalidated func_def
|
|
|
|
|
|
|
|
func_def->def.base = parent_id;
|
|
|
|
parent_def->derived.push_back(func_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_disposeOverriddenCursors(overridden);
|
|
|
|
}
|
2017-03-06 06:23:41 +00:00
|
|
|
}
|
2017-02-20 07:06:38 +00:00
|
|
|
}
|
2017-02-20 06:31:25 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
/*
|
|
|
|
optional<FuncId> base;
|
|
|
|
std::vector<FuncId> derived;
|
|
|
|
std::vector<VarId> locals;
|
|
|
|
std::vector<FuncRef> callers;
|
|
|
|
std::vector<FuncRef> callees;
|
|
|
|
std::vector<Location> uses;
|
|
|
|
*/
|
|
|
|
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-04-07 06:57:26 +00:00
|
|
|
optional<IndexTypeId> alias_of =
|
2017-04-19 05:28:33 +00:00
|
|
|
AddDeclTypeUsages(db, decl->cursor,
|
2017-03-17 07:58:41 +00:00
|
|
|
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-12 06:08:15 +00:00
|
|
|
IndexType* type_def = db->Resolve(type_id);
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
if (alias_of)
|
|
|
|
type_def->def.alias_of = alias_of.value();
|
2017-02-21 06:11:47 +00:00
|
|
|
|
2017-02-25 06:08:14 +00:00
|
|
|
type_def->def.short_name = decl->entityInfo->name;
|
2017-04-15 04:58:07 +00:00
|
|
|
type_def->def.detailed_name =
|
2017-03-17 07:58:41 +00:00
|
|
|
ns->QualifiedName(decl->semanticContainer, type_def->def.short_name);
|
|
|
|
|
2017-05-10 06:13:13 +00:00
|
|
|
type_def->def.definition_spelling = ResolveSpelling(decl->cursor);
|
|
|
|
type_def->def.definition_extent = ResolveExtent(decl->cursor);
|
2017-04-18 23:49:55 +00:00
|
|
|
UniqueAdd(type_def->uses, decl_loc_spelling);
|
2017-03-17 07:58:41 +00:00
|
|
|
break;
|
2017-02-21 06:11:47 +00:00
|
|
|
}
|
|
|
|
|
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-12 06:08:15 +00:00
|
|
|
IndexType* type_def = 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);
|
|
|
|
type_def->def.short_name = decl->entityInfo->name;
|
|
|
|
} else {
|
|
|
|
type_def->def.short_name = "<anonymous>";
|
|
|
|
}
|
|
|
|
|
2017-04-15 04:58:07 +00:00
|
|
|
type_def->def.detailed_name =
|
2017-03-17 07:58:41 +00:00
|
|
|
ns->QualifiedName(decl->semanticContainer, type_def->def.short_name);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
assert(decl->isDefinition);
|
2017-05-10 06:13:13 +00:00
|
|
|
type_def->def.definition_spelling = ResolveSpelling(decl->cursor);
|
|
|
|
type_def->def.definition_extent = ResolveExtent(decl->cursor);
|
2017-04-18 23:49:55 +00:00
|
|
|
UniqueAdd(type_def->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-04-19 05:28:33 +00:00
|
|
|
AddDeclTypeUsages(db, base_class->cursor,
|
2017-03-17 07:58:41 +00:00
|
|
|
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-12 06:08:15 +00:00
|
|
|
IndexType* type_def = db->Resolve(type_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
if (parent_type_id) {
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* parent_type_def =
|
2017-03-17 07:58:41 +00:00
|
|
|
db->Resolve(parent_type_id.value());
|
|
|
|
parent_type_def->derived.push_back(type_id);
|
|
|
|
type_def->def.parents.push_back(parent_type_id.value());
|
|
|
|
}
|
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:
|
|
|
|
std::cerr
|
|
|
|
<< "!! Unhandled indexDeclaration: "
|
|
|
|
<< clang::Cursor(decl->cursor).ToString() << " at "
|
2017-05-10 06:13:13 +00:00
|
|
|
<< ResolveSpelling(decl->cursor).start.ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " entityInfo->kind = " << decl->entityInfo->kind
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " entityInfo->USR = " << decl->entityInfo->USR
|
|
|
|
<< std::endl;
|
|
|
|
if (decl->declAsContainer)
|
|
|
|
std::cerr << " declAsContainer = "
|
|
|
|
<< clang::Cursor(decl->declAsContainer->cursor).ToString()
|
|
|
|
<< std::endl;
|
|
|
|
if (decl->semanticContainer)
|
|
|
|
std::cerr << " semanticContainer = "
|
|
|
|
<< clang::Cursor(decl->semanticContainer->cursor).ToString()
|
|
|
|
<< std::endl;
|
|
|
|
if (decl->lexicalContainer)
|
|
|
|
std::cerr << " lexicalContainer = "
|
|
|
|
<< clang::Cursor(decl->lexicalContainer->cursor).get_usr()
|
|
|
|
<< 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-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-11 05:26:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
void indexEntityReference(CXClientData client_data,
|
|
|
|
const CXIdxEntityRefInfo* ref) {
|
2017-04-08 22:54:36 +00:00
|
|
|
// Don't index references from or to system headers.
|
|
|
|
if (clang_Location_isInSystemHeader(clang_indexLoc_getCXSourceLocation(ref->loc)) ||
|
|
|
|
clang_Location_isInSystemHeader(clang_getCursorLocation(ref->referencedEntity->cursor)))
|
|
|
|
return;
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
//assert(AreEqualLocations(ref->loc, ref->cursor));
|
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
// if (clang_Location_isInSystemHeader(clang_getCursorLocation(ref->cursor)) ||
|
|
|
|
// clang_Location_isInSystemHeader(
|
|
|
|
// clang_getCursorLocation(ref->referencedEntity->cursor)))
|
|
|
|
// return;
|
2017-04-08 22:54:36 +00:00
|
|
|
|
|
|
|
// TODO: Use clang_getFileUniqueID
|
|
|
|
CXFile file;
|
|
|
|
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
|
|
|
|
|
|
|
// ref->cursor mainFile=0
|
|
|
|
// ref->loc mainFile=1
|
|
|
|
// ref->referencedEntity mainFile=1
|
|
|
|
//
|
2017-05-12 06:08:15 +00:00
|
|
|
// Regardless, we need to do more advanced location processing to handle multiple output IndexFile instances.
|
2017-04-08 18:36:32 +00:00
|
|
|
//bool mainFile = clang_Location_isFromMainFile(clang_indexLoc_getCXSourceLocation(ref->loc));
|
|
|
|
//Range loc_spelling = param->db->id_cache.ForceResolveSpelling(ref->cursor, false /*interesting*/);
|
|
|
|
//std::cerr << "mainFile: " << mainFile << ", loc: " << loc_spelling.ToString() << std::endl;
|
2017-04-03 01:34:15 +00:00
|
|
|
|
|
|
|
// Don't index references that are not from the main file.
|
2017-04-08 22:54:36 +00:00
|
|
|
//if (!clang_Location_isFromMainFile(clang_getCursorLocation(ref->cursor)))
|
|
|
|
// return;
|
|
|
|
|
2017-03-06 06:47:37 +00:00
|
|
|
|
2017-04-08 18:36:32 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
clang::Cursor cursor(ref->cursor);
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
// std::cerr << "REF kind=" << ref->referencedEntity->kind << " at " <<
|
|
|
|
// db->id_cache.Resolve(cursor, false).ToPrettyString(&db->id_cache) <<
|
|
|
|
// std::endl;
|
2017-03-14 04:31:53 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
switch (ref->referencedEntity->kind) {
|
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
|
|
|
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
clang::Cursor referenced = ref->referencedEntity->cursor;
|
|
|
|
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-12 06:08:15 +00:00
|
|
|
IndexVar* var_def = db->Resolve(var_id);
|
2017-04-18 23:49:55 +00:00
|
|
|
UniqueAdd(var_def->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:
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
// Note: be careful, calling db->ToFuncId invalidates the FuncDef* ptrs.
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId called_id = db->ToFuncId(ref->referencedEntity->USR);
|
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-12 06:08:15 +00:00
|
|
|
IndexFunc* caller_def = db->Resolve(caller_id);
|
|
|
|
IndexFunc* called_def = db->Resolve(called_id);
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-04-18 23:49:55 +00:00
|
|
|
AddFuncRef(&caller_def->def.callees, IndexFuncRef(called_id, loc_spelling));
|
|
|
|
AddFuncRef(&called_def->callers, IndexFuncRef(caller_id, loc_spelling));
|
2017-03-17 07:58:41 +00:00
|
|
|
} else {
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexFunc* called_def = db->Resolve(called_id);
|
2017-04-18 23:49:55 +00:00
|
|
|
AddFuncRef(&called_def->callers, IndexFuncRef(loc_spelling));
|
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-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-05-10 06:13:13 +00:00
|
|
|
Range loc_spelling = ResolveSpelling(ref->cursor);
|
2017-04-03 01:34:15 +00:00
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
clang::Cursor referenced = ref->referencedEntity->cursor;
|
|
|
|
referenced = referenced.template_specialization_to_template_definition();
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId referenced_id = db->ToTypeId(referenced.get_usr());
|
2017-03-17 07:58:41 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* referenced_def = 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
|
|
|
|
// does
|
|
|
|
// not appear to be a good way to disambiguate these references, as using
|
|
|
|
// parent type alone breaks other indexing tasks.
|
|
|
|
//
|
|
|
|
// 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-04-18 23:49:55 +00:00
|
|
|
UniqueAdd(referenced_def->uses, loc_spelling);
|
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:
|
|
|
|
std::cerr
|
|
|
|
<< "!! Unhandled indexEntityReference: " << cursor.ToString()
|
|
|
|
<< " at "
|
2017-05-10 06:13:13 +00:00
|
|
|
<< ResolveSpelling(ref->cursor).start.ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " ref->referencedEntity->kind = "
|
|
|
|
<< ref->referencedEntity->kind << std::endl;
|
|
|
|
if (ref->parentEntity)
|
|
|
|
std::cerr << " ref->parentEntity->kind = "
|
|
|
|
<< ref->parentEntity->kind << std::endl;
|
|
|
|
std::cerr
|
|
|
|
<< " ref->loc = "
|
2017-05-10 06:13:13 +00:00
|
|
|
<< ResolveSpelling(ref->cursor).start.ToString()
|
2017-03-17 07:58:41 +00:00
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " ref->kind = " << ref->kind << std::endl;
|
|
|
|
if (ref->parentEntity)
|
|
|
|
std::cerr << " parentEntity = "
|
|
|
|
<< clang::Cursor(ref->parentEntity->cursor).ToString()
|
|
|
|
<< std::endl;
|
|
|
|
if (ref->referencedEntity)
|
|
|
|
std::cerr << " referencedEntity = "
|
|
|
|
<< clang::Cursor(ref->referencedEntity->cursor).ToString()
|
|
|
|
<< std::endl;
|
|
|
|
if (ref->container)
|
|
|
|
std::cerr << " container = "
|
|
|
|
<< clang::Cursor(ref->container->cursor).ToString()
|
|
|
|
<< std::endl;
|
|
|
|
break;
|
2017-02-20 00:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-04-11 05:26:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> Parse(
|
2017-04-20 07:25:38 +00:00
|
|
|
IndexerConfig* config, FileConsumer::SharedState* file_consumer_shared,
|
2017-04-24 01:01:51 +00:00
|
|
|
std::string file,
|
2017-04-20 07:53:33 +00:00
|
|
|
std::vector<std::string> args,
|
2017-05-16 07:38:15 +00:00
|
|
|
const std::string& file_contents_path,
|
|
|
|
const optional<std::string>& file_contents,
|
2017-05-17 07:08:45 +00:00
|
|
|
PerformanceImportFile* perf,
|
2017-04-20 07:25:38 +00:00
|
|
|
bool dump_ast) {
|
2017-04-11 05:26:27 +00:00
|
|
|
|
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-03-17 07:58:41 +00:00
|
|
|
clang::Index index(0 /*excludeDeclarationsFromPCH*/,
|
|
|
|
0 /*displayDiagnostics*/);
|
2017-04-14 22:58:07 +00:00
|
|
|
std::vector<CXUnsavedFile> unsaved_files;
|
2017-05-16 07:38:15 +00:00
|
|
|
if (file_contents) {
|
|
|
|
CXUnsavedFile unsaved;
|
|
|
|
unsaved.Filename = file_contents_path.c_str();
|
|
|
|
unsaved.Contents = file_contents->c_str();
|
2017-05-20 21:45:46 +00:00
|
|
|
unsaved.Length = (unsigned long)file_contents->size();
|
2017-05-16 07:38:15 +00:00
|
|
|
unsaved_files.push_back(unsaved);
|
|
|
|
}
|
2017-05-20 21:45:46 +00:00
|
|
|
clang::TranslationUnit tu(index, file, args, unsaved_files, CXTranslationUnit_KeepGoing | CXTranslationUnit_DetailedPreprocessingRecord);
|
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-02-21 06:11:47 +00:00
|
|
|
Dump(tu.document_cursor());
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
IndexerCallbacks callbacks[] = {
|
2017-03-17 07:58:41 +00:00
|
|
|
{&abortQuery, &diagnostic, &enteredMainFile, &ppIncludedFile,
|
|
|
|
&importedASTFile, &startedTranslationUnit, &indexDeclaration,
|
|
|
|
&indexEntityReference}
|
2017-02-20 00:56:56 +00:00
|
|
|
};
|
|
|
|
|
2017-04-13 06:01:42 +00:00
|
|
|
FileConsumer file_consumer(file_consumer_shared);
|
2017-05-20 21:45:46 +00:00
|
|
|
IndexParam param(&tu, &file_consumer);
|
2017-03-06 06:47:37 +00:00
|
|
|
|
2017-04-24 01:01:51 +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-17 07:08:45 +00:00
|
|
|
//std::cerr << "!! [START] Indexing " << file << std::endl;
|
2017-04-11 05:26:27 +00:00
|
|
|
CXIndexAction index_action = clang_IndexAction_create(index.cx_index);
|
2017-02-20 00:56:56 +00:00
|
|
|
clang_indexTranslationUnit(index_action, ¶m, callbacks, sizeof(callbacks),
|
2017-04-03 01:34:15 +00:00
|
|
|
CXIndexOpt_IndexFunctionLocalSymbols | CXIndexOpt_SkipParsedBodiesInSession | CXIndexOpt_IndexImplicitTemplateInstantiations,
|
2017-03-17 07:58:41 +00:00
|
|
|
tu.cx_tu);
|
2017-02-20 00:56:56 +00:00
|
|
|
clang_IndexAction_dispose(index_action);
|
2017-05-17 07:08:45 +00:00
|
|
|
//std::cerr << "!! [END] Indexing " << file << std::endl;
|
|
|
|
|
|
|
|
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();
|
|
|
|
for (auto& entry : result) {
|
2017-04-20 04:57:44 +00:00
|
|
|
entry->last_modification_time = GetLastModificationTime(entry->path);
|
2017-04-24 01:01:51 +00:00
|
|
|
entry->import_file = file;
|
2017-04-21 04:06:15 +00:00
|
|
|
entry->args = args;
|
2017-04-08 22:54:36 +00:00
|
|
|
}
|
2017-04-19 00:05:14 +00:00
|
|
|
|
2017-04-19 05:38:39 +00:00
|
|
|
// TODO: Fix interesting checks.
|
2017-04-19 00:05:14 +00:00
|
|
|
for (auto& entry : result) {
|
|
|
|
for (auto& type : entry->types) {
|
|
|
|
if (!type.HasInterestingState()) {
|
2017-04-24 01:01:51 +00:00
|
|
|
std::cerr << "!!!! NO INTERESTING STATE FOR " << entry->path << " of !!! " << file << std::endl;
|
2017-04-19 00:05:14 +00:00
|
|
|
std::cerr << "!!!! USR " << type.def.usr << std::endl;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& func : entry->funcs) {
|
|
|
|
if (!func.HasInterestingState()) {
|
2017-04-24 01:01:51 +00:00
|
|
|
std::cerr << "!!!! NO INTERESTING STATE FOR " << entry->path << " of !!! " << file << std::endl;
|
2017-04-19 00:05:14 +00:00
|
|
|
std::cerr << "!!!! USR " << func.def.usr << std::endl;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& var : entry->vars) {
|
|
|
|
if (!var.HasInterestingState()) {
|
2017-04-24 01:01:51 +00:00
|
|
|
std::cerr << "!!!! NO INTERESTING STATE FOR " << entry->path << " of !!! " << file << std::endl;
|
2017-04-19 00:05:14 +00:00
|
|
|
std::cerr << "!!!! USR " << var.def.usr << std::endl;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 05:38:39 +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
|
|
|
}
|