[llvm] r203374 - Clean up SmallString a bit

David Blaikie dblaikie at gmail.com
Sat Mar 8 22:17:02 PST 2014


Author: dblaikie
Date: Sun Mar  9 00:17:01 2014
New Revision: 203374

URL: http://llvm.org/viewvc/llvm-project?rev=203374&view=rev
Log:
Clean up SmallString a bit

Move a common utility (assign(iter, iter)) into SmallVector (some of the
others could be moved there too, but this one seemed particularly
generic) and replace repetitions overrides with using directives.

And simplify SmallVector::assign(num, element) while I'm here rather
than thrashing these files (that cause everyone to rebuild) again.

Modified:
    llvm/trunk/include/llvm/ADT/SmallString.h
    llvm/trunk/include/llvm/ADT/SmallVector.h
    llvm/trunk/include/llvm/DebugInfo/DIContext.h
    llvm/trunk/unittests/ADT/SmallStringTest.cpp
    llvm/trunk/unittests/ADT/SmallVectorTest.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallString.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallString.h?rev=203374&r1=203373&r2=203374&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallString.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallString.h Sun Mar  9 00:17:01 2014
@@ -28,30 +28,18 @@ public:
   SmallString() {}
 
   /// Initialize from a StringRef.
-  SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
+  /*implicit*/ SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
 
   /// Initialize with a range.
   template<typename ItTy>
   SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
 
-  // Note that in order to add new overloads for append & assign, we have to
-  // duplicate the inherited versions so as not to inadvertently hide them.
-
   /// @}
   /// @name String Assignment
   /// @{
 
-  /// Assign from a repeated element.
-  void assign(size_t NumElts, char Elt) {
-    this->SmallVectorImpl<char>::assign(NumElts, Elt);
-  }
-
-  /// Assign from an iterator pair.
-  template<typename in_iter>
-  void assign(in_iter S, in_iter E) {
-    this->clear();
-    SmallVectorImpl<char>::append(S, E);
-  }
+  // Provide assign from SmallVectorImpl<char>
+  using SmallVectorImpl<char>::assign;
 
   /// Assign from a StringRef.
   void assign(StringRef RHS) {
@@ -65,20 +53,7 @@ public:
     SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
   }
 
