소스 검색

OnDiskHashTable: Use EndianStream.h to write little endian ostreams

Rather than rolling our own functions to write little endian data to
an ostream, we can use the support in llvm's EndianStream.h.

No functional change.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205044 91177308-0d34-0410-b5e6-96231b3b80d8
Justin Bogner 11 년 전
부모
커밋
366b49f98e
5개의 변경된 파일153개의 추가작업 그리고 128개의 파일을 삭제
  1. 12 43
      include/clang/Basic/OnDiskHashTable.h
  2. 39 20
      lib/Frontend/CacheTokens.cpp
  3. 1 0
      lib/Lex/PTHLexer.cpp
  4. 93 61
      lib/Serialization/ASTWriter.cpp
  5. 8 4
      lib/Serialization/GlobalModuleIndex.cpp

+ 12 - 43
include/clang/Basic/OnDiskHashTable.h

@@ -17,6 +17,7 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/LLVM.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/EndianStream.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/Host.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/raw_ostream.h"
@@ -29,45 +30,11 @@ namespace io {
 
 
 typedef uint32_t Offset;
 typedef uint32_t Offset;
 
 
-inline void Emit8(raw_ostream& Out, uint32_t V) {
-  Out << (unsigned char)(V);
-}
-
-inline void Emit16(raw_ostream& Out, uint32_t V) {
-  Out << (unsigned char)(V);
-  Out << (unsigned char)(V >>  8);
-  assert((V >> 16) == 0);
-}
-
-inline void Emit24(raw_ostream& Out, uint32_t V) {
-  Out << (unsigned char)(V);
-  Out << (unsigned char)(V >>  8);
-  Out << (unsigned char)(V >> 16);
-  assert((V >> 24) == 0);
-}
-
-inline void Emit32(raw_ostream& Out, uint32_t V) {
-  Out << (unsigned char)(V);
-  Out << (unsigned char)(V >>  8);
-  Out << (unsigned char)(V >> 16);
-  Out << (unsigned char)(V >> 24);
-}
-
-inline void Emit64(raw_ostream& Out, uint64_t V) {
-  Out << (unsigned char)(V);
-  Out << (unsigned char)(V >>  8);
-  Out << (unsigned char)(V >> 16);
-  Out << (unsigned char)(V >> 24);
-  Out << (unsigned char)(V >> 32);
-  Out << (unsigned char)(V >> 40);
-  Out << (unsigned char)(V >> 48);
-  Out << (unsigned char)(V >> 56);
-}
-
 inline void Pad(raw_ostream& Out, unsigned A) {
 inline void Pad(raw_ostream& Out, unsigned A) {
+  using namespace llvm::support;
   Offset off = (Offset) Out.tell();
   Offset off = (Offset) Out.tell();
   for (uint32_t n = llvm::OffsetToAlignment(off, A); n; --n)
   for (uint32_t n = llvm::OffsetToAlignment(off, A); n; --n)
-    Emit8(Out, 0);
+    endian::Writer<little>(Out).write<uint8_t>(0);
 }
 }
 
 
 inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) {
 inline uint16_t ReadUnalignedLE16(const unsigned char *&Data) {
@@ -188,7 +155,8 @@ public:
   }
   }
 
 
   io::Offset Emit(raw_ostream &out, Info &InfoObj) {
   io::Offset Emit(raw_ostream &out, Info &InfoObj) {
-    using namespace clang::io;
+    using namespace llvm::support;
+    endian::Writer<little> LE(out);
 
 
     // Emit the payload of the table.
     // Emit the payload of the table.
     for (unsigned i = 0; i < NumBuckets; ++i) {
     for (unsigned i = 0; i < NumBuckets; ++i) {
@@ -200,12 +168,12 @@ public:
       assert(B.off && "Cannot write a bucket at offset 0. Please add padding.");
       assert(B.off && "Cannot write a bucket at offset 0. Please add padding.");
 
 
       // Write out the number of items in the bucket.
       // Write out the number of items in the bucket.
-      Emit16(out, B.length);
+      LE.write<uint16_t>(B.length);
       assert(B.length != 0  && "Bucket has a head but zero length?");
       assert(B.length != 0  && "Bucket has a head but zero length?");
 
 
       // Write out the entries in the bucket.
       // Write out the entries in the bucket.
       for (Item *I = B.head; I ; I = I->next) {
       for (Item *I = B.head; I ; I = I->next) {
-        Emit32(out, I->hash);
+        LE.write<uint32_t>(I->hash);
         const std::pair<unsigned, unsigned>& Len =
         const std::pair<unsigned, unsigned>& Len =
           InfoObj.EmitKeyDataLength(out, I->key, I->data);
           InfoObj.EmitKeyDataLength(out, I->key, I->data);
         InfoObj.EmitKey(out, I->key, Len.first);
         InfoObj.EmitKey(out, I->key, Len.first);
@@ -214,11 +182,12 @@ public:
     }
     }
 
 
     // Emit the hashtable itself.
     // Emit the hashtable itself.
-    Pad(out, 4);
+    io::Pad(out, 4);
     io::Offset TableOff = out.tell();
     io::Offset TableOff = out.tell();
-    Emit32(out, NumBuckets);
-    Emit32(out, NumEntries);
-    for (unsigned i = 0; i < NumBuckets; ++i) Emit32(out, Buckets[i].off);
+    LE.write<uint32_t>(NumBuckets);
+    LE.write<uint32_t>(NumEntries);
+    for (unsigned i = 0; i < NumBuckets; ++i)
+      LE.write<uint32_t>(Buckets[i].off);
 
 
     return TableOff;
     return TableOff;
   }
   }

+ 39 - 20
lib/Frontend/CacheTokens.cpp

@@ -23,6 +23,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Preprocessor.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/Support/EndianStream.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Path.h"
@@ -78,21 +79,23 @@ public:
   unsigned getKind() const { return (unsigned) Kind; }
   unsigned getKind() const { return (unsigned) Kind; }
 
 
   void EmitData(raw_ostream& Out) {
   void EmitData(raw_ostream& Out) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     switch (Kind) {
     switch (Kind) {
     case IsFE: {
     case IsFE: {
       // Emit stat information.
       // Emit stat information.
       llvm::sys::fs::UniqueID UID = FE->getUniqueID();
       llvm::sys::fs::UniqueID UID = FE->getUniqueID();
-      ::Emit64(Out, UID.getFile());
-      ::Emit64(Out, UID.getDevice());
-      ::Emit64(Out, FE->getModificationTime());
-      ::Emit64(Out, FE->getSize());
+      LE.write<uint64_t>(UID.getFile());
+      LE.write<uint64_t>(UID.getDevice());
+      LE.write<uint64_t>(FE->getModificationTime());
+      LE.write<uint64_t>(FE->getSize());
     } break;
     } break;
     case IsDE:
     case IsDE:
       // Emit stat information.
       // Emit stat information.
-      ::Emit64(Out, Data->UniqueID.getFile());
-      ::Emit64(Out, Data->UniqueID.getDevice());
-      ::Emit64(Out, Data->ModTime);
-      ::Emit64(Out, Data->Size);
+      LE.write<uint64_t>(Data->UniqueID.getFile());
+      LE.write<uint64_t>(Data->UniqueID.getDevice());
+      LE.write<uint64_t>(Data->ModTime);
+      LE.write<uint64_t>(Data->Size);
       delete Data;
       delete Data;
       break;
       break;
     default:
     default:
@@ -120,32 +123,36 @@ public:
   static std::pair<unsigned,unsigned>
   static std::pair<unsigned,unsigned>
   EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
   EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
                     const PTHEntry& E) {
                     const PTHEntry& E) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
 
 
     unsigned n = V.getString().size() + 1 + 1;
     unsigned n = V.getString().size() + 1 + 1;
-    ::Emit16(Out, n);
+    LE.write<uint16_t>(n);
 
 
     unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
     unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
-    ::Emit8(Out, m);
+    LE.write<uint8_t>(m);
 
 
     return std::make_pair(n, m);
     return std::make_pair(n, m);
   }
   }
 
 
   static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
   static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
+    using namespace llvm::support;
     // Emit the entry kind.
     // Emit the entry kind.
-    ::Emit8(Out, (unsigned) V.getKind());
+    endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
     // Emit the string.
     // Emit the string.
     Out.write(V.getString().data(), n - 1);
     Out.write(V.getString().data(), n - 1);
   }
   }
 
 
   static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
   static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
                        const PTHEntry& E, unsigned) {
                        const PTHEntry& E, unsigned) {
-
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
 
 
     // For file entries emit the offsets into the PTH file for token data
     // For file entries emit the offsets into the PTH file for token data
     // and the preprocessor blocks table.
     // and the preprocessor blocks table.
     if (V.isFile()) {
     if (V.isFile()) {
-      ::Emit32(Out, E.getTokenOffset());
-      ::Emit32(Out, E.getPPCondTableOffset());
+      LE.write<uint32_t>(E.getTokenOffset());
+      LE.write<uint32_t>(E.getPPCondTableOffset());
     }
     }
 
 
     // Emit any other data associated with the key (i.e., stat information).
     // Emit any other data associated with the key (i.e., stat information).
@@ -186,18 +193,28 @@ class PTHWriter {
   /// Emit a token to the PTH file.
   /// Emit a token to the PTH file.
   void EmitToken(const Token& T);
   void EmitToken(const Token& T);
 
 
-  void Emit8(uint32_t V) { ::Emit8(Out, V); }
+  void Emit8(uint32_t V) {
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint8_t>(V);
+  }
 
 
-  void Emit16(uint32_t V) { ::Emit16(Out, V); }
+  void Emit16(uint32_t V) {
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint16_t>(V);
+  }
 
 
-  void Emit32(uint32_t V) { ::Emit32(Out, V); }
+  void Emit32(uint32_t V) {
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint32_t>(V);
+  }
 
 
   void EmitBuf(const char *Ptr, unsigned NumBytes) {
   void EmitBuf(const char *Ptr, unsigned NumBytes) {
     Out.write(Ptr, NumBytes);
     Out.write(Ptr, NumBytes);
   }
   }
 
 
   void EmitString(StringRef V) {
   void EmitString(StringRef V) {
-    ::Emit16(Out, V.size());
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint16_t>(V.size());
     EmitBuf(V.data(), V.size());
     EmitBuf(V.data(), V.size());
   }
   }
 
 
@@ -584,8 +601,9 @@ public:
 
 
   static std::pair<unsigned,unsigned>
   static std::pair<unsigned,unsigned>
   EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
   EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
+    using namespace llvm::support;
     unsigned n = key->II->getLength() + 1;
     unsigned n = key->II->getLength() + 1;
-    ::Emit16(Out, n);
+    endian::Writer<little>(Out).write<uint16_t>(n);
     return std::make_pair(n, sizeof(uint32_t));
     return std::make_pair(n, sizeof(uint32_t));
   }
   }
 
 
