From: Alp Toker Date: Sun, 6 Jul 2014 04:26:52 +0000 (+0000) Subject: Use PlistSupport in a few more places X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e62c5d94500d514bd3683e367cf4839d39ac7f22;p=clang Use PlistSupport in a few more places Switch over LogDiagnosticPrinter and introduce an integer helper. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@212384 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/PlistSupport.h b/include/clang/Basic/PlistSupport.h index 64853f932a..d5a347b33f 100644 --- a/include/clang/Basic/PlistSupport.h +++ b/include/clang/Basic/PlistSupport.h @@ -25,8 +25,8 @@ static const char *PlistHeader = "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" "\n"; -static void AddFID(FIDMap &FIDs, SmallVectorImpl &V, - const SourceManager &SM, SourceLocation L) { +static inline 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()) @@ -35,51 +35,28 @@ static void AddFID(FIDMap &FIDs, SmallVectorImpl &V, V.push_back(FID); } -static unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM, - SourceLocation L) { +static inline 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) { +static inline 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 inline 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 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) { + o << ""; + o << value; + o << ""; + return o; } -static raw_ostream &EmitString(raw_ostream &o, StringRef s) { +static inline 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; @@ -107,6 +84,35 @@ static raw_ostream &EmitString(raw_ostream &o, StringRef s) { o << ""; return o; } + +static inline 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"; + EmitInteger(o, Loc.getExpansionLineNumber()) << '\n'; + Indent(o, indent) << " col"; + EmitInteger(o, Loc.getExpansionColumnNumber() + offset) << '\n'; + Indent(o, indent) << " file"; + EmitInteger(o, GetFID(FM, SM, Loc)) << '\n'; + Indent(o, indent) << "\n"; +} + +static inline 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"; +} } } diff --git a/include/clang/Frontend/LogDiagnosticPrinter.h b/include/clang/Frontend/LogDiagnosticPrinter.h index 4ef643e2ef..0130319870 100644 --- a/include/clang/Frontend/LogDiagnosticPrinter.h +++ b/include/clang/Frontend/LogDiagnosticPrinter.h @@ -39,7 +39,10 @@ class LogDiagnosticPrinter : public DiagnosticConsumer { /// The level of the diagnostic. DiagnosticsEngine::Level DiagnosticLevel; }; - + + void EmitDiagEntry(llvm::raw_ostream &OS, + const LogDiagnosticPrinter::DiagEntry &DE); + raw_ostream &OS; const LangOptions *LangOpts; IntrusiveRefCntPtr DiagOpts; diff --git a/lib/Frontend/LogDiagnosticPrinter.cpp b/lib/Frontend/LogDiagnosticPrinter.cpp index 917c13feb7..19539e0a5e 100644 --- a/lib/Frontend/LogDiagnosticPrinter.cpp +++ b/lib/Frontend/LogDiagnosticPrinter.cpp @@ -10,11 +10,13 @@ #include "clang/Frontend/LogDiagnosticPrinter.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/FileManager.h" +#include "clang/Basic/PlistSupport.h" #include "clang/Basic/SourceManager.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" using namespace clang; +using namespace markup; LogDiagnosticPrinter::LogDiagnosticPrinter(raw_ostream &os, DiagnosticOptions *diags, @@ -40,19 +42,34 @@ static StringRef getLevelName(DiagnosticsEngine::Level Level) { llvm_unreachable("Invalid DiagnosticsEngine level!"); } -// Escape XML characters inside the raw string. -static void emitString(llvm::raw_svector_ostream &OS, const StringRef Raw) { - for (StringRef::iterator I = Raw.begin(), E = Raw.end(); I != E; ++I) { - char c = *I; - switch (c) { - default: OS << c; break; - case '&': OS << "&"; break; - case '<': OS << "<"; break; - case '>': OS << ">"; break; - case '\'': OS << "'"; break; - case '\"': OS << """; break; - } +void +LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS, + const LogDiagnosticPrinter::DiagEntry &DE) { + OS << " \n"; + OS << " level\n" + << " "; + EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n'; + if (!DE.Filename.empty()) { + OS << " filename\n" + << " "; + EmitString(OS, DE.Filename) << '\n'; + } + if (DE.Line != 0) { + OS << " line\n" + << " "; + EmitInteger(OS, DE.Line) << '\n'; + } + if (DE.Column != 0) { + OS << " column\n" + << " "; + EmitInteger(OS, DE.Column) << '\n'; } + if (!DE.Message.empty()) { + OS << " message\n" + << " "; + EmitString(OS, DE.Message) << '\n'; + } + OS << " \n"; } void LogDiagnosticPrinter::EndSourceFile() { @@ -72,48 +89,18 @@ void LogDiagnosticPrinter::EndSourceFile() { OS << "\n"; if (!MainFilename.empty()) { OS << " main-file\n" - << " "; - emitString(OS, MainFilename); - OS << "\n"; + << " "; + EmitString(OS, MainFilename) << '\n'; } if (!DwarfDebugFlags.empty()) { OS << " dwarf-debug-flags\n" - << " "; - emitString(OS, DwarfDebugFlags); - OS << "\n"; + << " "; + EmitString(OS, DwarfDebugFlags) << '\n'; } OS << " diagnostics\n"; OS << " \n"; - for (unsigned i = 0, e = Entries.size(); i != e; ++i) { - DiagEntry &DE = Entries[i]; - - OS << " \n"; - OS << " level\n" - << " "; - emitString(OS, getLevelName(DE.DiagnosticLevel)); - OS << "\n"; - if (!DE.Filename.empty()) { - OS << " filename\n" - << " "; - emitString(OS, DE.Filename); - OS << "\n"; - } - if (DE.Line != 0) { - OS << " line\n" - << " " << DE.Line << "\n"; - } - if (DE.Column != 0) { - OS << " column\n" - << " " << DE.Column << "\n"; - } - if (!DE.Message.empty()) { - OS << " message\n" - << " "; - emitString(OS, DE.Message); - OS << "\n"; - } - OS << " \n"; - } + for (auto &DE : Entries) + EmitDiagEntry(OS, DE); OS << " \n"; OS << "\n"; diff --git a/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp b/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp index 538a750c20..9b7cc51882 100644 --- a/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ b/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -173,8 +173,8 @@ static void ReportEvent(raw_ostream &o, const PathDiagnosticPiece& P, } // Output the call depth. - Indent(o, indent) << "depth" - << "" << depth << "\n"; + Indent(o, indent) << "depth"; + EmitInteger(o, depth) << '\n'; // Output the text. assert(!P.getString().empty());