1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SRC_DIAGNOSTIC_FORMATTER_H_
16 #define SRC_DIAGNOSTIC_FORMATTER_H_
17 
18 #include <memory>
19 #include <string>
20 
21 namespace tint {
22 namespace diag {
23 
24 class Diagnostic;
25 class List;
26 class Printer;
27 
28 /// Formatter are used to print a list of diagnostics messages.
29 class Formatter {
30  public:
31   /// Style controls the formatter's output style.
32   struct Style {
33     /// include the file path for each diagnostic
34     bool print_file = true;
35     /// include the severity for each diagnostic
36     bool print_severity = true;
37     /// include the source line(s) for the diagnostic
38     bool print_line = true;
39   };
40 
41   /// Constructor for the formatter using a default style.
42   Formatter();
43 
44   /// Constructor for the formatter using the custom style.
45   /// @param style the style used for the formatter.
46   explicit Formatter(const Style& style);
47 
48   ~Formatter();
49 
50   /// @param list the list of diagnostic messages to format
51   /// @param printer the printer used to display the formatted diagnostics
52   void format(const List& list, Printer* printer) const;
53 
54   /// @return the list of diagnostics |list| formatted to a string.
55   /// @param list the list of diagnostic messages to format
56   std::string format(const List& list) const;
57 
58  private:
59   struct State;
60 
61   void format(const Diagnostic& diag, State& state) const;
62 
63   Style const style_;
64 };
65 
66 }  // namespace diag
67 }  // namespace tint
68 
69 #endif  // SRC_DIAGNOSTIC_FORMATTER_H_
70