Skip to content

Commit 6244594

Browse files
committed
indexer: log the number of errors and the first diagnostic
Example log: ``` 15:47:45 indexer1 pipeline.cc:379 I parse /tmp/d/a.c error:1 use of undeclared identifier 'arg' clang /tmp/d/a.c --gcc-toolchain=/usr -working-directory=/tmp/d/ ```
1 parent 8cf8a3c commit 6244594

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

src/indexer.cc

+18-4
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,17 @@ class IndexFrontendAction : public ASTFrontendAction {
12031203
return std::make_unique<MultiplexConsumer>(std::move(consumers));
12041204
}
12051205
};
1206+
1207+
class IndexDiags : public DiagnosticConsumer {
1208+
public:
1209+
llvm::SmallString<64> message;
1210+
void HandleDiagnostic(DiagnosticsEngine::Level level,
1211+
const clang::Diagnostic &info) override {
1212+
DiagnosticConsumer::HandleDiagnostic(level, info);
1213+
if (message.empty())
1214+
info.FormatDiagnostic(message);
1215+
}
1216+
};
12061217
} // namespace
12071218

12081219
const int IndexFile::kMajorVersion = 21;
@@ -1252,7 +1263,7 @@ void init() {
12521263
g_config->index.multiVersionBlacklist);
12531264
}
12541265

1255-
std::vector<std::unique_ptr<IndexFile>>
1266+
IndexResult
12561267
index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
12571268
const std::string &opt_wdir, const std::string &main,
12581269
const std::vector<const char *> &args,
@@ -1281,7 +1292,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
12811292
ci->getPreprocessorOpts().addRemappedFile(filename, bufs.back().get());
12821293
}
12831294

1284-
DiagnosticConsumer dc;
1295+
IndexDiags dc;
12851296
auto clang = std::make_unique<CompilerInstance>(pch);
12861297
clang->setInvocation(std::move(ci));
12871298
clang->createDiagnostics(&dc, false);
@@ -1355,7 +1366,10 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
13551366
return {};
13561367
}
13571368

1358-
std::vector<std::unique_ptr<IndexFile>> result;
1369+
IndexResult result;
1370+
result.n_errs = (int)dc.getNumErrors();
1371+
// clang 7 does not implement operator std::string.
1372+
result.first_error = std::string(dc.message.data(), dc.message.size());
13591373
for (auto &it : param.uid2file) {
13601374
if (!it.second.db)
13611375
continue;
@@ -1392,7 +1406,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
13921406
entry->dependencies[llvm::CachedHashStringRef(intern(path))] =
13931407
file.mtime;
13941408
}
1395-
result.push_back(std::move(entry));
1409+
result.indexes.push_back(std::move(entry));
13961410
}
13971411

13981412
return result;

src/indexer.hh

+7-1
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,19 @@ struct IndexFile {
322322
std::string toString();
323323
};
324324

325+
struct IndexResult {
326+
std::vector<std::unique_ptr<IndexFile>> indexes;
327+
int n_errs = 0;
328+
std::string first_error;
329+
};
330+
325331
struct SemaManager;
326332
struct WorkingFiles;
327333
struct VFS;
328334

329335
namespace idx {
330336
void init();
331-
std::vector<std::unique_ptr<IndexFile>>
337+
IndexResult
332338
index(SemaManager *complete, WorkingFiles *wfiles, VFS *vfs,
333339
const std::string &opt_wdir, const std::string &file,
334340
const std::vector<const char *> &args,

src/pipeline.cc

+23-12
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,9 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
330330
return true;
331331
} while (0);
332332

333-
if (loud) {
334-
std::string line;
335-
if (LOG_V_ENABLED(1)) {
336-
line = "\n ";
337-
for (auto &arg : entry.args)
338-
(line += ' ') += arg;
339-
}
340-
LOG_S(INFO) << (deleted ? "delete " : "parse ") << path_to_index << line;
341-
}
342-
343333
std::vector<std::unique_ptr<IndexFile>> indexes;
334+
int n_errs = 0;
335+
std::string first_error;
344336
if (deleted) {
345337
indexes.push_back(std::make_unique<IndexFile>(request.path, "", false));
346338
if (request.path != path_to_index)
@@ -353,8 +345,12 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
353345
remapped.emplace_back(path_to_index, content);
354346
}
355347
bool ok;
356-
indexes = idx::index(completion, wfiles, vfs, entry.directory,
357-
path_to_index, entry.args, remapped, no_linkage, ok);
348+
auto result =
349+
idx::index(completion, wfiles, vfs, entry.directory, path_to_index,
350+
entry.args, remapped, no_linkage, ok);
351+
indexes = std::move(result.indexes);
352+
n_errs = result.n_errs;
353+
first_error = std::move(result.first_error);
358354

359355
if (!ok) {
360356
if (request.id.valid()) {
@@ -367,6 +363,21 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
367363
}
368364
}
369365

366+
if (loud || n_errs) {
367+
std::string line;
368+
SmallString<64> tmp;
369+
SmallString<256> msg;
370+
(Twine(deleted ? "delete " : "parse ") + path_to_index).toVector(msg);
371+
if (n_errs)
372+
msg += (" error:" + Twine(n_errs) + " " + first_error).toStringRef(tmp);
373+
if (LOG_V_ENABLED(1)) {
374+
msg += "\n ";
375+
for (const char *arg : entry.args)
376+
(msg += ' ') += arg;
377+
}
378+
LOG_S(INFO) << std::string_view(msg.data(), msg.size());
379+
}
380+
370381
for (std::unique_ptr<IndexFile> &curr : indexes) {
371382
std::string path = curr->path;
372383
if (!matcher.matches(path)) {

src/test.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,15 @@ bool runIndexTests(const std::string &filter_path, bool enable_update) {
315315
for (auto &arg : flags)
316316
cargs.push_back(arg.c_str());
317317
bool ok;
318-
auto dbs = ccls::idx::index(&completion, &wfiles, &vfs, "", path, cargs,
319-
{}, true, ok);
318+
auto result = ccls::idx::index(&completion, &wfiles, &vfs, "", path,
319+
cargs, {}, true, ok);
320320

321321
for (const auto &entry : all_expected_output) {
322322
const std::string &expected_path = entry.first;
323323
std::string expected_output = text_replacer.apply(entry.second);
324324

325325
// Get output from index operation.
326-
IndexFile *db = findDbForPathEnding(expected_path, dbs);
326+
IndexFile *db = findDbForPathEnding(expected_path, result.indexes);
327327
std::string actual_output = "{}";
328328
if (db) {
329329
verifySerializeToFrom(db);

0 commit comments

Comments
 (0)