//===--- 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/StaticAnalyzer/Core/AnalyzerOptions.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/Version.h" #include "clang/Lex/Preprocessor.h" #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Casting.h" #include "llvm/Support/raw_ostream.h" using namespace clang; using namespace ento; typedef llvm::DenseMap FIDMap; namespace { class PlistDiagnostics : public PathDiagnosticConsumer { const std::string OutputFile; const LangOptions &LangOpts; const bool SupportsCrossFileDiagnostics; public: PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string& prefix, const LangOptions &LangOpts, bool supportsMultipleFiles); virtual ~PlistDiagnostics() {} void FlushDiagnosticsImpl(std::vector &Diags, FilesMade *filesMade); virtual StringRef getName() const { return "PlistDiagnostics"; } PathGenerationScheme getGenerationScheme() const { return Extensive; } bool supportsLogicalOpControlFlow() const { return true; } virtual bool supportsCrossFileDiagnostics() const { return SupportsCrossFileDiagnostics; } }; } // end anonymous namespace PlistDiagnostics::PlistDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string& output, const LangOptions &LO, bool supportsMultipleFiles) : OutputFile(output), LangOpts(LO), SupportsCrossFileDiagnostics(supportsMultipleFiles) {} void ento::createPlistDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, const std::string& s, const Preprocessor &PP) { C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP.getLangOpts(), false)); } void ento::createPlistMultiFileDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, const std::string &s, const Preprocessor &PP) { C.push_back(new PlistDiagnostics(AnalyzerOpts, s, PP.getLangOpts(), true)); } static void AddFID(FIDMap &FIDs, SmallVectorImpl &V, const SourceManager* SM, SourceLocation L) { FileID FID = SM->getFileID(SM->getExpansionLoc(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, const SourceManager &SM, SourceLocation L) { FileID FID = SM.getFileID(SM.getExpansionLoc(L)); FIDMap::const_iterator I = FIDs.find(FID); assert(I != FIDs.end()); return I->second; } static raw_ostream &Indent(raw_ostream &o, const unsigned indent) { for (unsigned i = 0; i < indent; ++i) o << ' '; return o; } static void EmitLocation(raw_ostream &o, const SourceManager &SM, const LangOptions &LangOpts, SourceLocation L, const FIDMap &FM, unsigned indent, bool extend = false) { FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast(SM)); // Add in the length of the token, so that we cover multi-char tokens. unsigned offset = extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; Indent(o, indent) << "\n"; Indent(o, indent) << " line" << Loc.getExpansionLineNumber() << "\n"; Indent(o, indent) << " col" << Loc.getExpansionColumnNumber() + offset << "\n"; Indent(o, indent) << " file" << GetFID(FM, SM, Loc) << "\n"; Indent(o, indent) << "\n"; } static void EmitLocation(raw_ostream &o, const SourceManager &SM, const LangOptions &LangOpts, const PathDiagnosticLocation &L, const FIDMap& FM, unsigned indent, bool extend = false) { EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend); } static void EmitRange(raw_ostream &o, const SourceManager &SM, const LangOptions &LangOpts, PathDiagnosticRange R, const FIDMap &FM, unsigned indent) { Indent(o, indent) << "\n"; EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint); Indent(o, indent) << "\n"; } static raw_ostream &EmitString(raw_ostream &o, StringRef s) { o << ""; for (StringRef::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(raw_ostream &o, const PathDiagnosticControlFlowPiece& P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent) { Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "kindcontrol\n"; // 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; // Make the ranges of the start and end point self-consistent with adjacent edges // by forcing to use only the beginning of the range. This simplifies the layout // logic for clients. Indent(o, indent) << "start\n"; SourceLocation StartEdge = I->getStart().asRange().getBegin(); EmitRange(o, SM, LangOpts, SourceRange(StartEdge, StartEdge), FM, indent+1); Indent(o, indent) << "end\n"; SourceLocation EndEdge = I->getEnd().asRange().getBegin(); EmitRange(o, SM, LangOpts, SourceRange(EndEdge, EndEdge), 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(raw_ostream &o, const PathDiagnosticPiece& P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent, unsigned depth, bool isKeyEvent = false) { Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "kindevent\n"; if (isKeyEvent) { Indent(o, indent) << "key_event\n"; } // Output the location. FullSourceLoc L = P.getLocation().asLocation(); Indent(o, indent) << "location\n"; EmitLocation(o, SM, LangOpts, L, FM, indent); // Output the ranges (if any). ArrayRef Ranges = P.getRanges(); if (!Ranges.empty()) { Indent(o, indent) << "ranges\n"; Indent(o, indent) << "\n"; ++indent; for (ArrayRef::iterator I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { EmitRange(o, SM, LangOpts, *I, FM, indent+1); } --indent; Indent(o, indent) << "\n"; } // Output the call depth. Indent(o, indent) << "depth" << "" << depth << "\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"; Indent(o, indent); EmitString(o, P.getString()) << '\n'; // Finish up. --indent; Indent(o, indent); o << "\n"; } static void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent, unsigned depth, bool includeControlFlow, bool isKeyEvent = false); static void ReportCall(raw_ostream &o, const PathDiagnosticCallPiece &P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent, unsigned depth) { IntrusiveRefCntPtr callEnter = P.getCallEnterEvent(); if (callEnter) ReportPiece(o, *callEnter, FM, SM, LangOpts, indent, depth, true, P.isLastInMainSourceFile()); IntrusiveRefCntPtr callEnterWithinCaller = P.getCallEnterWithinCallerEvent(); ++depth; if (callEnterWithinCaller) ReportPiece(o, *callEnterWithinCaller, FM, SM, LangOpts, indent, depth, true); for (PathPieces::const_iterator I = P.path.begin(), E = P.path.end();I!=E;++I) ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, true); --depth; IntrusiveRefCntPtr callExit = P.getCallExitEvent(); if (callExit) ReportPiece(o, *callExit, FM, SM, LangOpts, indent, depth, true); } static void ReportMacro(raw_ostream &o, const PathDiagnosticMacroPiece& P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent, unsigned depth) { for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end(); I!=E; ++I) { ReportPiece(o, **I, FM, SM, LangOpts, indent, depth, false); } } static void ReportDiag(raw_ostream &o, const PathDiagnosticPiece& P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts) { ReportPiece(o, P, FM, SM, LangOpts, 4, 0, true); } static void ReportPiece(raw_ostream &o, const PathDiagnosticPiece &P, const FIDMap& FM, const SourceManager &SM, const LangOptions &LangOpts, unsigned indent, unsigned depth, bool includeControlFlow, bool isKeyEvent) { switch (P.getKind()) { case PathDiagnosticPiece::ControlFlow: if (includeControlFlow) ReportControlFlow(o, cast(P), FM, SM, LangOpts, indent); break; case PathDiagnosticPiece::Call: ReportCall(o, cast(P), FM, SM, LangOpts, indent, depth); break; case PathDiagnosticPiece::Event: ReportEvent(o, cast(P), FM, SM, LangOpts, indent, depth, isKeyEvent); break; case PathDiagnosticPiece::Macro: ReportMacro(o, cast(P), FM, SM, LangOpts, indent, depth); break; } } void PlistDiagnostics::FlushDiagnosticsImpl( std::vector &Diags, FilesMade *filesMade) { // Build up a set of FIDs that we use by scanning the locations and // ranges of the diagnostics. FIDMap FM; SmallVector Fids; const SourceManager* SM = 0; if (!Diags.empty()) SM = &(*(*Diags.begin())->path.begin())->getLocation().getManager(); for (std::vector::iterator DI = Diags.begin(), DE = Diags.end(); DI != DE; ++DI) { const PathDiagnostic *D = *DI; SmallVector WorkList; WorkList.push_back(&D->path); while (!WorkList.empty()) { const PathPieces &path = *WorkList.pop_back_val(); for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E; ++I) { const PathDiagnosticPiece *piece = I->getPtr(); AddFID(FM, Fids, SM, piece->getLocation().asLocation()); ArrayRef Ranges = piece->getRanges(); for (ArrayRef::iterator I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { AddFID(FM, Fids, SM, I->getBegin()); AddFID(FM, Fids, SM, I->getEnd()); } if (const PathDiagnosticCallPiece *call = dyn_cast(piece)) { IntrusiveRefCntPtr callEnterWithin = call->getCallEnterWithinCallerEvent(); if (callEnterWithin) AddFID(FM, Fids, SM, callEnterWithin->getLocation().asLocation()); WorkList.push_back(&call->path); } else if (const PathDiagnosticMacroPiece *macro = dyn_cast(piece)) { WorkList.push_back(¯o->subPieces); } } } } // Open the file. std::string ErrMsg; llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg); if (!ErrMsg.empty()) { llvm::errs() << "warning: could not create file: " << OutputFile << '\n'; return; } // Write the plist header. o << "\n" "\n" "\n"; // Write the root object: a containing... // - "clang_version", the string representation of clang version // - "files", an mapping from FIDs to file names // - "diagnostics", an containing the path diagnostics o << "\n" << " clang_version\n"; EmitString(o, getClangFullVersion()) << '\n'; o << " files\n" " \n"; for (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=Diags.begin(), DE = Diags.end(); DI!=DE; ++DI) { o << " \n" " path\n"; const PathDiagnostic *D = *DI; o << " \n"; for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); I != E; ++I) ReportDiag(o, **I, FM, *SM, LangOpts); o << " \n"; // Output the bug type and bug category. o << " description"; EmitString(o, D->getShortDescription()) << '\n'; o << " category"; EmitString(o, D->getCategory()) << '\n'; o << " type"; EmitString(o, D->getBugType()) << '\n'; // Output information about the semantic context where // the issue occurred. if (const Decl *DeclWithIssue = D->getDeclWithIssue()) { // FIXME: handle blocks, which have no name. if (const NamedDecl *ND = dyn_cast(DeclWithIssue)) { StringRef declKind; switch (ND->getKind()) { case Decl::CXXRecord: declKind = "C++ class"; break; case Decl::CXXMethod: declKind = "C++ method"; break; case Decl::ObjCMethod: declKind = "Objective-C method"; break; case Decl::Function: declKind = "function"; break; default: break; } if (!declKind.empty()) { const std::string &declName = ND->getDeclName().getAsString(); o << " issue_context_kind"; EmitString(o, declKind) << '\n'; o << " issue_context"; EmitString(o, declName) << '\n'; } // Output the bug hash for issue unique-ing. Currently, it's just an // offset from the beginning of the function. if (const Stmt *Body = DeclWithIssue->getBody()) { // If the bug uniqueing location exists, use it for the hash. // For example, this ensures that two leaks reported on the same line // will have different issue_hashes and that the hash will identify // the leak location even after code is added between the allocation // site and the end of scope (leak report location). PathDiagnosticLocation UPDLoc = D->getUniqueingLoc(); if (UPDLoc.isValid()) { FullSourceLoc UL(SM->getExpansionLoc(UPDLoc.asLocation()), *SM); FullSourceLoc UFunL(SM->getExpansionLoc( D->getUniqueingDecl()->getBody()->getLocStart()), *SM); o << " issue_hash" << UL.getExpansionLineNumber() - UFunL.getExpansionLineNumber() << "\n"; // Otherwise, use the location on which the bug is reported. } else { FullSourceLoc L(SM->getExpansionLoc(D->getLocation().asLocation()), *SM); FullSourceLoc FunL(SM->getExpansionLoc(Body->getLocStart()), *SM); o << " issue_hash" << L.getExpansionLineNumber() - FunL.getExpansionLineNumber() << "\n"; } } } } // Output the location of the bug. o << " location\n"; EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); // Output the diagnostic to the sub-diagnostic client, if any. if (!filesMade->empty()) { StringRef lastName; PDFileEntry::ConsumerFiles *files = filesMade->getFiles(*D); if (files) { for (PDFileEntry::ConsumerFiles::const_iterator CI = files->begin(), CE = files->end(); CI != CE; ++CI) { StringRef newName = CI->first; if (newName != lastName) { if (!lastName.empty()) { o << " \n"; } lastName = newName; o << " " << lastName << "_files\n"; o << " \n"; } o << " " << CI->second << "\n"; } o << " \n"; } } // Close up the entry. o << " \n"; } o << " \n"; // Finish. o << "\n"; }