@@ -598,7 +616,8 @@ public:
 
 
   static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
   static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
                        unsigned) {
                        unsigned) {
-    ::Emit32(Out, pID);
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint32_t>(pID);
   }
   }
 };
 };
 } // end anonymous namespace
 } // end anonymous namespace

+ 1 - 0
lib/Lex/PTHLexer.cpp

@@ -23,6 +23,7 @@
 #include "clang/Lex/Token.h"
 #include "clang/Lex/Token.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/Support/EndianStream.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/system_error.h"
 #include "llvm/Support/system_error.h"
 #include <memory>
 #include <memory>

+ 93 - 61
lib/Serialization/ASTWriter.cpp

@@ -46,6 +46,7 @@
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
+#include "llvm/Support/EndianStream.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Path.h"
@@ -1465,26 +1466,31 @@ namespace {
     
     
     std::pair<unsigned,unsigned>
     std::pair<unsigned,unsigned>
     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
+      using namespace llvm::support;
+      endian::Writer<little> Writer(Out);
       unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
       unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
-      clang::io::Emit16(Out, KeyLen);
+      Writer.write<uint16_t>(KeyLen);
       unsigned DataLen = 1 + 2 + 4 + 4;
       unsigned DataLen = 1 + 2 + 4 + 4;
       if (Data.isModuleHeader)
       if (Data.isModuleHeader)
         DataLen += 4;
         DataLen += 4;
-      clang::io::Emit8(Out, DataLen);
+      Writer.write<uint16_t>(DataLen);
       return std::make_pair(KeyLen, DataLen);
       return std::make_pair(KeyLen, DataLen);
     }
     }
     
     
     void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
     void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
