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 <string> 19 20 namespace tint { 21 namespace diag { 22 23 class Diagnostic; 24 class List; 25 class Printer; 26 27 /// Formatter are used to print a list of diagnostics messages. 28 class Formatter { 29 public: 30 /// Style controls the formatter's output style. 31 struct Style { 32 /// include the file path for each diagnostic 33 bool print_file = true; 34 /// include the severity for each diagnostic 35 bool print_severity = true; 36 /// include the source line(s) for the diagnostic 37 bool print_line = true; 38 /// print a newline at the end of a diagnostic list 39 bool print_newline_at_end = true; 40 /// width of a tab character 41 size_t tab_width = 2u; 42 }; 43 44 /// Constructor for the formatter using a default style. 45 Formatter(); 46 47 /// Constructor for the formatter using the custom style. 48 /// @param style the style used for the formatter. 49 explicit Formatter(const Style& style); 50 51 ~Formatter(); 52 53 /// @param list the list of diagnostic messages to format 54 /// @param printer the printer used to display the formatted diagnostics 55 void format(const List& list, Printer* printer) const; 56 57 /// @return the list of diagnostics `list` formatted to a string. 58 /// @param list the list of diagnostic messages to format 59 std::string format(const List& list) const; 60 61 private: 62 struct State; 63 64 void format(const Diagnostic& diag, State& state) const; 65 66 const Style style_; 67 }; 68 69 } // namespace diag 70 } // namespace tint 71 72 #endif // SRC_DIAGNOSTIC_FORMATTER_H_ 73