dump.cpp 882 B

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