LCOV - code coverage report
Current view: top level - lib/Support - LineIterator.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 36 36 100.0 %
Date: 2017-09-14 15:23:50 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- LineIterator.cpp - Implementation of line iteration ----------------===//
       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/Support/LineIterator.h"
      11             : #include "llvm/Support/MemoryBuffer.h"
      12             : 
      13             : using namespace llvm;
      14             : 
      15             : static bool isAtLineEnd(const char *P) {
      16    14458678 :   if (*P == '\n')
      17             :     return true;
      18    14030320 :   if (*P == '\r' && *(P + 1) == '\n')
      19             :     return true;
      20             :   return false;
      21             : }
      22             : 
      23             : static bool skipIfAtLineEnd(const char *&P) {
      24      715544 :   if (*P == '\n') {
      25      378600 :     ++P;
      26             :     return true;
      27             :   }
      28      336944 :   if (*P == '\r' && *(P + 1) == '\n') {
      29           2 :     P += 2;
      30             :     return true;
      31             :   }
      32             :   return false;
      33             : }
      34             : 
      35        4184 : line_iterator::line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks,
      36        4184 :                              char CommentMarker)
      37        8368 :     : Buffer(Buffer.getBufferSize() ? &Buffer : nullptr),
      38             :       CommentMarker(CommentMarker), SkipBlanks(SkipBlanks), LineNumber(1),
      39        4184 :       CurrentLine(Buffer.getBufferSize() ? Buffer.getBufferStart() : nullptr,
      40       12552 :                   0) {
      41             :   // Ensure that if we are constructed on a non-empty memory buffer that it is
      42             :   // a null terminated buffer.
      43        8368 :   if (Buffer.getBufferSize()) {
      44             :     assert(Buffer.getBufferEnd()[0] == '\0');
      45             :     // Make sure we don't skip a leading newline if we're keeping blanks
      46        4160 :     if (SkipBlanks || !isAtLineEnd(Buffer.getBufferStart()))
      47        4157 :       advance();
      48             :   }
      49        4184 : }
      50             : 
      51      381716 : void line_iterator::advance() {
      52             :   assert(Buffer && "Cannot advance past the end!");
      53             : 
      54      763432 :   const char *Pos = CurrentLine.end();
      55             :   assert(Pos == Buffer->getBufferStart() || isAtLineEnd(Pos) || *Pos == '\0');
      56             : 
      57      377559 :   if (skipIfAtLineEnd(Pos))
      58      377559 :     ++LineNumber;
      59      381716 :   if (!SkipBlanks && isAtLineEnd(Pos)) {
      60             :     // Nothing to do for a blank line.
      61      332786 :   } else if (CommentMarker == '\0') {
      62             :     // If we're not stripping comments, this is simpler.
      63          24 :     while (skipIfAtLineEnd(Pos))
      64          12 :       ++LineNumber;
      65             :   } else {
      66             :     // Skip comments and count line numbers, which is a bit more complex.
      67             :     for (;;) {
      68        4786 :       if (isAtLineEnd(Pos) && !SkipBlanks)
      69             :         break;
      70        3536 :       if (*Pos == CommentMarker)
      71             :         do {
      72       27122 :           ++Pos;
      73       27122 :         } while (*Pos != '\0' && !isAtLineEnd(Pos));
      74        3536 :       if (!skipIfAtLineEnd(Pos))
      75             :         break;
      76        1031 :       ++LineNumber;
      77             :     }
      78             :   }
      79             : 
      80      381716 :   if (*Pos == '\0') {
      81             :     // We've hit the end of the buffer, reset ourselves to the end state.
      82        3316 :     Buffer = nullptr;
      83        3316 :     CurrentLine = StringRef();
      84        3316 :     return;
      85             :   }
      86             : 
      87             :   // Measure the line.
      88             :   size_t Length = 0;
      89    41378143 :   while (Pos[Length] != '\0' && !isAtLineEnd(&Pos[Length])) {
      90    13666581 :     ++Length;
      91             :   }
      92             : 
      93      378400 :   CurrentLine = StringRef(Pos, Length);
      94             : }

Generated by: LCOV version 1.13