SarifDiagnostics.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //===--- SarifDiagnostics.cpp - Sarif Diagnostics for Paths -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the SarifDiagnostics object.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Analysis/MacroExpansionContext.h"
  13. #include "clang/Analysis/PathDiagnostic.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/Sarif.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "clang/Basic/Version.h"
  18. #include "clang/Lex/Preprocessor.h"
  19. #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/StringMap.h"
  22. #include "llvm/Support/ConvertUTF.h"
  23. #include "llvm/Support/JSON.h"
  24. #include "llvm/Support/Path.h"
  25. using namespace llvm;
  26. using namespace clang;
  27. using namespace ento;
  28. namespace {
  29. class SarifDiagnostics : public PathDiagnosticConsumer {
  30. std::string OutputFile;
  31. const LangOptions &LO;
  32. SarifDocumentWriter SarifWriter;
  33. public:
  34. SarifDiagnostics(const std::string &Output, const LangOptions &LO,
  35. const SourceManager &SM)
  36. : OutputFile(Output), LO(LO), SarifWriter(SM) {}
  37. ~SarifDiagnostics() override = default;
  38. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  39. FilesMade *FM) override;
  40. StringRef getName() const override { return "SarifDiagnostics"; }
  41. PathGenerationScheme getGenerationScheme() const override { return Minimal; }
  42. bool supportsLogicalOpControlFlow() const override { return true; }
  43. bool supportsCrossFileDiagnostics() const override { return true; }
  44. };
  45. } // end anonymous namespace
  46. void ento::createSarifDiagnosticConsumer(
  47. PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
  48. const std::string &Output, const Preprocessor &PP,
  49. const cross_tu::CrossTranslationUnitContext &CTU,
  50. const MacroExpansionContext &MacroExpansions) {
  51. // TODO: Emit an error here.
  52. if (Output.empty())
  53. return;
  54. C.push_back(
  55. new SarifDiagnostics(Output, PP.getLangOpts(), PP.getSourceManager()));
  56. createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, Output, PP,
  57. CTU, MacroExpansions);
  58. }
  59. static StringRef getRuleDescription(StringRef CheckName) {
  60. return llvm::StringSwitch<StringRef>(CheckName)
  61. #define GET_CHECKERS
  62. #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
  63. .Case(FULLNAME, HELPTEXT)
  64. #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
  65. #undef CHECKER
  66. #undef GET_CHECKERS
  67. ;
  68. }
  69. static StringRef getRuleHelpURIStr(StringRef CheckName) {
  70. return llvm::StringSwitch<StringRef>(CheckName)
  71. #define GET_CHECKERS
  72. #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
  73. .Case(FULLNAME, DOC_URI)
  74. #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
  75. #undef CHECKER
  76. #undef GET_CHECKERS
  77. ;
  78. }
  79. static ThreadFlowImportance
  80. calculateImportance(const PathDiagnosticPiece &Piece) {
  81. switch (Piece.getKind()) {
  82. case PathDiagnosticPiece::Call:
  83. case PathDiagnosticPiece::Macro:
  84. case PathDiagnosticPiece::Note:
  85. case PathDiagnosticPiece::PopUp:
  86. // FIXME: What should be reported here?
  87. break;
  88. case PathDiagnosticPiece::Event:
  89. return Piece.getTagStr() == "ConditionBRVisitor"
  90. ? ThreadFlowImportance::Important
  91. : ThreadFlowImportance::Essential;
  92. case PathDiagnosticPiece::ControlFlow:
  93. return ThreadFlowImportance::Unimportant;
  94. }
  95. return ThreadFlowImportance::Unimportant;
  96. }
  97. /// Accepts a SourceRange corresponding to a pair of the first and last tokens
  98. /// and converts to a Character granular CharSourceRange.
  99. static CharSourceRange convertTokenRangeToCharRange(const SourceRange &R,
  100. const SourceManager &SM,
  101. const LangOptions &LO) {
  102. // Caret diagnostics have the first and last locations pointed at the same
  103. // location, return these as-is.
  104. if (R.getBegin() == R.getEnd())
  105. return CharSourceRange::getCharRange(R);
  106. SourceLocation BeginCharLoc = R.getBegin();
  107. // For token ranges, the raw end SLoc points at the first character of the
  108. // last token in the range. This must be moved to one past the end of the
  109. // last character using the lexer.
  110. SourceLocation EndCharLoc =
  111. Lexer::getLocForEndOfToken(R.getEnd(), /* Offset = */ 0, SM, LO);
  112. return CharSourceRange::getCharRange(BeginCharLoc, EndCharLoc);
  113. }
  114. static SmallVector<ThreadFlow, 8> createThreadFlows(const PathDiagnostic *Diag,
  115. const LangOptions &LO) {
  116. SmallVector<ThreadFlow, 8> Flows;
  117. const PathPieces &Pieces = Diag->path.flatten(false);
  118. for (const auto &Piece : Pieces) {
  119. auto Range = convertTokenRangeToCharRange(
  120. Piece->getLocation().asRange(), Piece->getLocation().getManager(), LO);
  121. auto Flow = ThreadFlow::create()
  122. .setImportance(calculateImportance(*Piece))
  123. .setRange(Range)
  124. .setMessage(Piece->getString());
  125. Flows.push_back(Flow);
  126. }
  127. return Flows;
  128. }
  129. static StringMap<uint32_t>
  130. createRuleMapping(const std::vector<const PathDiagnostic *> &Diags,
  131. SarifDocumentWriter &SarifWriter) {
  132. StringMap<uint32_t> RuleMapping;
  133. llvm::StringSet<> Seen;
  134. for (const PathDiagnostic *D : Diags) {
  135. StringRef CheckName = D->getCheckerName();
  136. std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(CheckName);
  137. if (P.second) {
  138. auto Rule = SarifRule::create()
  139. .setName(CheckName)
  140. .setRuleId(CheckName)
  141. .setDescription(getRuleDescription(CheckName))
  142. .setHelpURI(getRuleHelpURIStr(CheckName));
  143. size_t RuleIdx = SarifWriter.createRule(Rule);
  144. RuleMapping[CheckName] = RuleIdx;
  145. }
  146. }
  147. return RuleMapping;
  148. }
  149. static SarifResult createResult(const PathDiagnostic *Diag,
  150. const StringMap<uint32_t> &RuleMapping,
  151. const LangOptions &LO) {
  152. StringRef CheckName = Diag->getCheckerName();
  153. uint32_t RuleIdx = RuleMapping.lookup(CheckName);
  154. auto Range = convertTokenRangeToCharRange(
  155. Diag->getLocation().asRange(), Diag->getLocation().getManager(), LO);
  156. SmallVector<ThreadFlow, 8> Flows = createThreadFlows(Diag, LO);
  157. auto Result = SarifResult::create(RuleIdx)
  158. .setRuleId(CheckName)
  159. .setDiagnosticMessage(Diag->getVerboseDescription())
  160. .setDiagnosticLevel(SarifResultLevel::Warning)
  161. .setLocations({Range})
  162. .setThreadFlows(Flows);
  163. return Result;
  164. }
  165. void SarifDiagnostics::FlushDiagnosticsImpl(
  166. std::vector<const PathDiagnostic *> &Diags, FilesMade *) {
  167. // We currently overwrite the file if it already exists. However, it may be
  168. // useful to add a feature someday that allows the user to append a run to an
  169. // existing SARIF file. One danger from that approach is that the size of the
  170. // file can become large very quickly, so decoding into JSON to append a run
  171. // may be an expensive operation.
  172. std::error_code EC;
  173. llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
  174. if (EC) {
  175. llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
  176. return;
  177. }
  178. std::string ToolVersion = getClangFullVersion();
  179. SarifWriter.createRun("clang", "clang static analyzer", ToolVersion);
  180. StringMap<uint32_t> RuleMapping = createRuleMapping(Diags, SarifWriter);
  181. for (const PathDiagnostic *D : Diags) {
  182. SarifResult Result = createResult(D, RuleMapping, LO);
  183. SarifWriter.appendResult(Result);
  184. }
  185. auto Document = SarifWriter.createDocument();
  186. OS << llvm::formatv("{0:2}\n", json::Value(std::move(Document)));
  187. }