LCOV - code coverage report
Current view: top level - include/llvm/TableGen - StringToOffsetTable.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 44 44 100.0 %
Date: 2017-09-14 15:23:50 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
      11             : #define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
      12             : 
      13             : #include "llvm/ADT/SmallString.h"
      14             : #include "llvm/ADT/StringExtras.h"
      15             : #include "llvm/ADT/StringMap.h"
      16             : #include "llvm/Support/raw_ostream.h"
      17             : #include <cctype>
      18             : 
      19             : namespace llvm {
      20             : 
      21             : /// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
      22             : /// and keeps track of their offset in a massive contiguous string allocation.
      23             : /// It can then output this string blob and use indexes into the string to
      24             : /// reference each piece.
      25         115 : class StringToOffsetTable {
      26             :   StringMap<unsigned> StringOffset;
      27             :   std::string AggregateString;
      28             : 
      29             : public:
      30           8 :   bool Empty() const { return StringOffset.empty(); }
      31             : 
      32      152814 :   unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
      33             :     auto IterBool =
      34      611256 :         StringOffset.insert(std::make_pair(Str, AggregateString.size()));
      35      152814 :     if (IterBool.second) {
      36             :       // Add the string to the aggregate if this is the first time found.
      37       48768 :       AggregateString.append(Str.begin(), Str.end());
      38       16256 :       if (appendZero)
      39        4899 :         AggregateString += '\0';
      40             :     }
      41             : 
      42      152814 :     return IterBool.first->second;
      43             :   }
      44             : 
      45          15 :   void EmitString(raw_ostream &O) {
      46             :     // Escape the string.
      47          30 :     SmallString<256> Str;
      48          45 :     raw_svector_ostream(Str).write_escaped(AggregateString);
      49          45 :     AggregateString = Str.str();
      50             : 
      51          15 :     O << "    \"";
      52          15 :     unsigned CharsPrinted = 0;
      53       99994 :     for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
      54       99979 :       if (CharsPrinted > 70) {
      55        1843 :         O << "\"\n    \"";
      56        1843 :         CharsPrinted = 0;
      57             :       }
      58      299937 :       O << AggregateString[i];
      59       99979 :       ++CharsPrinted;
      60             : 
      61             :       // Print escape sequences all together.
      62      199958 :       if (AggregateString[i] != '\\')
      63       88668 :         continue;
      64             : 
      65             :       assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
      66       22622 :       if (isdigit(AggregateString[i + 1])) {
      67             :         assert(isdigit(AggregateString[i + 2]) &&
      68             :                isdigit(AggregateString[i + 3]) &&
      69             :                "Expected 3 digit octal escape!");
      70       31332 :         O << AggregateString[++i];
      71       31332 :         O << AggregateString[++i];
      72       31332 :         O << AggregateString[++i];
      73       10444 :         CharsPrinted += 3;
      74             :       } else {
      75        2601 :         O << AggregateString[++i];
      76         867 :         ++CharsPrinted;
      77             :       }
      78             :     }
      79          15 :     O << "\"";
      80          15 :   }
      81             : 
      82             :   /// Emit the string using character literals. MSVC has a limitation that
      83             :   /// string literals cannot be longer than 64K.
      84           2 :   void EmitCharArray(raw_ostream &O) {
      85             :     assert(AggregateString.find(')') == std::string::npos &&
      86             :            "can't emit raw string with closing parens");
      87           2 :     int Count = 0;
      88           2 :     O << ' ';
      89      135218 :     for (char C : AggregateString) {
      90      135210 :       O << " \'";
      91             :       O.write_escaped(StringRef(&C, 1));
      92      135210 :       O << "\',";
      93      135210 :       Count++;
      94      135210 :       if (Count > 14) {
      95        9013 :         O << "\n ";
      96        9013 :         Count = 0;
      97             :       }
      98             :     }
      99           2 :     O << '\n';
     100           2 :   }
     101             : };
     102             : 
     103             : } // end namespace llvm
     104             : 
     105             : #endif

Generated by: LCOV version 1.13