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