This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathcommand_line.cc
More file actions
516 lines (446 loc) · 18.2 KB
/
command_line.cc
File metadata and controls
516 lines (446 loc) · 18.2 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// TODO: cleanup includes
#include "cache_manager.h"
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "diagnostics_engine.h"
#include "file_consumer.h"
#include "import_manager.h"
#include "import_pipeline.h"
#include "include_complete.h"
#include "indexer.h"
#include "lex_utils.h"
#include "lru_cache.h"
#include "lsp_diagnostic.h"
#include "match.h"
#include "message_handler.h"
#include "options.h"
#include "platform.h"
#include "project.h"
#include "query.h"
#include "query_utils.h"
#include "queue_manager.h"
#include "recorder.h"
#include "semantic_highlight_symbol_cache.h"
#include "serializer.h"
#include "serializers/json.h"
#include "test.h"
#include "timer.h"
#include "timestamp_manager.h"
#include "work_thread.h"
#include "working_files.h"
#include <doctest/doctest.h>
#include <rapidjson/error/en.h>
#include <loguru.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
// TODO: provide a feature like 'https://github.com/goldsborough/clang-expand',
// ie, a fully linear view of a function with inline function calls expanded.
// We can probably use vscode decorators to achieve it.
// TODO: implement ThreadPool type which monitors CPU usage / number of work
// items per second completed and scales up/down number of running threads.
std::string g_init_options;
Config* g_config;
namespace {
std::vector<std::string> kEmptyArgs;
// This function returns true if e2e timing should be displayed for the given
// MethodId.
bool ShouldDisplayMethodTiming(MethodType type) {
return type != kMethodType_TextDocumentPublishDiagnostics &&
type != kMethodType_CqueryPublishInactiveRegions &&
type != kMethodType_CqueryQueryDbStatus && type != kMethodType_Unknown;
}
void PrintHelp() {
std::cout << R"help(cquery is a low-latency C/C++/Objective-C language server.
Mode:
--check <path>
Load the project and try to create a translation unit for path.
This can be used to quickly test to see if your project
configuration will work. The current directory is used as the
project directory.
--test-unit Run unit tests.
--test-index <opt_filter_path>
Run index tests. opt_filter_path can be used to specify which
test to run (ie, "foo" will run all tests which contain "foo"
in the path). If not provided all tests are run.
(default if no other mode is specified)
Run as a language server over stdin and stdout
Other command line options:
--init <initializationOptions>
Override client provided initialization options
https://github.com/cquery-project/cquery/wiki/Initialization-options
--record <path>
Writes stdin to <path>.in and stdout to <path>.out
--log-file <path>
Logging file for diagnostics
--log-file-append <path>
Like --log-file, but appending
--log-all-to-stderr
Write all log messages to STDERR.
--wait-for-input
Wait for an '[Enter]' before exiting
--help Print this help information.
--ci Prevents tests from prompting the user for input. Used for
continuous integration so it can fail faster instead of timing
out.
--print-env Print all environment variables cquery is running with.
See more on https://github.com/cquery-project/cquery/wiki
)help";
}
// Writes the environment to stdcerr.
void PrintEnvironment(const char** env) {
while (*env) {
std::cerr << *env << std::endl;
++env;
}
}
struct Out_CqueryQueryDbStatus : public lsOutMessage<Out_CqueryQueryDbStatus> {
struct Params {
bool isActive = false;
};
std::string method = kMethodType_CqueryQueryDbStatus;
Params params;
};
MAKE_REFLECT_STRUCT(Out_CqueryQueryDbStatus::Params, isActive);
MAKE_REFLECT_STRUCT(Out_CqueryQueryDbStatus, jsonrpc, method, params);
void WriteQueryDbStatus(bool is_active) {
if (!g_config->emitQueryDbBlocked)
return;
static bool last_status = false;
if (is_active == last_status)
return;
last_status = is_active;
Out_CqueryQueryDbStatus out;
out.params.isActive = is_active;
QueueManager::WriteStdout(kMethodType_CqueryQueryDbStatus, out);
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// QUERYDB MAIN ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool QueryDbMainLoop(QueryDatabase* db,
Project* project,
FileConsumerSharedState* file_consumer_shared,
ImportManager* import_manager,
ImportPipelineStatus* status,
TimestampManager* timestamp_manager,
SemanticHighlightSymbolCache* semantic_cache,
WorkingFiles* working_files,
ClangCompleteManager* clang_complete,
IncludeComplete* include_complete,
CodeCompleteCache* global_code_complete_cache,
CodeCompleteCache* non_global_code_complete_cache,
CodeCompleteCache* signature_cache) {
auto* queue = QueueManager::instance();
bool did_work = false;
optional<std::unique_ptr<InMessage>> message =
queue->for_querydb.TryDequeue(true /*priority*/);
while (message) {
did_work = true;
bool found_handler = false;
for (MessageHandler* handler : *MessageHandler::message_handlers) {
if (handler->GetMethodType() == (*message)->GetMethodType()) {
handler->Run(std::move(*message));
found_handler = true;
break;
}
}
if (!found_handler) {
LOG_S(FATAL) << "Exiting; no handler for " << (*message)->GetMethodType();
exit(1);
}
message = queue->for_querydb.TryDequeue(true /*priority*/);
}
if (QueryDb_ImportMain(db, import_manager, status, semantic_cache,
working_files)) {
did_work = true;
}
return did_work;
}
void RunQueryDbThread(const std::string& bin_name) {
Project project;
SemanticHighlightSymbolCache semantic_cache;
WorkingFiles working_files;
FileConsumerSharedState file_consumer_shared;
DiagnosticsEngine diag_engine;
diag_engine.Init();
ClangCompleteManager clang_complete(
&project, &working_files,
[&](std::string path, std::vector<lsDiagnostic> diagnostics) {
diag_engine.Publish(&working_files, path, diagnostics);
},
[](lsRequestId id) {
if (id.has_value()) {
Out_Error out;
out.id = id;
out.error.code = lsErrorCodes::InternalError;
out.error.message =
"Dropping completion request; a newer request has come in that "
"will be serviced instead. This is not an error.";
QueueManager::WriteStdout(kMethodType_Unknown, out);
}
});
IncludeComplete include_complete(&project);
auto global_code_complete_cache = std::make_unique<CodeCompleteCache>();
auto non_global_code_complete_cache = std::make_unique<CodeCompleteCache>();
auto signature_cache = std::make_unique<CodeCompleteCache>();
ImportManager import_manager;
ImportPipelineStatus import_pipeline_status;
TimestampManager timestamp_manager;
QueryDatabase db;
// Setup shared references.
for (MessageHandler* handler : *MessageHandler::message_handlers) {
handler->db = &db;
handler->project = &project;
handler->diag_engine = &diag_engine;
handler->file_consumer_shared = &file_consumer_shared;
handler->import_manager = &import_manager;
handler->import_pipeline_status = &import_pipeline_status;
handler->timestamp_manager = ×tamp_manager;
handler->semantic_cache = &semantic_cache;
handler->working_files = &working_files;
handler->clang_complete = &clang_complete;
handler->include_complete = &include_complete;
handler->global_code_complete_cache = global_code_complete_cache.get();
handler->non_global_code_complete_cache =
non_global_code_complete_cache.get();
handler->signature_cache = signature_cache.get();
}
// Run query db main loop.
SetCurrentThreadName("querydb");
while (true) {
WriteQueryDbStatus(true);
bool did_work = QueryDbMainLoop(
&db, &project, &file_consumer_shared, &import_manager,
&import_pipeline_status, ×tamp_manager, &semantic_cache,
&working_files, &clang_complete, &include_complete,
global_code_complete_cache.get(), non_global_code_complete_cache.get(),
signature_cache.get());
if (!did_work) {
// Cleanup and free any unused memory.
FreeUnusedMemory();
WriteQueryDbStatus(false);
auto* queue = QueueManager::instance();
QueueManager::instance()->querydb_waiter->Wait(
&queue->for_querydb, &queue->do_id_map,
&queue->on_indexed_for_querydb);
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// STDIN MAIN //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Separate thread whose only job is to read from stdin and
// dispatch read commands to the actual indexer program. This
// cannot be done on the main thread because reading from std::cin
// blocks.
//
// |ipc| is connected to a server.
void LaunchStdinLoop(std::unordered_map<MethodType, Timer>* request_times) {
// If flushing cin requires flushing cout there could be deadlocks in some
// clients.
std::cin.tie(nullptr);
WorkThread::StartThread("stdin", [request_times]() {
auto* queue = QueueManager::instance();
while (true) {
std::unique_ptr<InMessage> message;
optional<std::string> err =
MessageRegistry::instance()->ReadMessageFromStdin(&message);
// Message parsing can fail if we don't recognize the method.
if (err) {
// The message may be partially deserialized.
// Emit an error ResponseMessage if |id| is available.
if (message) {
lsRequestId id = message->GetRequestId();
if (id.has_value()) {
Out_Error out;
out.id = id;
out.error.code = lsErrorCodes::InvalidParams;
out.error.message = std::move(*err);
queue->WriteStdout(kMethodType_Unknown, out);
}
}
continue;
}
// Cache |method_id| so we can access it after moving |message|.
MethodType method_type = message->GetMethodType();
(*request_times)[method_type] = Timer();
queue->for_querydb.Enqueue(std::move(message), false /*priority*/);
// If the message was to exit then querydb will take care of the actual
// exit. Stop reading from stdin since it might be detached.
if (method_type == kMethodType_Exit)
break;
}
});
}
void LaunchStdoutThread(std::unordered_map<MethodType, Timer>* request_times) {
WorkThread::StartThread("stdout", [=]() {
auto* queue = QueueManager::instance();
while (true) {
Stdout_Request message = queue->for_stdout.Dequeue();
if (ShouldDisplayMethodTiming(message.method)) {
Timer time = (*request_times)[message.method];
time.ResetAndPrint("[e2e] Running " + std::string(message.method));
}
RecordOutput(message.content);
fwrite(message.content.c_str(), message.content.size(), 1, stdout);
fflush(stdout);
}
});
}
void LanguageServerMain(const std::string& bin_name) {
std::unordered_map<MethodType, Timer> request_times;
LaunchStdinLoop(&request_times);
// We run a dedicated thread for writing to stdout because there can be an
// unknown number of delays when output information.
LaunchStdoutThread(&request_times);
// Start querydb which takes over this thread. The querydb will launch
// indexer threads as needed.
RunQueryDbThread(bin_name);
}
int main(int argc, char** argv, const char** env) {
// `clang-format` will not output anything if PATH is not set.
if (!getenv("PATH")) {
LOG_S(WARNING) << "The \"PATH\" environment variable is not set. "
<< "Formatting will not work.";
}
g_config = new Config();
TraceMe();
std::unordered_map<std::string, std::string> options =
ParseOptions(argc, argv);
// Setup logging ASAP.
if (HasOption(options, "--log-file")) {
loguru::add_file(options["--log-file"].c_str(), loguru::Truncate,
loguru::Verbosity_MAX);
}
if (HasOption(options, "--log-file-append")) {
loguru::add_file(options["--log-file-append"].c_str(), loguru::Append,
loguru::Verbosity_MAX);
}
if (HasOption(options, "-h") || HasOption(options, "--help")) {
PrintHelp();
// Also emit doctest help if --test-unit is passed.
if (!HasOption(options, "--test-unit"))
return 0;
}
if (!HasOption(options, "--log-all-to-stderr"))
loguru::g_stderr_verbosity = loguru::Verbosity_WARNING;
loguru::g_preamble_date = false;
loguru::g_preamble_time = false;
loguru::g_preamble_verbose = false;
loguru::g_flush_interval_ms = 0;
loguru::init(argc, argv);
QueueManager::Init();
bool language_server = true;
PlatformInit();
IndexInit();
if (HasOption(options, "--print-env"))
PrintEnvironment(env);
if (HasOption(options, "--record"))
EnableRecording(options["--record"]);
if (HasOption(options, "--check")) {
loguru::g_stderr_verbosity = loguru::Verbosity_MAX;
LOG_S(INFO) << "Running --check";
optional<AbsolutePath> path = NormalizePath(options["--check"]);
if (!path) {
ABORT_S() << "Cannot find path \"" << options["--check"] << "\". Make "
<< "sure to pass a specific cc/cpp file, not a directory.";
}
optional<std::string> content = ReadContent(*path);
if (!content || content->empty()) {
ABORT_S() << "Cannot read file content at \"" << *path
<< "\". Make sure to pass a specific cc/cpp file, not a "
<< "directory.";
}
LOG_S(INFO) << "Using path " << *path;
language_server = false;
Project project;
Config config;
if (!GetDefaultResourceDirectory())
ABORT_S() << "Cannot resolve resource directory";
config.resourceDirectory = GetDefaultResourceDirectory()->path;
project.Load(GetWorkingDirectory().path);
Project::Entry entry = project.FindCompilationEntryForFile(path->path);
LOG_S(INFO) << "Using arguments " << StringJoin(entry.args, " ");
ClangSanityCheck(entry);
return 0;
}
if (HasOption(options, "--test-unit")) {
language_server = false;
doctest::Context context;
context.applyCommandLine(argc, argv);
int res = context.run();
if (res != 0 || context.shouldExit())
return res;
}
if (HasOption(options, "--test-index")) {
language_server = false;
if (!RunIndexTests(options["--test-index"], !HasOption(options, "--ci")))
return 1;
}
if (language_server) {
if (HasOption(options, "--init")) {
// We check syntax error here but override client-side
// initializationOptions in messages/initialize.cc
g_init_options = options["--init"];
rapidjson::Document reader;
rapidjson::ParseResult ok = reader.Parse(g_init_options.c_str());
if (!ok) {
std::cerr << "Failed to parse --init as JSON: "
<< rapidjson::GetParseError_En(ok.Code()) << " ("
<< ok.Offset() << ")\n";
return 1;
}
JsonReader json_reader{&reader};
try {
Config config;
Reflect(json_reader, config);
} catch (std::invalid_argument& e) {
std::cerr << "Fail to parse --init "
<< static_cast<JsonReader&>(json_reader).GetPath()
<< ", expected " << e.what() << "\n";
return 1;
}
}
LanguageServerMain(argv[0]);
}
if (HasOption(options, "--wait-for-input")) {
std::cerr << std::endl << "[Enter] to exit" << std::endl;
getchar();
}
return 0;
}