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 | 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 | |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 51 | void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P, |
| 52 | unsigned num, unsigned max); |
| 53 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 54 | void HighlightRange(Rewriter& R, 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 | |
| 126 | // Create a new rewriter to generate HTML. |
| 127 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
| 128 | Rewriter R(SMgr); |
| 129 | |
| 130 | // Process the path. |
| 131 | |
| 132 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 133 | unsigned max = n; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 134 | |
| 135 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 136 | I!=E; ++I, --n) { |
| 137 | |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 138 | HandlePiece(R, *I, n, max); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Add line numbers, header, footer, etc. |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 143 | unsigned FileID = R.getSourceMgr().getMainFileID(); |
| 144 | html::EscapeText(R, FileID); |
| 145 | html::AddLineNumbers(R, FileID); |
| 146 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 147 | // If we have a preprocessor, relex the file and syntax highlight. |
| 148 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 149 | // for example. |
| 150 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 151 | if (PP) html::SyntaxHighlight(R, FileID, *PP); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 152 | |
| 153 | // FIXME: We eventually want to use PPF to create a fresh Preprocessor, |
| 154 | // once we have worked out the bugs. |
| 155 | // |
| 156 | // if (PPF) html::HighlightMacros(R, FileID, *PPF); |
| 157 | // |
| 158 | if (PP) html::HighlightMacros(R, FileID, *PP); |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 160 | // Get the full directory name of the analyzed file. |
| 161 | |
| 162 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 164 | // This is a cludge; basically we want to append either the full |
| 165 | // working directory if we have no directory information. This is |
| 166 | // a work in progress. |
| 167 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 168 | std::string DirName = ""; |
| 169 | |
| 170 | if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { |
| 171 | llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); |
| 172 | DirName = P.toString() + "/"; |
| 173 | } |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 175 | // Add the name of the file as an <h1> tag. |
| 176 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 177 | { |
| 178 | std::ostringstream os; |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 180 | os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
| 181 | "<tr><td class=\"rowname\">File:</td><td>" |
| 182 | << html::EscapeText(DirName) |
| 183 | << html::EscapeText(Entry->getName()) |
| 184 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
| 185 | "<a href=\"#EndPath\">line " |
| 186 | << (*D.rbegin()).getLocation().getLogicalLineNumber() |
| 187 | << ", column " |
| 188 | << (*D.rbegin()).getLocation().getLogicalColumnNumber() |
| 189 | << "</a></td></tr>\n" |
| 190 | "<tr><td class=\"rowname\">Description:</td><td>" |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 191 | << D.getDescription() << "</td></tr>\n"; |
| 192 | |
| 193 | // Output any other meta data. |
| 194 | |
| 195 | for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); |
| 196 | I!=E; ++I) { |
| 197 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
| 198 | } |
| 199 | |
| 200 | os << "</table>\n<h3>Annotated Source Code</h3>\n"; |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 202 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 205 | // Embed meta-data tags. |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 206 | |
| 207 | const std::string& BugDesc = D.getDescription(); |
| 208 | |
| 209 | if (!BugDesc.empty()) { |
| 210 | std::ostringstream os; |
Ted Kremenek | 86b4381 | 2008-04-02 18:03:20 +0000 | [diff] [blame] | 211 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 212 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | { |
| 216 | std::ostringstream os; |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 217 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 218 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 219 | } |
| 220 | |
| 221 | { |
| 222 | std::ostringstream os; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 223 | os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber() |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 224 | << " -->\n"; |
| 225 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 226 | } |
| 227 | |
| 228 | { |
| 229 | std::ostringstream os; |
| 230 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
| 231 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 232 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 233 | |
| 234 | // Add CSS, header, and footer. |
| 235 | |
| 236 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID); |
| 237 | |
| 238 | // Get the rewrite buffer. |
| 239 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 240 | |
| 241 | if (!Buf) { |
| 242 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // Create the stream to write out the HTML. |
| 247 | std::ofstream os; |
| 248 | |
| 249 | { |
| 250 | // Create a path for the target HTML file. |
| 251 | llvm::sys::Path F(FilePrefix); |
| 252 | F.makeUnique(false, NULL); |
| 253 | |
| 254 | // Rename the file with an HTML extension. |
| 255 | llvm::sys::Path H(F); |
| 256 | H.appendSuffix("html"); |
| 257 | F.renamePathOnDisk(H, NULL); |
| 258 | |
| 259 | os.open(H.toString().c_str()); |
| 260 | |
| 261 | if (!os) { |
| 262 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Emit the HTML to disk. |
| 268 | |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame] | 269 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 270 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void HTMLDiagnostics::HandlePiece(Rewriter& R, |
| 274 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 275 | unsigned num, unsigned max) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 276 | |
| 277 | // For now, just draw a box above the line in question, and emit the |
| 278 | // warning. |
| 279 | |
| 280 | FullSourceLoc Pos = P.getLocation(); |
| 281 | |
| 282 | if (!Pos.isValid()) |
| 283 | return; |
| 284 | |
| 285 | SourceManager& SM = R.getSourceMgr(); |
| 286 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 287 | unsigned FileID = SM.getCanonicalFileID(LPos.getLocation()); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 289 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 290 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 291 | if (!SM.isFromMainFile(LPos.getLocation())) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 292 | return; |
| 293 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 294 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 295 | const char* FileStart = Buf->getBufferStart(); |
| 296 | |
| 297 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 298 | // Compute the column number. Rewind from the current position to the start |
| 299 | // of the line. |
| 300 | |
| 301 | unsigned ColNo = LPos.getColumnNumber(); |
| 302 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 303 | const char *LineStart = TokLogicalPtr-ColNo; |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 304 | |
| 305 | // Only compute LineEnd if we display below a line. |
| 306 | const char *LineEnd = TokLogicalPtr; |
| 307 | |
| 308 | if (P.getDisplayHint() == PathDiagnosticPiece::Below) { |
| 309 | const char* FileEnd = Buf->getBufferEnd(); |
| 310 | |
| 311 | while (*LineEnd != '\n' && LineEnd != FileEnd) |
| 312 | ++LineEnd; |
| 313 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 315 | // Compute the margin offset by counting tabs and non-tabs. |
| 316 | |
| 317 | unsigned PosNo = 0; |
| 318 | |
| 319 | for (const char* c = LineStart; c != TokLogicalPtr; ++c) |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 320 | PosNo += *c == '\t' ? 8 : 1; |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 322 | // Create the html for the message. |
| 323 | |
| 324 | std::ostringstream os; |
| 325 | |
| 326 | os << "\n<tr><td class=\"num\"></td><td class=\"line\">" |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 327 | << "<div id=\""; |
| 328 | |
| 329 | if (num == max) |
| 330 | os << "EndPath"; |
| 331 | else |
| 332 | os << "Path" << num; |
| 333 | |
| 334 | os << "\" class=\"msg\" style=\"margin-left:" |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 335 | << PosNo << "ex\">"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 718ceb1 | 2008-04-02 21:04:20 +0000 | [diff] [blame] | 337 | if (max > 1) |
| 338 | os << "<span class=\"PathIndex\">[" << num << "]</span> "; |
| 339 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 340 | os << html::EscapeText(P.getString()) << "</div></td></tr>"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 341 | |
| 342 | // Insert the new html. |
| 343 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 344 | unsigned DisplayPos = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 346 | switch (P.getDisplayHint()) { |
| 347 | case PathDiagnosticPiece::Above: |
| 348 | DisplayPos = LineStart - FileStart; |
| 349 | break; |
| 350 | case PathDiagnosticPiece::Below: |
| 351 | DisplayPos = LineEnd - FileStart; |
| 352 | break; |
| 353 | default: |
| 354 | assert (false && "Unhandled hint."); |
| 355 | } |
| 356 | |
| 357 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str()); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 358 | |
| 359 | // Now highlight the ranges. |
| 360 | |
| 361 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 362 | I != E; ++I) |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 363 | HighlightRange(R, *I); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 366 | void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 368 | SourceManager& SM = R.getSourceMgr(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 370 | SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin()); |
| 371 | unsigned StartLineNo = SM.getLineNumber(LogicalStart); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 373 | SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd()); |
| 374 | unsigned EndLineNo = SM.getLineNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 375 | |
| 376 | if (EndLineNo < StartLineNo) |
| 377 | return; |
| 378 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 379 | if (!SM.isFromMainFile(LogicalStart) || |
| 380 | !SM.isFromMainFile(LogicalEnd)) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 381 | return; |
| 382 | |
| 383 | // Compute the column number of the end. |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 384 | unsigned EndColNo = SM.getColumnNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 385 | unsigned OldEndColNo = EndColNo; |
| 386 | |
| 387 | if (EndColNo) { |
| 388 | // 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] | 389 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Highlight the range. Make the span tag the outermost tag for the |
| 393 | // selected range. |
Ted Kremenek | dab4ead | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 395 | SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Ted Kremenek | b747814 | 2008-04-17 18:37:23 +0000 | [diff] [blame] | 396 | |
| 397 | html::HighlightRange(R, LogicalStart, E, |
| 398 | "<span class=\"mrange\">", "</span>"); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 399 | } |