Ted Kremenek | fd75e31 | 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 | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Ted Kremenek | fd75e31 | 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 | |
Ted Kremenek | 7287979 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 47 | void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P, |
| 48 | unsigned num, unsigned max); |
| 49 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 50 | void HighlightRange(Rewriter& R, SourceRange Range); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } // end anonymous namespace |
| 54 | |
| 55 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix) |
| 56 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) { |
| 57 | |
| 58 | // All html files begin with "report" |
| 59 | FilePrefix.appendComponent("report"); |
| 60 | } |
| 61 | |
| 62 | PathDiagnosticClient* |
| 63 | clang::CreateHTMLDiagnosticClient(const std::string& prefix) { |
| 64 | |
| 65 | return new HTMLDiagnostics(prefix); |
| 66 | } |
| 67 | |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | // Report processing. |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
| 72 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) { |
| 73 | |
| 74 | if (D.empty()) |
| 75 | return; |
| 76 | |
| 77 | // Create the HTML directory if it is missing. |
| 78 | |
| 79 | if (!createdDir) { |
| 80 | createdDir = true; |
Ted Kremenek | 1436990 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 81 | std::string ErrorMsg; |
| 82 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 83 | |
| 84 | if (!Directory.isDirectory()) { |
| 85 | llvm::cerr << "warning: could not create directory '" |
Ted Kremenek | 1436990 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 86 | << Directory.toString() << "'\n" |
| 87 | << "reason: " << ErrorMsg << '\n'; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 88 | |
| 89 | noDir = true; |
| 90 | |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (noDir) |
| 96 | return; |
| 97 | |
| 98 | // Create a new rewriter to generate HTML. |
| 99 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
| 100 | Rewriter R(SMgr); |
| 101 | |
| 102 | // Process the path. |
| 103 | |
| 104 | unsigned n = D.size(); |
Ted Kremenek | 7287979 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 105 | unsigned max = n; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 106 | |
| 107 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 108 | I!=E; ++I, --n) { |
| 109 | |
Ted Kremenek | 7287979 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 110 | HandlePiece(R, *I, n, max); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Add line numbers, header, footer, etc. |
Ted Kremenek | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 115 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
| 116 | html::EscapeText(R, FileID); |
| 117 | html::AddLineNumbers(R, FileID); |
| 118 | |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 119 | // Get the full directory name of the analyzed file. |
| 120 | |
| 121 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
| 122 | std::string DirName(Entry->getDir()->getName()); |
Ted Kremenek | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 124 | if (DirName == ".") |
| 125 | DirName = llvm::sys::Path::GetCurrentDirectory().toString(); |
| 126 | |
| 127 | // Add the name of the file as an <h1> tag. |
| 128 | |
Ted Kremenek | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 129 | { |
| 130 | std::ostringstream os; |
Ted Kremenek | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 132 | os << "<h1>" << html::EscapeText(DirName) |
| 133 | << "/" << html::EscapeText(Entry->getName()) << "</h1>\n"; |
Ted Kremenek | dfb08d4 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 134 | |
| 135 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | 44a70e1 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 138 | // Embed meta-data tags. |
Ted Kremenek | 44a70e1 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 139 | |
| 140 | const std::string& BugDesc = D.getDescription(); |
| 141 | |
| 142 | if (!BugDesc.empty()) { |
| 143 | std::ostringstream os; |
Ted Kremenek | 1cae1f4 | 2008-04-02 18:03:20 +0000 | [diff] [blame] | 144 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Ted Kremenek | 44a70e1 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 145 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | { |
| 149 | std::ostringstream os; |
| 150 | os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n"; |
| 151 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | std::ostringstream os; |
Ted Kremenek | 1436990 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 156 | os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber() |
Ted Kremenek | fe9b00f | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 157 | << " -->\n"; |
| 158 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 159 | } |
| 160 | |
| 161 | { |
| 162 | std::ostringstream os; |
| 163 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
| 164 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 165 | } |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 166 | |
| 167 | // Add CSS, header, and footer. |
| 168 | |
| 169 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
| 170 | |
| 171 | // Get the rewrite buffer. |
| 172 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 173 | |
| 174 | if (!Buf) { |
| 175 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Create the stream to write out the HTML. |
| 180 | std::ofstream os; |
| 181 | |
| 182 | { |
| 183 | // Create a path for the target HTML file. |
| 184 | llvm::sys::Path F(FilePrefix); |
| 185 | F.makeUnique(false, NULL); |
| 186 | |
| 187 | // Rename the file with an HTML extension. |
| 188 | llvm::sys::Path H(F); |
| 189 | H.appendSuffix("html"); |
| 190 | F.renamePathOnDisk(H, NULL); |
| 191 | |
| 192 | os.open(H.toString().c_str()); |
| 193 | |
| 194 | if (!os) { |
| 195 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Emit the HTML to disk. |
| 201 | |
Ted Kremenek | 88aeff9 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 202 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) { |
| 203 | // Expand tabs. |
| 204 | if (*I == '\t') |
| 205 | os << " "; |
| 206 | else |
| 207 | os << *I; |
| 208 | } |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void HTMLDiagnostics::HandlePiece(Rewriter& R, |
| 212 | const PathDiagnosticPiece& P, |
Ted Kremenek | 7287979 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 213 | unsigned num, unsigned max) { |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 214 | |
| 215 | // For now, just draw a box above the line in question, and emit the |
| 216 | // warning. |
| 217 | |
| 218 | FullSourceLoc Pos = P.getLocation(); |
| 219 | |
| 220 | if (!Pos.isValid()) |
| 221 | return; |
| 222 | |
| 223 | SourceManager& SM = R.getSourceMgr(); |
| 224 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 225 | unsigned FileID = SM.getCanonicalFileID(LPos.getLocation()); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 226 | |
| 227 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 228 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 229 | if (!SM.isFromMainFile(LPos.getLocation())) |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 230 | return; |
| 231 | |
| 232 | // Compute the column number. Rewind from the current position to the start |
| 233 | // of the line. |
| 234 | |
| 235 | unsigned ColNo = LPos.getColumnNumber(); |
| 236 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 237 | const char *LineStart = TokLogicalPtr-ColNo; |
| 238 | |
Ted Kremenek | dbdb01e | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 239 | // Compute the margin offset by counting tabs and non-tabs. |
| 240 | |
| 241 | unsigned PosNo = 0; |
| 242 | |
| 243 | for (const char* c = LineStart; c != TokLogicalPtr; ++c) |
Ted Kremenek | cd2eb9b | 2008-03-31 23:14:05 +0000 | [diff] [blame] | 244 | PosNo += *c == '\t' ? 4 : 1; |
Ted Kremenek | dbdb01e | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 246 | // Create the html for the message. |
| 247 | |
| 248 | std::ostringstream os; |
| 249 | |
| 250 | os << "\n<tr><td class=\"num\"></td><td class=\"line\">" |
Ted Kremenek | 7287979 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 251 | << "<div id=\""; |
| 252 | |
| 253 | if (num == max) |
| 254 | os << "EndPath"; |
| 255 | else |
| 256 | os << "Path" << num; |
| 257 | |
| 258 | os << "\" class=\"msg\" style=\"margin-left:" |
Ted Kremenek | dbdb01e | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 259 | << PosNo << "ex\">"; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 260 | |
Ted Kremenek | d8a68b2 | 2008-04-02 21:04:20 +0000 | [diff] [blame] | 261 | if (max > 1) |
| 262 | os << "<span class=\"PathIndex\">[" << num << "]</span> "; |
| 263 | |
Ted Kremenek | 57ba65c | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 264 | os << html::EscapeText(P.getString()) << "</div></td></tr>"; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 265 | |
| 266 | // Insert the new html. |
| 267 | |
| 268 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 269 | const char* FileStart = Buf->getBufferStart(); |
| 270 | |
| 271 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart), |
| 272 | os.str()); |
| 273 | |
| 274 | // Now highlight the ranges. |
| 275 | |
| 276 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 277 | I != E; ++I) |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 278 | HighlightRange(R, *I); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 281 | void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) { |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 283 | SourceManager& SM = R.getSourceMgr(); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 285 | SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin()); |
| 286 | unsigned StartLineNo = SM.getLineNumber(LogicalStart); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 288 | SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd()); |
| 289 | unsigned EndLineNo = SM.getLineNumber(LogicalEnd); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 290 | |
| 291 | if (EndLineNo < StartLineNo) |
| 292 | return; |
| 293 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 294 | if (!SM.isFromMainFile(LogicalStart) || |
| 295 | !SM.isFromMainFile(LogicalEnd)) |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 296 | return; |
| 297 | |
| 298 | // Compute the column number of the end. |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 299 | unsigned EndColNo = SM.getColumnNumber(LogicalEnd); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 300 | unsigned OldEndColNo = EndColNo; |
| 301 | |
| 302 | if (EndColNo) { |
| 303 | // Add in the length of the token, so that we cover multi-char tokens. |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 304 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM); |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Highlight the range. Make the span tag the outermost tag for the |
| 308 | // selected range. |
| 309 | |
| 310 | SourceLocation E = |
| 311 | LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo |
| 312 | ? -(OldEndColNo - EndColNo) |
| 313 | : EndColNo - OldEndColNo); |
| 314 | |
| 315 | R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">"); |
| 316 | R.InsertCStrAfter(E, "</span>"); |
Ted Kremenek | 352bc0c | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 317 | |
| 318 | if (EndLineNo == StartLineNo) |
| 319 | return; |
| 320 | |
| 321 | // Add in </span><span> tags for intermediate lines. |
| 322 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 323 | unsigned FileID = SM.getCanonicalFileID(LogicalStart); |
| 324 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
Ted Kremenek | 352bc0c | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 326 | unsigned Pos = SM.getFullFilePos(LogicalStart); |
| 327 | unsigned EndPos = SM.getFullFilePos(E); |
Ted Kremenek | 352bc0c | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 328 | const char* buf = Buf->getBufferStart(); |
| 329 | |
| 330 | for (; Pos != EndPos; ++Pos) { |
| 331 | |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 332 | SourceLocation L = SourceLocation::getFileLoc(FileID, Pos); |
| 333 | unsigned Col = SM.getColumnNumber(L); |
Ted Kremenek | 352bc0c | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 334 | |
| 335 | if (Col == 1) { |
| 336 | |
| 337 | // Start if a new line. Scan to see if we hit anything that is not |
| 338 | // whitespace or a newline. |
| 339 | |
| 340 | unsigned PosTmp = Pos; |
| 341 | bool NewLine = false; |
| 342 | |
| 343 | for ( ; PosTmp != EndPos ; ++PosTmp) { |
| 344 | switch (buf[PosTmp]) { |
| 345 | case ' ': |
| 346 | case '\t': continue; |
| 347 | case '\n': |
| 348 | NewLine = true; |
| 349 | break; |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | if (PosTmp == EndPos) |
| 358 | break; |
| 359 | |
| 360 | Pos = PosTmp; |
| 361 | |
| 362 | // Don't highlight a blank line. |
| 363 | if (NewLine) |
| 364 | continue; |
| 365 | |
| 366 | // This line contains text that we should highlight. |
| 367 | // Ignore leading whitespace. |
Ted Kremenek | e4a9de1 | 2008-04-14 21:06:04 +0000 | [diff] [blame^] | 368 | L = SourceLocation::getFileLoc(FileID, Pos); |
Ted Kremenek | 352bc0c | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 369 | R.InsertCStrAfter(L, "<span class=\"mrange\">"); |
| 370 | } |
| 371 | else if (buf[Pos] == '\n') |
| 372 | R.InsertCStrBefore(L, "</span>"); |
| 373 | } |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 374 | } |