dump.cpp 863 B

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