blob: afd8b26e5f210daabf94418d6ee6ff4211bad8ee [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"
20#include "llvm/System/Path.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallVector.h"
Ted Kremenek5d866252008-11-03 22:33:57 +000023using namespace clang;
Chris Lattner2b2453a2009-01-17 06:22:33 +000024
25typedef llvm::DenseMap<FileID, unsigned> FIDMap;
Ted Kremenek5d866252008-11-03 22:33:57 +000026
Ted Kremenek4fc82c82008-11-03 23:18:07 +000027namespace clang {
28 class Preprocessor;
29 class PreprocessorFactory;
30}
31
Ted Kremenek5d866252008-11-03 22:33:57 +000032namespace {
33 class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient {
Ted Kremenekddf32da2009-01-21 00:42:24 +000034 std::vector<const PathDiagnostic*> BatchedDiags;
35 const std::string OutputFile;
Ted Kremenek5d866252008-11-03 22:33:57 +000036 public:
37 PlistDiagnostics(const std::string& prefix);
Ted Kremenekddf32da2009-01-21 00:42:24 +000038 ~PlistDiagnostics();
Ted Kremenek5d866252008-11-03 22:33:57 +000039 void HandlePathDiagnostic(const PathDiagnostic* D);
40 };
41} // end anonymous namespace
42
Ted Kremenekddf32da2009-01-21 00:42:24 +000043PlistDiagnostics::PlistDiagnostics(const std::string& output)
44 : OutputFile(output) {}
Ted Kremenek5d866252008-11-03 22:33:57 +000045
Ted Kremenek4fc82c82008-11-03 23:18:07 +000046PathDiagnosticClient*
47clang::CreatePlistDiagnosticClient(const std::string& s,
48 Preprocessor*, PreprocessorFactory*) {
Ted Kremenek5d866252008-11-03 22:33:57 +000049 return new PlistDiagnostics(s);
50}
51
Chris Lattnera11d6172009-01-19 07:46:45 +000052static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenekc472d792009-01-23 20:06:20 +000053 SourceManager* SM, SourceLocation L) {
Ted Kremenek5d866252008-11-03 22:33:57 +000054
Ted Kremenekc472d792009-01-23 20:06:20 +000055 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000056 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000057 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 FIDs[FID] = V.size();
59 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000060}
61
Ted Kremenekc472d792009-01-23 20:06:20 +000062static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){
63 FileID FID = SM->getFileID(SM->getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000064 FIDMap::const_iterator I = FIDs.find(FID);
65 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000066 return I->second;
67}
68
69static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Chris Lattner2b2453a2009-01-17 06:22:33 +000070 for (unsigned i = 0; i < indent; ++i)
71 o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000072 return o;
73}
74
Ted Kremenekc472d792009-01-23 20:06:20 +000075static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM,
Ted Kremenek5d866252008-11-03 22:33:57 +000076 SourceLocation L, const FIDMap& FM,
77 const unsigned indent) {
78
79 Indent(o, indent) << "<dict>\n";
80 Indent(o, indent) << " <key>line</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000081 << SM->getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000082 Indent(o, indent) << " <key>col</key><integer>"
Ted Kremenekc472d792009-01-23 20:06:20 +000083 << SM->getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000084 Indent(o, indent) << " <key>file</key><integer>"
85 << GetFID(FM, SM, L) << "</integer>\n";
86 Indent(o, indent) << "</dict>\n";
87}
88
Ted Kremenekc472d792009-01-23 20:06:20 +000089static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R,
Ted Kremenek5d866252008-11-03 22:33:57 +000090 const FIDMap& FM, const unsigned indent) {
91
92 Indent(o, indent) << "<array>\n";
93 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
94 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
95 Indent(o, indent) << "</array>\n";
96}
97
98static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
Ted Kremenekc472d792009-01-23 20:06:20 +000099 const FIDMap& FM, SourceManager* SM) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000100
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000101 unsigned indent = 4;
Ted Kremenek5d866252008-11-03 22:33:57 +0000102 Indent(o, indent) << "<dict>\n";
103 ++indent;
104
105 // Output the location.
106 FullSourceLoc L = P.getLocation();
107
108 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000109 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek5d866252008-11-03 22:33:57 +0000110
111 // Output the ranges (if any).
112 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
113 RE = P.ranges_end();
114
115 if (RI != RE) {
116 Indent(o, indent) << "<key>ranges</key>\n";
117 Indent(o, indent) << "<array>\n";
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000118 ++indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000119 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000120 --indent;
Ted Kremenek5d866252008-11-03 22:33:57 +0000121 Indent(o, indent) << "</array>\n";
122 }
123
124 // Output the text.
125 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000126 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000127
128 // Output the hint.
Ted Kremenek3d7f2bc2009-03-02 19:40:15 +0000129#if 0
130 // Disable the display hint until we clear up its meaning.
Ted Kremenek5d866252008-11-03 22:33:57 +0000131 Indent(o, indent) << "<key>displayhint</key>\n";
132 Indent(o, indent) << "<string>"
133 << (P.getDisplayHint() == PathDiagnosticPiece::Above
134 ? "above" : "below")
135 << "</string>\n";
Ted Kremenek3d7f2bc2009-03-02 19:40:15 +0000136#endif
Ted Kremenekae3487f2009-03-02 21:44:02 +0000137 // Output the PathDiagnosticPiece::Kind.
138 Indent(o, indent) << "<key>kind</key>\n";
Ted Kremenek00086832009-03-10 02:49:29 +0000139 Indent(o, indent) << "<string>";
140
141 switch (P.getKind()) {
142 case PathDiagnosticPiece::Event: o << "Event"; break;
143 case PathDiagnosticPiece::ControlFlow: o << "ControlFlow"; break;
144 case PathDiagnosticPiece::Macro: o << "Macro"; break;
145 }
146 o << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000147
148
149 // Finish up.
150 --indent;
151 Indent(o, indent); o << "</dict>\n";
152}
153
154void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000155 if (!D)
156 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000157
Ted Kremenekddf32da2009-01-21 00:42:24 +0000158 if (D->empty()) {
159 delete D;
160 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000161 }
162
Ted Kremenekddf32da2009-01-21 00:42:24 +0000163 BatchedDiags.push_back(D);
164}
Ted Kremenek5d866252008-11-03 22:33:57 +0000165
Ted Kremenekddf32da2009-01-21 00:42:24 +0000166PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000167
168 // Build up a set of FIDs that we use by scanning the locations and
169 // ranges of the diagnostics.
170 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000171 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenekc472d792009-01-23 20:06:20 +0000172 SourceManager* SM = 0;
173
174 if (!BatchedDiags.empty())
175 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000176
Ted Kremenekddf32da2009-01-21 00:42:24 +0000177 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
178 DE = BatchedDiags.end(); DI != DE; ++DI) {
179
180 const PathDiagnostic *D = *DI;
181
182 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
183 AddFID(FM, Fids, SM, I->getLocation());
184
185 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
186 RE=I->ranges_end(); RI!=RE; ++RI) {
187 AddFID(FM, Fids, SM, RI->getBegin());
188 AddFID(FM, Fids, SM, RI->getEnd());
189 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000190 }
191 }
192
Ted Kremenekddf32da2009-01-21 00:42:24 +0000193 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000194 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000195 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000196 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000197 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000198 return;
199 }
200
201 // Write the plist header.
202 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000203 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
204 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
205 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000206
207 // Write the root object: a <dict> containing...
208 // - "files", an <array> mapping from FIDs to file names
209 // - "diagnostics", an <array> containing the path diagnostics
210 o << "<dict>\n"
211 " <key>files</key>\n"
212 " <array>\n";
213
Chris Lattner2b2453a2009-01-17 06:22:33 +0000214 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000215 I!=E; ++I)
Ted Kremenekc472d792009-01-23 20:06:20 +0000216 o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000217
218 o << " </array>\n"
219 " <key>diagnostics</key>\n"
220 " <array>\n";
221
Ted Kremenekddf32da2009-01-21 00:42:24 +0000222 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
223 DE = BatchedDiags.end(); DI!=DE; ++DI) {
224
225 o << " <dict>\n"
226 " <key>path</key>\n";
227
228 const PathDiagnostic *D = *DI;
229 // Create an owning smart pointer for 'D' just so that we auto-free it
230 // when we exit this method.
231 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
232
233 o << " <array>\n";
234
235 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
236 ReportDiag(o, *I, FM, SM);
237
238 o << " </array>\n";
239
240 // Output the bug type and bug category.
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000241 o << " <key>description</key>\n <string>" << D->getDescription()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000242 << "</string>\n"
243 << " <key>category</key>\n <string>" << D->getCategory()
244 << "</string>\n"
245 << " </dict>\n";
246 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000247
248 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000249
Ted Kremenek5d866252008-11-03 22:33:57 +0000250 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000251 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000252}