//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the PlistDiagnostics object. // //===----------------------------------------------------------------------===// #include "clang/Frontend/PathDiagnosticClients.h" #include "clang/Analysis/PathDiagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/Casting.h" #include "llvm/System/Path.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" using namespace clang; using llvm::cast; typedef llvm::DenseMap FIDMap; namespace clang { class Preprocessor; class PreprocessorFactory; } namespace { class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { std::vector BatchedDiags; const std::string OutputFile; public: PlistDiagnostics(const std::string& prefix); ~PlistDiagnostics(); void HandlePathDiagnostic(const PathDiagnostic* D); PathGenerationScheme getGenerationScheme() const { return Extensive; } bool supportsLogicalOpControlFlow() const { return true; } bool supportsAllBlockEdges() const { return true; } }; } // end anonymous namespace PlistDiagnostics::PlistDiagnostics(const std::string& output) : OutputFile(output) {} PathDiagnosticClient* clang::CreatePlistDiagnosticClient(const std::string& s, Preprocessor*, PreprocessorFactory*) { return new PlistDiagnostics(s); } static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl &V, SourceManager* SM, SourceLocation L) { FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); FIDMap::iterator I = FIDs.find(FID); if (I != FIDs.end()) return; FIDs[FID] = V.size(); V.push_back(FID); } static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){ FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); FIDMap::const_iterator I = FIDs.find(FID); assert(I != FIDs.end()); return I->second; } static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { for (unsigned i = 0; i < indent; ++i) o << ' '; return o; } static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM, SourceLocation L, const FIDMap& FM, const unsigned indent) { Indent(o, indent) << "\n"; Indent(o, indent) << " line" << SM->getInstantiationLineNumber(L) << "\n"; Indent(o, indent) << " col" << SM->getInstantiationColumnNumber(L) << "\n"; Indent(o, indent) << " file" << GetFID(FM, SM, L) << "\n"; Indent(o, indent) << "\n"; } static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R, const FIDMap& FM, const unsigned indent) { Indent(o, indent) << "\n"; EmitLocation(o, SM, R.getBegin(), FM, indent+1); EmitLocation(o, SM, R.getEnd(), FM, indent+1); Indent(o, indent) << "\n"; } static llvm::raw_ostream& EmitString(llvm::raw_ostream& o, const std::string& s) { o << ""; for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) { char c = *I; switch (c) { default: o << c; break; case '&': o << "&"; break; case '<': o << "<"; break; case '>': o << ">"; break; case '\'': o << "'"; break; case '\"': o << """; break; } } o << ""; return o; } static void ReportControlFlow(llvm::raw_ostream& o, const PathDiagnosticControlFlowPiece& P, const FIDMap& FM, SourceManager *SM, unsigned indent) { Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "kindcontrol\n"; // FIXME: Eventually remove (DEPRECATED) // Output the start and end locations. Indent(o, indent) << "start\n"; EmitLocation(o, SM, P.getStartLocation(), FM, indent); Indent(o, indent) << "end\n"; EmitLocation(o, SM, P.getEndLocation(), FM, indent); // Emit edges. Indent(o, indent) << "edges\n"; ++indent; Indent(o, indent) << "\n"; ++indent; for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end(); I!=E; ++I) { Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "start\n"; EmitRange(o, SM, I->getStart().asRange(), FM, indent+1); Indent(o, indent) << "end\n"; EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1); --indent; Indent(o, indent) << "\n"; } --indent; Indent(o, indent) << "\n"; --indent; // Output any helper text. const std::string& s = P.getString(); if (!s.empty()) { Indent(o, indent) << "alternate"; EmitString(o, s) << '\n'; } --indent; Indent(o, indent) << "\n"; } static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, const FIDMap& FM, SourceManager* SM, unsigned indent) { Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "kindevent\n"; // Output the location. FullSourceLoc L = P.getLocation(); Indent(o, indent) << "location\n"; EmitLocation(o, SM, L, FM, indent); // Output the ranges (if any). PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), RE = P.ranges_end(); if (RI != RE) { Indent(o, indent) << "ranges\n"; Indent(o, indent) << "\n"; ++indent; for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); --indent; Indent(o, indent) << "\n"; } // Output the text. assert(!P.getString().empty()); Indent(o, indent) << "extended_message\n"; Indent(o, indent); EmitString(o, P.getString()) << '\n'; // Output the short text. // FIXME: Really use a short string. Indent(o, indent) << "message\n"; EmitString(o, P.getString()) << '\n'; // Finish up. --indent; Indent(o, indent); o << "\n"; } static void ReportMacro(llvm::raw_ostream& o, const PathDiagnosticMacroPiece& P, const FIDMap& FM, SourceManager *SM, unsigned indent) { for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); I!=E; ++I) { switch ((*I)->getKind()) { default: break; case PathDiagnosticPiece::Event: ReportEvent(o, cast(**I), FM, SM, indent); break; case PathDiagnosticPiece::Macro: ReportMacro(o, cast(**I), FM, SM, indent); break; } } } static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, const FIDMap& FM, SourceManager* SM) { unsigned indent = 4; switch (P.getKind()) { case PathDiagnosticPiece::ControlFlow: ReportControlFlow(o, cast(P), FM, SM, indent); break; case PathDiagnosticPiece::Event: ReportEvent(o, cast(P), FM, SM, indent); break; case PathDiagnosticPiece::Macro: ReportMacro(o, cast(P), FM, SM, indent); break; } } void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { if (!D) return; if (D->empty()) { delete D; return; } BatchedDiags.push_back(D); } PlistDiagnostics::~PlistDiagnostics() { // Build up a set of FIDs that we use by scanning the locations and // ranges of the diagnostics. FIDMap FM; llvm::SmallVector Fids; SourceManager* SM = 0; if (!BatchedDiags.empty()) SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager(); for (std::vector::iterator DI = BatchedDiags.begin(), DE = BatchedDiags.end(); DI != DE; ++DI) { const PathDiagnostic *D = *DI; for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) { AddFID(FM, Fids, SM, I->getLocation()); for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), RE=I->ranges_end(); RI!=RE; ++RI) { AddFID(FM, Fids, SM, RI->getBegin()); AddFID(FM, Fids, SM, RI->getEnd()); } } } // Open the file. std::string ErrMsg; llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg); if (!ErrMsg.empty()) { llvm::errs() << "warning: could not creat file: " << OutputFile << '\n'; return; } // Write the plist header. o << "\n" "\n" "\n"; // Write the root object: a containing... // - "files", an mapping from FIDs to file names // - "diagnostics", an containing the path diagnostics o << "\n" " files\n" " \n"; for (llvm::SmallVectorImpl::iterator I=Fids.begin(), E=Fids.end(); I!=E; ++I) { o << " "; EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; } o << " \n" " diagnostics\n" " \n"; for (std::vector::iterator DI=BatchedDiags.begin(), DE = BatchedDiags.end(); DI!=DE; ++DI) { o << " \n" " path\n"; const PathDiagnostic *D = *DI; // Create an owning smart pointer for 'D' just so that we auto-free it // when we exit this method. llvm::OwningPtr OwnedD(const_cast(D)); o << " \n"; for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) ReportDiag(o, *I, FM, SM); o << " \n"; // Output the bug type and bug category. o << " description"; EmitString(o, D->getDescription()) << '\n'; o << " category"; EmitString(o, D->getCategory()) << '\n'; o << " type"; EmitString(o, D->getBugType()) << '\n'; // Output the location of the bug. o << " location\n"; EmitLocation(o, SM, D->getLocation(), FM, 2); // Close up the entry. o << " \n"; } o << " \n"; // Finish. o << "\n"; }