From 547ef3fc3d97dc238db6e4f7d9f8630168ba80f4 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Thu, 6 Apr 2017 23:10:17 -0700 Subject: [PATCH] simplify position part1 --- src/indexer.cpp | 10 ++--- src/position.cc | 98 +++++++++++++++++++++++++++---------------------- src/position.h | 13 ++++--- src/query.cc | 40 ++------------------ 4 files changed, 72 insertions(+), 89 deletions(-) diff --git a/src/indexer.cpp b/src/indexer.cpp index 5fae5568..b7a7acdd 100644 --- a/src/indexer.cpp +++ b/src/indexer.cpp @@ -93,8 +93,8 @@ void AddUsage(std::vector& uses, // First thought makes me think no, we don't. for (int i = uses.size() - 1; i >= 0; --i) { if (uses[i].start == loc.start) { - if (loc.start.interesting) - uses[i].start.interesting = true; + if (loc.interesting) + uses[i].interesting = true; return; } } @@ -115,9 +115,9 @@ Range IdCache::ForceResolve(const CXSourceRange& range, bool interesting) { unsigned int end_line, end_column; clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr); - return Range( - Position(interesting, start_line, start_column) /*start*/, - Position(interesting, end_line, end_column) /*end*/); + return Range(interesting, + Position(start_line, start_column) /*start*/, + Position(end_line, end_column) /*end*/); } Range IdCache::ForceResolveSpelling(const CXCursor& cx_cursor, bool interesting) { diff --git a/src/position.cc b/src/position.cc index 88f90b32..f58723f5 100644 --- a/src/position.cc +++ b/src/position.cc @@ -1,23 +1,25 @@ #include "position.h" +namespace { +// Skips until the character immediately following |skip_after|. +const char* SkipAfter(const char* input, char skip_after) { + while (*input && *input != skip_after) + ++input; + ++input; + return input; +} +} // namespace + Position::Position() {} -Position::Position(bool interesting, int32_t line, int32_t column) - : interesting(interesting), line(line), column(column) {} +Position::Position(int32_t line, int32_t column) + : line(line), column(column) {} Position::Position(const char* encoded) { - if (*encoded == '*') { - interesting = true; - ++encoded; - } - assert(encoded); line = atoi(encoded); - while (*encoded && *encoded != ':') - ++encoded; - if (*encoded == ':') - ++encoded; + encoded = SkipAfter(encoded, ':'); assert(encoded); column = atoi(encoded); } @@ -25,15 +27,12 @@ Position::Position(const char* encoded) { std::string Position::ToString() { // Output looks like this: // - // *1:2 + // 1:2 // - // * => interesting // 1 => line // 2 => column std::string result; - if (interesting) - result += '*'; result += std::to_string(line); result += ':'; result += std::to_string(column); @@ -43,15 +42,13 @@ std::string Position::ToString() { std::string Position::ToPrettyString(const std::string& filename) { // Output looks like this: // - // *1:2 + // 1:2:3 // - // * => interesting - // 1 => line - // 2 => column + // 1 => filename + // 2 => line + // 3 => column std::string result; - if (interesting) - result += '*'; result += filename; result += ':'; result += std::to_string(line); @@ -60,12 +57,6 @@ std::string Position::ToPrettyString(const std::string& filename) { return result; } -Position Position::WithInteresting(bool interesting) { - Position result = *this; - result.interesting = interesting; - return result; -} - bool Position::operator==(const Position& that) const { return line == that.line && column == that.column; } @@ -73,45 +64,66 @@ bool Position::operator==(const Position& that) const { bool Position::operator!=(const Position& that) const { return !(*this == that); } bool Position::operator<(const Position& that) const { - return interesting < that.interesting && line < that.line && column < that.column; + return line < that.line && column < that.column; } Range::Range() {} -Range::Range(Position start, Position end) : start(start), end(end) {} +Range::Range(bool interesting, Position start, Position end) : interesting(interesting), start(start), end(end) {} -Range::Range(const char* encoded) : start(encoded) { +Range::Range(const char* encoded) { end = start; + if (*encoded == '*') { + interesting = true; + ++encoded; + } + + start.line = atoi(encoded); + + encoded = SkipAfter(encoded, ':'); + assert(encoded); + start.column = atoi(encoded); + + encoded = SkipAfter(encoded, '-'); assert(encoded); - while (*encoded && *encoded != '-') - ++encoded; - if (*encoded == '-') - ++encoded; end.line = atoi(encoded); + encoded = SkipAfter(encoded, ':'); assert(encoded); - while (*encoded && *encoded != ':') - ++encoded; - if (*encoded == ':') - ++encoded; end.column = atoi(encoded); } std::string Range::ToString() { - std::string output; - output += start.ToString(); + // Output looks like this: + // + // *1:2-3:4 + // + // * => if present, range is interesting + // 1 => start line + // 2 => start column + // 3 => end line + // 4 => end column - output += "-"; + std::string output; + + if (interesting) + output += '*'; + output += std::to_string(start.line); + output += ':'; + output += std::to_string(start.column); + output += '-'; output += std::to_string(end.line); - output += ":"; + output += ':'; output += std::to_string(end.column); return output; } Range Range::WithInteresting(bool interesting) { - return Range(start.WithInteresting(interesting), end.WithInteresting(interesting)); + Range result = *this; + result.interesting = interesting; + return result; } bool Range::operator==(const Range& that) const { diff --git a/src/position.h b/src/position.h index 734ca960..4880c8ac 100644 --- a/src/position.h +++ b/src/position.h @@ -5,19 +5,16 @@ #include struct Position { - bool interesting = false; int32_t line = -1; int32_t column = -1; Position(); - Position(bool interesting, int32_t line, int32_t column); + Position(int32_t line, int32_t column); explicit Position(const char* encoded); std::string ToString(); std::string ToPrettyString(const std::string& filename); - Position WithInteresting(bool interesting); - // Compare two Positions and check if they are equal. Ignores the value of // |interesting|. bool operator==(const Position& that) const; @@ -26,11 +23,12 @@ struct Position { }; struct Range { + bool interesting = false; Position start; Position end; Range(); - Range(Position start, Position end); + Range(bool interesting, Position start, Position end); explicit Range(const char* encoded); std::string ToString(); @@ -39,4 +37,9 @@ struct Range { bool operator==(const Range& that) const; bool operator!=(const Range& that) const; bool operator<(const Range& that) const; +}; + +struct Location { + std::string path; + Range range; }; \ No newline at end of file diff --git a/src/query.cc b/src/query.cc index 66eabd14..589c3592 100644 --- a/src/query.cc +++ b/src/query.cc @@ -40,8 +40,8 @@ Usr MapIdToUsr(const IdCache& id_cache, const VarId& id) { return id_cache.var_id_to_usr.find(id)->second; } QueryableRange MapIdToUsr(const IdCache& id_cache, const Range& id) { - QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.start.interesting); - QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.end.interesting); + QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); + QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting); return QueryableRange(start, end); //assert(id_cache.file_id_to_file_path.find(id.file_id()) != id_cache.file_id_to_file_path.end()); //return QueryableLocation(id_cache.file_id_to_file_path.find(id.file_id())->second, id.line, id.column, id.interesting); @@ -77,8 +77,8 @@ std::vector MapIdToUsr(const IdCache& id_cache, const std::vector MapIdToUsr(const IdCache& id_cache, const std::vector& ids) { return Transform(ids, [&](Range id) { - QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.start.interesting); - QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.end.interesting); + QueryableLocation start(id_cache.primary_file, id.start.line, id.start.column, id.interesting); + QueryableLocation end(id_cache.primary_file, id.end.line, id.end.column, id.interesting); return QueryableRange(start, end); }); } @@ -379,38 +379,6 @@ void CompareGroups( -#if false -IndexUpdate::IndexUpdate(IndexedFile& file) { - // TODO: use delta constructor with an empty file. - - auto file_def = BuildFileDef(file); - files_def_update.push_back(file_def); - - for (const IndexedTypeDef& indexed : file.types) { - QueryableTypeDef query(file.id_cache, indexed); - types_def_update.push_back(query.def); - types_derived.push_back(QueryableTypeDef::DerivedUpdate(query.def.usr, query.derived)); - types_instantiations.push_back(QueryableTypeDef::InstantiationsUpdate(query.def.usr, query.instantiations)); - types_uses.push_back(QueryableTypeDef::UsesUpdate(query.def.usr, query.uses)); - } - - for (const IndexedFuncDef& indexed : file.funcs) { - QueryableFuncDef query(file.id_cache, indexed); - funcs_def_update.push_back(query.def); - funcs_declarations.push_back(QueryableFuncDef::DeclarationsUpdate(query.def.usr, query.declarations)); - funcs_derived.push_back(QueryableFuncDef::DerivedUpdate(query.def.usr, query.derived)); - funcs_callers.push_back(QueryableFuncDef::CallersUpdate(query.def.usr, query.callers)); - funcs_uses.push_back(QueryableFuncDef::UsesUpdate(query.def.usr, query.uses)); - } - - for (const IndexedVarDef& indexed : file.vars) { - QueryableVarDef query(file.id_cache, indexed); - vars_def_update.push_back(query.def); - vars_uses.push_back(QueryableVarDef::UsesUpdate(query.def.usr, query.uses)); - } -} -#endif - // static IndexUpdate IndexUpdate::CreateImport(IndexedFile& file) { // Return standard diff constructor but with an empty file so everything is