//===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "Internals.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/PlistSupport.h" #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" using namespace clang; using namespace arcmt; using namespace markup; static StringRef getLevelName(DiagnosticsEngine::Level Level) { switch (Level) { case DiagnosticsEngine::Ignored: llvm_unreachable("ignored"); case DiagnosticsEngine::Note: return "note"; case DiagnosticsEngine::Remark: case DiagnosticsEngine::Warning: return "warning"; case DiagnosticsEngine::Fatal: case DiagnosticsEngine::Error: return "error"; } llvm_unreachable("Invalid DiagnosticsEngine level!"); } void arcmt::writeARCDiagsToPlist(const std::string &outPath, ArrayRef diags, SourceManager &SM, const LangOptions &LangOpts) { DiagnosticIDs DiagIDs; // Build up a set of FIDs that we use by scanning the locations and // ranges of the diagnostics. FIDMap FM; SmallVector Fids; for (ArrayRef::iterator I = diags.begin(), E = diags.end(); I != E; ++I) { const StoredDiagnostic &D = *I; AddFID(FM, Fids, SM, D.getLocation()); for (StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI) { AddFID(FM, Fids, SM, RI->getBegin()); AddFID(FM, Fids, SM, RI->getEnd()); } } std::string errMsg; llvm::raw_fd_ostream o(outPath.c_str(), errMsg, llvm::sys::fs::F_Text); if (!errMsg.empty()) { llvm::errs() << "error: could not create file: " << outPath << '\n'; return; } EmitPlistHeader(o); // Write the root object: a containing... // - "files", an mapping from FIDs to file names // - "diagnostics", an containing the diagnostics o << "\n" " files\n" " \n"; for (FileID FID : Fids) EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n'; o << " \n" " diagnostics\n" " \n"; for (ArrayRef::iterator DI = diags.begin(), DE = diags.end(); DI != DE; ++DI) { const StoredDiagnostic &D = *DI; if (D.getLevel() == DiagnosticsEngine::Ignored) continue; o << " \n"; // Output the diagnostic. o << " description"; EmitString(o, D.getMessage()) << '\n'; o << " category"; EmitString(o, DiagIDs.getCategoryNameFromID( DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n'; o << " type"; EmitString(o, getLevelName(D.getLevel())) << '\n'; // Output the location of the bug. o << " location\n"; EmitLocation(o, SM, LangOpts, D.getLocation(), FM, 2); // Output the ranges (if any). StoredDiagnostic::range_iterator RI = D.range_begin(), RE = D.range_end(); if (RI != RE) { o << " ranges\n"; o << " \n"; for (; RI != RE; ++RI) EmitRange(o, SM, LangOpts, *RI, FM, 4); o << " \n"; } // Close up the entry. o << " \n"; } o << " \n"; // Finish. o << "\n"; }