Use def.spell for semantic parent and def.extent for lexical parent

Use language identifier in the spec

Remove unreliable Boltzmann distribution
This commit is contained in:
Fangrui Song 2018-02-14 20:58:42 -08:00
parent d678322c39
commit afe86ef146
4 changed files with 89 additions and 70 deletions

View File

@ -446,42 +446,48 @@ std::string GetDocumentContentInRange(CXTranslationUnit cx_tu,
return result; return result;
} }
void SetUsePreflight(IndexFile* db, ClangCursor lex_parent) { void SetUsePreflight(IndexFile* db, ClangCursor parent) {
switch (GetSymbolKind(lex_parent.get_kind())) { switch (GetSymbolKind(parent.get_kind())) {
default: default:
break; break;
case SymbolKind::Func: { case SymbolKind::Func: {
(void)db->ToFuncId(lex_parent.cx_cursor); (void)db->ToFuncId(parent.cx_cursor);
break; break;
} }
case SymbolKind::Type: { case SymbolKind::Type: {
(void)db->ToTypeId(lex_parent.cx_cursor); (void)db->ToTypeId(parent.cx_cursor);
break; break;
} }
case SymbolKind::Var: { case SymbolKind::Var: {
(void)db->ToVarId(lex_parent.cx_cursor); (void)db->ToVarId(parent.cx_cursor);
break; break;
} }
} }
} }
void SetUse(IndexFile* db, Maybe<Use>* def, Range range, ClangCursor lex_parent, Role role) { // |parent| should be resolved before using |SetUsePreflight| so that |def| will
switch (GetSymbolKind(lex_parent.get_kind())) { // not be invalidated by |To{Func,Type,Var}Id|.
void SetUse(IndexFile* db,
Maybe<Use>* def,
Range range,
ClangCursor parent,
Role role) {
switch (GetSymbolKind(parent.get_kind())) {
default: default:
*def = Use(range, Id<void>(), SymbolKind::File, role); *def = Use(range, Id<void>(), SymbolKind::File, role);
break; break;
case SymbolKind::Func: { case SymbolKind::Func: {
IndexFuncId id = db->ToFuncId(lex_parent.cx_cursor); IndexFuncId id = db->ToFuncId(parent.cx_cursor);
*def = Use(range, id, SymbolKind::Func, Role::Definition); *def = Use(range, id, SymbolKind::Func, Role::Definition);
break; break;
} }
case SymbolKind::Type: { case SymbolKind::Type: {
IndexTypeId id = db->ToTypeId(lex_parent.cx_cursor); IndexTypeId id = db->ToTypeId(parent.cx_cursor);
*def = Use(range, id, SymbolKind::Type, Role::Definition); *def = Use(range, id, SymbolKind::Type, Role::Definition);
break; break;
} }
case SymbolKind::Var: { case SymbolKind::Var: {
IndexVarId id = db->ToVarId(lex_parent.cx_cursor); IndexVarId id = db->ToVarId(parent.cx_cursor);
*def = Use(range, id, SymbolKind::Var, Role::Definition); *def = Use(range, id, SymbolKind::Var, Role::Definition);
break; break;
} }
@ -500,7 +506,9 @@ void SetTypeName(IndexType* type,
name = "(anon)"; name = "(anon)";
if (!container) if (!container)
parent.cursor = cursor.get_semantic_parent().cx_cursor; parent.cursor = cursor.get_semantic_parent().cx_cursor;
// Investigate why clang_getCursorPrettyPrinted is not fully qualified. // Investigate why clang_getCursorPrettyPrinted gives `struct A {}` `namespace ns {}`
// which are not qualified.
//type->def.detailed_name = param->PrettyPrintCursor(cursor.cx_cursor);
type->def.detailed_name = type->def.detailed_name =
param->ns.QualifiedName(container ? container : &parent, name); param->ns.QualifiedName(container ? container : &parent, name);
auto idx = type->def.detailed_name.find(name); auto idx = type->def.detailed_name.find(name);
@ -1289,12 +1297,17 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
case CXCursor_DeclRefExpr: { case CXCursor_DeclRefExpr: {
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor); ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) { if (ref_cursor.get_kind() == CXCursor_NonTypeTemplateParameter) {
SetUsePreflight(db, parent); IndexVarId ref_var_id = db->ToVarId(ref_cursor.get_usr_hash());
IndexVar* ref_var = IndexVar* ref_var = db->Resolve(ref_var_id);
db->Resolve(db->ToVarId(ref_cursor.get_usr_hash()));
if (ref_var->def.detailed_name.empty()) { if (ref_var->def.detailed_name.empty()) {
SetUse(db, &ref_var->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); SetUsePreflight(db, ref_cursor.get_semantic_parent());
SetUse(db, &ref_var->def.extent, ref_cursor.get_extent(), parent, Role::None); SetUsePreflight(db, ref_cursor.get_lexical_parent());
ref_var = db->Resolve(ref_var_id);
SetUse(db, &ref_var->def.spell, ref_cursor.get_spelling_range(),
ref_cursor.get_semantic_parent(), Role::Definition);
SetUse(db, &ref_var->def.extent, ref_cursor.get_extent(),
ref_cursor.get_lexical_parent(), Role::None);
ref_var = db->Resolve(ref_var_id);
ref_var->def.kind = ClangSymbolKind::Parameter; ref_var->def.kind = ClangSymbolKind::Parameter;
SetVarDetail(ref_var, ref_cursor.get_spelling(), ref_cursor, SetVarDetail(ref_var, ref_cursor.get_spelling(), ref_cursor,
nullptr, true, db, param); nullptr, true, db, param);
@ -1338,56 +1351,64 @@ ClangCursor::VisitResult TemplateVisitor(ClangCursor cursor,
case CXCursor_TemplateRef: { case CXCursor_TemplateRef: {
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor); ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
if (ref_cursor.get_kind() == CXCursor_TemplateTemplateParameter) { if (ref_cursor.get_kind() == CXCursor_TemplateTemplateParameter) {
SetUsePreflight(db, parent); IndexTypeId ref_type_id = db->ToTypeId(ref_cursor.get_usr_hash());
IndexType* ref_index = IndexType* ref_type = db->Resolve(ref_type_id);
db->Resolve(db->ToTypeId(ref_cursor.get_usr_hash()));
// TODO It seems difficult to get references to template template // TODO It seems difficult to get references to template template
// parameters. // parameters.
// CXCursor_TemplateTemplateParameter can be visited by visiting // CXCursor_TemplateTemplateParameter can be visited by visiting
// CXCursor_TranslationUnit, but not (confirm this) by visiting // CXCursor_TranslationUnit, but not (confirm this) by visiting
// {Class,Function}Template. Thus we need to initialize it here. // {Class,Function}Template. Thus we need to initialize it here.
if (ref_index->def.detailed_name.empty()) { if (ref_type->def.detailed_name.empty()) {
SetUse(db, &ref_index->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); SetUsePreflight(db, ref_cursor.get_semantic_parent());
SetUse(db, &ref_index->def.extent, ref_cursor.get_extent(), parent, Role::None); SetUsePreflight(db, ref_cursor.get_lexical_parent());
ref_type = db->Resolve(ref_type_id);
SetUse(db, &ref_type->def.spell, ref_cursor.get_spelling_range(),
ref_cursor.get_semantic_parent(), Role::Definition);
SetUse(db, &ref_type->def.extent, ref_cursor.get_extent(),
ref_cursor.get_lexical_parent(), Role::None);
#if CINDEX_HAVE_PRETTY #if CINDEX_HAVE_PRETTY
ref_index->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor); ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
#else #else
ref_index->def.detailed_name = ref_cursor.get_spelling(); ref_type->def.detailed_name = ref_cursor.get_spelling();
#endif #endif
ref_index->def.short_name_offset = 0; ref_type->def.short_name_offset = 0;
ref_index->def.short_name_size = ref_type->def.short_name_size =
int16_t(strlen(ref_index->def.detailed_name.c_str())); int16_t(strlen(ref_type->def.detailed_name.c_str()));
ref_index->def.kind = ClangSymbolKind::Parameter; ref_type->def.kind = ClangSymbolKind::Parameter;
} }
UniqueAddUseSpell(db, ref_index->uses, cursor); UniqueAddUseSpell(db, ref_type->uses, cursor);
} }
break; break;
} }
case CXCursor_TypeRef: { case CXCursor_TypeRef: {
ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor); ClangCursor ref_cursor = clang_getCursorReferenced(cursor.cx_cursor);
if (ref_cursor.get_kind() == CXCursor_TemplateTypeParameter) { if (ref_cursor.get_kind() == CXCursor_TemplateTypeParameter) {
SetUsePreflight(db, parent); IndexTypeId ref_type_id = db->ToTypeId(ref_cursor.get_usr_hash());
IndexType* ref_index = IndexType* ref_type = db->Resolve(ref_type_id);
db->Resolve(db->ToTypeId(ref_cursor.get_usr_hash()));
// TODO It seems difficult to get a FunctionTemplate's template // TODO It seems difficult to get a FunctionTemplate's template
// parameters. // parameters.
// CXCursor_TemplateTypeParameter can be visited by visiting // CXCursor_TemplateTypeParameter can be visited by visiting
// CXCursor_TranslationUnit, but not (confirm this) by visiting // CXCursor_TranslationUnit, but not (confirm this) by visiting
// {Class,Function}Template. Thus we need to initialize it here. // {Class,Function}Template. Thus we need to initialize it here.
if (ref_index->def.detailed_name.empty()) { if (ref_type->def.detailed_name.empty()) {
SetUse(db, &ref_index->def.spell, ref_cursor.get_spelling_range(), parent, Role::Definition); SetUsePreflight(db, ref_cursor.get_semantic_parent());
SetUse(db, &ref_index->def.extent, ref_cursor.get_extent(), parent, Role::None); SetUsePreflight(db, ref_cursor.get_lexical_parent());
ref_type = db->Resolve(ref_type_id);
SetUse(db, &ref_type->def.spell, ref_cursor.get_spelling_range(),
ref_cursor.get_semantic_parent(), Role::Definition);
SetUse(db, &ref_type->def.extent, ref_cursor.get_extent(),
ref_cursor.get_lexical_parent(), Role::None);
#if CINDEX_HAVE_PRETTY #if CINDEX_HAVE_PRETTY
ref_index->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor); ref_type->def.detailed_name = param->PrettyPrintCursor(ref_cursor.cx_cursor);
#else #else
ref_index->def.detailed_name = ref_cursor.get_spelling(); ref_type->def.detailed_name = ref_cursor.get_spelling();
#endif #endif
ref_index->def.short_name_offset = 0; ref_type->def.short_name_offset = 0;
ref_index->def.short_name_size = ref_type->def.short_name_size =
int16_t(strlen(ref_index->def.detailed_name.c_str())); int16_t(strlen(ref_type->def.detailed_name.c_str()));
ref_index->def.kind = ClangSymbolKind::Parameter; ref_type->def.kind = ClangSymbolKind::Parameter;
} }
UniqueAddUseSpell(db, ref_index->uses, cursor); UniqueAddUseSpell(db, ref_type->uses, cursor);
} }
break; break;
} }
@ -1491,7 +1512,9 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
} }
NamespaceHelper* ns = &param->ns; NamespaceHelper* ns = &param->ns;
ClangCursor sem_parent(fromContainer(decl->semanticContainer));
ClangCursor lex_parent(fromContainer(decl->lexicalContainer)); ClangCursor lex_parent(fromContainer(decl->lexicalContainer));
SetUsePreflight(db, sem_parent);
SetUsePreflight(db, lex_parent); SetUsePreflight(db, lex_parent);
switch (decl->entityInfo->kind) { switch (decl->entityInfo->kind) {
@ -1504,7 +1527,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
if (ns->def.detailed_name.empty()) { if (ns->def.detailed_name.empty()) {
SetTypeName(ns, decl_cursor, decl->semanticContainer, SetTypeName(ns, decl_cursor, decl->semanticContainer,
decl->entityInfo->name, param); decl->entityInfo->name, param);
SetUse(db, &ns->def.spell, decl_spell, lex_parent, Role::Definition); SetUse(db, &ns->def.spell, decl_spell, sem_parent, Role::Definition);
SetUse(db, &ns->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); SetUse(db, &ns->def.extent, decl_cursor.get_extent(), lex_parent, Role::None);
if (decl->semanticContainer) { if (decl->semanticContainer) {
IndexTypeId parent_id = db->ToTypeId( IndexTypeId parent_id = db->ToTypeId(
@ -1551,7 +1574,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
//} //}
if (decl->isDefinition) { if (decl->isDefinition) {
SetUse(db, &var->def.spell, decl_spell, lex_parent, Role::Definition); SetUse(db, &var->def.spell, decl_spell, sem_parent, Role::Definition);
SetUse(db, &var->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); SetUse(db, &var->def.extent, decl_cursor.get_extent(), lex_parent, Role::None);
} else { } else {
Maybe<Use> use; Maybe<Use> use;
@ -1625,7 +1648,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
if (decl->isDefinition && !is_template_specialization) { if (decl->isDefinition && !is_template_specialization) {
// assert(!func->def.spell); // assert(!func->def.spell);
// assert(!func->def.extent); // assert(!func->def.extent);
SetUse(db, &func->def.spell, decl_spelling, lex_parent, Role::Definition); SetUse(db, &func->def.spell, decl_spelling, sem_parent, Role::Definition);
SetUse(db, &func->def.extent, decl_extent, lex_parent, Role::None); SetUse(db, &func->def.extent, decl_extent, lex_parent, Role::None);
} else { } else {
IndexFunc::Declaration declaration; IndexFunc::Declaration declaration;
@ -1754,7 +1777,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
ClangCursor decl_cursor = decl->cursor; ClangCursor decl_cursor = decl->cursor;
Range spell = decl_cursor.get_spelling_range(); Range spell = decl_cursor.get_spelling_range();
Range extent = decl_cursor.get_extent(); Range extent = decl_cursor.get_extent();
SetUse(db, &type->def.spell, spell, lex_parent, Role::Definition); SetUse(db, &type->def.spell, spell, sem_parent, Role::Definition);
SetUse(db, &type->def.extent, extent, lex_parent, Role::None); SetUse(db, &type->def.extent, extent, lex_parent, Role::None);
SetTypeName(type, decl_cursor, decl->semanticContainer, SetTypeName(type, decl_cursor, decl->semanticContainer,
@ -1810,7 +1833,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
// } // }
if (decl->isDefinition) { if (decl->isDefinition) {
SetUse(db, &type->def.spell, decl_spell, lex_parent, Role::Definition); SetUse(db, &type->def.spell, decl_spell, sem_parent, Role::Definition);
SetUse(db, &type->def.extent, decl_cursor.get_extent(), lex_parent, Role::None); SetUse(db, &type->def.extent, decl_cursor.get_extent(), lex_parent, Role::None);
if (decl_cursor.get_kind() == CXCursor_EnumDecl) { if (decl_cursor.get_kind() == CXCursor_EnumDecl) {
@ -1848,9 +1871,17 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
} }
// TODO The name may be assigned in |ResolveToDeclarationType| but // TODO The name may be assigned in |ResolveToDeclarationType| but
// |spell| is nullopt. // |spell| is nullopt.
if (!origin->def.spell) { CXFile origin_file;
SetUse(db, &origin->def.spell, origin_cursor.get_spelling_range(), lex_parent, Role::Definition); Range origin_spell = origin_cursor.get_spelling_range(&origin_file);
SetUse(db, &origin->def.extent, origin_cursor.get_extent(), lex_parent, Role::None); if (!origin->def.spell && file == origin_file) {
SetUsePreflight(db, origin_cursor.get_semantic_parent());
SetUsePreflight(db, origin_cursor.get_lexical_parent());
origin = db->Resolve(origin_id);
type = db->Resolve(type_id);
SetUse(db, &origin->def.spell, origin_spell,
origin_cursor.get_semantic_parent(), Role::Definition);
SetUse(db, &origin->def.extent, origin_cursor.get_extent(),
origin_cursor.get_lexical_parent(), Role::None);
} }
origin->derived.push_back(type_id); origin->derived.push_back(type_id);
type->def.parents.push_back(origin_id); type->def.parents.push_back(origin_id);

View File

@ -351,9 +351,7 @@ struct IndexFunc {
std::vector<Use> uses; std::vector<Use> uses;
IndexFunc() {} // For serialization. IndexFunc() {} // For serialization.
IndexFunc(IndexFuncId id, Usr usr) : usr(usr), id(id) { IndexFunc(IndexFuncId id, Usr usr) : usr(usr), id(id) {}
// assert(usr.size() > 0);
}
bool operator<(const IndexFunc& other) const { return id < other.id; } bool operator<(const IndexFunc& other) const { return id < other.id; }
}; };
@ -438,9 +436,7 @@ struct IndexVar {
std::vector<Use> uses; std::vector<Use> uses;
IndexVar() {} // For serialization. IndexVar() {} // For serialization.
IndexVar(IndexVarId id, Usr usr) : usr(usr), id(id) { IndexVar(IndexVarId id, Usr usr) : usr(usr), id(id) {}
// assert(usr.size() > 0);
}
bool operator<(const IndexVar& other) const { return id < other.id; } bool operator<(const IndexVar& other) const { return id < other.id; }
}; };
@ -469,7 +465,7 @@ struct IndexInclude {
// Used to identify the language at a file level. The ordering is important, as // Used to identify the language at a file level. The ordering is important, as
// a file previously identified as `C`, will be changed to `Cpp` if it // a file previously identified as `C`, will be changed to `Cpp` if it
// encounters a c++ declaration. // encounters a c++ declaration.
enum class LanguageId { Unknown = 0, C = 1, Cpp = 2, ObjC = 3 }; enum class LanguageId { Unknown = 0, C = 1, Cpp = 2, ObjC = 3, ObjCpp = 4 };
MAKE_REFLECT_TYPE_PROXY(LanguageId); MAKE_REFLECT_TYPE_PROXY(LanguageId);
struct IndexFile { struct IndexFile {

View File

@ -15,7 +15,6 @@ REGISTER_IPC_MESSAGE(Ipc_CqueryRandom);
const double kDeclWeight = 3; const double kDeclWeight = 3;
const double kDamping = 0.1; const double kDamping = 0.1;
const double kAlpha = 2.80777024202851936522;
template <typename Q> template <typename Q>
struct Kind; struct Kind;
@ -102,7 +101,7 @@ struct CqueryRandomHandler : BaseMessageHandler<Ipc_CqueryRandom> {
} }
std::vector<double> x(n, 1), y; std::vector<double> x(n, 1), y;
while (1) { for (int j = 0; j < 30; j++) {
y.assign(n, kDamping); y.assign(n, kDamping);
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
for (auto& it : adj[i]) for (auto& it : adj[i])
@ -114,16 +113,7 @@ struct CqueryRandomHandler : BaseMessageHandler<Ipc_CqueryRandom> {
x.swap(y); x.swap(y);
} }
double sum = std::accumulate(x.begin(), x.end(), 0.), stdev = 0, offset = 0; double sum = std::accumulate(x.begin(), x.end(), 0.);
for (int i = 0; i < n; i++)
stdev += (x[i] - sum / n) * (x[i] - sum / n);
stdev = sqrt(stdev / n) * kAlpha;
for (int i = 0; i < n; i++)
offset = std::max(offset, x[i] / stdev);
sum = 0;
for (int i = 0; i < n; i++)
sum += x[i] = exp(x[i] / stdev - offset);
Out_LocationList out; Out_LocationList out;
out.id = request->id; out.id = request->id;
double roulette = rand() / (RAND_MAX + 1.0) * sum; double roulette = rand() / (RAND_MAX + 1.0) * sum;

View File

@ -223,7 +223,9 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in
case LanguageId::Cpp: case LanguageId::Cpp:
return "cpp"; return "cpp";
case LanguageId::ObjC: case LanguageId::ObjC:
return "objectivec"; return "objective-c";
case LanguageId::ObjCpp:
return "objective-cpp";
default: default:
return ""; return "";
} }