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 #include "flang/Frontend/TextDiagnostic.h" 10 #include "clang/Basic/DiagnosticOptions.h" 11 #include "llvm/Support/raw_ostream.h" 12 13 using namespace Fortran::frontend; 14 15 // TODO: Similar enums are defined in clang/lib/Frontend/TextDiagnostic.cpp. 16 // It would be best to share them 17 static const enum llvm::raw_ostream::Colors noteColor = 18 llvm::raw_ostream::BLACK; 19 static const enum llvm::raw_ostream::Colors remarkColor = 20 llvm::raw_ostream::BLUE; 21 static const enum llvm::raw_ostream::Colors warningColor = 22 llvm::raw_ostream::MAGENTA; 23 static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED; 24 static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED; 25 // Used for changing only the bold attribute. 26 static const enum llvm::raw_ostream::Colors savedColor = 27 llvm::raw_ostream::SAVEDCOLOR; 28 29 TextDiagnostic::TextDiagnostic() {} 30 31 TextDiagnostic::~TextDiagnostic() {} 32 33 /*static*/ void TextDiagnostic::PrintDiagnosticLevel(llvm::raw_ostream &os, 34 clang::DiagnosticsEngine::Level level, bool showColors) { 35 if (showColors) { 36 // Print diagnostic category in bold and color 37 switch (level) { 38 case clang::DiagnosticsEngine::Ignored: 39 llvm_unreachable("Invalid diagnostic type"); 40 case clang::DiagnosticsEngine::Note: 41 os.changeColor(noteColor, true); 42 break; 43 case clang::DiagnosticsEngine::Remark: 44 os.changeColor(remarkColor, true); 45 break; 46 case clang::DiagnosticsEngine::Warning: 47 os.changeColor(warningColor, true); 48 break; 49 case clang::DiagnosticsEngine::Error: 50 os.changeColor(errorColor, true); 51 break; 52 case clang::DiagnosticsEngine::Fatal: 53 os.changeColor(fatalColor, true); 54 break; 55 } 56 } 57 58 switch (level) { 59 case clang::DiagnosticsEngine::Ignored: 60 llvm_unreachable("Invalid diagnostic type"); 61 case clang::DiagnosticsEngine::Note: 62 os << "note"; 63 break; 64 case clang::DiagnosticsEngine::Remark: 65 os << "remark"; 66 break; 67 case clang::DiagnosticsEngine::Warning: 68 os << "warning"; 69 break; 70 case clang::DiagnosticsEngine::Error: 71 os << "error"; 72 break; 73 case clang::DiagnosticsEngine::Fatal: 74 os << "fatal error"; 75 break; 76 } 77 78 os << ": "; 79 80 if (showColors) 81 os.resetColor(); 82 } 83 84 /*static*/ 85 void TextDiagnostic::PrintDiagnosticMessage(llvm::raw_ostream &os, 86 bool isSupplemental, llvm::StringRef message, bool showColors) { 87 if (showColors && !isSupplemental) { 88 // Print primary diagnostic messages in bold and without color. 89 os.changeColor(savedColor, true); 90 } 91 92 os << message; 93 94 if (showColors) 95 os.resetColor(); 96 os << '\n'; 97 } 98