-      clang::io::Emit64(Out, key.FE->getSize());
+      using namespace llvm::support;
+      endian::Writer<little> LE(Out);
+      LE.write<uint64_t>(key.FE->getSize());
       KeyLen -= 8;
       KeyLen -= 8;
-      clang::io::Emit64(Out, key.FE->getModificationTime());
+      LE.write<uint64_t>(key.FE->getModificationTime());
       KeyLen -= 8;
       KeyLen -= 8;
       Out.write(key.Filename, KeyLen);
       Out.write(key.Filename, KeyLen);
     }
     }
     
     
     void EmitData(raw_ostream &Out, key_type_ref key,
     void EmitData(raw_ostream &Out, key_type_ref key,
                   data_type_ref Data, unsigned DataLen) {
                   data_type_ref Data, unsigned DataLen) {
-      using namespace clang::io;
+      using namespace llvm::support;
+      endian::Writer<little> LE(Out);
       uint64_t Start = Out.tell(); (void)Start;
       uint64_t Start = Out.tell(); (void)Start;
       
       
       unsigned char Flags = (Data.HeaderRole << 6)
       unsigned char Flags = (Data.HeaderRole << 6)
@@ -1493,13 +1499,13 @@ namespace {
                           | (Data.DirInfo << 2)
                           | (Data.DirInfo << 2)
                           | (Data.Resolved << 1)
                           | (Data.Resolved << 1)
                           | Data.IndexHeaderMapHeader;
                           | Data.IndexHeaderMapHeader;
-      Emit8(Out, (uint8_t)Flags);
-      Emit16(Out, (uint16_t) Data.NumIncludes);
+      LE.write<uint8_t>(Flags);
+      LE.write<uint16_t>(Data.NumIncludes);
       
       
       if (!Data.ControllingMacro)
       if (!Data.ControllingMacro)
-        Emit32(Out, (uint32_t)Data.ControllingMacroID);
+        LE.write<uint32_t>(Data.ControllingMacroID);
       else
       else
-        Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
+        LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
       
       
       unsigned Offset = 0;
       unsigned Offset = 0;
       if (!Data.Framework.empty()) {
       if (!Data.Framework.empty()) {
@@ -1516,11 +1522,11 @@ namespace {
         } else
         } else
           Offset = Pos->second;
           Offset = Pos->second;
       }
       }
-      Emit32(Out, Offset);
+      LE.write<uint32_t>(Offset);
 
 
       if (Data.isModuleHeader) {
       if (Data.isModuleHeader) {
         Module *Mod = HS.findModuleForHeader(key.FE).getModule();
         Module *Mod = HS.findModuleForHeader(key.FE).getModule();
-        Emit32(Out, Writer.getExistingSubmoduleID(Mod));
+        LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
       }
       }
 
 
       assert(Out.tell() - Start == DataLen && "Wrong data length");
       assert(Out.tell() - Start == DataLen && "Wrong data length");
@@ -1578,9 +1584,10 @@ void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
   SmallString<4096> TableData;
   SmallString<4096> TableData;
   uint32_t BucketOffset;
   uint32_t BucketOffset;
   {
   {
+    using namespace llvm::support;
     llvm::raw_svector_ostream Out(TableData);
     llvm::raw_svector_ostream Out(TableData);
     // Make sure that no bucket is at offset 0
     // Make sure that no bucket is at offset 0
-    clang::io::Emit32(Out, 0);
+    endian::Writer<little>(Out).write(0);
     BucketOffset = Generator.Emit(Out, GeneratorTrait);
     BucketOffset = Generator.Emit(Out, GeneratorTrait);
   }
   }
 
 
@@ -1835,12 +1842,14 @@ public:
   }
   }
 
 
   static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
   static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
