dump.cpp 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 NDEBUG
  5. #include "toolchain/lex/dump.h"
  6. #include <string>
  7. #include "common/raw_string_ostream.h"
  8. namespace Carbon::Lex {
  9. static LLVM_DUMP_METHOD auto Dump(const TokenizedBuffer& tokens)
  10. -> std::string {
  11. return PrintToString(tokens);
  12. }
  13. LLVM_DUMP_METHOD auto Dump(const TokenizedBuffer& tokens, TokenIndex token)
  14. -> std::string {
  15. RawStringOstream out;
  16. if (!token.has_value()) {
  17. out << "TokenIndex(<none>)";
  18. return out.TakeStr();
  19. }
  20. auto kind = tokens.GetKind(token);
  21. auto line = tokens.GetLineNumber(token);
  22. auto col = tokens.GetColumnNumber(token);
  23. out << "TokenIndex(kind: " << kind
  24. << ", loc: " << FormatEscaped(tokens.source().filename()) << ":" << line
  25. << ":" << col << ")";
  26. return out.TakeStr();
  27. }
  28. } // namespace Carbon::Lex
  29. #endif // NDEBUG