blob: 9b7da489160f7113a822da58a555725fff26e11a [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 Lattnera11d6172009-01-19 07:46:45 +000054static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V,
Ted Kremenek5d866252008-11-03 22:33:57 +000055 SourceManager& SM, SourceLocation L) {
56
Chris Lattnera11d6172009-01-19 07:46:45 +000057 FileID FID = SM.getFileID(SM.getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 FIDMap::iterator I = FIDs.find(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000059 if (I != FIDs.end()) return;
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 FIDs[FID] = V.size();
61 V.push_back(FID);
Ted Kremenek5d866252008-11-03 22:33:57 +000062}
63
Chris Lattnera11d6172009-01-19 07:46:45 +000064static unsigned GetFID(const FIDMap& FIDs, SourceManager& SM, SourceLocation L){
65 FileID FID = SM.getFileID(SM.getInstantiationLoc(L));
Chris Lattner2b2453a2009-01-17 06:22:33 +000066 FIDMap::const_iterator I = FIDs.find(FID);
67 assert(I != FIDs.end());
Ted Kremenek5d866252008-11-03 22:33:57 +000068 return I->second;
69}
70
71static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) {
Chris Lattner2b2453a2009-01-17 06:22:33 +000072 for (unsigned i = 0; i < indent; ++i)
73 o << ' ';
Ted Kremenek5d866252008-11-03 22:33:57 +000074 return o;
75}
76
77static void EmitLocation(llvm::raw_ostream& o, SourceManager& SM,
78 SourceLocation L, const FIDMap& FM,
79 const unsigned indent) {
80
81 Indent(o, indent) << "<dict>\n";
82 Indent(o, indent) << " <key>line</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000083 << SM.getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000084 Indent(o, indent) << " <key>col</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000085 << SM.getInstantiationColumnNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000086 Indent(o, indent) << " <key>file</key><integer>"
87 << GetFID(FM, SM, L) << "</integer>\n";
88 Indent(o, indent) << "</dict>\n";
89}
90
91static void EmitRange(llvm::raw_ostream& o, SourceManager& SM, SourceRange R,
92 const FIDMap& FM, const unsigned indent) {
93
94 Indent(o, indent) << "<array>\n";
95 EmitLocation(o, SM, R.getBegin(), FM, indent+1);
96 EmitLocation(o, SM, R.getEnd(), FM, indent+1);
97 Indent(o, indent) << "</array>\n";
98}
99
100static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P,
101 const FIDMap& FM, SourceManager& SM) {
102
103 unsigned indent = 2;
104 Indent(o, indent) << "<dict>\n";
105 ++indent;
106
107 // Output the location.
108 FullSourceLoc L = P.getLocation();
109
110 Indent(o, indent) << "<key>location</key>\n";
Chris Lattner59ddeab2009-01-16 23:06:35 +0000111 EmitLocation(o, SM, L, FM, indent);
Ted Kremenek5d866252008-11-03 22:33:57 +0000112
113 // Output the ranges (if any).
114 PathDiagnosticPiece::range_iterator RI = P.ranges_begin(),
115 RE = P.ranges_end();
116
117 if (RI != RE) {
118 Indent(o, indent) << "<key>ranges</key>\n";
119 Indent(o, indent) << "<array>\n";
120 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
121 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.
129 Indent(o, indent) << "<key>displayhint</key>\n";
130 Indent(o, indent) << "<string>"
131 << (P.getDisplayHint() == PathDiagnosticPiece::Above
132 ? "above" : "below")
133 << "</string>\n";
134
135
136 // Finish up.
137 --indent;
138 Indent(o, indent); o << "</dict>\n";
139}
140
141void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
142
143 // Create an owning smart pointer for 'D' just so that we auto-free it
144 // when we exit this method.
145 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
146
147 // Create the directory to contain the plist files if it is missing.
148 if (!createdDir) {
149 createdDir = true;
150 std::string ErrorMsg;
151 Directory.createDirectoryOnDisk(true, &ErrorMsg);
152
153 if (!Directory.isDirectory()) {
154 llvm::errs() << "warning: could not create directory '"
155 << Directory.toString() << "'\n"
156 << "reason: " << ErrorMsg << '\n';
157
158 noDir = true;
159
160 return;
161 }
162 }
163
164 if (noDir)
165 return;
166
167 // Get the source manager.
168 SourceManager& SM = D->begin()->getLocation().getManager();
169
170 // Build up a set of FIDs that we use by scanning the locations and
171 // ranges of the diagnostics.
172 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000173 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenek5d866252008-11-03 22:33:57 +0000174
175 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) {
Chris Lattner59ddeab2009-01-16 23:06:35 +0000176 AddFID(FM, Fids, SM, I->getLocation());
Ted Kremenek5d866252008-11-03 22:33:57 +0000177
178 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
Chris Lattnera11d6172009-01-19 07:46:45 +0000179 RE=I->ranges_end(); RI!=RE; ++RI) {
Ted Kremenek5d866252008-11-03 22:33:57 +0000180 AddFID(FM, Fids, SM, RI->getBegin());
181 AddFID(FM, Fids, SM, RI->getEnd());
182 }
183 }
184
185 // Create a path for the target Plist file.
186 llvm::sys::Path F(FilePrefix);
187 F.makeUnique(false, NULL);
188
189 // Rename the file with an Plist extension.
190 llvm::sys::Path H(F);
191 H.appendSuffix("plist");
192 F.renamePathOnDisk(H, NULL);
193
194 // Now create the plist file.
195 std::string ErrMsg;
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000196 llvm::raw_fd_ostream o(H.toString().c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000197
198 if (!ErrMsg.empty()) {
199 llvm::errs() << "warning: could not creat file: " << H.toString() << '\n';
200 return;
201 }
202
203 // Write the plist header.
204 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
205 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
206 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
207 "<plist version=\"1.0\">\n";
208
209 // Write the root object: a <dict> containing...
210 // - "files", an <array> mapping from FIDs to file names
211 // - "diagnostics", an <array> containing the path diagnostics
212 o << "<dict>\n"
213 " <key>files</key>\n"
214 " <array>\n";
215
Chris Lattner2b2453a2009-01-17 06:22:33 +0000216 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000217 I!=E; ++I)
218 o << " <string>" << SM.getFileEntryForID(*I)->getName() << "</string>\n";
219
220 o << " </array>\n"
221 " <key>diagnostics</key>\n"
222 " <array>\n";
223
224 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
225 ReportDiag(o, *I, FM, SM);
226
227 o << " </array>\n";
228
229 // Output the bug type and bug category.
Chris Lattnera11d6172009-01-19 07:46:45 +0000230 o << " <key>description</key>\n <string>" << D->getDescription()
231 << "</string>\n"
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000232 " <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}