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; |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 40 | Preprocessor* PP; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 41 | PreprocessorFactory* PPF; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 42 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 43 | HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 44 | PreprocessorFactory* ppf); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 45 | |
| 46 | virtual ~HTMLDiagnostics() {} |
| 47 | |
| 48 | virtual void HandlePathDiagnostic(const PathDiagnostic& D); |
| 49 | |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 50 | void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P, |
| 51 | unsigned num, unsigned max); |
| 52 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 53 | void HighlightRange(Rewriter& R, SourceRange Range); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // end anonymous namespace |
| 57 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 58 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 59 | PreprocessorFactory* ppf) |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 60 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 61 | PP(pp), PPF(ppf) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 62 | |
| 63 | // All html files begin with "report" |
| 64 | FilePrefix.appendComponent("report"); |
| 65 | } |
| 66 | |
| 67 | PathDiagnosticClient* |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 68 | clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, |
| 69 | PreprocessorFactory* PPF) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 71 | return new HTMLDiagnostics(prefix, PP, PPF); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | // Report processing. |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
| 78 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) { |
| 79 | |
| 80 | if (D.empty()) |
| 81 | return; |
| 82 | |
| 83 | // Create the HTML directory if it is missing. |
| 84 | |
| 85 | if (!createdDir) { |
| 86 | createdDir = true; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 87 | std::string ErrorMsg; |
| 88 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 89 | |
| 90 | if (!Directory.isDirectory()) { |
| 91 | llvm::cerr << "warning: could not create directory '" |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 92 | << Directory.toString() << "'\n" |
| 93 | << "reason: " << ErrorMsg << '\n'; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 94 | |
| 95 | noDir = true; |
| 96 | |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (noDir) |
| 102 | return; |
| 103 | |
| 104 | // Create a new rewriter to generate HTML. |
| 105 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
| 106 | Rewriter R(SMgr); |
| 107 | |
| 108 | // Process the path. |
| 109 | |
| 110 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 111 | unsigned max = n; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 112 | |
| 113 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 114 | I!=E; ++I, --n) { |
| 115 | |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 116 | HandlePiece(R, *I, n, max); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Add line numbers, header, footer, etc. |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 121 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
| 122 | html::EscapeText(R, FileID); |
| 123 | html::AddLineNumbers(R, FileID); |
| 124 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 125 | // If we have a preprocessor, relex the file and syntax highlight. |
| 126 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 127 | // for example. |
| 128 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 129 | if (PP) html::SyntaxHighlight(R, FileID, *PP); |
| 130 | if (PPF) html::HighlightMacros(R, FileID, *PPF); |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 132 | // Get the full directory name of the analyzed file. |
| 133 | |
| 134 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
| 135 | std::string DirName(Entry->getDir()->getName()); |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 137 | if (DirName == ".") |
| 138 | DirName = llvm::sys::Path::GetCurrentDirectory().toString(); |
| 139 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 140 | // Add the name of the file as an <h1> tag. |
| 141 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 142 | { |
| 143 | std::ostringstream os; |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 145 | os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
| 146 | "<tr><td class=\"rowname\">File:</td><td>" |
| 147 | << html::EscapeText(DirName) |
| 148 | << html::EscapeText(Entry->getName()) |
| 149 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
| 150 | "<a href=\"#EndPath\">line " |
| 151 | << (*D.rbegin()).getLocation().getLogicalLineNumber() |
| 152 | << ", column " |
| 153 | << (*D.rbegin()).getLocation().getLogicalColumnNumber() |
| 154 | << "</a></td></tr>\n" |
| 155 | "<tr><td class=\"rowname\">Description:</td><td>" |
| 156 | << D.getDescription() |
| 157 | << "</td></tr>\n</table>\n" |
| 158 | "<h3>Annotated Source Code</h3>\n"; |
| 159 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 160 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 163 | // Embed meta-data tags. |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 164 | |
| 165 | const std::string& BugDesc = D.getDescription(); |
| 166 | |
| 167 | if (!BugDesc.empty()) { |
| 168 | std::ostringstream os; |
Ted Kremenek | 86b4381 | 2008-04-02 18:03:20 +0000 | [diff] [blame] | 169 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 170 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | { |
| 174 | std::ostringstream os; |
| 175 | os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n"; |
| 176 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 177 | } |
| 178 | |
| 179 | { |
| 180 | std::ostringstream os; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 181 | os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber() |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 182 | << " -->\n"; |
| 183 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 184 | } |
| 185 | |
| 186 | { |
| 187 | std::ostringstream os; |
| 188 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
| 189 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 190 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 191 | |
| 192 | // Add CSS, header, and footer. |
| 193 | |
| 194 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
| 195 | |
| 196 | // Get the rewrite buffer. |
| 197 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 198 | |
| 199 | if (!Buf) { |
| 200 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | // Create the stream to write out the HTML. |
| 205 | std::ofstream os; |
| 206 | |
| 207 | { |
| 208 | // Create a path for the target HTML file. |
| 209 | llvm::sys::Path F(FilePrefix); |
| 210 | F.makeUnique(false, NULL); |
| 211 | |
| 212 | // Rename the file with an HTML extension. |
| 213 | llvm::sys::Path H(F); |
| 214 | H.appendSuffix("html"); |
| 215 | F.renamePathOnDisk(H, NULL); |
| 216 | |
| 217 | os.open(H.toString().c_str()); |
| 218 | |
| 219 | if (!os) { |
| 220 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Emit the HTML to disk. |
| 226 | |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame^] | 227 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 228 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void HTMLDiagnostics::HandlePiece(Rewriter& R, |
| 232 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 233 | unsigned num, unsigned max) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 234 | |
| 235 | // For now, just draw a box above the line in question, and emit the |
| 236 | // warning. |
| 237 | |
| 238 | FullSourceLoc Pos = P.getLocation(); |
| 239 | |
| 240 | if (!Pos.isValid()) |
| 241 | return; |
| 242 | |
| 243 | SourceManager& SM = R.getSourceMgr(); |
| 244 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 245 | unsigned FileID = SM.getCanonicalFileID(LPos.getLocation()); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 246 | |
| 247 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 248 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 249 | if (!SM.isFromMainFile(LPos.getLocation())) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 250 | return; |
| 251 | |
| 252 | // Compute the column number. Rewind from the current position to the start |
| 253 | // of the line. |
| 254 | |
| 255 | unsigned ColNo = LPos.getColumnNumber(); |
| 256 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 257 | const char *LineStart = TokLogicalPtr-ColNo; |
| 258 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 259 | // Compute the margin offset by counting tabs and non-tabs. |
| 260 | |
| 261 | unsigned PosNo = 0; |
| 262 | |
| 263 | for (const char* c = LineStart; c != TokLogicalPtr; ++c) |
Ted Kremenek | 8fb0016 | 2008-03-31 23:14:05 +0000 | [diff] [blame] | 264 | PosNo += *c == '\t' ? 4 : 1; |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 266 | // Create the html for the message. |
| 267 | |
| 268 | std::ostringstream os; |
| 269 | |
| 270 | os << "\n<tr><td class=\"num\"></td><td class=\"line\">" |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 271 | << "<div id=\""; |
| 272 | |
| 273 | if (num == max) |
| 274 | os << "EndPath"; |
| 275 | else |
| 276 | os << "Path" << num; |
| 277 | |
| 278 | os << "\" class=\"msg\" style=\"margin-left:" |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 279 | << PosNo << "ex\">"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 718ceb1 | 2008-04-02 21:04:20 +0000 | [diff] [blame] | 281 | if (max > 1) |
| 282 | os << "<span class=\"PathIndex\">[" << num << "]</span> "; |
| 283 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 284 | os << html::EscapeText(P.getString()) << "</div></td></tr>"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 285 | |
| 286 | // Insert the new html. |
| 287 | |
| 288 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 289 | const char* FileStart = Buf->getBufferStart(); |
| 290 | |
| 291 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart), |
| 292 | os.str()); |
| 293 | |
| 294 | // Now highlight the ranges. |
| 295 | |
| 296 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 297 | I != E; ++I) |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 298 | HighlightRange(R, *I); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 301 | void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 303 | SourceManager& SM = R.getSourceMgr(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 305 | SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin()); |
| 306 | unsigned StartLineNo = SM.getLineNumber(LogicalStart); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 308 | SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd()); |
| 309 | unsigned EndLineNo = SM.getLineNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 310 | |
| 311 | if (EndLineNo < StartLineNo) |
| 312 | return; |
| 313 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 314 | if (!SM.isFromMainFile(LogicalStart) || |
| 315 | !SM.isFromMainFile(LogicalEnd)) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 316 | return; |
| 317 | |
| 318 | // Compute the column number of the end. |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 319 | unsigned EndColNo = SM.getColumnNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 320 | unsigned OldEndColNo = EndColNo; |
| 321 | |
| 322 | if (EndColNo) { |
| 323 | // Add in the length of the token, so that we cover multi-char tokens. |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 324 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Highlight the range. Make the span tag the outermost tag for the |
| 328 | // selected range. |
Ted Kremenek | dab4ead | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 329 | |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 330 | SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Ted Kremenek | b747814 | 2008-04-17 18:37:23 +0000 | [diff] [blame] | 331 | |
| 332 | html::HighlightRange(R, LogicalStart, E, |
| 333 | "<span class=\"mrange\">", "</span>"); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 334 | } |