From 222e2a7620e6520ffaf4fc4e69d79c18da31542e Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Mon, 24 Sep 2012 09:58:17 +1000 Subject: Add the clang library to the repo (with some of my changes, too). --- clang/lib/ARCMigrate/PlistReporter.cpp | 195 +++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 clang/lib/ARCMigrate/PlistReporter.cpp (limited to 'clang/lib/ARCMigrate/PlistReporter.cpp') diff --git a/clang/lib/ARCMigrate/PlistReporter.cpp b/clang/lib/ARCMigrate/PlistReporter.cpp new file mode 100644 index 0000000..d1bc90f --- /dev/null +++ b/clang/lib/ARCMigrate/PlistReporter.cpp @@ -0,0 +1,195 @@ +//===--- 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/Lex/Lexer.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/FileManager.h" +using namespace clang; +using namespace arcmt; + +// FIXME: This duplicates significant functionality from PlistDiagnostics.cpp, +// it would be jolly good if there was a reusable PlistWriter or something. + +typedef llvm::DenseMap FIDMap; + +static void AddFID(FIDMap &FIDs, SmallVectorImpl &V, + const SourceManager &SM, SourceLocation L) { + + FileID FID = SM.getFileID(SM.getExpansionLoc(L)); + FIDMap::iterator I = FIDs.find(FID); + if (I != FIDs.end()) return; + FIDs[FID] = V.size(); + V.push_back(FID); +} + +static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, + SourceLocation L) { + FileID FID = SM.getFileID(SM.getExpansionLoc(L)); + FIDMap::const_iterator I = FIDs.find(FID); + assert(I != FIDs.end()); + return I->second; +} + +static raw_ostream& Indent(raw_ostream& o, const unsigned indent) { + for (unsigned i = 0; i < indent; ++i) o << ' '; + return o; +} + +static void EmitLocation(raw_ostream& o, const SourceManager &SM, + const LangOptions &LangOpts, + SourceLocation L, const FIDMap &FM, + unsigned indent, bool extend = false) { + + FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast(SM)); + + // Add in the length of the token, so that we cover multi-char tokens. + unsigned offset = + extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; + + Indent(o, indent) << "\n"; + Indent(o, indent) << " line" + << Loc.getExpansionLineNumber() << "\n"; + Indent(o, indent) << " col" + << Loc.getExpansionColumnNumber() + offset << "\n"; + Indent(o, indent) << " file" + << GetFID(FM, SM, Loc) << "\n"; + Indent(o, indent) << "\n"; +} + +static void EmitRange(raw_ostream& o, const SourceManager &SM, + const LangOptions &LangOpts, + CharSourceRange R, const FIDMap &FM, + unsigned indent) { + Indent(o, indent) << "\n"; + EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); + EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, R.isTokenRange()); + Indent(o, indent) << "\n"; +} + +static raw_ostream& EmitString(raw_ostream& o, + StringRef s) { + o << ""; + for (StringRef::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) { + char c = *I; + switch (c) { + default: o << c; break; + case '&': o << "&"; break; + case '<': o << "<"; break; + case '>': o << ">"; break; + case '\'': o << "'"; break; + case '\"': o << """; break; + } + } + o << ""; + return o; +} + +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); + if (!errMsg.empty()) { + llvm::errs() << "error: could not create file: " << outPath << '\n'; + return; + } + + // Write the plist header. + o << "\n" + "\n" + "\n"; + + // 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 (SmallVectorImpl::iterator I=Fids.begin(), E=Fids.end(); + I!=E; ++I) { + o << " "; + EmitString(o, SM.getFileEntryForID(*I)->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"; + if (D.getLevel() >= DiagnosticsEngine::Error) + EmitString(o, "error") << '\n'; + else if (D.getLevel() == DiagnosticsEngine::Warning) + EmitString(o, "warning") << '\n'; + else + EmitString(o, "note") << '\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"; +} -- cgit v1.2.3