HTMLDiagnostics.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Lex/Lexer.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/Rewrite/Core/HTMLRewrite.h"
  20. #include "clang/Rewrite/Core/Rewriter.h"
  21. #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
  22. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  23. #include "clang/StaticAnalyzer/Core/IssueHash.h"
  24. #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
  25. #include "llvm/Support/Errc.h"
  26. #include "llvm/Support/FileSystem.h"
  27. #include "llvm/Support/MemoryBuffer.h"
  28. #include "llvm/Support/Path.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <sstream>
  31. using namespace clang;
  32. using namespace ento;
  33. //===----------------------------------------------------------------------===//
  34. // Boilerplate.
  35. //===----------------------------------------------------------------------===//
  36. namespace {
  37. class HTMLDiagnostics : public PathDiagnosticConsumer {
  38. std::string Directory;
  39. bool createdDir, noDir;
  40. const Preprocessor &PP;
  41. AnalyzerOptions &AnalyzerOpts;
  42. public:
  43. HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string& prefix, const Preprocessor &pp);
  44. ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); }
  45. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  46. FilesMade *filesMade) override;
  47. StringRef getName() const override {
  48. return "HTMLDiagnostics";
  49. }
  50. unsigned ProcessMacroPiece(raw_ostream &os,
  51. const PathDiagnosticMacroPiece& P,
  52. unsigned num);
  53. void HandlePiece(Rewriter& R, FileID BugFileID,
  54. const PathDiagnosticPiece& P, unsigned num, unsigned max);
  55. void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
  56. const char *HighlightStart = "<span class=\"mrange\">",
  57. const char *HighlightEnd = "</span>");
  58. void ReportDiag(const PathDiagnostic& D,
  59. FilesMade *filesMade);
  60. };
  61. } // end anonymous namespace
  62. HTMLDiagnostics::HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts,
  63. const std::string& prefix,
  64. const Preprocessor &pp)
  65. : Directory(prefix), createdDir(false), noDir(false), PP(pp), AnalyzerOpts(AnalyzerOpts) {
  66. }
  67. void ento::createHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
  68. PathDiagnosticConsumers &C,
  69. const std::string& prefix,
  70. const Preprocessor &PP) {
  71. C.push_back(new HTMLDiagnostics(AnalyzerOpts, prefix, PP));
  72. }
  73. //===----------------------------------------------------------------------===//
  74. // Report processing.
  75. //===----------------------------------------------------------------------===//
  76. void HTMLDiagnostics::FlushDiagnosticsImpl(
  77. std::vector<const PathDiagnostic *> &Diags,
  78. FilesMade *filesMade) {
  79. for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
  80. et = Diags.end(); it != et; ++it) {
  81. ReportDiag(**it, filesMade);
  82. }
  83. }
  84. void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
  85. FilesMade *filesMade) {
  86. // Create the HTML directory if it is missing.
  87. if (!createdDir) {
  88. createdDir = true;
  89. if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) {
  90. llvm::errs() << "warning: could not create directory '"
  91. << Directory << "': " << ec.message() << '\n';
  92. noDir = true;
  93. return;
  94. }
  95. }
  96. if (noDir)
  97. return;
  98. // First flatten out the entire path to make it easier to use.
  99. PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
  100. // The path as already been prechecked that all parts of the path are
  101. // from the same file and that it is non-empty.
  102. const SourceManager &SMgr = path.front()->getLocation().getManager();
  103. assert(!path.empty());
  104. FileID FID =
  105. path.front()->getLocation().asLocation().getExpansionLoc().getFileID();
  106. assert(FID.isValid());
  107. // Create a new rewriter to generate HTML.
  108. Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
  109. // Get the function/method name
  110. SmallString<128> declName("unknown");
  111. int offsetDecl = 0;
  112. if (const Decl *DeclWithIssue = D.getDeclWithIssue()) {
  113. if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
  114. declName = ND->getDeclName().getAsString();
  115. }
  116. if (const Stmt *Body = DeclWithIssue->getBody()) {
  117. // Retrieve the relative position of the declaration which will be used
  118. // for the file name
  119. FullSourceLoc L(
  120. SMgr.getExpansionLoc(path.back()->getLocation().asLocation()),
  121. SMgr);
  122. FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getLocStart()), SMgr);
  123. offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber();
  124. }
  125. }
  126. // Process the path.
  127. // Maintain the counts of extra note pieces separately.
  128. unsigned TotalPieces = path.size();
  129. unsigned TotalNotePieces =
  130. std::count_if(path.begin(), path.end(),
  131. [](const IntrusiveRefCntPtr<PathDiagnosticPiece> &p) {
  132. return isa<PathDiagnosticNotePiece>(p.get());
  133. });
  134. unsigned TotalRegularPieces = TotalPieces - TotalNotePieces;
  135. unsigned NumRegularPieces = TotalRegularPieces;
  136. unsigned NumNotePieces = TotalNotePieces;
  137. for (auto I = path.rbegin(), E = path.rend(); I != E; ++I) {
  138. if (isa<PathDiagnosticNotePiece>(I->get())) {
  139. // This adds diagnostic bubbles, but not navigation.
  140. // Navigation through note pieces would be added later,
  141. // as a separate pass through the piece list.
  142. HandlePiece(R, FID, **I, NumNotePieces, TotalNotePieces);
  143. --NumNotePieces;
  144. } else {
  145. HandlePiece(R, FID, **I, NumRegularPieces, TotalRegularPieces);
  146. --NumRegularPieces;
  147. }
  148. }
  149. // Add line numbers, header, footer, etc.
  150. // unsigned FID = R.getSourceMgr().getMainFileID();
  151. html::EscapeText(R, FID);
  152. html::AddLineNumbers(R, FID);
  153. // If we have a preprocessor, relex the file and syntax highlight.
  154. // We might not have a preprocessor if we come from a deserialized AST file,
  155. // for example.
  156. html::SyntaxHighlight(R, FID, PP);
  157. html::HighlightMacros(R, FID, PP);
  158. // Get the full directory name of the analyzed file.
  159. const FileEntry* Entry = SMgr.getFileEntryForID(FID);
  160. // This is a cludge; basically we want to append either the full
  161. // working directory if we have no directory information. This is
  162. // a work in progress.
  163. llvm::SmallString<0> DirName;
  164. if (llvm::sys::path::is_relative(Entry->getName())) {
  165. llvm::sys::fs::current_path(DirName);
  166. DirName += '/';
  167. }
  168. int LineNumber = path.back()->getLocation().asLocation().getExpansionLineNumber();
  169. int ColumnNumber = path.back()->getLocation().asLocation().getExpansionColumnNumber();
  170. // Add the name of the file as an <h1> tag.
  171. {
  172. std::string s;
  173. llvm::raw_string_ostream os(s);
  174. os << "<!-- REPORTHEADER -->\n"
  175. << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
  176. "<tr><td class=\"rowname\">File:</td><td>"
  177. << html::EscapeText(DirName)
  178. << html::EscapeText(Entry->getName())
  179. << "</td></tr>\n<tr><td class=\"rowname\">Warning:</td><td>"
  180. "<a href=\"#EndPath\">line "
  181. << LineNumber
  182. << ", column "
  183. << ColumnNumber
  184. << "</a><br />"
  185. << D.getVerboseDescription() << "</td></tr>\n";
  186. // The navigation across the extra notes pieces.
  187. unsigned NumExtraPieces = 0;
  188. for (const auto &Piece : path) {
  189. if (const auto *P = dyn_cast<PathDiagnosticNotePiece>(Piece.get())) {
  190. int LineNumber =
  191. P->getLocation().asLocation().getExpansionLineNumber();
  192. int ColumnNumber =
  193. P->getLocation().asLocation().getExpansionColumnNumber();
  194. os << "<tr><td class=\"rowname\">Note:</td><td>"
  195. << "<a href=\"#Note" << NumExtraPieces << "\">line "
  196. << LineNumber << ", column " << ColumnNumber << "</a><br />"
  197. << P->getString() << "</td></tr>";
  198. ++NumExtraPieces;
  199. }
  200. }
  201. // Output any other meta data.
  202. for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
  203. I!=E; ++I) {
  204. os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
  205. }
  206. os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
  207. "<h3>Annotated Source Code</h3>\n";
  208. R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
  209. }
  210. // Embed meta-data tags.
  211. {
  212. std::string s;
  213. llvm::raw_string_ostream os(s);
  214. StringRef BugDesc = D.getVerboseDescription();
  215. if (!BugDesc.empty())
  216. os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
  217. StringRef BugType = D.getBugType();
  218. if (!BugType.empty())
  219. os << "\n<!-- BUGTYPE " << BugType << " -->\n";
  220. PathDiagnosticLocation UPDLoc = D.getUniqueingLoc();
  221. FullSourceLoc L(SMgr.getExpansionLoc(UPDLoc.isValid()
  222. ? UPDLoc.asLocation()
  223. : D.getLocation().asLocation()),
  224. SMgr);
  225. const Decl *DeclWithIssue = D.getDeclWithIssue();
  226. StringRef BugCategory = D.getCategory();
  227. if (!BugCategory.empty())
  228. os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
  229. os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
  230. os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n";
  231. os << "\n<!-- FUNCTIONNAME " << declName << " -->\n";
  232. os << "\n<!-- ISSUEHASHCONTENTOFLINEINCONTEXT "
  233. << GetIssueHash(SMgr, L, D.getCheckName(), D.getBugType(), DeclWithIssue,
  234. PP.getLangOpts()) << " -->\n";
  235. os << "\n<!-- BUGLINE "
  236. << LineNumber
  237. << " -->\n";
  238. os << "\n<!-- BUGCOLUMN "
  239. << ColumnNumber
  240. << " -->\n";
  241. os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
  242. // Mark the end of the tags.
  243. os << "\n<!-- BUGMETAEND -->\n";
  244. // Insert the text.
  245. R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
  246. }
  247. // Add CSS, header, and footer.
  248. html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
  249. // Get the rewrite buffer.
  250. const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
  251. if (!Buf) {
  252. llvm::errs() << "warning: no diagnostics generated for main file.\n";
  253. return;
  254. }
  255. // Create a path for the target HTML file.
  256. int FD;
  257. SmallString<128> Model, ResultPath;
  258. if (!AnalyzerOpts.shouldWriteStableReportFilename()) {
  259. llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
  260. if (std::error_code EC =
  261. llvm::sys::fs::make_absolute(Model)) {
  262. llvm::errs() << "warning: could not make '" << Model
  263. << "' absolute: " << EC.message() << '\n';
  264. return;
  265. }
  266. if (std::error_code EC =
  267. llvm::sys::fs::createUniqueFile(Model, FD, ResultPath)) {
  268. llvm::errs() << "warning: could not create file in '" << Directory
  269. << "': " << EC.message() << '\n';
  270. return;
  271. }
  272. } else {
  273. int i = 1;
  274. std::error_code EC;
  275. do {
  276. // Find a filename which is not already used
  277. std::stringstream filename;
  278. Model = "";
  279. filename << "report-"
  280. << llvm::sys::path::filename(Entry->getName()).str()
  281. << "-" << declName.c_str()
  282. << "-" << offsetDecl
  283. << "-" << i << ".html";
  284. llvm::sys::path::append(Model, Directory,
  285. filename.str());
  286. EC = llvm::sys::fs::openFileForWrite(Model,
  287. FD,
  288. llvm::sys::fs::F_RW |
  289. llvm::sys::fs::F_Excl);
  290. if (EC && EC != llvm::errc::file_exists) {
  291. llvm::errs() << "warning: could not create file '" << Model
  292. << "': " << EC.message() << '\n';
  293. return;
  294. }
  295. i++;
  296. } while (EC);
  297. }
  298. llvm::raw_fd_ostream os(FD, true);
  299. if (filesMade)
  300. filesMade->addDiagnostic(D, getName(),
  301. llvm::sys::path::filename(ResultPath));
  302. // Emit the HTML to disk.
  303. for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
  304. os << *I;
  305. }
  306. void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
  307. const PathDiagnosticPiece& P,
  308. unsigned num, unsigned max) {
  309. // For now, just draw a box above the line in question, and emit the
  310. // warning.
  311. FullSourceLoc Pos = P.getLocation().asLocation();
  312. if (!Pos.isValid())
  313. return;
  314. SourceManager &SM = R.getSourceMgr();
  315. assert(&Pos.getManager() == &SM && "SourceManagers are different!");
  316. std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
  317. if (LPosInfo.first != BugFileID)
  318. return;
  319. const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
  320. const char* FileStart = Buf->getBufferStart();
  321. // Compute the column number. Rewind from the current position to the start
  322. // of the line.
  323. unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
  324. const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
  325. const char *LineStart = TokInstantiationPtr-ColNo;
  326. // Compute LineEnd.
  327. const char *LineEnd = TokInstantiationPtr;
  328. const char* FileEnd = Buf->getBufferEnd();
  329. while (*LineEnd != '\n' && LineEnd != FileEnd)
  330. ++LineEnd;
  331. // Compute the margin offset by counting tabs and non-tabs.
  332. unsigned PosNo = 0;
  333. for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
  334. PosNo += *c == '\t' ? 8 : 1;
  335. // Create the html for the message.
  336. const char *Kind = nullptr;
  337. bool IsNote = false;
  338. bool SuppressIndex = (max == 1);
  339. switch (P.getKind()) {
  340. case PathDiagnosticPiece::Call:
  341. llvm_unreachable("Calls and extra notes should already be handled");
  342. case PathDiagnosticPiece::Event: Kind = "Event"; break;
  343. case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
  344. // Setting Kind to "Control" is intentional.
  345. case PathDiagnosticPiece::Macro: Kind = "Control"; break;
  346. case PathDiagnosticPiece::Note:
  347. Kind = "Note";
  348. IsNote = true;
  349. SuppressIndex = true;
  350. break;
  351. }
  352. std::string sbuf;
  353. llvm::raw_string_ostream os(sbuf);
  354. os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
  355. if (IsNote)
  356. os << "Note" << num;
  357. else if (num == max)
  358. os << "EndPath";
  359. else
  360. os << "Path" << num;
  361. os << "\" class=\"msg";
  362. if (Kind)
  363. os << " msg" << Kind;
  364. os << "\" style=\"margin-left:" << PosNo << "ex";
  365. // Output a maximum size.
  366. if (!isa<PathDiagnosticMacroPiece>(P)) {
  367. // Get the string and determining its maximum substring.
  368. const auto &Msg = P.getString();
  369. unsigned max_token = 0;
  370. unsigned cnt = 0;
  371. unsigned len = Msg.size();
  372. for (char C : Msg)
  373. switch (C) {
  374. default:
  375. ++cnt;
  376. continue;
  377. case ' ':
  378. case '\t':
  379. case '\n':
  380. if (cnt > max_token) max_token = cnt;
  381. cnt = 0;
  382. }
  383. if (cnt > max_token)
  384. max_token = cnt;
  385. // Determine the approximate size of the message bubble in em.
  386. unsigned em;
  387. const unsigned max_line = 120;
  388. if (max_token >= max_line)
  389. em = max_token / 2;
  390. else {
  391. unsigned characters = max_line;
  392. unsigned lines = len / max_line;
  393. if (lines > 0) {
  394. for (; characters > max_token; --characters)
  395. if (len / characters > lines) {
  396. ++characters;
  397. break;
  398. }
  399. }
  400. em = characters / 2;
  401. }
  402. if (em < max_line/2)
  403. os << "; max-width:" << em << "em";
  404. }
  405. else
  406. os << "; max-width:100em";
  407. os << "\">";
  408. if (!SuppressIndex) {
  409. os << "<table class=\"msgT\"><tr><td valign=\"top\">";
  410. os << "<div class=\"PathIndex";
  411. if (Kind) os << " PathIndex" << Kind;
  412. os << "\">" << num << "</div>";
  413. if (num > 1) {
  414. os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
  415. << (num - 1)
  416. << "\" title=\"Previous event ("
  417. << (num - 1)
  418. << ")\">&#x2190;</a></div></td>";
  419. }
  420. os << "</td><td>";
  421. }
  422. if (const PathDiagnosticMacroPiece *MP =
  423. dyn_cast<PathDiagnosticMacroPiece>(&P)) {
  424. os << "Within the expansion of the macro '";
  425. // Get the name of the macro by relexing it.
  426. {
  427. FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
  428. assert(L.isFileID());
  429. StringRef BufferInfo = L.getBufferData();
  430. std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
  431. const char* MacroName = LocInfo.second + BufferInfo.data();
  432. Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
  433. BufferInfo.begin(), MacroName, BufferInfo.end());
  434. Token TheTok;
  435. rawLexer.LexFromRawLexer(TheTok);
  436. for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
  437. os << MacroName[i];
  438. }
  439. os << "':\n";
  440. if (!SuppressIndex) {
  441. os << "</td>";
  442. if (num < max) {
  443. os << "<td><div class=\"PathNav\"><a href=\"#";
  444. if (num == max - 1)
  445. os << "EndPath";
  446. else
  447. os << "Path" << (num + 1);
  448. os << "\" title=\"Next event ("
  449. << (num + 1)
  450. << ")\">&#x2192;</a></div></td>";
  451. }
  452. os << "</tr></table>";
  453. }
  454. // Within a macro piece. Write out each event.
  455. ProcessMacroPiece(os, *MP, 0);
  456. }
  457. else {
  458. os << html::EscapeText(P.getString());
  459. if (!SuppressIndex) {
  460. os << "</td>";
  461. if (num < max) {
  462. os << "<td><div class=\"PathNav\"><a href=\"#";
  463. if (num == max - 1)
  464. os << "EndPath";
  465. else
  466. os << "Path" << (num + 1);
  467. os << "\" title=\"Next event ("
  468. << (num + 1)
  469. << ")\">&#x2192;</a></div></td>";
  470. }
  471. os << "</tr></table>";
  472. }
  473. }
  474. os << "</div></td></tr>";
  475. // Insert the new html.
  476. unsigned DisplayPos = LineEnd - FileStart;
  477. SourceLocation Loc =
  478. SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
  479. R.InsertTextBefore(Loc, os.str());
  480. // Now highlight the ranges.
  481. ArrayRef<SourceRange> Ranges = P.getRanges();
  482. for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
  483. E = Ranges.end(); I != E; ++I) {
  484. HighlightRange(R, LPosInfo.first, *I);
  485. }
  486. }
  487. static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
  488. unsigned x = n % ('z' - 'a');
  489. n /= 'z' - 'a';
  490. if (n > 0)
  491. EmitAlphaCounter(os, n);
  492. os << char('a' + x);
  493. }
  494. unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
  495. const PathDiagnosticMacroPiece& P,
  496. unsigned num) {
  497. for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
  498. I!=E; ++I) {
  499. if (const PathDiagnosticMacroPiece *MP =
  500. dyn_cast<PathDiagnosticMacroPiece>(*I)) {
  501. num = ProcessMacroPiece(os, *MP, num);
  502. continue;
  503. }
  504. if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
  505. os << "<div class=\"msg msgEvent\" style=\"width:94%; "
  506. "margin-left:5px\">"
  507. "<table class=\"msgT\"><tr>"
  508. "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
  509. EmitAlphaCounter(os, num++);
  510. os << "</div></td><td valign=\"top\">"
  511. << html::EscapeText(EP->getString())
  512. << "</td></tr></table></div>\n";
  513. }
  514. }
  515. return num;
  516. }
  517. void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
  518. SourceRange Range,
  519. const char *HighlightStart,
  520. const char *HighlightEnd) {
  521. SourceManager &SM = R.getSourceMgr();
  522. const LangOptions &LangOpts = R.getLangOpts();
  523. SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
  524. unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
  525. SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
  526. unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
  527. if (EndLineNo < StartLineNo)
  528. return;
  529. if (SM.getFileID(InstantiationStart) != BugFileID ||
  530. SM.getFileID(InstantiationEnd) != BugFileID)
  531. return;
  532. // Compute the column number of the end.
  533. unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
  534. unsigned OldEndColNo = EndColNo;
  535. if (EndColNo) {
  536. // Add in the length of the token, so that we cover multi-char tokens.
  537. EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
  538. }
  539. // Highlight the range. Make the span tag the outermost tag for the
  540. // selected range.
  541. SourceLocation E =
  542. InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
  543. html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
  544. }