PlistDiagnostics.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. virtual ~PlistDiagnostics() {}
  38. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  39. FilesMade *filesMade) override;
  40. virtual 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. SourceLocation StartEdge = I->getStart().asRange().getBegin();
  96. EmitRange(o, SM, LangOpts, CharSourceRange::getTokenRange(StartEdge), FM,
  97. indent + 1);
  98. Indent(o, indent) << "<key>end</key>\n";
  99. SourceLocation EndEdge = I->getEnd().asRange().getBegin();
  100. EmitRange(o, SM, LangOpts, CharSourceRange::getTokenRange(EndEdge), FM,
  101. indent + 1);
  102. --indent;
  103. Indent(o, indent) << "</dict>\n";
  104. }
  105. --indent;
  106. Indent(o, indent) << "</array>\n";
  107. --indent;
  108. // Output any helper text.
  109. const std::string& s = P.getString();
  110. if (!s.empty()) {
  111. Indent(o, indent) << "<key>alternate</key>";
  112. EmitString(o, s) << '\n';
  113. }
  114. --indent;
  115. Indent(o, indent) << "</dict>\n";
  116. }
  117. static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P,
  118. const FIDMap& FM,
  119. const SourceManager &SM,
  120. const LangOptions &LangOpts,
  121. unsigned indent,
  122. unsigned depth,
  123. bool isKeyEvent = false) {
  124. Indent(o, indent) << "<dict>\n";
  125. ++indent;
  126. Indent(o, indent) << "<key>kind</key><string>event</string>\n";
  127. if (isKeyEvent) {
  128. Indent(o, indent) << "<key>key_event</key><true/>\n";
  129. }
  130. // Output the location.
  131. FullSourceLoc L = P.getLocation().asLocation();
  132. Indent(o, indent) << "<key>location</key>\n";
  133. EmitLocation(o, SM, LangOpts, L, FM, indent);
  134. // Output the ranges (if any).
  135. ArrayRef<SourceRange> Ranges = P.getRanges();
  136. if (!Ranges.empty()) {
  137. Indent(o, indent) << "<key>ranges</key>\n";
  138. Indent(o, indent) << "<array>\n";
  139. ++indent;
  140. for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
  141. I != E; ++I) {
  142. EmitRange(o, SM, LangOpts, CharSourceRange::getTokenRange(*I), FM,
  143. indent + 1);
  144. }
  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. // Output information about the semantic context where
  324. // the issue occurred.
  325. if (const Decl *DeclWithIssue = D->getDeclWithIssue()) {
  326. // FIXME: handle blocks, which have no name.
  327. if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
  328. StringRef declKind;
  329. switch (ND->getKind()) {
  330. case Decl::CXXRecord:
  331. declKind = "C++ class";
  332. break;
  333. case Decl::CXXMethod:
  334. declKind = "C++ method";
  335. break;
  336. case Decl::ObjCMethod:
  337. declKind = "Objective-C method";
  338. break;
  339. case Decl::Function:
  340. declKind = "function";
  341. break;
  342. default:
  343. break;
  344. }
  345. if (!declKind.empty()) {
  346. const std::string &declName = ND->getDeclName().getAsString();
  347. o << " <key>issue_context_kind</key>";
  348. EmitString(o, declKind) << '\n';
  349. o << " <key>issue_context</key>";
  350. EmitString(o, declName) << '\n';
  351. }
  352. // Output the bug hash for issue unique-ing. Currently, it's just an
  353. // offset from the beginning of the function.
  354. if (const Stmt *Body = DeclWithIssue->getBody()) {
  355. // If the bug uniqueing location exists, use it for the hash.
  356. // For example, this ensures that two leaks reported on the same line
  357. // will have different issue_hashes and that the hash will identify
  358. // the leak location even after code is added between the allocation
  359. // site and the end of scope (leak report location).
  360. PathDiagnosticLocation UPDLoc = D->getUniqueingLoc();
  361. if (UPDLoc.isValid()) {
  362. FullSourceLoc UL(SM->getExpansionLoc(UPDLoc.asLocation()),
  363. *SM);
  364. FullSourceLoc UFunL(SM->getExpansionLoc(
  365. D->getUniqueingDecl()->getBody()->getLocStart()), *SM);
  366. o << " <key>issue_hash</key><string>"
  367. << UL.getExpansionLineNumber() - UFunL.getExpansionLineNumber()
  368. << "</string>\n";
  369. // Otherwise, use the location on which the bug is reported.
  370. } else {
  371. FullSourceLoc L(SM->getExpansionLoc(D->getLocation().asLocation()),
  372. *SM);
  373. FullSourceLoc FunL(SM->getExpansionLoc(Body->getLocStart()), *SM);
  374. o << " <key>issue_hash</key><string>"
  375. << L.getExpansionLineNumber() - FunL.getExpansionLineNumber()
  376. << "</string>\n";
  377. }
  378. }
  379. }
  380. }
  381. // Output the location of the bug.
  382. o << " <key>location</key>\n";
  383. EmitLocation(o, *SM, LangOpts, D->getLocation().asLocation(), FM, 2);
  384. // Output the diagnostic to the sub-diagnostic client, if any.
  385. if (!filesMade->empty()) {
  386. StringRef lastName;
  387. PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D);
  388. if (files) {
  389. for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(),
  390. CE = files->end(); CI != CE; ++CI) {
  391. StringRef newName = CI->first;
  392. if (newName != lastName) {
  393. if (!lastName.empty()) {
  394. o << " </array>\n";
  395. }
  396. lastName = newName;
  397. o << " <key>" << lastName << "_files</key>\n";
  398. o << " <array>\n";
  399. }
  400. o << " <string>" << CI->second << "</string>\n";
  401. }
  402. o << " </array>\n";
  403. }
  404. }
  405. // Close up the entry.
  406. o << " </dict>\n";
  407. }
  408. o << " </array>\n";
  409. // Finish.
  410. o << "</dict>\n</plist>";
  411. }