blob: c40794cef75599a73aa0a456b4dd1fb483dee4d0 [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";
139 Indent(o, indent) << "<string>"
140 << (P.getKind() == PathDiagnosticPiece::Event
141 ? "Event" : "ControlFlow")
142 << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000143
144
145 // Finish up.
146 --indent;
147 Indent(o, indent); o << "</dict>\n";
148}
149
150void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000151 if (!D)
152 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000153
Ted Kremenekddf32da2009-01-21 00:42:24 +0000154 if (D->empty()) {
155 delete D;
156 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000157 }
158
Ted Kremenekddf32da2009-01-21 00:42:24 +0000159 BatchedDiags.push_back(D);
160}
Ted Kremenek5d866252008-11-03 22:33:57 +0000161
Ted Kremenekddf32da2009-01-21 00:42:24 +0000162PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000163
164 // Build up a set of FIDs that we use by scanning the locations and
165 // ranges of the diagnostics.
166 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000167 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenekc472d792009-01-23 20:06:20 +0000168 SourceManager* SM = 0;
169
170 if (!BatchedDiags.empty())
171 SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000172
Ted Kremenekddf32da2009-01-21 00:42:24 +0000173 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
174 DE = BatchedDiags.end(); DI != DE; ++DI) {
175
176 const PathDiagnostic *D = *DI;
177
178 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
179 AddFID(FM, Fids, SM, I->getLocation());
180
181 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
182 RE=I->ranges_end(); RI!=RE; ++RI) {
183 AddFID(FM, Fids, SM, RI->getBegin());
184 AddFID(FM, Fids, SM, RI->getEnd());
185 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000186 }
187 }
188
Ted Kremenekddf32da2009-01-21 00:42:24 +0000189 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000190 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000191 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000192 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000193 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000194 return;
195 }
196
197 // Write the plist header.
198 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000199 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
200 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
201 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000202
203 // Write the root object: a <dict> containing...
204 // - "files", an <array> mapping from FIDs to file names
205 // - "diagnostics", an <array> containing the path diagnostics
206 o << "<dict>\n"
207 " <key>files</key>\n"
208 " <array>\n";
209
Chris Lattner2b2453a2009-01-17 06:22:33 +0000210 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000211 I!=E; ++I)
Ted Kremenekc472d792009-01-23 20:06:20 +0000212 o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000213
214 o << " </array>\n"
215 " <key>diagnostics</key>\n"
216 " <array>\n";
217
Ted Kremenekddf32da2009-01-21 00:42:24 +0000218 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
219 DE = BatchedDiags.end(); DI!=DE; ++DI) {
220
221 o << " <dict>\n"
222 " <key>path</key>\n";
223
224 const PathDiagnostic *D = *DI;
225 // Create an owning smart pointer for 'D' just so that we auto-free it
226 // when we exit this method.
227 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
228
229 o << " <array>\n";
230
231 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
232 ReportDiag(o, *I, FM, SM);
233
234 o << " </array>\n";
235
236 // Output the bug type and bug category.
Ted Kremenekd671c5a2009-02-02 21:45:32 +0000237 o << " <key>description</key>\n <string>" << D->getDescription()
Ted Kremenekddf32da2009-01-21 00:42:24 +0000238 << "</string>\n"
239 << " <key>category</key>\n <string>" << D->getCategory()
240 << "</string>\n"
241 << " </dict>\n";
242 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000243
244 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000245
Ted Kremenek5d866252008-11-03 22:33:57 +0000246 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000247 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000248}