1//===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- 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#include "Internals.h"
11#include "clang/Basic/FileManager.h"
12#include "clang/Basic/SourceManager.h"
13#include "clang/Lex/Lexer.h"
14using namespace clang;
15using namespace arcmt;
16
17// FIXME: This duplicates significant functionality from PlistDiagnostics.cpp,
18// it would be jolly good if there was a reusable PlistWriter or something.
19
20typedef llvm::DenseMap<FileID, unsigned> FIDMap;
21
22static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
23                   const SourceManager &SM, SourceLocation L) {
24
25  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
26  FIDMap::iterator I = FIDs.find(FID);
27  if (I != FIDs.end()) return;
28  FIDs[FID] = V.size();
29  V.push_back(FID);
30}
31
32static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM,
33                       SourceLocation L) {
34  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
35  FIDMap::const_iterator I = FIDs.find(FID);
36  assert(I != FIDs.end());
37  return I->second;
38}
39
40static raw_ostream& Indent(raw_ostream& o, const unsigned indent) {
41  for (unsigned i = 0; i < indent; ++i) o << ' ';
42  return o;
43}
44
45static void EmitLocation(raw_ostream& o, const SourceManager &SM,
46                         const LangOptions &LangOpts,
47                         SourceLocation L, const FIDMap &FM,
48                         unsigned indent, bool extend = false) {
49
50  FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM));
51
52  // Add in the length of the token, so that we cover multi-char tokens.
53  unsigned offset =
54    extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
55
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";
64}
65
66static void EmitRange(raw_ostream& o, const SourceManager &SM,
67                      const LangOptions &LangOpts,
68                      CharSourceRange R, const FIDMap &FM,
69                      unsigned indent) {
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";
74}
75
76static raw_ostream& EmitString(raw_ostream& o,
77                                     StringRef s) {
78  o << "<string>";
79  for (StringRef::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) {
80    char c = *I;
81    switch (c) {
82    default:   o << c; break;
83    case '&':  o << "&amp;"; break;
84    case '<':  o << "&lt;"; break;
85    case '>':  o << "&gt;"; break;
86    case '\'': o << "&apos;"; break;
87    case '\"': o << "&quot;"; break;
88    }
89  }
90  o << "</string>";
91  return o;
92}
93
94void arcmt::writeARCDiagsToPlist(const std::string &outPath,
95                                 ArrayRef<StoredDiagnostic> diags,
96                                 SourceManager &SM,
97                                 const LangOptions &LangOpts) {
98  DiagnosticIDs DiagIDs;
99
100  // Build up a set of FIDs that we use by scanning the locations and
101  // ranges of the diagnostics.
102  FIDMap FM;
103  SmallVector<FileID, 10> Fids;
104
105  for (ArrayRef<StoredDiagnostic>::iterator
106         I = diags.begin(), E = diags.end(); I != E; ++I) {
107    const StoredDiagnostic &D = *I;
108
109    AddFID(FM, Fids, SM, D.getLocation());
110
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());
115    }
116  }
117
118  std::string errMsg;
119  llvm::raw_fd_ostream o(outPath.c_str(), errMsg);
120  if (!errMsg.empty()) {
121    llvm::errs() << "error: could not create file: " << outPath << '\n';
122    return;
123  }
124
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";
130
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
134  o << "<dict>\n"
135       " <key>files</key>\n"
136       " <array>\n";
137
138  for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end();
139       I!=E; ++I) {
140    o << "  ";
141    EmitString(o, SM.getFileEntryForID(*I)->getName()) << '\n';
142  }
143
144  o << " </array>\n"
145       " <key>diagnostics</key>\n"
146       " <array>\n";
147
148  for (ArrayRef<StoredDiagnostic>::iterator
149         DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) {
150
151    const StoredDiagnostic &D = *DI;
152
153    if (D.getLevel() == DiagnosticsEngine::Ignored)
154      continue;
155
156    o << "  <dict>\n";
157
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';
169    else
170      EmitString(o, "note") << '\n';
171
172    // Output the location of the bug.
173    o << "  <key>location</key>\n";
174    EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2);
175
176    // Output the ranges (if any).
177    StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end();
178
179    if (RI != RE) {
180      o << "   <key>ranges</key>\n";
181      o << "   <array>\n";
182      for (; RI != RE; ++RI)
183        EmitRange(o, SM, LangOpts, *RI, FM, 4);
184      o << "   </array>\n";
185    }
186
187    // Close up the entry.
188    o << "  </dict>\n";
189  }
190
191  o << " </array>\n";
192
193  // Finish.
194  o << "</dict>\n</plist>";
195}
196