-    clang::io::Emit32(Out, Key);
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint32_t>(Key);
   }
   }
 
 
   static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
   static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
                        unsigned) {
                        unsigned) {
-    clang::io::Emit32(Out, Data.MacroDirectivesOffset);
+    using namespace llvm::support;
+    endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
   }
   }
 };
 };
 } // end anonymous namespace
 } // end anonymous namespace
@@ -2036,9 +2045,10 @@ void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
   SmallString<4096> MacroTable;
   SmallString<4096> MacroTable;
   uint32_t BucketOffset;
   uint32_t BucketOffset;
   {
   {
+    using namespace llvm::support;
     llvm::raw_svector_ostream Out(MacroTable);
     llvm::raw_svector_ostream Out(MacroTable);
     // Make sure that no bucket is at offset 0
     // Make sure that no bucket is at offset 0
-    clang::io::Emit32(Out, 0);
+    endian::Writer<little>(Out).write(0);
     BucketOffset = Generator.Emit(Out);
     BucketOffset = Generator.Emit(Out);
   }
   }
 
 
@@ -2709,8 +2719,10 @@ public:
   std::pair<unsigned,unsigned>
   std::pair<unsigned,unsigned>
     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
                       data_type_ref Methods) {
                       data_type_ref Methods) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
-    clang::io::Emit16(Out, KeyLen);
+    LE.write<uint16_t>(KeyLen);
     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
          Method = Method->getNext())
          Method = Method->getNext())
