clang API Documentation
00001 //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "Internals.h" 00011 #include "clang/Lex/Lexer.h" 00012 #include "clang/Basic/SourceManager.h" 00013 #include "clang/Basic/FileManager.h" 00014 using namespace clang; 00015 using namespace arcmt; 00016 00017 // FIXME: This duplicates significant functionality from PlistDiagnostics.cpp, 00018 // it would be jolly good if there was a reusable PlistWriter or something. 00019 00020 typedef llvm::DenseMap<FileID, unsigned> FIDMap; 00021 00022 static void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V, 00023 const SourceManager &SM, SourceLocation L) { 00024 00025 FileID FID = SM.getFileID(SM.getExpansionLoc(L)); 00026 FIDMap::iterator I = FIDs.find(FID); 00027 if (I != FIDs.end()) return; 00028 FIDs[FID] = V.size(); 00029 V.push_back(FID); 00030 } 00031 00032 static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, 00033 SourceLocation L) { 00034 FileID FID = SM.getFileID(SM.getExpansionLoc(L)); 00035 FIDMap::const_iterator I = FIDs.find(FID); 00036 assert(I != FIDs.end()); 00037 return I->second; 00038 } 00039 00040 static raw_ostream& Indent(raw_ostream& o, const unsigned indent) { 00041 for (unsigned i = 0; i < indent; ++i) o << ' '; 00042 return o; 00043 } 00044 00045 static void EmitLocation(raw_ostream& o, const SourceManager &SM, 00046 const LangOptions &LangOpts, 00047 SourceLocation L, const FIDMap &FM, 00048 unsigned indent, bool extend = false) { 00049 00050 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager&>(SM)); 00051 00052 // Add in the length of the token, so that we cover multi-char tokens. 00053 unsigned offset = 00054 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; 00055 00056 Indent(o, indent) << "<dict>\n"; 00057 Indent(o, indent) << " <key>line</key><integer>" 00058 << Loc.getExpansionLineNumber() << "</integer>\n"; 00059 Indent(o, indent) << " <key>col</key><integer>" 00060 << Loc.getExpansionColumnNumber() + offset << "</integer>\n"; 00061 Indent(o, indent) << " <key>file</key><integer>" 00062 << GetFID(FM, SM, Loc) << "</integer>\n"; 00063 Indent(o, indent) << "</dict>\n"; 00064 } 00065 00066 static void EmitRange(raw_ostream& o, const SourceManager &SM, 00067 const LangOptions &LangOpts, 00068 CharSourceRange R, const FIDMap &FM, 00069 unsigned indent) { 00070 Indent(o, indent) << "<array>\n"; 00071 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); 00072 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, R.isTokenRange()); 00073 Indent(o, indent) << "</array>\n"; 00074 } 00075 00076 static raw_ostream& EmitString(raw_ostream& o, 00077 StringRef s) { 00078 o << "<string>"; 00079 for (StringRef::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) { 00080 char c = *I; 00081 switch (c) { 00082 default: o << c; break; 00083 case '&': o << "&"; break; 00084 case '<': o << "<"; break; 00085 case '>': o << ">"; break; 00086 case '\'': o << "'"; break; 00087 case '\"': o << """; break; 00088 } 00089 } 00090 o << "</string>"; 00091 return o; 00092 } 00093 00094 void arcmt::writeARCDiagsToPlist(const std::string &outPath, 00095 ArrayRef<StoredDiagnostic> diags, 00096 SourceManager &SM, 00097 const LangOptions &LangOpts) { 00098 DiagnosticIDs DiagIDs; 00099 00100 // Build up a set of FIDs that we use by scanning the locations and 00101 // ranges of the diagnostics. 00102 FIDMap FM; 00103 SmallVector<FileID, 10> Fids; 00104 00105 for (ArrayRef<StoredDiagnostic>::iterator 00106 I = diags.begin(), E = diags.end(); I != E; ++I) { 00107 const StoredDiagnostic &D = *I; 00108 00109 AddFID(FM, Fids, SM, D.getLocation()); 00110 00111 for (StoredDiagnostic::range_iterator 00112 RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI) { 00113 AddFID(FM, Fids, SM, RI->getBegin()); 00114 AddFID(FM, Fids, SM, RI->getEnd()); 00115 } 00116 } 00117 00118 std::string errMsg; 00119 llvm::raw_fd_ostream o(outPath.c_str(), errMsg); 00120 if (!errMsg.empty()) { 00121 llvm::errs() << "error: could not create file: " << outPath << '\n'; 00122 return; 00123 } 00124 00125 // Write the plist header. 00126 o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 00127 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " 00128 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" 00129 "<plist version=\"1.0\">\n"; 00130 00131 // Write the root object: a <dict> containing... 00132 // - "files", an <array> mapping from FIDs to file names 00133 // - "diagnostics", an <array> containing the diagnostics 00134 o << "<dict>\n" 00135 " <key>files</key>\n" 00136 " <array>\n"; 00137 00138 for (SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); 00139 I!=E; ++I) { 00140 o << " "; 00141 EmitString(o, SM.getFileEntryForID(*I)->getName()) << '\n'; 00142 } 00143 00144 o << " </array>\n" 00145 " <key>diagnostics</key>\n" 00146 " <array>\n"; 00147 00148 for (ArrayRef<StoredDiagnostic>::iterator 00149 DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) { 00150 00151 const StoredDiagnostic &D = *DI; 00152 00153 if (D.getLevel() == DiagnosticsEngine::Ignored) 00154 continue; 00155 00156 o << " <dict>\n"; 00157 00158 // Output the diagnostic. 00159 o << " <key>description</key>"; 00160 EmitString(o, D.getMessage()) << '\n'; 00161 o << " <key>category</key>"; 00162 EmitString(o, DiagIDs.getCategoryNameFromID( 00163 DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n'; 00164 o << " <key>type</key>"; 00165 if (D.getLevel() >= DiagnosticsEngine::Error) 00166 EmitString(o, "error") << '\n'; 00167 else if (D.getLevel() == DiagnosticsEngine::Warning) 00168 EmitString(o, "warning") << '\n'; 00169 else 00170 EmitString(o, "note") << '\n'; 00171 00172 // Output the location of the bug. 00173 o << " <key>location</key>\n"; 00174 EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2); 00175 00176 // Output the ranges (if any). 00177 StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end(); 00178 00179 if (RI != RE) { 00180 o << " <key>ranges</key>\n"; 00181 o << " <array>\n"; 00182 for (; RI != RE; ++RI) 00183 EmitRange(o, SM, LangOpts, *RI, FM, 4); 00184 o << " </array>\n"; 00185 } 00186 00187 // Close up the entry. 00188 o << " </dict>\n"; 00189 } 00190 00191 o << " </array>\n"; 00192 00193 // Finish. 00194 o << "</dict>\n</plist>"; 00195 }