blob: 14280288cd4004ce9bdb0173e9899cb788a11cb8 [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
14#include "clang/Driver/PathDiagnosticClients.h"
15#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"
23
24using namespace clang;
25typedef llvm::DenseMap<unsigned,unsigned> FIDMap;
26
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 {
34 llvm::sys::Path Directory, FilePrefix;
35 bool createdDir, noDir;
36 public:
37 PlistDiagnostics(const std::string& prefix);
38 ~PlistDiagnostics() {}
39 void HandlePathDiagnostic(const PathDiagnostic* D);
40 };
41} // end anonymous namespace
42
43PlistDiagnostics::PlistDiagnostics(const std::string& prefix)
44 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) {
45 FilePrefix.appendComponent("report"); // All Plist files begin with "report"
46}
47
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
54static void AddFID(FIDMap& FIDs,
55 llvm::SmallVectorImpl<unsigned>& V,
56 SourceManager& SM, SourceLocation L) {
57
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000058 unsigned fid = SM.getCanonicalFileID(SM.getInstantiationLoc(L));
Ted Kremenek5d866252008-11-03 22:33:57 +000059 FIDMap::iterator I = FIDs.find(fid);
60 if (I != FIDs.end()) return;
61 FIDs[fid] = V.size();
62 V.push_back(fid);
63}
64
65static unsigned GetFID(const FIDMap& FIDs,
66 SourceManager& SM, SourceLocation L) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000067 unsigned fid = SM.getCanonicalFileID(SM.getInstantiationLoc(L));
Ted Kremenek5d866252008-11-03 22:33:57 +000068 FIDMap::const_iterator I = FIDs.find(fid);
69 assert (I != FIDs.end());
70 return I->second;
71}
72
73static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
74 for (unsigned i = 0; i < indent; ++i) o << ' ';
75 return o;
76}
77
78static void EmitLocation(llvm::raw_ostream& o, SourceManager& SM,
79 SourceLocation L, const FIDMap& FM,
80 const unsigned indent) {
81
82 Indent(o, indent) << "<dict>\n";
83 Indent(o, indent) << " <key>line</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000084 << SM.getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000085 Indent(o, indent) << " <key>col</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +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
92static void EmitRange(llvm::raw_ostream& o, SourceManager& SM, SourceRange R,
93 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
101static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
102 const FIDMap& FM, SourceManager& SM) {
103
104 unsigned indent = 2;
105 Indent(o, indent) << "<dict>\n";
106 ++indent;
107
108 // Output the location.
109 FullSourceLoc L = P.getLocation();
110
111 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000112 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek5d866252008-11-03 22:33:57 +0000113
114 // Output the ranges (if any).
115 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
116 RE = P.ranges_end();
117
118 if (RI != RE) {
119 Indent(o, indent) << "<key>ranges</key>\n";
120 Indent(o, indent) << "<array>\n";
121 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
122 Indent(o, indent) << "</array>\n";
123 }
124
125 // Output the text.
126 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000127 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000128
129 // Output the hint.
130 Indent(o, indent) << "<key>displayhint</key>\n";
131 Indent(o, indent) << "<string>"
132 << (P.getDisplayHint() == PathDiagnosticPiece::Above
133 ? "above" : "below")
134 << "</string>\n";
135
136
137 // Finish up.
138 --indent;
139 Indent(o, indent); o << "</dict>\n";
140}
141
142void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
143
144 // Create an owning smart pointer for 'D' just so that we auto-free it
145 // when we exit this method.
146 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
147
148 // Create the directory to contain the plist files if it is missing.
149 if (!createdDir) {
150 createdDir = true;
151 std::string ErrorMsg;
152 Directory.createDirectoryOnDisk(true, &ErrorMsg);
153
154 if (!Directory.isDirectory()) {
155 llvm::errs() << "warning: could not create directory '"
156 << Directory.toString() << "'\n"
157 << "reason: " << ErrorMsg << '\n';
158
159 noDir = true;
160
161 return;
162 }
163 }
164
165 if (noDir)
166 return;
167
168 // Get the source manager.
169 SourceManager& SM = D->begin()->getLocation().getManager();
170
171 // Build up a set of FIDs that we use by scanning the locations and
172 // ranges of the diagnostics.
173 FIDMap FM;
174 llvm::SmallVector<unsigned, 10> Fids;
175
176 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) {
Chris Lattner59ddeab2009-01-16 23:06:35 +0000177 AddFID(FM, Fids, SM, I->getLocation());
Ted Kremenek5d866252008-11-03 22:33:57 +0000178
179 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
180 RE=I->ranges_end(); RI!=RE; ++RI) {
181 AddFID(FM, Fids, SM, RI->getBegin());
182 AddFID(FM, Fids, SM, RI->getEnd());
183 }
184 }
185
186 // Create a path for the target Plist file.
187 llvm::sys::Path F(FilePrefix);
188 F.makeUnique(false, NULL);
189
190 // Rename the file with an Plist extension.
191 llvm::sys::Path H(F);
192 H.appendSuffix("plist");
193 F.renamePathOnDisk(H, NULL);
194
195 // Now create the plist file.
196 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000197 llvm::raw_fd_ostream o(H.toString().c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000198
199 if (!ErrMsg.empty()) {
200 llvm::errs() << "warning: could not creat file: " << H.toString() << '\n';
201 return;
202 }
203
204 // Write the plist header.
205 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
206 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
207 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
208 "<plist version=\"1.0\">\n";
209
210 // Write the root object: a <dict> containing...
211 // - "files", an <array> mapping from FIDs to file names
212 // - "diagnostics", an <array> containing the path diagnostics
213 o << "<dict>\n"
214 " <key>files</key>\n"
215 " <array>\n";
216
217 for (llvm::SmallVectorImpl<unsigned>::iterator I=Fids.begin(), E=Fids.end();
218 I!=E; ++I)
219 o << " <string>" << SM.getFileEntryForID(*I)->getName() << "</string>\n";
220
221 o << " </array>\n"
222 " <key>diagnostics</key>\n"
223 " <array>\n";
224
225 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
226 ReportDiag(o, *I, FM, SM);
227
228 o << " </array>\n";
229
230 // Output the bug type and bug category.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000231 o << " <key>description</key>\n <string>" << D->getDescription() << "</string>\n"
232 " <key>category</key>\n <string>" << D->getCategory() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000233
234 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000235 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000236}