line.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TESTING_FILE_TEST_LINE_H_
  5. #define CARBON_TESTING_FILE_TEST_LINE_H_
  6. #include "common/ostream.h"
  7. #include "llvm/ADT/StringRef.h"
  8. namespace Carbon::Testing {
  9. // Interface for lines.
  10. class FileTestLineBase : public Printable<FileTestLineBase> {
  11. public:
  12. explicit FileTestLineBase(int file_number, int line_number)
  13. : file_number_(file_number), line_number_(line_number) {}
  14. virtual ~FileTestLineBase() = default;
  15. // Prints the autoupdated line.
  16. virtual auto Print(llvm::raw_ostream& out) const -> void = 0;
  17. virtual auto is_blank() const -> bool = 0;
  18. auto file_number() const -> int { return file_number_; }
  19. auto line_number() const -> int { return line_number_; }
  20. private:
  21. int file_number_;
  22. int line_number_;
  23. };
  24. // A line in the original file test.
  25. //
  26. // `final` because we use pointer arithmetic on this type.
  27. class FileTestLine final : public FileTestLineBase {
  28. public:
  29. explicit FileTestLine(int file_number, int line_number, llvm::StringRef line)
  30. : FileTestLineBase(file_number, line_number), line_(line) {}
  31. auto Print(llvm::raw_ostream& out) const -> void override { out << line_; }
  32. auto is_blank() const -> bool override { return line_.empty(); }
  33. auto indent() const -> llvm::StringRef {
  34. return line_.substr(0, line_.find_first_not_of(" \n"));
  35. }
  36. private:
  37. llvm::StringRef line_;
  38. };
  39. } // namespace Carbon::Testing
  40. #endif // CARBON_TESTING_FILE_TEST_LINE_H_