Fix a bug in codegenprep where it was losing track of values OptimizeMemoryInst
[oota-llvm.git] / utils / TableGen / StringToOffsetTable.h
index ac9422c5d72daa0a1bf6f86b261a1e87df6f5894..d94d3a266822c9d2c5d0341aab5907e4995d0a60 100644 (file)
 #define TBLGEN_STRING_TO_OFFSET_TABLE_H
 
 #include "llvm/ADT/SmallString.h"
-#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/Support/raw_ostream.h"
+#include <cctype>
 
 namespace llvm {
 
@@ -26,16 +27,17 @@ class StringToOffsetTable {
   std::string AggregateString;
 public:
   
-  unsigned GetOrAddStringOffset(StringRef Str) {
-    unsigned &Entry = StringOffset[Str];
-    if (Entry == 0) {
+  unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
+    StringMapEntry<unsigned> &Entry = StringOffset.GetOrCreateValue(Str, -1U);
+    if (Entry.getValue() == -1U) {
       // Add the string to the aggregate if this is the first time found.
-      Entry = AggregateString.size();
+      Entry.setValue(AggregateString.size());
       AggregateString.append(Str.begin(), Str.end());
-      AggregateString += '\0';
+      if (appendZero)
+        AggregateString += '\0';
     }
     
-    return Entry;
+    return Entry.getValue();
   }
   
   void EmitString(raw_ostream &O) {