Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1 | //===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===// |
| 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 | // This file defines the HTMLDiagnostics object. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "HTMLDiagnostics.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/Analysis/PathDiagnostic.h" |
| 19 | #include "clang/Rewrite/Rewriter.h" |
| 20 | #include "clang/Rewrite/HTMLRewrite.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/Streams.h" |
| 25 | #include "llvm/System/Path.h" |
| 26 | #include <fstream> |
| 27 | #include <sstream> |
| 28 | |
| 29 | using namespace clang; |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Boilerplate. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient { |
| 38 | llvm::sys::Path Directory, FilePrefix; |
| 39 | bool createdDir, noDir; |
| 40 | public: |
| 41 | HTMLDiagnostics(const std::string& prefix); |
| 42 | |
| 43 | virtual ~HTMLDiagnostics() {} |
| 44 | |
| 45 | virtual void HandlePathDiagnostic(const PathDiagnostic& D); |
| 46 | |
| 47 | void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P, unsigned num); |
| 48 | void HighlightRange(Rewriter& R, SourceRange Range, unsigned MainFileID); |
| 49 | }; |
| 50 | |
| 51 | } // end anonymous namespace |
| 52 | |
| 53 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix) |
| 54 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) { |
| 55 | |
| 56 | // All html files begin with "report" |
| 57 | FilePrefix.appendComponent("report"); |
| 58 | } |
| 59 | |
| 60 | PathDiagnosticClient* |
| 61 | clang::CreateHTMLDiagnosticClient(const std::string& prefix) { |
| 62 | |
| 63 | return new HTMLDiagnostics(prefix); |
| 64 | } |
| 65 | |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | // Report processing. |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
| 70 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) { |
| 71 | |
| 72 | if (D.empty()) |
| 73 | return; |
| 74 | |
| 75 | // Create the HTML directory if it is missing. |
| 76 | |
| 77 | if (!createdDir) { |
| 78 | createdDir = true; |
| 79 | Directory.createDirectoryOnDisk(true, NULL); |
| 80 | |
| 81 | if (!Directory.isDirectory()) { |
| 82 | llvm::cerr << "warning: could not create directory '" |
| 83 | << FilePrefix.toString() << "'\n"; |
| 84 | |
| 85 | noDir = true; |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (noDir) |
| 92 | return; |
| 93 | |
| 94 | // Create a new rewriter to generate HTML. |
| 95 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
| 96 | Rewriter R(SMgr); |
| 97 | |
| 98 | // Process the path. |
| 99 | |
| 100 | unsigned n = D.size(); |
| 101 | |
| 102 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 103 | I!=E; ++I, --n) { |
| 104 | |
| 105 | HandlePiece(R, *I, n); |
| 106 | } |
| 107 | |
| 108 | // Add line numbers, header, footer, etc. |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 110 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
| 111 | html::EscapeText(R, FileID); |
| 112 | html::AddLineNumbers(R, FileID); |
| 113 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 114 | // Add the name of the file. |
| 115 | |
| 116 | { |
| 117 | std::ostringstream os; |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 5d0f592 | 2008-03-27 17:25:28 +0000 | [diff] [blame^] | 119 | os << "<h1>"; |
| 120 | |
| 121 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
| 122 | const char* dname = Entry->getDir()->getName(); |
| 123 | |
| 124 | if (strcmp(dname,".") == 0) |
| 125 | os << html::EscapeText(llvm::sys::Path::GetCurrentDirectory().toString()); |
| 126 | else |
| 127 | os << html::EscapeText(dname); |
| 128 | |
| 129 | os << "/" << html::EscapeText(Entry->getName()) << "</h1>\n"; |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 130 | |
| 131 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 132 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 133 | |
| 134 | // Add CSS, header, and footer. |
| 135 | |
| 136 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
| 137 | |
| 138 | // Get the rewrite buffer. |
| 139 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 140 | |
| 141 | if (!Buf) { |
| 142 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Create the stream to write out the HTML. |
| 147 | std::ofstream os; |
| 148 | |
| 149 | { |
| 150 | // Create a path for the target HTML file. |
| 151 | llvm::sys::Path F(FilePrefix); |
| 152 | F.makeUnique(false, NULL); |
| 153 | |
| 154 | // Rename the file with an HTML extension. |
| 155 | llvm::sys::Path H(F); |
| 156 | H.appendSuffix("html"); |
| 157 | F.renamePathOnDisk(H, NULL); |
| 158 | |
| 159 | os.open(H.toString().c_str()); |
| 160 | |
| 161 | if (!os) { |
| 162 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Emit the HTML to disk. |
| 168 | |
| 169 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
| 170 | os << *I; |
| 171 | } |
| 172 | |
| 173 | void HTMLDiagnostics::HandlePiece(Rewriter& R, |
| 174 | const PathDiagnosticPiece& P, |
| 175 | unsigned num) { |
| 176 | |
| 177 | // For now, just draw a box above the line in question, and emit the |
| 178 | // warning. |
| 179 | |
| 180 | FullSourceLoc Pos = P.getLocation(); |
| 181 | |
| 182 | if (!Pos.isValid()) |
| 183 | return; |
| 184 | |
| 185 | SourceManager& SM = R.getSourceMgr(); |
| 186 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
| 187 | unsigned FileID = LPos.getLocation().getFileID(); |
| 188 | |
| 189 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 190 | |
| 191 | unsigned MainFileID = SM.getMainFileID(); |
| 192 | |
| 193 | if (FileID != MainFileID) |
| 194 | return; |
| 195 | |
| 196 | // Compute the column number. Rewind from the current position to the start |
| 197 | // of the line. |
| 198 | |
| 199 | unsigned ColNo = LPos.getColumnNumber(); |
| 200 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 201 | const char *LineStart = TokLogicalPtr-ColNo; |
| 202 | |
| 203 | // Create the html for the message. |
| 204 | |
| 205 | std::ostringstream os; |
| 206 | |
| 207 | os << "\n<tr><td class=\"num\"></td><td class=\"line\">" |
| 208 | << "<div class=\"msg\" style=\"margin-left:" |
| 209 | << ColNo << "ex\">"; |
| 210 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 211 | os << html::EscapeText(P.getString()) << "</div></td></tr>"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 212 | |
| 213 | // Insert the new html. |
| 214 | |
| 215 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 216 | const char* FileStart = Buf->getBufferStart(); |
| 217 | |
| 218 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart), |
| 219 | os.str()); |
| 220 | |
| 221 | // Now highlight the ranges. |
| 222 | |
| 223 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 224 | I != E; ++I) |
| 225 | HighlightRange(R, *I, MainFileID); |
| 226 | } |
| 227 | |
| 228 | void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range, |
| 229 | unsigned MainFileID) { |
| 230 | |
| 231 | SourceManager& SourceMgr = R.getSourceMgr(); |
| 232 | |
| 233 | SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin()); |
| 234 | unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart); |
| 235 | |
| 236 | SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd()); |
| 237 | unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd); |
| 238 | |
| 239 | if (EndLineNo < StartLineNo) |
| 240 | return; |
| 241 | |
| 242 | if (LogicalStart.getFileID() != MainFileID || |
| 243 | LogicalEnd.getFileID() != MainFileID) |
| 244 | return; |
| 245 | |
| 246 | // Compute the column number of the end. |
| 247 | unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd); |
| 248 | unsigned OldEndColNo = EndColNo; |
| 249 | |
| 250 | if (EndColNo) { |
| 251 | // Add in the length of the token, so that we cover multi-char tokens. |
| 252 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr); |
| 253 | } |
| 254 | |
| 255 | // Highlight the range. Make the span tag the outermost tag for the |
| 256 | // selected range. |
| 257 | |
| 258 | SourceLocation E = |
| 259 | LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo |
| 260 | ? -(OldEndColNo - EndColNo) |
| 261 | : EndColNo - OldEndColNo); |
| 262 | |
| 263 | R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">"); |
| 264 | R.InsertCStrAfter(E, "</span>"); |
| 265 | } |