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