blob: 16c5c1019d7d0ddfce7d42779313c344b8c9fd26 [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 Kremenek5d866252008-11-03 22:33:57 +000041 void HandlePathDiagnostic(const PathDiagnostic* D);
42 };
43} // end anonymous namespace
44
Ted Kremenekddf32da2009-01-21 00:42:24 +000045PlistDiagnostics::PlistDiagnostics(const std::string& output)
46 : OutputFile(output) {}
Ted Kremenek5d866252008-11-03 22:33:57 +000047
Ted Kremenek4fc82c82008-11-03 23:18:07 +000048PathDiagnosticClient*
49clang::CreatePlistDiagnosticClient(const std::string& s,
50 Preprocessor*, PreprocessorFactory*) {
Ted Kremenek5d866252008-11-03 22:33:57 +000051 return new PlistDiagnostics(s);
52}
53
Chris Lattnera11d6172009-01-19 07:46:45 +000054static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenekc472d792009-01-23 20:06:20 +000055 SourceManager* SM, SourceLocation L) {
Ted Kremenek5d866252008-11-03 22:33:57 +000056
Ted Kremenekc472d792009-01-23 20:06:20 +000057 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000059 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 FIDs[FID] = V.size();
61 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000062}
63
Ted Kremenekc472d792009-01-23 20:06:20 +000064static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){
65 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000066 FIDMap::const_iterator I = FIDs.find(FID);
67 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000068 return I->second;
69}
70
71static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Ted Kremenek082cb8d2009-03-12 18:41:53 +000072 for (unsigned i = 0; i < indent; ++i) o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000073 return o;
74}
75
Ted Kremenekc472d792009-01-23 20:06:20 +000076static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM,
Ted Kremenek5d866252008-11-03 22:33:57 +000077 SourceLocation L, const FIDMap& FM,
78 const unsigned indent) {
79
80 Indent(o, indent) << "<dict>\n";
81 Indent(o, indent) << " <key>line</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000082 << SM->getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000083 Indent(o, indent) << " <key>col</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000084 << SM->getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000085 Indent(o, indent) << " <key>file</key><integer>"
86 << GetFID(FM, SM, L) << "</integer>\n";
87 Indent(o, indent) << "</dict>\n";
88}
89
Ted Kremenekc472d792009-01-23 20:06:20 +000090static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R,
Ted Kremenek5d866252008-11-03 22:33:57 +000091 const FIDMap& FM, const unsigned indent) {
92
93 Indent(o, indent) << "<array>\n";
94 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
95 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
96 Indent(o, indent) << "</array>\n";
97}
98
Ted Kremenek082cb8d2009-03-12 18:41:53 +000099static void ReportControlFlow(llvm::raw_ostream& o,
100 const PathDiagnosticControlFlowPiece& P,
101 const FIDMap& FM, SourceManager *SM,
102 unsigned indent) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000103
Ted Kremenek5d866252008-11-03 22:33:57 +0000104 Indent(o, indent) << "<dict>\n";
105 ++indent;
106
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000107 Indent(o, indent) << "<key>kind</key><string>control</string>\n";
108
109 // Output the start and end locations.
110 Indent(o, indent) << "<key>start</key>\n";
111 EmitLocation(o, SM, P.getStartLocation(), FM, indent);
112 Indent(o, indent) << "<key>end</key>\n";
113 EmitLocation(o, SM, P.getEndLocation(), FM, indent);
114
115 // Output any helper text.
116 const std::string& s = P.getString();
117 if (!s.empty()) {
118 Indent(o, indent) << "<key>alternate</key><string>" << s << "</string>\n";
119 }
120
121 --indent;
122 Indent(o, indent) << "</dict>\n";
123}
124
125static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
126 const FIDMap& FM, SourceManager* SM, unsigned indent) {
127
128 Indent(o, indent) << "<dict>\n";
129 ++indent;
130
131 Indent(o, indent) << "<key>kind</key><string>event</string>\n";
132
Ted Kremenek5d866252008-11-03 22:33:57 +0000133 // Output the location.
134 FullSourceLoc L = P.getLocation();
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000135
Ted Kremenek5d866252008-11-03 22:33:57 +0000136 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000137 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000138
Ted Kremenek5d866252008-11-03 22:33:57 +0000139 // Output the ranges (if any).
140 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000141 RE = P.ranges_end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000142
143 if (RI != RE) {
144 Indent(o, indent) << "<key>ranges</key>\n";
145 Indent(o, indent) << "<array>\n";
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000146 ++indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000147 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000148 --indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000149 Indent(o, indent) << "</array>\n";
150 }
151
152 // Output the text.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000153 assert(!P.getString().empty());
Ted Kremenek5d866252008-11-03 22:33:57 +0000154 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000155 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000156
Ted Kremenek5d866252008-11-03 22:33:57 +0000157 // Finish up.
158 --indent;
159 Indent(o, indent); o << "</dict>\n";
160}
161
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000162static void ReportMacro(llvm::raw_ostream& o,
163 const PathDiagnosticMacroPiece& P,
164 const FIDMap& FM, SourceManager *SM,
165 unsigned indent) {
166
167 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
168 I!=E; ++I) {
169
170 switch ((*I)->getKind()) {
171 default:
172 break;
173 case PathDiagnosticPiece::Event:
174 ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent);
175 break;
176 case PathDiagnosticPiece::Macro:
177 ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent);
178 break;
179 }
180 }
181}
182
183static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
184 const FIDMap& FM, SourceManager* SM) {
185
186 unsigned indent = 4;
187
188 switch (P.getKind()) {
189 case PathDiagnosticPiece::ControlFlow:
190 ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM,
191 indent);
192 break;
193 case PathDiagnosticPiece::Event:
194 ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent);
195 break;
196 case PathDiagnosticPiece::Macro:
197 ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent);
198 break;
199 }
200}
201
Ted Kremenek5d866252008-11-03 22:33:57 +0000202void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000203 if (!D)
204 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000205
Ted Kremenekddf32da2009-01-21 00:42:24 +0000206 if (D->empty()) {
207 delete D;
208 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000209 }
210
Ted Kremenekddf32da2009-01-21 00:42:24 +0000211 BatchedDiags.push_back(D);
212}
Ted Kremenek5d866252008-11-03 22:33:57 +0000213
Ted Kremenekddf32da2009-01-21 00:42:24 +0000214PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000215
216 // Build up a set of FIDs that we use by scanning the locations and
217 // ranges of the diagnostics.
218 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000219 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenekc472d792009-01-23 20:06:20 +0000220 SourceManager* SM = 0;
221
222 if (!BatchedDiags.empty())
223 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000224
Ted Kremenekddf32da2009-01-21 00:42:24 +0000225 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
226 DE = BatchedDiags.end(); DI != DE; ++DI) {
227
228 const PathDiagnostic *D = *DI;
229
230 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
231 AddFID(FM, Fids, SM, I->getLocation());
232
233 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
234 RE=I->ranges_end(); RI!=RE; ++RI) {
235 AddFID(FM, Fids, SM, RI->getBegin());
236 AddFID(FM, Fids, SM, RI->getEnd());
237 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000238 }
239 }
240
Ted Kremenekddf32da2009-01-21 00:42:24 +0000241 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000242 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000243 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000244 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000245 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000246 return;
247 }
248
249 // Write the plist header.
250 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000251 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
252 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
253 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000254
255 // Write the root object: a <dict> containing...
256 // - "files", an <array> mapping from FIDs to file names
257 // - "diagnostics", an <array> containing the path diagnostics
258 o << "<dict>\n"
259 " <key>files</key>\n"
260 " <array>\n";
261
Chris Lattner2b2453a2009-01-17 06:22:33 +0000262 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000263 I!=E; ++I)
Ted Kremenekc472d792009-01-23 20:06:20 +0000264 o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000265
266 o << " </array>\n"
267 " <key>diagnostics</key>\n"
268 " <array>\n";
269
Ted Kremenekddf32da2009-01-21 00:42:24 +0000270 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
271 DE = BatchedDiags.end(); DI!=DE; ++DI) {
272
273 o << " <dict>\n"
274 " <key>path</key>\n";
275
276 const PathDiagnostic *D = *DI;
277 // Create an owning smart pointer for 'D' just so that we auto-free it
278 // when we exit this method.
279 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
280
281 o << " <array>\n";
282
283 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
284 ReportDiag(o, *I, FM, SM);
285
286 o << " </array>\n";
287
288 // Output the bug type and bug category.
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000289 o << " <key>description</key><string>" << D->getDescription()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000290 << "</string>\n"
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000291 << " <key>category</key><string>" << D->getCategory()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000292 << "</string>\n"
Ted Kremenek082cb8d2009-03-12 18:41:53 +0000293 << " <key>type</key><string>" << D->getBugType()
294 << "</string>\n"
295 << " </dict>\n";
296
Ted Kremenekddf32da2009-01-21 00:42:24 +0000297 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000298
299 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000300
Ted Kremenek5d866252008-11-03 22:33:57 +0000301 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000302 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000303}