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 | |
Zhongxing Xu | 8148839 | 2008-08-24 02:33:36 +0000 | [diff] [blame] | 14 | #include "clang/Driver/HTMLDiagnostics.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathDiagnostic.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 20 | #include "clang/Rewrite/Rewriter.h" |
| 21 | #include "clang/Rewrite/HTMLRewrite.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/Streams.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 27 | #include "llvm/System/Path.h" |
| 28 | #include <fstream> |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Boilerplate. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient { |
| 39 | llvm::sys::Path Directory, FilePrefix; |
| 40 | bool createdDir, noDir; |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 41 | Preprocessor* PP; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 42 | PreprocessorFactory* PPF; |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 43 | std::vector<const PathDiagnostic*> BatchedDiags; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 44 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 45 | HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 46 | PreprocessorFactory* ppf); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 48 | virtual ~HTMLDiagnostics(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 50 | virtual void HandlePathDiagnostic(const PathDiagnostic* D); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 52 | void HandlePiece(Rewriter& R, unsigned BugFileID, |
| 53 | const PathDiagnosticPiece& P, unsigned num, unsigned max); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 55 | void HighlightRange(Rewriter& R, unsigned BugFileID, SourceRange Range); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 56 | |
| 57 | void ReportDiag(const PathDiagnostic& D); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | } // end anonymous namespace |
| 61 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 62 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 63 | PreprocessorFactory* ppf) |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 64 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 65 | PP(pp), PPF(ppf) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 66 | |
| 67 | // All html files begin with "report" |
| 68 | FilePrefix.appendComponent("report"); |
| 69 | } |
| 70 | |
| 71 | PathDiagnosticClient* |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 72 | clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, |
| 73 | PreprocessorFactory* PPF) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 75 | return new HTMLDiagnostics(prefix, PP, PPF); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // Report processing. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 82 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
| 83 | if (!D) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 84 | return; |
| 85 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 86 | if (D->empty()) { |
| 87 | delete D; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | BatchedDiags.push_back(D); |
| 92 | } |
| 93 | |
| 94 | HTMLDiagnostics::~HTMLDiagnostics() { |
| 95 | |
| 96 | while (!BatchedDiags.empty()) { |
| 97 | const PathDiagnostic* D = BatchedDiags.back(); |
| 98 | BatchedDiags.pop_back(); |
| 99 | ReportDiag(*D); |
| 100 | delete D; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) { |
| 105 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 106 | // Create the HTML directory if it is missing. |
| 107 | |
| 108 | if (!createdDir) { |
| 109 | createdDir = true; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 110 | std::string ErrorMsg; |
| 111 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 112 | |
| 113 | if (!Directory.isDirectory()) { |
| 114 | llvm::cerr << "warning: could not create directory '" |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 115 | << Directory.toString() << "'\n" |
| 116 | << "reason: " << ErrorMsg << '\n'; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 117 | |
| 118 | noDir = true; |
| 119 | |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (noDir) |
| 125 | return; |
| 126 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 127 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 128 | |
| 129 | unsigned FileID = 0; |
| 130 | bool FileIDInitialized = false; |
| 131 | |
| 132 | // Verify that the entire path is from the same FileID. |
| 133 | for (PathDiagnostic::const_iterator I=D.begin(), E=D.end(); I != E; ++I) { |
| 134 | |
| 135 | FullSourceLoc L = I->getLocation(); |
| 136 | |
| 137 | if (!L.isFileID()) |
| 138 | return; // FIXME: Emit a warning? |
| 139 | |
| 140 | if (!FileIDInitialized) { |
| 141 | FileID = L.getCanonicalFileID(); |
| 142 | FileIDInitialized = true; |
| 143 | } |
| 144 | else if (L.getCanonicalFileID() != FileID) |
| 145 | return; // FIXME: Emit a warning? |
| 146 | |
| 147 | // Check the source ranges. |
| 148 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 149 | RE=I->ranges_end(); RI!=RE; ++RI) { |
| 150 | |
| 151 | SourceLocation L = RI->getBegin(); |
| 152 | |
| 153 | if (!L.isFileID()) |
| 154 | return; // FIXME: Emit a warning? |
| 155 | |
| 156 | if (SMgr.getCanonicalFileID(L) != FileID) |
| 157 | return; // FIXME: Emit a warning? |
| 158 | |
| 159 | L = RI->getEnd(); |
| 160 | |
| 161 | if (!L.isFileID()) |
| 162 | return; // FIXME: Emit a warning? |
| 163 | |
| 164 | if (SMgr.getCanonicalFileID(L) != FileID) |
| 165 | return; // FIXME: Emit a warning? |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (!FileIDInitialized) |
| 170 | return; // FIXME: Emit a warning? |
| 171 | |
| 172 | // Create a new rewriter to generate HTML. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 173 | Rewriter R(SMgr); |
| 174 | |
| 175 | // Process the path. |
| 176 | |
| 177 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 178 | unsigned max = n; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 179 | |
| 180 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 181 | I!=E; ++I, --n) { |
| 182 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 183 | HandlePiece(R, FileID, *I, n, max); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Add line numbers, header, footer, etc. |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 188 | // unsigned FileID = R.getSourceMgr().getMainFileID(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 189 | html::EscapeText(R, FileID); |
| 190 | html::AddLineNumbers(R, FileID); |
| 191 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 192 | // If we have a preprocessor, relex the file and syntax highlight. |
| 193 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 194 | // for example. |
| 195 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 196 | if (PP) html::SyntaxHighlight(R, FileID, *PP); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 197 | |
| 198 | // FIXME: We eventually want to use PPF to create a fresh Preprocessor, |
| 199 | // once we have worked out the bugs. |
| 200 | // |
| 201 | // if (PPF) html::HighlightMacros(R, FileID, *PPF); |
| 202 | // |
| 203 | if (PP) html::HighlightMacros(R, FileID, *PP); |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 205 | // Get the full directory name of the analyzed file. |
| 206 | |
| 207 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 209 | // This is a cludge; basically we want to append either the full |
| 210 | // working directory if we have no directory information. This is |
| 211 | // a work in progress. |
| 212 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 213 | std::string DirName = ""; |
| 214 | |
| 215 | if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { |
| 216 | llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); |
| 217 | DirName = P.toString() + "/"; |
| 218 | } |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 220 | // Add the name of the file as an <h1> tag. |
| 221 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 222 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 223 | std::string s; |
| 224 | llvm::raw_string_ostream os(s); |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 226 | os << "<!-- REPORTHEADER -->\n" |
| 227 | << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 228 | "<tr><td class=\"rowname\">File:</td><td>" |
| 229 | << html::EscapeText(DirName) |
| 230 | << html::EscapeText(Entry->getName()) |
| 231 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
| 232 | "<a href=\"#EndPath\">line " |
| 233 | << (*D.rbegin()).getLocation().getLogicalLineNumber() |
| 234 | << ", column " |
| 235 | << (*D.rbegin()).getLocation().getLogicalColumnNumber() |
| 236 | << "</a></td></tr>\n" |
| 237 | "<tr><td class=\"rowname\">Description:</td><td>" |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 238 | << D.getDescription() << "</td></tr>\n"; |
| 239 | |
| 240 | // Output any other meta data. |
| 241 | |
| 242 | for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); |
| 243 | I!=E; ++I) { |
| 244 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
| 245 | } |
| 246 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 247 | os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n" |
| 248 | "<h3>Annotated Source Code</h3>\n"; |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 250 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 253 | // Embed meta-data tags. |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 254 | |
| 255 | const std::string& BugDesc = D.getDescription(); |
| 256 | |
| 257 | if (!BugDesc.empty()) { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 258 | std::string s; |
| 259 | llvm::raw_string_ostream os(s); |
Ted Kremenek | 86b4381 | 2008-04-02 18:03:20 +0000 | [diff] [blame] | 260 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 261 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 264 | const std::string& BugCategory = D.getCategory(); |
| 265 | |
| 266 | if (!BugCategory.empty()) { |
| 267 | std::string s; |
| 268 | llvm::raw_string_ostream os(s); |
| 269 | os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; |
| 270 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 271 | } |
| 272 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 273 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 274 | std::string s; |
| 275 | llvm::raw_string_ostream os(s); |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 276 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 277 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 278 | } |
| 279 | |
| 280 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 281 | std::string s; |
| 282 | llvm::raw_string_ostream os(s); |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 283 | os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber() |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 284 | << " -->\n"; |
| 285 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 286 | } |
| 287 | |
| 288 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 289 | std::string s; |
| 290 | llvm::raw_string_ostream os(s); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 291 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
| 292 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 293 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 294 | |
| 295 | // Add CSS, header, and footer. |
| 296 | |
Ted Kremenek | f6f593f | 2008-07-07 18:31:05 +0000 | [diff] [blame] | 297 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName()); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 298 | |
| 299 | // Get the rewrite buffer. |
| 300 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 301 | |
| 302 | if (!Buf) { |
| 303 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Create the stream to write out the HTML. |
| 308 | std::ofstream os; |
| 309 | |
| 310 | { |
| 311 | // Create a path for the target HTML file. |
| 312 | llvm::sys::Path F(FilePrefix); |
| 313 | F.makeUnique(false, NULL); |
| 314 | |
| 315 | // Rename the file with an HTML extension. |
| 316 | llvm::sys::Path H(F); |
| 317 | H.appendSuffix("html"); |
| 318 | F.renamePathOnDisk(H, NULL); |
| 319 | |
| 320 | os.open(H.toString().c_str()); |
| 321 | |
| 322 | if (!os) { |
| 323 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 324 | return; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // Emit the HTML to disk. |
| 329 | |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame] | 330 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 331 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 334 | void HTMLDiagnostics::HandlePiece(Rewriter& R, unsigned BugFileID, |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 335 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 336 | unsigned num, unsigned max) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 337 | |
| 338 | // For now, just draw a box above the line in question, and emit the |
| 339 | // warning. |
| 340 | |
| 341 | FullSourceLoc Pos = P.getLocation(); |
| 342 | |
| 343 | if (!Pos.isValid()) |
| 344 | return; |
| 345 | |
| 346 | SourceManager& SM = R.getSourceMgr(); |
| 347 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 348 | unsigned FileID = SM.getCanonicalFileID(LPos.getLocation()); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 350 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 351 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 352 | if (LPos.getCanonicalFileID() != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 353 | return; |
| 354 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 355 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 356 | const char* FileStart = Buf->getBufferStart(); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 358 | // Compute the column number. Rewind from the current position to the start |
| 359 | // of the line. |
| 360 | |
| 361 | unsigned ColNo = LPos.getColumnNumber(); |
| 362 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 363 | const char *LineStart = TokLogicalPtr-ColNo; |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 364 | |
| 365 | // Only compute LineEnd if we display below a line. |
| 366 | const char *LineEnd = TokLogicalPtr; |
| 367 | |
| 368 | if (P.getDisplayHint() == PathDiagnosticPiece::Below) { |
| 369 | const char* FileEnd = Buf->getBufferEnd(); |
| 370 | |
| 371 | while (*LineEnd != '\n' && LineEnd != FileEnd) |
| 372 | ++LineEnd; |
| 373 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 375 | // Compute the margin offset by counting tabs and non-tabs. |
| 376 | |
| 377 | unsigned PosNo = 0; |
| 378 | |
| 379 | for (const char* c = LineStart; c != TokLogicalPtr; ++c) |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 380 | PosNo += *c == '\t' ? 8 : 1; |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 381 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 382 | // Create the html for the message. |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 383 | { |
| 384 | // Get the string and determining its maximum substring. |
| 385 | const std::string& Msg = P.getString(); |
| 386 | unsigned max_token = 0; |
| 387 | unsigned cnt = 0; |
| 388 | unsigned len = Msg.size(); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 390 | for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I) |
| 391 | switch (*I) { |
| 392 | default: |
| 393 | ++cnt; |
| 394 | continue; |
| 395 | case ' ': |
| 396 | case '\t': |
| 397 | case '\n': |
| 398 | if (cnt > max_token) max_token = cnt; |
| 399 | cnt = 0; |
| 400 | } |
| 401 | |
| 402 | if (cnt > max_token) max_token = cnt; |
| 403 | |
| 404 | // Next, determine the approximate size of the message bubble in em. |
| 405 | unsigned em; |
Ted Kremenek | f91ce77 | 2008-10-24 21:17:16 +0000 | [diff] [blame^] | 406 | const unsigned max_line = 120; |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 407 | |
| 408 | if (max_token >= max_line) |
| 409 | em = max_token / 2; |
| 410 | else { |
| 411 | unsigned characters = max_line; |
| 412 | unsigned lines = len / max_line; |
| 413 | |
| 414 | if (lines > 0) { |
| 415 | for (; characters > max_token; --characters) |
| 416 | if (len / characters > lines) { |
| 417 | ++characters; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | em = characters / 2; |
| 423 | } |
| 424 | |
| 425 | // Now generate the message bubble. |
| 426 | std::string s; |
| 427 | llvm::raw_string_ostream os(s); |
| 428 | |
| 429 | os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; |
| 430 | |
| 431 | if (num == max) |
| 432 | os << "EndPath"; |
| 433 | else |
| 434 | os << "Path" << num; |
| 435 | |
| 436 | os << "\" class=\"msg\" style=\"margin-left:" << PosNo << "ex"; |
| 437 | if (em < max_line/2) os << "; max-width:" << em << "em"; |
| 438 | os << "\">"; |
| 439 | |
| 440 | if (max > 1) |
| 441 | os << "<span class=\"PathIndex\">[" << num << "]</span> "; |
| 442 | |
| 443 | os << html::EscapeText(Msg) << "</div></td></tr>"; |
| 444 | |
| 445 | // Insert the new html. |
| 446 | unsigned DisplayPos = 0; |
| 447 | |
| 448 | switch (P.getDisplayHint()) { |
| 449 | case PathDiagnosticPiece::Above: |
| 450 | DisplayPos = LineStart - FileStart; |
| 451 | break; |
| 452 | case PathDiagnosticPiece::Below: |
| 453 | DisplayPos = LineEnd - FileStart; |
| 454 | break; |
| 455 | default: |
| 456 | assert (false && "Unhandled hint."); |
| 457 | } |
| 458 | |
| 459 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str()); |
| 460 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 461 | |
| 462 | // Now highlight the ranges. |
| 463 | |
| 464 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 465 | I != E; ++I) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 466 | HighlightRange(R, FileID, *I); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 469 | void HTMLDiagnostics::HighlightRange(Rewriter& R, unsigned BugFileID, |
| 470 | SourceRange Range) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 471 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 472 | SourceManager& SM = R.getSourceMgr(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 473 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 474 | SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin()); |
| 475 | unsigned StartLineNo = SM.getLineNumber(LogicalStart); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 476 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 477 | SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd()); |
| 478 | unsigned EndLineNo = SM.getLineNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 479 | |
| 480 | if (EndLineNo < StartLineNo) |
| 481 | return; |
| 482 | |
Ted Kremenek | ae8a255 | 2008-07-29 23:35:38 +0000 | [diff] [blame] | 483 | if (SM.getCanonicalFileID(LogicalStart) != BugFileID || |
| 484 | SM.getCanonicalFileID(LogicalEnd) != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 485 | return; |
| 486 | |
| 487 | // Compute the column number of the end. |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 488 | unsigned EndColNo = SM.getColumnNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 489 | unsigned OldEndColNo = EndColNo; |
| 490 | |
| 491 | if (EndColNo) { |
| 492 | // 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] | 493 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Highlight the range. Make the span tag the outermost tag for the |
| 497 | // selected range. |
Ted Kremenek | dab4ead | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 499 | SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Ted Kremenek | b747814 | 2008-04-17 18:37:23 +0000 | [diff] [blame] | 500 | |
| 501 | html::HighlightRange(R, LogicalStart, E, |
| 502 | "<span class=\"mrange\">", "</span>"); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 503 | } |