blob: ab5b5a394ab67c794df2d4af2f8e8cba2dc1a9d2 [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 {
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 Kremenek5d866252008-11-03 22:33:57 +000053 SourceManager& SM, SourceLocation L) {
54
Chris Lattnera11d6172009-01-19 07:46:45 +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
Chris Lattnera11d6172009-01-19 07:46:45 +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
75static void EmitLocation(llvm::raw_ostream& o, SourceManager& SM,
76 SourceLocation L, const FIDMap& FM,
77 const unsigned indent) {
78
79 Indent(o, indent) << "<dict>\n";
80 Indent(o, indent) << " <key>line</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +000081 << SM.getInstantiationLineNumber(L) << "</integer>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +000082 Indent(o, indent) << " <key>col</key><integer>"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +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
89static void EmitRange(llvm::raw_ostream& o, SourceManager& SM, SourceRange R,
90 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,
99 const FIDMap& FM, SourceManager& SM) {
100
101 unsigned indent = 2;
102 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";
118 for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1);
119 Indent(o, indent) << "</array>\n";
120 }
121
122 // Output the text.
123 Indent(o, indent) << "<key>message</key>\n";
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000124 Indent(o, indent) << "<string>" << P.getString() << "</string>\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000125
126 // Output the hint.
127 Indent(o, indent) << "<key>displayhint</key>\n";
128 Indent(o, indent) << "<string>"
129 << (P.getDisplayHint() == PathDiagnosticPiece::Above
130 ? "above" : "below")
131 << "</string>\n";
132
133
134 // Finish up.
135 --indent;
136 Indent(o, indent); o << "</dict>\n";
137}
138
139void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000140 if (!D)
141 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000142
Ted Kremenekddf32da2009-01-21 00:42:24 +0000143 if (D->empty()) {
144 delete D;
145 return;
Ted Kremenek5d866252008-11-03 22:33:57 +0000146 }
147
Ted Kremenekddf32da2009-01-21 00:42:24 +0000148 BatchedDiags.push_back(D);
149}
Ted Kremenek5d866252008-11-03 22:33:57 +0000150
Ted Kremenekddf32da2009-01-21 00:42:24 +0000151PlistDiagnostics::~PlistDiagnostics() {
Ted Kremenek5d866252008-11-03 22:33:57 +0000152
153 // Build up a set of FIDs that we use by scanning the locations and
154 // ranges of the diagnostics.
155 FIDMap FM;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000156 llvm::SmallVector<FileID, 10> Fids;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000157 SourceManager& SM = (*BatchedDiags.begin())->begin()->getLocation().getManager();
Ted Kremenek5d866252008-11-03 22:33:57 +0000158
Ted Kremenekddf32da2009-01-21 00:42:24 +0000159 for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(),
160 DE = BatchedDiags.end(); DI != DE; ++DI) {
161
162 const PathDiagnostic *D = *DI;
163
164 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) {
165 AddFID(FM, Fids, SM, I->getLocation());
166
167 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
168 RE=I->ranges_end(); RI!=RE; ++RI) {
169 AddFID(FM, Fids, SM, RI->getBegin());
170 AddFID(FM, Fids, SM, RI->getEnd());
171 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000172 }
173 }
174
Ted Kremenekddf32da2009-01-21 00:42:24 +0000175 // Open the file.
Ted Kremenek5d866252008-11-03 22:33:57 +0000176 std::string ErrMsg;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000177 llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg);
Ted Kremenek5d866252008-11-03 22:33:57 +0000178 if (!ErrMsg.empty()) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000179 llvm::errs() << "warning: could not creat file: " << OutputFile << '\n';
Ted Kremenek5d866252008-11-03 22:33:57 +0000180 return;
181 }
182
183 // Write the plist header.
184 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Ted Kremenekddf32da2009-01-21 00:42:24 +0000185 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
186 "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
187 "<plist version=\"1.0\">\n";
Ted Kremenek5d866252008-11-03 22:33:57 +0000188
189 // Write the root object: a <dict> containing...
190 // - "files", an <array> mapping from FIDs to file names
191 // - "diagnostics", an <array> containing the path diagnostics
192 o << "<dict>\n"
193 " <key>files</key>\n"
194 " <array>\n";
195
Chris Lattner2b2453a2009-01-17 06:22:33 +0000196 for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
Ted Kremenek5d866252008-11-03 22:33:57 +0000197 I!=E; ++I)
198 o << " <string>" << SM.getFileEntryForID(*I)->getName() << "</string>\n";
199
200 o << " </array>\n"
201 " <key>diagnostics</key>\n"
202 " <array>\n";
203
Ted Kremenekddf32da2009-01-21 00:42:24 +0000204 for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(),
205 DE = BatchedDiags.end(); DI!=DE; ++DI) {
206
207 o << " <dict>\n"
208 " <key>path</key>\n";
209
210 const PathDiagnostic *D = *DI;
211 // Create an owning smart pointer for 'D' just so that we auto-free it
212 // when we exit this method.
213 llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D));
214
215 o << " <array>\n";
216
217 for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I)
218 ReportDiag(o, *I, FM, SM);
219
220 o << " </array>\n";
221
222 // Output the bug type and bug category.
223 o << " <key>description</key>\n <string>" << D->getDescription()
224 << "</string>\n"
225 << " <key>category</key>\n <string>" << D->getCategory()
226 << "</string>\n"
227 << " </dict>\n";
228 }
Ted Kremenek5d866252008-11-03 22:33:57 +0000229
230 o << " </array>\n";
Ted Kremenekddf32da2009-01-21 00:42:24 +0000231
Ted Kremenek5d866252008-11-03 22:33:57 +0000232 // Finish.
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000233 o << "</dict>\n</plist>";
Ted Kremenek5d866252008-11-03 22:33:57 +0000234}