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