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 13 #include "clang/Analysis/PathDiagnostic.h" 14 #include "clang/Basic/Version.h" 15 #include "clang/Lex/Preprocessor.h" 16 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 17 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/Support/JSON.h" 21 #include "llvm/Support/Path.h" 22 23 using namespace llvm; 24 using namespace clang; 25 using namespace ento; 26 27 namespace { 28 class SarifDiagnostics : public PathDiagnosticConsumer { 29 std::string OutputFile; 30 31 public: 32 SarifDiagnostics(AnalyzerOptions &, const std::string &Output) 33 : OutputFile(Output) {} 34 ~SarifDiagnostics() override = default; 35 36 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 37 FilesMade *FM) override; 38 39 StringRef getName() const override { return "SarifDiagnostics"; } 40 PathGenerationScheme getGenerationScheme() const override { return Minimal; } 41 bool supportsLogicalOpControlFlow() const override { return true; } 42 bool supportsCrossFileDiagnostics() const override { return true; } 43 }; 44 } // end anonymous namespace 45 46 void ento::createSarifDiagnosticConsumer( 47 AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, 48 const std::string &Output, const Preprocessor &, 49 const cross_tu::CrossTranslationUnitContext &) { 50 C.push_back(new SarifDiagnostics(AnalyzerOpts, Output)); 51 } 52 53 static StringRef getFileName(const FileEntry &FE) { 54 StringRef Filename = FE.tryGetRealPathName(); 55 if (Filename.empty()) 56 Filename = FE.getName(); 57 return Filename; 58 } 59 60 static std::string percentEncodeURICharacter(char C) { 61 // RFC 3986 claims alpha, numeric, and this handful of 62 // characters are not reserved for the path component and 63 // should be written out directly. Otherwise, percent 64 // encode the character and write that out instead of the 65 // reserved character. 66 if (llvm::isAlnum(C) || 67 StringRef::npos != StringRef("-._~:@!$&'()*+,;=").find(C)) 68 return std::string(&C, 1); 69 return "%" + llvm::toHex(StringRef(&C, 1)); 70 } 71 72 static std::string fileNameToURI(StringRef Filename) { 73 llvm::SmallString<32> Ret = StringRef("file://"); 74 75 // Get the root name to see if it has a URI authority. 76 StringRef Root = sys::path::root_name(Filename); 77 if (Root.startswith("//")) { 78 // There is an authority, so add it to the URI. 79 Ret += Root.drop_front(2).str(); 80 } else if (!Root.empty()) { 81 // There is no authority, so end the component and add the root to the URI. 82 Ret += Twine("/" + Root).str(); 83 } 84 85 auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename); 86 assert(Iter != End && "Expected there to be a non-root path component."); 87 // Add the rest of the path components, encoding any reserved characters; 88 // we skip past the first path component, as it was handled it above. 89 std::for_each(++Iter, End, [&Ret](StringRef Component) { 90 // For reasons unknown to me, we may get a backslash with Windows native 91 // paths for the initial backslash following the drive component, which 92 // we need to ignore as a URI path part. 93 if (Component == "\\") 94 return; 95 96 // Add the separator between the previous path part and the one being 97 // currently processed. 98 Ret += "/"; 99 100 // URI encode the part. 101 for (char C : Component) { 102 Ret += percentEncodeURICharacter(C); 103 } 104 }); 105 106 return Ret.str().str(); 107 } 108 109 static json::Object createArtifactLocation(const FileEntry &FE) { 110 return json::Object{{"uri", fileNameToURI(getFileName(FE))}}; 111 } 112 113 static json::Object createArtifact(const FileEntry &FE) { 114 return json::Object{{"location", createArtifactLocation(FE)}, 115 {"roles", json::Array{"resultFile"}}, 116 {"length", FE.getSize()}, 117 {"mimeType", "text/plain"}}; 118 } 119 120 static json::Object createArtifactLocation(const FileEntry &FE, 121 json::Array &Artifacts) { 122 std::string FileURI = fileNameToURI(getFileName(FE)); 123 124 // See if the Artifacts array contains this URI already. If it does not, 125 // create a new artifact object to add to the array. 126 auto I = llvm::find_if(Artifacts, [&](const json::Value &File) { 127 if (const json::Object *Obj = File.getAsObject()) { 128 if (const json::Object *FileLoc = Obj->getObject("location")) { 129 Optional<StringRef> URI = FileLoc->getString("uri"); 130 return URI && URI->equals(FileURI); 131 } 132 } 133 return false; 134 }); 135 136 // Calculate the index within the artifact array so it can be stored in 137 // the JSON object. 138 auto Index = static_cast<unsigned>(std::distance(Artifacts.begin(), I)); 139 if (I == Artifacts.end()) 140 Artifacts.push_back(createArtifact(FE)); 141 142 return json::Object{{"uri", FileURI}, {"index", Index}}; 143 } 144 145 static json::Object createTextRegion(SourceRange R, const SourceManager &SM) { 146 json::Object Region{ 147 {"startLine", SM.getExpansionLineNumber(R.getBegin())}, 148 {"startColumn", SM.getExpansionColumnNumber(R.getBegin())}, 149 }; 150 if (R.getBegin() == R.getEnd()) { 151 Region["endColumn"] = SM.getExpansionColumnNumber(R.getBegin()); 152 } else { 153 Region["endLine"] = SM.getExpansionLineNumber(R.getEnd()); 154 Region["endColumn"] = SM.getExpansionColumnNumber(R.getEnd()) + 1; 155 } 156 return Region; 157 } 158 159 static json::Object createPhysicalLocation(SourceRange R, const FileEntry &FE, 160 const SourceManager &SMgr, 161 json::Array &Artifacts) { 162 return json::Object{ 163 {{"artifactLocation", createArtifactLocation(FE, Artifacts)}, 164 {"region", createTextRegion(R, SMgr)}}}; 165 } 166 167 enum class Importance { Important, Essential, Unimportant }; 168 169 static StringRef importanceToStr(Importance I) { 170 switch (I) { 171 case Importance::Important: 172 return "important"; 173 case Importance::Essential: 174 return "essential"; 175 case Importance::Unimportant: 176 return "unimportant"; 177 } 178 llvm_unreachable("Fully covered switch is not so fully covered"); 179 } 180 181 static json::Object createThreadFlowLocation(json::Object &&Location, 182 Importance I) { 183 return json::Object{{"location", std::move(Location)}, 184 {"importance", importanceToStr(I)}}; 185 } 186 187 static json::Object createMessage(StringRef Text) { 188 return json::Object{{"text", Text.str()}}; 189 } 190 191 static json::Object createLocation(json::Object &&PhysicalLocation, 192 StringRef Message = "") { 193 json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}}; 194 if (!Message.empty()) 195 Ret.insert({"message", createMessage(Message)}); 196 return Ret; 197 } 198 199 static Importance calculateImportance(const PathDiagnosticPiece &Piece) { 200 switch (Piece.getKind()) { 201 case PathDiagnosticPiece::Call: 202 case PathDiagnosticPiece::Macro: 203 case PathDiagnosticPiece::Note: 204 case PathDiagnosticPiece::PopUp: 205 // FIXME: What should be reported here? 206 break; 207 case PathDiagnosticPiece::Event: 208 return Piece.getTagStr() == "ConditionBRVisitor" ? Importance::Important 209 : Importance::Essential; 210 case PathDiagnosticPiece::ControlFlow: 211 return Importance::Unimportant; 212 } 213 return Importance::Unimportant; 214 } 215 216 static json::Object createThreadFlow(const PathPieces &Pieces, 217 json::Array &Artifacts) { 218 const SourceManager &SMgr = Pieces.front()->getLocation().getManager(); 219 json::Array Locations; 220 for (const auto &Piece : Pieces) { 221 const PathDiagnosticLocation &P = Piece->getLocation(); 222 Locations.push_back(createThreadFlowLocation( 223 createLocation(createPhysicalLocation( 224 P.asRange(), 225 *P.asLocation().getExpansionLoc().getFileEntry(), 226 SMgr, Artifacts), 227 Piece->getString()), 228 calculateImportance(*Piece))); 229 } 230 return json::Object{{"locations", std::move(Locations)}}; 231 } 232 233 static json::Object createCodeFlow(const PathPieces &Pieces, 234 json::Array &Artifacts) { 235 return json::Object{ 236 {"threadFlows", json::Array{createThreadFlow(Pieces, Artifacts)}}}; 237 } 238 239 static json::Object createResult(const PathDiagnostic &Diag, 240 json::Array &Artifacts, 241 const StringMap<unsigned> &RuleMapping) { 242 const PathPieces &Path = Diag.path.flatten(false); 243 const SourceManager &SMgr = Path.front()->getLocation().getManager(); 244 245 auto Iter = RuleMapping.find(Diag.getCheckerName()); 246 assert(Iter != RuleMapping.end() && "Rule ID is not in the array index map?"); 247 248 return json::Object{ 249 {"message", createMessage(Diag.getVerboseDescription())}, 250 {"codeFlows", json::Array{createCodeFlow(Path, Artifacts)}}, 251 {"locations", 252 json::Array{createLocation(createPhysicalLocation( 253 Diag.getLocation().asRange(), 254 *Diag.getLocation().asLocation().getExpansionLoc().getFileEntry(), 255 SMgr, Artifacts))}}, 256 {"ruleIndex", Iter->getValue()}, 257 {"ruleId", Diag.getCheckerName()}}; 258 } 259 260 static StringRef getRuleDescription(StringRef CheckName) { 261 return llvm::StringSwitch<StringRef>(CheckName) 262 #define GET_CHECKERS 263 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \ 264 .Case(FULLNAME, HELPTEXT) 265 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 266 #undef CHECKER 267 #undef GET_CHECKERS 268 ; 269 } 270 271 static StringRef getRuleHelpURIStr(StringRef CheckName) { 272 return llvm::StringSwitch<StringRef>(CheckName) 273 #define GET_CHECKERS 274 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \ 275 .Case(FULLNAME, DOC_URI) 276 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 277 #undef CHECKER 278 #undef GET_CHECKERS 279 ; 280 } 281 282 static json::Object createRule(const PathDiagnostic &Diag) { 283 StringRef CheckName = Diag.getCheckerName(); 284 json::Object Ret{ 285 {"fullDescription", createMessage(getRuleDescription(CheckName))}, 286 {"name", CheckName}, 287 {"id", CheckName}}; 288 289 std::string RuleURI = getRuleHelpURIStr(CheckName); 290 if (!RuleURI.empty()) 291 Ret["helpUri"] = RuleURI; 292 293 return Ret; 294 } 295 296 static json::Array createRules(std::vector<const PathDiagnostic *> &Diags, 297 StringMap<unsigned> &RuleMapping) { 298 json::Array Rules; 299 llvm::StringSet<> Seen; 300 301 llvm::for_each(Diags, [&](const PathDiagnostic *D) { 302 StringRef RuleID = D->getCheckerName(); 303 std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(RuleID); 304 if (P.second) { 305 RuleMapping[RuleID] = Rules.size(); // Maps RuleID to an Array Index. 306 Rules.push_back(createRule(*D)); 307 } 308 }); 309 310 return Rules; 311 } 312 313 static json::Object createTool(std::vector<const PathDiagnostic *> &Diags, 314 StringMap<unsigned> &RuleMapping) { 315 return json::Object{ 316 {"driver", json::Object{{"name", "clang"}, 317 {"fullName", "clang static analyzer"}, 318 {"language", "en-US"}, 319 {"version", getClangFullVersion()}, 320 {"rules", createRules(Diags, RuleMapping)}}}}; 321 } 322 323 static json::Object createRun(std::vector<const PathDiagnostic *> &Diags) { 324 json::Array Results, Artifacts; 325 StringMap<unsigned> RuleMapping; 326 json::Object Tool = createTool(Diags, RuleMapping); 327 328 llvm::for_each(Diags, [&](const PathDiagnostic *D) { 329 Results.push_back(createResult(*D, Artifacts, RuleMapping)); 330 }); 331 332 return json::Object{{"tool", std::move(Tool)}, 333 {"results", std::move(Results)}, 334 {"artifacts", std::move(Artifacts)}}; 335 } 336 337 void SarifDiagnostics::FlushDiagnosticsImpl( 338 std::vector<const PathDiagnostic *> &Diags, FilesMade *) { 339 // We currently overwrite the file if it already exists. However, it may be 340 // useful to add a feature someday that allows the user to append a run to an 341 // existing SARIF file. One danger from that approach is that the size of the 342 // file can become large very quickly, so decoding into JSON to append a run 343 // may be an expensive operation. 344 std::error_code EC; 345 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text); 346 if (EC) { 347 llvm::errs() << "warning: could not create file: " << EC.message() << '\n'; 348 return; 349 } 350 json::Object Sarif{ 351 {"$schema", 352 "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"}, 353 {"version", "2.1.0"}, 354 {"runs", json::Array{createRun(Diags)}}}; 355 OS << llvm::formatv("{0:2}\n", json::Value(std::move(Sarif))); 356 } 357