PlistDiagnostics.cpp revision 2042fc1f36d471f437023e8899f0c4fadded2341
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 for (std::vector<const PathDiagnostic*>::iterator DI = Diags.begin(), 328 DE = Diags.end(); DI != DE; ++DI) { 329 330 const PathDiagnostic *D = *DI; 331 332 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); 333 I!=E; ++I) { 334 AddFID(FM, Fids, SM, (*I)->getLocation().asLocation()); 335 336 for (PathDiagnosticPiece::range_iterator RI = (*I)->ranges_begin(), 337 RE= (*I)->ranges_end(); RI != RE; ++RI) { 338 AddFID(FM, Fids, SM, RI->getBegin()); 339 AddFID(FM, Fids, SM, RI->getEnd()); 340 } 341 } 342 } 343 344 // Open the file. 345 std::string ErrMsg; 346 llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg); 347 if (!ErrMsg.empty()) { 348 llvm::errs() << "warning: could not create file: " << OutputFile << '\n'; 349 return; 350 } 351 352 // Write the plist header. 353 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 354 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " 355 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" 356 "<plist version=\"1.0\">\n"; 357 358 // Write the root object: a <dict> containing... 359 // - "files", an <array> mapping from FIDs to file names 360 // - "diagnostics", an <array> containing the path diagnostics 361 o << "<dict>\n" 362 " <key>files</key>\n" 363 " <array>\n"; 364 365 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); 366 I!=E; ++I) { 367 o << " "; 368 EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; 369 } 370 371 o << " </array>\n" 372 " <key>diagnostics</key>\n" 373 " <array>\n"; 374 375 for (std::vector<const PathDiagnostic*>::iterator DI=Diags.begin(), 376 DE = Diags.end(); DI!=DE; ++DI) { 377 378 o << " <dict>\n" 379 " <key>path</key>\n"; 380 381 const PathDiagnostic *D = *DI; 382 383 o << " <array>\n"; 384 385 for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); 386 I != E; ++I) 387 ReportDiag(o, **I, FM, *SM, LangOpts); 388 389 o << " </array>\n"; 390 391 // Output the bug type and bug category. 392 o << " <key>description</key>"; 393 EmitString(o, D->getDescription()) << '\n'; 394 o << " <key>category</key>"; 395 EmitString(o, D->getCategory()) << '\n'; 396 o << " <key>type</key>"; 397 EmitString(o, D->getBugType()) << '\n'; 398 399 // Output the location of the bug. 400 o << " <key>location</key>\n"; 401 EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); 402 403 // Output the diagnostic to the sub-diagnostic client, if any. 404 if (SubPD) { 405 std::vector<const PathDiagnostic *> SubDiags; 406 SubDiags.push_back(D); 407 SmallVector<std::string, 1> SubFilesMade; 408 SubPD->FlushDiagnosticsImpl(SubDiags, &SubFilesMade); 409 410 if (!SubFilesMade.empty()) { 411 o << " <key>" << SubPD->getName() << "_files</key>\n"; 412 o << " <array>\n"; 413 for (size_t i = 0, n = SubFilesMade.size(); i < n ; ++i) 414 o << " <string>" << SubFilesMade[i] << "</string>\n"; 415 o << " </array>\n"; 416 } 417 } 418 419 // Close up the entry. 420 o << " </dict>\n"; 421 } 422 423 o << " </array>\n"; 424 425 // Finish. 426 o << "</dict>\n</plist>"; 427 428 if (FilesMade) 429 FilesMade->push_back(OutputFile); 430} 431