Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame^] | 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/Driver/PathDiagnosticClients.h" |
| 15 | #include "clang/Analysis/PathDiagnostic.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include "llvm/System/Path.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | typedef llvm::DenseMap<unsigned,unsigned> FIDMap; |
| 26 | |
| 27 | namespace { |
| 28 | class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { |
| 29 | llvm::sys::Path Directory, FilePrefix; |
| 30 | bool createdDir, noDir; |
| 31 | public: |
| 32 | PlistDiagnostics(const std::string& prefix); |
| 33 | ~PlistDiagnostics() {} |
| 34 | void HandlePathDiagnostic(const PathDiagnostic* D); |
| 35 | }; |
| 36 | } // end anonymous namespace |
| 37 | |
| 38 | PlistDiagnostics::PlistDiagnostics(const std::string& prefix) |
| 39 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) { |
| 40 | FilePrefix.appendComponent("report"); // All Plist files begin with "report" |
| 41 | } |
| 42 | |
| 43 | PathDiagnosticClient* clang::CreatePlistDiagnosticClient(const std::string& s) { |
| 44 | return new PlistDiagnostics(s); |
| 45 | } |
| 46 | |
| 47 | static void AddFID(FIDMap& FIDs, |
| 48 | llvm::SmallVectorImpl<unsigned>& V, |
| 49 | SourceManager& SM, SourceLocation L) { |
| 50 | |
| 51 | unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(L)); |
| 52 | FIDMap::iterator I = FIDs.find(fid); |
| 53 | if (I != FIDs.end()) return; |
| 54 | FIDs[fid] = V.size(); |
| 55 | V.push_back(fid); |
| 56 | } |
| 57 | |
| 58 | static unsigned GetFID(const FIDMap& FIDs, |
| 59 | SourceManager& SM, SourceLocation L) { |
| 60 | |
| 61 | unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(L)); |
| 62 | FIDMap::const_iterator I = FIDs.find(fid); |
| 63 | assert (I != FIDs.end()); |
| 64 | return I->second; |
| 65 | } |
| 66 | |
| 67 | static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { |
| 68 | for (unsigned i = 0; i < indent; ++i) o << ' '; |
| 69 | return o; |
| 70 | } |
| 71 | |
| 72 | static void EmitLocation(llvm::raw_ostream& o, SourceManager& SM, |
| 73 | SourceLocation L, const FIDMap& FM, |
| 74 | const unsigned indent) { |
| 75 | |
| 76 | Indent(o, indent) << "<dict>\n"; |
| 77 | Indent(o, indent) << " <key>line</key><integer>" |
| 78 | << SM.getLogicalLineNumber(L) << "</integer>\n"; |
| 79 | Indent(o, indent) << " <key>col</key><integer>" |
| 80 | << SM.getLogicalColumnNumber(L) << "</integer>\n"; |
| 81 | Indent(o, indent) << " <key>file</key><integer>" |
| 82 | << GetFID(FM, SM, L) << "</integer>\n"; |
| 83 | Indent(o, indent) << "</dict>\n"; |
| 84 | } |
| 85 | |
| 86 | static void EmitRange(llvm::raw_ostream& o, SourceManager& SM, SourceRange R, |
| 87 | const FIDMap& FM, const unsigned indent) { |
| 88 | |
| 89 | Indent(o, indent) << "<array>\n"; |
| 90 | EmitLocation(o, SM, R.getBegin(), FM, indent+1); |
| 91 | EmitLocation(o, SM, R.getEnd(), FM, indent+1); |
| 92 | Indent(o, indent) << "</array>\n"; |
| 93 | } |
| 94 | |
| 95 | static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, |
| 96 | const FIDMap& FM, SourceManager& SM) { |
| 97 | |
| 98 | unsigned indent = 2; |
| 99 | Indent(o, indent) << "<dict>\n"; |
| 100 | ++indent; |
| 101 | |
| 102 | // Output the location. |
| 103 | FullSourceLoc L = P.getLocation(); |
| 104 | |
| 105 | Indent(o, indent) << "<key>location</key>\n"; |
| 106 | EmitLocation(o, SM, L.getLocation(), FM, indent); |
| 107 | |
| 108 | // Output the ranges (if any). |
| 109 | PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), |
| 110 | RE = P.ranges_end(); |
| 111 | |
| 112 | if (RI != RE) { |
| 113 | Indent(o, indent) << "<key>ranges</key>\n"; |
| 114 | Indent(o, indent) << "<array>\n"; |
| 115 | for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); |
| 116 | Indent(o, indent) << "</array>\n"; |
| 117 | } |
| 118 | |
| 119 | // Output the text. |
| 120 | Indent(o, indent) << "<key>message</key>\n"; |
| 121 | Indent(o, indent) << "<string>" << P.getString() << "</string>"; |
| 122 | |
| 123 | // Output the hint. |
| 124 | Indent(o, indent) << "<key>displayhint</key>\n"; |
| 125 | Indent(o, indent) << "<string>" |
| 126 | << (P.getDisplayHint() == PathDiagnosticPiece::Above |
| 127 | ? "above" : "below") |
| 128 | << "</string>\n"; |
| 129 | |
| 130 | |
| 131 | // Finish up. |
| 132 | --indent; |
| 133 | Indent(o, indent); o << "</dict>\n"; |
| 134 | } |
| 135 | |
| 136 | void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
| 137 | |
| 138 | // Create an owning smart pointer for 'D' just so that we auto-free it |
| 139 | // when we exit this method. |
| 140 | llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D)); |
| 141 | |
| 142 | // Create the directory to contain the plist files if it is missing. |
| 143 | if (!createdDir) { |
| 144 | createdDir = true; |
| 145 | std::string ErrorMsg; |
| 146 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
| 147 | |
| 148 | if (!Directory.isDirectory()) { |
| 149 | llvm::errs() << "warning: could not create directory '" |
| 150 | << Directory.toString() << "'\n" |
| 151 | << "reason: " << ErrorMsg << '\n'; |
| 152 | |
| 153 | noDir = true; |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (noDir) |
| 160 | return; |
| 161 | |
| 162 | // Get the source manager. |
| 163 | SourceManager& SM = D->begin()->getLocation().getManager(); |
| 164 | |
| 165 | // Build up a set of FIDs that we use by scanning the locations and |
| 166 | // ranges of the diagnostics. |
| 167 | FIDMap FM; |
| 168 | llvm::SmallVector<unsigned, 10> Fids; |
| 169 | |
| 170 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) { |
| 171 | AddFID(FM, Fids, SM, I->getLocation().getLocation()); |
| 172 | |
| 173 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 174 | RE=I->ranges_end(); RI!=RE; ++RI) { |
| 175 | AddFID(FM, Fids, SM, RI->getBegin()); |
| 176 | AddFID(FM, Fids, SM, RI->getEnd()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Create a path for the target Plist file. |
| 181 | llvm::sys::Path F(FilePrefix); |
| 182 | F.makeUnique(false, NULL); |
| 183 | |
| 184 | // Rename the file with an Plist extension. |
| 185 | llvm::sys::Path H(F); |
| 186 | H.appendSuffix("plist"); |
| 187 | F.renamePathOnDisk(H, NULL); |
| 188 | |
| 189 | // Now create the plist file. |
| 190 | std::string ErrMsg; |
| 191 | llvm::raw_fd_ostream o(H.toString().c_str(), ErrMsg); |
| 192 | |
| 193 | if (!ErrMsg.empty()) { |
| 194 | llvm::errs() << "warning: could not creat file: " << H.toString() << '\n'; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // Write the plist header. |
| 199 | o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 200 | "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " |
| 201 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" |
| 202 | "<plist version=\"1.0\">\n"; |
| 203 | |
| 204 | // Write the root object: a <dict> containing... |
| 205 | // - "files", an <array> mapping from FIDs to file names |
| 206 | // - "diagnostics", an <array> containing the path diagnostics |
| 207 | o << "<dict>\n" |
| 208 | " <key>files</key>\n" |
| 209 | " <array>\n"; |
| 210 | |
| 211 | for (llvm::SmallVectorImpl<unsigned>::iterator I=Fids.begin(), E=Fids.end(); |
| 212 | I!=E; ++I) |
| 213 | o << " <string>" << SM.getFileEntryForID(*I)->getName() << "</string>\n"; |
| 214 | |
| 215 | o << " </array>\n" |
| 216 | " <key>diagnostics</key>\n" |
| 217 | " <array>\n"; |
| 218 | |
| 219 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) |
| 220 | ReportDiag(o, *I, FM, SM); |
| 221 | |
| 222 | o << " </array>\n"; |
| 223 | |
| 224 | // Output the bug type and bug category. |
| 225 | o << " <key>description</key><string>" << D->getDescription() << "</string>\n" |
| 226 | " <key>category</key><string>" << D->getCategory() << "</string>\n"; |
| 227 | |
| 228 | // Finish. |
| 229 | o << "</dict>\n"; |
| 230 | } |