forked from ffevotte/clang-tags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cxx
300 lines (252 loc) · 7.71 KB
/
main.cxx
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "application.hxx"
#include "util/util.hxx"
#include "request/request.hxx"
#include "getopt++/getopt.hxx"
#include <boost/asio.hpp>
class CompilationDatabaseCommand : public Request::CommandParser {
public:
CompilationDatabaseCommand (const std::string & name, Application & application)
: Request::CommandParser (name, "Read a compilation database"),
application_ (application)
{
prompt_ = "load> ";
defaults();
using Request::key;
add (key ("database", args_.fileName)
->metavar ("FILEPATH")
->description ("Load compilation commands from a JSON compilation database"));
}
void defaults () {
args_.fileName = "compile_commands.json";
}
void run (std::ostream & cout) {
application_.compilationDatabase (args_, cout);
}
private:
Application & application_;
Application::CompilationDatabaseArgs args_;
};
class UpdateCommand : public Request::CommandParser {
public:
UpdateCommand (const std::string & name, Application & application)
: Request::CommandParser (name, "Update the source code index"),
application_ (application)
{
prompt_ = "update> ";
defaults();
using Request::key;
add (key ("diagnostics", args_.diagnostics)
->metavar ("true|false")
->description ("Print compilation diagnostics"));
}
void defaults () {
args_.diagnostics = true;
}
void run (std::ostream & cout) {
application_.update (args_, cout);
}
protected:
Application & application_;
Application::IndexArgs args_;
};
class IndexCommand : public UpdateCommand {
public:
IndexCommand (const std::string & name, Application & application)
: UpdateCommand (name, application)
{
setDescription ("Index the source code base");
prompt_ = "index> ";
defaults ();
using Request::key;
add (key ("exclude", args_.exclude)
->metavar ("PATH")
->description ("Exclude path"));
}
void defaults () {
UpdateCommand::defaults();
args_.exclude = {"/usr"};
}
void run (std::ostream & cout) {
application_.index (args_, cout);
}
};
class FindCommand : public Request::CommandParser {
public:
FindCommand (const std::string & name, Application & application)
: Request::CommandParser (name, "Find the definition of a symbol"),
application_ (application)
{
prompt_ = "find> ";
defaults ();
using Request::key;
add (key ("file", args_.fileName)
->metavar ("FILENAME")
->description ("Source file name"));
add (key ("offset", args_.offset)
->metavar ("OFFSET")
->description ("Offset in bytes"));
add (key ("mostSpecific", args_.mostSpecific)
->metavar ("true|false")
->description ("Display only the most specific identifier at this location"));
add (key ("diagnostics", args_.diagnostics)
->metavar ("true|false")
->description ("Print compilation diagnostics"));
add (key ("fromIndex", args_.fromIndex)
->metavar ("true|false")
->description ("Search in the index (faster but potentially out-of-date)"));
}
void defaults () {
args_.fileName = "";
args_.offset = 0;
args_.mostSpecific = false;
args_.diagnostics = true;
args_.fromIndex = true;
}
void run (std::ostream & cout) {
application_.findDefinition (args_, cout);
}
private:
Application & application_;
Application::FindDefinitionArgs args_;
};
class GrepCommand : public Request::CommandParser {
public:
GrepCommand (const std::string & name, Application & application)
: Request::CommandParser (name, "Find all references to a definition"),
application_ (application)
{
prompt_ = "grep> ";
defaults();
using Request::key;
add (key ("usr", args_.usr)
->metavar ("USR")
->description ("Unified Symbol Resolution for the symbol"));
}
void defaults () {
args_.usr = "c:@F@main";
}
void run (std::ostream & cout) {
application_.grep (args_, cout);
}
private:
Application & application_;
Application::GrepArgs args_;
};
class CompleteCommand : public Request::CommandParser {
public:
CompleteCommand (const std::string & name, Application & application)
: Request::CommandParser (name, "Complete the code at point"),
application_ (application)
{
prompt_ = "complete> ";
defaults();
using Request::key;
add (key ("file", args_.fileName)
->metavar ("FILENAME")
->description ("Source file name"));
add (key ("line", args_.line)
->metavar ("LINE_NO")
->description ("Line number (counting from 0)"));
add (key ("column", args_.column)
->metavar ("COLUMN_NO")
->description ("Column number (counting from 0)"));
}
void defaults () {
args_.fileName = "";
args_.line = 0;
args_.column = 0;
}
void run (std::ostream & cout) {
application_.complete (args_, cout);
}
private:
Application & application_;
Application::CompleteArgs args_;
};
struct ExitCommand : public Request::CommandParser {
ExitCommand (const std::string & name)
: Request::CommandParser (name, "Shutdown server")
{
prompt_ = "exit> ";
}
void run (std::ostream & cout) {
cout << "Exiting..." << std::endl;
throw std::runtime_error ("shutdown requested");
}
};
int main (int argc, char **argv) {
Getopt options (argc, argv);
options.add ("help", 'h', 0,
"print this help message and exit");
options.add ("stdin", 's', 0,
"read a request from the standard input and exit");
options.add ("cachesize", 'l', 1,
"specify the maximum size of the translation unit cache (in MB)");
try {
options.get();
} catch (...) {
std::cerr << options.usage();
return 1;
}
if (options.getCount ("help") > 0) {
std::cerr << options.usage();
return 0;
}
// Default to a cache of 1GB.
unsigned long cacheLimit = 1024;
if (options.getCount ("cachesize") > 0) {
try {
cacheLimit = std::stoul(options["cachesize"]);
} catch (...) {
std::cerr << "Invalid cachesize value: " << options["cachesize"] << std::endl;
return 1;
}
}
// Convert to bytes from MB.
cacheLimit *= 1024 * 1024;
Storage storage;
Application app (storage, cacheLimit);
Request::Parser p ("Clang-tags server\n");
p .add (new CompilationDatabaseCommand ("load", app))
.add (new IndexCommand ("index", app))
.add (new UpdateCommand ("update", app))
.add (new FindCommand ("find", app))
.add (new GrepCommand ("grep", app))
.add (new CompleteCommand ("complete", app))
.add (new ExitCommand ("exit"))
.prompt ("clang-dde> ");
if (options.getCount ("stdin") > 0) {
p.parseJson (std::cin, std::cout);
}
else {
const std::string pidPath (".ct.pid");
std::ofstream pidFile (pidPath);
pidFile << getpid() << std::endl;
pidFile.close();
std::cerr << "Server starting with pid: " << getpid() << std::endl;
const std::string socketPath (".ct.sock");
try
{
boost::asio::io_service io_service;
boost::asio::local::stream_protocol::endpoint endpoint (socketPath);
boost::asio::local::stream_protocol::acceptor acceptor (io_service, endpoint);
for (;;)
{
boost::asio::local::stream_protocol::iostream socket;
boost::system::error_code err;
acceptor.accept(*socket.rdbuf(), err);
if (!err) {
p.parseJson (socket, socket, /*verbose=*/true);
}
}
}
catch (std::exception& e)
{
std::cerr << std::endl << "Caught exception: " << e.what() << std::endl;
}
std::cerr << "Server exiting..." << std::endl;
unlink (socketPath.c_str());
unlink (pidPath.c_str());
}
return EXIT_SUCCESS;
}