PlistDiagnostics.cpp revision c4bac8e376b98d633bb00ee5f510d5e58449753c
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/PathDiagnosticConsumers.h" 15#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 16#include "clang/Basic/SourceManager.h" 17#include "clang/Basic/FileManager.h" 18#include "clang/Lex/Preprocessor.h" 19#include "llvm/Support/raw_ostream.h" 20#include "llvm/Support/Casting.h" 21#include "llvm/ADT/DenseMap.h" 22#include "llvm/ADT/SmallVector.h" 23using namespace clang; 24using namespace ento; 25 26typedef llvm::DenseMap<FileID, unsigned> FIDMap; 27 28 29namespace { 30 class PlistDiagnostics : public PathDiagnosticConsumer { 31 const std::string OutputFile; 32 const LangOptions &LangOpts; 33 const bool SupportsCrossFileDiagnostics; 34 public: 35 PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts, 36 bool supportsMultipleFiles); 37 38 virtual ~PlistDiagnostics() {} 39 40 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 41 FilesMade *filesMade); 42 43 virtual StringRef getName() const { 44 return "PlistDiagnostics"; 45 } 46 47 PathGenerationScheme getGenerationScheme() const { return Extensive; } 48 bool supportsLogicalOpControlFlow() const { return true; } 49 bool supportsAllBlockEdges() const { return true; } 50 virtual bool useVerboseDescription() const { return false; } 51 virtual bool supportsCrossFileDiagnostics() const { 52 return SupportsCrossFileDiagnostics; 53 } 54 }; 55} // end anonymous namespace 56 57PlistDiagnostics::PlistDiagnostics(const std::string& output, 58 const LangOptions &LO, 59 bool supportsMultipleFiles) 60 : OutputFile(output), LangOpts(LO), 61 SupportsCrossFileDiagnostics(supportsMultipleFiles) {} 62 63void ento::createPlistDiagnosticConsumer(PathDiagnosticConsumers &C, 64 const std::string& s, 65 const Preprocessor &PP) { 66 C.push_back(new PlistDiagnostics(s, PP.getLangOpts(), false)); 67} 68 69void ento::createPlistMultiFileDiagnosticConsumer(PathDiagnosticConsumers &C, 70 const std::string &s, 71 const Preprocessor &PP) { 72 C.push_back(new PlistDiagnostics(s, PP.getLangOpts(), true)); 73} 74 75static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V, 76 const SourceManager* SM, SourceLocation L) { 77 78 FileID FID = SM->getFileID(SM->getExpansionLoc(L)); 79 FIDMap::iterator I = FIDs.find(FID); 80 if (I != FIDs.end()) return; 81 FIDs[FID] = V.size(); 82 V.push_back(FID); 83} 84 85static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, 86 SourceLocation L) { 87 FileID FID = SM.getFileID(SM.getExpansionLoc(L)); 88 FIDMap::const_iterator I = FIDs.find(FID); 89 assert(I != FIDs.end()); 90 return I->second; 91} 92 93static raw_ostream &Indent(raw_ostream &o, const unsigned indent) { 94 for (unsigned i = 0; i < indent; ++i) o << ' '; 95 return o; 96} 97 98static void EmitLocation(raw_ostream &o, const SourceManager &SM, 99 const LangOptions &LangOpts, 100 SourceLocation L, const FIDMap &FM, 101 unsigned indent, bool extend = false) { 102 103 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM)); 104 105 // Add in the length of the token, so that we cover multi-char tokens. 106 unsigned offset = 107 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; 108 109 Indent(o, indent) << "<dict>\n"; 110 Indent(o, indent) << " <key>line</key><integer>" 111 << Loc.getExpansionLineNumber() << "</integer>\n"; 112 Indent(o, indent) << " <key>col</key><integer>" 113 << Loc.getExpansionColumnNumber() + offset << "</integer>\n"; 114 Indent(o, indent) << " <key>file</key><integer>" 115 << GetFID(FM, SM, Loc) << "</integer>\n"; 116 Indent(o, indent) << "</dict>\n"; 117} 118 119static void EmitLocation(raw_ostream &o, const SourceManager &SM, 120 const LangOptions &LangOpts, 121 const PathDiagnosticLocation &L, const FIDMap& FM, 122 unsigned indent, bool extend = false) { 123 EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend); 124} 125 126static void EmitRange(raw_ostream &o, const SourceManager &SM, 127 const LangOptions &LangOpts, 128 PathDiagnosticRange R, const FIDMap &FM, 129 unsigned indent) { 130 Indent(o, indent) << "<array>\n"; 131 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); 132 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint); 133 Indent(o, indent) << "</array>\n"; 134} 135 136static raw_ostream &EmitString(raw_ostream &o, StringRef s) { 137 o << "<string>"; 138 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) { 139 char c = *I; 140 switch (c) { 141 default: o << c; break; 142 case '&': o << "&"; break; 143 case '<': o << "<"; break; 144 case '>': o << ">"; break; 145 case '\'': o << "'"; break; 146 case '\"': o << """; break; 147 } 148 } 149 o << "</string>"; 150 return o; 151} 152 153static void ReportControlFlow(raw_ostream &o, 154 const PathDiagnosticControlFlowPiece& P, 155 const FIDMap& FM, 156 const SourceManager &SM, 157 const LangOptions &LangOpts, 158 unsigned indent) { 159 160 Indent(o, indent) << "<dict>\n"; 161 ++indent; 162 163 Indent(o, indent) << "<key>kind</key><string>control</string>\n"; 164 165 // Emit edges. 166 Indent(o, indent) << "<key>edges</key>\n"; 167 ++indent; 168 Indent(o, indent) << "<array>\n"; 169 ++indent; 170 for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end(); 171 I!=E; ++I) { 172 Indent(o, indent) << "<dict>\n"; 173 ++indent; 174 175 // Make the ranges of the start and end point self-consistent with adjacent edges 176 // by forcing to use only the beginning of the range. This simplifies the layout 177 // logic for clients. 178 Indent(o, indent) << "<key>start</key>\n"; 179 SourceLocation StartEdge = I->getStart().asRange().getBegin(); 180 EmitRange(o, SM, LangOpts, SourceRange(StartEdge, StartEdge), FM, indent+1); 181 182 Indent(o, indent) << "<key>end</key>\n"; 183 SourceLocation EndEdge = I->getEnd().asRange().getBegin(); 184 EmitRange(o, SM, LangOpts, SourceRange(EndEdge, EndEdge), FM, indent+1); 185 186 --indent; 187 Indent(o, indent) << "</dict>\n"; 188 } 189 --indent; 190 Indent(o, indent) << "</array>\n"; 191 --indent; 192 193 // Output any helper text. 194 const std::string& s = P.getString(); 195 if (!s.empty()) { 196 Indent(o, indent) << "<key>alternate</key>"; 197 EmitString(o, s) << '\n'; 198 } 199 200 --indent; 201 Indent(o, indent) << "</dict>\n"; 202} 203 204static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P, 205 const FIDMap& FM, 206 const SourceManager &SM, 207 const LangOptions &LangOpts, 208 unsigned indent, 209 unsigned depth) { 210 211 Indent(o, indent) << "<dict>\n"; 212 ++indent; 213 214 Indent(o, indent) << "<key>kind</key><string>event</string>\n"; 215 216 // Output the location. 217 FullSourceLoc L = P.getLocation().asLocation(); 218 219 Indent(o, indent) << "<key>location</key>\n"; 220 EmitLocation(o, SM, LangOpts, L, FM, indent); 221 222 // Output the ranges (if any). 223 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), 224 RE = P.ranges_end(); 225 226 if (RI != RE) { 227 Indent(o, indent) << "<key>ranges</key>\n"; 228 Indent(o, indent) << "<array>\n"; 229 ++indent; 230 for (; RI != RE; ++RI) 231 EmitRange(o, SM, LangOpts, *RI, FM, indent+1); 232 --indent; 233 Indent(o, indent) << "</array>\n"; 234 } 235 236 // Output the call depth. 237 Indent(o, indent) << "<key>depth</key>" 238 << "<integer>" << depth << "</integer>\n"; 239 240 // Output the text. 241 assert(!P.getString().empty()); 242 Indent(o, indent) << "<key>extended_message</key>\n"; 243 Indent(o, indent); 244 EmitString(o, P.getString()) << '\n'; 245 246 // Output the short text. 247 // FIXME: Really use a short string. 248 Indent(o, indent) << "<key>message</key>\n"; 249 EmitString(o, P.getString()) << '\n'; 250 251 // Finish up. 252 --indent; 253 Indent(o, indent); o << "</dict>\n"; 254} 255 256static void ReportPiece(raw_ostream &o, 257 const PathDiagnosticPiece &P, 258 const FIDMap& FM, const SourceManager &SM, 259 const LangOptions &LangOpts, 260 unsigned indent, 261 unsigned depth, 262 bool includeControlFlow); 263 264static void ReportCall(raw_ostream &o, 265 const PathDiagnosticCallPiece &P, 266 const FIDMap& FM, const SourceManager &SM, 267 const LangOptions &LangOpts, 268 unsigned indent, 269 unsigned depth) { 270 271 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter = 272 P.getCallEnterEvent(); 273 274 if (callEnter) 275 ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true); 276 277 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnterWithinCaller = 278 P.getCallEnterWithinCallerEvent(); 279 280 ++depth; 281 282 if (callEnterWithinCaller) 283 ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts, 284 indent, depth, true); 285 286 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I) 287 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true); 288 289 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit = 290 P.getCallExitEvent(); 291 292 if (callExit) 293 ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true); 294} 295 296static void ReportMacro(raw_ostream &o, 297 const PathDiagnosticMacroPiece& P, 298 const FIDMap& FM, const SourceManager &SM, 299 const LangOptions &LangOpts, 300 unsigned indent, 301 unsigned depth) { 302 303 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end(); 304 I!=E; ++I) { 305 ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false); 306 } 307} 308 309static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P, 310 const FIDMap& FM, const SourceManager &SM, 311 const LangOptions &LangOpts) { 312 ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true); 313} 314 315static void ReportPiece(raw_ostream &o, 316 const PathDiagnosticPiece &P, 317 const FIDMap& FM, const SourceManager &SM, 318 const LangOptions &LangOpts, 319 unsigned indent, 320 unsigned depth, 321 bool includeControlFlow) { 322 switch (P.getKind()) { 323 case PathDiagnosticPiece::ControlFlow: 324 if (includeControlFlow) 325 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM, 326 LangOpts, indent); 327 break; 328 case PathDiagnosticPiece::Call: 329 ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts, 330 indent, depth); 331 break; 332 case PathDiagnosticPiece::Event: 333 ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts, 334 indent, depth); 335 break; 336 case PathDiagnosticPiece::Macro: 337 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts, 338 indent, depth); 339 break; 340 } 341} 342 343void PlistDiagnostics::FlushDiagnosticsImpl( 344 std::vector<const PathDiagnostic *> &Diags, 345 FilesMade *filesMade) { 346 // Build up a set of FIDs that we use by scanning the locations and 347 // ranges of the diagnostics. 348 FIDMap FM; 349 SmallVector<FileID, 10> Fids; 350 const SourceManager* SM = 0; 351 352 if (!Diags.empty()) 353 SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager(); 354 355 356 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(), 357 DE = Diags.end(); DI != DE; ++DI) { 358 359 const PathDiagnostic *D = *DI; 360 361 llvm::SmallVector<const PathPieces *, 5> WorkList; 362 WorkList.push_back(&D->path); 363 364 while (!WorkList.empty()) { 365 const PathPieces &path = *WorkList.back(); 366 WorkList.pop_back(); 367 368 for (PathPieces::const_iterator I = path.begin(), E = path.end(); 369 I!=E; ++I) { 370 const PathDiagnosticPiece *piece = I->getPtr(); 371 AddFID(FM, Fids, SM, piece->getLocation().asLocation()); 372 373 for (PathDiagnosticPiece::range_iterator RI = piece->ranges_begin(), 374 RE= piece->ranges_end(); RI != RE; ++RI) { 375 AddFID(FM, Fids, SM, RI->getBegin()); 376 AddFID(FM, Fids, SM, RI->getEnd()); 377 } 378 379 if (const PathDiagnosticCallPiece *call = 380 dyn_cast<PathDiagnosticCallPiece>(piece)) { 381 IntrusiveRefCntPtr<PathDiagnosticEventPiece> 382 callEnterWithin = call->getCallEnterWithinCallerEvent(); 383 if (callEnterWithin) 384 AddFID(FM, Fids, SM, callEnterWithin->getLocation().asLocation()); 385 386 WorkList.push_back(&call->path); 387 } 388 else if (const PathDiagnosticMacroPiece *macro = 389 dyn_cast<PathDiagnosticMacroPiece>(piece)) { 390 WorkList.push_back(¯o->subPieces); 391 } 392 } 393 } 394 } 395 396 // Open the file. 397 std::string ErrMsg; 398 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg); 399 if (!ErrMsg.empty()) { 400 llvm::errs() << "warning: could not create file: " << OutputFile << '\n'; 401 return; 402 } 403 404 // Write the plist header. 405 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 406 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " 407 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" 408 "<plist version=\"1.0\">\n"; 409 410 // Write the root object: a <dict> containing... 411 // - "files", an <array> mapping from FIDs to file names 412 // - "diagnostics", an <array> containing the path diagnostics 413 o << "<dict>\n" 414 " <key>files</key>\n" 415 " <array>\n"; 416 417 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); 418 I!=E; ++I) { 419 o << " "; 420 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; 421 } 422 423 o << " </array>\n" 424 " <key>diagnostics</key>\n" 425 " <array>\n"; 426 427 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(), 428 DE = Diags.end(); DI!=DE; ++DI) { 429 430 o << " <dict>\n" 431 " <key>path</key>\n"; 432 433 const PathDiagnostic *D = *DI; 434 435 o << " <array>\n"; 436 437 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); 438 I != E; ++I) 439 ReportDiag(o, **I, FM, *SM, LangOpts); 440 441 o << " </array>\n"; 442 443 // Output the bug type and bug category. 444 o << " <key>description</key>"; 445 EmitString(o, D->getDescription()) << '\n'; 446 o << " <key>category</key>"; 447 EmitString(o, D->getCategory()) << '\n'; 448 o << " <key>type</key>"; 449 EmitString(o, D->getBugType()) << '\n'; 450 451 // Output information about the semantic context where 452 // the issue occurred. 453 if (const Decl *DeclWithIssue = D->getDeclWithIssue()) { 454 // FIXME: handle blocks, which have no name. 455 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) { 456 StringRef declKind; 457 switch (ND->getKind()) { 458 case Decl::CXXRecord: 459 declKind = "C++ class"; 460 break; 461 case Decl::CXXMethod: 462 declKind = "C++ method"; 463 break; 464 case Decl::ObjCMethod: 465 declKind = "Objective-C method"; 466 break; 467 case Decl::Function: 468 declKind = "function"; 469 break; 470 default: 471 break; 472 } 473 if (!declKind.empty()) { 474 const std::string &declName = ND->getDeclName().getAsString(); 475 o << " <key>issue_context_kind</key>"; 476 EmitString(o, declKind) << '\n'; 477 o << " <key>issue_context</key>"; 478 EmitString(o, declName) << '\n'; 479 } 480 481 // Output the bug hash for issue unique-ing. Currently, it's just an 482 // offset from the beginning of the function. 483 if (const Stmt *Body = DeclWithIssue->getBody()) { 484 FullSourceLoc Loc(SM->getExpansionLoc(D->getLocation().asLocation()), 485 *SM); 486 FullSourceLoc FunLoc(SM->getExpansionLoc(Body->getLocStart()), *SM); 487 o << " <key>issue_hash</key><integer>" 488 << Loc.getExpansionLineNumber() - FunLoc.getExpansionLineNumber() 489 << "</integer>\n"; 490 } 491 } 492 } 493 494 // Output the location of the bug. 495 o << " <key>location</key>\n"; 496 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); 497 498 // Output the diagnostic to the sub-diagnostic client, if any. 499 if (!filesMade->empty()) { 500 StringRef lastName; 501 for (FilesMade::iterator I = filesMade->begin(), E = filesMade->end(); 502 I != E; ++I) { 503 StringRef newName = I->first; 504 if (newName != lastName) { 505 if (!lastName.empty()) 506 o << " </array>\n"; 507 lastName = newName; 508 o << " <key>" << lastName << "_files</key>\n"; 509 o << " <array>\n"; 510 } 511 o << " <string>" << I->second << "</string>\n"; 512 } 513 o << " </array>\n"; 514 } 515 516 // Close up the entry. 517 o << " </dict>\n"; 518 } 519 520 o << " </array>\n"; 521 522 // Finish. 523 o << "</dict>\n</plist>"; 524 525 if (filesMade) { 526 StringRef Name(getName()); 527 filesMade->push_back(std::make_pair(Name, OutputFile)); 528 } 529} 530