From 05e4af0b92f8b1febeb5abafd1f128cb5c3e00a4 Mon Sep 17 00:00:00 2001 From: Edwin Vane Date: Fri, 16 Aug 2013 12:18:53 +0000 Subject: [PATCH] Tweak Replacement comparisons * Introduce operator< to replace Replacement::Less * Make operator== and operator< on Replacements non-member functions * Change order of comparisons in operator< to do string comparisons last git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188550 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Tooling/Refactoring.h | 16 +++++++--------- lib/Tooling/Refactoring.cpp | 27 ++++++++++++++------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/include/clang/Tooling/Refactoring.h b/include/clang/Tooling/Refactoring.h index 7ebac33a9a..f5b621154f 100644 --- a/include/clang/Tooling/Refactoring.h +++ b/include/clang/Tooling/Refactoring.h @@ -114,14 +114,6 @@ public: /// \brief Returns a human readable string representation. std::string toString() const; - /// \brief Comparator to be able to use Replacement in std::set for uniquing. - class Less { - public: - bool operator()(const Replacement &R1, const Replacement &R2) const; - }; - - bool operator==(const Replacement &Other) const; - private: void setFromSourceLocation(SourceManager &Sources, SourceLocation Start, unsigned Length, StringRef ReplacementText); @@ -133,9 +125,15 @@ public: std::string ReplacementText; }; +/// \brief Less-than operator between two Replacements. +bool operator<(const Replacement &LHS, const Replacement &RHS); + +/// \brief Equal-to operator between two Replacements. +bool operator==(const Replacement &LHS, const Replacement &RHS); + /// \brief A set of Replacements. /// FIXME: Change to a vector and deduplicate in the RefactoringTool. -typedef std::set Replacements; +typedef std::set Replacements; /// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. /// diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp index bc94a2b2c1..7a8203f93d 100644 --- a/lib/Tooling/Refactoring.cpp +++ b/lib/Tooling/Refactoring.cpp @@ -80,20 +80,21 @@ std::string Replacement::toString() const { return result; } -bool Replacement::Less::operator()(const Replacement &R1, - const Replacement &R2) const { - if (R1.FilePath != R2.FilePath) return R1.FilePath < R2.FilePath; - if (R1.ReplacementRange.getOffset() != R2.ReplacementRange.getOffset()) - return R1.ReplacementRange.getOffset() < R2.ReplacementRange.getOffset(); - if (R1.ReplacementRange.getLength() != R2.ReplacementRange.getLength()) - return R1.ReplacementRange.getLength() < R2.ReplacementRange.getLength(); - return R1.ReplacementText < R2.ReplacementText; +bool operator<(const Replacement &LHS, const Replacement &RHS) { + if (LHS.getOffset() != RHS.getOffset()) + return LHS.getOffset() < RHS.getOffset(); + if (LHS.getLength() != RHS.getLength()) + return LHS.getLength() < RHS.getLength(); + if (LHS.getFilePath() != RHS.getFilePath()) + return LHS.getFilePath() < RHS.getFilePath(); + return LHS.getReplacementText() < RHS.getReplacementText(); } -bool Replacement::operator==(const Replacement &Other) const { - return ReplacementRange.getOffset() == Other.ReplacementRange.getOffset() && - ReplacementRange.getLength() == Other.ReplacementRange.getLength() && - FilePath == Other.FilePath && ReplacementText == Other.ReplacementText; +bool operator==(const Replacement &LHS, const Replacement &RHS) { + return LHS.getOffset() == RHS.getOffset() && + LHS.getLength() == RHS.getLength() && + LHS.getFilePath() == RHS.getFilePath() && + LHS.getReplacementText() == RHS.getReplacementText(); } void Replacement::setFromSourceLocation(SourceManager &Sources, @@ -208,7 +209,7 @@ void deduplicate(std::vector &Replaces, return; // Deduplicate - std::sort(Replaces.begin(), Replaces.end(), Replacement::Less()); + std::sort(Replaces.begin(), Replaces.end()); std::vector::iterator End = std::unique(Replaces.begin(), Replaces.end()); Replaces.erase(End, Replaces.end()); -- 2.40.0