PlistDiagnostics.cpp revision 62ff52868976a8494224a2914f1869329777944c
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 OwningPtr<PathDiagnosticConsumer> SubPD; 34 bool flushed; 35 public: 36 PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts, 37 PathDiagnosticConsumer *subPD); 38 39 virtual ~PlistDiagnostics() {} 40 41 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 42 SmallVectorImpl<std::string> *FilesMade); 43 44 virtual StringRef getName() const { 45 return "PlistDiagnostics"; 46 } 47 48 PathGenerationScheme getGenerationScheme() const; 49 bool supportsLogicalOpControlFlow() const { return true; } 50 bool supportsAllBlockEdges() const { return true; } 51 virtual bool useVerboseDescription() const { return false; } 52 }; 53} // end anonymous namespace 54 55PlistDiagnostics::PlistDiagnostics(const std::string& output, 56 const LangOptions &LO, 57 PathDiagnosticConsumer *subPD) 58 : OutputFile(output), LangOpts(LO), SubPD(subPD), flushed(false) {} 59 60PathDiagnosticConsumer* 61ento::createPlistDiagnosticConsumer(const std::string& s, const Preprocessor &PP, 62 PathDiagnosticConsumer *subPD) { 63 return new PlistDiagnostics(s, PP.getLangOptions(), subPD); 64} 65 66PathDiagnosticConsumer::PathGenerationScheme 67PlistDiagnostics::getGenerationScheme() const { 68 if (const PathDiagnosticConsumer *PD = SubPD.get()) 69 return PD->getGenerationScheme(); 70 71 return Extensive; 72} 73 74static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V, 75 const SourceManager* SM, SourceLocation L) { 76 77 FileID FID = SM->getFileID(SM->getExpansionLoc(L)); 78 FIDMap::iterator I = FIDs.find(FID); 79 if (I != FIDs.end()) return; 80 FIDs[FID] = V.size(); 81 V.push_back(FID); 82} 83 84static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, 85 SourceLocation L) { 86 FileID FID = SM.getFileID(SM.getExpansionLoc(L)); 87 FIDMap::const_iterator I = FIDs.find(FID); 88 assert(I != FIDs.end()); 89 return I->second; 90} 91 92static raw_ostream &Indent(raw_ostream &o, const unsigned indent) { 93 for (unsigned i = 0; i < indent; ++i) o << ' '; 94 return o; 95} 96 97static void EmitLocation(raw_ostream &o, const SourceManager &SM, 98 const LangOptions &LangOpts, 99 SourceLocation L, const FIDMap &FM, 100 unsigned indent, bool extend = false) { 101 102 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM)); 103 104 // Add in the length of the token, so that we cover multi-char tokens. 105 unsigned offset = 106 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; 107 108 Indent(o, indent) << "<dict>\n"; 109 Indent(o, indent) << " <key>line</key><integer>" 110 << Loc.getExpansionLineNumber() << "</integer>\n"; 111 Indent(o, indent) << " <key>col</key><integer>" 112 << Loc.getExpansionColumnNumber() + offset << "</integer>\n"; 113 Indent(o, indent) << " <key>file</key><integer>" 114 << GetFID(FM, SM, Loc) << "</integer>\n"; 115 Indent(o, indent) << "</dict>\n"; 116} 117 118static void EmitLocation(raw_ostream &o, const SourceManager &SM, 119 const LangOptions &LangOpts, 120 const PathDiagnosticLocation &L, const FIDMap& FM, 121 unsigned indent, bool extend = false) { 122 EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend); 123} 124 125static void EmitRange(raw_ostream &o, const SourceManager &SM, 126 const LangOptions &LangOpts, 127 PathDiagnosticRange R, const FIDMap &FM, 128 unsigned indent) { 129 Indent(o, indent) << "<array>\n"; 130 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); 131 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint); 132 Indent(o, indent) << "</array>\n"; 133} 134 135static raw_ostream &EmitString(raw_ostream &o, 136 const std::string& s) { 137 o << "<string>"; 138 for (std::string::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 Indent(o, indent) << "<key>start</key>\n"; 175 EmitRange(o, SM, LangOpts, I->getStart().asRange(), FM, indent+1); 176 Indent(o, indent) << "<key>end</key>\n"; 177 EmitRange(o, SM, LangOpts, I->getEnd().asRange(), FM, indent+1); 178 --indent; 179 Indent(o, indent) << "</dict>\n"; 180 } 181 --indent; 182 Indent(o, indent) << "</array>\n"; 183 --indent; 184 185 // Output any helper text. 186 const std::string& s = P.getString(); 187 if (!s.empty()) { 188 Indent(o, indent) << "<key>alternate</key>"; 189 EmitString(o, s) << '\n'; 190 } 191 192 --indent; 193 Indent(o, indent) << "</dict>\n"; 194} 195 196static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P, 197 const FIDMap& FM, 198 const SourceManager &SM, 199 const LangOptions &LangOpts, 200 unsigned indent) { 201 202 Indent(o, indent) << "<dict>\n"; 203 ++indent; 204 205 Indent(o, indent) << "<key>kind</key><string>event</string>\n"; 206 207 // Output the location. 208 FullSourceLoc L = P.getLocation().asLocation(); 209 210 Indent(o, indent) << "<key>location</key>\n"; 211 EmitLocation(o, SM, LangOpts, L, FM, indent); 212 213 // Output the ranges (if any). 214 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), 215 RE = P.ranges_end(); 216 217 if (RI != RE) { 218 Indent(o, indent) << "<key>ranges</key>\n"; 219 Indent(o, indent) << "<array>\n"; 220 ++indent; 221 for (; RI != RE; ++RI) 222 EmitRange(o, SM, LangOpts, *RI, FM, indent+1); 223 --indent; 224 Indent(o, indent) << "</array>\n"; 225 } 226 227 // Output the text. 228 assert(!P.getString().empty()); 229 Indent(o, indent) << "<key>extended_message</key>\n"; 230 Indent(o, indent); 231 EmitString(o, P.getString()) << '\n'; 232 233 // Output the short text. 234 // FIXME: Really use a short string. 235 Indent(o, indent) << "<key>message</key>\n"; 236 EmitString(o, P.getString()) << '\n'; 237 238 // Finish up. 239 --indent; 240 Indent(o, indent); o << "</dict>\n"; 241} 242 243static void ReportPiece(raw_ostream &o, 244 const PathDiagnosticPiece &P, 245 const FIDMap& FM, const SourceManager &SM, 246 const LangOptions &LangOpts, 247 unsigned indent, 248 bool includeControlFlow); 249 250static void ReportCall(raw_ostream &o, 251 const PathDiagnosticCallPiece &P, 252 const FIDMap& FM, const SourceManager &SM, 253 const LangOptions &LangOpts, 254 unsigned indent) { 255 256 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter = 257 P.getCallEnterEvent(); 258 if (callEnter) 259 ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, true); 260 261 for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I) 262 ReportPiece(o, **I, FM, SM, LangOpts, indent, true); 263 264 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit = 265 P.getCallExitEvent(); 266 if (callExit) 267 ReportPiece(o, *callExit, FM, SM, LangOpts, indent, true); 268} 269 270static void ReportMacro(raw_ostream &o, 271 const PathDiagnosticMacroPiece& P, 272 const FIDMap& FM, const SourceManager &SM, 273 const LangOptions &LangOpts, 274 unsigned indent) { 275 276 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end(); 277 I!=E; ++I) { 278 ReportPiece(o, **I, FM, SM, LangOpts, indent, false); 279 } 280} 281 282static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P, 283 const FIDMap& FM, const SourceManager &SM, 284 const LangOptions &LangOpts) { 285 ReportPiece(o, P, FM, SM, LangOpts, 4, true); 286} 287 288static void ReportPiece(raw_ostream &o, 289 const PathDiagnosticPiece &P, 290 const FIDMap& FM, const SourceManager &SM, 291 const LangOptions &LangOpts, 292 unsigned indent, 293 bool includeControlFlow) { 294 switch (P.getKind()) { 295 case PathDiagnosticPiece::ControlFlow: 296 if (includeControlFlow) 297 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM, 298 LangOpts, indent); 299 break; 300 case PathDiagnosticPiece::Call: 301 ReportCall(o, cast<PathDiagnosticCallPiece>(P), FM, SM, LangOpts, 302 indent); 303 break; 304 case PathDiagnosticPiece::Event: 305 ReportEvent(o, cast<PathDiagnosticSpotPiece>(P), FM, SM, LangOpts, 306 indent); 307 break; 308 case PathDiagnosticPiece::Macro: 309 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts, 310 indent); 311 break; 312 } 313} 314 315void PlistDiagnostics::FlushDiagnosticsImpl( 316 std::vector<const PathDiagnostic *> &Diags, 317 SmallVectorImpl<std::string> *FilesMade) { 318 // Build up a set of FIDs that we use by scanning the locations and 319 // ranges of the diagnostics. 320 FIDMap FM; 321 SmallVector<FileID, 10> Fids; 322 const SourceManager* SM = 0; 323 324 if (!Diags.empty()) 325 SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager(); 326 327 328 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(), 329 DE = Diags.end(); DI != DE; ++DI) { 330 331 const PathDiagnostic *D = *DI; 332 333 llvm::SmallVector<const PathPieces *, 5> WorkList; 334 WorkList.push_back(&D->path); 335 336 while (!WorkList.empty()) { 337 const PathPieces &path = *WorkList.back(); 338 WorkList.pop_back(); 339 340 for (PathPieces::const_iterator I = path.begin(), E = path.end(); 341 I!=E; ++I) { 342 const PathDiagnosticPiece *piece = I->getPtr(); 343 AddFID(FM, Fids, SM, piece->getLocation().asLocation()); 344 345 for (PathDiagnosticPiece::range_iterator RI = piece->ranges_begin(), 346 RE= piece->ranges_end(); RI != RE; ++RI) { 347 AddFID(FM, Fids, SM, RI->getBegin()); 348 AddFID(FM, Fids, SM, RI->getEnd()); 349 } 350 351 if (const PathDiagnosticCallPiece *call = 352 dyn_cast<PathDiagnosticCallPiece>(piece)) { 353 WorkList.push_back(&call->path); 354 } 355 else if (const PathDiagnosticMacroPiece *macro = 356 dyn_cast<PathDiagnosticMacroPiece>(piece)) { 357 WorkList.push_back(¯o->subPieces); 358 } 359 } 360 } 361 } 362 363 // Open the file. 364 std::string ErrMsg; 365 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg); 366 if (!ErrMsg.empty()) { 367 llvm::errs() << "warning: could not create file: " << OutputFile << '\n'; 368 return; 369 } 370 371 // Write the plist header. 372 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 373 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " 374 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" 375 "<plist version=\"1.0\">\n"; 376 377 // Write the root object: a <dict> containing... 378 // - "files", an <array> mapping from FIDs to file names 379 // - "diagnostics", an <array> containing the path diagnostics 380 o << "<dict>\n" 381 " <key>files</key>\n" 382 " <array>\n"; 383 384 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); 385 I!=E; ++I) { 386 o << " "; 387 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; 388 } 389 390 o << " </array>\n" 391 " <key>diagnostics</key>\n" 392 " <array>\n"; 393 394 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(), 395 DE = Diags.end(); DI!=DE; ++DI) { 396 397 o << " <dict>\n" 398 " <key>path</key>\n"; 399 400 const PathDiagnostic *D = *DI; 401 402 o << " <array>\n"; 403 404 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); 405 I != E; ++I) 406 ReportDiag(o, **I, FM, *SM, LangOpts); 407 408 o << " </array>\n"; 409 410 // Output the bug type and bug category. 411 o << " <key>description</key>"; 412 EmitString(o, D->getDescription()) << '\n'; 413 o << " <key>category</key>"; 414 EmitString(o, D->getCategory()) << '\n'; 415 o << " <key>type</key>"; 416 EmitString(o, D->getBugType()) << '\n'; 417 418 // Output the location of the bug. 419 o << " <key>location</key>\n"; 420 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); 421 422 // Output the diagnostic to the sub-diagnostic client, if any. 423 if (SubPD) { 424 std::vector<const PathDiagnostic *> SubDiags; 425 SubDiags.push_back(D); 426 SmallVector<std::string, 1> SubFilesMade; 427 SubPD->FlushDiagnosticsImpl(SubDiags, &SubFilesMade); 428 429 if (!SubFilesMade.empty()) { 430 o << " <key>" << SubPD->getName() << "_files</key>\n"; 431 o << " <array>\n"; 432 for (size_t i = 0, n = SubFilesMade.size(); i < n ; ++i) 433 o << " <string>" << SubFilesMade[i] << "</string>\n"; 434 o << " </array>\n"; 435 } 436 } 437 438 // Close up the entry. 439 o << " </dict>\n"; 440 } 441 442 o << " </array>\n"; 443 444 // Finish. 445 o << "</dict>\n</plist>"; 446 447 if (FilesMade) 448 FilesMade->push_back(OutputFile); 449} 450