format_providers.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_
  6. #include "common/ostream.h"
  7. #include "llvm/Support/FormatVariadicDetails.h"
  8. namespace Carbon {
  9. // Selects a formatv string based on the value. If the format style is not
  10. // provided, as in `{0}`, the value uses standard formatting.
  11. //
  12. // When used, the true and false outputs are separated by a `|`.
  13. //
  14. // For example, `{0:true|false}` would yield standard bool formatting.
  15. //
  16. // If needed, the _full_ style string can be wrapped with `'` in order to
  17. // preserve prefix or suffix whitespace (which is stripped by formatv). For
  18. // example, `{0:' true | false '}` retains whitespace which would be dropped
  19. // before `true` and after `false`.
  20. struct BoolAsSelect {
  21. // NOLINTNEXTLINE(google-explicit-constructor)
  22. BoolAsSelect(bool value) : value(value) {}
  23. bool value;
  24. };
  25. // Selects a formatv string based on the value. If the format style is not
  26. // provided, as in `{0}`, the value uses standard formatting.
  27. //
  28. // The style is a series of match cases, separated by `|`. Each case is a pair
  29. // formatted as `<selector>:<output string>`.
  30. //
  31. // Supported selectors are:
  32. // - `=<value>`: Matches when the value is correct.
  33. // - Empty for the default. This is optional, although it's a fatal error to not
  34. // handle a value. If provided, it must be last.
  35. //
  36. // For example, `{0:=0:zero|=1:one|:other}` breaks down into:
  37. // - `=0` -> `zero`
  38. // - `=1` -> `one`
  39. // - default -> `other`
  40. //
  41. // As another example, `{0:=1:is|:are}` is a way to handle plural-based output.
  42. //
  43. // If needed, the _full_ style string can be wrapped with `'` in order to
  44. // preserve prefix or suffix whitespace (which is stripped by formatv). For
  45. // example, `{0:'=0: zero |=1: one '}` retains whitespace which would be dropped
  46. // after `one`.
  47. struct IntAsSelect {
  48. // NOLINTNEXTLINE(google-explicit-constructor)
  49. IntAsSelect(int value) : value(value) {}
  50. int value;
  51. };
  52. } // namespace Carbon
  53. // See BoolAsSelect.
  54. template <>
  55. struct llvm::format_provider<Carbon::BoolAsSelect> {
  56. static auto format(const Carbon::BoolAsSelect& wrapper, raw_ostream& out,
  57. StringRef style) -> void;
  58. };
  59. // See IntAsSelect.
  60. template <>
  61. struct llvm::format_provider<Carbon::IntAsSelect> {
  62. static auto format(const Carbon::IntAsSelect& wrapper, raw_ostream& out,
  63. StringRef style) -> void;
  64. };
  65. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_