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