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