[Option] Store arg strings in a set backed by a BumpPtrAllocator
authorReid Kleckner <reid@kleckner.net>
Mon, 15 Jul 2013 13:46:24 +0000 (13:46 +0000)
committerReid Kleckner <reid@kleckner.net>
Mon, 15 Jul 2013 13:46:24 +0000 (13:46 +0000)
No functionality change.

This is preparing to move response file parsing into lib/Option so it
can be shared between clang and lld.  This change isn't just a
micro-optimization.  Clang's driver uses a std::set<std::string> to
unique arguments while parsing response files, so this matches that.

llvm-svn: 186319

llvm/include/llvm/Option/ArgList.h
llvm/lib/Option/ArgList.cpp

index 06ba679c2b55314fa905bdb3403dd2a96764ec7b..2f4d85295b25eebd8e04801a34bf90b1e81d4e6b 100644 (file)
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Option/OptSpecifier.h"
 #include "llvm/Option/Option.h"
-#include <list>
+#include "llvm/Support/Allocator.h"
 #include <string>
 #include <vector>
 
@@ -298,7 +299,7 @@ private:
   /// This is mutable since we treat the ArgList as being the list
   /// of Args, and allow routines to add new strings (to have a
   /// convenient place to store the memory) via MakeIndex.
-  mutable std::list<std::string> SynthesizedStrings;
+  mutable StringSet<BumpPtrAllocator> SynthesizedStrings;
 
   /// The number of original input argument strings.
   unsigned NumInputArgStrings;
index 15f7e8bf4b8262c50389914f72d49a0d6427515f..18a7b5982beb4888b4d532b608588667a8bd4696 100644 (file)
@@ -323,9 +323,18 @@ InputArgList::~InputArgList() {
 unsigned InputArgList::MakeIndex(StringRef String0) const {
   unsigned Index = ArgStrings.size();
 
+  // If necessary, make a copy so we can null terminate it.
+  std::string NullTerminated;
+  if (String0.back() != '\0') {
+    NullTerminated.append(String0.data(), String0.size());
+    NullTerminated.push_back('\0');
+    String0 = StringRef(&NullTerminated[0], NullTerminated.size());
+  }
+
   // Tuck away so we have a reliable const char *.
-  SynthesizedStrings.push_back(String0);
-  ArgStrings.push_back(SynthesizedStrings.back().c_str());
+  String0 = SynthesizedStrings.GetOrCreateValue(String0).getKey();
+  assert(String0.back() == '\0');
+  ArgStrings.push_back(String0.data());
 
   return Index;
 }