ccls/indexer.cpp

860 lines
29 KiB
C++
Raw Normal View History

2017-02-22 08:52:00 +00:00
#include "indexer.h"
2017-02-17 09:57:44 +00:00
#include "serializer.h"
2017-02-27 07:23:43 +00:00
IndexedFile::IndexedFile(const std::string& path, IdCache* id_cache)
: path(path), id_cache(id_cache) {
2017-02-25 23:59:09 +00:00
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-26 19:45:59 +00:00
for (const auto& entry : id_cache->usr_to_type_id)
2017-02-25 23:59:09 +00:00
types.push_back(IndexedTypeDef(entry.second, entry.first));
2017-02-26 19:45:59 +00:00
for (const auto& entry : id_cache->usr_to_func_id)
2017-02-25 23:59:09 +00:00
funcs.push_back(IndexedFuncDef(entry.second, entry.first));
2017-02-26 19:45:59 +00:00
for (const auto& entry : id_cache->usr_to_var_id)
2017-02-25 23:59:09 +00:00
vars.push_back(IndexedVarDef(entry.second, entry.first));
}
2017-02-19 02:34:51 +00:00
2017-02-20 00:56:56 +00:00
// TODO: Optimize for const char*?
2017-02-23 08:18:54 +00:00
TypeId IndexedFile::ToTypeId(const std::string& usr) {
2017-02-26 19:45:59 +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-02-27 07:25:59 +00:00
TypeId id(types.size());
2017-02-23 08:18:54 +00:00
types.push_back(IndexedTypeDef(id, usr));
2017-02-26 19:45:59 +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-02-23 08:18:54 +00:00
FuncId IndexedFile::ToFuncId(const std::string& usr) {
2017-02-26 19:45:59 +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-02-27 07:25:59 +00:00
FuncId id(funcs.size());
2017-02-23 08:18:54 +00:00
funcs.push_back(IndexedFuncDef(id, usr));
2017-02-26 19:45:59 +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-02-23 08:18:54 +00:00
VarId IndexedFile::ToVarId(const std::string& usr) {
2017-02-26 19:45:59 +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-02-27 07:25:59 +00:00
VarId id(vars.size());
2017-02-23 08:18:54 +00:00
vars.push_back(IndexedVarDef(id, usr));
2017-02-26 19:45:59 +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-02-23 08:18:54 +00:00
TypeId IndexedFile::ToTypeId(const CXCursor& cursor) {
2017-02-20 00:56:56 +00:00
return ToTypeId(clang::Cursor(cursor).get_usr());
}
2017-02-23 08:18:54 +00:00
FuncId IndexedFile::ToFuncId(const CXCursor& cursor) {
2017-02-20 00:56:56 +00:00
return ToFuncId(clang::Cursor(cursor).get_usr());
}
2017-02-23 08:18:54 +00:00
VarId IndexedFile::ToVarId(const CXCursor& cursor) {
2017-02-20 00:56:56 +00:00
return ToVarId(clang::Cursor(cursor).get_usr());
}
2017-02-23 08:18:54 +00:00
IndexedTypeDef* IndexedFile::Resolve(TypeId id) {
2017-02-25 23:59:09 +00:00
return &types[id.id];
2017-02-17 09:57:44 +00:00
}
2017-02-23 08:18:54 +00:00
IndexedFuncDef* IndexedFile::Resolve(FuncId id) {
2017-02-25 23:59:09 +00:00
return &funcs[id.id];
2017-02-17 09:57:44 +00:00
}
2017-02-23 08:18:54 +00:00
IndexedVarDef* IndexedFile::Resolve(VarId id) {
2017-02-25 23:59:09 +00:00
return &vars[id.id];
2017-02-17 09:57:44 +00:00
}
2017-02-23 08:18:54 +00:00
std::string IndexedFile::ToString() {
2017-02-18 19:37:24 +00:00
rapidjson::StringBuffer output;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(output);
writer.SetFormatOptions(
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
writer.SetIndent(' ', 2);
Serialize(writer, this);
2017-02-18 19:37:24 +00:00
return output.GetString();
2017-02-17 09:57:44 +00:00
}
2017-02-27 07:23:43 +00:00
IndexedTypeDef::IndexedTypeDef(TypeId id, const std::string& usr) : id(id), def(usr) {
2017-02-24 08:39:25 +00:00
assert(usr.size() > 0);
//std::cout << "Creating type with usr " << usr << std::endl;
}
2017-02-25 06:08:14 +00:00
void IndexedTypeDef::AddUsage(Location loc, bool insert_if_not_present) {
2017-02-24 08:39:25 +00:00
for (int i = uses.size() - 1; i >= 0; --i) {
if (uses[i].IsEqualTo(loc)) {
if (loc.interesting)
uses[i].interesting = true;
return;
}
}
if (insert_if_not_present)
uses.push_back(loc);
}
2017-02-20 06:40:55 +00:00
2017-02-21 05:16:45 +00:00
template<typename T>
bool Contains(const std::vector<T>& vec, const T& element) {
for (const T& entry : vec) {
if (entry == element)
return true;
}
return false;
}
2017-02-20 06:40:55 +00:00
2017-02-17 09:57:44 +00:00
2017-02-20 00:56:56 +00:00
int abortQuery(CXClientData client_data, void *reserved) {
// 0 -> continue
return 0;
}
void diagnostic(CXClientData client_data, CXDiagnosticSet, void *reserved) {}
CXIdxClientFile enteredMainFile(CXClientData client_data, CXFile mainFile, void *reserved) {
return nullptr;
}
CXIdxClientFile ppIncludedFile(CXClientData client_data, const CXIdxIncludedFileInfo *) {
return nullptr;
}
CXIdxClientASTFile importedASTFile(CXClientData client_data, const CXIdxImportedASTFileInfo *) {
return nullptr;
}
CXIdxClientContainer startedTranslationUnit(CXClientData client_data, void *reserved) {
return nullptr;
}
2017-02-20 02:00:58 +00:00
2017-02-20 06:40:55 +00:00
clang::VisiterResult DumpVisitor(clang::Cursor cursor, clang::Cursor parent, int* level) {
for (int i = 0; i < *level; ++i)
std::cout << " ";
std::cout << clang::ToString(cursor.get_kind()) << " " << cursor.get_spelling() << std::endl;
*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) {}
};
clang::VisiterResult FindChildOfKindVisitor(clang::Cursor cursor, clang::Cursor parent, FindChildOfKindParam* param) {
if (cursor.get_kind() == param->target_kind) {
param->result = cursor;
return clang::VisiterResult::Break;
}
return clang::VisiterResult::Recurse;
}
2017-02-22 01:06:43 +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, &param);
return param.result;
}
2017-02-22 01:06:43 +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()) {
case CXCursor_TypeRef:
case CXCursor_TemplateRef:
*result = cursor;
return clang::VisiterResult::Break;
}
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 02:00:58 +00:00
2017-02-20 00:56:56 +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 {
2017-02-23 08:18:54 +00:00
IndexedFile* db;
2017-02-20 00:56:56 +00:00
NamespaceHelper* ns;
// Record the last type usage location we recorded. Clang will sometimes
// visit the same expression twice so we wan't to avoid double-reporting
// usage information for those locations.
2017-02-21 00:25:00 +00:00
Location last_type_usage_location;
Location last_func_usage_location;
2017-02-20 00:56:56 +00:00
2017-02-23 08:18:54 +00:00
IndexParam(IndexedFile* db, NamespaceHelper* ns) : db(db), ns(ns) {}
2017-02-20 00:56:56 +00:00
};
/*
std::string GetNamespacePrefx(const CXIdxDeclInfo* decl) {
const CXIdxContainerInfo* container = decl->lexicalContainer;
while (container) {
}
}
*/
bool IsTypeDefinition(const CXIdxContainerInfo* container) {
if (!container)
return false;
switch (container->cursor.kind) {
2017-02-21 05:32:40 +00:00
case CXCursor_EnumDecl:
case CXCursor_UnionDecl:
2017-02-20 00:56:56 +00:00
case CXCursor_StructDecl:
case CXCursor_ClassDecl:
return true;
default:
return false;
}
}
struct VisitDeclForTypeUsageParam {
2017-02-23 08:18:54 +00:00
IndexedFile* db;
bool is_interesting;
int has_processed_any = false;
2017-02-22 01:06:43 +00:00
optional<clang::Cursor> previous_cursor;
optional<TypeId> initial_type;
2017-02-23 08:18:54 +00:00
VisitDeclForTypeUsageParam(IndexedFile* db, bool is_interesting)
: db(db), is_interesting(is_interesting) {}
};
void VisitDeclForTypeUsageVisitorHandler(clang::Cursor cursor, VisitDeclForTypeUsageParam* param) {
param->has_processed_any = true;
2017-02-23 08:18:54 +00:00
IndexedFile* db = param->db;
2017-02-21 06:28:01 +00:00
// TODO: Something in STL (type_traits)? reports an empty USR.
std::string referenced_usr = cursor.get_referenced().get_usr();
if (referenced_usr == "")
return;
TypeId ref_type_id = db->ToTypeId(referenced_usr);
if (!param->initial_type)
param->initial_type = ref_type_id;
if (param->is_interesting) {
2017-02-23 08:18:54 +00:00
IndexedTypeDef* ref_type_def = db->Resolve(ref_type_id);
2017-02-27 02:03:14 +00:00
Location loc = db->id_cache->Resolve(cursor, true /*interesting*/);
ref_type_def->AddUsage(loc);
}
2017-02-20 19:08:27 +00:00
}
clang::VisiterResult VisitDeclForTypeUsageVisitor(clang::Cursor cursor, clang::Cursor parent, VisitDeclForTypeUsageParam* param) {
switch (cursor.get_kind()) {
case CXCursor_TemplateRef:
case CXCursor_TypeRef:
if (param->previous_cursor) {
VisitDeclForTypeUsageVisitorHandler(param->previous_cursor.value(), param);
// This if is inside the above if because if there are multiple TypeRefs,
// we always want to process the first one. If we did not always process
// the first one, we cannot tell if there are more TypeRefs after it and
// logic for fetching the return type breaks. This happens in ParmDecl
// instances which only have one TypeRef child but are not interesting
// usages.
if (!param->is_interesting)
return clang::VisiterResult::Break;
}
param->previous_cursor = cursor;
}
return clang::VisiterResult::Continue;
}
2017-02-23 08:18:54 +00:00
optional<TypeId> ResolveDeclToType(IndexedFile* db, clang::Cursor decl_cursor,
2017-02-20 22:06:50 +00:00
bool is_interesting, const CXIdxContainerInfo* semantic_container,
const CXIdxContainerInfo* lexical_container) {
//
// 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
//
// 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
//
// 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;
if (IsTypeDefinition(semantic_container) && !IsTypeDefinition(lexical_container)) {
assert(decl_cursor.is_definition());
process_last_type_ref = false;
}
2017-02-20 19:08:27 +00:00
VisitDeclForTypeUsageParam param(db, is_interesting);
decl_cursor.VisitChildren(&VisitDeclForTypeUsageVisitor, &param);
// 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(), &param);
2017-02-20 22:06:50 +00:00
}
else {
// If we are not processing the last type ref, it *must* be a TypeRef (ie,
// and not a TemplateRef).
assert(!param.previous_cursor.has_value() ||
param.previous_cursor.value().get_kind() == CXCursor_TypeRef);
}
2017-02-20 19:08:27 +00:00
return param.initial_type;
}
2017-02-20 19:08:27 +00:00
2017-02-20 00:56:56 +00:00
void indexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
2017-02-21 07:33:05 +00:00
bool is_system_def = clang_Location_isInSystemHeader(clang_getCursorLocation(decl->cursor));
2017-02-20 00:56:56 +00:00
IndexParam* param = static_cast<IndexParam*>(client_data);
2017-02-23 08:18:54 +00:00
IndexedFile* db = param->db;
2017-02-20 00:56:56 +00:00
NamespaceHelper* ns = param->ns;
switch (decl->entityInfo->kind) {
case CXIdxEntity_CXXNamespace:
{
ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer, decl->entityInfo->name);
break;
}
2017-02-21 05:32:40 +00:00
case CXIdxEntity_EnumConstant:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_Field:
case CXIdxEntity_Variable:
2017-02-20 06:06:46 +00:00
case CXIdxEntity_CXXStaticVariable:
2017-02-20 00:56:56 +00:00
{
2017-02-20 19:08:27 +00:00
clang::Cursor decl_cursor = decl->cursor;
2017-02-20 00:56:56 +00:00
VarId var_id = db->ToVarId(decl->entityInfo->USR);
2017-02-23 08:18:54 +00:00
IndexedVarDef* var_def = db->Resolve(var_id);
2017-02-20 00:56:56 +00:00
2017-02-26 01:08:05 +00:00
var_def->is_bad_def = is_system_def;
2017-02-21 07:33:05 +00:00
2017-02-20 00:56:56 +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-02-25 06:08:14 +00:00
var_def->def.short_name = decl->entityInfo->name;
var_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, var_def->def.short_name);
2017-02-20 00:56:56 +00:00
//}
2017-02-27 02:03:14 +00:00
Location decl_loc = db->id_cache->Resolve(decl->loc, false /*interesting*/);
2017-02-20 07:51:31 +00:00
if (decl->isDefinition)
2017-02-25 06:08:14 +00:00
var_def->def.definition = decl_loc;
2017-02-20 07:51:31 +00:00
else
2017-02-25 06:08:14 +00:00
var_def->def.declaration = decl_loc;
2017-02-21 05:34:46 +00:00
var_def->uses.push_back(decl_loc);
2017-02-20 00:56:56 +00:00
2017-02-20 06:06:46 +00:00
2017-02-21 05:16:45 +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-02-22 01:06:43 +00:00
optional<TypeId> var_type = ResolveDeclToType(db, decl_cursor, decl_cursor.get_kind() != CXCursor_ParmDecl /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer);
if (var_type.has_value())
2017-02-25 06:08:14 +00:00
var_def->def.variable_type = var_type.value();
2017-02-20 00:56:56 +00:00
2017-02-20 19:08:27 +00:00
2017-02-20 00:56:56 +00:00
if (decl->isDefinition && IsTypeDefinition(decl->semanticContainer)) {
TypeId declaring_type_id = db->ToTypeId(decl->semanticContainer->cursor);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* declaring_type_def = db->Resolve(declaring_type_id);
2017-02-25 06:08:14 +00:00
var_def->def.declaring_type = declaring_type_id;
declaring_type_def->def.vars.push_back(var_id);
2017-02-20 00:56:56 +00:00
}
break;
}
case CXIdxEntity_Function:
case CXIdxEntity_CXXConstructor:
2017-02-20 06:31:25 +00:00
case CXIdxEntity_CXXDestructor:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_CXXInstanceMethod:
case CXIdxEntity_CXXStaticMethod:
2017-02-21 07:33:05 +00:00
case CXIdxEntity_CXXConversionFunction:
2017-02-20 00:56:56 +00:00
{
2017-02-20 19:08:27 +00:00
clang::Cursor decl_cursor = decl->cursor;
2017-02-20 00:56:56 +00:00
FuncId func_id = db->ToFuncId(decl->entityInfo->USR);
2017-02-23 08:18:54 +00:00
IndexedFuncDef* func_def = db->Resolve(func_id);
2017-02-20 00:56:56 +00:00
2017-02-26 01:08:05 +00:00
func_def->is_bad_def = is_system_def;
2017-02-21 07:33:05 +00:00
2017-02-20 00:56:56 +00:00
// TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow.
//if (!decl->isRedeclaration) {
2017-02-25 06:08:14 +00:00
func_def->def.short_name = decl->entityInfo->name;
func_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, func_def->def.short_name);
2017-02-20 00:56:56 +00:00
//}
2017-02-27 02:03:14 +00:00
Location decl_loc = db->id_cache->Resolve(decl->loc, false /*interesting*/);
2017-02-20 00:56:56 +00:00
if (decl->isDefinition)
2017-02-25 06:08:14 +00:00
func_def->def.definition = decl_loc;
2017-02-20 07:51:31 +00:00
else
2017-02-25 06:08:14 +00:00
func_def->declarations.push_back(decl_loc);
2017-02-21 05:34:46 +00:00
func_def->uses.push_back(decl_loc);
2017-02-20 00:56:56 +00:00
2017-02-20 07:06:38 +00:00
bool is_pure_virtual = clang_CXXMethod_isPureVirtual(decl->cursor);
2017-02-21 05:16:45 +00:00
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;
2017-02-20 07:06:38 +00:00
2017-02-20 00:56:56 +00:00
// 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.
2017-02-21 05:16:45 +00:00
if (IsTypeDefinition(decl->semanticContainer)) {
2017-02-20 00:56:56 +00:00
TypeId declaring_type_id = db->ToTypeId(decl->semanticContainer->cursor);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* declaring_type_def = db->Resolve(declaring_type_id);
2017-02-25 06:08:14 +00:00
func_def->def.declaring_type = declaring_type_id;
2017-02-21 05:16:45 +00:00
// Mark a type reference at the ctor/dtor location.
// TODO: Should it be interesting?
if (is_ctor_or_dtor) {
Location type_usage_loc = decl_loc;
declaring_type_def->AddUsage(type_usage_loc);
}
// Register function in declaring type if it hasn't been registered yet.
2017-02-25 06:08:14 +00:00
if (!Contains(declaring_type_def->def.funcs, func_id))
declaring_type_def->def.funcs.push_back(func_id);
2017-02-20 00:56:56 +00:00
}
2017-02-21 05:16:45 +00:00
// We don't actually need to know the return type, but we need to mark it
// as an interesting usage.
2017-02-20 22:06:50 +00:00
ResolveDeclToType(db, decl_cursor, true /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer);
//TypeResolution ret_type = ResolveToType(db, decl_cursor.get_type().get_return_type());
//if (ret_type.resolved_type)
// AddInterestingUsageToType(db, ret_type, FindLocationOfTypeSpecifier(decl_cursor));
2017-02-20 02:35:56 +00:00
2017-02-20 07:06:38 +00:00
if (decl->isDefinition || is_pure_virtual) {
2017-02-20 06:06:46 +00:00
// 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.
2017-02-20 19:08:27 +00:00
// 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.
2017-02-20 02:35:56 +00:00
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.
2017-02-20 22:06:50 +00:00
ResolveDeclToType(db, arg, true /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer);
//TypeResolution arg_type = ResolveToType(db, arg.get_type());
//if (arg_type.resolved_type)
// AddInterestingUsageToType(db, arg_type, FindLocationOfTypeSpecifier(arg));
2017-02-20 02:35:56 +00:00
break;
}
}
2017-02-20 07:06:38 +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];
FuncId parent_id = db->ToFuncId(parent.get_usr());
2017-02-23 08:18:54 +00:00
IndexedFuncDef* parent_def = db->Resolve(parent_id);
2017-02-20 07:06:38 +00:00
func_def = db->Resolve(func_id); // ToFuncId invalidated func_def
2017-02-25 06:08:14 +00:00
func_def->def.base = parent_id;
2017-02-20 07:06:38 +00:00
parent_def->derived.push_back(func_id);
}
clang_disposeOverriddenCursors(overridden);
}
2017-02-20 02:35:56 +00:00
}
2017-02-20 00:56:56 +00:00
/*
2017-02-22 01:06:43 +00:00
optional<FuncId> base;
2017-02-20 00:56:56 +00:00
std::vector<FuncId> derived;
std::vector<VarId> locals;
std::vector<FuncRef> callers;
std::vector<FuncRef> callees;
2017-02-21 00:25:00 +00:00
std::vector<Location> uses;
2017-02-20 00:56:56 +00:00
*/
break;
}
2017-02-20 02:35:56 +00:00
case CXIdxEntity_Typedef:
case CXIdxEntity_CXXTypeAlias:
{
2017-02-22 01:06:43 +00:00
optional<TypeId> alias_of = ResolveDeclToType(db, decl->cursor, true /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer);
2017-02-20 06:31:25 +00:00
2017-02-20 22:06:50 +00:00
TypeId type_id = db->ToTypeId(decl->entityInfo->USR);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* type_def = db->Resolve(type_id);
2017-02-20 02:35:56 +00:00
2017-02-26 01:08:05 +00:00
type_def->is_bad_def = is_system_def;
2017-02-21 07:33:05 +00:00
2017-02-20 22:06:50 +00:00
if (alias_of)
2017-02-25 06:08:14 +00:00
type_def->def.alias_of = alias_of.value();
2017-02-20 02:35:56 +00:00
2017-02-25 06:08:14 +00:00
type_def->def.short_name = decl->entityInfo->name;
type_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->def.short_name);
2017-02-20 02:35:56 +00:00
2017-02-27 02:03:14 +00:00
Location decl_loc = db->id_cache->Resolve(decl->loc, true /*interesting*/);
2017-02-25 06:08:14 +00:00
type_def->def.definition = decl_loc.WithInteresting(false);
type_def->AddUsage(decl_loc);
2017-02-20 02:35:56 +00:00
break;
}
2017-02-21 05:32:40 +00:00
case CXIdxEntity_Enum:
case CXIdxEntity_Union:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_Struct:
case CXIdxEntity_CXXClass:
{
TypeId type_id = db->ToTypeId(decl->entityInfo->USR);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* type_def = db->Resolve(type_id);
2017-02-20 00:56:56 +00:00
2017-02-26 01:08:05 +00:00
type_def->is_bad_def = is_system_def;
2017-02-21 07:33:05 +00:00
2017-02-20 00:56:56 +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) {
2017-02-21 06:11:47 +00:00
// 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-02-25 06:08:14 +00:00
type_def->def.short_name = decl->entityInfo->name;
2017-02-21 06:11:47 +00:00
}
else {
2017-02-25 06:08:14 +00:00
type_def->def.short_name = "<anonymous>";
2017-02-21 06:11:47 +00:00
}
2017-02-25 06:08:14 +00:00
type_def->def.qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->def.short_name);
2017-02-21 06:11:47 +00:00
2017-02-20 00:56:56 +00:00
// }
2017-02-20 07:51:31 +00:00
assert(decl->isDefinition);
2017-02-27 02:03:14 +00:00
Location decl_loc = db->id_cache->Resolve(decl->loc, true /*interesting*/);
2017-02-25 06:08:14 +00:00
type_def->def.definition = decl_loc.WithInteresting(false);
type_def->AddUsage(decl_loc);
2017-02-20 00:56:56 +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);
2017-02-21 05:32:40 +00:00
if (class_info) {
for (unsigned int i = 0; i < class_info->numBases; ++i) {
const CXIdxBaseClassInfo* base_class = class_info->bases[i];
2017-02-22 01:06:43 +00:00
optional<TypeId> parent_type_id = ResolveDeclToType(db, base_class->cursor, true /*is_interesting*/, decl->semanticContainer, decl->lexicalContainer);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* type_def = db->Resolve(type_id); // type_def ptr could be invalidated by ResolveDeclToType.
2017-02-21 05:32:40 +00:00
if (parent_type_id) {
2017-02-23 08:18:54 +00:00
IndexedTypeDef* parent_type_def = db->Resolve(parent_type_id.value());
2017-02-21 05:32:40 +00:00
parent_type_def->derived.push_back(type_id);
2017-02-25 06:08:14 +00:00
type_def->def.parents.push_back(parent_type_id.value());
2017-02-21 05:32:40 +00:00
}
}
2017-02-20 00:56:56 +00:00
}
break;
}
default:
2017-02-27 02:03:14 +00:00
std::cout << "!! Unhandled indexDeclaration: " << clang::Cursor(decl->cursor).ToString() << " at " << db->id_cache->Resolve(decl->loc, false /*interesting*/).ToString() << std::endl;
2017-02-20 02:35:56 +00:00
std::cout << " entityInfo->kind = " << decl->entityInfo->kind << std::endl;
2017-02-20 00:56:56 +00:00
std::cout << " entityInfo->USR = " << decl->entityInfo->USR << std::endl;
if (decl->declAsContainer)
std::cout << " declAsContainer = " << clang::Cursor(decl->declAsContainer->cursor).ToString() << std::endl;
if (decl->semanticContainer)
std::cout << " semanticContainer = " << clang::Cursor(decl->semanticContainer->cursor).ToString() << std::endl;
if (decl->lexicalContainer)
std::cout << " lexicalContainer = " << clang::Cursor(decl->lexicalContainer->cursor).get_usr() << std::endl;
break;
}
}
bool IsFunction(CXCursorKind kind) {
switch (kind) {
case CXCursor_CXXMethod:
case CXCursor_FunctionDecl:
return true;
}
return false;
}
void indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) {
IndexParam* param = static_cast<IndexParam*>(client_data);
2017-02-23 08:18:54 +00:00
IndexedFile* db = param->db;
2017-02-20 00:56:56 +00:00
clang::Cursor cursor(ref->cursor);
switch (ref->referencedEntity->kind) {
2017-02-21 05:32:40 +00:00
case CXIdxEntity_EnumConstant:
2017-02-20 06:24:05 +00:00
case CXIdxEntity_CXXStaticVariable:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_Variable:
case CXIdxEntity_Field:
{
VarId var_id = db->ToVarId(ref->referencedEntity->cursor);
2017-02-23 08:18:54 +00:00
IndexedVarDef* var_def = db->Resolve(var_id);
2017-02-27 02:03:14 +00:00
var_def->uses.push_back(db->id_cache->Resolve(ref->loc, false /*interesting*/));
2017-02-20 00:56:56 +00:00
break;
}
2017-02-21 07:33:05 +00:00
case CXIdxEntity_CXXConversionFunction:
2017-02-20 02:00:58 +00:00
case CXIdxEntity_CXXStaticMethod:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_CXXInstanceMethod:
case CXIdxEntity_Function:
2017-02-20 06:31:25 +00:00
case CXIdxEntity_CXXConstructor:
case CXIdxEntity_CXXDestructor:
2017-02-20 00:56:56 +00:00
{
2017-02-21 05:59:48 +00:00
// TODO: Redirect container to constructor for the following example, ie,
// we should be inserting an outgoing function call from the Foo
// ctor.
//
2017-02-20 00:56:56 +00:00
// int Gen() { return 5; }
// class Foo {
// int x = Gen();
// }
// Don't report duplicate usages.
2017-02-20 02:00:58 +00:00
// TODO: search full history?
2017-02-27 02:03:14 +00:00
Location loc = db->id_cache->Resolve(ref->loc, false /*interesting*/);
2017-02-20 00:56:56 +00:00
if (param->last_func_usage_location == loc) break;
param->last_func_usage_location = loc;
// Note: be careful, calling db->ToFuncId invalidates the FuncDef* ptrs.
FuncId called_id = db->ToFuncId(ref->referencedEntity->USR);
if (IsFunction(ref->container->cursor.kind)) {
FuncId caller_id = db->ToFuncId(ref->container->cursor);
2017-02-23 08:18:54 +00:00
IndexedFuncDef* caller_def = db->Resolve(caller_id);
IndexedFuncDef* called_def = db->Resolve(called_id);
2017-02-20 00:56:56 +00:00
2017-02-25 06:08:14 +00:00
caller_def->def.callees.push_back(FuncRef(called_id, loc));
2017-02-20 00:56:56 +00:00
called_def->callers.push_back(FuncRef(caller_id, loc));
2017-02-21 05:34:46 +00:00
called_def->uses.push_back(loc);
2017-02-20 00:56:56 +00:00
}
else {
2017-02-23 08:18:54 +00:00
IndexedFuncDef* called_def = db->Resolve(called_id);
2017-02-21 05:34:46 +00:00
called_def->uses.push_back(loc);
2017-02-20 00:56:56 +00:00
}
2017-02-21 05:16:45 +00:00
// For constructor/destructor, also add a usage against the type. Clang
// will insert and visit implicit constructor references, so we also check
// the location of the ctor call compared to the parent call. If they are
// the same, this is most likely an implicit ctors.
clang::Cursor ref_cursor = ref->cursor;
if (ref->referencedEntity->kind == CXIdxEntity_CXXConstructor ||
2017-02-24 08:39:25 +00:00
ref->referencedEntity->kind == CXIdxEntity_CXXDestructor) {
2017-02-21 05:16:45 +00:00
2017-02-27 02:03:14 +00:00
Location parent_loc = db->id_cache->Resolve(ref->parentEntity->cursor, true /*interesting*/);
Location our_loc = db->id_cache->Resolve(ref->loc, true /*is_interesting*/);
2017-02-21 05:16:45 +00:00
if (!parent_loc.IsEqualTo(our_loc)) {
2017-02-23 08:18:54 +00:00
IndexedFuncDef* called_def = db->Resolve(called_id);
2017-02-25 06:08:14 +00:00
assert(called_def->def.declaring_type.has_value());
IndexedTypeDef* type_def = db->Resolve(called_def->def.declaring_type.value());
2017-02-21 05:16:45 +00:00
type_def->AddUsage(our_loc);
}
}
2017-02-20 00:56:56 +00:00
break;
}
2017-02-17 09:57:44 +00:00
2017-02-20 02:35:56 +00:00
case CXIdxEntity_Typedef:
case CXIdxEntity_CXXTypeAlias:
2017-02-21 05:32:40 +00:00
case CXIdxEntity_Enum:
case CXIdxEntity_Union:
2017-02-20 00:56:56 +00:00
case CXIdxEntity_Struct:
case CXIdxEntity_CXXClass:
{
TypeId referenced_id = db->ToTypeId(ref->referencedEntity->USR);
2017-02-23 08:18:54 +00:00
IndexedTypeDef* referenced_def = db->Resolve(referenced_id);
2017-02-27 02:03:14 +00:00
2017-02-26 01:08:05 +00:00
// We will not get a declaration visit for forward declared types. Try to mark them as non-bad
// defs here so we will output usages/etc.
if (referenced_def->is_bad_def) {
bool is_system_def = clang_Location_isInSystemHeader(clang_getCursorLocation(ref->referencedEntity->cursor));
2017-02-27 02:03:14 +00:00
Location loc = db->id_cache->Resolve(ref->referencedEntity->cursor, false /*interesting*/);
2017-02-26 01:08:05 +00:00
if (!is_system_def && loc.raw_file_id != -1)
referenced_def->is_bad_def = false;
}
2017-02-20 00:56:56 +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.
//
2017-02-20 21:55:48 +00:00
// To work around this, we check to see if the usage location has been
// inserted into all_uses previously.
2017-02-20 00:56:56 +00:00
//
// struct Foo {};
// void Make() {
// Foo f;
// }
//
2017-02-27 02:03:14 +00:00
referenced_def->AddUsage(db->id_cache->Resolve(ref->loc, false /*interesting*/));
2017-02-20 00:56:56 +00:00
break;
}
default:
2017-02-27 02:03:14 +00:00
std::cout << "!! Unhandled indexEntityReference: " << cursor.ToString() << " at " << db->id_cache->Resolve(ref->loc, false /*interesting*/).ToString() << std::endl;
2017-02-20 00:56:56 +00:00
std::cout << " ref->referencedEntity->kind = " << ref->referencedEntity->kind << std::endl;
if (ref->parentEntity)
std::cout << " ref->parentEntity->kind = " << ref->parentEntity->kind << std::endl;
2017-02-27 02:03:14 +00:00
std::cout << " ref->loc = " << db->id_cache->Resolve(ref->loc, false /*interesting*/).ToString() << std::endl;
2017-02-20 00:56:56 +00:00
std::cout << " ref->kind = " << ref->kind << std::endl;
if (ref->parentEntity)
std::cout << " parentEntity = " << clang::Cursor(ref->parentEntity->cursor).ToString() << std::endl;
if (ref->referencedEntity)
std::cout << " referencedEntity = " << clang::Cursor(ref->referencedEntity->cursor).ToString() << std::endl;
if (ref->container)
std::cout << " container = " << clang::Cursor(ref->container->cursor).ToString() << std::endl;
break;
}
}
2017-02-17 09:57:44 +00:00
2017-02-27 07:28:33 +00:00
IndexedFile Parse(IdCache* id_cache, std::string filename, std::vector<std::string> args, bool dump_ast) {
2017-02-16 09:35:30 +00:00
clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/);
2017-02-17 09:57:44 +00:00
clang::TranslationUnit tu(index, filename, args);
2017-02-16 09:35:30 +00:00
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
CXIndexAction index_action = clang_IndexAction_create(index.cx_index);
IndexerCallbacks callbacks[] = {
{ &abortQuery, &diagnostic, &enteredMainFile, &ppIncludedFile, &importedASTFile, &startedTranslationUnit, &indexDeclaration, &indexEntityReference }
/*
callbacks.abortQuery = &abortQuery;
callbacks.diagnostic = &diagnostic;
callbacks.enteredMainFile = &enteredMainFile;
callbacks.ppIncludedFile = &ppIncludedFile;
callbacks.importedASTFile = &importedASTFile;
callbacks.startedTranslationUnit = &startedTranslationUnit;
callbacks.indexDeclaration = &indexDeclaration;
callbacks.indexEntityReference = &indexEntityReference;
*/
};
2017-02-27 07:23:43 +00:00
IndexedFile db(filename, id_cache);
2017-02-20 00:56:56 +00:00
NamespaceHelper ns;
IndexParam param(&db, &ns);
clang_indexTranslationUnit(index_action, &param, callbacks, sizeof(callbacks),
2017-02-21 07:33:05 +00:00
CXIndexOpt_IndexFunctionLocalSymbols | CXIndexOpt_SkipParsedBodiesInSession, tu.cx_tu);
2017-02-20 00:56:56 +00:00
clang_IndexAction_dispose(index_action);
return db;
}