2017-02-18 23:58:40 +00:00
|
|
|
#include <algorithm>
|
2017-02-17 09:57:44 +00:00
|
|
|
#include <optional>
|
2017-02-16 09:35:30 +00:00
|
|
|
#include <iostream>
|
2017-02-17 09:57:44 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cassert>
|
|
|
|
#include <fstream>
|
2017-02-18 19:37:24 +00:00
|
|
|
#include <unordered_map>
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
#include "libclangmm/clangmm.h"
|
|
|
|
#include "libclangmm/Utility.h"
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
#include <rapidjson/writer.h>
|
|
|
|
#include <rapidjson/prettywriter.h>
|
|
|
|
#include <rapidjson/stringbuffer.h>
|
|
|
|
#include <rapidjson/document.h>
|
|
|
|
|
2017-02-16 09:35:30 +00:00
|
|
|
//#include <clang-c\Index.h>
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
// While indexing, we should refer to symbols by USR. When joining into the db, we can have optimized access.
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
struct TypeDef;
|
|
|
|
struct FuncDef;
|
|
|
|
struct VarDef;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
/*
|
2017-02-17 09:57:44 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Id {
|
|
|
|
uint64_t file_id;
|
|
|
|
uint64_t local_id;
|
|
|
|
|
|
|
|
Id() : file_id(0), local_id(0) {} // Needed for containers. Do not use directly.
|
|
|
|
Id(uint64_t file_id, uint64_t local_id)
|
|
|
|
: file_id(file_id), local_id(local_id) {}
|
|
|
|
};
|
2017-02-18 19:37:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct LocalId {
|
|
|
|
uint64_t local_id;
|
|
|
|
|
|
|
|
LocalId() : local_id(0) {} // Needed for containers. Do not use directly.
|
|
|
|
explicit LocalId(uint64_t local_id) : local_id(local_id) {}
|
|
|
|
};
|
|
|
|
using TypeId = LocalId<TypeDef>;
|
|
|
|
using FuncId = LocalId<FuncDef>;
|
|
|
|
using VarId = LocalId<VarDef>;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Ref {
|
2017-02-18 19:37:24 +00:00
|
|
|
LocalId<T> id;
|
2017-02-17 09:57:44 +00:00
|
|
|
clang::SourceLocation loc;
|
2017-02-18 21:18:08 +00:00
|
|
|
|
|
|
|
Ref(LocalId<T> id, clang::SourceLocation loc) : id(id), loc(loc) {}
|
2017-02-17 09:57:44 +00:00
|
|
|
};
|
|
|
|
using TypeRef = Ref<TypeDef>;
|
|
|
|
using FuncRef = Ref<FuncDef>;
|
|
|
|
using VarRef = Ref<VarDef>;
|
|
|
|
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// NOTE: declaration is empty if there is no forward declaration!
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
struct TypeDef {
|
2017-02-17 09:57:44 +00:00
|
|
|
// General metadata.
|
|
|
|
TypeId id;
|
|
|
|
std::string usr;
|
2017-02-18 19:37:24 +00:00
|
|
|
std::string short_name;
|
|
|
|
std::string qualified_name;
|
2017-02-20 07:51:31 +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.
|
2017-02-17 09:57:44 +00:00
|
|
|
std::optional<clang::SourceLocation> definition;
|
|
|
|
|
2017-02-19 05:47:16 +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).
|
|
|
|
std::optional<TypeId> alias_of;
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
// Immediate parent and immediate derived types.
|
|
|
|
std::vector<TypeId> parents;
|
|
|
|
std::vector<TypeId> derived;
|
|
|
|
|
|
|
|
// Types, functions, and variables defined in this type.
|
|
|
|
std::vector<TypeId> types;
|
|
|
|
std::vector<FuncId> funcs;
|
|
|
|
std::vector<VarId> vars;
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
// Every usage, useful for things like renames.
|
|
|
|
std::vector<clang::SourceLocation> all_uses;
|
|
|
|
// Usages that a user is probably interested in.
|
|
|
|
std::vector<clang::SourceLocation> interesting_uses;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-19 00:53:31 +00:00
|
|
|
TypeDef(TypeId id, const std::string& usr) : id(id), usr(usr) {
|
2017-02-19 02:44:04 +00:00
|
|
|
assert(usr.size() > 0);
|
2017-02-19 02:34:51 +00:00
|
|
|
//std::cout << "Creating type with usr " << usr << std::endl;
|
2017-02-19 00:53:31 +00:00
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
};
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
struct FuncDef {
|
|
|
|
// General metadata.
|
|
|
|
FuncId id;
|
|
|
|
std::string usr;
|
2017-02-18 19:37:24 +00:00
|
|
|
std::string short_name;
|
|
|
|
std::string qualified_name;
|
2017-02-17 09:57:44 +00:00
|
|
|
std::optional<clang::SourceLocation> declaration;
|
|
|
|
std::optional<clang::SourceLocation> definition;
|
|
|
|
|
|
|
|
// Type which declares this one (ie, it is a method)
|
2017-02-18 19:37:24 +00:00
|
|
|
std::optional<TypeId> declaring_type;
|
2017-02-17 09:57:44 +00:00
|
|
|
// Method this method overrides.
|
2017-02-18 19:37:24 +00:00
|
|
|
std::optional<FuncId> base;
|
2017-02-17 09:57:44 +00:00
|
|
|
// Methods which directly override this one.
|
|
|
|
std::vector<FuncId> derived;
|
|
|
|
|
|
|
|
// Local variables defined in this function.
|
|
|
|
std::vector<VarId> locals;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
// Functions which call this one.
|
2017-02-20 00:56:56 +00:00
|
|
|
// TODO: Functions can get called outside of just functions - for example,
|
|
|
|
// they can get called in static context (maybe redirect to main?)
|
|
|
|
// or in class initializer list (redirect to class ctor?)
|
|
|
|
// - Right now those usages will not get listed here (but they should be
|
|
|
|
// inside of all_uses).
|
2017-02-17 09:57:44 +00:00
|
|
|
std::vector<FuncRef> callers;
|
|
|
|
// Functions that this function calls.
|
|
|
|
std::vector<FuncRef> callees;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
// All usages. For interesting usages, see callees.
|
|
|
|
std::vector<clang::SourceLocation> all_uses;
|
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
FuncDef(FuncId id, const std::string& usr) : id(id), usr(usr) {
|
|
|
|
assert(usr.size() > 0);
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
};
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
struct VarDef {
|
|
|
|
// General metadata.
|
|
|
|
VarId id;
|
|
|
|
std::string usr;
|
2017-02-18 19:37:24 +00:00
|
|
|
std::string short_name;
|
|
|
|
std::string qualified_name;
|
2017-02-17 09:57:44 +00:00
|
|
|
std::optional<clang::SourceLocation> declaration;
|
2017-02-20 07:51:31 +00:00
|
|
|
// TODO: definitions should be a list of locations, since there can be more
|
|
|
|
// than one.
|
|
|
|
std::optional<clang::SourceLocation> definition;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
// Type of the variable.
|
2017-02-18 19:37:24 +00:00
|
|
|
std::optional<TypeId> variable_type;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
// Type which declares this one (ie, it is a method)
|
2017-02-18 19:37:24 +00:00
|
|
|
std::optional<TypeId> declaring_type;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
// Usages.
|
2017-02-20 00:56:56 +00:00
|
|
|
std::vector<clang::SourceLocation> all_uses;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
VarDef(VarId id, const std::string& usr) : id(id), usr(usr) {
|
|
|
|
assert(usr.size() > 0);
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
};
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct ParsingDatabase {
|
|
|
|
// NOTE: Every Id is resolved to a file_id of 0. The correct file_id needs
|
|
|
|
// to get fixed up when inserting into the real db.
|
2017-02-18 19:37:24 +00:00
|
|
|
std::unordered_map<std::string, TypeId> usr_to_type_id;
|
|
|
|
std::unordered_map<std::string, FuncId> usr_to_func_id;
|
|
|
|
std::unordered_map<std::string, VarId> usr_to_var_id;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
std::vector<TypeDef> types;
|
|
|
|
std::vector<FuncDef> funcs;
|
|
|
|
std::vector<VarDef> vars;
|
|
|
|
|
2017-02-19 02:34:51 +00:00
|
|
|
ParsingDatabase();
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
TypeId ToTypeId(const std::string& usr);
|
|
|
|
FuncId ToFuncId(const std::string& usr);
|
|
|
|
VarId ToVarId(const std::string& usr);
|
2017-02-20 00:56:56 +00:00
|
|
|
TypeId ToTypeId(const CXCursor& usr);
|
|
|
|
FuncId ToFuncId(const CXCursor& usr);
|
|
|
|
VarId ToVarId(const CXCursor& usr);
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
TypeDef* Resolve(TypeId id);
|
|
|
|
FuncDef* Resolve(FuncId id);
|
|
|
|
VarDef* Resolve(VarId id);
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
std::string ToString();
|
2017-02-17 09:57:44 +00:00
|
|
|
};
|
|
|
|
|
2017-02-19 02:34:51 +00:00
|
|
|
ParsingDatabase::ParsingDatabase() {}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
// TODO: Optimize for const char*?
|
2017-02-17 09:57:44 +00:00
|
|
|
TypeId ParsingDatabase::ToTypeId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_type_id.find(usr);
|
|
|
|
if (it != usr_to_type_id.end())
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
TypeId id(types.size());
|
|
|
|
types.push_back(TypeDef(id, usr));
|
|
|
|
usr_to_type_id[usr] = id;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
2017-02-16 09:35:30 +00:00
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
FuncId ParsingDatabase::ToFuncId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_func_id.find(usr);
|
|
|
|
if (it != 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-18 19:37:24 +00:00
|
|
|
FuncId id(funcs.size());
|
|
|
|
funcs.push_back(FuncDef(id, usr));
|
|
|
|
usr_to_func_id[usr] = id;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
VarId ParsingDatabase::ToVarId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_var_id.find(usr);
|
|
|
|
if (it != 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-18 19:37:24 +00:00
|
|
|
VarId id(vars.size());
|
|
|
|
vars.push_back(VarDef(id, usr));
|
|
|
|
usr_to_var_id[usr] = id;
|
2017-02-17 09:57:44 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
TypeId ParsingDatabase::ToTypeId(const CXCursor& cursor) {
|
|
|
|
return ToTypeId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
|
|
|
FuncId ParsingDatabase::ToFuncId(const CXCursor& cursor) {
|
|
|
|
return ToFuncId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
|
|
|
VarId ParsingDatabase::ToVarId(const CXCursor& cursor) {
|
|
|
|
return ToVarId(clang::Cursor(cursor).get_usr());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
TypeDef* ParsingDatabase::Resolve(TypeId id) {
|
|
|
|
return &types[id.local_id];
|
|
|
|
}
|
|
|
|
FuncDef* ParsingDatabase::Resolve(FuncId id) {
|
|
|
|
return &funcs[id.local_id];
|
|
|
|
}
|
|
|
|
VarDef* ParsingDatabase::Resolve(VarId id) {
|
|
|
|
return &vars[id.local_id];
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
|
|
|
|
using Writer = rapidjson::PrettyWriter<rapidjson::StringBuffer>;
|
|
|
|
|
|
|
|
void Write(Writer& writer, const char* key, clang::SourceLocation location) {
|
|
|
|
if (key) writer.Key(key);
|
2017-02-18 19:37:24 +00:00
|
|
|
std::string s = location.ToString();
|
|
|
|
writer.String(s.c_str());
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
void Write(Writer& writer, const char* key, std::optional<clang::SourceLocation> location) {
|
|
|
|
if (location) {
|
|
|
|
Write(writer, key, location.value());
|
|
|
|
}
|
|
|
|
//else {
|
|
|
|
// if (key) writer.Key(key);
|
|
|
|
// writer.Null();
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(Writer& writer, const char* key, const std::vector<clang::SourceLocation>& locs) {
|
|
|
|
if (locs.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key) writer.Key(key);
|
|
|
|
writer.StartArray();
|
|
|
|
for (const clang::SourceLocation& loc : locs)
|
|
|
|
Write(writer, nullptr, loc);
|
|
|
|
writer.EndArray();
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
void Write(Writer& writer, const char* key, LocalId<T> id) {
|
|
|
|
if (key) writer.Key(key);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.Uint64(id.local_id);
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
void Write(Writer& writer, const char* key, std::optional<LocalId<T>> id) {
|
|
|
|
if (id) {
|
|
|
|
Write(writer, key, id.value());
|
|
|
|
}
|
|
|
|
//else {
|
|
|
|
// if (key) writer.Key(key);
|
|
|
|
// writer.Null();
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Write(Writer& writer, const char* key, const std::vector<LocalId<T>>& ids) {
|
|
|
|
if (ids.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key) writer.Key(key);
|
|
|
|
writer.StartArray();
|
|
|
|
for (LocalId<T> id : ids)
|
|
|
|
Write(writer, nullptr, id);
|
|
|
|
writer.EndArray();
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
void Write(Writer& writer, const char* key, Ref<T> ref) {
|
|
|
|
if (key) writer.Key(key);
|
2017-02-18 19:37:24 +00:00
|
|
|
std::string s = std::to_string(ref.id.local_id) + "@" + ref.loc.ToString();
|
|
|
|
writer.String(s.c_str());
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
void Write(Writer& writer, const char* key, const std::vector<Ref<T>>& refs) {
|
|
|
|
if (refs.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key) writer.Key(key);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.StartArray();
|
2017-02-18 23:58:40 +00:00
|
|
|
for (Ref<T> ref : refs)
|
|
|
|
Write(writer, nullptr, ref);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndArray();
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
void Write(Writer& writer, const char* key, const std::string& value) {
|
|
|
|
if (value.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key) writer.Key(key);
|
|
|
|
writer.String(value.c_str());
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
void Write(Writer& writer, const char* key, uint64_t value) {
|
|
|
|
if (key) writer.Key(key);
|
|
|
|
writer.Uint64(value);
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
std::string ParsingDatabase::ToString() {
|
2017-02-19 02:34:51 +00:00
|
|
|
auto it = usr_to_type_id.find("");
|
|
|
|
if (it != usr_to_type_id.end()) {
|
|
|
|
Resolve(it->second)->short_name = "<fundamental>";
|
2017-02-20 00:56:56 +00:00
|
|
|
assert(Resolve(it->second)->all_uses.size() == 0);
|
|
|
|
assert(Resolve(it->second)->interesting_uses.size() == 0);
|
2017-02-19 02:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
#define WRITE(name) Write(writer, #name, def.name)
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
writer.StartObject();
|
|
|
|
|
|
|
|
// Types
|
|
|
|
writer.Key("types");
|
|
|
|
writer.StartArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
for (TypeDef& def : types) {
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.StartObject();
|
2017-02-18 23:58:40 +00:00
|
|
|
WRITE(id);
|
|
|
|
WRITE(usr);
|
|
|
|
WRITE(short_name);
|
|
|
|
WRITE(qualified_name);
|
|
|
|
WRITE(definition);
|
2017-02-19 05:47:16 +00:00
|
|
|
WRITE(alias_of);
|
2017-02-18 23:58:40 +00:00
|
|
|
WRITE(parents);
|
|
|
|
WRITE(derived);
|
|
|
|
WRITE(types);
|
|
|
|
WRITE(funcs);
|
|
|
|
WRITE(vars);
|
2017-02-20 00:56:56 +00:00
|
|
|
WRITE(all_uses);
|
|
|
|
WRITE(interesting_uses);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndObject();
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// Functions
|
|
|
|
writer.Key("functions");
|
|
|
|
writer.StartArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
for (FuncDef& def : funcs) {
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.StartObject();
|
2017-02-18 23:58:40 +00:00
|
|
|
WRITE(id);
|
|
|
|
WRITE(usr);
|
|
|
|
WRITE(short_name);
|
|
|
|
WRITE(qualified_name);
|
|
|
|
WRITE(declaration);
|
|
|
|
WRITE(definition);
|
|
|
|
WRITE(declaring_type);
|
|
|
|
WRITE(base);
|
|
|
|
WRITE(derived);
|
|
|
|
WRITE(locals);
|
|
|
|
WRITE(callers);
|
|
|
|
WRITE(callees);
|
2017-02-20 00:56:56 +00:00
|
|
|
WRITE(all_uses);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndObject();
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// Variables
|
|
|
|
writer.Key("variables");
|
|
|
|
writer.StartArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
for (VarDef& def : vars) {
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.StartObject();
|
2017-02-18 23:58:40 +00:00
|
|
|
WRITE(id);
|
|
|
|
WRITE(usr);
|
|
|
|
WRITE(short_name);
|
|
|
|
WRITE(qualified_name);
|
|
|
|
WRITE(declaration);
|
2017-02-20 07:51:31 +00:00
|
|
|
WRITE(definition);
|
2017-02-18 23:58:40 +00:00
|
|
|
WRITE(variable_type);
|
|
|
|
WRITE(declaring_type);
|
2017-02-20 00:56:56 +00:00
|
|
|
//WRITE(initializations);
|
|
|
|
WRITE(all_uses);
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndObject();
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndArray();
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
writer.EndObject();
|
|
|
|
|
|
|
|
return output.GetString();
|
2017-02-18 23:58:40 +00:00
|
|
|
|
|
|
|
#undef WRITE
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FileDef {
|
|
|
|
uint64_t id;
|
|
|
|
std::string path;
|
|
|
|
std::vector<TypeDef> types;
|
|
|
|
std::vector<FuncDef> funcs;
|
|
|
|
std::vector<VarDef> vars;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
/*
|
2017-02-17 09:57:44 +00:00
|
|
|
struct Database {
|
2017-02-18 19:37:24 +00:00
|
|
|
std::unordered_map<std::string, TypeId> usr_to_type_id;
|
|
|
|
std::unordered_map<std::string, FuncId> usr_to_func_id;
|
|
|
|
std::unordered_map<std::string, VarId> usr_to_var_id;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
std::vector<FileDef> files;
|
|
|
|
|
|
|
|
TypeId ToTypeId(const std::string& usr);
|
|
|
|
FuncId ToFuncId(const std::string& usr);
|
|
|
|
VarId ToVarId(const std::string& usr);
|
|
|
|
};
|
|
|
|
|
|
|
|
TypeId Database::ToTypeId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_type_id.find(usr);
|
|
|
|
assert(it != usr_to_type_id.end() && "Usr is not registered");
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
FuncId Database::ToFuncId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_func_id.find(usr);
|
|
|
|
assert(it != usr_to_func_id.end() && "Usr is not registered");
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
VarId Database::ToVarId(const std::string& usr) {
|
2017-02-18 19:37:24 +00:00
|
|
|
auto it = usr_to_var_id.find(usr);
|
|
|
|
assert(it != usr_to_var_id.end() && "Usr is not registered");
|
2017-02-17 09:57:44 +00:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeDef* Resolve(FileDef* file, TypeId id) {
|
|
|
|
assert(file->id == id.file_id);
|
|
|
|
return &file->types[id.local_id];
|
|
|
|
}
|
|
|
|
FuncDef* Resolve(FileDef* file, FuncId id) {
|
|
|
|
assert(file->id == id.file_id);
|
|
|
|
return &file->funcs[id.local_id];
|
|
|
|
}
|
|
|
|
VarDef* Resolve(FileDef* file, VarId id) {
|
|
|
|
assert(file->id == id.file_id);
|
|
|
|
return &file->vars[id.local_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeDef* Resolve(Database* db, TypeId id) {
|
|
|
|
return Resolve(&db->files[id.file_id], id);
|
|
|
|
}
|
|
|
|
FuncDef* Resolve(Database* db, FuncId id) {
|
|
|
|
return Resolve(&db->files[id.file_id], id);
|
|
|
|
}
|
|
|
|
VarDef* Resolve(Database* db, VarId id) {
|
|
|
|
return Resolve(&db->files[id.file_id], id);
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
*/
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-20 06:40:55 +00:00
|
|
|
#if false
|
2017-02-17 09:57:44 +00:00
|
|
|
struct NamespaceStack {
|
|
|
|
std::vector<std::string> stack;
|
|
|
|
|
|
|
|
void Push(const std::string& ns);
|
|
|
|
void Pop();
|
2017-02-18 21:18:08 +00:00
|
|
|
std::string ComputeQualifiedName(
|
|
|
|
ParsingDatabase* db, std::optional<TypeId> declaring_type, std::string short_name);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
static NamespaceStack kEmpty;
|
2017-02-17 09:57:44 +00:00
|
|
|
};
|
2017-02-18 19:37:24 +00:00
|
|
|
NamespaceStack NamespaceStack::kEmpty;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
void NamespaceStack::Push(const std::string& ns) {
|
|
|
|
stack.push_back(ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NamespaceStack::Pop() {
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2017-02-18 21:18:08 +00:00
|
|
|
std::string NamespaceStack::ComputeQualifiedName(
|
|
|
|
ParsingDatabase* db, std::optional<TypeId> declaring_type, std::string short_name) {
|
|
|
|
if (declaring_type) {
|
|
|
|
TypeDef* def = db->Resolve(declaring_type.value());
|
|
|
|
return def->qualified_name + "::" + short_name;
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
std::string result;
|
|
|
|
for (const std::string& ns : stack)
|
|
|
|
result += ns + "::";
|
2017-02-18 21:18:08 +00:00
|
|
|
result += short_name;
|
2017-02-17 09:57:44 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-02-16 09:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<TypeId> ResolveDeclaringType(CXCursorKind kind, ParsingDatabase* db, const clang::Cursor& cursor, std::optional<TypeId> declaring_type) {
|
|
|
|
// Resolve the declaring type for out-of-line method definitions.
|
2017-02-18 21:18:08 +00:00
|
|
|
if (!declaring_type) {
|
2017-02-18 19:37:24 +00:00
|
|
|
clang::Cursor parent = cursor.get_semantic_parent();
|
|
|
|
switch (parent.get_kind()) {
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
declaring_type = db->ToTypeId(parent.get_usr());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldDecl, etc must have a declaring type.
|
|
|
|
assert(cursor.get_kind() != kind || declaring_type);
|
|
|
|
|
|
|
|
return declaring_type;
|
|
|
|
}
|
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
// |func_id| is the function definition that is currently being processed.
|
|
|
|
void InsertReference(ParsingDatabase* db, std::optional<FuncId> func_id, clang::Cursor referencer) {
|
|
|
|
clang::SourceLocation loc = referencer.get_source_location();
|
|
|
|
clang::Cursor referenced = referencer.get_referenced();
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
// Try to reference the actual template, instead of a specialization.
|
|
|
|
CXCursor generic_def = clang_getSpecializedCursorTemplate(referenced.cx_cursor);
|
|
|
|
if (!clang_Cursor_isNull(generic_def))
|
|
|
|
referenced = clang::Cursor(generic_def);
|
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
switch (referenced.get_kind()) {
|
2017-02-19 08:49:19 +00:00
|
|
|
case CXCursor_Constructor:
|
|
|
|
case CXCursor_Destructor:
|
2017-02-19 07:46:57 +00:00
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
case CXCursor_FunctionDecl:
|
2017-02-20 00:56:56 +00:00
|
|
|
case CXCursor_FunctionTemplate:
|
2017-02-19 07:46:57 +00:00
|
|
|
{
|
|
|
|
FuncId referenced_id = db->ToFuncId(referenced.get_usr());
|
|
|
|
FuncDef* referenced_def = db->Resolve(referenced_id);
|
2017-02-19 08:49:19 +00:00
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
if (func_id) {
|
|
|
|
FuncDef* func_def = db->Resolve(func_id.value());
|
|
|
|
func_def->callees.push_back(FuncRef(referenced_id, loc));
|
|
|
|
referenced_def->callers.push_back(FuncRef(func_id.value(), loc));
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
referenced_def->all_uses.push_back(loc);
|
2017-02-19 07:46:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
{
|
|
|
|
VarId referenced_id = db->ToVarId(referenced.get_usr());
|
|
|
|
VarDef* referenced_def = db->Resolve(referenced_id);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
referenced_def->all_uses.push_back(loc);
|
2017-02-19 07:46:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
std::cerr << "Unhandled reference from \"" << referencer.ToString()
|
|
|
|
<< "\" to \"" << referenced.ToString() << "\"" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 02:03:13 +00:00
|
|
|
void InsertTypeUsageAtLocation(ParsingDatabase* db, clang::Type type, const clang::SourceLocation& location) {
|
|
|
|
clang::Type raw_type = type.strip_qualifiers();
|
|
|
|
|
2017-02-19 02:34:51 +00:00
|
|
|
std::string usr = raw_type.get_usr();
|
|
|
|
if (usr == "")
|
2017-02-19 02:03:13 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Add a usage to the type of the variable.
|
|
|
|
TypeId type_id = db->ToTypeId(raw_type.get_usr());
|
2017-02-20 00:56:56 +00:00
|
|
|
db->Resolve(type_id)->interesting_uses.push_back(location);
|
2017-02-19 02:03:13 +00:00
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
struct VarDeclVisitorParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
std::optional<FuncId> func_id;
|
2017-02-19 08:49:19 +00:00
|
|
|
bool seen_type_ref = false;
|
2017-02-19 07:46:57 +00:00
|
|
|
|
|
|
|
VarDeclVisitorParam(ParsingDatabase* db, std::optional<FuncId> func_id)
|
|
|
|
: db(db), func_id(func_id) {}
|
|
|
|
};
|
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
// NOTE: This function does not process any of the definitions/etc defined
|
|
|
|
// inside of the call initializing the variable. That should be handled
|
|
|
|
// by the function-definition visitor!
|
2017-02-19 07:46:57 +00:00
|
|
|
clang::VisiterResult VarDeclVisitor(clang::Cursor cursor, clang::Cursor parent, VarDeclVisitorParam* param) {
|
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
InsertTypeUsageAtLocation(param->db, cursor.get_referenced().get_type(), cursor.get_source_location());
|
2017-02-19 08:49:19 +00:00
|
|
|
return clang::VisiterResult::Continue;
|
2017-02-19 07:46:57 +00:00
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
case CXCursor_TypeRef:
|
|
|
|
// This block of code will have two TypeRef nodes:
|
|
|
|
// Foo Foo::name = 3
|
|
|
|
// We try to avoid the second reference here by only processing the first one.
|
|
|
|
if (!param->seen_type_ref) {
|
|
|
|
param->seen_type_ref = true;
|
|
|
|
InsertTypeUsageAtLocation(param->db, cursor.get_referenced().get_type(), cursor.get_source_location());
|
|
|
|
}
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
|
2017-02-19 09:43:52 +00:00
|
|
|
case CXCursor_CallExpr:
|
2017-02-19 08:49:19 +00:00
|
|
|
case CXCursor_UnexposedExpr:
|
|
|
|
case CXCursor_UnaryOperator:
|
|
|
|
return clang::VisiterResult::Continue;
|
2017-02-19 09:05:57 +00:00
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "VarDeclVisitor unhandled " << cursor.ToString() << std::endl;
|
2017-02-19 08:49:19 +00:00
|
|
|
return clang::VisiterResult::Continue;
|
2017-02-19 07:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
void HandleVarDecl(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor var, std::optional<TypeId> declaring_type, std::optional<FuncId> func_id, bool declare_variable) {
|
2017-02-18 21:18:08 +00:00
|
|
|
//Dump(var);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
// Add a usage to the type of the variable.
|
2017-02-19 08:49:19 +00:00
|
|
|
//if (var.is_definition())
|
|
|
|
// InsertTypeUsageAtLocation(db, var.get_type(), var.get_source_location());
|
|
|
|
|
|
|
|
// Add usage to types.
|
|
|
|
VarDeclVisitorParam varDeclVisitorParam(db, func_id);
|
|
|
|
var.VisitChildren(&VarDeclVisitor, &varDeclVisitorParam);
|
2017-02-19 09:05:57 +00:00
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
if (!declare_variable)
|
|
|
|
return;
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
// Note: if there is no USR then there can be no declaring type, as all
|
|
|
|
// member variables of a class must have a name. Only function parameters
|
|
|
|
// can be nameless.
|
|
|
|
std::string var_usr = var.get_usr();
|
|
|
|
if (var_usr.size() == 0) {
|
|
|
|
assert(var.get_kind() == CXCursor_ParmDecl);
|
|
|
|
return;
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
VarId var_id = db->ToVarId(var_usr);
|
2017-02-18 19:37:24 +00:00
|
|
|
VarDef* var_def = db->Resolve(var_id);
|
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
declaring_type = ResolveDeclaringType(CXCursor_FieldDecl, db, var, declaring_type);
|
2017-02-18 19:37:24 +00:00
|
|
|
if (declaring_type && !var_def->declaration) {
|
2017-02-19 02:44:04 +00:00
|
|
|
// Note: If USR is null there can be no declaring type.
|
2017-02-18 19:37:24 +00:00
|
|
|
db->Resolve(declaring_type.value())->vars.push_back(var_id);
|
|
|
|
var_def->declaring_type = declaring_type;
|
|
|
|
}
|
|
|
|
|
2017-02-19 02:44:04 +00:00
|
|
|
// TODO: We could use RAII to verify we don't modify db while have a *Def
|
|
|
|
// instance alive.
|
|
|
|
var_def->short_name = var.get_spelling();
|
|
|
|
var_def->qualified_name =
|
|
|
|
ns->ComputeQualifiedName(db, declaring_type, var_def->short_name);
|
2017-02-19 00:53:31 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// We don't do any additional processing for non-definitions.
|
|
|
|
if (!var.is_definition()) {
|
|
|
|
var_def->declaration = var.get_source_location();
|
|
|
|
return;
|
|
|
|
}
|
2017-02-18 21:18:08 +00:00
|
|
|
// If we're a definition and there hasn't been a forward decl, just assign
|
|
|
|
// declaration location to definition location.
|
|
|
|
else if (!var_def->declaration) {
|
|
|
|
var_def->declaration = var.get_source_location();
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-18 21:18:08 +00:00
|
|
|
// TODO: Figure out how to scan initializations properly. We probably need
|
|
|
|
// to scan for assignment statement, or definition+ctor.
|
2017-02-20 00:56:56 +00:00
|
|
|
//var_def->initializations.push_back(var.get_source_location());
|
2017-02-19 07:46:57 +00:00
|
|
|
clang::Type var_type = var.get_type().strip_qualifiers();
|
2017-02-19 02:34:51 +00:00
|
|
|
std::string var_type_usr = var.get_type().strip_qualifiers().get_usr();
|
2017-02-19 07:46:57 +00:00
|
|
|
if (var_type_usr != "") {
|
2017-02-19 02:34:51 +00:00
|
|
|
var_def->variable_type = db->ToTypeId(var_type_usr);
|
2017-02-19 07:46:57 +00:00
|
|
|
/*
|
|
|
|
for (clang::Type template_param_type : var_type.get_template_arguments()) {
|
|
|
|
std::string usr = template_param_type.get_usr();
|
|
|
|
if (usr == "")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//TypeId template_param_id = db->ToTypeId(usr);
|
|
|
|
InsertTypeUsageAtLocation(db, template_param_type, var.get_source_location());
|
|
|
|
|
|
|
|
//std::cout << template_param_type.get_usr() << std::endl;
|
|
|
|
}*/
|
2017-02-19 08:49:19 +00:00
|
|
|
//VarDeclVisitorParam varDeclVisitorParam(db, func_id);
|
|
|
|
//var.VisitChildren(&VarDeclVisitor, &varDeclVisitorParam);
|
2017-02-19 07:46:57 +00:00
|
|
|
}
|
2017-02-19 08:49:19 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
// TODO: Should we declare variables on prototypes? ie,
|
|
|
|
//
|
|
|
|
// foo(int* x);
|
|
|
|
//
|
|
|
|
// I'm inclined to say yes if we want a rename refactoring.
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct FuncDefinitionParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
NamespaceStack* ns;
|
2017-02-18 21:18:08 +00:00
|
|
|
FuncId func_id;
|
2017-02-19 08:49:19 +00:00
|
|
|
bool is_definition;
|
2017-02-19 09:05:57 +00:00
|
|
|
bool has_return_type;
|
2017-02-19 08:49:19 +00:00
|
|
|
|
2017-02-19 09:05:57 +00:00
|
|
|
FuncDefinitionParam(ParsingDatabase* db, NamespaceStack* ns, FuncId func_id, bool is_definition, bool has_return_type)
|
|
|
|
: db(db), ns(ns), func_id(func_id), is_definition(is_definition), has_return_type(has_return_type) {}
|
2017-02-18 19:37:24 +00:00
|
|
|
};
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
clang::VisiterResult VisitFuncDefinition(clang::Cursor cursor, clang::Cursor parent, FuncDefinitionParam* param) {
|
2017-02-19 09:05:57 +00:00
|
|
|
if (param->has_return_type) {
|
|
|
|
// Foo* Foo::Bar() {} will have two TypeRef nodes.
|
|
|
|
assert(cursor.get_kind() == CXCursor_TypeRef);
|
|
|
|
InsertTypeUsageAtLocation(param->db, cursor.get_referenced().get_type(), cursor.get_source_location());
|
|
|
|
param->has_return_type = false;
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
//std::cout << "VistFuncDefinition got " << cursor.ToString() << std::endl;
|
2017-02-17 09:57:44 +00:00
|
|
|
switch (cursor.get_kind()) {
|
2017-02-19 09:05:57 +00:00
|
|
|
|
2017-02-18 21:18:08 +00:00
|
|
|
case CXCursor_CallExpr:
|
2017-02-19 09:43:52 +00:00
|
|
|
// When CallExpr points to a constructor, it does not have a child
|
|
|
|
// DeclRefExpr which also points to the constructor. Normal function calls
|
|
|
|
// (to a function of any type) look like this:
|
|
|
|
//
|
|
|
|
// CallExpr func_name
|
|
|
|
// ... (setup this pointer)
|
|
|
|
// *RefExpr func_name
|
|
|
|
// ... (setup arguments)
|
|
|
|
//
|
|
|
|
// Constructors, on the other hand, look like this:
|
|
|
|
//
|
|
|
|
// CallExpr func_name
|
|
|
|
// ... (setup arguments)
|
|
|
|
//
|
|
|
|
// We can't check the parent for a VarDecl, because a normal CallExpr could
|
|
|
|
// point to that. We simply check if the cursor references a constructor,
|
|
|
|
// and if so, insert the reference now, since it won't happen later.
|
|
|
|
if (cursor.get_referenced().get_kind() == CXCursor_Constructor)
|
|
|
|
InsertReference(param->db, param->func_id, cursor);
|
2017-02-18 19:37:24 +00:00
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-18 21:18:08 +00:00
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
case CXCursor_MemberRefExpr:
|
2017-02-18 21:18:08 +00:00
|
|
|
case CXCursor_DeclRefExpr:
|
|
|
|
InsertReference(param->db, param->func_id, cursor);
|
2017-02-19 05:47:16 +00:00
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
case CXCursor_VarDecl:
|
|
|
|
case CXCursor_ParmDecl:
|
2017-02-19 08:49:19 +00:00
|
|
|
//std::cout << "!! Parsing var decl " << cursor.ToString() << std::endl;
|
|
|
|
HandleVarDecl(param->db, param->ns, cursor, std::nullopt, param->func_id, param->is_definition);
|
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
case CXCursor_ReturnStmt:
|
2017-02-18 21:18:08 +00:00
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-16 09:35:30 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
default:
|
2017-02-18 21:18:08 +00:00
|
|
|
//std::cerr << "Unhandled VisitFuncDefinition kind " << clang::ToString(cursor.get_kind()) << std::endl;
|
|
|
|
return clang::VisiterResult::Recurse;
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
void HandleFunc(ParsingDatabase* db, NamespaceStack* ns, clang::Cursor func, std::optional<TypeId> declaring_type) {
|
2017-02-17 09:57:44 +00:00
|
|
|
// What this method must process:
|
|
|
|
// - function declaration
|
|
|
|
// - function definition
|
|
|
|
// - method declaration
|
|
|
|
// - method inline definition
|
|
|
|
// - method definition
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// Resolve id before checking for is_definition so that we insert the
|
|
|
|
// function into the db even if it is only a prototype. This is needed for
|
|
|
|
// various file-level operations like outlining.
|
|
|
|
FuncId func_id = db->ToFuncId(func.get_usr());
|
|
|
|
|
|
|
|
// TODO: Consider skipping some of this processing if we've done it already
|
|
|
|
// (ie, parsed prototype, then parse definition).
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
declaring_type =
|
|
|
|
ResolveDeclaringType(CXCursor_CXXMethod, db, func, declaring_type);
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
FuncDef* func_def = db->Resolve(func_id);
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
func_def->short_name = func.get_spelling();
|
|
|
|
func_def->qualified_name =
|
2017-02-18 21:18:08 +00:00
|
|
|
ns->ComputeQualifiedName(db, declaring_type, func_def->short_name);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
if (declaring_type && !func_def->declaration) {
|
|
|
|
db->Resolve(declaring_type.value())->funcs.push_back(func_id);
|
|
|
|
func_def->declaring_type = declaring_type;
|
|
|
|
}
|
|
|
|
|
2017-02-19 02:03:13 +00:00
|
|
|
// Don't process definition/body for declarations.
|
2017-02-18 19:37:24 +00:00
|
|
|
if (!func.is_definition()) {
|
|
|
|
func_def->declaration = func.get_source_location();
|
2017-02-19 02:03:13 +00:00
|
|
|
|
|
|
|
// We insert type references for arguments but don't use the normal visitor
|
|
|
|
// because that will add a definition for the variable. These are not
|
|
|
|
// "real" variables so we don't want to add definitions for them.
|
|
|
|
|
|
|
|
// We navigate using cursor arguments so we can get location data.
|
2017-02-19 08:49:19 +00:00
|
|
|
/*
|
2017-02-19 02:03:13 +00:00
|
|
|
for (clang::Cursor arg : func.get_arguments()) {
|
|
|
|
switch (arg.get_kind()) {
|
|
|
|
case CXCursor_ParmDecl:
|
|
|
|
InsertTypeUsageAtLocation(db, arg.get_type(), arg.get_source_location());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 08:49:19 +00:00
|
|
|
*/
|
2017-02-18 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
if (func.is_definition())
|
|
|
|
func_def->definition = func.get_source_location();
|
|
|
|
|
2017-02-19 09:05:57 +00:00
|
|
|
// Ignore any fundamental types for return. Note that void is a fundamental
|
|
|
|
// type.
|
|
|
|
bool has_return_type = !func.get_type().get_return_type().is_fundamental();
|
|
|
|
|
|
|
|
FuncDefinitionParam funcDefinitionParam(db, &NamespaceStack::kEmpty, func_id, func.is_definition(), has_return_type);
|
2017-02-18 19:37:24 +00:00
|
|
|
func.VisitChildren(&VisitFuncDefinition, &funcDefinitionParam);
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
struct UsingParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
TypeId active_type;
|
|
|
|
|
|
|
|
UsingParam(ParsingDatabase* db, TypeId active_type)
|
|
|
|
: db(db), active_type(active_type) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
clang::VisiterResult VisitUsing(clang::Cursor cursor, clang::Cursor parent, UsingParam* param) {
|
|
|
|
ParsingDatabase* db = param->db;
|
|
|
|
|
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
{
|
|
|
|
TypeId source_type = db->ToTypeId(cursor.get_referenced().get_usr());
|
|
|
|
db->Resolve(param->active_type)->alias_of = source_type;
|
|
|
|
return clang::VisiterResult::Break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
std::cerr << "Unhandled VisitClassDecl kind " << clang::ToString(cursor.get_kind()) << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
struct ClassDeclParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
NamespaceStack* ns;
|
|
|
|
TypeId active_type;
|
|
|
|
|
|
|
|
ClassDeclParam(ParsingDatabase* db, NamespaceStack* ns, TypeId active_type)
|
|
|
|
: db(db), ns(ns), active_type(active_type) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
clang::VisiterResult VisitClassDecl(clang::Cursor cursor, clang::Cursor parent, ClassDeclParam* param) {
|
|
|
|
ParsingDatabase* db = param->db;
|
|
|
|
|
|
|
|
switch (cursor.get_kind()) {
|
2017-02-19 05:47:16 +00:00
|
|
|
case CXCursor_CXXAccessSpecifier:
|
|
|
|
break;
|
|
|
|
|
2017-02-19 08:49:19 +00:00
|
|
|
case CXCursor_Constructor:
|
|
|
|
case CXCursor_Destructor:
|
2017-02-17 09:57:44 +00:00
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
HandleFunc(param->db, param->ns, cursor, param->active_type);
|
|
|
|
break;
|
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
case CXCursor_FieldDecl:
|
2017-02-18 21:18:08 +00:00
|
|
|
case CXCursor_VarDecl:
|
2017-02-19 08:49:19 +00:00
|
|
|
HandleVarDecl(param->db, param->ns, cursor, param->active_type, std::nullopt, true /*declare_variable*/);
|
2017-02-18 19:37:24 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "Unhandled VisitClassDecl kind " << clang::ToString(cursor.get_kind()) << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
}
|
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
void HandleClassDecl(clang::Cursor cursor, ParsingDatabase* db, NamespaceStack* ns, bool is_alias) {
|
|
|
|
TypeId type_id = db->ToTypeId(cursor.get_usr());
|
|
|
|
TypeDef* type_def = db->Resolve(type_id);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
type_def->short_name = cursor.get_spelling();
|
2017-02-18 21:18:08 +00:00
|
|
|
// TODO: Support nested classes (pass in declaring type insteaad of nullopt!)
|
2017-02-19 05:47:16 +00:00
|
|
|
type_def->qualified_name =
|
|
|
|
ns->ComputeQualifiedName(db, std::nullopt, type_def->short_name);
|
2017-02-18 19:37:24 +00:00
|
|
|
|
|
|
|
if (!cursor.is_definition()) {
|
2017-02-19 05:47:16 +00:00
|
|
|
if (!type_def->declaration)
|
|
|
|
type_def->declaration = cursor.get_source_location();
|
2017-02-18 19:37:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
type_def->definition = cursor.get_source_location();
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
if (is_alias) {
|
|
|
|
UsingParam usingParam(db, type_id);
|
|
|
|
cursor.VisitChildren(&VisitUsing, &usingParam);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ClassDeclParam classDeclParam(db, ns, type_id);
|
|
|
|
cursor.VisitChildren(&VisitClassDecl, &classDeclParam);
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
struct FileParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
NamespaceStack* ns;
|
|
|
|
|
|
|
|
FileParam(ParsingDatabase* db, NamespaceStack* ns) : db(db), ns(ns) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
clang::VisiterResult VisitFile(clang::Cursor cursor, clang::Cursor parent, FileParam* param) {
|
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
case CXCursor_Namespace:
|
|
|
|
// For a namespace, visit the children of the namespace, but this time with
|
|
|
|
// a pushed namespace stack.
|
|
|
|
param->ns->Push(cursor.get_display_name());
|
|
|
|
cursor.VisitChildren(&VisitFile, param);
|
|
|
|
param->ns->Pop();
|
|
|
|
break;
|
|
|
|
|
2017-02-19 05:47:16 +00:00
|
|
|
case CXCursor_TypeAliasDecl:
|
|
|
|
case CXCursor_TypedefDecl:
|
|
|
|
HandleClassDecl(cursor, param->db, param->ns, true /*is_alias*/);
|
|
|
|
break;
|
|
|
|
|
2017-02-19 07:46:57 +00:00
|
|
|
case CXCursor_ClassTemplate:
|
2017-02-18 23:58:40 +00:00
|
|
|
case CXCursor_StructDecl:
|
2017-02-17 09:57:44 +00:00
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
// TODO: Cleanup Handle* param order.
|
2017-02-19 05:47:16 +00:00
|
|
|
HandleClassDecl(cursor, param->db, param->ns, false /*is_alias*/);
|
2017-02-17 09:57:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
case CXCursor_FunctionDecl:
|
2017-02-20 00:56:56 +00:00
|
|
|
case CXCursor_FunctionTemplate:
|
2017-02-17 09:57:44 +00:00
|
|
|
HandleFunc(param->db, param->ns, cursor, std::nullopt);
|
|
|
|
break;
|
|
|
|
|
2017-02-18 21:18:08 +00:00
|
|
|
case CXCursor_VarDecl:
|
2017-02-19 08:49:19 +00:00
|
|
|
HandleVarDecl(param->db, param->ns, cursor, std::nullopt, std::nullopt, true /*declare_variable*/);
|
2017-02-18 21:18:08 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "Unhandled VisitFile kind " << clang::ToString(cursor.get_kind()) << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Continue;
|
|
|
|
}
|
2017-02-20 06:40:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
ParsingDatabase Parse2(std::string filename) {
|
|
|
|
std::vector<std::string> args;
|
|
|
|
|
|
|
|
clang::Index index(0 /*excludeDeclarationsFromPCH*/, 0 /*displayDiagnostics*/);
|
|
|
|
clang::TranslationUnit tu(index, filename, args);
|
|
|
|
|
|
|
|
std::cout << "Start document dump" << std::endl;
|
|
|
|
Dump(tu.document_cursor());
|
|
|
|
std::cout << "Done document dump" << std::endl << std::endl;
|
|
|
|
|
|
|
|
ParsingDatabase db;
|
|
|
|
NamespaceStack ns;
|
|
|
|
FileParam file_param(&db, &ns);
|
|
|
|
|
|
|
|
tu.document_cursor().VisitChildren(&VisitFile, &file_param);
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
std::optional<clang::Cursor> result;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<clang::Cursor> FindChildOfKind(clang::Cursor cursor, CXCursorKind kind) {
|
|
|
|
FindChildOfKindParam param(kind);
|
|
|
|
cursor.VisitChildren(&FindChildOfKindVisitor, ¶m);
|
|
|
|
return param.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-20 19:08:27 +00:00
|
|
|
clang::VisiterResult FindTypeVisitor(clang::Cursor cursor, clang::Cursor parent, std::optional<clang::Cursor>* result) {
|
|
|
|
switch (cursor.get_kind()) {
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
*result = cursor;
|
|
|
|
return clang::VisiterResult::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clang::VisiterResult::Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<clang::Cursor> FindType(clang::Cursor cursor) {
|
|
|
|
std::optional<clang::Cursor> result;
|
|
|
|
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 {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
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.
|
|
|
|
clang::SourceLocation last_type_usage_location;
|
|
|
|
clang::SourceLocation last_func_usage_location;
|
|
|
|
|
|
|
|
IndexParam(ParsingDatabase* db, NamespaceHelper* ns) : db(db), ns(ns) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
std::string GetNamespacePrefx(const CXIdxDeclInfo* decl) {
|
|
|
|
const CXIdxContainerInfo* container = decl->lexicalContainer;
|
|
|
|
while (container) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-02-20 19:08:27 +00:00
|
|
|
bool HasUsage(const std::vector<clang::SourceLocation>& usages, const clang::SourceLocation& usage) {
|
|
|
|
for (int i = usages.size() - 1; i >= 0; --i) {
|
|
|
|
if (usages[i] == usage)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
bool IsTypeDefinition(const CXIdxContainerInfo* container) {
|
|
|
|
if (!container)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (container->cursor.kind) {
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
#if false
|
|
|
|
struct TypeResolution {
|
|
|
|
std::optional<TypeId> resolved_type;
|
|
|
|
|
|
|
|
// If |check_template_arguments| is true, |original_type| may have template
|
|
|
|
// parameters with interesting usage information.
|
|
|
|
std::vector<clang::Type> template_arguments;
|
|
|
|
};
|
|
|
|
|
|
|
|
TypeResolution ResolveToType(ParsingDatabase* db, clang::Type type) {
|
|
|
|
TypeResolution result;
|
|
|
|
|
|
|
|
type = type.strip_qualifiers();
|
|
|
|
std::string usr = type.get_usr();
|
2017-02-20 19:08:27 +00:00
|
|
|
|
|
|
|
if (usr == "")
|
2017-02-20 21:48:46 +00:00
|
|
|
return result;
|
2017-02-20 19:08:27 +00:00
|
|
|
|
|
|
|
// TODO: Add a check and don't resolve template specializations that exist in source code.
|
|
|
|
// Resolve template specialization so that we always point to the non-specialized type.
|
2017-02-20 21:48:46 +00:00
|
|
|
result.template_arguments = type.get_template_arguments();
|
|
|
|
if (result.template_arguments.size() > 0) {
|
|
|
|
clang::Cursor decl = clang_getTypeDeclaration(type.cx_type);
|
|
|
|
clang::Cursor unresolved_decl = clang_getSpecializedCursorTemplate(decl.cx_cursor);
|
|
|
|
usr = clang::Cursor(unresolved_decl).get_usr();
|
|
|
|
/*
|
|
|
|
std::string template_usr = clang::Cursor(unresolved_decl).get_usr();
|
|
|
|
if (template_usr != "") {
|
|
|
|
result.check_template_arguments = true;
|
|
|
|
result.original_type = type;
|
|
|
|
usr = template_usr;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
2017-02-20 19:08:27 +00:00
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
result.resolved_type = db->ToTypeId(usr);
|
|
|
|
return result;
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clang::SourceLocation FindLocationOfTypeSpecifier(clang::Cursor cursor) {
|
|
|
|
std::cout << "FindLocationOfTypeSpecifier " << std::endl;
|
|
|
|
Dump(cursor);
|
|
|
|
|
|
|
|
std::optional<clang::Cursor> child = FindType(cursor);
|
|
|
|
assert(child.has_value()); // If this assert ever fails just use |cursor| loc or figure out what type ref we are missing.
|
|
|
|
return child.value().get_source_location();
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
void AddInterestingUsageToType(ParsingDatabase* db, TypeResolution resolved_type, clang::SourceLocation location) {
|
|
|
|
// TODO: pass cursor in. Implement custom visitor just for this. Cursor resolves type as needed. Can we use visitor types as the actual types?
|
|
|
|
|
|
|
|
TypeDef* type_def = db->Resolve(resolved_type.resolved_type.value());
|
2017-02-20 19:08:27 +00:00
|
|
|
type_def->interesting_uses.push_back(location);
|
2017-02-20 21:48:46 +00:00
|
|
|
|
|
|
|
//if (resolved_type.check_template_arguments) {
|
|
|
|
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct VisitDeclForTypeUsageParam {
|
|
|
|
ParsingDatabase* db;
|
|
|
|
bool is_interesting;
|
|
|
|
int has_processed_any = false;
|
|
|
|
std::optional<clang::Cursor> previous_cursor;
|
|
|
|
std::optional<TypeId> initial_type;
|
|
|
|
|
|
|
|
VisitDeclForTypeUsageParam(ParsingDatabase* db, bool is_interesting)
|
|
|
|
: db(db), is_interesting(is_interesting) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void VisitDeclForTypeUsageVisitorHandler(clang::Cursor cursor, VisitDeclForTypeUsageParam* param) {
|
|
|
|
param->has_processed_any = true;
|
|
|
|
ParsingDatabase* db = param->db;
|
|
|
|
|
|
|
|
TypeId ref_type_id = db->ToTypeId(cursor.get_referenced().get_usr());
|
|
|
|
if (!param->initial_type)
|
|
|
|
param->initial_type = ref_type_id;
|
|
|
|
|
|
|
|
if (param->is_interesting) {
|
|
|
|
TypeDef* ref_type_def = db->Resolve(ref_type_id);
|
|
|
|
ref_type_def->interesting_uses.push_back(cursor.get_source_location());
|
|
|
|
}
|
2017-02-20 19:08:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:46 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> ResolveDeclToType(ParsingDatabase* db, clang::Cursor decl_cursor, const CXIdxContainerInfo* semantic_container, const CXIdxContainerInfo* lexical_container, bool is_interesting) {
|
|
|
|
//
|
|
|
|
// 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() {}
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
VisitDeclForTypeUsageParam param(db, is_interesting);
|
|
|
|
decl_cursor.VisitChildren(&VisitDeclForTypeUsageVisitor, ¶m);
|
|
|
|
|
|
|
|
// VisitDeclForTypeUsageVisitor guarantees that if there are multiple TypeRef
|
|
|
|
// children, the first one will always be visited.
|
|
|
|
if (param.previous_cursor && process_last_type_ref) {
|
|
|
|
VisitDeclForTypeUsageVisitorHandler(param.previous_cursor.value(), ¶m);
|
|
|
|
} 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
|
|
|
|
2017-02-20 21:48:46 +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) {
|
|
|
|
IndexParam* param = static_cast<IndexParam*>(client_data);
|
|
|
|
ParsingDatabase* db = param->db;
|
|
|
|
NamespaceHelper* ns = param->ns;
|
|
|
|
|
|
|
|
switch (decl->entityInfo->kind) {
|
|
|
|
case CXIdxEntity_CXXNamespace:
|
|
|
|
{
|
|
|
|
ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer, decl->entityInfo->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
VarDef* var_def = db->Resolve(var_id);
|
|
|
|
|
|
|
|
// TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow.
|
|
|
|
// TODO: Verify this gets called multiple times
|
|
|
|
//if (!decl->isRedeclaration) {
|
|
|
|
var_def->short_name = decl->entityInfo->name;
|
|
|
|
var_def->qualified_name = ns->QualifiedName(decl->semanticContainer, var_def->short_name);
|
|
|
|
//}
|
|
|
|
|
2017-02-20 07:51:31 +00:00
|
|
|
if (decl->isDefinition)
|
|
|
|
var_def->definition = decl->loc;
|
|
|
|
else
|
|
|
|
var_def->declaration = decl->loc;
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
var_def->all_uses.push_back(decl->loc);
|
|
|
|
|
2017-02-20 06:06:46 +00:00
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
std::optional<TypeId> var_type = ResolveDeclToType(db, decl_cursor, decl->semanticContainer, decl->lexicalContainer, decl_cursor.get_kind() != CXCursor_ParmDecl);
|
|
|
|
if (var_type.has_value())
|
|
|
|
var_def->variable_type = var_type.value();
|
2017-02-20 19:08:27 +00:00
|
|
|
// Declaring variable type information.
|
2017-02-20 21:48:46 +00:00
|
|
|
/*
|
|
|
|
TypeResolution var_type = ResolveToType(db, decl_cursor.get_type());
|
|
|
|
if (var_type.resolved_type) {
|
|
|
|
var_def->variable_type = var_type.resolved_type.value();
|
2017-02-20 06:06:46 +00:00
|
|
|
// Insert an interesting type usage for variable declarations. Parameters
|
|
|
|
// are handled when a function is declared because clang doesn't provide
|
|
|
|
// parameter declarations for unnamed parameters.
|
2017-02-20 19:08:27 +00:00
|
|
|
if (decl_cursor.get_kind() != CXCursor_ParmDecl)
|
2017-02-20 21:48:46 +00:00
|
|
|
AddInterestingUsageToType(db, var_type, FindLocationOfTypeSpecifier(decl_cursor));
|
2017-02-20 02:00:58 +00:00
|
|
|
}
|
2017-02-20 21:48:46 +00:00
|
|
|
*/
|
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);
|
|
|
|
TypeDef* declaring_type_def = db->Resolve(declaring_type_id);
|
|
|
|
var_def->declaring_type = declaring_type_id;
|
|
|
|
declaring_type_def->vars.push_back(var_id);
|
|
|
|
}
|
|
|
|
// std::optional<clang::SourceLocation> declaration;
|
|
|
|
// std::vector<clang::SourceLocation> initializations;
|
|
|
|
// std::optional<TypeId> variable_type;
|
|
|
|
// std::optional<TypeId> declaring_type;
|
|
|
|
// std::vector<clang::SourceLocation> uses;
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
FuncDef* func_def = db->Resolve(func_id);
|
|
|
|
|
|
|
|
// TODO: Eventually run with this if. Right now I want to iron out bugs this may shadow.
|
|
|
|
//if (!decl->isRedeclaration) {
|
|
|
|
func_def->short_name = decl->entityInfo->name;
|
|
|
|
func_def->qualified_name = ns->QualifiedName(decl->semanticContainer, func_def->short_name);
|
|
|
|
//}
|
|
|
|
|
|
|
|
if (decl->isDefinition)
|
|
|
|
func_def->definition = decl->loc;
|
2017-02-20 07:51:31 +00:00
|
|
|
else
|
|
|
|
func_def->declaration = decl->loc;
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
func_def->all_uses.push_back(decl->loc);
|
|
|
|
|
2017-02-20 07:06:38 +00:00
|
|
|
bool is_pure_virtual = clang_CXXMethod_isPureVirtual(decl->cursor);
|
|
|
|
|
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-20 07:06:38 +00:00
|
|
|
if ((decl->isDefinition || is_pure_virtual) && IsTypeDefinition(decl->semanticContainer)) {
|
2017-02-20 00:56:56 +00:00
|
|
|
TypeId declaring_type_id = db->ToTypeId(decl->semanticContainer->cursor);
|
|
|
|
TypeDef* declaring_type_def = db->Resolve(declaring_type_id);
|
|
|
|
func_def->declaring_type = declaring_type_id;
|
|
|
|
declaring_type_def->funcs.push_back(func_id);
|
|
|
|
}
|
|
|
|
|
2017-02-20 21:48:46 +00:00
|
|
|
// We don't actually need to know the return type, but we need to mark it
|
|
|
|
// as an interesting usage.
|
|
|
|
ResolveDeclToType(db, decl_cursor, decl->semanticContainer, decl->lexicalContainer, true /*is_interesting*/);
|
|
|
|
|
|
|
|
//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:
|
2017-02-20 21:48:46 +00:00
|
|
|
// We don't need to know the arg type, but we do want to mark it as
|
|
|
|
// an interesting usage.
|
|
|
|
ResolveDeclToType(db, arg, decl->semanticContainer, decl->lexicalContainer, true /*is_interesting*/);
|
|
|
|
|
|
|
|
//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());
|
|
|
|
FuncDef* parent_def = db->Resolve(parent_id);
|
|
|
|
func_def = db->Resolve(func_id); // ToFuncId invalidated func_def
|
|
|
|
|
|
|
|
func_def->base = parent_id;
|
|
|
|
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
|
|
|
/*
|
|
|
|
std::optional<FuncId> base;
|
|
|
|
std::vector<FuncId> derived;
|
|
|
|
std::vector<VarId> locals;
|
|
|
|
std::vector<FuncRef> callers;
|
|
|
|
std::vector<FuncRef> callees;
|
|
|
|
std::vector<clang::SourceLocation> uses;
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:35:56 +00:00
|
|
|
case CXIdxEntity_Typedef:
|
|
|
|
case CXIdxEntity_CXXTypeAlias:
|
|
|
|
{
|
|
|
|
TypeId type_id = db->ToTypeId(decl->entityInfo->USR);
|
|
|
|
|
|
|
|
std::optional<clang::Cursor> type_ref = FindChildOfKind(decl->cursor, CXCursor_TypeRef);
|
|
|
|
assert(type_ref.has_value());
|
|
|
|
TypeId alias_of = db->ToTypeId(type_ref.value().get_referenced().get_usr());
|
2017-02-20 06:31:25 +00:00
|
|
|
|
2017-02-20 02:35:56 +00:00
|
|
|
TypeDef* type_def = db->Resolve(type_id);
|
|
|
|
|
|
|
|
type_def->alias_of = alias_of;
|
|
|
|
db->Resolve(alias_of)->interesting_uses.push_back(type_ref.value().get_source_location());
|
|
|
|
|
|
|
|
type_def->short_name = decl->entityInfo->name;
|
|
|
|
type_def->qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->short_name);
|
|
|
|
|
|
|
|
type_def->definition = decl->loc;
|
|
|
|
type_def->all_uses.push_back(decl->loc);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
case CXIdxEntity_CXXClass:
|
|
|
|
{
|
|
|
|
ns->RegisterQualifiedName(decl->entityInfo->USR, decl->semanticContainer, decl->entityInfo->name);
|
|
|
|
|
|
|
|
TypeId type_id = db->ToTypeId(decl->entityInfo->USR);
|
|
|
|
TypeDef* type_def = db->Resolve(type_id);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
type_def->short_name = decl->entityInfo->name;
|
|
|
|
type_def->qualified_name = ns->QualifiedName(decl->semanticContainer, type_def->short_name);
|
|
|
|
// }
|
|
|
|
|
2017-02-20 07:51:31 +00:00
|
|
|
assert(decl->isDefinition);
|
2017-02-20 00:56:56 +00:00
|
|
|
type_def->definition = decl->loc;
|
|
|
|
|
|
|
|
type_def->all_uses.push_back(decl->loc);
|
|
|
|
|
|
|
|
//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);
|
|
|
|
for (unsigned int i = 0; i < class_info->numBases; ++i) {
|
|
|
|
const CXIdxBaseClassInfo* base_class = class_info->bases[i];
|
|
|
|
|
|
|
|
TypeId parent_type_id = db->ToTypeId(clang::Cursor(base_class->cursor).get_referenced().get_usr());
|
|
|
|
TypeDef* parent_type_def = db->Resolve(parent_type_id);
|
|
|
|
TypeDef* type_def = db->Resolve(type_id); // type_def ptr could be invalidated by ToTypeId.
|
|
|
|
|
|
|
|
parent_type_def->derived.push_back(type_id);
|
|
|
|
type_def->parents.push_back(parent_type_id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cout << "!! Unhandled indexDeclaration: " << clang::Cursor(decl->cursor).ToString() << " at " << clang::SourceLocation(decl->loc).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);
|
|
|
|
ParsingDatabase* db = param->db;
|
|
|
|
clang::Cursor cursor(ref->cursor);
|
|
|
|
|
2017-02-20 06:06:46 +00:00
|
|
|
// TODO: Index entity call/ctor creation, like Foo().x = 3
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
switch (ref->referencedEntity->kind) {
|
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);
|
|
|
|
VarDef* var_def = db->Resolve(var_id);
|
|
|
|
var_def->all_uses.push_back(ref->loc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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:
|
2017-02-20 00:56:56 +00:00
|
|
|
{
|
|
|
|
// TODO: Redirect container to constructor for
|
|
|
|
// 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-20 00:56:56 +00:00
|
|
|
clang::SourceLocation loc = ref->loc;
|
|
|
|
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);
|
|
|
|
FuncDef* caller_def = db->Resolve(caller_id);
|
|
|
|
FuncDef* called_def = db->Resolve(called_id);
|
|
|
|
|
|
|
|
caller_def->callees.push_back(FuncRef(called_id, loc));
|
|
|
|
called_def->callers.push_back(FuncRef(caller_id, loc));
|
|
|
|
called_def->all_uses.push_back(loc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FuncDef* called_def = db->Resolve(called_id);
|
|
|
|
called_def->all_uses.push_back(loc);
|
|
|
|
}
|
|
|
|
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-20 00:56:56 +00:00
|
|
|
case CXIdxEntity_Struct:
|
|
|
|
case CXIdxEntity_CXXClass:
|
|
|
|
{
|
|
|
|
std::cout << "Reference at " << clang::SourceLocation(ref->loc).ToString() << std::endl;
|
|
|
|
TypeId referenced_id = db->ToTypeId(ref->referencedEntity->USR);
|
|
|
|
TypeDef* referenced_def = db->Resolve(referenced_id);
|
2017-02-17 09:57:44 +00:00
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// To work around this, we store the last type usage location. If our
|
|
|
|
// current location is the same as that location, don't report it as a
|
|
|
|
// usage. We don't need to check active type id because there can only be
|
|
|
|
// one type reference at any location in code.
|
|
|
|
//
|
|
|
|
// struct Foo {};
|
|
|
|
// void Make() {
|
|
|
|
// Foo f;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
clang::SourceLocation loc = ref->loc;
|
2017-02-20 02:00:58 +00:00
|
|
|
//if (param->last_type_usage_location == loc) break;
|
|
|
|
//param->last_type_usage_location = loc;
|
|
|
|
|
2017-02-20 19:08:27 +00:00
|
|
|
if (!HasUsage(referenced_def->all_uses, loc))
|
|
|
|
referenced_def->all_uses.push_back(loc);
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-02-20 02:00:58 +00:00
|
|
|
/*
|
2017-02-20 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Variable declarations have an embedded TypeRef.
|
|
|
|
//
|
|
|
|
if (cursor.get_kind() == CXCursor_TypeRef &&
|
|
|
|
ref->parentEntity && ref->parentEntity->kind == CXIdxEntity_Variable) {
|
|
|
|
referenced_def->interesting_uses.push_back(loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If this is a type reference to a method then there will be two calls to
|
|
|
|
// this method with a TypeRef cursor kind. Only the return type is an
|
|
|
|
// interesting use (ie, Foo* is interesting, but not |Foo| in Foo::Hello).
|
|
|
|
//
|
|
|
|
// Foo* Foo::Hello() {}
|
|
|
|
//
|
|
|
|
// We handle this by adding a |needs_return_type_index| bool to FuncDef.
|
|
|
|
// It is only set to true when the type has a return value. We visit the
|
|
|
|
// return type TypeRef first, so we consume the bool and the second TypeRef
|
|
|
|
// will not get marked as interesting.
|
|
|
|
//
|
|
|
|
if (cursor.get_kind() == CXCursor_TypeRef &&
|
|
|
|
ref->parentEntity && ref->parentEntity->kind == CXIdxEntity_CXXInstanceMethod) {
|
|
|
|
FuncId declaring_func_id = db->ToFuncId(ref->parentEntity->USR);
|
|
|
|
FuncDef* declaring_func_def = db->Resolve(declaring_func_id);
|
|
|
|
|
|
|
|
if (declaring_func_def->needs_return_type_index) {
|
|
|
|
declaring_func_def->needs_return_type_index = false;
|
|
|
|
referenced_def->interesting_uses.push_back(loc);
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 02:00:58 +00:00
|
|
|
*/
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cout << "!! Unhandled indexEntityReference: " << cursor.ToString() << " at " << clang::SourceLocation(ref->loc).ToString() << std::endl;
|
|
|
|
std::cout << " ref->referencedEntity->kind = " << ref->referencedEntity->kind << std::endl;
|
|
|
|
if (ref->parentEntity)
|
|
|
|
std::cout << " ref->parentEntity->kind = " << ref->parentEntity->kind << std::endl;
|
|
|
|
std::cout << " ref->loc = " << clang::SourceLocation(ref->loc).ToString() << std::endl;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ParsingDatabase Parse(std::string filename) {
|
2017-02-16 09:35:30 +00:00
|
|
|
std::vector<std::string> args;
|
|
|
|
|
|
|
|
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-20 00:56:56 +00:00
|
|
|
Dump(tu.document_cursor());
|
|
|
|
|
|
|
|
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;
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
ParsingDatabase db;
|
|
|
|
NamespaceHelper ns;
|
|
|
|
IndexParam param(&db, &ns);
|
|
|
|
clang_indexTranslationUnit(index_action, ¶m, callbacks, sizeof(callbacks),
|
|
|
|
CXIndexOpt_IndexFunctionLocalSymbols, tu.cx_tu);
|
|
|
|
|
|
|
|
clang_IndexAction_dispose(index_action);
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
template<typename T>
|
|
|
|
bool AreEqual(const std::vector<T>& a, const std::vector<T>& b) {
|
|
|
|
if (a.size() != b.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < a.size(); ++i) {
|
|
|
|
if (a[i] != b[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const std::vector<std::string>& strs) {
|
|
|
|
for (const std::string& str : strs) {
|
|
|
|
std::cout << str << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
|
|
|
|
std::string ToString(const rapidjson::Document& document) {
|
|
|
|
rapidjson::StringBuffer buffer;
|
|
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
|
|
|
writer.SetFormatOptions(
|
|
|
|
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
|
|
|
|
writer.SetIndent(' ', 2);
|
|
|
|
|
|
|
|
buffer.Clear();
|
|
|
|
document.Accept(writer);
|
|
|
|
return buffer.GetString();
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
std::vector<std::string> split_string(const std::string& str, const std::string& delimiter) {
|
|
|
|
// http://stackoverflow.com/a/13172514
|
|
|
|
std::vector<std::string> strings;
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
std::string::size_type prev = 0;
|
|
|
|
while ((pos = str.find(delimiter, prev)) != std::string::npos) {
|
|
|
|
strings.push_back(str.substr(prev, pos - prev));
|
|
|
|
prev = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To get the last substring (or only, if delimiter is not found)
|
|
|
|
strings.push_back(str.substr(prev));
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:56:56 +00:00
|
|
|
|
2017-02-18 23:58:40 +00:00
|
|
|
void DiffDocuments(rapidjson::Document& expected, rapidjson::Document& actual) {
|
|
|
|
std::vector<std::string> actual_output;
|
|
|
|
{
|
2017-02-20 00:56:56 +00:00
|
|
|
std::string buffer = ToString(actual);
|
|
|
|
actual_output = split_string(buffer, "\n");
|
2017-02-18 23:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> expected_output;
|
|
|
|
{
|
2017-02-20 00:56:56 +00:00
|
|
|
std::string buffer = ToString(expected);
|
|
|
|
expected_output = split_string(buffer, "\n");
|
2017-02-18 23:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int len = std::min(actual_output.size(), expected_output.size());
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
if (actual_output[i] != expected_output[i]) {
|
2017-02-19 02:03:13 +00:00
|
|
|
std::cout << "Line " << i << " differs:" << std::endl;
|
2017-02-18 23:58:40 +00:00
|
|
|
std::cout << " expected: " << expected_output[i] << std::endl;
|
|
|
|
std::cout << " actual: " << actual_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actual_output.size() > len) {
|
|
|
|
std::cout << "Additional output in actual:" << std::endl;
|
|
|
|
for (int i = len; i < actual_output.size(); ++i)
|
|
|
|
std::cout << " " << actual_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_output.size() > len) {
|
|
|
|
std::cout << "Additional output in expected:" << std::endl;
|
|
|
|
for (int i = len; i < expected_output.size(); ++i)
|
|
|
|
std::cout << " " << expected_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 19:37:24 +00:00
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
int main(int argc, char** argv) {
|
2017-02-20 00:56:56 +00:00
|
|
|
/*
|
|
|
|
ParsingDatabase db = Parse("tests/vars/function_local.cc");
|
|
|
|
std::cout << std::endl << "== Database ==" << std::endl;
|
|
|
|
std::cout << db.ToString();
|
|
|
|
std::cin.get();
|
|
|
|
return 0;
|
|
|
|
*/
|
|
|
|
|
2017-02-17 09:57:44 +00:00
|
|
|
for (std::string path : GetFilesInFolder("tests")) {
|
2017-02-20 21:48:46 +00:00
|
|
|
//if (path != "tests/declaration_vs_definition/class_member_static.cc") continue;
|
|
|
|
//if (path != "tests/usage/type_usage_declare_param.cc") continue;
|
2017-02-20 19:08:27 +00:00
|
|
|
//if (path == "tests/constructors/constructor.cc") continue;
|
|
|
|
//if (path == "tests/constructors/destructor.cc") continue;
|
|
|
|
//if (path == "tests/usage/func_usage_call_method.cc") continue;
|
2017-02-20 21:48:46 +00:00
|
|
|
//if (path != "tests/usage/type_usage_as_template_parameter.cc") continue;
|
|
|
|
//if (path != "tests/usage/type_usage_as_template_parameter_complex.cc") continue;
|
|
|
|
//if (path != "tests/usage/type_usage_as_template_parameter_simple.cc") continue;
|
2017-02-20 06:06:46 +00:00
|
|
|
//if (path != "tests/usage/type_usage_typedef_and_using.cc") continue;
|
2017-02-19 09:43:52 +00:00
|
|
|
//if (path != "tests/usage/type_usage_declare_local.cc") continue;
|
2017-02-20 00:56:56 +00:00
|
|
|
//if (path != "tests/usage/func_usage_addr_method.cc") continue;
|
|
|
|
//if (path != "tests/usage/func_usage_template_func.cc") continue;
|
2017-02-20 06:06:46 +00:00
|
|
|
//if (path != "tests/usage/usage_inside_of_call.cc") continue;
|
2017-02-20 02:00:58 +00:00
|
|
|
//if (path != "tests/foobar.cc") continue;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// Parse expected output from the test, parse it into JSON document.
|
|
|
|
std::string expected_output;
|
2017-02-17 09:57:44 +00:00
|
|
|
ParseTestExpectation(path, &expected_output);
|
2017-02-18 19:37:24 +00:00
|
|
|
rapidjson::Document expected;
|
|
|
|
expected.Parse(expected_output.c_str());
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
// Run test.
|
2017-02-17 09:57:44 +00:00
|
|
|
std::cout << "[START] " << path << std::endl;
|
|
|
|
ParsingDatabase db = Parse(path);
|
2017-02-18 23:58:40 +00:00
|
|
|
std::string actual_output = db.ToString();
|
2017-02-18 19:37:24 +00:00
|
|
|
rapidjson::Document actual;
|
|
|
|
actual.Parse(actual_output.c_str());
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-02-18 19:37:24 +00:00
|
|
|
if (actual == expected) {
|
2017-02-17 09:57:44 +00:00
|
|
|
std::cout << "[PASSED] " << path << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cout << "[FAILED] " << path << std::endl;
|
|
|
|
std::cout << "Expected output for " << path << ":" << std::endl;
|
2017-02-18 19:37:24 +00:00
|
|
|
std::cout << expected_output;
|
2017-02-17 09:57:44 +00:00
|
|
|
std::cout << "Actual output for " << path << ":" << std::endl;
|
2017-02-18 19:37:24 +00:00
|
|
|
std::cout << actual_output;
|
2017-02-18 23:58:40 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
DiffDocuments(expected, actual);
|
2017-02-17 09:57:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 09:35:30 +00:00
|
|
|
|
|
|
|
std::cin.get();
|
2017-02-17 09:57:44 +00:00
|
|
|
return 0;
|
2017-02-18 23:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 07:55:29 +00:00
|
|
|
// TODO: ctor/dtor, copy ctor
|