@@ -2720,27 +2732,31 @@ public:
          Method = Method->getNext())
          Method = Method->getNext())
       if (Method->Method)
       if (Method->Method)
         DataLen += 4;
         DataLen += 4;
-    clang::io::Emit16(Out, DataLen);
+    LE.write<uint16_t>(DataLen);
     return std::make_pair(KeyLen, DataLen);
     return std::make_pair(KeyLen, DataLen);
   }
   }
 
 
   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     uint64_t Start = Out.tell();
     uint64_t Start = Out.tell();
     assert((Start >> 32) == 0 && "Selector key offset too large");
     assert((Start >> 32) == 0 && "Selector key offset too large");
     Writer.SetSelectorOffset(Sel, Start);
     Writer.SetSelectorOffset(Sel, Start);
     unsigned N = Sel.getNumArgs();
     unsigned N = Sel.getNumArgs();
-    clang::io::Emit16(Out, N);
+    LE.write<uint16_t>(N);
     if (N == 0)
     if (N == 0)
       N = 1;
       N = 1;
     for (unsigned I = 0; I != N; ++I)
     for (unsigned I = 0; I != N; ++I)
-      clang::io::Emit32(Out,
-                    Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
+      LE.write<uint32_t>(
+          Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
   }
   }
 
 
   void EmitData(raw_ostream& Out, key_type_ref,
   void EmitData(raw_ostream& Out, key_type_ref,
                 data_type_ref Methods, unsigned DataLen) {
                 data_type_ref Methods, unsigned DataLen) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     uint64_t Start = Out.tell(); (void)Start;
     uint64_t Start = Out.tell(); (void)Start;
-    clang::io::Emit32(Out, Methods.ID);
+    LE.write<uint32_t>(Methods.ID);
     unsigned NumInstanceMethods = 0;
     unsigned NumInstanceMethods = 0;
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
          Method = Method->getNext())
          Method = Method->getNext())
@@ -2760,16 +2776,16 @@ public:
     unsigned FactoryBits = Methods.Factory.getBits();
     unsigned FactoryBits = Methods.Factory.getBits();
     assert(FactoryBits < 4);
     assert(FactoryBits < 4);
     unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
     unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
-    clang::io::Emit16(Out, NumInstanceMethodsAndBits);
-    clang::io::Emit16(Out, NumFactoryMethodsAndBits);
+    LE.write<uint16_t>(NumInstanceMethodsAndBits);
+    LE.write<uint16_t>(NumFactoryMethodsAndBits);
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
     for (const ObjCMethodList *Method = &Methods.Instance; Method;
          Method = Method->getNext())
          Method = Method->getNext())
       if (Method->Method)
       if (Method->Method)
-        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
+        LE.write<uint32_t>(Writer.getDeclID(Method->Method));
     for (const ObjCMethodList *Method = &Methods.Factory; Method;
     for (const ObjCMethodList *Method = &Methods.Factory; Method;
          Method = Method->getNext())
          Method = Method->getNext())
       if (Method->Method)
       if (Method->Method)
-        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
+        LE.write<uint32_t>(Writer.getDeclID(Method->Method));
 
 
     assert(Out.tell() - Start == DataLen && "Data length is wrong");
     assert(Out.tell() - Start == DataLen && "Data length is wrong");
   }
   }
@@ -2838,10 +2854,11 @@ void ASTWriter::WriteSelectors(Sema &SemaRef) {
     SmallString<4096> MethodPool;
     SmallString<4096> MethodPool;
     uint32_t BucketOffset;
     uint32_t BucketOffset;
     {
     {
+      using namespace llvm::support;
       ASTMethodPoolTrait Trait(*this);
       ASTMethodPoolTrait Trait(*this);
       llvm::raw_svector_ostream Out(MethodPool);
       llvm::raw_svector_ostream Out(MethodPool);
       // Make sure that no bucket is at offset 0
       // Make sure that no bucket is at offset 0
-      clang::io::Emit32(Out, 0);
+      endian::Writer<little>(Out).write<uint32_t>(0);
       BucketOffset = Generator.Emit(Out, Trait);
       BucketOffset = Generator.Emit(Out, Trait);
     }
     }
 
 
@@ -3086,11 +3103,14 @@ public:
            D != DEnd; ++D)
            D != DEnd; ++D)
         DataLen += sizeof(DeclID);
         DataLen += sizeof(DeclID);
     }
     }
