From d573a681309b0bebc9705e69ff76fe70e955778e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 4 Feb 2018 10:03:24 -0800 Subject: [PATCH] Simplify operator== --- src/indexer.h | 107 +++++++++++++++++--------------------------------- src/query.h | 66 +++++++++++-------------------- wscript | 2 +- 3 files changed, 61 insertions(+), 114 deletions(-) diff --git a/src/indexer.h b/src/indexer.h index d7fbf647..016d2b2f 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -53,9 +53,9 @@ struct Id { bool HasValue() const { return id != RawId(-1); } - bool operator==(const Id& other) const { return id == other.id; } - - bool operator<(const Id& other) const { return id < other.id; } + bool operator==(const Id& o) const { return id == o.id; } + bool operator!=(const Id& o) const { return id != o.id; } + bool operator<(const Id& o) const { return id < o.id; } }; namespace std { @@ -65,23 +65,9 @@ struct hash> { }; } // namespace std -template -bool operator==(const Id& a, const Id& b) { - assert(a.group == b.group && "Cannot compare Ids from different groups"); - return a.id == b.id; -} -template -bool operator!=(const Id& a, const Id& b) { - return !(a == b); -} - -template -void Reflect(Reader& visitor, Id& id) { - id.id = visitor.GetUint64(); -} -template -void Reflect(Writer& visitor, Id& value) { - visitor.Uint64(value.id); +template +void Reflect(TVisitor& visitor, Id& id) { + Reflect(visitor, id.id); } using IndexTypeId = Id; @@ -103,29 +89,16 @@ struct IndexFuncRef { IndexFuncRef(Range loc, bool is_implicit) : loc(loc), is_implicit(is_implicit) {} - inline bool operator==(const IndexFuncRef& other) { - return id == other.id && loc == other.loc && - is_implicit == other.is_implicit; + std::tuple ToTuple() const { + return {id, loc, is_implicit}; } - inline bool operator!=(const IndexFuncRef& other) { - return !(*this == other); - } - inline bool operator<(const IndexFuncRef& other) const { - if (id != other.id) - return id < other.id; - if (loc != other.loc) - return loc < other.loc; - return is_implicit < other.is_implicit; + bool operator==(const IndexFuncRef& o) { return ToTuple() == o.ToTuple(); } + bool operator!=(const IndexFuncRef& o) { return !(*this == o); } + bool operator<(const IndexFuncRef& o) const { + return ToTuple() < o.ToTuple(); } }; -inline bool operator==(const IndexFuncRef& a, const IndexFuncRef& b) { - return a.id == b.id && a.loc == b.loc; -} -inline bool operator!=(const IndexFuncRef& a, const IndexFuncRef& b) { - return !(a == b); -} - inline void Reflect(Reader& visitor, IndexFuncRef& value) { std::string s = visitor.GetString(); const char* str_value = s.c_str(); @@ -190,19 +163,16 @@ struct TypeDefDefinitionData { int16_t short_name_size = 0; ClangSymbolKind kind = ClangSymbolKind::Unknown; - bool operator==( - const TypeDefDefinitionData& other) const { - return detailed_name == other.detailed_name && - definition_spelling == other.definition_spelling && - definition_extent == other.definition_extent && - alias_of == other.alias_of && parents == other.parents && - types == other.types && funcs == other.funcs && vars == other.vars && - hover == other.hover && comments == other.comments; + bool operator==(const TypeDefDefinitionData& o) const { + return detailed_name == o.detailed_name && + definition_spelling == o.definition_spelling && + definition_extent == o.definition_extent && alias_of == o.alias_of && + parents == o.parents && types == o.types && funcs == o.funcs && + vars == o.vars && kind == o.kind && hover == o.hover && + kind == o.kind && hover == o.hover && comments == o.comments; } - - bool operator!=( - const TypeDefDefinitionData& other) const { - return !(*this == other); + bool operator!=(const TypeDefDefinitionData& o) const { + return !(*this == o); } std::string_view ShortName() const { @@ -290,20 +260,16 @@ struct FuncDefDefinitionData { ClangSymbolKind kind = ClangSymbolKind::Unknown; StorageClass storage = StorageClass::Invalid; - bool operator==( - const FuncDefDefinitionData& other) - const { - return detailed_name == other.detailed_name && hover == other.hover && - definition_spelling == other.definition_spelling && - definition_extent == other.definition_extent && - declaring_type == other.declaring_type && base == other.base && - locals == other.locals && callees == other.callees && - hover == other.hover && comments == other.comments; + bool operator==(const FuncDefDefinitionData& o) const { + return detailed_name == o.detailed_name && + definition_spelling == o.definition_spelling && + definition_extent == o.definition_extent && + declaring_type == o.declaring_type && base == o.base && + locals == o.locals && callees == o.callees && kind == o.kind && + storage == o.storage && hover == o.hover && comments == o.comments; } - bool operator!=( - const FuncDefDefinitionData& other) - const { - return !(*this == other); + bool operator!=(const FuncDefDefinitionData& o) const { + return !(*this == o); } std::string_view ShortName() const { @@ -419,12 +385,13 @@ struct VarDefDefinitionData { } bool is_macro() const { return kind == ClangSymbolKind::Macro; } - bool operator==( - const VarDefDefinitionData& other) const { - return detailed_name == other.detailed_name && hover == other.hover && - definition_spelling == other.definition_spelling && - definition_extent == other.definition_extent && - variable_type == other.variable_type && comments == other.comments; + bool operator==(const VarDefDefinitionData& o) const { + return detailed_name == o.detailed_name && + definition_spelling == o.definition_spelling && + definition_extent == o.definition_extent && + variable_type == o.variable_type && parent_id == o.parent_id && + parent_kind == o.parent_kind && kind == o.kind && + storage == o.storage && hover == o.hover && comments == o.comments; } bool operator!=( const VarDefDefinitionData& other) const { diff --git a/src/query.h b/src/query.h index 18d700ec..813f566e 100644 --- a/src/query.h +++ b/src/query.h @@ -62,12 +62,10 @@ struct QueryLocation { bool HasValue() const { return range.HasValue(); } - bool operator==(const QueryLocation& other) const { - return path == other.path && range == other.range; - } - bool operator!=(const QueryLocation& other) const { - return !(*this == other); + bool operator==(const QueryLocation& o) const { + return path == o.path && range == o.range; } + bool operator!=(const QueryLocation& o) const { return !(*this == o); } bool operator<(const QueryLocation& o) const { if (path != o.path) return path < o.path; @@ -95,14 +93,14 @@ struct SymbolIdx { idx(RawId(-1)) {} // Default ctor needed by stdlib. Do not use. SymbolIdx(SymbolKind kind, RawId idx) : kind(kind), idx(idx) {} - bool operator==(const SymbolIdx& that) const { - return kind == that.kind && idx == that.idx; + bool operator==(const SymbolIdx& o) const { + return kind == o.kind && idx == o.idx; } - bool operator!=(const SymbolIdx& that) const { return !(*this == that); } - bool operator<(const SymbolIdx& that) const { - if (kind != that.kind) - return kind < that.kind; - return idx < that.idx; + bool operator!=(const SymbolIdx& o) const { return !(*this == o); } + bool operator<(const SymbolIdx& o) const { + if (kind != o.kind) + return kind < o.kind; + return idx < o.idx; } }; MAKE_REFLECT_STRUCT(SymbolIdx, kind, idx); @@ -117,15 +115,12 @@ struct SymbolRef { SymbolRef(SymbolIdx idx, SymbolRole role, QueryLocation loc) : idx(idx), role(role), loc(loc) {} - bool operator==(const SymbolRef& that) const { - return idx == that.idx && loc == that.loc; - } - bool operator!=(const SymbolRef& that) const { return !(*this == that); } - bool operator<(const SymbolRef& that) const { - if (idx != that.idx) - return idx < that.idx; - return loc < that.loc; + std::tuple ToTuple() const { + return {idx, role, loc}; } + bool operator==(const SymbolRef& o) const { return ToTuple() == o.ToTuple(); } + bool operator!=(const SymbolRef& o) const { return !(*this == o); } + bool operator<(const SymbolRef& o) const { return ToTuple() < o.ToTuple(); } }; MAKE_REFLECT_STRUCT(SymbolRef, idx, loc); @@ -141,17 +136,15 @@ struct QueryFuncRef { QueryFuncRef(QueryFuncId id, QueryLocation loc, bool is_implicit) : id_(id), loc(loc), is_implicit(is_implicit) {} - bool operator==(const QueryFuncRef& that) const { - return id_ == that.id_ && loc == that.loc && - is_implicit == that.is_implicit; + std::tuple ToTuple() const { + return {id_, loc, is_implicit}; } - bool operator!=(const QueryFuncRef& that) const { return !(*this == that); } - bool operator<(const QueryFuncRef& that) const { - if (id_ != that.id_) - return id_ < that.id_; - if (loc != that.loc) - return loc < that.loc; - return is_implicit < that.is_implicit; + bool operator==(const QueryFuncRef& o) const { + return ToTuple() == o.ToTuple(); + } + bool operator!=(const QueryFuncRef& o) const { return !(*this == o); } + bool operator<(const QueryFuncRef& o) const { + return ToTuple() < o.ToTuple(); } }; MAKE_REFLECT_STRUCT(QueryFuncRef, id_, loc, is_implicit); @@ -176,23 +169,10 @@ struct MergeableUpdate { MergeableUpdate(TId id, const std::vector& to_add) : id(id), to_add(to_add) {} - MergeableUpdate(TId id, const std::vector>& to_add) : id(id) { - for (auto& x : to_add) - this->to_add.push_back(x.value); - } MergeableUpdate(TId id, const std::vector& to_add, const std::vector& to_remove) : id(id), to_add(to_add), to_remove(to_remove) {} - MergeableUpdate(TId id, - const std::vector>& to_add, - const std::vector>& to_remove) - : id(id) { - for (auto& x : to_add) - this->to_add.push_back(x.value); - for (auto& x : to_remove) - this->to_remove.push_back(x.value); - } }; template void Reflect(TVisitor& visitor, MergeableUpdate& value) { diff --git a/wscript b/wscript index dded52aa..462b8f9f 100644 --- a/wscript +++ b/wscript @@ -142,7 +142,7 @@ def configure(ctx): # /wd4722: ignores warning C4722 (destructor never returns) in loguru # /wd4267: ignores warning C4267 (conversion from 'size_t' to 'type'), roughly -Wno-sign-compare # /MD: use multithread c library from DLL - cxxflags = ['/nologo', '/FS', '/EHsc', '/Zi', '/W3', '/WX', '/wd4996', '/wd4722', '/wd4267', '/wd4800', '/MD'] + cxxflags = ['/nologo', '/FS', '/EHsc', '/Zi', '/W3', '/wd4996', '/wd4722', '/wd4267', '/wd4800', '/MD'] if 'release' in ctx.options.variant: cxxflags.append('/O2') # There is no O3 else: