]> granicus.if.org Git - clang/commitdiff
Avoid std::string thrashing in MultiKeywordSelector::getName(), and simplify.
authorDaniel Dunbar <daniel@zuster.org>
Sat, 17 Oct 2009 18:13:02 +0000 (18:13 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sat, 17 Oct 2009 18:13:02 +0000 (18:13 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84343 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Basic/IdentifierTable.cpp

index 93c260fdbe17a180ed6b030778887cc26347915d..a3c5c4a408d8df982eb7e1819816832b14f5f606 100644 (file)
@@ -16,6 +16,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/Support/raw_ostream.h"
 #include <cstdio>
 
 using namespace clang;
@@ -153,7 +154,7 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
 
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
-  const char *Name = getName();
+  const char *Name = getNameStart();
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
@@ -301,24 +302,15 @@ IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
 }
 
 std::string MultiKeywordSelector::getName() const {
-  std::string Result;
-  unsigned Length = 0;
+  llvm::SmallString<256> Str;
+  llvm::raw_svector_ostream OS(Str);
   for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
     if (*I)
-      Length += (*I)->getLength();
-    ++Length;  // :
+      OS << (*I)->getNameStr();
+    OS << ':';
   }
 
-  Result.reserve(Length);
-
-  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
-    if (*I)
-      Result.insert(Result.end(), (*I)->getName(),
-                    (*I)->getName()+(*I)->getLength());
-    Result.push_back(':');
-  }
-
-  return Result;
+  return OS.str();
 }
 
 std::string Selector::getAsString() const {
@@ -330,11 +322,12 @@ std::string Selector::getAsString() const {
 
     // If the number of arguments is 0 then II is guaranteed to not be null.
     if (getNumArgs() == 0)
-      return II->getName();
+      return II->getNameStr();
+
+    if (!II)
+      return ":";
 
-    std::string Res = II ? II->getName() : "";
-    Res += ":";
-    return Res;
+    return II->getNameStr().str() + ":";
   }
 
   // We have a multiple keyword selector (no embedded flags).