From: Ted Kremenek Date: Mon, 3 Nov 2008 22:33:57 +0000 (+0000) Subject: Add path diagnostics client for emitting path reports using Plists. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d866256333fb6115a8a86ac4f89b2ca36e20c70;p=clang Add path diagnostics client for emitting path reports using Plists. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58647 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Driver/PathDiagnosticClients.h b/include/clang/Driver/PathDiagnosticClients.h index be72c49fd2..78457bd76d 100644 --- a/include/clang/Driver/PathDiagnosticClients.h +++ b/include/clang/Driver/PathDiagnosticClients.h @@ -1,4 +1,4 @@ -//===--- HTMLPathDiagnostic.h - HTML Diagnostics for Paths ------*- C++ -*-===// +//===--- PathDiagnosticClients.h - Path Diagnostic Clients ------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,12 +7,12 @@ // //===----------------------------------------------------------------------===// // -// This file defines the interface to create a HTMLPathDiagnostic object. +// This file defines the interface to create different path diagostic clients. // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_PATH_HTML_DIAGNOSTIC_H -#define LLVM_CLANG_PATH_HTML_DIAGNOSTIC_H +#ifndef LLVM_CLANG_PATH_DIAGNOSTIC_CLIENTS_H +#define LLVM_CLANG_PATH_DIAGNOSTIC_CLiENTS_H #include @@ -22,10 +22,11 @@ class PathDiagnosticClient; class Preprocessor; class PreprocessorFactory; - PathDiagnosticClient* CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, PreprocessorFactory* PPF); + +PathDiagnosticClient* CreatePlistDiagnosticClient(const std::string& prefix); } #endif diff --git a/lib/Driver/PlistDiagnostics.cpp b/lib/Driver/PlistDiagnostics.cpp new file mode 100644 index 0000000000..fe8022c7c1 --- /dev/null +++ b/lib/Driver/PlistDiagnostics.cpp @@ -0,0 +1,230 @@ +//===--- 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/Driver/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/System/Path.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" + +using namespace clang; +typedef llvm::DenseMap FIDMap; + +namespace { + class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { + llvm::sys::Path Directory, FilePrefix; + bool createdDir, noDir; + public: + PlistDiagnostics(const std::string& prefix); + ~PlistDiagnostics() {} + void HandlePathDiagnostic(const PathDiagnostic* D); + }; +} // end anonymous namespace + +PlistDiagnostics::PlistDiagnostics(const std::string& prefix) + : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) { + FilePrefix.appendComponent("report"); // All Plist files begin with "report" +} + +PathDiagnosticClient* clang::CreatePlistDiagnosticClient(const std::string& s) { + return new PlistDiagnostics(s); +} + +static void AddFID(FIDMap& FIDs, + llvm::SmallVectorImpl& V, + SourceManager& SM, SourceLocation L) { + + unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(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) { + + unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(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.getLogicalLineNumber(L) << "\n"; + Indent(o, indent) << " col" + << SM.getLogicalColumnNumber(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 void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, + const FIDMap& FM, SourceManager& SM) { + + unsigned indent = 2; + Indent(o, indent) << "\n"; + ++indent; + + // Output the location. + FullSourceLoc L = P.getLocation(); + + Indent(o, indent) << "location\n"; + EmitLocation(o, SM, L.getLocation(), 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"; + for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); + Indent(o, indent) << "\n"; + } + + // Output the text. + Indent(o, indent) << "message\n"; + Indent(o, indent) << "" << P.getString() << ""; + + // Output the hint. + Indent(o, indent) << "displayhint\n"; + Indent(o, indent) << "" + << (P.getDisplayHint() == PathDiagnosticPiece::Above + ? "above" : "below") + << "\n"; + + + // Finish up. + --indent; + Indent(o, indent); o << "\n"; +} + +void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { + + // 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)); + + // Create the directory to contain the plist files if it is missing. + if (!createdDir) { + createdDir = true; + std::string ErrorMsg; + Directory.createDirectoryOnDisk(true, &ErrorMsg); + + if (!Directory.isDirectory()) { + llvm::errs() << "warning: could not create directory '" + << Directory.toString() << "'\n" + << "reason: " << ErrorMsg << '\n'; + + noDir = true; + + return; + } + } + + if (noDir) + return; + + // Get the source manager. + SourceManager& SM = D->begin()->getLocation().getManager(); + + // Build up a set of FIDs that we use by scanning the locations and + // ranges of the diagnostics. + FIDMap FM; + llvm::SmallVector Fids; + + for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) { + AddFID(FM, Fids, SM, I->getLocation().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()); + } + } + + // Create a path for the target Plist file. + llvm::sys::Path F(FilePrefix); + F.makeUnique(false, NULL); + + // Rename the file with an Plist extension. + llvm::sys::Path H(F); + H.appendSuffix("plist"); + F.renamePathOnDisk(H, NULL); + + // Now create the plist file. + std::string ErrMsg; + llvm::raw_fd_ostream o(H.toString().c_str(), ErrMsg); + + if (!ErrMsg.empty()) { + llvm::errs() << "warning: could not creat file: " << H.toString() << '\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 << " " << SM.getFileEntryForID(*I)->getName() << "\n"; + + o << " \n" + " diagnostics\n" + " \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" << D->getDescription() << "\n" + " category" << D->getCategory() << "\n"; + + // Finish. + o << "\n"; +}