2
0

LineIterator.cpp 2.6 KB

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