forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_providers.h
72 lines (61 loc) · 2.32 KB
/
format_providers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_
#define CARBON_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_
#include "common/ostream.h"
#include "llvm/Support/FormatVariadicDetails.h"
namespace Carbon {
// Selects a formatv string based on the value.
//
// Supported format styles are:
// - None, as in `{0}`. This uses standard integer formatting.
// - Selector, as in `{0:true|false}`. The output string used is separated by a
// `|`, with the true case first. the example would yield standard bool
// formatting.
struct BoolAsSelect {
// NOLINTNEXTLINE(google-explicit-constructor)
BoolAsSelect(bool value) : value(value) {}
bool value;
};
// Selects a formatv string based on the value.
//
// Supported format styles are:
// - None, as in `{0}`. This uses standard integer formatting.
// - Selector, as in `{0:=0:zero|:default}`. This is detailed below.
// - Plural `s`, as in `{0:s}`. This outputs an `s` when the value is not 1,
// equivalent to `{0:=1:|:s}`.
//
// The style is a series of match cases, separated by `|`. Each case is a pair
// formatted as `<selector>:<output string>`.
//
// Supported selectors are:
// - `=<value>`: Matches when the value is correct.
// - Empty for the default. This is optional, although it's a fatal error to not
// handle a value. If provided, it must be last.
//
// For example, `{0:=0:zero|=1:one|:other}` breaks down into:
// - `=0` -> `zero`
// - `=1` -> `one`
// - default -> `other`
//
// As another example, `{0:=1:is|:are}` is a way to handle plural-based output.
struct IntAsSelect {
// NOLINTNEXTLINE(google-explicit-constructor)
IntAsSelect(int value) : value(value) {}
int value;
};
} // namespace Carbon
// See BoolAsSelect.
template <>
struct llvm::format_provider<Carbon::BoolAsSelect> {
static auto format(const Carbon::BoolAsSelect& wrapper, raw_ostream& out,
StringRef style) -> void;
};
// See IntAsSelect.
template <>
struct llvm::format_provider<Carbon::IntAsSelect> {
static auto format(const Carbon::IntAsSelect& wrapper, raw_ostream& out,
StringRef style) -> void;
};
#endif // CARBON_TOOLCHAIN_DIAGNOSTICS_FORMAT_PROVIDERS_H_