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/
```
This commit is contained in:
Fangrui Song 2020-06-28 17:16:35 -07:00
parent 8cf8a3c4a4
commit 6244594d71
4 changed files with 51 additions and 20 deletions

View File

@ -1203,6 +1203,17 @@ public:
return std::make_unique<MultiplexConsumer>(std::move(consumers));
}
};
class IndexDiags : public DiagnosticConsumer {
public:
llvm::SmallString<64> message;
void HandleDiagnostic(DiagnosticsEngine::Level level,
const clang::Diagnostic &info) override {
DiagnosticConsumer::HandleDiagnostic(level, info);
if (message.empty())
info.FormatDiagnostic(message);
}
};
} // namespace
const int IndexFile::kMajorVersion = 21;
@ -1252,7 +1263,7 @@ void init() {
g_config->index.multiVersionBlacklist);
}
std::vector<std::unique_ptr<IndexFile>>
IndexResult
index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
const std::string &opt_wdir, const std::string &main,
const std::vector<const char *> &args,
@ -1281,7 +1292,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
ci->getPreprocessorOpts().addRemappedFile(filename, bufs.back().get());
}
DiagnosticConsumer dc;
IndexDiags dc;
auto clang = std::make_unique<CompilerInstance>(pch);
clang->setInvocation(std::move(ci));
clang->createDiagnostics(&dc, false);
@ -1355,7 +1366,10 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
return {};
}
std::vector<std::unique_ptr<IndexFile>> result;
IndexResult result;
result.n_errs = (int)dc.getNumErrors();
// clang 7 does not implement operator std::string.
result.first_error = std::string(dc.message.data(), dc.message.size());
for (auto &it : param.uid2file) {
if (!it.second.db)
continue;
@ -1392,7 +1406,7 @@ index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs,
entry->dependencies[llvm::CachedHashStringRef(intern(path))] =
file.mtime;
}
result.push_back(std::move(entry));
result.indexes.push_back(std::move(entry));
}
return result;

View File

@ -322,13 +322,19 @@ struct IndexFile {
std::string toString();
};
struct IndexResult {
std::vector<std::unique_ptr<IndexFile>> indexes;
int n_errs = 0;
std::string first_error;
};
struct SemaManager;
struct WorkingFiles;
struct VFS;
namespace idx {
void init();
std::vector<std::unique_ptr<IndexFile>>
IndexResult
index(SemaManager *complete, WorkingFiles *wfiles, VFS *vfs,
const std::string &opt_wdir, const std::string &file,
const std::vector<const char *> &args,

View File

@ -330,17 +330,9 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
return true;
} while (0);
if (loud) {
std::string line;
if (LOG_V_ENABLED(1)) {
line = "\n ";
for (auto &arg : entry.args)
(line += ' ') += arg;
}
LOG_S(INFO) << (deleted ? "delete " : "parse ") << path_to_index << line;
}
std::vector<std::unique_ptr<IndexFile>> indexes;
int n_errs = 0;
std::string first_error;
if (deleted) {
indexes.push_back(std::make_unique<IndexFile>(request.path, "", false));
if (request.path != path_to_index)
@ -353,8 +345,12 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
remapped.emplace_back(path_to_index, content);
}
bool ok;
indexes = idx::index(completion, wfiles, vfs, entry.directory,
path_to_index, entry.args, remapped, no_linkage, ok);
auto result =
idx::index(completion, wfiles, vfs, entry.directory, path_to_index,
entry.args, remapped, no_linkage, ok);
indexes = std::move(result.indexes);
n_errs = result.n_errs;
first_error = std::move(result.first_error);
if (!ok) {
if (request.id.valid()) {
@ -367,6 +363,21 @@ bool indexer_Parse(SemaManager *completion, WorkingFiles *wfiles,
}
}
if (loud || n_errs) {
std::string line;
SmallString<64> tmp;
SmallString<256> msg;
(Twine(deleted ? "delete " : "parse ") + path_to_index).toVector(msg);
if (n_errs)
msg += (" error:" + Twine(n_errs) + " " + first_error).toStringRef(tmp);
if (LOG_V_ENABLED(1)) {
msg += "\n ";
for (const char *arg : entry.args)
(msg += ' ') += arg;
}
LOG_S(INFO) << std::string_view(msg.data(), msg.size());
}
for (std::unique_ptr<IndexFile> &curr : indexes) {
std::string path = curr->path;
if (!matcher.matches(path)) {

View File

@ -315,15 +315,15 @@ bool runIndexTests(const std::string &filter_path, bool enable_update) {
for (auto &arg : flags)
cargs.push_back(arg.c_str());
bool ok;
auto dbs = ccls::idx::index(&completion, &wfiles, &vfs, "", path, cargs,
{}, true, ok);
auto result = ccls::idx::index(&completion, &wfiles, &vfs, "", path,
cargs, {}, true, ok);
for (const auto &entry : all_expected_output) {
const std::string &expected_path = entry.first;
std::string expected_output = text_replacer.apply(entry.second);
// Get output from index operation.
IndexFile *db = findDbForPathEnding(expected_path, dbs);
IndexFile *db = findDbForPathEnding(expected_path, result.indexes);
std::string actual_output = "{}";
if (db) {
verifySerializeToFrom(db);