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