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