1//===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#include "flang/Frontend/TextDiagnostic.h"
14#include "clang/Basic/DiagnosticOptions.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace Fortran::frontend;
18
19// TODO: Similar enums are defined in clang/lib/Frontend/TextDiagnostic.cpp.
20// It would be best to share them
21static const enum llvm::raw_ostream::Colors noteColor =
22 llvm::raw_ostream::BLACK;
23static const enum llvm::raw_ostream::Colors remarkColor =
24 llvm::raw_ostream::BLUE;
25static const enum llvm::raw_ostream::Colors warningColor =
26 llvm::raw_ostream::MAGENTA;
27static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED;
28static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED;
29// Used for changing only the bold attribute.
30static const enum llvm::raw_ostream::Colors savedColor =
31 llvm::raw_ostream::SAVEDCOLOR;
32
33TextDiagnostic::TextDiagnostic() {}
34
35TextDiagnostic::~TextDiagnostic() {}
36
37/*static*/ void
38TextDiagnostic::printDiagnosticLevel(llvm::raw_ostream &os,
39 clang::DiagnosticsEngine::Level level,
40 bool showColors) {
41 if (showColors) {
42 // Print diagnostic category in bold and color
43 switch (level) {
44 case clang::DiagnosticsEngine::Ignored:
45 llvm_unreachable("Invalid diagnostic type");
46 case clang::DiagnosticsEngine::Note:
47 os.changeColor(noteColor, true);
48 break;
49 case clang::DiagnosticsEngine::Remark:
50 os.changeColor(remarkColor, true);
51 break;
52 case clang::DiagnosticsEngine::Warning:
53 os.changeColor(warningColor, true);
54 break;
55 case clang::DiagnosticsEngine::Error:
56 os.changeColor(errorColor, true);
57 break;
58 case clang::DiagnosticsEngine::Fatal:
59 os.changeColor(fatalColor, true);
60 break;
61 }
62 }
63
64 switch (level) {
65 case clang::DiagnosticsEngine::Ignored:
66 llvm_unreachable("Invalid diagnostic type");
67 case clang::DiagnosticsEngine::Note:
68 os << "note";
69 break;
70 case clang::DiagnosticsEngine::Remark:
71 os << "remark";
72 break;
73 case clang::DiagnosticsEngine::Warning:
74 os << "warning";
75 break;
76 case clang::DiagnosticsEngine::Error:
77 os << "error";
78 break;
79 case clang::DiagnosticsEngine::Fatal:
80 os << "fatal error";
81 break;
82 }
83
84 os << ": ";
85
86 if (showColors)
87 os.resetColor();
88}
89
90/*static*/
91void TextDiagnostic::printDiagnosticMessage(llvm::raw_ostream &os,
92 bool isSupplemental,
93 llvm::StringRef message,
94 bool showColors) {
95 if (showColors && !isSupplemental) {
96 // Print primary diagnostic messages in bold and without color.
97 os.changeColor(savedColor, true);
98 }
99
100 os << message;
101
102 if (showColors)
103 os.resetColor();
104 os << '\n';
105}
106

source code of flang/lib/Frontend/TextDiagnostic.cpp