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