-    clang::io::Emit16(Out, DataLen);
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
+
+    LE.write<uint16_t>(DataLen);
     // We emit the key length after the data length so that every
     // We emit the key length after the data length so that every
     // string is preceded by a 16-bit length. This matches the PTH
     // string is preceded by a 16-bit length. This matches the PTH
     // format for storing identifiers.
     // format for storing identifiers.
-    clang::io::Emit16(Out, KeyLen);
+    LE.write<uint16_t>(KeyLen);
     return std::make_pair(KeyLen, DataLen);
     return std::make_pair(KeyLen, DataLen);
   }
   }
 
 
@@ -3105,24 +3125,28 @@ public:
   static void emitMacroOverrides(raw_ostream &Out,
   static void emitMacroOverrides(raw_ostream &Out,
                                  llvm::ArrayRef<SubmoduleID> Overridden) {
                                  llvm::ArrayRef<SubmoduleID> Overridden) {
     if (!Overridden.empty()) {
     if (!Overridden.empty()) {
-      clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
+      using namespace llvm::support;
+      endian::Writer<little> LE(Out);
+      LE.write<uint32_t>(Overridden.size() | 0x80000000U);
       for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
       for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
-        clang::io::Emit32(Out, Overridden[I]);
+        LE.write<uint32_t>(Overridden[I]);
     }
     }
   }
   }
 
 
   void EmitData(raw_ostream& Out, IdentifierInfo* II,
   void EmitData(raw_ostream& Out, IdentifierInfo* II,
                 IdentID ID, unsigned) {
                 IdentID ID, unsigned) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     MacroDirective *Macro = 0;
     MacroDirective *Macro = 0;
     if (!isInterestingIdentifier(II, Macro)) {
     if (!isInterestingIdentifier(II, Macro)) {
-      clang::io::Emit32(Out, ID << 1);
+      LE.write<uint32_t>(ID << 1);
       return;
       return;
     }
     }
 
 
-    clang::io::Emit32(Out, (ID << 1) | 0x01);
+    LE.write<uint32_t>((ID << 1) | 0x01);
     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
-    clang::io::Emit16(Out, Bits);
+    LE.write<uint16_t>(Bits);
     Bits = 0;
     Bits = 0;
     bool HadMacroDefinition = hadMacroDefinition(II, Macro);
     bool HadMacroDefinition = hadMacroDefinition(II, Macro);
     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
@@ -3131,10 +3155,10 @@ public:
     Bits = (Bits << 1) | unsigned(II->isPoisoned());
     Bits = (Bits << 1) | unsigned(II->isPoisoned());
     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
-    clang::io::Emit16(Out, Bits);
+    LE.write<uint16_t>(Bits);
 
 
     if (HadMacroDefinition) {
     if (HadMacroDefinition) {
-      clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
+      LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
       if (IsModule) {
       if (IsModule) {
         // Write the IDs of macros coming from different submodules.
         // Write the IDs of macros coming from different submodules.
         SubmoduleID ModID;
         SubmoduleID ModID;
@@ -3147,14 +3171,14 @@ public:
           if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
           if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
             InfoID = Writer.getMacroID(DefMD->getInfo());
             InfoID = Writer.getMacroID(DefMD->getInfo());
             assert(InfoID);
             assert(InfoID);
-            clang::io::Emit32(Out, InfoID << 1);
+            LE.write<uint32_t>(InfoID << 1);
           } else {
           } else {
             assert(isa<UndefMacroDirective>(MD));
             assert(isa<UndefMacroDirective>(MD));
-            clang::io::Emit32(Out, (ModID << 1) | 1);
+            LE.write<uint32_t>((ModID << 1) | 1);
           }
           }
         }
         }
         emitMacroOverrides(Out, Overridden);
         emitMacroOverrides(Out, Overridden);
-        clang::io::Emit32(Out, 0);
+        LE.write<uint32_t>(0);
       }
       }
     }
     }
 
 
@@ -3169,7 +3193,7 @@ public:
     for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
     for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
                                                 DEnd = Decls.rend();
                                                 DEnd = Decls.rend();
          D != DEnd; ++D)
          D != DEnd; ++D)
-      clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
+      LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
   }
   }
 
 
   /// \brief Returns the most recent local decl or the given decl if there are
   /// \brief Returns the most recent local decl or the given decl if there are
