LineIterator.cpp 2.8 KB

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