[PATCH] D47033: Support: Add a raw_ostream::write_zeros() function. NFCI.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 17 15:15:51 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL332675: Support: Add a raw_ostream::write_zeros() function. NFCI. (authored by pcc, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D47033?vs=147395&id=147398#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D47033

Files:
  cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
  llvm/trunk/include/llvm/Support/raw_ostream.h
  llvm/trunk/lib/Support/raw_ostream.cpp


Index: llvm/trunk/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h
+++ llvm/trunk/include/llvm/Support/raw_ostream.h
@@ -242,6 +242,9 @@
   /// indent - Insert 'NumSpaces' spaces.
   raw_ostream &indent(unsigned NumSpaces);
 
+  /// write_zeros - Insert 'NumZeros' nulls.
+  raw_ostream &write_zeros(unsigned NumZeros);
+
   /// Changes the foreground color of text that will be output from this point
   /// forward.
   /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
Index: llvm/trunk/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp
+++ llvm/trunk/lib/Support/raw_ostream.cpp
@@ -455,23 +455,35 @@
   return *this;
 }
 
-/// indent - Insert 'NumSpaces' spaces.
-raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
-  static const char Spaces[] = "                                "
-                               "                                "
-                               "                ";
+template <char C>
+static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) {
+  static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
+                               C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
+                               C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
+                               C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
+                               C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C};
 
   // Usually the indentation is small, handle it with a fastpath.
-  if (NumSpaces < array_lengthof(Spaces))
-    return write(Spaces, NumSpaces);
+  if (NumChars < array_lengthof(Chars))
+    return OS.write(Chars, NumChars);
 
-  while (NumSpaces) {
-    unsigned NumToWrite = std::min(NumSpaces,
-                                   (unsigned)array_lengthof(Spaces)-1);
-    write(Spaces, NumToWrite);
-    NumSpaces -= NumToWrite;
+  while (NumChars) {
+    unsigned NumToWrite = std::min(NumChars,
+                                   (unsigned)array_lengthof(Chars)-1);
+    OS.write(Chars, NumToWrite);
+    NumChars -= NumToWrite;
   }
-  return *this;
+  return OS;
+}
+
+/// indent - Insert 'NumSpaces' spaces.
+raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
+  return write_padding<' '>(*this, NumSpaces);
+}
+
+/// write_zeros - Insert 'NumZeros' nulls.
+raw_ostream &raw_ostream::write_zeros(unsigned NumZeros) {
+  return write_padding<'\0'>(*this, NumZeros);
 }
 
 void raw_ostream::anchor() {}
Index: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
===================================================================
--- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
+++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
@@ -1372,8 +1372,7 @@
   // and coverage mappings is a multiple of 8.
   if (size_t Rem = OS.str().size() % 8) {
     CoverageMappingSize += 8 - Rem;
-    for (size_t I = 0, S = 8 - Rem; I < S; ++I)
-      OS << '\0';
+    OS.write_zeros(8 - Rem);
   }
   auto *FilenamesAndMappingsVal =
       llvm::ConstantDataArray::getString(Ctx, OS.str(), false);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47033.147398.patch
Type: text/x-patch
Size: 3209 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180517/d65d9026/attachment.bin>


More information about the llvm-commits mailing list