Explicit conversion of SmallString to std::string

Yaron Keren yaron.keren at gmail.com
Wed Oct 15 02:53:09 PDT 2014


SmallString conversion to std::string is currently done by going through a
StringRef, S.str().str(). This patch provides a direct conversion function,
S.string().

Yaron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141015/59284d5f/attachment.html>
-------------- next part --------------
Index: include/llvm/ADT/SmallString.h
===================================================================
--- include/llvm/ADT/SmallString.h	(revision 219782)
+++ include/llvm/ADT/SmallString.h	(working copy)
@@ -266,6 +266,11 @@
   /// Explicit conversion to StringRef.
   StringRef str() const { return StringRef(this->begin(), this->size()); }
 
+  /// Explicit conversion to std::string.
+  std::string string() const {
+    return std::string(this->begin(), this->size());
+  }
+
   // TODO: Make this const, if it's safe...
   const char* c_str() {
     this->push_back(0);
Index: unittests/ADT/APIntTest.cpp
===================================================================
--- unittests/ADT/APIntTest.cpp	(revision 219782)
+++ unittests/ADT/APIntTest.cpp	(working copy)
@@ -385,53 +385,53 @@
   bool isSigned;
 
   APInt(8, 0).toString(S, 2, true, true);
-  EXPECT_EQ(S.str().str(), "0b0");
+  EXPECT_EQ(S.string(), "0b0");
   S.clear();
   APInt(8, 0).toString(S, 8, true, true);
-  EXPECT_EQ(S.str().str(), "00");
+  EXPECT_EQ(S.string(), "00");
   S.clear();
   APInt(8, 0).toString(S, 10, true, true);
-  EXPECT_EQ(S.str().str(), "0");
+  EXPECT_EQ(S.string(), "0");
   S.clear();
   APInt(8, 0).toString(S, 16, true, true);
-  EXPECT_EQ(S.str().str(), "0x0");
+  EXPECT_EQ(S.string(), "0x0");
   S.clear();
   APInt(8, 0).toString(S, 36, true, false);
-  EXPECT_EQ(S.str().str(), "0");
+  EXPECT_EQ(S.string(), "0");
   S.clear();
 
   isSigned = false;
   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
-  EXPECT_EQ(S.str().str(), "0b11111111");
+  EXPECT_EQ(S.string(), "0b11111111");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
-  EXPECT_EQ(S.str().str(), "0377");
+  EXPECT_EQ(S.string(), "0377");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
-  EXPECT_EQ(S.str().str(), "255");
+  EXPECT_EQ(S.string(), "255");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
-  EXPECT_EQ(S.str().str(), "0xFF");
+  EXPECT_EQ(S.string(), "0xFF");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
-  EXPECT_EQ(S.str().str(), "73");
+  EXPECT_EQ(S.string(), "73");
   S.clear();
 
   isSigned = true;
   APInt(8, 255, isSigned).toString(S, 2, isSigned, true);
-  EXPECT_EQ(S.str().str(), "-0b1");
+  EXPECT_EQ(S.string(), "-0b1");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 8, isSigned, true);
-  EXPECT_EQ(S.str().str(), "-01");
+  EXPECT_EQ(S.string(), "-01");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
-  EXPECT_EQ(S.str().str(), "-1");
+  EXPECT_EQ(S.string(), "-1");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
-  EXPECT_EQ(S.str().str(), "-0x1");
+  EXPECT_EQ(S.string(), "-0x1");
   S.clear();
   APInt(8, 255, isSigned).toString(S, 36, isSigned, false);
-  EXPECT_EQ(S.str().str(), "-1");
+  EXPECT_EQ(S.string(), "-1");
   S.clear();
 }
 
Index: tools/clang/lib/AST/Expr.cpp
===================================================================
--- tools/clang/lib/AST/Expr.cpp	(revision 219782)
+++ tools/clang/lib/AST/Expr.cpp	(working copy)
@@ -662,7 +662,7 @@
     Out << Proto;
 
     Out.flush();
-    return Name.str().str();
+    return Name.string();
   }
   if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
     for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
@@ -694,7 +694,7 @@
     Out <<  ']';
 
     Out.flush();
-    return Name.str().str();
+    return Name.string();
   }
   if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
     // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
Index: tools/clang/lib/AST/ItaniumMangle.cpp
===================================================================
--- tools/clang/lib/AST/ItaniumMangle.cpp	(revision 219782)
+++ tools/clang/lib/AST/ItaniumMangle.cpp	(working copy)
@@ -277,7 +277,7 @@
       return;
 
     int status = 0;
-    char *result = abi::__cxa_demangle(Out.str().str().c_str(), 0, 0, &status);
+    char *result = abi::__cxa_demangle(Out.string().c_str(), 0, 0, &status);
     assert(status == 0 && "Could not demangle mangled name!");
     free(result);
   }
Index: tools/clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- tools/clang/lib/Lex/HeaderSearch.cpp	(revision 219782)
+++ tools/clang/lib/Lex/HeaderSearch.cpp	(working copy)
@@ -143,7 +143,7 @@
     Code.toStringUnsigned(HashStr, /*Radix*/36);
     llvm::sys::path::append(Result, ModuleName + "-" + HashStr.str() + ".pcm");
   }
-  return Result.str().str();
+  return Result.string();
 }
 
 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Index: tools/clang/lib/Lex/PPDirectives.cpp
===================================================================
--- tools/clang/lib/Lex/PPDirectives.cpp	(revision 219782)
+++ tools/clang/lib/Lex/PPDirectives.cpp	(working copy)
@@ -1572,7 +1572,7 @@
       Diag(HashLoc, diag::warn_auto_module_import)
         << IncludeKind << PathString 
         << FixItHint::CreateReplacement(ReplaceRange,
-             "@import " + PathString.str().str() + ";");
+             "@import " + PathString.string() + ";");
     }
     
     // Load the module. Only make macros visible. We'll make the declarations


More information about the llvm-commits mailing list