Skip to content

Commit 5a30003

Browse files
committed
[SourceKit] Share common object printer code
This is an attempt to clean up code duplication around printing SourceKit request and response objects.
1 parent a51aa91 commit 5a30003

File tree

3 files changed

+120
-123
lines changed

3 files changed

+120
-123
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//===--- RequestResponsePrinterBase.h - -------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H
14+
#define LLVM_SOURCEKITD_REQUESTRESPONSEPRINTERBASE_H
15+
16+
#include "sourcekitd/sourcekitd.h"
17+
#include "sourcekitd/Logging.h"
18+
#include <vector>
19+
20+
namespace llvm {
21+
class StringRef;
22+
template <typename T> class ArrayRef;
23+
class raw_ostream;
24+
}
25+
namespace SourceKit {
26+
class UIdent;
27+
}
28+
29+
namespace sourcekitd {
30+
31+
template <typename VisitorImplClass, typename VisitedType>
32+
class RequestResponsePrinterBase {
33+
llvm::raw_ostream &OS;
34+
unsigned Indent;
35+
bool PrintAsJSON;
36+
public:
37+
typedef std::vector<std::pair<SourceKit::UIdent, VisitedType>> DictMap;
38+
39+
RequestResponsePrinterBase(llvm::raw_ostream &OS, unsigned Indent = 0,
40+
bool PrintAsJSON = false)
41+
: OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { }
42+
43+
void visitNull() {
44+
OS << "<<NULL>>";
45+
}
46+
47+
void visitDictionary(const DictMap &Map) {
48+
OS << "{\n";
49+
Indent += 2;
50+
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
51+
auto &Pair = Map[i];
52+
OS.indent(Indent);
53+
if (PrintAsJSON) {
54+
visitString(Pair.first.getName());
55+
} else {
56+
OSColor(OS, DictKeyColor) << Pair.first.getName();
57+
}
58+
OS << ": ";
59+
static_cast<VisitorImplClass *>(this)->visit(Pair.second);
60+
if (i < e-1)
61+
OS << ',';
62+
OS << '\n';
63+
}
64+
Indent -= 2;
65+
OS.indent(Indent) << '}';
66+
}
67+
68+
void visitArray(llvm::ArrayRef<VisitedType> Arr) {
69+
OS << "[\n";
70+
Indent += 2;
71+
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
72+
auto Obj = Arr[i];
73+
OS.indent(Indent);
74+
static_cast<VisitorImplClass *>(this)->visit(Obj);
75+
if (i < e-1)
76+
OS << ',';
77+
OS << '\n';
78+
}
79+
Indent -= 2;
80+
OS.indent(Indent) << ']';
81+
}
82+
83+
void visitInt64(int64_t Val) {
84+
OS << Val;
85+
}
86+
87+
void visitBool(bool Val) {
88+
OS << Val;
89+
}
90+
91+
void visitString(llvm::StringRef Str) {
92+
OS << '\"';
93+
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
94+
// characters because it will be invalid JSON.
95+
writeEscaped(Str, OS);
96+
OS << '\"';
97+
}
98+
99+
void visitUID(llvm::StringRef UID) {
100+
if (PrintAsJSON) {
101+
visitString(UID);
102+
} else {
103+
OSColor(OS, UIDColor) << UID;
104+
}
105+
}
106+
};
107+
108+
}
109+
110+
#endif

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "DictionaryKeys.h"
1414
#include "sourcekitd/Internal.h"
1515
#include "sourcekitd/Logging.h"
16+
#include "sourcekitd/RequestResponsePrinterBase.h"
1617
#include "SourceKit/Support/Logging.h"
1718
#include "SourceKit/Support/UIdent.h"
1819
#include "llvm/ADT/ArrayRef.h"
@@ -298,77 +299,12 @@ class VariantVisitor {
298299
}
299300
};
300301

