r188550 - Tweak Replacement comparisons
Edwin Vane
edwin.vane at intel.com
Fri Aug 16 05:18:53 PDT 2013
Author: revane
Date: Fri Aug 16 07:18:53 2013
New Revision: 188550
URL: http://llvm.org/viewvc/llvm-project?rev=188550&view=rev
Log:
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
Modified:
cfe/trunk/include/clang/Tooling/Refactoring.h
cfe/trunk/lib/Tooling/Refactoring.cpp
Modified: cfe/trunk/include/clang/Tooling/Refactoring.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring.h?rev=188550&r1=188549&r2=188550&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring.h (original)
+++ cfe/trunk/include/clang/Tooling/Refactoring.h Fri Aug 16 07:18:53 2013
@@ -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<Replacement, Replacement::Less> Replacements;
+typedef std::set<Replacement> Replacements;
/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite.
///
Modified: cfe/trunk/lib/Tooling/Refactoring.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring.cpp?rev=188550&r1=188549&r2=188550&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Refactoring.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring.cpp Fri Aug 16 07:18:53 2013
@@ -80,20 +80,21 @@ std::string Replacement::toString() cons
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<Replacement
return;
// Deduplicate
- std::sort(Replaces.begin(), Replaces.end(), Replacement::Less());
+ std::sort(Replaces.begin(), Replaces.end());
std::vector<Replacement>::iterator End =
std::unique(Replaces.begin(), Replaces.end());
Replaces.erase(End, Replaces.end());
More information about the cfe-commits
mailing list