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