-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
JsonImpl.hpp
267 lines (251 loc) · 8.96 KB
/
JsonImpl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* This file is part of arduino-preprocessor.
*
* Copyright 2017 BCMI LABS SA
*
* arduino-preprocessor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
#pragma once
#include <clang/AST/ASTConsumer.h>
#include <clang/Basic/SourceManager.h>
#include <sstream>
using namespace clang;
using namespace std;
#define JSON_NOEXCEPTION
#include "json.hpp"
#include "utils.hpp"
#include "clang/include/clang/Sema/CodeCompleteConsumer.h"
using json = nlohmann::json;
template<unsigned InternalLen>
inline string encode(const SmallString<InternalLen> &s) {
return s.str().str();
}
inline json encode(const SourceManager &sm, const SourceLocation &loc) {
PresumedLoc presumed = sm.getPresumedLoc(loc);
stringstream pos;
pos << presumed.getLine() << ":" << presumed.getColumn();
std::string filename(presumed.getFilename());
filename = quoteCppString(filename);
return json{
{"file", filename.c_str()},
{"pos", pos.str()}};
}
inline json encode(const SourceManager &sm, const CharSourceRange &range) {
if (range.isInvalid()) {
return json{};
}
return json{
{"begin", encode(sm, range.getBegin())},
{"end", encode(sm, range.getEnd())}};
}
inline json encode(const SourceManager &sm, const FixItHint &hint) {
return json{
{"before_previous", hint.BeforePreviousInsertions},
{"text", hint.CodeToInsert},
{"insert_from", encode(sm, hint.InsertFromRange)},
{"remove", encode(sm, hint.RemoveRange)}};
}
template<typename T>
inline json encode(const SourceManager &sm, const ArrayRef<T> &array) {
json res = json::array();
for (T elem : array) {
res.push_back(encode(sm, elem));
}
return res;
}
inline json encode(const CodeCompletionString *ccs) {
json chunks = json::array();
for (const CodeCompletionString::Chunk &c : *ccs) {
switch (c.Kind) {
case CodeCompletionString::ChunkKind::CK_Colon:
chunks.push_back(json{
{"t", ":"}
});
break;
case CodeCompletionString::ChunkKind::CK_Comma:
chunks.push_back(json{
{"t", ","}
});
break;
case CodeCompletionString::ChunkKind::CK_CurrentParameter:
chunks.push_back(json{
{"current_param", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_Equal:
chunks.push_back(json{
{"t", "="}
});
break;
case CodeCompletionString::ChunkKind::CK_HorizontalSpace:
chunks.push_back(json{
{"t", " "}
});
break;
case CodeCompletionString::ChunkKind::CK_Informative:
chunks.push_back(json{
{"info", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_LeftAngle:
chunks.push_back(json{
{"t", "<"}
});
break;
case CodeCompletionString::ChunkKind::CK_LeftBrace:
chunks.push_back(json{
{"t", "{"}
});
break;
case CodeCompletionString::ChunkKind::CK_LeftBracket:
chunks.push_back(json{
{"t", "["}
});
break;
case CodeCompletionString::ChunkKind::CK_LeftParen:
chunks.push_back(json{
{"t", "("}
});
break;
case CodeCompletionString::ChunkKind::CK_Optional:
chunks.push_back(json{
{"optional", encode(c.Optional)}
});
break;
case CodeCompletionString::ChunkKind::CK_Placeholder:
chunks.push_back(json{
{"placeholder", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_ResultType:
chunks.push_back(json{
{"res", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_RightAngle:
chunks.push_back(json{
{"t", ">"}
});
break;
case CodeCompletionString::ChunkKind::CK_RightBrace:
chunks.push_back(json{
{"t", "}"}
});
break;
case CodeCompletionString::ChunkKind::CK_RightBracket:
chunks.push_back(json{
{"t", "]"}
});
break;
case CodeCompletionString::ChunkKind::CK_RightParen:
chunks.push_back(json{
{"t", ")"}
});
break;
case CodeCompletionString::ChunkKind::CK_SemiColon:
chunks.push_back(json{
{"t", ";"}
});
break;
case CodeCompletionString::ChunkKind::CK_Text:
chunks.push_back(json{
{"t", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_TypedText:
chunks.push_back(json{
{"typedtext", c.Text}
});
break;
case CodeCompletionString::ChunkKind::CK_VerticalSpace:
chunks.push_back(json{
{"t", "\n"}
});
break;
};
}
json res = json::object();
res["chunks"] = chunks;
// This seems to be redundant
//if (ccs->getTypedText()) {
// res["typedtext"] = ccs->getTypedText();
//}
if (ccs->getBriefComment()) {
res["brief"] = ccs->getBriefComment();
}
return res;
}
inline json encode(const CodeCompletionResult &cc, const CodeCompletionString *ccs, const SourceManager &sm) {
json res = json{
{"completion", encode(ccs)}
};
/* XXX: This makes a memory corruption and core-dumps (at least on Windows), something to check...
// Extract doxygen comment if available
ASTContext& ast = cc.Declaration->getASTContext();
RawComment* comment = ast.getRawCommentForDeclNoCache(cc.Declaration);
if (comment) {
res["doxy"] = comment->getRawText(sm).str();
}
*/
switch (cc.Kind) {
case CodeCompletionResult::RK_Declaration:
{
SourceLocation loc = cc.Declaration->getLocation();
PresumedLoc presumedLoc = sm.getPresumedLoc(loc);
std::string filename(presumedLoc.getFilename());
filename = quoteCppString(filename);
res["location"] = filename.c_str();
res["type"] = cc.Declaration->getDeclKindName();
// For each parameter extract type and name
if (const FunctionDecl * d = dyn_cast<FunctionDecl>(cc.Declaration)) {
json p = json::array();
ArrayRef<ParmVarDecl*> params = d->parameters();
for (size_t i = 0; i < params.size(); i++) {
p.push_back(json{
{"type", params[i]->getType().getAsString()},
{"name", params[i]->getNameAsString()},
});
}
res["parameters"] = p;
}
break;
}
case CodeCompletionResult::RK_Keyword:
{
res["type"] = "Keyword";
break;
}
case CodeCompletionResult::RK_Pattern:
{
res["type"] = "Pattern";
break;
}
case CodeCompletionResult::RK_Macro:
{
res["type"] = "Macro";
break;
}
};
return res;
}