@@ -3238,10 +3262,11 @@ void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
     SmallString<4096> IdentifierTable;
     SmallString<4096> IdentifierTable;
     uint32_t BucketOffset;
     uint32_t BucketOffset;
     {
     {
+      using namespace llvm::support;
       ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
       ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
       llvm::raw_svector_ostream Out(IdentifierTable);
       llvm::raw_svector_ostream Out(IdentifierTable);
       // Make sure that no bucket is at offset 0
       // Make sure that no bucket is at offset 0
-      clang::io::Emit32(Out, 0);
+      endian::Writer<little>(Out).write<uint32_t>(0);
       BucketOffset = Generator.Emit(Out, Trait);
       BucketOffset = Generator.Emit(Out, Trait);
     }
     }
 
 
@@ -3330,6 +3355,8 @@ public:
   std::pair<unsigned,unsigned>
   std::pair<unsigned,unsigned>
     EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
     EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
                       data_type_ref Lookup) {
                       data_type_ref Lookup) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     unsigned KeyLen = 1;
     unsigned KeyLen = 1;
     switch (Name.getNameKind()) {
     switch (Name.getNameKind()) {
     case DeclarationName::Identifier:
     case DeclarationName::Identifier:
@@ -3348,35 +3375,35 @@ public:
     case DeclarationName::CXXUsingDirective:
     case DeclarationName::CXXUsingDirective:
       break;
       break;
     }
     }
-    clang::io::Emit16(Out, KeyLen);
+    LE.write<uint16_t>(KeyLen);
 
 
     // 2 bytes for num of decls and 4 for each DeclID.
     // 2 bytes for num of decls and 4 for each DeclID.
     unsigned DataLen = 2 + 4 * Lookup.size();
     unsigned DataLen = 2 + 4 * Lookup.size();
-    clang::io::Emit16(Out, DataLen);
+    LE.write<uint16_t>(DataLen);
 
 
     return std::make_pair(KeyLen, DataLen);
     return std::make_pair(KeyLen, DataLen);
   }
   }
 
 
   void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
   void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
-    using namespace clang::io;
-
-    Emit8(Out, Name.getNameKind());
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
+    LE.write<uint8_t>(Name.getNameKind());
     switch (Name.getNameKind()) {
     switch (Name.getNameKind()) {
     case DeclarationName::Identifier:
     case DeclarationName::Identifier:
-      Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
+      LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
       return;
       return;
     case DeclarationName::ObjCZeroArgSelector:
     case DeclarationName::ObjCZeroArgSelector:
     case DeclarationName::ObjCOneArgSelector:
     case DeclarationName::ObjCOneArgSelector:
     case DeclarationName::ObjCMultiArgSelector:
     case DeclarationName::ObjCMultiArgSelector:
-      Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
+      LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
       return;
       return;
     case DeclarationName::CXXOperatorName:
     case DeclarationName::CXXOperatorName:
       assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
       assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
              "Invalid operator?");
              "Invalid operator?");
-      Emit8(Out, Name.getCXXOverloadedOperator());
+      LE.write<uint8_t>(Name.getCXXOverloadedOperator());
       return;
       return;
     case DeclarationName::CXXLiteralOperatorName:
     case DeclarationName::CXXLiteralOperatorName:
-      Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
+      LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
       return;
       return;
     case DeclarationName::CXXConstructorName:
     case DeclarationName::CXXConstructorName:
     case DeclarationName::CXXDestructorName:
     case DeclarationName::CXXDestructorName:
@@ -3390,11 +3417,13 @@ public:
 
 
   void EmitData(raw_ostream& Out, key_type_ref,
   void EmitData(raw_ostream& Out, key_type_ref,
                 data_type Lookup, unsigned DataLen) {
                 data_type Lookup, unsigned DataLen) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     uint64_t Start = Out.tell(); (void)Start;
     uint64_t Start = Out.tell(); (void)Start;
-    clang::io::Emit16(Out, Lookup.size());
+    LE.write<uint16_t>(Lookup.size());
     for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
     for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
          I != E; ++I)
          I != E; ++I)
-      clang::io::Emit32(Out, Writer.GetDeclRef(*I));
+      LE.write<uint32_t>(Writer.GetDeclRef(*I));
 
 
     assert(Out.tell() - Start == DataLen && "Data length is wrong");
     assert(Out.tell() - Start == DataLen && "Data length is wrong");
   }
   }