301-
class VariantPrinter : public VariantVisitor<VariantPrinter> {
302-
raw_ostream &OS;
303-
unsigned Indent;
304-
bool PrintAsJSON;
302+
class VariantPrinter : public VariantVisitor<VariantPrinter>,
303+
public RequestResponsePrinterBase<VariantPrinter,
304+
sourcekitd_variant_t> {
305305
public:
306306
VariantPrinter(raw_ostream &OS, unsigned Indent = 0, bool PrintAsJSON = false)
307-
: OS(OS), Indent(Indent), PrintAsJSON(PrintAsJSON) { }
308-
309-
void visitNull() {
310-
OS << "<<NULL>>";
311-
}
312-
313-
void visitDictionary(const DictMap &Map) {
314-
OS << "{\n";
315-
Indent += 2;
316-
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
317-
auto &Pair = Map[i];
318-
OS.indent(Indent);
319-
if (PrintAsJSON) {
320-
visitString(Pair.first.getName());
321-
} else {
322-
OSColor(OS, DictKeyColor) << Pair.first.getName();
323-
}
324-
OS << ": ";
325-
VariantPrinter(OS, Indent, PrintAsJSON).visit(Pair.second);
326-
if (i < e-1)
327-
OS << ',';
328-
OS << '\n';
329-
}
330-
Indent -= 2;
331-
OS.indent(Indent) << '}';
332-
}
333-
334-
void visitArray(ArrayRef<sourcekitd_variant_t> Arr) {
335-
OS << "[\n";
336-
Indent += 2;
337-
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
338-
auto Obj = Arr[i];
339-
OS.indent(Indent);
340-
VariantPrinter(OS, Indent, PrintAsJSON).visit(Obj);
341-
if (i < e-1)
342-
OS << ',';
343-
OS << '\n';
344-
}
345-
Indent -= 2;
346-
OS.indent(Indent) << ']';
347-
}
348-
349-
void visitInt64(int64_t Val) {
350-
OS << Val;
351-
}
352-
353-
void visitBool(bool Val) {
354-
OS << Val;
355-
}
356-
357-
void visitString(StringRef Str) {
358-
OS << '\"';
359-
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
360-
// characters because it will be invalid JSON.
361-
writeEscaped(Str, OS);
362-
OS << '\"';
363-
}
364-
365-
void visitUID(StringRef UID) {
366-
if (PrintAsJSON) {
367-
visitString(UID);
368-
} else {
369-
OSColor(OS, UIDColor) << UID;
370-
}
371-
}
307+
: RequestResponsePrinterBase(OS, Indent, PrintAsJSON) { }
372308
};
373309
}
374310

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
#include "sourcekitd/CodeCompletionResultsArray.h"
1515
#include "sourcekitd/DocSupportAnnotationArray.h"
1616
#include "sourcekitd/TokenAnnotationsArray.h"
17-
#include "sourcekitd/Logging.h"
17+
#include "sourcekitd/RequestResponsePrinterBase.h"
1818
#include "SourceKit/Support/UIdent.h"
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/SmallString.h"
2121
#include "llvm/Support/ErrorHandling.h"
2222
#include "llvm/Support/MemoryBuffer.h"
23-
#include <map>
2423
#include <vector>
2524
#include <xpc/xpc.h>
2625

@@ -137,60 +136,12 @@ class SKDObjectVisitor {
137136
}
138137
};
139138

140-
class SKDObjectPrinter : public SKDObjectVisitor<SKDObjectPrinter> {
141-
raw_ostream &OS;
142-
unsigned Indent;
139+
class SKDObjectPrinter : public SKDObjectVisitor<SKDObjectPrinter>,
140+
public RequestResponsePrinterBase<SKDObjectPrinter,
141+
sourcekitd_object_t> {
143142
public:
144143
SKDObjectPrinter(raw_ostream &OS, unsigned Indent = 0)
145-
: OS(OS), Indent(Indent) { }
146-
147-
void visitDictionary(const DictMap &Map) {
148-
OS << "{\n";
149-
Indent += 2;
150-
for (unsigned i = 0, e = Map.size(); i != e; ++i) {
151-
auto &Pair = Map[i];
152-
OS.indent(Indent);
153-
OSColor(OS, DictKeyColor) << Pair.first.getName();
154-
OS << ": ";
155-
SKDObjectPrinter(OS, Indent).visit(Pair.second);
156-
if (i < e-1)
157-
OS << ',';
158-
OS << '\n';
159-
}
160-
Indent -= 2;
161-
OS.indent(Indent) << '}';
162-
}
163-
164-
void visitArray(ArrayRef<sourcekitd_object_t> Arr) {
165-
OS << "[\n";
166-
Indent += 2;
167-
for (unsigned i = 0, e = Arr.size(); i != e; ++i) {
168-
auto Obj = Arr[i];
169-
OS.indent(Indent);
170-
SKDObjectPrinter(OS, Indent).visit(Obj);
171-
if (i < e-1)
172-
OS << ',';
173-
OS << '\n';
174-
}
175-
Indent -= 2;
176-
OS.indent(Indent) << ']';
177-
}
178-
179-
void visitInt64(int64_t Val) {
180-
OS << Val;
181-
}
182-
183-
void visitString(StringRef Str) {
184-
OS << '\"';
185-
// Avoid raw_ostream's write_escaped, we don't want to escape unicode
186-
// characters because it will be invalid JSON.
187-
writeEscaped(Str, OS);
188-
OS << '\"';
189-
}
190-
191-
void visitUID(StringRef UID) {
192-
OSColor(OS, UIDColor) << UID;
193-
}
144+
: RequestResponsePrinterBase(OS, Indent) { }
194145
};
195146

196147
} // anonymous namespace.

0 commit comments

Comments
 (0)