1 //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// 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 // This diagnostic client prints out their diagnostic messages. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Frontend/TextDiagnosticPrinter.h" 14 #include "clang/Basic/DiagnosticOptions.h" 15 #include "clang/Basic/SourceManager.h" 16 #include "clang/Frontend/TextDiagnostic.h" 17 #include "clang/Lex/Lexer.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/raw_ostream.h" 20 using namespace clang; 21 22 TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os, 23 DiagnosticOptions &DiagOpts, 24 bool _OwnsOutputStream) 25 : OS(os), DiagOpts(DiagOpts), OwnsOutputStream(_OwnsOutputStream) {} 26 27 TextDiagnosticPrinter::~TextDiagnosticPrinter() { 28 if (OwnsOutputStream) 29 delete &OS; 30 } 31 32 void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, 33 const Preprocessor *PP) { 34 // Build the TextDiagnostic utility. 35 TextDiag.reset(new TextDiagnostic(OS, LO, DiagOpts, PP)); 36 } 37 38 void TextDiagnosticPrinter::EndSourceFile() { 39 TextDiag.reset(); 40 } 41 42 /// Print any diagnostic option information to a raw_ostream. 43 /// 44 /// This implements all of the logic for adding diagnostic options to a message 45 /// (via OS). Each relevant option is comma separated and all are enclosed in 46 /// the standard bracketing: " [...]". 47 static void printDiagnosticOptions(raw_ostream &OS, 48 DiagnosticsEngine::Level Level, 49 const Diagnostic &Info, 50 const DiagnosticOptions &DiagOpts) { 51 bool Started = false; 52 if (DiagOpts.ShowOptionNames) { 53 // Handle special cases for non-warnings early. 54 if (Info.getID() == diag::fatal_too_many_errors) { 55 OS << " [-ferror-limit=]"; 56 return; 57 } 58 59 // The code below is somewhat fragile because we are essentially trying to 60 // report to the user what happened by inferring what the diagnostic engine 61 // did. Eventually it might make more sense to have the diagnostic engine 62 // include some "why" information in the diagnostic. 63 64 // If this is a warning which has been mapped to an error by the user (as 65 // inferred by checking whether the default mapping is to an error) then 66 // flag it as such. Note that diagnostics could also have been mapped by a 67 // pragma, but we don't currently have a way to distinguish this. 68 if (Level == DiagnosticsEngine::Error && 69 Info.getDiags()->getDiagnosticIDs()->isWarningOrExtension( 70 Info.getID()) && 71 !Info.getDiags()->getDiagnosticIDs()->isDefaultMappingAsError( 72 Info.getID())) { 73 OS << " [-Werror"; 74 Started = true; 75 } 76 77 StringRef Opt = 78 Info.getDiags()->getDiagnosticIDs()->getWarningOptionForDiag( 79 Info.getID()); 80 if (!Opt.empty()) { 81 OS << (Started ? "," : " [") 82 << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt; 83 StringRef OptValue = Info.getFlagValue(); 84 if (!OptValue.empty()) 85 OS << "=" << OptValue; 86 Started = true; 87 } 88 } 89 90 // If the user wants to see category information, include it too. 91 if (DiagOpts.ShowCategories) { 92 unsigned DiagCategory = 93 DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); 94 if (DiagCategory) { 95 OS << (Started ? "," : " ["); 96 Started = true; 97 if (DiagOpts.ShowCategories == 1) 98 OS << DiagCategory; 99 else { 100 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value"); 101 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory); 102 } 103 } 104 } 105 if (Started) 106 OS << ']'; 107 } 108 109 void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, 110 const Diagnostic &Info) { 111 // Default implementation (Warnings/errors count). 112 DiagnosticConsumer::HandleDiagnostic(Level, Info); 113 114 // Render the diagnostic message into a temporary buffer eagerly. We'll use 115 // this later as we print out the diagnostic to the terminal. 116 SmallString<100> OutStr; 117 Info.FormatDiagnostic(OutStr); 118 119 llvm::raw_svector_ostream DiagMessageStream(OutStr); 120 printDiagnosticOptions(DiagMessageStream, Level, Info, DiagOpts); 121 122 // Keeps track of the starting position of the location 123 // information (e.g., "foo.c:10:4:") that precedes the error 124 // message. We use this information to determine how long the 125 // file+line+column number prefix is. 126 uint64_t StartOfLocationInfo = OS.tell(); 127 128 if (!Prefix.empty()) 129 OS << Prefix << ": "; 130 131 // Use a dedicated, simpler path for diagnostics without a valid location. 132 // This is important as if the location is missing, we may be emitting 133 // diagnostics in a context that lacks language options, a source manager, or 134 // other infrastructure necessary when emitting more rich diagnostics. 135 if (!Info.getLocation().isValid()) { 136 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts.ShowColors); 137 TextDiagnostic::printDiagnosticMessage( 138 OS, /*IsSupplemental=*/Level == DiagnosticsEngine::Note, 139 DiagMessageStream.str(), OS.tell() - StartOfLocationInfo, 140 DiagOpts.MessageLength, DiagOpts.ShowColors); 141 OS.flush(); 142 return; 143 } 144 145 // Assert that the rest of our infrastructure is setup properly. 146 assert(Info.hasSourceManager() && 147 "Unexpected diagnostic with no source manager"); 148 assert(TextDiag && "Unexpected diagnostic outside source file processing"); 149 150 TextDiag->emitDiagnostic( 151 FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level, 152 DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints()); 153 154 OS.flush(); 155 } 156