PlistDiagnostics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //===--- PlistDiagnostics.cpp - Plist 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 PlistDiagnostics object.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/PlistSupport.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Basic/Version.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
  20. #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/Support/Casting.h"
  24. using namespace clang;
  25. using namespace ento;
  26. using namespace markup;
  27. namespace {
  28. class PlistDiagnostics : public PathDiagnosticConsumer {
  29. const std::string OutputFile;
  30. const LangOptions &LangOpts;
  31. const bool SupportsCrossFileDiagnostics;
  32. public:
  33. PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
  34. const std::string& prefix,
  35. const LangOptions &LangOpts,
  36. bool supportsMultipleFiles);
  37. ~PlistDiagnostics() override {}
  38. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  39. FilesMade *filesMade) override;
  40. StringRef getName() const override {
  41. return "PlistDiagnostics";
  42. }
  43. PathGenerationScheme getGenerationScheme() const override {
  44. return Extensive;
  45. }
  46. bool supportsLogicalOpControlFlow() const override { return true; }
  47. bool supportsCrossFileDiagnostics() const override {
  48. return SupportsCrossFileDiagnostics;
  49. }
  50. };
  51. } // end anonymous namespace
  52. PlistDiagnostics::PlistDiagnostics(AnalyzerOptions &AnalyzerOpts,
  53. const std::string& output,
  54. const LangOptions &LO,
  55. bool supportsMultipleFiles)
  56. : OutputFile(output),
  57. LangOpts(LO),
  58. SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
  59. void ento::createPlistDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
  60. PathDiagnosticConsumers &C,
  61. const std::string& s,
  62. const Preprocessor &PP) {
  63. C.push_back(new PlistDiagnostics(AnalyzerOpts, s,
  64. PP.getLangOpts(), false));
  65. }
  66. void ento::createPlistMultiFileDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
  67. PathDiagnosticConsumers &C,
  68. const std::string &s,
  69. const Preprocessor &PP) {
  70. C.push_back(new PlistDiagnostics(AnalyzerOpts, s,
  71. PP.getLangOpts(), true));
  72. }
  73. static void ReportControlFlow(raw_ostream &o,
  74. const PathDiagnosticControlFlowPiece& P,
  75. const FIDMap& FM,
  76. const SourceManager &SM,
  77. const LangOptions &LangOpts,
  78. unsigned indent) {
  79. Indent(o, indent) << "<dict>\n";
  80. ++indent;
  81. Indent(o, indent) << "<key>kind</key><string>control</string>\n";
  82. // Emit edges.
  83. Indent(o, indent) << "<key>edges</key>\n";
  84. ++indent;
  85. Indent(o, indent) << "<array>\n";
  86. ++indent;
  87. for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end();
  88. I!=E; ++I) {
  89. Indent(o, indent) << "<dict>\n";
  90. ++indent;
  91. // Make the ranges of the start and end point self-consistent with adjacent edges
  92. // by forcing to use only the beginning of the range. This simplifies the layout
  93. // logic for clients.
  94. Indent(o, indent) << "<key>start</key>\n";
  95. SourceRange StartEdge(
  96. SM.getExpansionLoc(I->getStart().asRange().getBegin()));
  97. EmitRange(o, SM, Lexer::getAsCharRange(StartEdge, SM, LangOpts), FM,
  98. indent + 1);
  99. Indent(o, indent) << "<key>end</key>\n";
  100. SourceRange EndEdge(SM.getExpansionLoc(I->getEnd().asRange().getBegin()));
  101. EmitRange(o, SM, Lexer::getAsCharRange(EndEdge, SM, LangOpts), FM,
  102. indent + 1);
  103. --indent;
  104. Indent(o, indent) << "</dict>\n";
  105. }
  106. --indent;
  107. Indent(o, indent) << "</array>\n";
  108. --indent;
  109. // Output any helper text.
  110. const std::string& s = P.getString();
  111. if (!s.empty()) {
  112. Indent(o, indent) << "<key>alternate</key>";
  113. EmitString(o, s) << '\n';
  114. }
  115. --indent;
  116. Indent(o, indent) << "</dict>\n";
  117. }
  118. static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P,
  119. const FIDMap& FM,
  120. const SourceManager &SM,
  121. const LangOptions &LangOpts,
  122. unsigned indent,
  123. unsigned depth,
  124. bool isKeyEvent = false) {
  125. Indent(o, indent) << "<dict>\n";
  126. ++indent;
  127. Indent(o, indent) << "<key>kind</key><string>event</string>\n";
  128. if (isKeyEvent) {
  129. Indent(o, indent) << "<key>key_event</key><true/>\n";
  130. }
  131. // Output the location.
  132. FullSourceLoc L = P.getLocation().asLocation();
  133. Indent(o, indent) << "<key>location</key>\n";
  134. EmitLocation(o, SM, L, FM, indent);
  135. // Output the ranges (if any).
  136. ArrayRef<SourceRange> Ranges = P.getRanges();
  137. if (!Ranges.empty()) {
  138. Indent(o, indent) << "<key>ranges</key>\n";
  139. Indent(o, indent) << "<array>\n";
  140. ++indent;
  141. for (auto &R : Ranges)
  142. EmitRange(o, SM,
  143. Lexer::getAsCharRange(SM.getExpansionRange(R), SM, LangOpts),
  144. FM, indent + 1);
  145. --indent;
  146. Indent(o, indent) << "</array>\n";
  147. }
  148. // Output the call depth.
  149. Indent(o, indent) << "<key>depth</key>";
  150. EmitInteger(o, depth) << '\n';
  151. // Output the text.
  152. assert(!P.getString().empty());
  153. Indent(o, indent) << "<key>extended_message</key>\n";
  154. Indent(o, indent);
  155. EmitString(o, P.getString()) << '\n';
  156. // Output the short text.
  157. // FIXME: Really use a short string.
  158. Indent(o, indent) << "<key>message</key>\n";
  159. Indent(o, indent);
  160. EmitString(o, P.getString()) << '\n';
  161. // Finish up.
  162. --indent;
  163. Indent(o, indent); o << "</dict>\n";
  164. }
  165. static void ReportPiece(raw_ostream &o,
  166. const PathDiagnosticPiece &P,
  167. const FIDMap& FM, const SourceManager &SM,
  168. const LangOptions &LangOpts,
  169. unsigned indent,
  170. unsigned depth,
  171. bool includeControlFlow,
  172. bool isKeyEvent = false);
  173. static void ReportCall(raw_ostream &o,
  174. const PathDiagnosticCallPiece &P,
  175. const FIDMap& FM, const SourceManager &SM,
  176. const LangOptions &LangOpts,
  177. unsigned indent,
  178. unsigned depth) {
  179. IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter =
  180. P.getCallEnterEvent();
  181. if (callEnter)
  182. ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true,
  183. P.isLastInMainSourceFile());
  184. IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnterWithinCaller =
  185. P.getCallEnterWithinCallerEvent();
  186. ++depth;
  187. if (callEnterWithinCaller)
  188. ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts,
  189. indent, depth, true);
  190. for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I)
  191. ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true);
  192. --depth;
  193. IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
  194. P.getCallExitEvent();
  195. if (callExit)
  196. ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true);
  197. }
  198. static void ReportMacro(raw_ostream &o,
  199. const PathDiagnosticMacroPiece& P,
  200. const FIDMap& FM, const SourceManager &SM,
  201. const LangOptions &LangOpts,
  202. unsigned indent,
  203. unsigned depth) {
  204. for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
  205. I!=E; ++I) {
  206. ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false);
  207. }
  208. }
  209. static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P,
  210. const FIDMap& FM, const SourceManager &SM,
  211. const LangOptions &LangOpts) {
  212. ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true);
  213. }
  214. static void ReportPiece(raw_ostream &o,
  215. const PathDiagnosticPiece &P,
  216. const FIDMap& FM, const SourceManager &SM,
  217. const LangOptions &LangOpts,
  218. unsigned indent,
  219. unsigned depth,
  220. bool includeControlFlow,
  221. bool isKeyEvent) {
  222. switch (P.getKind()) {
  223. case PathDiagnosticPiece::ControlFlow:
  224. if (includeControlFlow)
  225. ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
  226. LangOpts, indent);
  227. break;
  228. case PathDiagnosticPiece::Call:
  229. ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts,
  230. indent, depth);
  231. break;
  232. case PathDiagnosticPiece::Event:
  233. ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts,
  234. indent, depth, isKeyEvent);
  235. break;
  236. case PathDiagnosticPiece::Macro:
  237. ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts,
  238. indent, depth);
  239. break;
  240. }
  241. }
  242. void PlistDiagnostics::FlushDiagnosticsImpl(
  243. std::vector<const PathDiagnostic *> &Diags,
  244. FilesMade *filesMade) {
  245. // Build up a set of FIDs that we use by scanning the locations and
  246. // ranges of the diagnostics.
  247. FIDMap FM;
  248. SmallVector<FileID, 10> Fids;
  249. const SourceManager* SM = nullptr;
  250. if (!Diags.empty())
  251. SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager();
  252. for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(),
  253. DE = Diags.end(); DI != DE; ++DI) {
  254. const PathDiagnostic *D = *DI;
  255. SmallVector<const PathPieces *, 5> WorkList;
  256. WorkList.push_back(&D->path);
  257. while (!WorkList.empty()) {
  258. const PathPieces &path = *WorkList.pop_back_val();
  259. for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E;
  260. ++I) {
  261. const PathDiagnosticPiece *piece = I->get();
  262. AddFID(FM, Fids, *SM, piece->getLocation().asLocation());
  263. ArrayRef<SourceRange> Ranges = piece->getRanges();
  264. for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
  265. E = Ranges.end(); I != E; ++I) {
  266. AddFID(FM, Fids, *SM, I->getBegin());
  267. AddFID(FM, Fids, *SM, I->getEnd());
  268. }
  269. if (const PathDiagnosticCallPiece *call =
  270. dyn_cast<PathDiagnosticCallPiece>(piece)) {
  271. IntrusiveRefCntPtr<PathDiagnosticEventPiece>
  272. callEnterWithin = call->getCallEnterWithinCallerEvent();
  273. if (callEnterWithin)
  274. AddFID(FM, Fids, *SM, callEnterWithin->getLocation().asLocation());
  275. WorkList.push_back(&call->path);
  276. }
  277. else if (const PathDiagnosticMacroPiece *macro =
  278. dyn_cast<PathDiagnosticMacroPiece>(piece)) {
  279. WorkList.push_back(&macro->subPieces);
  280. }
  281. }
  282. }
  283. }
  284. // Open the file.
  285. std::error_code EC;
  286. llvm::raw_fd_ostream o(OutputFile, EC, llvm::sys::fs::F_Text);
  287. if (EC) {
  288. llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
  289. return;
  290. }
  291. EmitPlistHeader(o);
  292. // Write the root object: a <dict> containing...
  293. // - "clang_version", the string representation of clang version
  294. // - "files", an <array> mapping from FIDs to file names
  295. // - "diagnostics", an <array> containing the path diagnostics
  296. o << "<dict>\n" <<
  297. " <key>clang_version</key>\n";
  298. EmitString(o, getClangFullVersion()) << '\n';
  299. o << " <key>files</key>\n"
  300. " <array>\n";
  301. for (FileID FID : Fids)
  302. EmitString(o << " ", SM->getFileEntryForID(FID)->getName()) << '\n';
  303. o << " </array>\n"
  304. " <key>diagnostics</key>\n"
  305. " <array>\n";
  306. for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(),
  307. DE = Diags.end(); DI!=DE; ++DI) {
  308. o << " <dict>\n"
  309. " <key>path</key>\n";
  310. const PathDiagnostic *D = *DI;
  311. o << " <array>\n";
  312. for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end();
  313. I != E; ++I)
  314. ReportDiag(o, **I, FM, *SM, LangOpts);
  315. o << " </array>\n";
  316. // Output the bug type and bug category.
  317. o << " <key>description</key>";
  318. EmitString(o, D->getShortDescription()) << '\n';
  319. o << " <key>category</key>";
  320. EmitString(o, D->getCategory()) << '\n';
  321. o << " <key>type</key>";
  322. EmitString(o, D->getBugType()) << '\n';
  323. o << " <key>check_name</key>";
  324. EmitString(o, D->getCheckName()) << '\n';
  325. // Output information about the semantic context where
  326. // the issue occurred.
  327. if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
  328. // FIXME: handle blocks, which have no name.
  329. if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
  330. StringRef declKind;
  331. switch (ND->getKind()) {
  332. case Decl::CXXRecord:
  333. declKind = "C++ class";
  334. break;
  335. case Decl::CXXMethod:
  336. declKind = "C++ method";
  337. break;
  338. case Decl::ObjCMethod:
  339. declKind = "Objective-C method";
  340. break;
  341. case Decl::Function:
  342. declKind = "function";
  343. break;
  344. default:
  345. break;
  346. }
  347. if (!declKind.empty()) {
  348. const std::string &declName = ND->getDeclName().getAsString();
  349. o << " <key>issue_context_kind</key>";
  350. EmitString(o, declKind) << '\n';
  351. o << " <key>issue_context</key>";
  352. EmitString(o, declName) << '\n';
  353. }
  354. // Output the bug hash for issue unique-ing. Currently, it's just an
  355. // offset from the beginning of the function.
  356. if (const Stmt *Body = DeclWithIssue->getBody()) {
  357. // If the bug uniqueing location exists, use it for the hash.
  358. // For example, this ensures that two leaks reported on the same line
  359. // will have different issue_hashes and that the hash will identify
  360. // the leak location even after code is added between the allocation
  361. // site and the end of scope (leak report location).
  362. PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
  363. if (UPDLoc.isValid()) {
  364. FullSourceLoc UL(SM->getExpansionLoc(UPDLoc.asLocation()),
  365. *SM);
  366. FullSourceLoc UFunL(SM->getExpansionLoc(
  367. D->getUniqueingDecl()->getBody()->getLocStart()), *SM);
  368. o << " <key>issue_hash</key><string>"
  369. << UL.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
  370. << "</string>\n";
  371. // Otherwise, use the location on which the bug is reported.
  372. } else {
  373. FullSourceLoc L(SM->getExpansionLoc(D->getLocation().asLocation()),
  374. *SM);
  375. FullSourceLoc FunL(SM->getExpansionLoc(Body->getLocStart()), *SM);
  376. o << " <key>issue_hash</key><string>"
  377. << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
  378. << "</string>\n";
  379. }
  380. }
  381. }
  382. }
  383. // Output the location of the bug.
  384. o << " <key>location</key>\n";
  385. EmitLocation(o, *SM, D->getLocation().asLocation(), FM, 2);
  386. // Output the diagnostic to the sub-diagnostic client, if any.
  387. if (!filesMade->empty()) {
  388. StringRef lastName;
  389. PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
  390. if (files) {
  391. for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
  392. CE = files->end(); CI != CE; ++CI) {
  393. StringRef newName = CI->first;
  394. if (newName != lastName) {
  395. if (!lastName.empty()) {
  396. o << " </array>\n";
  397. }
  398. lastName = newName;
  399. o << " <key>" << lastName << "_files</key>\n";
  400. o << " <array>\n";
  401. }
  402. o << " <string>" << CI->second << "</string>\n";
  403. }
  404. o << " </array>\n";
  405. }
  406. }
  407. // Close up the entry.
  408. o << " </dict>\n";
  409. }
  410. o << " </array>\n";
  411. // Finish.
  412. o << "</dict>\n</plist>";
  413. }