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 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PathDiagnosticClients.h" |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 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" |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Casting.h" |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 21 | #include "llvm/System/Path.h" |
| 22 | #include "llvm/ADT/DenseMap.h" |
| 23 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 24 | using namespace clang; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 25 | using llvm::cast; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 26 | |
| 27 | typedef llvm::DenseMap<FileID, unsigned> FIDMap; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 29 | namespace clang { |
| 30 | class Preprocessor; |
| 31 | class PreprocessorFactory; |
| 32 | } |
| 33 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 36 | std::vector<const PathDiagnostic*> BatchedDiags; |
| 37 | const std::string OutputFile; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 38 | public: |
| 39 | PlistDiagnostics(const std::string& prefix); |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 40 | ~PlistDiagnostics(); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 41 | void HandlePathDiagnostic(const PathDiagnostic* D); |
| 42 | |
Ted Kremenek | 67f4964 | 2009-04-02 00:44:18 +0000 | [diff] [blame^] | 43 | PathGenerationScheme getGenerationScheme() const { return Extensive; } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 44 | bool supportsLogicalOpControlFlow() const { return true; } |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 45 | bool supportsAllBlockEdges() const { return true; } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 46 | }; |
| 47 | } // end anonymous namespace |
| 48 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 49 | PlistDiagnostics::PlistDiagnostics(const std::string& output) |
| 50 | : OutputFile(output) {} |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 52 | PathDiagnosticClient* |
| 53 | clang::CreatePlistDiagnosticClient(const std::string& s, |
| 54 | Preprocessor*, PreprocessorFactory*) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 55 | return new PlistDiagnostics(s); |
| 56 | } |
| 57 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 58 | static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 59 | const SourceManager* SM, SourceLocation L) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 61 | FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 62 | FIDMap::iterator I = FIDs.find(FID); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 63 | if (I != FIDs.end()) return; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 64 | FIDs[FID] = V.size(); |
| 65 | V.push_back(FID); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 68 | static unsigned GetFID(const FIDMap& FIDs, const SourceManager* SM, |
| 69 | SourceLocation L) { |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 70 | FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 71 | FIDMap::const_iterator I = FIDs.find(FID); |
| 72 | assert(I != FIDs.end()); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 73 | return I->second; |
| 74 | } |
| 75 | |
| 76 | static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 77 | for (unsigned i = 0; i < indent; ++i) o << ' '; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 78 | return o; |
| 79 | } |
| 80 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 81 | static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM, |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 82 | SourceLocation L, const FIDMap& FM, |
| 83 | const unsigned indent) { |
| 84 | |
| 85 | Indent(o, indent) << "<dict>\n"; |
| 86 | Indent(o, indent) << " <key>line</key><integer>" |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 87 | << SM->getInstantiationLineNumber(L) << "</integer>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 88 | Indent(o, indent) << " <key>col</key><integer>" |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 89 | << SM->getInstantiationColumnNumber(L) << "</integer>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 90 | Indent(o, indent) << " <key>file</key><integer>" |
| 91 | << GetFID(FM, SM, L) << "</integer>\n"; |
| 92 | Indent(o, indent) << "</dict>\n"; |
| 93 | } |
| 94 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 95 | static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM, |
| 96 | const PathDiagnosticLocation &L, const FIDMap& FM, |
| 97 | const unsigned indent) { |
| 98 | EmitLocation(o, SM, L.asLocation(), FM, indent); |
| 99 | } |
| 100 | |
| 101 | static void EmitRange(llvm::raw_ostream& o, const SourceManager* SM, |
| 102 | SourceRange R, const FIDMap& FM, |
| 103 | const unsigned indent) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 104 | |
| 105 | Indent(o, indent) << "<array>\n"; |
| 106 | EmitLocation(o, SM, R.getBegin(), FM, indent+1); |
| 107 | EmitLocation(o, SM, R.getEnd(), FM, indent+1); |
| 108 | Indent(o, indent) << "</array>\n"; |
| 109 | } |
| 110 | |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 111 | static llvm::raw_ostream& EmitString(llvm::raw_ostream& o, |
| 112 | const std::string& s) { |
| 113 | o << "<string>"; |
| 114 | for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) { |
| 115 | char c = *I; |
| 116 | switch (c) { |
| 117 | default: o << c; break; |
| 118 | case '&': o << "&"; break; |
| 119 | case '<': o << "<"; break; |
| 120 | case '>': o << ">"; break; |
| 121 | case '\'': o << "'"; break; |
| 122 | case '\"': o << """; break; |
| 123 | } |
| 124 | } |
| 125 | o << "</string>"; |
| 126 | return o; |
| 127 | } |
| 128 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 129 | static void ReportControlFlow(llvm::raw_ostream& o, |
| 130 | const PathDiagnosticControlFlowPiece& P, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 131 | const FIDMap& FM, const SourceManager *SM, |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 132 | unsigned indent) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 134 | Indent(o, indent) << "<dict>\n"; |
| 135 | ++indent; |
| 136 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 137 | Indent(o, indent) << "<key>kind</key><string>control</string>\n"; |
| 138 | |
Ted Kremenek | f48fbc6 | 2009-03-27 15:53:20 +0000 | [diff] [blame] | 139 | // FIXME: Eventually remove (DEPRECATED) |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 140 | // Output the start and end locations. |
| 141 | Indent(o, indent) << "<key>start</key>\n"; |
| 142 | EmitLocation(o, SM, P.getStartLocation(), FM, indent); |
| 143 | Indent(o, indent) << "<key>end</key>\n"; |
| 144 | EmitLocation(o, SM, P.getEndLocation(), FM, indent); |
| 145 | |
Ted Kremenek | f48fbc6 | 2009-03-27 15:53:20 +0000 | [diff] [blame] | 146 | // Emit edges. |
| 147 | Indent(o, indent) << "<key>edges</key>\n"; |
| 148 | ++indent; |
| 149 | Indent(o, indent) << "<array>\n"; |
| 150 | ++indent; |
| 151 | for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end(); |
| 152 | I!=E; ++I) { |
| 153 | Indent(o, indent) << "<dict>\n"; |
| 154 | ++indent; |
| 155 | Indent(o, indent) << "<key>start</key>\n"; |
| 156 | EmitRange(o, SM, I->getStart().asRange(), FM, indent+1); |
| 157 | Indent(o, indent) << "<key>end</key>\n"; |
| 158 | EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1); |
| 159 | --indent; |
| 160 | Indent(o, indent) << "</dict>\n"; |
| 161 | } |
| 162 | --indent; |
| 163 | Indent(o, indent) << "</array>\n"; |
| 164 | --indent; |
| 165 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 166 | // Output any helper text. |
| 167 | const std::string& s = P.getString(); |
| 168 | if (!s.empty()) { |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 169 | Indent(o, indent) << "<key>alternate</key>"; |
| 170 | EmitString(o, s) << '\n'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | --indent; |
| 174 | Indent(o, indent) << "</dict>\n"; |
| 175 | } |
| 176 | |
| 177 | static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 178 | const FIDMap& FM, const SourceManager* SM, |
| 179 | unsigned indent) { |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 180 | |
| 181 | Indent(o, indent) << "<dict>\n"; |
| 182 | ++indent; |
| 183 | |
| 184 | Indent(o, indent) << "<key>kind</key><string>event</string>\n"; |
| 185 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 186 | // Output the location. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 187 | FullSourceLoc L = P.getLocation().asLocation(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 189 | Indent(o, indent) << "<key>location</key>\n"; |
Chris Lattner | 59ddeab | 2009-01-16 23:06:35 +0000 | [diff] [blame] | 190 | EmitLocation(o, SM, L, FM, indent); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 192 | // Output the ranges (if any). |
| 193 | PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 194 | RE = P.ranges_end(); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 195 | |
| 196 | if (RI != RE) { |
| 197 | Indent(o, indent) << "<key>ranges</key>\n"; |
| 198 | Indent(o, indent) << "<array>\n"; |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 199 | ++indent; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 200 | for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 201 | --indent; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 202 | Indent(o, indent) << "</array>\n"; |
| 203 | } |
| 204 | |
| 205 | // Output the text. |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 206 | assert(!P.getString().empty()); |
Ted Kremenek | 61dc71a | 2009-03-19 00:42:56 +0000 | [diff] [blame] | 207 | Indent(o, indent) << "<key>extended_message</key>\n"; |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 208 | Indent(o, indent); |
| 209 | EmitString(o, P.getString()) << '\n'; |
Ted Kremenek | 61dc71a | 2009-03-19 00:42:56 +0000 | [diff] [blame] | 210 | |
| 211 | // Output the short text. |
| 212 | // FIXME: Really use a short string. |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 213 | Indent(o, indent) << "<key>message</key>\n"; |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 214 | EmitString(o, P.getString()) << '\n'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 216 | // Finish up. |
| 217 | --indent; |
| 218 | Indent(o, indent); o << "</dict>\n"; |
| 219 | } |
| 220 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 221 | static void ReportMacro(llvm::raw_ostream& o, |
| 222 | const PathDiagnosticMacroPiece& P, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 223 | const FIDMap& FM, const SourceManager *SM, |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 224 | unsigned indent) { |
| 225 | |
| 226 | for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); |
| 227 | I!=E; ++I) { |
| 228 | |
| 229 | switch ((*I)->getKind()) { |
| 230 | default: |
| 231 | break; |
| 232 | case PathDiagnosticPiece::Event: |
| 233 | ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent); |
| 234 | break; |
| 235 | case PathDiagnosticPiece::Macro: |
| 236 | ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 243 | const FIDMap& FM, const SourceManager* SM) { |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 244 | |
| 245 | unsigned indent = 4; |
| 246 | |
| 247 | switch (P.getKind()) { |
| 248 | case PathDiagnosticPiece::ControlFlow: |
| 249 | ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM, |
| 250 | indent); |
| 251 | break; |
| 252 | case PathDiagnosticPiece::Event: |
| 253 | ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent); |
| 254 | break; |
| 255 | case PathDiagnosticPiece::Macro: |
| 256 | ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 261 | void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 262 | if (!D) |
| 263 | return; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 265 | if (D->empty()) { |
| 266 | delete D; |
| 267 | return; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 270 | BatchedDiags.push_back(D); |
| 271 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 272 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 273 | PlistDiagnostics::~PlistDiagnostics() { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 274 | |
| 275 | // Build up a set of FIDs that we use by scanning the locations and |
| 276 | // ranges of the diagnostics. |
| 277 | FIDMap FM; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 278 | llvm::SmallVector<FileID, 10> Fids; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 279 | const SourceManager* SM = 0; |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 280 | |
| 281 | if (!BatchedDiags.empty()) |
| 282 | SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager(); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 284 | for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(), |
| 285 | DE = BatchedDiags.end(); DI != DE; ++DI) { |
| 286 | |
| 287 | const PathDiagnostic *D = *DI; |
| 288 | |
| 289 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 290 | AddFID(FM, Fids, SM, I->getLocation().asLocation()); |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 291 | |
| 292 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 293 | RE=I->ranges_end(); RI!=RE; ++RI) { |
| 294 | AddFID(FM, Fids, SM, RI->getBegin()); |
| 295 | AddFID(FM, Fids, SM, RI->getEnd()); |
| 296 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 300 | // Open the file. |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 301 | std::string ErrMsg; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 302 | llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 303 | if (!ErrMsg.empty()) { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 304 | llvm::errs() << "warning: could not creat file: " << OutputFile << '\n'; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
| 308 | // Write the plist header. |
| 309 | o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 310 | "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " |
| 311 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" |
| 312 | "<plist version=\"1.0\">\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 313 | |
| 314 | // Write the root object: a <dict> containing... |
| 315 | // - "files", an <array> mapping from FIDs to file names |
| 316 | // - "diagnostics", an <array> containing the path diagnostics |
| 317 | o << "<dict>\n" |
| 318 | " <key>files</key>\n" |
| 319 | " <array>\n"; |
| 320 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 321 | for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 322 | I!=E; ++I) { |
| 323 | o << " "; |
| 324 | EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; |
| 325 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 326 | |
| 327 | o << " </array>\n" |
| 328 | " <key>diagnostics</key>\n" |
| 329 | " <array>\n"; |
| 330 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 331 | for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(), |
| 332 | DE = BatchedDiags.end(); DI!=DE; ++DI) { |
| 333 | |
| 334 | o << " <dict>\n" |
| 335 | " <key>path</key>\n"; |
| 336 | |
| 337 | const PathDiagnostic *D = *DI; |
| 338 | // Create an owning smart pointer for 'D' just so that we auto-free it |
| 339 | // when we exit this method. |
| 340 | llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D)); |
| 341 | |
| 342 | o << " <array>\n"; |
| 343 | |
| 344 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) |
| 345 | ReportDiag(o, *I, FM, SM); |
| 346 | |
| 347 | o << " </array>\n"; |
| 348 | |
| 349 | // Output the bug type and bug category. |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 350 | o << " <key>description</key>"; |
| 351 | EmitString(o, D->getDescription()) << '\n'; |
| 352 | o << " <key>category</key>"; |
| 353 | EmitString(o, D->getCategory()) << '\n'; |
| 354 | o << " <key>type</key>"; |
| 355 | EmitString(o, D->getBugType()) << '\n'; |
Ted Kremenek | ca1bada | 2009-03-27 15:31:11 +0000 | [diff] [blame] | 356 | |
| 357 | // Output the location of the bug. |
| 358 | o << " <key>location</key>\n"; |
| 359 | EmitLocation(o, SM, D->getLocation(), FM, 2); |
| 360 | |
Ted Kremenek | b0b6f72 | 2009-03-28 06:40:54 +0000 | [diff] [blame] | 361 | // Close up the entry. |
Ted Kremenek | ca1bada | 2009-03-27 15:31:11 +0000 | [diff] [blame] | 362 | o << " </dict>\n"; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 363 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 364 | |
| 365 | o << " </array>\n"; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 367 | // Finish. |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 368 | o << "</dict>\n</plist>"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 369 | } |