Skip to content

Commit 7a79ed9

Browse files
committed
completion: if preamble size changes, rebuild it
Fix #190 If a new header is added, the preamble size changes. Language clients may cache completion results, thus we rebuild preamble to avoid inaccurate results.
1 parent a086e3d commit 7a79ed9

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/sema_manager.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ void *PreambleMain(void *manager_) {
423423
BuildCompilerInvocation(task.path, session->file.args, FS))
424424
BuildPreamble(*session, *CI, FS, task, std::move(stat_cache));
425425

426-
if (task.from_diag) {
426+
if (task.comp_task) {
427+
manager->comp_tasks.PushBack(std::move(task.comp_task));
428+
} else if (task.from_diag) {
427429
manager->ScheduleDiag(task.path, 0);
428430
} else {
429431
int debounce =
@@ -474,12 +476,18 @@ void *CompletionMain(void *manager_) {
474476
DiagnosticConsumer DC;
475477
std::string content = manager->wfiles->GetContent(task->path);
476478
auto Buf = llvm::MemoryBuffer::getMemBuffer(content);
479+
PreambleBounds Bounds =
480+
ComputePreambleBounds(*CI->getLangOpts(), Buf.get(), 0);
477481
bool in_preamble =
478482
GetOffsetForPosition({task->position.line, task->position.character},
479-
content) <
480-
ComputePreambleBounds(*CI->getLangOpts(), Buf.get(), 0).Size;
481-
if (in_preamble)
483+
content) < (int)Bounds.Size;
484+
if (in_preamble) {
482485
preamble.reset();
486+
} else if (preamble && Bounds.Size != preamble->Preamble.getBounds().Size) {
487+
manager->preamble_tasks.PushBack({task->path, std::move(task), false},
488+
true);
489+
continue;
490+
}
483491
auto Clang = BuildCompilerInstance(*session, std::move(CI), FS, DC,
484492
preamble.get(), task->path, Buf);
485493
if (!Clang)
@@ -556,7 +564,7 @@ void *DiagnosticMain(void *manager_) {
556564
}
557565
}
558566
if (rebuild) {
559-
manager->preamble_tasks.PushBack({task.path, true}, true);
567+
manager->preamble_tasks.PushBack({task.path, nullptr, true}, true);
560568
continue;
561569
}
562570
}

src/sema_manager.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ struct SemaManager {
114114
std::function<void(clang::CodeCompleteConsumer *OptConsumer)>;
115115
using OnDropped = std::function<void(RequestId request_id)>;
116116

117-
struct PreambleTask {
118-
std::string path;
119-
bool from_diag = false;
120-
};
121117
struct CompTask {
122118
CompTask(const RequestId &id, const std::string &path,
123119
const Position &position,
@@ -138,6 +134,11 @@ struct SemaManager {
138134
int64_t wait_until;
139135
int64_t debounce;
140136
};
137+
struct PreambleTask {
138+
std::string path;
139+
std::unique_ptr<CompTask> comp_task;
140+
bool from_diag = false;
141+
};
141142

142143
SemaManager(Project *project, WorkingFiles *wfiles,
143144
OnDiagnostic on_diagnostic, OnDropped on_dropped);

0 commit comments

Comments
 (0)