line.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 {
  11. public:
  12. explicit FileTestLineBase(int line_number) : line_number_(line_number) {}
  13. virtual ~FileTestLineBase() {}
  14. // Prints the autoupdated line.
  15. virtual auto Print(llvm::raw_ostream& out) const -> void = 0;
  16. auto line_number() const -> int { return line_number_; }
  17. private:
  18. int line_number_;
  19. };
  20. // A line in the original file test.
  21. class FileTestLine : public FileTestLineBase {
  22. public:
  23. explicit FileTestLine(int line_number, llvm::StringRef line)
  24. : FileTestLineBase(line_number), line_(line) {}
  25. auto Print(llvm::raw_ostream& out) const -> void override { out << line_; }
  26. auto indent() const -> llvm::StringRef {
  27. return line_.substr(0, line_.find_first_not_of(" \n"));
  28. }
  29. private:
  30. llvm::StringRef line_;
  31. };
  32. } // namespace Carbon::Testing
  33. #endif // CARBON_TESTING_FILE_TEST_LINE_H_