2017-02-22 08:52:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-01-13 06:13:08 +00:00
|
|
|
#include "clang_cursor.h"
|
2017-11-11 19:41:09 +00:00
|
|
|
#include "clang_index.h"
|
2018-01-17 01:48:22 +00:00
|
|
|
#include "clang_symbol_kind.h"
|
2017-11-11 19:41:09 +00:00
|
|
|
#include "clang_translation_unit.h"
|
2017-11-04 22:29:03 +00:00
|
|
|
#include "clang_utils.h"
|
2017-04-08 22:54:36 +00:00
|
|
|
#include "file_consumer.h"
|
2018-01-11 05:16:46 +00:00
|
|
|
#include "file_contents.h"
|
2017-04-18 04:06:01 +00:00
|
|
|
#include "language_server_api.h"
|
2018-02-02 05:31:56 +00:00
|
|
|
#include "maybe.h"
|
2017-05-17 07:08:45 +00:00
|
|
|
#include "performance.h"
|
2017-09-22 01:14:57 +00:00
|
|
|
#include "position.h"
|
|
|
|
#include "serializer.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2017-03-25 20:32:44 +00:00
|
|
|
#include <optional.h>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <rapidjson/document.h>
|
2017-02-22 08:52:00 +00:00
|
|
|
#include <rapidjson/prettywriter.h>
|
|
|
|
#include <rapidjson/stringbuffer.h>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <rapidjson/writer.h>
|
2018-01-31 04:59:31 +00:00
|
|
|
#include <string_view.h>
|
2017-09-22 01:14:57 +00:00
|
|
|
|
2017-03-25 20:32:44 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <cstdint>
|
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
|
|
|
|
2018-02-08 03:14:44 +00:00
|
|
|
struct IndexFile;
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexType;
|
|
|
|
struct IndexFunc;
|
|
|
|
struct IndexVar;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
// The order matters. In FindSymbolsAtLocation, we want Var/Func ordered in
|
|
|
|
// front of others.
|
|
|
|
enum class SymbolKind : uint8_t { Invalid, File, Type, Func, Var };
|
2018-01-30 00:35:01 +00:00
|
|
|
MAKE_REFLECT_TYPE_PROXY(SymbolKind);
|
2018-01-28 04:25:14 +00:00
|
|
|
|
2018-02-10 02:56:14 +00:00
|
|
|
// FIXME: Make old compiler happy.
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<::SymbolKind> {
|
|
|
|
size_t operator()(const ::SymbolKind& instance) const {
|
|
|
|
using type = std::underlying_type<::SymbolKind>::type;
|
|
|
|
return std::hash<type>()(static_cast<type>(instance));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
using RawId = uint32_t;
|
|
|
|
|
2017-03-17 07:58:41 +00:00
|
|
|
template <typename T>
|
2017-02-25 23:59:09 +00:00
|
|
|
struct Id {
|
2018-02-03 18:33:22 +00:00
|
|
|
RawId id;
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-02-02 07:10:37 +00:00
|
|
|
// Invalid id.
|
|
|
|
Id() : id(-1) {}
|
2018-02-03 18:33:22 +00:00
|
|
|
explicit Id(RawId id) : id(id) {}
|
2018-02-02 06:47:22 +00:00
|
|
|
template <typename U>
|
|
|
|
explicit Id(Id<U> o) : id(o.id) {}
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2017-04-15 05:55:01 +00:00
|
|
|
// Needed for google::dense_hash_map.
|
2018-02-03 18:33:22 +00:00
|
|
|
explicit operator RawId() const { return id; }
|
2017-04-15 05:55:01 +00:00
|
|
|
|
2018-02-03 18:33:22 +00:00
|
|
|
bool HasValue() const { return id != RawId(-1); }
|
2018-02-02 06:31:43 +00:00
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
bool operator==(const Id& o) const { return id == o.id; }
|
|
|
|
bool operator!=(const Id& o) const { return id != o.id; }
|
|
|
|
bool operator<(const Id& o) const { return id < o.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>> {
|
2018-02-08 03:14:44 +00:00
|
|
|
size_t operator()(const Id<T>& k) const { return hash<RawId>()(k.id); }
|
2017-03-17 07:58:41 +00:00
|
|
|
};
|
2017-09-22 01:14:57 +00:00
|
|
|
} // namespace std
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-02-04 18:03:24 +00:00
|
|
|
template <typename TVisitor, typename T>
|
|
|
|
void Reflect(TVisitor& visitor, Id<T>& id) {
|
|
|
|
Reflect(visitor, id.id);
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:14:44 +00:00
|
|
|
using IndexFileId = Id<IndexFile>;
|
2017-05-12 06:08:15 +00:00
|
|
|
using IndexTypeId = Id<IndexType>;
|
|
|
|
using IndexFuncId = Id<IndexFunc>;
|
|
|
|
using IndexVarId = Id<IndexVar>;
|
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
|
|
|
|
2018-02-08 18:38:27 +00:00
|
|
|
struct Reference {
|
|
|
|
Range range;
|
2018-02-09 17:42:10 +00:00
|
|
|
Id<void> id;
|
|
|
|
SymbolKind kind;
|
2018-02-08 18:38:27 +00:00
|
|
|
SymbolRole role;
|
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
bool HasValue() const { return id.HasValue(); }
|
2018-02-08 18:38:27 +00:00
|
|
|
std::tuple<Range, Id<void>, SymbolKind, SymbolRole> ToTuple() const {
|
2018-02-09 17:42:10 +00:00
|
|
|
return std::make_tuple(range, id, kind, role);
|
2018-02-08 18:38:27 +00:00
|
|
|
}
|
|
|
|
bool operator==(const Reference& o) const {
|
|
|
|
return ToTuple() == o.ToTuple();
|
|
|
|
}
|
|
|
|
bool operator<(const Reference& o) const {
|
|
|
|
return ToTuple() < o.ToTuple();
|
|
|
|
}
|
2018-02-08 02:29:34 +00:00
|
|
|
};
|
|
|
|
|
2018-02-09 06:06:09 +00:00
|
|
|
// NOTE: id can be -1 if the function call is not coming from a function.
|
|
|
|
struct IndexFuncRef : Reference {
|
|
|
|
// Constructors are unnecessary in C++17 p0017
|
|
|
|
IndexFuncRef() = default;
|
|
|
|
IndexFuncRef(Range range, Id<void> id, SymbolKind kind, SymbolRole role)
|
|
|
|
: Reference{range, id, kind, role} {}
|
2017-02-22 08:52:00 +00:00
|
|
|
};
|
|
|
|
|
2018-02-05 01:35:16 +00:00
|
|
|
void Reflect(Reader& visitor, IndexFuncRef& value);
|
|
|
|
void Reflect(Writer& visitor, IndexFuncRef& value);
|
2018-02-08 18:38:27 +00:00
|
|
|
void Reflect(Reader& visitor, Reference& value);
|
|
|
|
void Reflect(Writer& visitor, Reference& value);
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
struct IndexFamily {
|
|
|
|
using FileId = Id<IndexFile>;
|
|
|
|
using FuncId = Id<IndexFunc>;
|
|
|
|
using TypeId = Id<IndexType>;
|
|
|
|
using VarId = Id<IndexVar>;
|
2018-02-10 11:50:39 +00:00
|
|
|
using Range = ::Range;
|
2018-02-10 08:06:45 +00:00
|
|
|
using FuncRef = IndexFuncRef;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename F>
|
2017-02-25 06:08:14 +00:00
|
|
|
struct TypeDefDefinitionData {
|
2017-02-22 08:52:00 +00:00
|
|
|
// General metadata.
|
2017-04-15 04:58:07 +00:00
|
|
|
std::string detailed_name;
|
2018-01-31 07:48:34 +00:00
|
|
|
std::string hover;
|
|
|
|
std::string comments;
|
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.
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::Range> definition_spelling;
|
|
|
|
Maybe<typename F::Range> definition_extent;
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-02-25 06:08:14 +00:00
|
|
|
// Immediate parent types.
|
2018-02-10 08:06:45 +00:00
|
|
|
std::vector<typename F::TypeId> parents;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
|
|
|
// Types, functions, and variables defined in this type.
|
2018-02-10 08:06:45 +00:00
|
|
|
std::vector<typename F::TypeId> types;
|
|
|
|
std::vector<typename F::FuncId> funcs;
|
|
|
|
std::vector<typename F::VarId> vars;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
typename F::FileId file;
|
2018-02-08 03:14:44 +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).
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::TypeId> alias_of;
|
2018-02-08 03:14:44 +00:00
|
|
|
|
2018-01-31 07:01:45 +00:00
|
|
|
int16_t short_name_offset = 0;
|
|
|
|
int16_t short_name_size = 0;
|
2018-01-31 07:48:34 +00:00
|
|
|
ClangSymbolKind kind = ClangSymbolKind::Unknown;
|
2018-01-31 07:01:45 +00:00
|
|
|
|
2018-02-04 18:03:24 +00:00
|
|
|
bool operator==(const TypeDefDefinitionData& o) const {
|
|
|
|
return detailed_name == o.detailed_name &&
|
|
|
|
definition_spelling == o.definition_spelling &&
|
|
|
|
definition_extent == o.definition_extent && alias_of == o.alias_of &&
|
|
|
|
parents == o.parents && types == o.types && funcs == o.funcs &&
|
|
|
|
vars == o.vars && kind == o.kind && hover == o.hover &&
|
2018-02-10 08:06:45 +00:00
|
|
|
comments == o.comments;
|
2017-02-25 23:59:09 +00:00
|
|
|
}
|
2018-02-04 18:03:24 +00:00
|
|
|
bool operator!=(const TypeDefDefinitionData& o) const {
|
|
|
|
return !(*this == o);
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2018-01-31 07:01:45 +00:00
|
|
|
|
|
|
|
std::string_view ShortName() const {
|
|
|
|
return std::string_view(detailed_name.c_str() + short_name_offset,
|
|
|
|
short_name_size);
|
|
|
|
}
|
2017-02-25 06:08:14 +00:00
|
|
|
};
|
2018-02-10 08:06:45 +00:00
|
|
|
template <typename TVisitor, typename Family>
|
|
|
|
void Reflect(TVisitor& visitor, TypeDefDefinitionData<Family>& value) {
|
2018-01-08 04:10:16 +00:00
|
|
|
REFLECT_MEMBER_START();
|
2017-04-15 04:58:07 +00:00
|
|
|
REFLECT_MEMBER(detailed_name);
|
2018-01-31 07:01:45 +00:00
|
|
|
REFLECT_MEMBER(short_name_offset);
|
|
|
|
REFLECT_MEMBER(short_name_size);
|
2018-01-06 06:33:31 +00:00
|
|
|
REFLECT_MEMBER(kind);
|
2017-12-19 05:20:00 +00:00
|
|
|
REFLECT_MEMBER(hover);
|
2017-12-24 02:22:22 +00:00
|
|
|
REFLECT_MEMBER(comments);
|
2017-05-19 07:02:01 +00:00
|
|
|
REFLECT_MEMBER(definition_spelling);
|
|
|
|
REFLECT_MEMBER(definition_extent);
|
2018-02-08 03:14:44 +00:00
|
|
|
REFLECT_MEMBER(file);
|
2017-03-14 08:33:39 +00:00
|
|
|
REFLECT_MEMBER(alias_of);
|
|
|
|
REFLECT_MEMBER(parents);
|
|
|
|
REFLECT_MEMBER(types);
|
|
|
|
REFLECT_MEMBER(funcs);
|
|
|
|
REFLECT_MEMBER(vars);
|
|
|
|
REFLECT_MEMBER_END();
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexType {
|
2018-02-10 08:06:45 +00:00
|
|
|
using Def = TypeDefDefinitionData<IndexFamily>;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId id;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
Def def;
|
|
|
|
|
2017-02-25 06:08:14 +00:00
|
|
|
// Immediate derived types.
|
2017-04-07 06:57:26 +00:00
|
|
|
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.
|
2017-04-21 07:03:33 +00:00
|
|
|
std::vector<IndexVarId> instances;
|
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.
|
2018-02-08 18:38:27 +00:00
|
|
|
std::vector<Reference> uses;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
IndexType() {} // For serialization.
|
2018-01-13 08:10:39 +00:00
|
|
|
IndexType(IndexTypeId id, Usr usr);
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
bool operator<(const IndexType& other) const { return id < other.id; }
|
2017-02-22 08:52:00 +00:00
|
|
|
};
|
2017-12-12 07:56:50 +00:00
|
|
|
MAKE_HASHABLE(IndexType, t.id);
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
template <typename F>
|
2017-02-25 06:08:14 +00:00
|
|
|
struct FuncDefDefinitionData {
|
2017-02-22 08:52:00 +00:00
|
|
|
// General metadata.
|
2017-04-15 04:58:07 +00:00
|
|
|
std::string detailed_name;
|
2018-01-31 07:48:34 +00:00
|
|
|
std::string hover;
|
|
|
|
std::string comments;
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::Range> definition_spelling;
|
|
|
|
Maybe<typename F::Range> definition_extent;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
|
|
|
// Method this method overrides.
|
2018-02-10 08:06:45 +00:00
|
|
|
std::vector<typename F::FuncId> base;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
|
|
|
// Local variables defined in this function.
|
2018-02-10 08:06:45 +00:00
|
|
|
std::vector<typename F::VarId> locals;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2017-02-25 06:08:14 +00:00
|
|
|
// Functions that this function calls.
|
2018-02-10 08:06:45 +00:00
|
|
|
std::vector<typename F::FuncRef> callees;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
typename F::FileId file;
|
2018-02-08 03:14:44 +00:00
|
|
|
// Type which declares this one (ie, it is a method)
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::TypeId> declaring_type;
|
2018-01-31 07:01:45 +00:00
|
|
|
int16_t short_name_offset = 0;
|
|
|
|
int16_t short_name_size = 0;
|
2018-01-31 06:39:39 +00:00
|
|
|
ClangSymbolKind kind = ClangSymbolKind::Unknown;
|
|
|
|
StorageClass storage = StorageClass::Invalid;
|
|
|
|
|
2018-02-04 18:03:24 +00:00
|
|
|
bool operator==(const FuncDefDefinitionData& o) const {
|
|
|
|
return detailed_name == o.detailed_name &&
|
|
|
|
definition_spelling == o.definition_spelling &&
|
|
|
|
definition_extent == o.definition_extent &&
|
|
|
|
declaring_type == o.declaring_type && base == o.base &&
|
|
|
|
locals == o.locals && callees == o.callees && kind == o.kind &&
|
|
|
|
storage == o.storage && hover == o.hover && comments == o.comments;
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2018-02-04 18:03:24 +00:00
|
|
|
bool operator!=(const FuncDefDefinitionData& o) const {
|
|
|
|
return !(*this == o);
|
2017-02-25 23:59:09 +00:00
|
|
|
}
|
2018-01-31 06:39:39 +00:00
|
|
|
|
|
|
|
std::string_view ShortName() const {
|
|
|
|
return std::string_view(detailed_name.c_str() + short_name_offset,
|
|
|
|
short_name_size);
|
|
|
|
}
|
2017-02-25 06:08:14 +00:00
|
|
|
};
|
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
template <typename TVisitor, typename Family>
|
|
|
|
void Reflect(TVisitor& visitor, FuncDefDefinitionData<Family>& value) {
|
2018-01-08 04:10:16 +00:00
|
|
|
REFLECT_MEMBER_START();
|
2017-04-15 04:58:07 +00:00
|
|
|
REFLECT_MEMBER(detailed_name);
|
2018-01-31 06:39:39 +00:00
|
|
|
REFLECT_MEMBER(short_name_offset);
|
|
|
|
REFLECT_MEMBER(short_name_size);
|
2018-01-06 06:33:31 +00:00
|
|
|
REFLECT_MEMBER(kind);
|
2018-01-26 17:28:29 +00:00
|
|
|
REFLECT_MEMBER(storage);
|
2017-12-19 05:20:00 +00:00
|
|
|
REFLECT_MEMBER(hover);
|
2017-12-24 02:22:22 +00:00
|
|
|
REFLECT_MEMBER(comments);
|
2017-05-19 07:02:01 +00:00
|
|
|
REFLECT_MEMBER(definition_spelling);
|
|
|
|
REFLECT_MEMBER(definition_extent);
|
2018-02-08 03:14:44 +00:00
|
|
|
REFLECT_MEMBER(file);
|
2017-03-14 08:33:39 +00:00
|
|
|
REFLECT_MEMBER(declaring_type);
|
|
|
|
REFLECT_MEMBER(base);
|
|
|
|
REFLECT_MEMBER(locals);
|
|
|
|
REFLECT_MEMBER(callees);
|
|
|
|
REFLECT_MEMBER_END();
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexFunc {
|
2018-02-10 08:06:45 +00:00
|
|
|
using Def = FuncDefDefinitionData<IndexFamily>;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexFuncId id;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
Def def;
|
|
|
|
|
2017-05-27 21:09:20 +00:00
|
|
|
struct Declaration {
|
2017-05-28 01:53:22 +00:00
|
|
|
// Range of only the function name.
|
2017-05-27 21:09:20 +00:00
|
|
|
Range spelling;
|
2017-05-28 01:53:22 +00:00
|
|
|
// Full range of the declaration.
|
|
|
|
Range extent;
|
|
|
|
// Full text of the declaration.
|
|
|
|
std::string content;
|
|
|
|
// Location of the parameter names.
|
|
|
|
std::vector<Range> param_spellings;
|
2017-05-27 21:09:20 +00:00
|
|
|
};
|
|
|
|
|
2017-02-25 06:08:14 +00:00
|
|
|
// Places the function is forward-declared.
|
2017-05-27 21:09:20 +00:00
|
|
|
std::vector<Declaration> declarations;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
|
|
|
// Methods which directly override this one.
|
2017-04-07 06:57:26 +00:00
|
|
|
std::vector<IndexFuncId> derived;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
2017-04-13 07:47:47 +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-12-12 07:56:50 +00:00
|
|
|
IndexFunc() {} // For serialization.
|
2018-01-13 08:10:39 +00:00
|
|
|
IndexFunc(IndexFuncId id, Usr usr) : usr(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-12-12 07:56:50 +00:00
|
|
|
bool operator<(const IndexFunc& other) const { return id < other.id; }
|
2017-02-22 08:52:00 +00:00
|
|
|
};
|
2017-12-12 07:56:50 +00:00
|
|
|
MAKE_HASHABLE(IndexFunc, t.id);
|
2017-09-22 01:14:57 +00:00
|
|
|
MAKE_REFLECT_STRUCT(IndexFunc::Declaration,
|
|
|
|
spelling,
|
|
|
|
extent,
|
|
|
|
content,
|
|
|
|
param_spellings);
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
template <typename F>
|
2017-02-25 06:08:14 +00:00
|
|
|
struct VarDefDefinitionData {
|
2017-02-22 08:52:00 +00:00
|
|
|
// General metadata.
|
2017-04-15 04:58:07 +00:00
|
|
|
std::string detailed_name;
|
2018-01-31 07:48:34 +00:00
|
|
|
std::string hover;
|
|
|
|
std::string comments;
|
2017-04-05 08:06:18 +00:00
|
|
|
// TODO: definitions should be a list of ranges, since there can be more
|
|
|
|
// than one - when??
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::Range> definition_spelling;
|
|
|
|
Maybe<typename F::Range> definition_extent;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
typename F::FileId file;
|
2017-02-22 08:52:00 +00:00
|
|
|
// Type of the variable.
|
2018-02-10 08:06:45 +00:00
|
|
|
Maybe<typename F::TypeId> variable_type;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
// Function/type which declares this one.
|
2018-02-03 18:33:22 +00:00
|
|
|
Maybe<Id<void>> parent_id;
|
2018-01-31 07:01:45 +00:00
|
|
|
int16_t short_name_offset = 0;
|
|
|
|
int16_t short_name_size = 0;
|
2018-01-28 20:34:31 +00:00
|
|
|
SymbolKind parent_kind = SymbolKind::Invalid;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
ClangSymbolKind kind = ClangSymbolKind::Unknown;
|
|
|
|
// Note a variable may have instances of both |None| and |Extern|
|
|
|
|
// (declaration).
|
|
|
|
StorageClass storage = StorageClass::Invalid;
|
2018-01-15 18:08:03 +00:00
|
|
|
|
2018-01-28 04:25:14 +00:00
|
|
|
bool is_local() const {
|
|
|
|
return kind == ClangSymbolKind::Parameter ||
|
|
|
|
kind == ClangSymbolKind::Variable;
|
|
|
|
}
|
2018-01-06 06:33:31 +00:00
|
|
|
bool is_macro() const { return kind == ClangSymbolKind::Macro; }
|
2017-05-21 01:26:50 +00:00
|
|
|
|
2018-02-04 18:03:24 +00:00
|
|
|
bool operator==(const VarDefDefinitionData& o) const {
|
|
|
|
return detailed_name == o.detailed_name &&
|
|
|
|
definition_spelling == o.definition_spelling &&
|
|
|
|
definition_extent == o.definition_extent &&
|
|
|
|
variable_type == o.variable_type && parent_id == o.parent_id &&
|
|
|
|
parent_kind == o.parent_kind && kind == o.kind &&
|
|
|
|
storage == o.storage && hover == o.hover && comments == o.comments;
|
2017-03-17 07:58:41 +00:00
|
|
|
}
|
2018-02-08 03:14:44 +00:00
|
|
|
bool operator!=(const VarDefDefinitionData& other) const {
|
2017-03-17 07:58:41 +00:00
|
|
|
return !(*this == other);
|
2017-02-25 23:59:09 +00:00
|
|
|
}
|
2018-01-31 04:59:31 +00:00
|
|
|
|
|
|
|
std::string_view ShortName() const {
|
|
|
|
return std::string_view(detailed_name.c_str() + short_name_offset,
|
|
|
|
short_name_size);
|
|
|
|
}
|
2017-02-25 06:08:14 +00:00
|
|
|
};
|
|
|
|
|
2018-02-10 08:06:45 +00:00
|
|
|
template <typename TVisitor, typename Family>
|
|
|
|
void Reflect(TVisitor& visitor, VarDefDefinitionData<Family>& value) {
|
2018-01-08 04:10:16 +00:00
|
|
|
REFLECT_MEMBER_START();
|
2017-04-15 04:58:07 +00:00
|
|
|
REFLECT_MEMBER(detailed_name);
|
2018-01-31 04:59:31 +00:00
|
|
|
REFLECT_MEMBER(short_name_size);
|
|
|
|
REFLECT_MEMBER(short_name_offset);
|
2017-12-19 05:20:00 +00:00
|
|
|
REFLECT_MEMBER(hover);
|
2017-12-24 02:22:22 +00:00
|
|
|
REFLECT_MEMBER(comments);
|
2017-04-05 08:06:18 +00:00
|
|
|
REFLECT_MEMBER(definition_spelling);
|
|
|
|
REFLECT_MEMBER(definition_extent);
|
2018-02-08 03:14:44 +00:00
|
|
|
REFLECT_MEMBER(file);
|
2017-03-14 08:33:39 +00:00
|
|
|
REFLECT_MEMBER(variable_type);
|
2018-01-28 20:34:31 +00:00
|
|
|
REFLECT_MEMBER(parent_id);
|
|
|
|
REFLECT_MEMBER(parent_kind);
|
|
|
|
REFLECT_MEMBER(kind);
|
|
|
|
REFLECT_MEMBER(storage);
|
2017-03-14 08:33:39 +00:00
|
|
|
REFLECT_MEMBER_END();
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexVar {
|
2018-02-10 08:06:45 +00:00
|
|
|
using Def = VarDefDefinitionData<IndexFamily>;
|
2017-02-25 06:08:14 +00:00
|
|
|
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexVarId id;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
Def def;
|
|
|
|
|
2018-01-27 05:50:17 +00:00
|
|
|
std::vector<Range> declarations;
|
2017-02-22 08:52:00 +00:00
|
|
|
// Usages.
|
2018-02-09 05:11:35 +00:00
|
|
|
std::vector<Reference> uses;
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2017-12-12 07:56:50 +00:00
|
|
|
IndexVar() {} // For serialization.
|
2018-01-13 08:10:39 +00:00
|
|
|
IndexVar(IndexVarId id, Usr usr) : usr(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-12-12 07:56:50 +00:00
|
|
|
bool operator<(const IndexVar& other) const { return id < other.id; }
|
2017-02-22 08:52:00 +00:00
|
|
|
};
|
2017-12-12 07:56:50 +00:00
|
|
|
MAKE_HASHABLE(IndexVar, t.id);
|
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;
|
2018-01-13 08:10:39 +00:00
|
|
|
std::unordered_map<Usr, IndexTypeId> usr_to_type_id;
|
|
|
|
std::unordered_map<Usr, IndexFuncId> usr_to_func_id;
|
|
|
|
std::unordered_map<Usr, IndexVarId> usr_to_var_id;
|
|
|
|
std::unordered_map<IndexTypeId, Usr> type_id_to_usr;
|
|
|
|
std::unordered_map<IndexFuncId, Usr> func_id_to_usr;
|
|
|
|
std::unordered_map<IndexVarId, Usr> 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-02-25 23:59:09 +00:00
|
|
|
};
|
|
|
|
|
2017-05-21 03:46:15 +00:00
|
|
|
struct IndexInclude {
|
|
|
|
// Line that has the include directive. We don't have complete range
|
|
|
|
// information - a line is good enough for clicking.
|
|
|
|
int line = 0;
|
|
|
|
// Absolute path to the index.
|
|
|
|
std::string resolved_path;
|
|
|
|
};
|
|
|
|
|
2017-11-30 21:56:55 +00:00
|
|
|
// Used to identify the language at a file level. The ordering is important, as
|
|
|
|
// a file previously identified as `C`, will be changed to `Cpp` if it
|
|
|
|
// encounters a c++ declaration.
|
2017-12-01 17:50:39 +00:00
|
|
|
enum class LanguageId { Unknown = 0, C = 1, Cpp = 2, ObjC = 3 };
|
2018-01-30 00:35:01 +00:00
|
|
|
MAKE_REFLECT_TYPE_PROXY(LanguageId);
|
2017-11-30 21:56:55 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexFile {
|
2017-02-28 06:41:42 +00:00
|
|
|
IdCache id_cache;
|
2017-02-25 23:59:09 +00:00
|
|
|
|
2018-01-28 05:47:37 +00:00
|
|
|
// For both JSON and MessagePack cache files.
|
|
|
|
static const int kMajorVersion;
|
|
|
|
// For MessagePack cache files.
|
2018-01-28 03:21:35 +00:00
|
|
|
// JSON has good forward compatibility because field addition/deletion do not
|
|
|
|
// harm but currently no efforts have been made to make old MessagePack cache
|
|
|
|
// files accepted by newer cquery.
|
2018-01-28 05:47:37 +00:00
|
|
|
static const int kMinorVersion;
|
2017-04-20 06:02:24 +00:00
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
std::string path;
|
2017-04-21 04:06:15 +00:00
|
|
|
std::vector<std::string> args;
|
2017-04-20 04:57:44 +00:00
|
|
|
int64_t last_modification_time = 0;
|
2017-11-30 21:56:55 +00:00
|
|
|
LanguageId language = LanguageId::Unknown;
|
2017-04-20 04:57:44 +00:00
|
|
|
|
2017-04-20 07:25:38 +00:00
|
|
|
// The path to the translation unit cc file which caused the creation of this
|
2017-05-12 06:08:15 +00:00
|
|
|
// IndexFile. When parsing a translation unit we generate many IndexFile
|
2017-04-20 07:25:38 +00:00
|
|
|
// 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;
|
|
|
|
|
2017-05-20 21:45:46 +00:00
|
|
|
// Source ranges that were not processed.
|
|
|
|
std::vector<Range> skipped_by_preprocessor;
|
|
|
|
|
2017-05-21 03:46:15 +00:00
|
|
|
std::vector<IndexInclude> includes;
|
2017-04-11 05:26:27 +00:00
|
|
|
std::vector<std::string> dependencies;
|
2017-05-12 06:08:15 +00:00
|
|
|
std::vector<IndexType> types;
|
|
|
|
std::vector<IndexFunc> funcs;
|
|
|
|
std::vector<IndexVar> vars;
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2017-06-20 01:52:25 +00:00
|
|
|
// Diagnostics found when indexing this file. Not serialized.
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsDiagnostic> diagnostics_;
|
2017-07-30 04:24:02 +00:00
|
|
|
// File contents at the time of index. Not serialized.
|
2018-01-30 05:34:28 +00:00
|
|
|
std::string file_contents;
|
2017-06-20 01:52:25 +00:00
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
IndexFile(const std::string& path, const std::string& contents);
|
2017-02-22 08:52:00 +00:00
|
|
|
|
2018-01-13 08:10:39 +00:00
|
|
|
IndexTypeId ToTypeId(Usr usr);
|
|
|
|
IndexFuncId ToFuncId(Usr usr);
|
|
|
|
IndexVarId ToVarId(Usr usr);
|
2017-04-07 06:57:26 +00:00
|
|
|
IndexTypeId ToTypeId(const CXCursor& usr);
|
|
|
|
IndexFuncId ToFuncId(const CXCursor& usr);
|
|
|
|
IndexVarId ToVarId(const CXCursor& usr);
|
2017-05-12 06:08:15 +00:00
|
|
|
IndexType* Resolve(IndexTypeId id);
|
|
|
|
IndexFunc* Resolve(IndexFuncId id);
|
|
|
|
IndexVar* Resolve(IndexVarId id);
|
2017-02-22 08:52:00 +00:00
|
|
|
|
|
|
|
std::string ToString();
|
|
|
|
};
|
|
|
|
|
2017-12-29 09:19:02 +00:00
|
|
|
struct NamespaceHelper {
|
|
|
|
std::unordered_map<ClangCursor, std::string>
|
|
|
|
container_cursor_to_qualified_name;
|
|
|
|
|
|
|
|
std::string QualifiedName(const CXIdxContainerInfo* container,
|
2018-01-31 04:59:31 +00:00
|
|
|
std::string_view unqualified_name);
|
2017-12-29 09:19:02 +00:00
|
|
|
};
|
|
|
|
|
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.
|
2017-09-22 01:14:57 +00:00
|
|
|
// |dependencies| are the existing dependencies of |import_file| if this is a
|
|
|
|
// reparse.
|
2018-01-20 07:56:49 +00:00
|
|
|
optional<std::vector<std::unique_ptr<IndexFile>>> Parse(
|
2017-09-22 01:14:57 +00:00
|
|
|
Config* config,
|
2017-12-29 16:29:47 +00:00
|
|
|
FileConsumerSharedState* file_consumer_shared,
|
2017-04-24 01:01:51 +00:00
|
|
|
std::string file,
|
2017-09-27 06:03:43 +00:00
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<FileContents>& file_contents,
|
2017-05-17 07:08:45 +00:00
|
|
|
PerformanceImportFile* perf,
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangIndex* index,
|
2017-04-20 07:25:38 +00:00
|
|
|
bool dump_ast = false);
|
2018-01-20 07:56:49 +00:00
|
|
|
optional<std::vector<std::unique_ptr<IndexFile>>> ParseWithTu(
|
2018-02-01 05:48:39 +00:00
|
|
|
Config* config,
|
2017-12-29 16:29:47 +00:00
|
|
|
FileConsumerSharedState* file_consumer_shared,
|
2017-09-27 06:03:43 +00:00
|
|
|
PerformanceImportFile* perf,
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangTranslationUnit* tu,
|
|
|
|
ClangIndex* index,
|
2017-09-27 06:03:43 +00:00
|
|
|
const std::string& file,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<CXUnsavedFile>& file_contents);
|
|
|
|
|
2017-12-30 21:11:55 +00:00
|
|
|
void ConcatTypeAndName(std::string& type, const std::string& name);
|
|
|
|
|
2017-04-17 20:40:50 +00:00
|
|
|
void IndexInit();
|
2017-12-01 17:45:44 +00:00
|
|
|
|
|
|
|
void ClangSanityCheck();
|
2017-12-23 15:51:34 +00:00
|
|
|
|
2017-12-24 00:49:11 +00:00
|
|
|
std::string GetClangVersion();
|