blob: 757b1ee13843541bcfd9deebe557eccf0a5055b9 [file] [log] [blame]
Ted Kremenek5d866252008-11-03 22:33:57 +00001//===--- 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 Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/PathDiagnosticClients.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000015#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 Kremenek082cb8d2009-03-12 18:41:53 +000020#include "llvm/Support/Casting.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000021#include "llvm/System/Path.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/SmallVector.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000024using namespace clang;
Ted Kremenek082cb8d2009-03-12 18:41:53 +000025using llvm::cast;
Chris Lattner2b2453a2009-01-17 06:22:33 +000026
27typedef llvm::DenseMap<FileID, unsigned> FIDMap;
Ted Kremenek5d866252008-11-03 22:33:57 +000028
Ted Kremenek4fc82c82008-11-03 23:18:07 +000029namespace clang {
30 class Preprocessor;
31 class PreprocessorFactory;
32}
33
Ted Kremenek5d866252008-11-03 22:33:57 +000034namespace {
35 class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient {
Ted Kremenekddf32da2009-01-21 00:42:24 +000036 std::vector<const PathDiagnostic*> BatchedDiags;
37 const std::string OutputFile;
Ted Kremenek5d866252008-11-03 22:33:57 +000038 public:
39 PlistDiagnostics(const std::string& prefix);
Ted Kremenekddf32da2009-01-21 00:42:24 +000040 ~PlistDiagnostics();
Ted Kremenekbabdd7b2009-03-27 05:06:10 +000041 void HandlePathDiagnostic(const PathDiagnostic* D);
42
43 bool supportsLogicalOpControlFlow() const { return true; }
Ted Kremenek5d866252008-11-03 22:33:57 +000044 };
45} // end anonymous namespace
46
Ted Kremenekddf32da2009-01-21 00:42:24 +000047PlistDiagnostics::PlistDiagnostics(const std::string& output)
48 : OutputFile(output) {}
Ted Kremenek5d866252008-11-03 22:33:57 +000049
Ted Kremenek4fc82c82008-11-03 23:18:07 +000050PathDiagnosticClient*
51clang::CreatePlistDiagnosticClient(const std::string& s,
52 Preprocessor*, PreprocessorFactory*) {
Ted Kremenek5d866252008-11-03 22:33:57 +000053 return new PlistDiagnostics(s);
54}
55
Chris Lattnera11d6172009-01-19 07:46:45 +000056static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenekc472d792009-01-23 20:06:20 +000057 SourceManager* SM, SourceLocation L) {
Ted Kremenek5d866252008-11-03 22:33:57 +000058
Ted Kremenekc472d792009-01-23 20:06:20 +000059 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000061 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000062 FIDs[FID] = V.size();
63 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000064}
65
Ted Kremenekc472d792009-01-23 20:06:20 +000066static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){
67 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000068 FIDMap::const_iterator I = FIDs.find(FID);
69 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000070 return I->second;
71}
72
73static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +000074 for (unsigned i = 0; i < indent; ++i) o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000075 return o;
76}
77
Ted Kremenekc472d792009-01-23 20:06:20 +000078static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM,
Ted Kremenek5d866252008-11-03 22:33:57 +000079 SourceLocation L, const FIDMap& FM,
80 const unsigned indent) {
81
82 Indent(o, indent) << "<dict>\n";
83 Indent(o, indent) << " <key>line</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000084 << SM->getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000085 Indent(o, indent) << " <key>col</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000086 << SM->getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000087 Indent(o, indent) << " <key>file</key><integer>"
88 << GetFID(FM, SM, L) << "</integer>\n";
89 Indent(o, indent) << "</dict>\n";
90}
91
Ted Kremenekc472d792009-01-23 20:06:20 +000092static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R,
Ted Kremenek5d866252008-11-03 22:33:57 +000093 const FIDMap& FM, const unsigned indent) {
94
95 Indent(o, indent) << "<array>\n";
96 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
97 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
98 Indent(o, indent) << "</array>\n";
99}
100
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000101static void ReportControlFlow(llvm::raw_ostream& o,
102 const PathDiagnosticControlFlowPiece& P,
103 const FIDMap& FM, SourceManager *SM,
104 unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000105
Ted Kremenek5d866252008-11-03 22:33:57 +0000106 Indent(o, indent) << "<dict>\n";
107 ++indent;
108
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000109 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
110
111 // Output the start and end locations.
112 Indent(o, indent) << "<key>start</key>\n";
113 EmitLocation(o, SM, P.getStartLocation(), FM, indent);
114 Indent(o, indent) << "<key>end</key>\n";
115 EmitLocation(o, SM, P.getEndLocation(), FM, indent);
116
117 // Output any helper text.
118 const std::string& s = P.getString();
119 if (!s.empty()) {
120 Indent(o, indent) << "<key>alternate</key><string>" << s << "</string>\n";
121 }
122
123 --indent;
124 Indent(o, indent) << "</dict>\n";
125}
126
127static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
128 const FIDMap& FM, SourceManager* SM, unsigned indent) {
129
130 Indent(o, indent) << "<dict>\n";
131 ++indent;
132
133 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
134
Ted Kremenek5d866252008-11-03 22:33:57 +0000135 // Output the location.
136 FullSourceLoc L = P.getLocation();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000137
Ted Kremenek5d866252008-11-03 22:33:57 +0000138 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000139 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000140
Ted Kremenek5d866252008-11-03 22:33:57 +0000141 // Output the ranges (if any).
142 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000143 RE = P.ranges_end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000144
145 if (RI != RE) {
146 Indent(o, indent) << "<key>ranges</key>\n";
147 Indent(o, indent) << "<array>\n";
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000148 ++indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000149 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000150 --indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000151 Indent(o, indent) << "</array>\n";
152 }
153
154 // Output the text.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000155 assert(!P.getString().empty());
Ted Kremenek61dc71a2009-03-19 00:42:56 +0000156 Indent(o, indent) << "<key>extended_message</key>\n";
157 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
158
159 // Output the short text.
160 // FIXME: Really use a short string.
Ted Kremenek5d866252008-11-03 22:33:57 +0000161 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000162 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000163
Ted Kremenek5d866252008-11-03 22:33:57 +0000164 // Finish up.
165 --indent;
166 Indent(o, indent); o << "</dict>\n";
167}
168
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000169static void ReportMacro(llvm::raw_ostream& o,
170 const PathDiagnosticMacroPiece& P,
171 const FIDMap& FM, SourceManager *SM,
172 unsigned indent) {
173
174 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
175 I!=E; ++I) {
176
177 switch ((*I)->getKind()) {
178 default:
179 break;
180 case PathDiagnosticPiece::Event:
181 ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent);
182 break;
183 case PathDiagnosticPiece::Macro:
184 ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent);
185 break;
186 }
187 }
188}
189
190static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
191 const FIDMap& FM, SourceManager* SM) {
192
193 unsigned indent = 4;
194
195 switch (P.getKind()) {
196 case PathDiagnosticPiece::ControlFlow:
197 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
198 indent);
199 break;
200 case PathDiagnosticPiece::Event:
201 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent);
202 break;
203 case PathDiagnosticPiece::Macro:
204 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent);
205 break;
206 }
207}
208
Ted Kremenek5d866252008-11-03 22:33:57 +0000209void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000210 if (!D)
211 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000212
Ted Kremenekddf32da2009-01-21 00:42:24 +0000213 if (D->empty()) {
214 delete D;
215 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000216 }
217
Ted Kremenekddf32da2009-01-21 00:42:24 +0000218 BatchedDiags.push_back(D);
219}
Ted Kremenek5d866252008-11-03 22:33:57 +0000220
Ted Kremenekddf32da2009-01-21 00:42:24 +0000221PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000222
223 // Build up a set of FIDs that we use by scanning the locations and
224 // ranges of the diagnostics.
225 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000226 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenekc472d792009-01-23 20:06:20 +0000227 SourceManager* SM = 0;
228
229 if (!BatchedDiags.empty())
230 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000231
Ted Kremenekddf32da2009-01-21 00:42:24 +0000232 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
233 DE = BatchedDiags.end(); DI != DE; ++DI) {
234
235 const PathDiagnostic *D = *DI;
236
237 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
238 AddFID(FM, Fids, SM, I->getLocation());
239
240 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
241 RE=I->ranges_end(); RI!=RE; ++RI) {
242 AddFID(FM, Fids, SM, RI->getBegin());
243 AddFID(FM, Fids, SM, RI->getEnd());
244 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000245 }
246 }
247
Ted Kremenekddf32da2009-01-21 00:42:24 +0000248 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000249 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000250 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000251 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000252 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000253 return;
254 }
255
256 // Write the plist header.
257 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000258 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
259 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
260 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000261
262 // Write the root object: a <dict> containing...
263 // - "files", an <array> mapping from FIDs to file names
264 // - "diagnostics", an <array> containing the path diagnostics
265 o << "<dict>\n"
266 " <key>files</key>\n"
267 " <array>\n";
268
Chris Lattner2b2453a2009-01-17 06:22:33 +0000269 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000270 I!=E; ++I)
Ted Kremenekc472d792009-01-23 20:06:20 +0000271 o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000272
273 o << " </array>\n"
274 " <key>diagnostics</key>\n"
275 " <array>\n";
276
Ted Kremenekddf32da2009-01-21 00:42:24 +0000277 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
278 DE = BatchedDiags.end(); DI!=DE; ++DI) {
279
280 o << " <dict>\n"
281 " <key>path</key>\n";
282
283 const PathDiagnostic *D = *DI;
284 // Create an owning smart pointer for 'D' just so that we auto-free it
285 // when we exit this method.
286 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
287
288 o << " <array>\n";
289
290 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
291 ReportDiag(o, *I, FM, SM);
292
293 o << " </array>\n";
294
295 // Output the bug type and bug category.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000296 o << " <key>description</key><string>" << D->getDescription()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000297 << "</string>\n"
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000298 << " <key>category</key><string>" << D->getCategory()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000299 << "</string>\n"
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000300 << " <key>type</key><string>" << D->getBugType()
301 << "</string>\n"
302 << " </dict>\n";
303
Ted Kremenekddf32da2009-01-21 00:42:24 +0000304 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000305
306 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000307
Ted Kremenek5d866252008-11-03 22:33:57 +0000308 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000309 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000310}