ccls/src/indexer.h

512 lines
14 KiB
C
Raw Normal View History

2017-02-22 08:52:00 +00:00
#pragma once
2017-04-08 22:54:36 +00:00
#include "file_consumer.h"
2017-04-05 08:06:18 +00:00
#include "position.h"
2017-03-14 08:33:39 +00:00
#include "serializer.h"
2017-04-05 08:06:18 +00:00
#include "utils.h"
#include "language_server_api.h"
2017-03-25 20:32:44 +00:00
#include "libclangmm/clangmm.h"
#include "libclangmm/Utility.h"
2017-02-22 08:52:00 +00:00
2017-03-25 20:32:44 +00:00
#include <optional.h>
2017-02-22 08:52:00 +00:00
#include <rapidjson/writer.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/document.h>
2017-03-25 20:32:44 +00:00
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <fstream>
2017-04-14 22:30:33 +00:00
#include <iostream>
2017-03-25 20:32:44 +00:00
#include <unordered_map>
2017-04-14 22:30:33 +00:00
#include <vector>
2017-03-25 20:32:44 +00:00
2017-02-24 08:39:25 +00:00
struct IndexedTypeDef;
struct IndexedFuncDef;
struct IndexedVarDef;
2017-02-22 08:52:00 +00:00
using namespace std::experimental;
2017-03-17 07:58:41 +00:00
template <typename T>
2017-02-25 23:59:09 +00:00
struct Id {
size_t id;
2017-02-25 23:59:09 +00:00
2017-03-17 07:58:41 +00:00
Id() : id(0) {} // Needed for containers. Do not use directly.
explicit Id(size_t id) : id(id) {}
2017-02-25 23:59:09 +00:00
// Needed for google::dense_hash_map.
explicit operator size_t() const { return id; }
2017-03-17 07:58:41 +00:00
bool operator==(const Id<T>& other) const { return id == other.id; }
2017-02-25 23:59:09 +00:00
2017-03-17 07:58:41 +00:00
bool operator<(const Id<T>& other) const { return id < other.id; }
2017-02-25 23:59:09 +00:00
};
namespace std {
2017-03-17 07:58:41 +00:00
template <typename T>
struct hash<Id<T>> {
2017-04-17 20:40:50 +00:00
size_t operator()(const Id<T>& k) const { return hash<size_t>()(k.id); }
2017-03-17 07:58:41 +00:00
};
2017-02-25 23:59:09 +00:00
}
2017-03-17 07:58:41 +00:00
template <typename T>
2017-02-25 23:59:09 +00:00
bool operator==(const Id<T>& a, const Id<T>& b) {
assert(a.group == b.group && "Cannot compare Ids from different groups");
return a.id == b.id;
}
template <typename T>
bool operator!=(const Id<T>& a, const Id<T>& b) {
return !(a == b);
}
2017-02-25 23:59:09 +00:00
using IndexTypeId = Id<IndexedTypeDef>;
using IndexFuncId = Id<IndexedFuncDef>;
using IndexVarId = Id<IndexedVarDef>;
2017-02-25 23:59:09 +00:00
2017-03-13 06:03:54 +00:00
struct IdCache;
2017-03-11 02:24:51 +00:00
2017-03-17 07:58:41 +00:00
template <typename T>
2017-02-25 23:59:09 +00:00
struct Ref {
Id<T> id() const {
assert(has_id());
return id_;
}
bool has_id() const {
return id_.id != -1;
}
Id<T> id_;
2017-04-05 08:06:18 +00:00
Range loc;
2017-02-22 08:52:00 +00:00
2017-03-17 07:58:41 +00:00
Ref() {} // For serialization.
Ref(Id<T> id, Range loc) : id_(id), loc(loc) {}
Ref(Range loc) : id_(Id<T>(-1)), loc(loc) {}
2017-02-22 08:52:00 +00:00
2017-02-25 23:59:09 +00:00
bool operator==(const Ref<T>& other) {
return id_ == other.id_ && loc == other.loc;
2017-02-25 23:59:09 +00:00
}
2017-03-17 07:58:41 +00:00
bool operator!=(const Ref<T>& other) { return !(*this == other); }
2017-02-25 23:59:09 +00:00
bool operator<(const Ref<T>& other) const {
2017-04-14 22:30:33 +00:00
return id_ < other.id && loc < other.loc;
2017-02-22 08:52:00 +00:00
}
};
2017-03-17 07:58:41 +00:00
template <typename T>
2017-02-25 23:59:09 +00:00
bool operator==(const Ref<T>& a, const Ref<T>& b) {
return a.id_ == b.id_ && a.loc == b.loc;
2017-02-22 08:52:00 +00:00
}
2017-03-17 07:58:41 +00:00
template <typename T>
2017-02-25 23:59:09 +00:00
bool operator!=(const Ref<T>& a, const Ref<T>& b) {
return !(a == b);
}
2017-02-22 08:52:00 +00:00
2017-04-08 08:04:38 +00:00
using IndexFuncRef = Ref<IndexedFuncDef>;
2017-02-22 08:52:00 +00:00
// TODO: skip as much forward-processing as possible when |is_system_def| is
// set to false.
// TODO: Either eliminate the defs created as a by-product of cross-referencing,
// or do not emit things we don't have definitions for.
2017-04-05 08:06:18 +00:00
template <typename TypeId,
typename FuncId,
typename VarId,
typename Range>
2017-02-25 06:08:14 +00:00
struct TypeDefDefinitionData {
2017-02-22 08:52:00 +00:00
// General metadata.
std::string usr;
std::string short_name;
std::string detailed_name;
2017-02-22 08:52:00 +00:00
// While a class/type can technically have a separate declaration/definition,
// it doesn't really happen in practice. The declaration never contains
// comments or insightful information. The user always wants to jump from
// the declaration to the definition - never the other way around like in
// functions and (less often) variables.
//
// It's also difficult to identify a `class Foo;` statement with the clang
// indexer API (it's doable using cursor AST traversal), so we don't bother
// supporting the feature.
optional<Range> definition_spelling;
2017-04-05 08:06:18 +00:00
optional<Range> definition_extent;
2017-02-22 08:52:00 +00:00
// If set, then this is the same underlying type as the given value (ie, this
// type comes from a using or typedef statement).
optional<TypeId> alias_of;
2017-02-25 06:08:14 +00:00
// Immediate parent types.
2017-02-22 08:52:00 +00:00
std::vector<TypeId> parents;
// Types, functions, and variables defined in this type.
std::vector<TypeId> types;
std::vector<FuncId> funcs;
std::vector<VarId> vars;
2017-03-17 07:58:41 +00:00
TypeDefDefinitionData() {} // For reflection.
2017-02-27 07:23:43 +00:00
TypeDefDefinitionData(const std::string& usr) : usr(usr) {}
2017-02-25 23:59:09 +00:00
bool HasInterestingState() const {
return
!short_name.empty() ||
!detailed_name.empty() ||
definition_spelling ||
definition_extent ||
alias_of ||
!parents.empty() ||
!types.empty() ||
!funcs.empty() ||
!vars.empty();
}
bool operator==(const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
2017-04-05 08:06:18 +00:00
definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent &&
alias_of == other.alias_of &&
2017-03-17 07:58:41 +00:00
parents == other.parents && types == other.types &&
funcs == other.funcs && vars == other.vars;
2017-02-25 23:59:09 +00:00
}
bool operator!=(const TypeDefDefinitionData<TypeId, FuncId, VarId, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return !(*this == other);
}
2017-02-25 06:08:14 +00:00
};
2017-03-17 07:58:41 +00:00
template <typename TVisitor,
typename TypeId,
typename FuncId,
typename VarId,
2017-04-05 08:06:18 +00:00
typename Range>
2017-03-17 07:58:41 +00:00
void Reflect(TVisitor& visitor,
TypeDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER_START();
REFLECT_MEMBER(usr);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER(definition);
REFLECT_MEMBER(alias_of);
REFLECT_MEMBER(parents);
REFLECT_MEMBER(types);
REFLECT_MEMBER(funcs);
REFLECT_MEMBER(vars);
REFLECT_MEMBER_END();
}
2017-02-25 06:08:14 +00:00
struct IndexedTypeDef {
using Def = TypeDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, Range>;
2017-04-05 08:06:18 +00:00
Def def;
2017-02-25 06:08:14 +00:00
IndexTypeId id;
2017-02-27 07:23:43 +00:00
2017-02-25 06:08:14 +00:00
// Immediate derived types.
std::vector<IndexTypeId> derived;
2017-02-25 06:08:14 +00:00
2017-04-03 01:34:15 +00:00
// Declared variables of this type.
// TODO: this needs a lot more work and lots of tests.
// TODO: add instantiation on ctor / dtor, do not add instantiation if type is ptr
std::vector<IndexVarId> instantiations;
2017-04-03 01:34:15 +00:00
2017-02-22 08:52:00 +00:00
// Every usage, useful for things like renames.
// NOTE: Do not insert directly! Use AddUsage instead.
2017-04-05 08:06:18 +00:00
std::vector<Range> uses;
2017-02-22 08:52:00 +00:00
2017-03-17 07:58:41 +00:00
IndexedTypeDef() : def("") {} // For serialization
IndexedTypeDef(IndexTypeId id, const std::string& usr);
2017-02-25 23:59:09 +00:00
2017-04-03 01:34:15 +00:00
bool HasInterestingState() const {
return
def.HasInterestingState() ||
2017-04-03 01:34:15 +00:00
!derived.empty() ||
!instantiations.empty() ||
!uses.empty();
}
2017-02-25 23:59:09 +00:00
bool operator<(const IndexedTypeDef& other) const {
2017-02-27 07:23:43 +00:00
return def.usr < other.def.usr;
2017-02-25 23:59:09 +00:00
}
2017-02-22 08:52:00 +00:00
};
2017-03-25 20:40:04 +00:00
MAKE_HASHABLE(IndexedTypeDef, t.def.usr);
2017-02-25 23:59:09 +00:00
2017-04-05 08:06:18 +00:00
template <typename TypeId,
typename FuncId,
typename VarId,
typename FuncRef,
typename Range>
2017-02-25 06:08:14 +00:00
struct FuncDefDefinitionData {
2017-02-22 08:52:00 +00:00
// General metadata.
std::string usr;
std::string short_name;
std::string detailed_name;
optional<Range> definition_spelling;
2017-04-05 08:06:18 +00:00
optional<Range> definition_extent;
2017-02-22 08:52:00 +00:00
// Type which declares this one (ie, it is a method)
optional<TypeId> declaring_type;
2017-02-25 06:08:14 +00:00
2017-02-22 08:52:00 +00:00
// Method this method overrides.
optional<FuncId> base;
// Local variables defined in this function.
std::vector<VarId> locals;
2017-02-25 06:08:14 +00:00
// Functions that this function calls.
std::vector<FuncRef> callees;
2017-03-17 07:58:41 +00:00
FuncDefDefinitionData() {} // For reflection.
2017-02-27 07:23:43 +00:00
FuncDefDefinitionData(const std::string& usr) : usr(usr) {
2017-03-17 07:58:41 +00:00
// assert(usr.size() > 0);
2017-02-25 06:08:14 +00:00
}
2017-02-25 23:59:09 +00:00
bool HasInterestingState() const {
return
!short_name.empty() ||
!detailed_name.empty() ||
definition_spelling ||
definition_extent ||
declaring_type ||
base ||
!locals.empty() ||
!callees.empty();
}
2017-03-17 07:58:41 +00:00
bool operator==(
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
2017-04-05 08:06:18 +00:00
definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent &&
2017-03-17 07:58:41 +00:00
declaring_type == other.declaring_type && base == other.base &&
locals == other.locals && callees == other.callees;
}
bool operator!=(
const FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return !(*this == other);
2017-02-25 23:59:09 +00:00
}
2017-02-25 06:08:14 +00:00
};
2017-03-17 07:58:41 +00:00
template <typename TVisitor,
typename TypeId,
typename FuncId,
typename VarId,
typename FuncRef,
2017-04-05 08:06:18 +00:00
typename Range>
2017-03-17 07:58:41 +00:00
void Reflect(
TVisitor& visitor,
FuncDefDefinitionData<TypeId, FuncId, VarId, FuncRef, Range>& value) {
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER_START();
REFLECT_MEMBER(usr);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER(definition);
REFLECT_MEMBER(declaring_type);
REFLECT_MEMBER(base);
REFLECT_MEMBER(locals);
REFLECT_MEMBER(callees);
REFLECT_MEMBER_END();
}
2017-02-25 06:08:14 +00:00
struct IndexedFuncDef {
2017-04-08 08:04:38 +00:00
using Def = FuncDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, IndexFuncRef, Range>;
2017-04-05 08:06:18 +00:00
Def def;
2017-02-25 06:08:14 +00:00
IndexFuncId id;
2017-02-27 07:23:43 +00:00
2017-02-25 06:08:14 +00:00
// Places the function is forward-declared.
2017-04-05 08:06:18 +00:00
std::vector<Range> declarations;
2017-02-25 06:08:14 +00:00
// Methods which directly override this one.
std::vector<IndexFuncId> derived;
2017-02-25 06:08:14 +00:00
// Calls/usages of this function. If the call is coming from outside a
// function context then the FuncRef will not have an associated id.
//
// To get all usages, also include the ranges inside of declarations and
// def.definition_spelling.
2017-04-08 08:04:38 +00:00
std::vector<IndexFuncRef> callers;
2017-02-22 08:52:00 +00:00
2017-03-17 07:58:41 +00:00
IndexedFuncDef() {} // For reflection.
IndexedFuncDef(IndexFuncId id, const std::string& usr) : def(usr), id(id) {
2017-03-17 07:58:41 +00:00
// assert(usr.size() > 0);
2017-02-22 08:52:00 +00:00
}
2017-02-25 23:59:09 +00:00
2017-04-03 01:34:15 +00:00
bool HasInterestingState() const {
return
def.HasInterestingState() ||
2017-04-03 01:34:15 +00:00
!def.callees.empty() ||
!declarations.empty() ||
!derived.empty() ||
!callers.empty();
2017-04-03 01:34:15 +00:00
}
2017-02-25 23:59:09 +00:00
bool operator<(const IndexedFuncDef& other) const {
2017-02-27 07:23:43 +00:00
return def.usr < other.def.usr;
2017-02-25 23:59:09 +00:00
}
2017-02-22 08:52:00 +00:00
};
2017-03-25 20:40:04 +00:00
MAKE_HASHABLE(IndexedFuncDef, t.def.usr);
2017-02-25 23:59:09 +00:00
2017-04-05 08:06:18 +00:00
template <typename TypeId,
typename FuncId,
typename VarId,
typename Range>
2017-02-25 06:08:14 +00:00
struct VarDefDefinitionData {
2017-02-22 08:52:00 +00:00
// General metadata.
std::string usr;
std::string short_name;
std::string detailed_name;
2017-04-05 08:06:18 +00:00
optional<Range> declaration;
// TODO: definitions should be a list of ranges, since there can be more
// than one - when??
optional<Range> definition_spelling;
2017-04-05 08:06:18 +00:00
optional<Range> definition_extent;
2017-02-22 08:52:00 +00:00
// Type of the variable.
optional<TypeId> variable_type;
// Type which declares this one (ie, it is a method)
optional<TypeId> declaring_type;
2017-03-17 07:58:41 +00:00
VarDefDefinitionData() {} // For reflection.
2017-02-27 07:23:43 +00:00
VarDefDefinitionData(const std::string& usr) : usr(usr) {}
2017-02-25 23:59:09 +00:00
bool HasInterestingState() const {
return
!short_name.empty() ||
!detailed_name.empty() ||
declaration ||
definition_spelling ||
definition_extent ||
variable_type ||
declaring_type;
}
bool operator==(const VarDefDefinitionData<TypeId, FuncId, VarId, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return usr == other.usr && short_name == other.short_name &&
detailed_name == other.detailed_name &&
2017-04-05 08:06:18 +00:00
declaration == other.declaration &&
definition_spelling == other.definition_spelling &&
definition_extent == other.definition_extent &&
2017-03-17 07:58:41 +00:00
variable_type == other.variable_type &&
declaring_type == other.declaring_type;
}
bool operator!=(const VarDefDefinitionData<TypeId, FuncId, VarId, Range>&
2017-03-17 07:58:41 +00:00
other) const {
return !(*this == other);
2017-02-25 23:59:09 +00:00
}
2017-02-25 06:08:14 +00:00
};
2017-03-17 07:58:41 +00:00
template <typename TVisitor,
typename TypeId,
typename FuncId,
typename VarId,
2017-04-05 08:06:18 +00:00
typename Range>
2017-03-17 07:58:41 +00:00
void Reflect(TVisitor& visitor,
VarDefDefinitionData<TypeId, FuncId, VarId, Range>& value) {
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER_START();
REFLECT_MEMBER(usr);
REFLECT_MEMBER(short_name);
REFLECT_MEMBER(detailed_name);
2017-04-05 08:06:18 +00:00
REFLECT_MEMBER(definition_spelling);
REFLECT_MEMBER(definition_extent);
2017-03-14 08:33:39 +00:00
REFLECT_MEMBER(variable_type);
REFLECT_MEMBER(declaring_type);
REFLECT_MEMBER_END();
}
2017-02-25 06:08:14 +00:00
struct IndexedVarDef {
using Def = VarDefDefinitionData<IndexTypeId, IndexFuncId, IndexVarId, Range>;
2017-04-05 08:06:18 +00:00
Def def;
2017-02-25 06:08:14 +00:00
IndexVarId id;
2017-02-27 07:23:43 +00:00
2017-02-22 08:52:00 +00:00
// Usages.
2017-04-05 08:06:18 +00:00
std::vector<Range> uses;
2017-02-25 23:59:09 +00:00
2017-03-17 07:58:41 +00:00
IndexedVarDef() : def("") {} // For serialization
IndexedVarDef(IndexVarId id, const std::string& usr) : def(usr), id(id) {
2017-03-17 07:58:41 +00:00
// assert(usr.size() > 0);
2017-02-22 08:52:00 +00:00
}
2017-02-25 23:59:09 +00:00
2017-04-03 01:34:15 +00:00
bool HasInterestingState() const {
return
def.HasInterestingState() ||
2017-04-03 01:34:15 +00:00
!uses.empty();
}
2017-02-25 23:59:09 +00:00
bool operator<(const IndexedVarDef& other) const {
2017-02-27 07:23:43 +00:00
return def.usr < other.def.usr;
2017-02-25 23:59:09 +00:00
}
2017-02-22 08:52:00 +00:00
};
2017-03-25 20:40:04 +00:00
MAKE_HASHABLE(IndexedVarDef, t.def.usr);
2017-02-22 08:52:00 +00:00
2017-02-26 19:45:59 +00:00
struct IdCache {
2017-04-03 01:34:15 +00:00
std::string primary_file;
std::unordered_map<std::string, IndexTypeId> usr_to_type_id;
std::unordered_map<std::string, IndexFuncId> usr_to_func_id;
std::unordered_map<std::string, IndexVarId> usr_to_var_id;
std::unordered_map<IndexTypeId, std::string> type_id_to_usr;
std::unordered_map<IndexFuncId, std::string> func_id_to_usr;
std::unordered_map<IndexVarId, std::string> var_id_to_usr;
2017-02-22 08:52:00 +00:00
2017-04-03 01:34:15 +00:00
IdCache(const std::string& primary_file);
2017-04-05 08:06:18 +00:00
Range Resolve(const CXSourceRange& range);
Range ResolveSpelling(const CXCursor& cx_cursor);
Range ResolveExtent(const CXCursor& cx_cursor);
2017-02-25 23:59:09 +00:00
};
struct IndexedFile {
2017-02-28 06:41:42 +00:00
IdCache id_cache;
2017-02-25 23:59:09 +00:00
static constexpr int kCurrentVersion = 1;
int version = 0;
2017-02-27 07:23:43 +00:00
std::string path;
int64_t last_modification_time = 0;
2017-04-20 07:25:38 +00:00
// The path to the translation unit cc file which caused the creation of this
// IndexedFile. When parsing a translation unit we generate many IndexedFile
// instances (ie, each header has a separate one). When the user edits a
// header we need to lookup the original translation unit and reindex that.
std::string import_file;
// The content of |path| when it was indexed.
//std::string content;
2017-02-27 07:23:43 +00:00
std::vector<std::string> dependencies;
2017-02-23 08:18:54 +00:00
std::vector<IndexedTypeDef> types;
std::vector<IndexedFuncDef> funcs;
std::vector<IndexedVarDef> vars;
2017-02-22 08:52:00 +00:00
2017-02-28 06:41:42 +00:00
IndexedFile(const std::string& path);
2017-02-22 08:52:00 +00:00
IndexTypeId ToTypeId(const std::string& usr);
IndexFuncId ToFuncId(const std::string& usr);
IndexVarId ToVarId(const std::string& usr);
IndexTypeId ToTypeId(const CXCursor& usr);
IndexFuncId ToFuncId(const CXCursor& usr);
IndexVarId ToVarId(const CXCursor& usr);
IndexedTypeDef* Resolve(IndexTypeId id);
IndexedFuncDef* Resolve(IndexFuncId id);
IndexedVarDef* Resolve(IndexVarId id);
2017-02-22 08:52:00 +00:00
std::string ToString();
};
2017-04-20 07:25:38 +00:00
// |import_file| is the cc file which is what gets passed to clang.
// |desired_index_file| is the (h or cc) file which has actually changed.
// |dependencies| are the existing dependencies of |import_file| if this is a reparse.
2017-04-20 07:25:38 +00:00
std::vector<std::unique_ptr<IndexedFile>> Parse(
IndexerConfig* config, FileConsumer::SharedState* file_consumer_shared,
std::string desired_index_file, std::string import_file, const std::vector<std::string>& existing_dependencies,
std::vector<std::string> args,
2017-04-20 07:25:38 +00:00
bool dump_ast = false);
2017-04-17 20:40:50 +00:00
void IndexInit();