blob: 0b2ca52f11241fd61852c13c25ddd6c86c67568e [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"
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 {
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
Chris Lattner2b2453a2009-01-17 06:22:33 +000054static void AddFID(FIDMap &FIDs,
55 llvm::SmallVectorImpl<FileID> &V,
Ted Kremenek5d866252008-11-03 22:33:57 +000056 SourceManager& SM, SourceLocation L) {
57
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 FileID FID = SM.getCanonicalFileID(SM.getInstantiationLoc(L));
59 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000060 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000061 FIDs[FID] = V.size();
62 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000063}
64
65static unsigned GetFID(const FIDMap& FIDs,
66 SourceManager& SM, SourceLocation L) {
Chris Lattner2b2453a2009-01-17 06:22:33 +000067 FileID FID = SM.getCanonicalFileID(SM.getInstantiationLoc(L));
68 FIDMap::const_iterator I = FIDs.find(FID);
69 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000070 return I->second;
71}
72
73static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Chris Lattner2b2453a2009-01-17 06:22:33 +000074 for (unsigned i = 0; i < indent; ++i)
75 o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000076 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>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000085 << SM.getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000086 Indent(o, indent) << " <key>col</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000087 << SM.getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000088 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";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000113 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek5d866252008-11-03 22:33:57 +0000114
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;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000175 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenek5d866252008-11-03 22:33:57 +0000176
177 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) {
Chris Lattner59ddeab2009-01-16 23:06:35 +0000178 AddFID(FM, Fids, SM, I->getLocation());
Ted Kremenek5d866252008-11-03 22:33:57 +0000179
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;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000198 llvm::raw_fd_ostream o(H.toString().c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000199
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
Chris Lattner2b2453a2009-01-17 06:22:33 +0000218 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000219 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}