2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2017-02-23 09:23:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-26 08:11:47 +00:00
|
|
|
#include "indexer.h"
|
2017-03-07 09:32:29 +00:00
|
|
|
#include "serializer.h"
|
2017-02-26 08:11:47 +00:00
|
|
|
|
2018-05-30 06:56:14 +00:00
|
|
|
#include <llvm/ADT/DenseMap.h>
|
2018-05-13 20:30:24 +00:00
|
|
|
#include <llvm/ADT/SmallVector.h>
|
2017-04-17 20:40:50 +00:00
|
|
|
|
2018-09-03 02:11:10 +00:00
|
|
|
namespace llvm {
|
|
|
|
template <> struct DenseMapInfo<SymbolRef> {
|
|
|
|
static inline SymbolRef getEmptyKey() { return {}; }
|
|
|
|
static inline SymbolRef getTombstoneKey() {
|
|
|
|
SymbolRef ret{};
|
|
|
|
ret.usr = -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(SymbolRef sym) {
|
|
|
|
return std::hash<SymbolRef>()(sym);
|
|
|
|
}
|
|
|
|
static bool isEqual(SymbolRef l, SymbolRef r) { return l == r; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-15 05:41:35 +00:00
|
|
|
struct QueryFile {
|
2017-04-07 05:42:57 +00:00
|
|
|
struct Def {
|
2017-04-15 05:41:35 +00:00
|
|
|
std::string path;
|
2018-02-15 06:41:07 +00:00
|
|
|
std::vector<std::string> args;
|
2018-05-05 03:40:52 +00:00
|
|
|
LanguageId language;
|
2017-05-21 03:46:15 +00:00
|
|
|
// Includes in the file.
|
|
|
|
std::vector<IndexInclude> includes;
|
2017-10-28 22:09:14 +00:00
|
|
|
// Parts of the file which are disabled.
|
2018-07-06 00:53:33 +00:00
|
|
|
std::vector<Range> skipped_ranges;
|
2018-03-31 03:16:33 +00:00
|
|
|
// Used by |$ccls/freshenIndex|.
|
2018-01-27 17:29:28 +00:00
|
|
|
std::vector<std::string> dependencies;
|
2017-04-07 05:42:57 +00:00
|
|
|
};
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-06-01 03:06:09 +00:00
|
|
|
using DefUpdate = std::pair<Def, std::string>;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
int id = -1;
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<Def> def;
|
2018-09-03 02:11:10 +00:00
|
|
|
llvm::DenseMap<SymbolRef, int> symbol2refcnt;
|
|
|
|
llvm::DenseMap<SymbolRef, int> outline2refcnt;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
template <typename Q, typename QDef> struct QueryEntity {
|
2018-02-22 07:23:39 +00:00
|
|
|
using Def = QDef;
|
2018-08-09 17:08:14 +00:00
|
|
|
Def *AnyDef() {
|
|
|
|
Def *ret = nullptr;
|
|
|
|
for (auto &i : static_cast<Q *>(this)->def) {
|
2018-02-22 06:34:34 +00:00
|
|
|
ret = &i;
|
|
|
|
if (i.spell)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
const Def *AnyDef() const {
|
|
|
|
return const_cast<QueryEntity *>(this)->AnyDef();
|
|
|
|
}
|
2018-02-22 06:34:34 +00:00
|
|
|
};
|
|
|
|
|
2018-09-08 05:29:36 +00:00
|
|
|
using DeclRefUpdate =
|
|
|
|
std::unordered_map<Usr,
|
|
|
|
std::pair<std::vector<DeclRef>, std::vector<DeclRef>>>;
|
2018-05-04 04:20:10 +00:00
|
|
|
using UseUpdate =
|
|
|
|
std::unordered_map<Usr, std::pair<std::vector<Use>, std::vector<Use>>>;
|
|
|
|
using UsrUpdate =
|
|
|
|
std::unordered_map<Usr, std::pair<std::vector<Usr>, std::vector<Usr>>>;
|
2017-02-27 02:03:14 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryFunc : QueryEntity<QueryFunc, FuncDef> {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2018-05-13 20:30:24 +00:00
|
|
|
llvm::SmallVector<Def, 1> def;
|
2018-09-08 05:29:36 +00:00
|
|
|
std::vector<DeclRef> declarations;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> uses;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::vector<Usr> derived;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryType : QueryEntity<QueryType, TypeDef> {
|
2018-01-13 08:10:39 +00:00
|
|
|
Usr usr;
|
2018-05-13 20:30:24 +00:00
|
|
|
llvm::SmallVector<Def, 1> def;
|
2018-09-08 05:29:36 +00:00
|
|
|
std::vector<DeclRef> declarations;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> uses;
|
2018-04-30 04:49:03 +00:00
|
|
|
std::vector<Usr> derived;
|
|
|
|
std::vector<Usr> instances;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
struct QueryVar : QueryEntity<QueryVar, VarDef> {
|
2017-12-12 07:56:50 +00:00
|
|
|
Usr usr;
|
2018-05-13 20:30:24 +00:00
|
|
|
llvm::SmallVector<Def, 1> def;
|
2018-09-08 05:29:36 +00:00
|
|
|
std::vector<DeclRef> declarations;
|
2018-02-10 20:53:18 +00:00
|
|
|
std::vector<Use> uses;
|
2017-02-26 08:11:47 +00:00
|
|
|
};
|
2017-03-14 08:33:39 +00:00
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
struct IndexUpdate {
|
2017-04-08 06:45:28 +00:00
|
|
|
// Creates a new IndexUpdate based on the delta from previous to current. If
|
|
|
|
// no delta computation should be done just pass null for previous.
|
2018-08-09 17:08:14 +00:00
|
|
|
static IndexUpdate CreateDelta(IndexFile *previous, IndexFile *current);
|
2017-04-07 05:42:57 +00:00
|
|
|
|
2018-05-08 15:56:20 +00:00
|
|
|
int file_id;
|
|
|
|
|
|
|
|
// Dummy one to refresh all semantic highlight.
|
|
|
|
bool refresh = false;
|
2018-04-30 04:49:03 +00:00
|
|
|
|
2018-07-13 02:13:01 +00:00
|
|
|
decltype(IndexFile::lid2path) prev_lid2path;
|
|
|
|
decltype(IndexFile::lid2path) lid2path;
|
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
// File updates.
|
2018-04-30 04:49:03 +00:00
|
|
|
std::optional<std::string> files_removed;
|
|
|
|
std::optional<QueryFile::DefUpdate> files_def_update;
|
|
|
|
|
|
|
|
// Function updates.
|
2018-05-30 06:56:14 +00:00
|
|
|
int funcs_hint;
|
2018-09-03 02:11:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryFunc::Def>> funcs_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryFunc::Def>> funcs_def_update;
|
2018-09-08 05:29:36 +00:00
|
|
|
DeclRefUpdate funcs_declarations;
|
2018-05-04 04:20:10 +00:00
|
|
|
UseUpdate funcs_uses;
|
|
|
|
UsrUpdate funcs_derived;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Type updates.
|
2018-05-30 06:56:14 +00:00
|
|
|
int types_hint;
|
2018-09-03 02:11:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryType::Def>> types_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryType::Def>> types_def_update;
|
2018-09-08 05:29:36 +00:00
|
|
|
DeclRefUpdate types_declarations;
|
2018-05-04 04:20:10 +00:00
|
|
|
UseUpdate types_uses;
|
|
|
|
UsrUpdate types_derived;
|
|
|
|
UsrUpdate types_instances;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
|
|
|
// Variable updates.
|
2018-05-30 06:56:14 +00:00
|
|
|
int vars_hint;
|
2018-09-03 02:11:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryVar::Def>> vars_removed;
|
2018-05-04 04:20:10 +00:00
|
|
|
std::vector<std::pair<Usr, QueryVar::Def>> vars_def_update;
|
2018-09-08 05:29:36 +00:00
|
|
|
DeclRefUpdate vars_declarations;
|
2018-05-04 04:20:10 +00:00
|
|
|
UseUpdate vars_uses;
|
2017-02-27 07:23:43 +00:00
|
|
|
};
|
|
|
|
|
2018-09-11 23:42:04 +00:00
|
|
|
struct DenseMapInfoForUsr {
|
|
|
|
static inline Usr getEmptyKey() { return 0; }
|
|
|
|
static inline Usr getTombstoneKey() { return ~0ULL; }
|
|
|
|
static unsigned getHashValue(Usr w) { return w; }
|
|
|
|
static bool isEqual(Usr l, Usr r) { return l == r; }
|
2018-05-30 06:56:14 +00:00
|
|
|
};
|
|
|
|
|
2018-07-13 02:13:01 +00:00
|
|
|
using Lid2file_id = std::unordered_map<int, int>;
|
|
|
|
|
2017-02-27 07:23:43 +00:00
|
|
|
// The query database is heavily optimized for fast queries. It is stored
|
|
|
|
// in-memory.
|
2018-05-30 06:56:14 +00:00
|
|
|
struct DB {
|
2017-10-17 05:44:58 +00:00
|
|
|
std::vector<QueryFile> files;
|
2018-05-15 05:13:18 +00:00
|
|
|
llvm::StringMap<int> name2file_id;
|
2018-09-11 23:42:04 +00:00
|
|
|
llvm::DenseMap<Usr, int, DenseMapInfoForUsr> func_usr, type_usr, var_usr;
|
2018-05-30 06:56:14 +00:00
|
|
|
std::vector<QueryFunc> funcs;
|
|
|
|
std::vector<QueryType> types;
|
|
|
|
std::vector<QueryVar> vars;
|
2017-02-27 07:23:43 +00:00
|
|
|
|
2018-09-03 02:11:10 +00:00
|
|
|
template <typename Def>
|
2018-08-09 17:08:14 +00:00
|
|
|
void RemoveUsrs(SymbolKind kind, int file_id,
|
2018-09-03 02:11:10 +00:00
|
|
|
const std::vector<std::pair<Usr, Def>> &to_remove);
|
2017-02-27 07:23:43 +00:00
|
|
|
// Insert the contents of |update| into |db|.
|
2018-08-09 17:08:14 +00:00
|
|
|
void ApplyIndexUpdate(IndexUpdate *update);
|
|
|
|
int GetFileId(const std::string &path);
|
|
|
|
int Update(QueryFile::DefUpdate &&u);
|
2018-07-13 02:13:01 +00:00
|
|
|
void Update(const Lid2file_id &, int file_id,
|
|
|
|
std::vector<std::pair<Usr, QueryType::Def>> &&us);
|
|
|
|
void Update(const Lid2file_id &, int file_id,
|
|
|
|
std::vector<std::pair<Usr, QueryFunc::Def>> &&us);
|
|
|
|
void Update(const Lid2file_id &, int file_id,
|
|
|
|
std::vector<std::pair<Usr, QueryVar::Def>> &&us);
|
2018-05-30 06:56:14 +00:00
|
|
|
std::string_view GetSymbolName(SymbolIdx sym, bool qualified);
|
2017-04-07 08:01:58 +00:00
|
|
|
|
2018-09-11 23:42:04 +00:00
|
|
|
bool HasFunc(Usr usr) const { return func_usr.count(usr); }
|
|
|
|
bool HasType(Usr usr) const { return type_usr.count(usr); }
|
|
|
|
bool HasVar(Usr usr) const { return var_usr.count(usr); }
|
2017-04-08 07:52:57 +00:00
|
|
|
|
2018-09-11 23:42:04 +00:00
|
|
|
QueryFunc &Func(Usr usr) { return funcs[func_usr[usr]]; }
|
|
|
|
QueryType &Type(Usr usr) { return types[type_usr[usr]]; }
|
|
|
|
QueryVar &Var(Usr usr) { return vars[var_usr[usr]]; }
|
2018-05-30 06:56:14 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
QueryFile &GetFile(SymbolIdx ref) { return files[ref.usr]; }
|
|
|
|
QueryFunc &GetFunc(SymbolIdx ref) { return Func(ref.usr); }
|
|
|
|
QueryType &GetType(SymbolIdx ref) { return Type(ref.usr); }
|
|
|
|
QueryVar &GetVar(SymbolIdx ref) { return Var(ref.usr); }
|
2017-04-07 08:01:58 +00:00
|
|
|
};
|