format_providers.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "toolchain/diagnostics/format_providers.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. auto llvm::format_provider<Carbon::BoolAsSelect>::format(
  8. const Carbon::BoolAsSelect& wrapper, raw_ostream& out, StringRef style)
  9. -> void {
  10. if (style.empty()) {
  11. llvm::format_provider<bool>::format(wrapper.value, out, style);
  12. return;
  13. }
  14. // Remove wrapping quotes if present.
  15. if (style.starts_with('\'') && style.ends_with('\'')) {
  16. style = style.drop_front().drop_back();
  17. }
  18. auto sep = style.find('|');
  19. CARBON_CHECK(
  20. sep != llvm::StringRef::npos,
  21. "BoolAsSelect requires a `|` separating true and false results: `{0}`",
  22. style);
  23. CARBON_CHECK(style.find('|', sep + 1) == llvm::StringRef::npos,
  24. "BoolAsSelect only allows one `|`: `{0}`", style);
  25. if (wrapper.value) {
  26. out << style.take_front(sep);
  27. } else {
  28. out << style.drop_front(sep + 1);
  29. }
  30. }
  31. auto llvm::format_provider<Carbon::IntAsSelect>::format(
  32. const Carbon::IntAsSelect& wrapper, raw_ostream& out, StringRef style)
  33. -> void {
  34. if (style.empty()) {
  35. llvm::format_provider<int>::format(wrapper.value, out, style);
  36. return;
  37. }
  38. // Remove wrapping quotes if present.
  39. if (style.starts_with('\'') && style.ends_with('\'')) {
  40. style = style.drop_front().drop_back();
  41. }
  42. auto cursor = style;
  43. while (!cursor.empty()) {
  44. auto case_sep = cursor.find("|");
  45. auto token = cursor.substr(0, case_sep);
  46. if (case_sep == llvm::StringRef::npos) {
  47. cursor = llvm::StringRef();
  48. } else {
  49. cursor = cursor.drop_front(case_sep + 1);
  50. }
  51. auto pair_sep = token.find(':');
  52. CARBON_CHECK(pair_sep != llvm::StringRef::npos,
  53. "IntAsSelect requires a `:` separating each comparison and "
  54. "output string: `{0}`",
  55. style);
  56. auto comp = token.take_front(pair_sep);
  57. auto output_string = token.drop_front(pair_sep + 1);
  58. if (comp.empty()) {
  59. // Default case.
  60. CARBON_CHECK(cursor.empty(),
  61. "IntAsSelect requires the default case be last: `{0}`",
  62. style);
  63. out << output_string;
  64. return;
  65. } else if (comp.consume_front("=")) {
  66. // Equality comparison.
  67. int value;
  68. CARBON_CHECK(to_integer(comp, value),
  69. "IntAsSelect has invalid value in comparison: `{0}`", style);
  70. if (value == wrapper.value) {
  71. out << output_string;
  72. return;
  73. }
  74. } else {
  75. CARBON_FATAL("IntAsSelect has unrecognized comparison: `{0}`", style);
  76. }
  77. }
  78. CARBON_FATAL("IntAsSelect doesn't handle `{0}`: `{1}`", wrapper.value, style);
  79. }