blob: 500524157e57ec0698c74ee07206b91356a3406a [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
58 unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(L));
59 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) {
67
68 unsigned fid = SM.getCanonicalFileID(SM.getLogicalLoc(L));
69 FIDMap::const_iterator I = FIDs.find(fid);
70 assert (I != FIDs.end());
71 return I->second;
72}
73
74static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
75 for (unsigned i = 0; i < indent; ++i) o << ' ';
76 return o;
77}
78
79static void EmitLocation(llvm::raw_ostream& o, SourceManager& SM,
80 SourceLocation L, const FIDMap& FM,
81 const unsigned indent) {
82
83 Indent(o, indent) << "<dict>\n";
84 Indent(o, indent) << " <key>line</key><integer>"
85 << SM.getLogicalLineNumber(L) << "</integer>\n";
86 Indent(o, indent) << " <key>col</key><integer>"
87 << SM.getLogicalColumnNumber(L) << "</integer>\n";
88 Indent(o, indent) << " <key>file</key><integer>"
89 << GetFID(FM, SM, L) << "</integer>\n";
90 Indent(o, indent) << "</dict>\n";
91}
92
93static void EmitRange(llvm::raw_ostream& o, SourceManager& SM, SourceRange R,
94 const FIDMap& FM, const unsigned indent) {
95
96 Indent(o, indent) << "<array>\n";
97 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
98 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
99 Indent(o, indent) << "</array>\n";
100}
101
102static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
103 const FIDMap& FM, SourceManager& SM) {
104
105 unsigned indent = 2;
106 Indent(o, indent) << "<dict>\n";
107 ++indent;
108
109 // Output the location.
110 FullSourceLoc L = P.getLocation();
111
112 Indent(o, indent) << "<key>location</key>\n";
113 EmitLocation(o, SM, L.getLocation(), FM, indent);
114
115 // Output the ranges (if any).
116 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
117 RE = P.ranges_end();
118
119 if (RI != RE) {
120 Indent(o, indent) << "<key>ranges</key>\n";
121 Indent(o, indent) << "<array>\n";
122 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
123 Indent(o, indent) << "</array>\n";
124 }
125
126 // Output the text.
127 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000128 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000129
130 // Output the hint.
131 Indent(o, indent) << "<key>displayhint</key>\n";
132 Indent(o, indent) << "<string>"
133 << (P.getDisplayHint() == PathDiagnosticPiece::Above
134 ? "above" : "below")
135 << "</string>\n";
136
137
138 // Finish up.
139 --indent;
140 Indent(o, indent); o << "</dict>\n";
141}
142
143void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
144
145 // Create an owning smart pointer for 'D' just so that we auto-free it
146 // when we exit this method.
147 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
148
149 // Create the directory to contain the plist files if it is missing.
150 if (!createdDir) {
151 createdDir = true;
152 std::string ErrorMsg;
153 Directory.createDirectoryOnDisk(true, &ErrorMsg);
154
155 if (!Directory.isDirectory()) {
156 llvm::errs() << "warning: could not create directory '"
157 << Directory.toString() << "'\n"
158 << "reason: " << ErrorMsg << '\n';
159
160 noDir = true;
161
162 return;
163 }
164 }
165
166 if (noDir)
167 return;
168
169 // Get the source manager.
170 SourceManager& SM = D->begin()->getLocation().getManager();
171
172 // Build up a set of FIDs that we use by scanning the locations and
173 // ranges of the diagnostics.
174 FIDMap FM;
175 llvm::SmallVector<unsigned, 10> Fids;
176
177 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) {
178 AddFID(FM, Fids, SM, I->getLocation().getLocation());
179
180 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
181 RE=I->ranges_end(); RI!=RE; ++RI) {
182 AddFID(FM, Fids, SM, RI->getBegin());
183 AddFID(FM, Fids, SM, RI->getEnd());
184 }
185 }
186
187 // Create a path for the target Plist file.
188 llvm::sys::Path F(FilePrefix);
189 F.makeUnique(false, NULL);
190
191 // Rename the file with an Plist extension.
192 llvm::sys::Path H(F);
193 H.appendSuffix("plist");
194 F.renamePathOnDisk(H, NULL);
195
196 // Now create the plist file.
197 std::string ErrMsg;
198 llvm::raw_fd_ostream o(H.toString().c_str(), ErrMsg);
199
200 if (!ErrMsg.empty()) {
201 llvm::errs() << "warning: could not creat file: " << H.toString() << '\n';
202 return;
203 }
204
205 // Write the plist header.
206 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
207 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
208 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
209 "<plist version=\"1.0\">\n";
210
211 // Write the root object: a <dict> containing...
212 // - "files", an <array> mapping from FIDs to file names
213 // - "diagnostics", an <array> containing the path diagnostics
214 o << "<dict>\n"
215 " <key>files</key>\n"
216 " <array>\n";
217
218 for (llvm::SmallVectorImpl<unsigned>::iterator I=Fids.begin(), E=Fids.end();
219 I!=E; ++I)
220 o << " <string>" << SM.getFileEntryForID(*I)->getName() << "</string>\n";
221
222 o << " </array>\n"
223 " <key>diagnostics</key>\n"
224 " <array>\n";
225
226 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
227 ReportDiag(o, *I, FM, SM);
228
229 o << " </array>\n";
230
231 // Output the bug type and bug category.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000232 o << " <key>description</key>\n <string>" << D->getDescription() << "</string>\n"
233 " <key>category</key>\n <string>" << D->getCategory() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000234
235 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000236 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000237}