-  /// @}
-  /// @name String Concatenation
-  /// @{
-
-  /// Append from an iterator pair.
-  template<typename in_iter>
-  void append(in_iter S, in_iter E) {
-    SmallVectorImpl<char>::append(S, E);
-  }
-
-  void append(size_t NumInputs, char Elt) {
-    SmallVectorImpl<char>::append(NumInputs, Elt);
-  }
-
+  using SmallVectorImpl<char>::append;
 
   /// Append from a StringRef.
   void append(StringRef RHS) {
@@ -94,12 +69,6 @@ public:
   /// @name String Comparison
   /// @{
 
-  /// Check for string equality.  This is more efficient than compare() when
-  /// the relative ordering of inequal strings isn't needed.
-  bool equals(StringRef RHS) const {
-    return str().equals(RHS);
-  }
-
   /// Check for string equality, ignoring case.
   bool equals_lower(StringRef RHS) const {
     return str().equals_lower(RHS);
@@ -276,6 +245,9 @@ public:
   /// Implicit conversion to StringRef.
   operator StringRef() const { return str(); }
 
+  // Provide op= for SmallVectorImpl<char>
+  using SmallVectorImpl<char>::operator=;
+
   // Extra operators.
   const SmallString &operator=(StringRef RHS) {
     this->clear();
@@ -283,9 +255,15 @@ public:
   }
 
   SmallString &operator+=(StringRef RHS) {
-    this->append(RHS.begin(), RHS.end());
+    append(RHS.begin(), RHS.end());
     return *this;
   }
+
+  SmallString &operator+=(const SmallVectorImpl<char> &RHS) {
+    append(RHS.begin(), RHS.end());
+    return *this;
+  }
+
   SmallString &operator+=(char C) {
     this->push_back(C);
     return *this;

Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=203374&r1=203373&r2=203374&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Sun Mar  9 00:17:01 2014
@@ -451,10 +451,12 @@ public:
 
   void assign(unsigned NumElts, const T &Elt) {
     clear();
-    if (this->capacity() < NumElts)
-      this->grow(NumElts);
-    this->setEnd(this->begin()+NumElts);
-    std::uninitialized_fill(this->begin(), this->end(), Elt);
+    append(NumElts, Elt);
+  }
+
+  template <typename in_iter> void assign(in_iter S, in_iter E) {
+    clear();
+    append(S, E);
   }
 
   iterator erase(iterator I) {

Modified: llvm/trunk/include/llvm/DebugInfo/DIContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/DIContext.h?rev=203374&r1=203373&r2=203374&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/DIContext.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/DIContext.h Sun Mar  9 00:17:01 2014
@@ -50,8 +50,7 @@ public:
 
   bool operator==(const DILineInfo &RHS) const {
     return Line == RHS.Line && Column == RHS.Column &&
-           FileName.equals(RHS.FileName) &&
-           FunctionName.equals(RHS.FunctionName);
+           FileName == RHS.FileName && FunctionName == RHS.FunctionName;
   }
   bool operator!=(const DILineInfo &RHS) const {
     return !(*this == RHS);

Modified: llvm/trunk/unittests/ADT/SmallStringTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallStringTest.cpp?rev=203374&r1=203373&r2=203374&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallStringTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallStringTest.cpp Sun Mar  9 00:17:01 2014
@@ -50,13 +50,6 @@ TEST_F(SmallStringTest, AssignRepeated)
   EXPECT_STREQ("aaa", theString.c_str());
 }
 
-TEST_F(SmallStringTest, AssignIterPair) {
-  StringRef abc = "abc";
-  theString.assign(abc.begin(), abc.end());
-  EXPECT_EQ(3u, theString.size());
-  EXPECT_STREQ("abc", theString.c_str());
-}
-
 TEST_F(SmallStringTest, AssignStringRef) {
   StringRef abc = "abc";
   theString.assign(abc);
@@ -87,6 +80,23 @@ TEST_F(SmallStringTest, AppendStringRef)
   EXPECT_EQ(6u, theString.size());
   EXPECT_STREQ("abcabc", theString.c_str());
 }
+
+TEST_F(SmallStringTest, PlusEqualsStringRef) {
+  StringRef abc = "abc";
+  theString += abc;
+  theString += abc;
+  EXPECT_EQ(6u, theString.size());
+  EXPECT_STREQ("abcabc", theString.c_str());
+}
+
+TEST_F(SmallStringTest, PlusEqualsSmallVector) {
+  StringRef abc = "abc";
+  SmallVector<char, 10> abcVec(abc.begin(), abc.end());
+  theString += abcVec;
+  theString += abcVec;
+  EXPECT_EQ(6u, theString.size());
+  EXPECT_STREQ("abcabc", theString.c_str());
+}
 
 TEST_F(SmallStringTest, AppendSmallVector) {
   StringRef abc = "abc";

Modified: llvm/trunk/unittests/ADT/SmallVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=203374&r1=203373&r2=203374&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallVectorTest.cpp Sun Mar  9 00:17:01 2014
@@ -338,6 +338,17 @@ TYPED_TEST(SmallVectorTest, AssignTest)
   this->assertValuesInOrder(this->theVector, 2u, 77, 77);
 }
 
+TYPED_TEST(SmallVectorTest, AssignIterPair) {
+  SCOPED_TRACE("AssignIterPair");
+
+  std::vector<int> v;
+  v.push_back(1);
+  v.push_back(2);
+  this->theVector.push_back(Constructable(1));
+  this->theVector.assign(v.begin(), v.end());
+  this->assertValuesInOrder(this->theVector, 2u, 1, 2);
+}
+
 // Erase a single element
 TYPED_TEST(SmallVectorTest, EraseTest) {
   SCOPED_TRACE("EraseTest");





More information about the llvm-commits mailing list