From 5a300033f56e4d7b3a4cd0099398d45356c69916 Mon Sep 17 00:00:00 2001 From: Brian Croom Date: Thu, 23 Jun 2016 07:37:47 -0400 Subject: [PATCH] [SourceKit] Share common object printer code This is an attempt to clean up code duplication around printing SourceKit request and response objects. --- .../sourcekitd/RequestResponsePrinterBase.h | 110 ++++++++++++++++++ .../lib/API/sourcekitdAPI-Common.cpp | 74 +----------- .../sourcekitd/lib/API/sourcekitdAPI-XPC.cpp | 59 +--------- 3 files changed, 120 insertions(+), 123 deletions(-) create mode 100644 tools/SourceKit/tools/sourcekitd/include/sourcekitd/RequestResponsePrinterBase.h diff --git a/tools/SourceKit/tools/sourcekitd/include/sourcekitd/RequestResponsePrinterBase.h b/tools/SourceKit/tools/sourcekitd/include/sourcekitd/RequestResponsePrinterBase.h new file mode 100644 index 0000000000000..ec1b7834f670b --- /dev/null +++ b/tools/SourceKit/tools/sourcekitd/include/sourcekitd/RequestResponsePrinterBase.h @@ -0,0 +1,110 @@ +//===--- RequestResponsePrinterBase.h - -------------------------*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H +#define LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H + +#include "sourcekitd/sourcekitd.h" +#include "sourcekitd/Logging.h" +#include + +namespace llvm { + class StringRef; + template class ArrayRef; + class raw_ostream; +} +namespace SourceKit { + class UIdent; +} + +namespace sourcekitd { + +template +class RequestResponsePrinterBase { + llvm::raw_ostream &OS; + unsigned Indent; + bool PrintAsJSON; +public: + typedef std::vector> DictMap; + + RequestResponsePrinterBase(llvm::raw_ostream &OS, unsigned Indent = 0, + bool PrintAsJSON = false) + : OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { } + + void visitNull() { + OS << "<>"; + } + + void visitDictionary(const DictMap &Map) { + OS << "{\n"; + Indent += 2; + for (unsigned i = 0, e = Map.size(); i != e; ++i) { + auto &Pair = Map[i]; + OS.indent(Indent); + if (PrintAsJSON) { + visitString(Pair.first.getName()); + } else { + OSColor(OS, DictKeyColor) << Pair.first.getName(); + } + OS << ": "; + static_cast(this)->visit(Pair.second); + if (i < e-1) + OS << ','; + OS << '\n'; + } + Indent -= 2; + OS.indent(Indent) << '}'; + } + + void visitArray(llvm::ArrayRef Arr) { + OS << "[\n"; + Indent += 2; + for (unsigned i = 0, e = Arr.size(); i != e; ++i) { + auto Obj = Arr[i]; + OS.indent(Indent); + static_cast(this)->visit(Obj); + if (i < e-1) + OS << ','; + OS << '\n'; + } + Indent -= 2; + OS.indent(Indent) << ']'; + } + + void visitInt64(int64_t Val) { + OS << Val; + } + + void visitBool(bool Val) { + OS << Val; + } + + void visitString(llvm::StringRef Str) { + OS << '\"'; + // Avoid raw_ostream's write_escaped, we don't want to escape unicode + // characters because it will be invalid JSON. + writeEscaped(Str, OS); + OS << '\"'; + } + + void visitUID(llvm::StringRef UID) { + if (PrintAsJSON) { + visitString(UID); + } else { + OSColor(OS, UIDColor) << UID; + } + } +}; + +} + +#endif diff --git a/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp b/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp index d0ddeaa04552a..f26735b352c92 100644 --- a/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp +++ b/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp @@ -13,6 +13,7 @@ #include "DictionaryKeys.h" #include "sourcekitd/Internal.h" #include "sourcekitd/Logging.h" +#include "sourcekitd/RequestResponsePrinterBase.h" #include "SourceKit/Support/Logging.h" #include "SourceKit/Support/UIdent.h" #include "llvm/ADT/ArrayRef.h" @@ -298,77 +299,12 @@ class VariantVisitor { } }; -class VariantPrinter : public VariantVisitor { - raw_ostream &OS; - unsigned Indent; - bool PrintAsJSON; +class VariantPrinter : public VariantVisitor, + public RequestResponsePrinterBase { public: VariantPrinter(raw_ostream &OS, unsigned Indent = 0, bool PrintAsJSON = false) - : OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { } - - void visitNull() { - OS << "<>"; - } - - void visitDictionary(const DictMap &Map) { - OS << "{\n"; - Indent += 2; - for (unsigned i = 0, e = Map.size(); i != e; ++i) { - auto &Pair = Map[i]; - OS.indent(Indent); - if (PrintAsJSON) { - visitString(Pair.first.getName()); - } else { - OSColor(OS, DictKeyColor) << Pair.first.getName(); - } - OS << ": "; - VariantPrinter(OS, Indent, PrintAsJSON).visit(Pair.second); - if (i < e-1) - OS << ','; - OS << '\n'; - } - Indent -= 2; - OS.indent(Indent) << '}'; - } - - void visitArray(ArrayRef Arr) { - OS << "[\n"; - Indent += 2; - for (unsigned i = 0, e = Arr.size(); i != e; ++i) { - auto Obj = Arr[i]; - OS.indent(Indent); - VariantPrinter(OS, Indent, PrintAsJSON).visit(Obj); - if (i < e-1) - OS << ','; - OS << '\n'; - } - Indent -= 2; - OS.indent(Indent) << ']'; - } - - void visitInt64(int64_t Val) { - OS << Val; - } - - void visitBool(bool Val) { - OS << Val; - } - - void visitString(StringRef Str) { - OS << '\"'; - // Avoid raw_ostream's write_escaped, we don't want to escape unicode - // characters because it will be invalid JSON. - writeEscaped(Str, OS); - OS << '\"'; - } - - void visitUID(StringRef UID) { - if (PrintAsJSON) { - visitString(UID); - } else { - OSColor(OS, UIDColor) << UID; - } - } + : RequestResponsePrinterBase(OS, Indent, PrintAsJSON) { } }; } diff --git a/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp b/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp index 792109057c81b..f3f413ffa9a75 100644 --- a/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp +++ b/tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp @@ -14,13 +14,12 @@ #include "sourcekitd/CodeCompletionResultsArray.h" #include "sourcekitd/DocSupportAnnotationArray.h" #include "sourcekitd/TokenAnnotationsArray.h" -#include "sourcekitd/Logging.h" +#include "sourcekitd/RequestResponsePrinterBase.h" #include "SourceKit/Support/UIdent.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MemoryBuffer.h" -#include #include #include @@ -137,60 +136,12 @@ class SKDObjectVisitor { } }; -class SKDObjectPrinter : public SKDObjectVisitor { - raw_ostream &OS; - unsigned Indent; +class SKDObjectPrinter : public SKDObjectVisitor, + public RequestResponsePrinterBase { public: SKDObjectPrinter(raw_ostream &OS, unsigned Indent = 0) - : OS(OS), Indent(Indent) { } - - void visitDictionary(const DictMap &Map) { - OS << "{\n"; - Indent += 2; - for (unsigned i = 0, e = Map.size(); i != e; ++i) { - auto &Pair = Map[i]; - OS.indent(Indent); - OSColor(OS, DictKeyColor) << Pair.first.getName(); - OS << ": "; - SKDObjectPrinter(OS, Indent).visit(Pair.second); - if (i < e-1) - OS << ','; - OS << '\n'; - } - Indent -= 2; - OS.indent(Indent) << '}'; - } - - void visitArray(ArrayRef Arr) { - OS << "[\n"; - Indent += 2; - for (unsigned i = 0, e = Arr.size(); i != e; ++i) { - auto Obj = Arr[i]; - OS.indent(Indent); - SKDObjectPrinter(OS, Indent).visit(Obj); - if (i < e-1) - OS << ','; - OS << '\n'; - } - Indent -= 2; - OS.indent(Indent) << ']'; - } - - void visitInt64(int64_t Val) { - OS << Val; - } - - void visitString(StringRef Str) { - OS << '\"'; - // Avoid raw_ostream's write_escaped, we don't want to escape unicode - // characters because it will be invalid JSON. - writeEscaped(Str, OS); - OS << '\"'; - } - - void visitUID(StringRef UID) { - OSColor(OS, UIDColor) << UID; - } + : RequestResponsePrinterBase(OS, Indent) { } }; } // anonymous namespace.