1 //===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Frontend/LogDiagnosticPrinter.h" 11 #include "clang/Basic/DiagnosticOptions.h" 12 #include "clang/Basic/FileManager.h" 13 #include "clang/Basic/PlistSupport.h" 14 #include "clang/Basic/SourceManager.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/Support/ErrorHandling.h" 17 #include "llvm/Support/raw_ostream.h" 18 using namespace clang; 19 using namespace markup; 20 21 LogDiagnosticPrinter::LogDiagnosticPrinter( 22 raw_ostream &os, DiagnosticOptions *diags, 23 std::unique_ptr<raw_ostream> StreamOwner) 24 : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr), 25 DiagOpts(diags) {} 26 27 static StringRef getLevelName(DiagnosticsEngine::Level Level) { 28 switch (Level) { 29 case DiagnosticsEngine::Ignored: return "ignored"; 30 case DiagnosticsEngine::Remark: return "remark"; 31 case DiagnosticsEngine::Note: return "note"; 32 case DiagnosticsEngine::Warning: return "warning"; 33 case DiagnosticsEngine::Error: return "error"; 34 case DiagnosticsEngine::Fatal: return "fatal error"; 35 } 36 llvm_unreachable("Invalid DiagnosticsEngine level!"); 37 } 38 39 void 40 LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS, 41 const LogDiagnosticPrinter::DiagEntry &DE) { 42 OS << " <dict>\n"; 43 OS << " <key>level</key>\n" 44 << " "; 45 EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n'; 46 if (!DE.Filename.empty()) { 47 OS << " <key>filename</key>\n" 48 << " "; 49 EmitString(OS, DE.Filename) << '\n'; 50 } 51 if (DE.Line != 0) { 52 OS << " <key>line</key>\n" 53 << " "; 54 EmitInteger(OS, DE.Line) << '\n'; 55 } 56 if (DE.Column != 0) { 57 OS << " <key>column</key>\n" 58 << " "; 59 EmitInteger(OS, DE.Column) << '\n'; 60 } 61 if (!DE.Message.empty()) { 62 OS << " <key>message</key>\n" 63 << " "; 64 EmitString(OS, DE.Message) << '\n'; 65 } 66 OS << " </dict>\n"; 67 } 68 69 void LogDiagnosticPrinter::EndSourceFile() { 70 // We emit all the diagnostics in EndSourceFile. However, we don't emit any 71 // entry if no diagnostics were present. 72 // 73 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we 74 // will miss any diagnostics which are emitted after and outside the 75 // translation unit processing. 76 if (Entries.empty()) 77 return; 78 79 // Write to a temporary string to ensure atomic write of diagnostic object. 80 SmallString<512> Msg; 81 llvm::raw_svector_ostream OS(Msg); 82 83 OS << "<dict>\n"; 84 if (!MainFilename.empty()) { 85 OS << " <key>main-file</key>\n" 86 << " "; 87 EmitString(OS, MainFilename) << '\n'; 88 } 89 if (!DwarfDebugFlags.empty()) { 90 OS << " <key>dwarf-debug-flags</key>\n" 91 << " "; 92 EmitString(OS, DwarfDebugFlags) << '\n'; 93 } 94 OS << " <key>diagnostics</key>\n"; 95 OS << " <array>\n"; 96 for (auto &DE : Entries) 97 EmitDiagEntry(OS, DE); 98 OS << " </array>\n"; 99 OS << "</dict>\n"; 100 101 this->OS << OS.str(); 102 } 103 104 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, 105 const Diagnostic &Info) { 106 // Default implementation (Warnings/errors count). 107 DiagnosticConsumer::HandleDiagnostic(Level, Info); 108 109 // Initialize the main file name, if we haven't already fetched it. 110 if (MainFilename.empty() && Info.hasSourceManager()) { 111 const SourceManager &SM = Info.getSourceManager(); 112 FileID FID = SM.getMainFileID(); 113 if (!FID.isInvalid()) { 114 const FileEntry *FE = SM.getFileEntryForID(FID); 115 if (FE && FE->isValid()) 116 MainFilename = FE->getName(); 117 } 118 } 119 120 // Create the diag entry. 121 DiagEntry DE; 122 DE.DiagnosticID = Info.getID(); 123 DE.DiagnosticLevel = Level; 124 125 // Format the message. 126 SmallString<100> MessageStr; 127 Info.FormatDiagnostic(MessageStr); 128 DE.Message = MessageStr.str(); 129 130 // Set the location information. 131 DE.Filename = ""; 132 DE.Line = DE.Column = 0; 133 if (Info.getLocation().isValid() && Info.hasSourceManager()) { 134 const SourceManager &SM = Info.getSourceManager(); 135 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); 136 137 if (PLoc.isInvalid()) { 138 // At least print the file name if available: 139 FileID FID = SM.getFileID(Info.getLocation()); 140 if (!FID.isInvalid()) { 141 const FileEntry *FE = SM.getFileEntryForID(FID); 142 if (FE && FE->isValid()) 143 DE.Filename = FE->getName(); 144 } 145 } else { 146 DE.Filename = PLoc.getFilename(); 147 DE.Line = PLoc.getLine(); 148 DE.Column = PLoc.getColumn(); 149 } 150 } 151 152 // Record the diagnostic entry. 153 Entries.push_back(DE); 154 } 155 156