1 //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Internals.h"
11 #include "clang/Lex/Lexer.h"
12 #include "clang/Basic/SourceManager.h"
13 #include "clang/Basic/FileManager.h"
14 using namespace clang;
15 using namespace arcmt;
17 // FIXME: This duplicates significant functionality from PlistDiagnostics.cpp,
18 // it would be jolly good if there was a reusable PlistWriter or something.
20 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
22 static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
23 const SourceManager &SM, SourceLocation L) {
25 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
26 FIDMap::iterator I = FIDs.find(FID);
27 if (I != FIDs.end()) return;
32 static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
34 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
35 FIDMap::const_iterator I = FIDs.find(FID);
36 assert(I != FIDs.end());
40 static raw_ostream& Indent(raw_ostream& o, const unsigned indent) {
41 for (unsigned i = 0; i < indent; ++i) o << ' ';
45 static void EmitLocation(raw_ostream& o, const SourceManager &SM,
46 const LangOptions &LangOpts,
47 SourceLocation L, const FIDMap &FM,
48 unsigned indent, bool extend = false) {
50 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
52 // Add in the length of the token, so that we cover multi-char tokens.
54 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
56 Indent(o, indent) << "<dict>\n";
57 Indent(o, indent) << " <key>line</key><integer>"
58 << Loc.getExpansionLineNumber() << "</integer>\n";
59 Indent(o, indent) << " <key>col</key><integer>"
60 << Loc.getExpansionColumnNumber() + offset << "</integer>\n";
61 Indent(o, indent) << " <key>file</key><integer>"
62 << GetFID(FM, SM, Loc) << "</integer>\n";
63 Indent(o, indent) << "</dict>\n";
66 static void EmitRange(raw_ostream& o, const SourceManager &SM,
67 const LangOptions &LangOpts,
68 CharSourceRange R, const FIDMap &FM,
70 Indent(o, indent) << "<array>\n";
71 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1);
72 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, R.isTokenRange());
73 Indent(o, indent) << "</array>\n";
76 static raw_ostream& EmitString(raw_ostream& o,
79 for (StringRef::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) {
82 default: o << c; break;
83 case '&': o << "&"; break;
84 case '<': o << "<"; break;
85 case '>': o << ">"; break;
86 case '\'': o << "'"; break;
87 case '\"': o << """; break;
94 void arcmt::writeARCDiagsToPlist(const std::string &outPath,
95 ArrayRef<StoredDiagnostic> diags,
97 const LangOptions &LangOpts) {
98 DiagnosticIDs DiagIDs;
100 // Build up a set of FIDs that we use by scanning the locations and
101 // ranges of the diagnostics.
103 SmallVector<FileID, 10> Fids;
105 for (ArrayRef<StoredDiagnostic>::iterator
106 I = diags.begin(), E = diags.end(); I != E; ++I) {
107 const StoredDiagnostic &D = *I;
109 AddFID(FM, Fids, SM, D.getLocation());
111 for (StoredDiagnostic::range_iterator
112 RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI) {
113 AddFID(FM, Fids, SM, RI->getBegin());
114 AddFID(FM, Fids, SM, RI->getEnd());
119 llvm::raw_fd_ostream o(outPath.c_str(), errMsg);
120 if (!errMsg.empty()) {
121 llvm::errs() << "error: could not create file: " << outPath << '\n';
125 // Write the plist header.
126 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
127 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
128 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
129 "<plist version=\"1.0\">\n";
131 // Write the root object: a <dict> containing...
132 // - "files", an <array> mapping from FIDs to file names
133 // - "diagnostics", an <array> containing the diagnostics
135 " <key>files</key>\n"
138 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
141 EmitString(o, SM.getFileEntryForID(*I)->getName()) << '\n';
145 " <key>diagnostics</key>\n"
148 for (ArrayRef<StoredDiagnostic>::iterator
149 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
151 const StoredDiagnostic &D = *DI;
153 if (D.getLevel() == DiagnosticsEngine::Ignored)
158 // Output the diagnostic.
159 o << " <key>description</key>";
160 EmitString(o, D.getMessage()) << '\n';
161 o << " <key>category</key>";
162 EmitString(o, DiagIDs.getCategoryNameFromID(
163 DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n';
164 o << " <key>type</key>";
165 if (D.getLevel() >= DiagnosticsEngine::Error)
166 EmitString(o, "error") << '\n';
167 else if (D.getLevel() == DiagnosticsEngine::Warning)
168 EmitString(o, "warning") << '\n';
170 EmitString(o, "note") << '\n';
172 // Output the location of the bug.
173 o << " <key>location</key>\n";
174 EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2);
176 // Output the ranges (if any).
177 StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end();
180 o << " <key>ranges</key>\n";
182 for (; RI != RE; ++RI)
183 EmitRange(o, SM, LangOpts, *RI, FM, 4);
187 // Close up the entry.
194 o << "</dict>\n</plist>";