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/SourceManager.h" 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm/Support/raw_ostream.h" 17 using namespace clang; 18 19 LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os, 20 DiagnosticOptions *diags, 21 bool _OwnsOutputStream) 22 : OS(os), LangOpts(0), DiagOpts(diags), 23 OwnsOutputStream(_OwnsOutputStream) { 24 } 25 26 LogDiagnosticPrinter::~LogDiagnosticPrinter() { 27 if (OwnsOutputStream) 28 delete &OS; 29 } 30 31 static StringRef getLevelName(DiagnosticsEngine::Level Level) { 32 switch (Level) { 33 case DiagnosticsEngine::Ignored: return "ignored"; 34 case DiagnosticsEngine::Remark: return "remark"; 35 case DiagnosticsEngine::Note: return "note"; 36 case DiagnosticsEngine::Warning: return "warning"; 37 case DiagnosticsEngine::Error: return "error"; 38 case DiagnosticsEngine::Fatal: return "fatal error"; 39 } 40 llvm_unreachable("Invalid DiagnosticsEngine level!"); 41 } 42 43 // Escape XML characters inside the raw string. 44 static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) { 45 for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) { 46 char c = *I; 47 switch (c) { 48 default: OS << c; break; 49 case '&': OS << "&"; break; 50 case '<': OS << "<"; break; 51 case '>': OS << ">"; break; 52 case '\'': OS << "'"; break; 53 case '\"': OS << """; break; 54 } 55 } 56 } 57 58 void LogDiagnosticPrinter::EndSourceFile() { 59 // We emit all the diagnostics in EndSourceFile. However, we don't emit any 60 // entry if no diagnostics were present. 61 // 62 // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we 63 // will miss any diagnostics which are emitted after and outside the 64 // translation unit processing. 65 if (Entries.empty()) 66 return; 67 68 // Write to a temporary string to ensure atomic write of diagnostic object. 69 SmallString<512> Msg; 70 llvm::raw_svector_ostream OS(Msg); 71 72 OS << "<dict>\n"; 73 if (!MainFilename.empty()) { 74 OS << " <key>main-file</key>\n" 75 << " <string>"; 76 emitString(OS, MainFilename); 77 OS << "</string>\n"; 78 } 79 if (!DwarfDebugFlags.empty()) { 80 OS << " <key>dwarf-debug-flags</key>\n" 81 << " <string>"; 82 emitString(OS, DwarfDebugFlags); 83 OS << "</string>\n"; 84 } 85 OS << " <key>diagnostics</key>\n"; 86 OS << " <array>\n"; 87 for (unsigned i = 0, e = Entries.size(); i != e; ++i) { 88 DiagEntry &DE = Entries[i]; 89 90 OS << " <dict>\n"; 91 OS << " <key>level</key>\n" 92 << " <string>"; 93 emitString(OS, getLevelName(DE.DiagnosticLevel)); 94 OS << "</string>\n"; 95 if (!DE.Filename.empty()) { 96 OS << " <key>filename</key>\n" 97 << " <string>"; 98 emitString(OS, DE.Filename); 99 OS << "</string>\n"; 100 } 101 if (DE.Line != 0) { 102 OS << " <key>line</key>\n" 103 << " <integer>" << DE.Line << "</integer>\n"; 104 } 105 if (DE.Column != 0) { 106 OS << " <key>column</key>\n" 107 << " <integer>" << DE.Column << "</integer>\n"; 108 } 109 if (!DE.Message.empty()) { 110 OS << " <key>message</key>\n" 111 << " <string>"; 112 emitString(OS, DE.Message); 113 OS << "</string>\n"; 114 } 115 OS << " </dict>\n"; 116 } 117 OS << " </array>\n"; 118 OS << "</dict>\n"; 119 120 this->OS << OS.str(); 121 } 122 123 void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, 124 const Diagnostic &Info) { 125 // Default implementation (Warnings/errors count). 126 DiagnosticConsumer::HandleDiagnostic(Level, Info); 127 128 // Initialize the main file name, if we haven't already fetched it. 129 if (MainFilename.empty() && Info.hasSourceManager()) { 130 const SourceManager &SM = Info.getSourceManager(); 131 FileID FID = SM.getMainFileID(); 132 if (!FID.isInvalid()) { 133 const FileEntry *FE = SM.getFileEntryForID(FID); 134 if (FE && FE->isValid()) 135 MainFilename = FE->getName(); 136 } 137 } 138 139 // Create the diag entry. 140 DiagEntry DE; 141 DE.DiagnosticID = Info.getID(); 142 DE.DiagnosticLevel = Level; 143 144 // Format the message. 145 SmallString<100> MessageStr; 146 Info.FormatDiagnostic(MessageStr); 147 DE.Message = MessageStr.str(); 148 149 // Set the location information. 150 DE.Filename = ""; 151 DE.Line = DE.Column = 0; 152 if (Info.getLocation().isValid() && Info.hasSourceManager()) { 153 const SourceManager &SM = Info.getSourceManager(); 154 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); 155 156 if (PLoc.isInvalid()) { 157 // At least print the file name if available: 158 FileID FID = SM.getFileID(Info.getLocation()); 159 if (!FID.isInvalid()) { 160 const FileEntry *FE = SM.getFileEntryForID(FID); 161 if (FE && FE->isValid()) 162 DE.Filename = FE->getName(); 163 } 164 } else { 165 DE.Filename = PLoc.getFilename(); 166 DE.Line = PLoc.getLine(); 167 DE.Column = PLoc.getColumn(); 168 } 169 } 170 171 // Record the diagnostic entry. 172 Entries.push_back(DE); 173 } 174 175