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 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/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" |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/MemoryBuffer.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" |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 28 | |
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; |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 40 | const Preprocessor &PP; |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 41 | std::vector<const PathDiagnostic*> BatchedDiags; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 42 | public: |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 43 | HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 44 | |
| 45 | virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); } |
| 46 | |
| 47 | virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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 | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 50 | |
| 51 | virtual llvm::StringRef getName() const { |
| 52 | return "HTMLDiagnostics"; |
| 53 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 55 | unsigned ProcessMacroPiece(llvm::raw_ostream& os, |
| 56 | const PathDiagnosticMacroPiece& P, |
| 57 | unsigned num); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 59 | void HandlePiece(Rewriter& R, FileID BugFileID, |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 60 | const PathDiagnosticPiece& P, unsigned num, unsigned max); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 62 | void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, |
| 63 | const char *HighlightStart = "<span class=\"mrange\">", |
| 64 | const char *HighlightEnd = "</span>"); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 66 | void ReportDiag(const PathDiagnostic& D, |
| 67 | llvm::SmallVectorImpl<std::string> *FilesMade); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 68 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 70 | } // end anonymous namespace |
| 71 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 72 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, |
| 73 | const Preprocessor &pp) |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 74 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 75 | PP(pp) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | // All html files begin with "report" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 77 | FilePrefix.appendComponent("report"); |
| 78 | } |
| 79 | |
| 80 | PathDiagnosticClient* |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 81 | clang::CreateHTMLDiagnosticClient(const std::string& prefix, |
| 82 | const Preprocessor &PP) { |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 83 | return new HTMLDiagnostics(prefix, PP); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | // Report processing. |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 90 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
| 91 | if (!D) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 92 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 94 | if (D->empty()) { |
| 95 | delete D; |
| 96 | return; |
| 97 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 7db0a94 | 2009-04-02 05:17:38 +0000 | [diff] [blame] | 99 | const_cast<PathDiagnostic*>(D)->flattenLocations(); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 100 | BatchedDiags.push_back(D); |
| 101 | } |
| 102 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 103 | void |
| 104 | HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade) |
| 105 | { |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 106 | while (!BatchedDiags.empty()) { |
| 107 | const PathDiagnostic* D = BatchedDiags.back(); |
| 108 | BatchedDiags.pop_back(); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 109 | ReportDiag(*D, FilesMade); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 110 | delete D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | } |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 114 | void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D, |
| 115 | llvm::SmallVectorImpl<std::string> *FilesMade){ |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 116 | // Create the HTML directory if it is missing. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 117 | if (!createdDir) { |
| 118 | createdDir = true; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 119 | std::string ErrorMsg; |
| 120 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 122 | if (!Directory.isDirectory()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 123 | llvm::errs() << "warning: could not create directory '" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 124 | << Directory.str() << "'\n" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | << "reason: " << ErrorMsg << '\n'; |
| 126 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 127 | noDir = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 133 | if (noDir) |
| 134 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 136 | const SourceManager &SMgr = D.begin()->getLocation().getManager(); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 137 | FileID FID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 139 | // Verify that the entire path is from the same FileID. |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 140 | for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 141 | FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 143 | if (FID.isInvalid()) { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 144 | FID = SMgr.getFileID(L); |
| 145 | } else if (SMgr.getFileID(L) != FID) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 146 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 148 | // Check the source ranges. |
| 149 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 150 | RE=I->ranges_end(); RI!=RE; ++RI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 152 | SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin()); |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 153 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 154 | if (!L.isFileID() || SMgr.getFileID(L) != FID) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 155 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 157 | L = SMgr.getInstantiationLoc(RI->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 159 | if (!L.isFileID() || SMgr.getFileID(L) != FID) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | return; // FIXME: Emit a warning? |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 164 | if (FID.isInvalid()) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 165 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 167 | // Create a new rewriter to generate HTML. |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 168 | Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
| 170 | // Process the path. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 171 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 172 | unsigned max = n; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 174 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 175 | I!=E; ++I, --n) |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 176 | HandlePiece(R, FID, *I, n, max); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 178 | // Add line numbers, header, footer, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 180 | // unsigned FID = R.getSourceMgr().getMainFileID(); |
| 181 | html::EscapeText(R, FID); |
| 182 | html::AddLineNumbers(R, FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 184 | // If we have a preprocessor, relex the file and syntax highlight. |
| 185 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 186 | // for example. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 188 | html::SyntaxHighlight(R, FID, PP); |
| 189 | html::HighlightMacros(R, FID, PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 191 | // Get the full directory name of the analyzed file. |
| 192 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 193 | const FileEntry* Entry = SMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 195 | // This is a cludge; basically we want to append either the full |
| 196 | // working directory if we have no directory information. This is |
| 197 | // a work in progress. |
| 198 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 199 | std::string DirName = ""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 201 | if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { |
| 202 | llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 203 | DirName = P.str() + "/"; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
| 206 | // Add the name of the file as an <h1> tag. |
| 207 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 208 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 209 | std::string s; |
| 210 | llvm::raw_string_ostream os(s); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 212 | os << "<!-- REPORTHEADER -->\n" |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 213 | << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 214 | "<tr><td class=\"rowname\">File:</td><td>" |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 215 | << html::EscapeText(DirName) |
| 216 | << html::EscapeText(Entry->getName()) |
| 217 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | "<a href=\"#EndPath\">line " |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 219 | << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber() |
| 220 | << ", column " |
| 221 | << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber() |
| 222 | << "</a></td></tr>\n" |
| 223 | "<tr><td class=\"rowname\">Description:</td><td>" |
| 224 | << D.getDescription() << "</td></tr>\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 226 | // Output any other meta data. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 228 | for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); |
| 229 | I!=E; ++I) { |
| 230 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
| 231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 233 | os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | "<h3>Annotated Source Code</h3>\n"; |
| 235 | |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 236 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 239 | // Embed meta-data tags. |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 240 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 241 | std::string s; |
| 242 | llvm::raw_string_ostream os(s); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
| 244 | const std::string& BugDesc = D.getDescription(); |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 245 | if (!BugDesc.empty()) |
| 246 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 248 | const std::string& BugType = D.getBugType(); |
| 249 | if (!BugType.empty()) |
| 250 | os << "\n<!-- BUGTYPE " << BugType << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
| 252 | const std::string& BugCategory = D.getCategory(); |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 253 | if (!BugCategory.empty()) |
| 254 | os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; |
| 255 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 256 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 257 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 258 | os << "\n<!-- BUGLINE " |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 259 | << D.back()->getLocation().asLocation().getInstantiationLineNumber() |
| 260 | << " -->\n"; |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 261 | |
| 262 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 264 | // Mark the end of the tags. |
| 265 | os << "\n<!-- BUGMETAEND -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 267 | // Insert the text. |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 268 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 271 | // Add CSS, header, and footer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 273 | html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 275 | // Get the rewrite buffer. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 276 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 278 | if (!Buf) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 279 | llvm::errs() << "warning: no diagnostics generated for main file.\n"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 283 | // Create a path for the target HTML file. |
| 284 | llvm::sys::Path F(FilePrefix); |
| 285 | F.makeUnique(false, NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 287 | // Rename the file with an HTML extension. |
| 288 | llvm::sys::Path H(F); |
| 289 | H.appendSuffix("html"); |
| 290 | F.renamePathOnDisk(H, NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 292 | std::string ErrorMsg; |
| 293 | llvm::raw_fd_ostream os(H.c_str(), ErrorMsg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 295 | if (!ErrorMsg.empty()) { |
| 296 | (llvm::errs() << "warning: could not create file '" << F.str() |
| 297 | << "'\n").flush(); |
| 298 | return; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 301 | if (FilesMade) |
| 302 | FilesMade->push_back(H.getLast()); |
| 303 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 304 | // Emit the HTML to disk. |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame] | 305 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 309 | void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID, |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 310 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 311 | unsigned num, unsigned max) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 313 | // For now, just draw a box above the line in question, and emit the |
| 314 | // warning. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 315 | FullSourceLoc Pos = P.getLocation().asLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 317 | if (!Pos.isValid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | return; |
| 319 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 320 | SourceManager &SM = R.getSourceMgr(); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 321 | assert(&Pos.getManager() == &SM && "SourceManagers are different!"); |
| 322 | std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 324 | if (LPosInfo.first != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 325 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 327 | const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | const char* FileStart = Buf->getBufferStart(); |
| 329 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 330 | // Compute the column number. Rewind from the current position to the start |
| 331 | // of the line. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 332 | unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); |
| 333 | const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 334 | const char *LineStart = TokInstantiationPtr-ColNo; |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | 3f0b656 | 2009-02-18 22:10:00 +0000 | [diff] [blame] | 336 | // Compute LineEnd. |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 337 | const char *LineEnd = TokInstantiationPtr; |
Ted Kremenek | 3f0b656 | 2009-02-18 22:10:00 +0000 | [diff] [blame] | 338 | const char* FileEnd = Buf->getBufferEnd(); |
| 339 | while (*LineEnd != '\n' && LineEnd != FileEnd) |
| 340 | ++LineEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 342 | // Compute the margin offset by counting tabs and non-tabs. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | unsigned PosNo = 0; |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 344 | for (const char* c = LineStart; c != TokInstantiationPtr; ++c) |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 345 | PosNo += *c == '\t' ? 8 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 347 | // Create the html for the message. |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 348 | |
| 349 | const char *Kind = 0; |
| 350 | switch (P.getKind()) { |
| 351 | case PathDiagnosticPiece::Event: Kind = "Event"; break; |
| 352 | case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break; |
| 353 | // Setting Kind to "Control" is intentional. |
| 354 | case PathDiagnosticPiece::Macro: Kind = "Control"; break; |
| 355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 357 | std::string sbuf; |
| 358 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 360 | os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 362 | if (num == max) |
| 363 | os << "EndPath"; |
| 364 | else |
| 365 | os << "Path" << num; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 367 | os << "\" class=\"msg"; |
| 368 | if (Kind) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | os << " msg" << Kind; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 370 | os << "\" style=\"margin-left:" << PosNo << "ex"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 372 | // Output a maximum size. |
| 373 | if (!isa<PathDiagnosticMacroPiece>(P)) { |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 374 | // Get the string and determining its maximum substring. |
| 375 | const std::string& Msg = P.getString(); |
| 376 | unsigned max_token = 0; |
| 377 | unsigned cnt = 0; |
| 378 | unsigned len = Msg.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 380 | for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I) |
| 381 | switch (*I) { |
| 382 | default: |
| 383 | ++cnt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | continue; |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 385 | case ' ': |
| 386 | case '\t': |
| 387 | case '\n': |
| 388 | if (cnt > max_token) max_token = cnt; |
| 389 | cnt = 0; |
| 390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 392 | if (cnt > max_token) |
| 393 | max_token = cnt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 395 | // Determine the approximate size of the message bubble in em. |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 396 | unsigned em; |
Ted Kremenek | 4e25ee3 | 2009-03-02 23:06:15 +0000 | [diff] [blame] | 397 | const unsigned max_line = 120; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 399 | if (max_token >= max_line) |
| 400 | em = max_token / 2; |
| 401 | else { |
| 402 | unsigned characters = max_line; |
| 403 | unsigned lines = len / max_line; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 405 | if (lines > 0) { |
| 406 | for (; characters > max_token; --characters) |
| 407 | if (len / characters > lines) { |
| 408 | ++characters; |
| 409 | break; |
| 410 | } |
| 411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 413 | em = characters / 2; |
| 414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 416 | if (em < max_line/2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | os << "; max-width:" << em << "em"; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 418 | } |
| 419 | else |
| 420 | os << "; max-width:100em"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 422 | os << "\">"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 424 | if (max > 1) { |
| 425 | os << "<table class=\"msgT\"><tr><td valign=\"top\">"; |
| 426 | os << "<div class=\"PathIndex"; |
| 427 | if (Kind) os << " PathIndex" << Kind; |
| 428 | os << "\">" << num << "</div>"; |
| 429 | os << "</td><td>"; |
| 430 | } |
| 431 | |
| 432 | if (const PathDiagnosticMacroPiece *MP = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | dyn_cast<PathDiagnosticMacroPiece>(&P)) { |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 434 | |
| 435 | os << "Within the expansion of the macro '"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 437 | // Get the name of the macro by relexing it. |
| 438 | { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 439 | FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 440 | assert(L.isFileID()); |
| 441 | std::pair<const char*, const char*> BufferInfo = L.getBufferData(); |
| 442 | const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first; |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 443 | Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.first, |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 444 | MacroName, BufferInfo.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 446 | Token TheTok; |
| 447 | rawLexer.LexFromRawLexer(TheTok); |
| 448 | for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i) |
| 449 | os << MacroName[i]; |
Ted Kremenek | 80bae76 | 2009-03-02 23:05:40 +0000 | [diff] [blame] | 450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 452 | os << "':\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 454 | if (max > 1) |
Ted Kremenek | 80bae76 | 2009-03-02 23:05:40 +0000 | [diff] [blame] | 455 | os << "</td></tr></table>"; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 456 | |
| 457 | // Within a macro piece. Write out each event. |
| 458 | ProcessMacroPiece(os, *MP, 0); |
| 459 | } |
| 460 | else { |
| 461 | os << html::EscapeText(P.getString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 463 | if (max > 1) |
| 464 | os << "</td></tr></table>"; |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 467 | os << "</div></td></tr>"; |
| 468 | |
| 469 | // Insert the new html. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | unsigned DisplayPos = LineEnd - FileStart; |
| 471 | SourceLocation Loc = |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 472 | SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos); |
| 473 | |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 474 | R.InsertTextBefore(Loc, os.str()); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 475 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | // Now highlight the ranges. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 477 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 478 | I != E; ++I) |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 479 | HighlightRange(R, LPosInfo.first, *I); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 480 | |
| 481 | #if 0 |
| 482 | // If there is a code insertion hint, insert that code. |
| 483 | // FIXME: This code is disabled because it seems to mangle the HTML |
| 484 | // output. I'm leaving it here because it's generally the right idea, |
| 485 | // but needs some help from someone more familiar with the rewriter. |
| 486 | for (const CodeModificationHint *Hint = P.code_modifications_begin(), |
| 487 | *HintEnd = P.code_modifications_end(); |
| 488 | Hint != HintEnd; ++Hint) { |
| 489 | if (Hint->RemoveRange.isValid()) { |
| 490 | HighlightRange(R, LPosInfo.first, Hint->RemoveRange, |
| 491 | "<span class=\"CodeRemovalHint\">", "</span>"); |
| 492 | } |
| 493 | if (Hint->InsertionLoc.isValid()) { |
| 494 | std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true); |
| 495 | EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode |
| 496 | + "</span>"; |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 497 | R.InsertTextBefore(Hint->InsertionLoc, EscapedCode); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | #endif |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 503 | static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) { |
| 504 | llvm::SmallVector<char, 10> buf; |
| 505 | |
| 506 | do { |
| 507 | unsigned x = n % ('z' - 'a'); |
| 508 | buf.push_back('a' + x); |
| 509 | n = n / ('z' - 'a'); |
| 510 | } while (n); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 512 | assert(!buf.empty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 514 | for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(), |
| 515 | E=buf.rend(); I!=E; ++I) |
| 516 | os << *I; |
| 517 | } |
| 518 | |
| 519 | unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os, |
| 520 | const PathDiagnosticMacroPiece& P, |
| 521 | unsigned num) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 523 | for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); |
| 524 | I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 526 | if (const PathDiagnosticMacroPiece *MP = |
| 527 | dyn_cast<PathDiagnosticMacroPiece>(*I)) { |
| 528 | num = ProcessMacroPiece(os, *MP, num); |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) { |
| 533 | os << "<div class=\"msg msgEvent\" style=\"width:94%; " |
| 534 | "margin-left:5px\">" |
| 535 | "<table class=\"msgT\"><tr>" |
| 536 | "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">"; |
| 537 | EmitAlphaCounter(os, num++); |
| 538 | os << "</div></td><td valign=\"top\">" |
| 539 | << html::EscapeText(EP->getString()) |
| 540 | << "</td></tr></table></div>\n"; |
| 541 | } |
| 542 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 544 | return num; |
| 545 | } |
| 546 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 547 | void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 548 | SourceRange Range, |
| 549 | const char *HighlightStart, |
| 550 | const char *HighlightEnd) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 551 | SourceManager &SM = R.getSourceMgr(); |
| 552 | const LangOptions &LangOpts = R.getLangOpts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 554 | SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin()); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 555 | unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 557 | SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd()); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 558 | unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 560 | if (EndLineNo < StartLineNo) |
| 561 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 563 | if (SM.getFileID(InstantiationStart) != BugFileID || |
| 564 | SM.getFileID(InstantiationEnd) != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 565 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 567 | // Compute the column number of the end. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 568 | unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 569 | unsigned OldEndColNo = EndColNo; |
| 570 | |
| 571 | if (EndColNo) { |
| 572 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 573 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 574 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 576 | // Highlight the range. Make the span tag the outermost tag for the |
| 577 | // selected range. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 579 | SourceLocation E = |
| 580 | InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 582 | html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 583 | } |