LCOV - code coverage report
Current view: top level - lib/Support - Twine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 88 123 71.5 %
Date: 2018-02-23 15:42:53 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
       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             : #include "llvm/ADT/Twine.h"
      11             : #include "llvm/ADT/SmallString.h"
      12             : #include "llvm/Support/Debug.h"
      13             : #include "llvm/Support/FormatVariadic.h"
      14             : #include "llvm/Support/raw_ostream.h"
      15             : using namespace llvm;
      16             : 
      17     5798265 : std::string Twine::str() const {
      18             :   // If we're storing only a std::string, just return it.
      19     5798265 :   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
      20      619207 :     return *LHS.stdString;
      21             : 
      22             :   // If we're storing a formatv_object, we can avoid an extra copy by formatting
      23             :   // it immediately and returning the result.
      24     5179058 :   if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
      25         162 :     return LHS.formatvObject->str();
      26             : 
      27             :   // Otherwise, flatten and copy the contents first.
      28             :   SmallString<256> Vec;
      29    10357812 :   return toStringRef(Vec).str();
      30             : }
      31             : 
      32     6809098 : void Twine::toVector(SmallVectorImpl<char> &Out) const {
      33             :   raw_svector_ostream OS(Out);
      34     6809155 :   print(OS);
      35     6809180 : }
      36             : 
      37     1681522 : StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
      38             :   if (isUnary()) {
      39     1475957 :     switch (getLHSKind()) {
      40      321088 :     case CStringKind:
      41             :       // Already null terminated, yay!
      42      642176 :       return StringRef(LHS.cString);
      43      673557 :     case StdStringKind: {
      44      673557 :       const std::string *str = LHS.stdString;
      45     1347114 :       return StringRef(str->c_str(), str->size());
      46             :     }
      47             :     default:
      48             :       break;
      49             :     }
      50             :   }
      51      686877 :   toVector(Out);
      52      686884 :   Out.push_back(0);
      53             :   Out.pop_back();
      54      686885 :   return StringRef(Out.data(), Out.size());
      55             : }
      56             : 
      57    95038211 : void Twine::printOneChild(raw_ostream &OS, Child Ptr,
      58             :                           NodeKind Kind) const {
      59    95038211 :   switch (Kind) {
      60             :   case Twine::NullKind: break;
      61             :   case Twine::EmptyKind: break;
      62    17043633 :   case Twine::TwineKind:
      63    17043633 :     Ptr.twine->print(OS);
      64    17043604 :     break;
      65    33745950 :   case Twine::CStringKind:
      66    33745950 :     OS << Ptr.cString;
      67    33746110 :     break;
      68     4214168 :   case Twine::StdStringKind:
      69     4214168 :     OS << *Ptr.stdString;
      70             :     break;
      71    13709181 :   case Twine::StringRefKind:
      72    13709181 :     OS << *Ptr.stringRef;
      73    13709314 :     break;
      74       84708 :   case Twine::SmallStringKind:
      75       84708 :     OS << *Ptr.smallString;
      76             :     break;
      77        5053 :   case Twine::FormatvObjectKind:
      78        5053 :     OS << *Ptr.formatvObject;
      79        5053 :     break;
      80     6140758 :   case Twine::CharKind:
      81     6140758 :     OS << Ptr.character;
      82             :     break;
      83     3602278 :   case Twine::DecUIKind:
      84     3602278 :     OS << Ptr.decUI;
      85             :     break;
      86     2518665 :   case Twine::DecIKind:
      87     2518665 :     OS << Ptr.decI;
      88             :     break;
      89      397647 :   case Twine::DecULKind:
      90      397647 :     OS << *Ptr.decUL;
      91      397647 :     break;
      92        7843 :   case Twine::DecLKind:
      93        7843 :     OS << *Ptr.decL;
      94        7843 :     break;
      95           5 :   case Twine::DecULLKind:
      96           5 :     OS << *Ptr.decULL;
      97           5 :     break;
      98           0 :   case Twine::DecLLKind:
      99           0 :     OS << *Ptr.decLL;
     100           0 :     break;
     101       60483 :   case Twine::UHexKind:
     102       60483 :     OS.write_hex(*Ptr.uHex);
     103       60483 :     break;
     104             :   }
     105    95038577 : }
     106             : 
     107          30 : void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
     108             :                               NodeKind Kind) const {
     109          30 :   switch (Kind) {
     110           2 :   case Twine::NullKind:
     111           2 :     OS << "null"; break;
     112           7 :   case Twine::EmptyKind:
     113           7 :     OS << "empty"; break;
     114           3 :   case Twine::TwineKind:
     115           3 :     OS << "rope:";
     116           3 :     Ptr.twine->printRepr(OS);
     117           3 :     break;
     118          13 :   case Twine::CStringKind:
     119          13 :     OS << "cstring:\""
     120          13 :        << Ptr.cString << "\"";
     121          13 :     break;
     122           0 :   case Twine::StdStringKind:
     123           0 :     OS << "std::string:\""
     124           0 :        << Ptr.stdString << "\"";
     125           0 :     break;
     126           0 :   case Twine::StringRefKind:
     127           0 :     OS << "stringref:\""
     128           0 :        << Ptr.stringRef << "\"";
     129           0 :     break;
     130           3 :   case Twine::SmallStringKind:
     131           6 :     OS << "smallstring:\"" << *Ptr.smallString << "\"";
     132           3 :     break;
     133           2 :   case Twine::FormatvObjectKind:
     134           2 :     OS << "formatv:\"" << *Ptr.formatvObject << "\"";
     135           2 :     break;
     136           0 :   case Twine::CharKind:
     137           0 :     OS << "char:\"" << Ptr.character << "\"";
     138           0 :     break;
     139           0 :   case Twine::DecUIKind:
     140           0 :     OS << "decUI:\"" << Ptr.decUI << "\"";
     141           0 :     break;
     142           0 :   case Twine::DecIKind:
     143           0 :     OS << "decI:\"" << Ptr.decI << "\"";
     144           0 :     break;
     145           0 :   case Twine::DecULKind:
     146           0 :     OS << "decUL:\"" << *Ptr.decUL << "\"";
     147           0 :     break;
     148           0 :   case Twine::DecLKind:
     149           0 :     OS << "decL:\"" << *Ptr.decL << "\"";
     150           0 :     break;
     151           0 :   case Twine::DecULLKind:
     152           0 :     OS << "decULL:\"" << *Ptr.decULL << "\"";
     153           0 :     break;
     154           0 :   case Twine::DecLLKind:
     155           0 :     OS << "decLL:\"" << *Ptr.decLL << "\"";
     156           0 :     break;
     157           0 :   case Twine::UHexKind:
     158           0 :     OS << "uhex:\"" << Ptr.uHex << "\"";
     159           0 :     break;
     160             :   }
     161          30 : }
     162             : 
     163    47521209 : void Twine::print(raw_ostream &OS) const {
     164    47521209 :   printOneChild(OS, LHS, getLHSKind());
     165    47520986 :   printOneChild(OS, RHS, getRHSKind());
     166    47521026 : }
     167             : 
     168          15 : void Twine::printRepr(raw_ostream &OS) const {
     169          15 :   OS << "(Twine ";
     170          15 :   printOneChildRepr(OS, LHS, getLHSKind());
     171          15 :   OS << " ";
     172          15 :   printOneChildRepr(OS, RHS, getRHSKind());
     173          15 :   OS << ")";
     174          15 : }
     175             : 
     176             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     177             : LLVM_DUMP_METHOD void Twine::dump() const {
     178             :   print(dbgs());
     179             : }
     180             : 
     181             : LLVM_DUMP_METHOD void Twine::dumpRepr() const {
     182             :   printRepr(dbgs());
     183             : }
     184             : #endif

Generated by: LCOV version 1.13