@@ -3487,7 +3516,8 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
   // Create the on-disk hash table in a buffer.
   // Create the on-disk hash table in a buffer.
   llvm::raw_svector_ostream Out(LookupTable);
   llvm::raw_svector_ostream Out(LookupTable);
   // Make sure that no bucket is at offset 0
   // Make sure that no bucket is at offset 0
-  clang::io::Emit32(Out, 0);
+  using namespace llvm::support;
+  endian::Writer<little>(Out).write<uint32_t>(0);
   return Generator.Emit(Out, Trait);
   return Generator.Emit(Out, Trait);
 }
 }
 
 
@@ -4187,17 +4217,19 @@ void ASTWriter::WriteASTCore(Sema &SemaRef,
       for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
       for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
                                            MEnd = Chain->ModuleMgr.end();
                                            MEnd = Chain->ModuleMgr.end();
            M != MEnd; ++M) {
            M != MEnd; ++M) {
+        using namespace llvm::support;
+        endian::Writer<little> LE(Out);
         StringRef FileName = (*M)->FileName;
         StringRef FileName = (*M)->FileName;
-        io::Emit16(Out, FileName.size());
+        LE.write<uint16_t>(FileName.size());
         Out.write(FileName.data(), FileName.size());
         Out.write(FileName.data(), FileName.size());
-        io::Emit32(Out, (*M)->SLocEntryBaseOffset);
-        io::Emit32(Out, (*M)->BaseIdentifierID);
-        io::Emit32(Out, (*M)->BaseMacroID);
-        io::Emit32(Out, (*M)->BasePreprocessedEntityID);
-        io::Emit32(Out, (*M)->BaseSubmoduleID);
-        io::Emit32(Out, (*M)->BaseSelectorID);
-        io::Emit32(Out, (*M)->BaseDeclID);
-        io::Emit32(Out, (*M)->BaseTypeIndex);
+        LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
+        LE.write<uint32_t>((*M)->BaseIdentifierID);
+        LE.write<uint32_t>((*M)->BaseMacroID);
+        LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
+        LE.write<uint32_t>((*M)->BaseSubmoduleID);
+        LE.write<uint32_t>((*M)->BaseSelectorID);
+        LE.write<uint32_t>((*M)->BaseDeclID);
+        LE.write<uint32_t>((*M)->BaseTypeIndex);
       }
       }
     }
     }
     Record.clear();
     Record.clear();

+ 8 - 4
lib/Serialization/GlobalModuleIndex.cpp

@@ -631,10 +631,12 @@ public:
 
 
   std::pair<unsigned,unsigned>
   std::pair<unsigned,unsigned>
   EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
   EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
+    using namespace llvm::support;
+    endian::Writer<little> LE(Out);
     unsigned KeyLen = Key.size();
     unsigned KeyLen = Key.size();
     unsigned DataLen = Data.size() * 4;
     unsigned DataLen = Data.size() * 4;
-    clang::io::Emit16(Out, KeyLen);
-    clang::io::Emit16(Out, DataLen);
+    LE.write<uint16_t>(KeyLen);
+    LE.write<uint16_t>(DataLen);
     return std::make_pair(KeyLen, DataLen);
     return std::make_pair(KeyLen, DataLen);
   }
   }
   
   
@@ -644,8 +646,9 @@ public:
 
 
   void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
   void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
                 unsigned DataLen) {
                 unsigned DataLen) {
+    using namespace llvm::support;
     for (unsigned I = 0, N = Data.size(); I != N; ++I)
     for (unsigned I = 0, N = Data.size(); I != N; ++I)
-      clang::io::Emit32(Out, Data[I]);
+      endian::Writer<little>(Out).write<uint32_t>(Data[I]);
   }
   }
 };
 };
 
 
@@ -707,9 +710,10 @@ void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
     SmallString<4096> IdentifierTable;
     SmallString<4096> IdentifierTable;
     uint32_t BucketOffset;
     uint32_t BucketOffset;
     {
     {
+      using namespace llvm::support;
       llvm::raw_svector_ostream Out(IdentifierTable);
       llvm::raw_svector_ostream Out(IdentifierTable);
       // Make sure that no bucket is at offset 0
       // Make sure that no bucket is at offset 0
-      clang::io::Emit32(Out, 0);
+      endian::Writer<little>(Out).write<uint32_t>(0);
       BucketOffset = Generator.Emit(Out, Trait);
       BucketOffset = Generator.Emit(Out, Trait);
     }
     }