blob: 5c9c5cd25da7cc84b810d8270425a372f26f19aa [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- ClangdUnit.cpp -----------------------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===---------------------------------------------------------------------===//
9
10#include "ClangdUnit.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000011
Ilya Biryukov38d79772017-05-16 09:38:59 +000012#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/CompilerInvocation.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000014#include "clang/Frontend/FrontendActions.h"
Ilya Biryukov0f62ed22017-05-26 12:26:51 +000015#include "clang/Frontend/Utils.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000016#include "clang/Index/IndexDataConsumer.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000017#include "clang/Index/IndexingAction.h"
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000018#include "clang/Lex/Lexer.h"
19#include "clang/Lex/MacroInfo.h"
20#include "clang/Lex/Preprocessor.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000021#include "clang/Lex/PreprocessorOptions.h"
22#include "clang/Sema/Sema.h"
23#include "clang/Serialization/ASTWriter.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000024#include "clang/Tooling/CompilationDatabase.h"
Ilya Biryukov04db3682017-07-21 13:29:29 +000025#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/Support/CrashRecoveryContext.h"
Krasimir Georgieva1de3c92017-06-15 09:11:57 +000028#include "llvm/Support/Format.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000029
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000030#include <algorithm>
Ilya Biryukov02d58702017-08-01 15:51:38 +000031#include <chrono>
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +000032
Ilya Biryukov38d79772017-05-16 09:38:59 +000033using namespace clang::clangd;
34using namespace clang;
35
Ilya Biryukov04db3682017-07-21 13:29:29 +000036namespace {
37
38class DeclTrackingASTConsumer : public ASTConsumer {
39public:
40 DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls)
41 : TopLevelDecls(TopLevelDecls) {}
42
43 bool HandleTopLevelDecl(DeclGroupRef DG) override {
44 for (const Decl *D : DG) {
45 // ObjCMethodDecl are not actually top-level decls.
46 if (isa<ObjCMethodDecl>(D))
47 continue;
48
49 TopLevelDecls.push_back(D);
50 }
51 return true;
52 }
53
54private:
55 std::vector<const Decl *> &TopLevelDecls;
56};
57
58class ClangdFrontendAction : public SyntaxOnlyAction {
59public:
60 std::vector<const Decl *> takeTopLevelDecls() {
61 return std::move(TopLevelDecls);
62 }
63
64protected:
65 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
66 StringRef InFile) override {
67 return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
68 }
69
70private:
71 std::vector<const Decl *> TopLevelDecls;
72};
73
Ilya Biryukov02d58702017-08-01 15:51:38 +000074class CppFilePreambleCallbacks : public PreambleCallbacks {
Ilya Biryukov04db3682017-07-21 13:29:29 +000075public:
76 std::vector<serialization::DeclID> takeTopLevelDeclIDs() {
77 return std::move(TopLevelDeclIDs);
78 }
79
80 void AfterPCHEmitted(ASTWriter &Writer) override {
81 TopLevelDeclIDs.reserve(TopLevelDecls.size());
82 for (Decl *D : TopLevelDecls) {
83 // Invalid top-level decls may not have been serialized.
84 if (D->isInvalidDecl())
85 continue;
86 TopLevelDeclIDs.push_back(Writer.getDeclID(D));
87 }
88 }
89
90 void HandleTopLevelDecl(DeclGroupRef DG) override {
91 for (Decl *D : DG) {
92 if (isa<ObjCMethodDecl>(D))
93 continue;
94 TopLevelDecls.push_back(D);
95 }
96 }
97
98private:
99 std::vector<Decl *> TopLevelDecls;
100 std::vector<serialization::DeclID> TopLevelDeclIDs;
101};
102
103/// Convert from clang diagnostic level to LSP severity.
104static int getSeverity(DiagnosticsEngine::Level L) {
105 switch (L) {
106 case DiagnosticsEngine::Remark:
107 return 4;
108 case DiagnosticsEngine::Note:
109 return 3;
110 case DiagnosticsEngine::Warning:
111 return 2;
112 case DiagnosticsEngine::Fatal:
113 case DiagnosticsEngine::Error:
114 return 1;
115 case DiagnosticsEngine::Ignored:
116 return 0;
117 }
118 llvm_unreachable("Unknown diagnostic level!");
119}
120
121llvm::Optional<DiagWithFixIts> toClangdDiag(StoredDiagnostic D) {
122 auto Location = D.getLocation();
123 if (!Location.isValid() || !Location.getManager().isInMainFile(Location))
124 return llvm::None;
125
126 Position P;
127 P.line = Location.getSpellingLineNumber() - 1;
128 P.character = Location.getSpellingColumnNumber();
129 Range R = {P, P};
130 clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()};
131
132 llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic;
133 for (const FixItHint &Fix : D.getFixIts()) {
134 FixItsForDiagnostic.push_back(clang::tooling::Replacement(
135 Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert));
136 }
137 return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)};
138}
139
140class StoreDiagsConsumer : public DiagnosticConsumer {
141public:
142 StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {}
143
144 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
145 const clang::Diagnostic &Info) override {
146 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
147
148 if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info)))
149 Output.push_back(std::move(*convertedDiag));
150 }
151
152private:
153 std::vector<DiagWithFixIts> &Output;
154};
155
156class EmptyDiagsConsumer : public DiagnosticConsumer {
157public:
158 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
159 const clang::Diagnostic &Info) override {}
160};
161
162std::unique_ptr<CompilerInvocation>
163createCompilerInvocation(ArrayRef<const char *> ArgList,
164 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
165 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
166 auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags),
167 std::move(VFS));
168 // We rely on CompilerInstance to manage the resource (i.e. free them on
169 // EndSourceFile), but that won't happen if DisableFree is set to true.
170 // Since createInvocationFromCommandLine sets it to true, we have to override
171 // it.
172 CI->getFrontendOpts().DisableFree = false;
173 return CI;
174}
175
176/// Creates a CompilerInstance from \p CI, with main buffer overriden to \p
177/// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not
178/// null. Note that vfs::FileSystem inside returned instance may differ from \p
179/// VFS if additional file remapping were set in command-line arguments.
180/// On some errors, returns null. When non-null value is returned, it's expected
181/// to be consumed by the FrontendAction as it will have a pointer to the \p
182/// Buffer that will only be deleted if BeginSourceFile is called.
183std::unique_ptr<CompilerInstance>
184prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
185 const PrecompiledPreamble *Preamble,
186 std::unique_ptr<llvm::MemoryBuffer> Buffer,
187 std::shared_ptr<PCHContainerOperations> PCHs,
188 IntrusiveRefCntPtr<vfs::FileSystem> VFS,
189 DiagnosticConsumer &DiagsClient) {
190 assert(VFS && "VFS is null");
191 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
192 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
193 "of ContentsBuffer");
194
195 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
196 // sure it will be released if no error is emitted.
197 if (Preamble) {
198 Preamble->AddImplicitPreamble(*CI, Buffer.get());
199 } else {
200 CI->getPreprocessorOpts().addRemappedFile(
201 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
202 }
203
204 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
205 Clang->setInvocation(std::move(CI));
206 Clang->createDiagnostics(&DiagsClient, false);
207
208 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
209 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
210 VFS = VFSWithRemapping;
211 Clang->setVirtualFileSystem(VFS);
212
213 Clang->setTarget(TargetInfo::CreateTargetInfo(
214 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
215 if (!Clang->hasTarget())
216 return nullptr;
217
218 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
219 // release it.
220 Buffer.release();
221 return Clang;
222}
223
Ilya Biryukov02d58702017-08-01 15:51:38 +0000224template <class T> bool futureIsReady(std::shared_future<T> const &Future) {
225 return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
226}
227
Ilya Biryukov04db3682017-07-21 13:29:29 +0000228} // namespace
229
Ilya Biryukov38d79772017-05-16 09:38:59 +0000230namespace {
231
232CompletionItemKind getKind(CXCursorKind K) {
233 switch (K) {
234 case CXCursor_MacroInstantiation:
235 case CXCursor_MacroDefinition:
236 return CompletionItemKind::Text;
237 case CXCursor_CXXMethod:
238 return CompletionItemKind::Method;
239 case CXCursor_FunctionDecl:
240 case CXCursor_FunctionTemplate:
241 return CompletionItemKind::Function;
242 case CXCursor_Constructor:
243 case CXCursor_Destructor:
244 return CompletionItemKind::Constructor;
245 case CXCursor_FieldDecl:
246 return CompletionItemKind::Field;
247 case CXCursor_VarDecl:
248 case CXCursor_ParmDecl:
249 return CompletionItemKind::Variable;
250 case CXCursor_ClassDecl:
251 case CXCursor_StructDecl:
252 case CXCursor_UnionDecl:
253 case CXCursor_ClassTemplate:
254 case CXCursor_ClassTemplatePartialSpecialization:
255 return CompletionItemKind::Class;
256 case CXCursor_Namespace:
257 case CXCursor_NamespaceAlias:
258 case CXCursor_NamespaceRef:
259 return CompletionItemKind::Module;
260 case CXCursor_EnumConstantDecl:
261 return CompletionItemKind::Value;
262 case CXCursor_EnumDecl:
263 return CompletionItemKind::Enum;
264 case CXCursor_TypeAliasDecl:
265 case CXCursor_TypeAliasTemplateDecl:
266 case CXCursor_TypedefDecl:
267 case CXCursor_MemberRef:
268 case CXCursor_TypeRef:
269 return CompletionItemKind::Reference;
270 default:
271 return CompletionItemKind::Missing;
272 }
273}
274
275class CompletionItemsCollector : public CodeCompleteConsumer {
276 std::vector<CompletionItem> *Items;
277 std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator;
278 CodeCompletionTUInfo CCTUInfo;
279
280public:
281 CompletionItemsCollector(std::vector<CompletionItem> *Items,
282 const CodeCompleteOptions &CodeCompleteOpts)
283 : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false),
284 Items(Items),
285 Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()),
286 CCTUInfo(Allocator) {}
287
288 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
289 CodeCompletionResult *Results,
290 unsigned NumResults) override {
291 for (unsigned I = 0; I != NumResults; ++I) {
292 CodeCompletionResult &Result = Results[I];
293 CodeCompletionString *CCS = Result.CreateCodeCompletionString(
294 S, Context, *Allocator, CCTUInfo,
295 CodeCompleteOpts.IncludeBriefComments);
296 if (CCS) {
297 CompletionItem Item;
Krasimir Georgieve6035a52017-06-08 15:11:51 +0000298 for (CodeCompletionString::Chunk C : *CCS) {
299 switch (C.Kind) {
300 case CodeCompletionString::CK_ResultType:
301 Item.detail = C.Text;
302 break;
303 case CodeCompletionString::CK_Optional:
304 break;
305 default:
306 Item.label += C.Text;
307 break;
308 }
309 }
Ilya Biryukov38d79772017-05-16 09:38:59 +0000310 assert(CCS->getTypedText());
Ilya Biryukov38d79772017-05-16 09:38:59 +0000311 Item.kind = getKind(Result.CursorKind);
Krasimir Georgieva1de3c92017-06-15 09:11:57 +0000312 // Priority is a 16-bit integer, hence at most 5 digits.
Krasimir Georgieva1de3c92017-06-15 09:11:57 +0000313 assert(CCS->getPriority() < 99999 && "Expecting code completion result "
314 "priority to have at most "
315 "5-digits");
Ilya Biryukovde462742017-07-27 17:43:07 +0000316 llvm::raw_string_ostream(Item.sortText)
317 << llvm::format("%05d%s", CCS->getPriority(), CCS->getTypedText());
Krasimir Georgieva1de3c92017-06-15 09:11:57 +0000318 Item.insertText = Item.filterText = CCS->getTypedText();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000319 if (CCS->getBriefComment())
320 Item.documentation = CCS->getBriefComment();
321 Items->push_back(std::move(Item));
322 }
323 }
324 }
325
326 GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; }
327
328 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
329};
330} // namespace
331
Ilya Biryukov0f62ed22017-05-26 12:26:51 +0000332std::vector<CompletionItem>
Ilya Biryukov02d58702017-08-01 15:51:38 +0000333clangd::codeComplete(PathRef FileName, tooling::CompileCommand Command,
334 PrecompiledPreamble const *Preamble, StringRef Contents,
335 Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS,
336 std::shared_ptr<PCHContainerOperations> PCHs) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000337 std::vector<const char *> ArgStrs;
338 for (const auto &S : Command.CommandLine)
339 ArgStrs.push_back(S.c_str());
340
Krasimir Georgieve4130d52017-07-25 11:37:43 +0000341 VFS->setCurrentWorkingDirectory(Command.Directory);
342
Ilya Biryukov04db3682017-07-21 13:29:29 +0000343 std::unique_ptr<CompilerInvocation> CI;
344 EmptyDiagsConsumer DummyDiagsConsumer;
345 {
346 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
347 CompilerInstance::createDiagnostics(new DiagnosticOptions,
348 &DummyDiagsConsumer, false);
349 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
350 }
351 assert(CI && "Couldn't create CompilerInvocation");
352
353 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
354 llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName);
355
356 // Attempt to reuse the PCH from precompiled preamble, if it was built.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000357 if (Preamble) {
358 auto Bounds =
359 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
Ilya Biryukov02d58702017-08-01 15:51:38 +0000360 if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get()))
361 Preamble = nullptr;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000362 }
363
Ilya Biryukov02d58702017-08-01 15:51:38 +0000364 auto Clang = prepareCompilerInstance(std::move(CI), Preamble,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000365 std::move(ContentsBuffer), PCHs, VFS,
366 DummyDiagsConsumer);
367 auto &DiagOpts = Clang->getDiagnosticOpts();
368 DiagOpts.IgnoreWarnings = true;
369
370 auto &FrontendOpts = Clang->getFrontendOpts();
371 FrontendOpts.SkipFunctionBodies = true;
372
373 FrontendOpts.CodeCompleteOpts.IncludeGlobals = true;
374 // we don't handle code patterns properly yet, disable them.
375 FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = false;
376 FrontendOpts.CodeCompleteOpts.IncludeMacros = true;
377 FrontendOpts.CodeCompleteOpts.IncludeBriefComments = true;
378
379 FrontendOpts.CodeCompletionAt.FileName = FileName;
380 FrontendOpts.CodeCompletionAt.Line = Pos.line + 1;
381 FrontendOpts.CodeCompletionAt.Column = Pos.character + 1;
382
Ilya Biryukov38d79772017-05-16 09:38:59 +0000383 std::vector<CompletionItem> Items;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000384 Clang->setCodeCompletionConsumer(
385 new CompletionItemsCollector(&Items, FrontendOpts.CodeCompleteOpts));
Ilya Biryukov38d79772017-05-16 09:38:59 +0000386
Ilya Biryukov04db3682017-07-21 13:29:29 +0000387 SyntaxOnlyAction Action;
388 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
389 // FIXME(ibiryukov): log errors
390 return Items;
391 }
392 if (!Action.Execute()) {
393 // FIXME(ibiryukov): log errors
394 }
395 Action.EndSourceFile();
Ilya Biryukov38d79772017-05-16 09:38:59 +0000396
Ilya Biryukov38d79772017-05-16 09:38:59 +0000397 return Items;
398}
399
Ilya Biryukov02d58702017-08-01 15:51:38 +0000400void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
401 AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000402}
Ilya Biryukovf01af682017-05-23 13:42:59 +0000403
Ilya Biryukov02d58702017-08-01 15:51:38 +0000404llvm::Optional<ParsedAST>
405ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI,
406 const PrecompiledPreamble *Preamble,
407 ArrayRef<serialization::DeclID> PreambleDeclIDs,
408 std::unique_ptr<llvm::MemoryBuffer> Buffer,
409 std::shared_ptr<PCHContainerOperations> PCHs,
410 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000411
412 std::vector<DiagWithFixIts> ASTDiags;
413 StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags);
414
415 auto Clang =
416 prepareCompilerInstance(std::move(CI), Preamble, std::move(Buffer), PCHs,
417 VFS, /*ref*/ UnitDiagsConsumer);
418
419 // Recover resources if we crash before exiting this method.
420 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup(
421 Clang.get());
422
423 auto Action = llvm::make_unique<ClangdFrontendAction>();
424 if (!Action->BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) {
425 // FIXME(ibiryukov): log error
426 return llvm::None;
427 }
428 if (!Action->Execute()) {
429 // FIXME(ibiryukov): log error
430 }
431
432 // UnitDiagsConsumer is local, we can not store it in CompilerInstance that
433 // has a longer lifetime.
434 Clang->getDiagnostics().setClient(new EmptyDiagsConsumer);
435
436 std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls();
437 std::vector<serialization::DeclID> PendingDecls;
438 if (Preamble) {
439 PendingDecls.reserve(PreambleDeclIDs.size());
440 PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(),
441 PreambleDeclIDs.end());
442 }
443
444 return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls),
445 std::move(PendingDecls), std::move(ASTDiags));
446}
447
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000448namespace {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000449
450SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
451 const FileEntry *FE,
452 unsigned Offset) {
453 SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1);
454 return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
455}
456
457SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr,
458 const FileEntry *FE, Position Pos) {
459 SourceLocation InputLoc =
460 Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1);
461 return Mgr.getMacroArgExpandedLocation(InputLoc);
462}
463
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000464/// Finds declarations locations that a given source location refers to.
465class DeclarationLocationsFinder : public index::IndexDataConsumer {
466 std::vector<Location> DeclarationLocations;
467 const SourceLocation &SearchedLocation;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000468 const ASTContext &AST;
469 Preprocessor &PP;
470
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000471public:
472 DeclarationLocationsFinder(raw_ostream &OS,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000473 const SourceLocation &SearchedLocation,
474 ASTContext &AST, Preprocessor &PP)
475 : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {}
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000476
477 std::vector<Location> takeLocations() {
478 // Don't keep the same location multiple times.
479 // This can happen when nodes in the AST are visited twice.
480 std::sort(DeclarationLocations.begin(), DeclarationLocations.end());
Kirill Bobyrev46213872017-06-28 20:57:28 +0000481 auto last =
482 std::unique(DeclarationLocations.begin(), DeclarationLocations.end());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000483 DeclarationLocations.erase(last, DeclarationLocations.end());
484 return std::move(DeclarationLocations);
485 }
486
Ilya Biryukov02d58702017-08-01 15:51:38 +0000487 bool
488 handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
489 ArrayRef<index::SymbolRelation> Relations, FileID FID,
490 unsigned Offset,
491 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000492 if (isSearchedLocation(FID, Offset)) {
493 addDeclarationLocation(D->getSourceRange());
494 }
495 return true;
496 }
497
498private:
499 bool isSearchedLocation(FileID FID, unsigned Offset) const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000500 const SourceManager &SourceMgr = AST.getSourceManager();
501 return SourceMgr.getFileOffset(SearchedLocation) == Offset &&
502 SourceMgr.getFileID(SearchedLocation) == FID;
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000503 }
504
Ilya Biryukov04db3682017-07-21 13:29:29 +0000505 void addDeclarationLocation(const SourceRange &ValSourceRange) {
506 const SourceManager &SourceMgr = AST.getSourceManager();
507 const LangOptions &LangOpts = AST.getLangOpts();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000508 SourceLocation LocStart = ValSourceRange.getBegin();
509 SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(),
Ilya Biryukov04db3682017-07-21 13:29:29 +0000510 0, SourceMgr, LangOpts);
Kirill Bobyrev46213872017-06-28 20:57:28 +0000511 Position Begin;
512 Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1;
513 Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1;
514 Position End;
515 End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1;
516 End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1;
517 Range R = {Begin, End};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000518 Location L;
519 L.uri = URI::fromFile(
520 SourceMgr.getFilename(SourceMgr.getSpellingLoc(LocStart)));
521 L.range = R;
522 DeclarationLocations.push_back(L);
523 }
524
Kirill Bobyrev46213872017-06-28 20:57:28 +0000525 void finish() override {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000526 // Also handle possible macro at the searched location.
527 Token Result;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000528 if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(),
529 AST.getLangOpts(), false)) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000530 if (Result.is(tok::raw_identifier)) {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000531 PP.LookUpIdentifierInfo(Result);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000532 }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000533 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000534 if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) {
535 std::pair<FileID, unsigned int> DecLoc =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000536 AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000537 // Get the definition just before the searched location so that a macro
538 // referenced in a '#undef MACRO' can still be found.
Ilya Biryukov04db3682017-07-21 13:29:29 +0000539 SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation(
540 AST.getSourceManager(),
541 AST.getSourceManager().getFileEntryForID(DecLoc.first),
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000542 DecLoc.second - 1);
543 MacroDefinition MacroDef =
Ilya Biryukov04db3682017-07-21 13:29:29 +0000544 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
545 MacroInfo *MacroInf = MacroDef.getMacroInfo();
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000546 if (MacroInf) {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000547 addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(),
548 MacroInf->getDefinitionEndLoc()));
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000549 }
550 }
551 }
552 }
553};
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000554
Ilya Biryukov02d58702017-08-01 15:51:38 +0000555SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
556 const FileEntry *FE) {
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000557 // The language server protocol uses zero-based line and column numbers.
558 // Clang uses one-based numbers.
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000559
Ilya Biryukov02d58702017-08-01 15:51:38 +0000560 const ASTContext &AST = Unit.getASTContext();
Ilya Biryukov04db3682017-07-21 13:29:29 +0000561 const SourceManager &SourceMgr = AST.getSourceManager();
562
563 SourceLocation InputLocation =
564 getMacroArgExpandedLocation(SourceMgr, FE, Pos);
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000565 if (Pos.character == 0) {
566 return InputLocation;
567 }
568
569 // This handle cases where the position is in the middle of a token or right
570 // after the end of a token. In theory we could just use GetBeginningOfToken
571 // to find the start of the token at the input position, but this doesn't
572 // work when right after the end, i.e. foo|.
573 // So try to go back by one and see if we're still inside the an identifier
574 // token. If so, Take the beginning of this token.
575 // (It should be the same identifier because you can't have two adjacent
576 // identifiers without another token in between.)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000577 SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation(
578 SourceMgr, FE, Position{Pos.line, Pos.character - 1});
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000579 Token Result;
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000580 if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr,
Ilya Biryukov04db3682017-07-21 13:29:29 +0000581 AST.getLangOpts(), false)) {
Ilya Biryukov4203d2a2017-06-29 17:11:32 +0000582 // getRawToken failed, just use InputLocation.
583 return InputLocation;
584 }
585
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000586 if (Result.is(tok::raw_identifier)) {
587 return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr,
Ilya Biryukov02d58702017-08-01 15:51:38 +0000588 AST.getLangOpts());
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000589 }
590
591 return InputLocation;
592}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000593} // namespace
Ilya Biryukov04db3682017-07-21 13:29:29 +0000594
Ilya Biryukov02d58702017-08-01 15:51:38 +0000595std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos) {
596 const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
597 const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
598 if (!FE)
599 return {};
600
601 SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE);
602
603 auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>(
604 llvm::errs(), SourceLocationBeg, AST.getASTContext(),
605 AST.getPreprocessor());
606 index::IndexingOptions IndexOpts;
607 IndexOpts.SystemSymbolFilter =
608 index::IndexingOptions::SystemSymbolFilterKind::All;
609 IndexOpts.IndexFunctionLocals = true;
610
611 indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(),
612 DeclLocationsFinder, IndexOpts);
613
614 return DeclLocationsFinder->takeLocations();
615}
616
617void ParsedAST::ensurePreambleDeclsDeserialized() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000618 if (PendingTopLevelDecls.empty())
619 return;
620
621 std::vector<const Decl *> Resolved;
622 Resolved.reserve(PendingTopLevelDecls.size());
623
624 ExternalASTSource &Source = *getASTContext().getExternalSource();
625 for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) {
626 // Resolve the declaration ID to an actual declaration, possibly
627 // deserializing the declaration in the process.
628 if (Decl *D = Source.GetExternalDecl(TopLevelDecl))
629 Resolved.push_back(D);
630 }
631
632 TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size());
633 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
634
635 PendingTopLevelDecls.clear();
636}
637
Ilya Biryukov02d58702017-08-01 15:51:38 +0000638ParsedAST::ParsedAST(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000639
Ilya Biryukov02d58702017-08-01 15:51:38 +0000640ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
Ilya Biryukov04db3682017-07-21 13:29:29 +0000641
Ilya Biryukov02d58702017-08-01 15:51:38 +0000642ParsedAST::~ParsedAST() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000643 if (Action) {
644 Action->EndSourceFile();
645 }
646}
647
Ilya Biryukov02d58702017-08-01 15:51:38 +0000648ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
649
650const ASTContext &ParsedAST::getASTContext() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000651 return Clang->getASTContext();
652}
653
Ilya Biryukov02d58702017-08-01 15:51:38 +0000654Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
Ilya Biryukov04db3682017-07-21 13:29:29 +0000655
Ilya Biryukov02d58702017-08-01 15:51:38 +0000656const Preprocessor &ParsedAST::getPreprocessor() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000657 return Clang->getPreprocessor();
658}
659
Ilya Biryukov02d58702017-08-01 15:51:38 +0000660ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000661 ensurePreambleDeclsDeserialized();
662 return TopLevelDecls;
663}
664
Ilya Biryukov02d58702017-08-01 15:51:38 +0000665const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const {
Ilya Biryukov04db3682017-07-21 13:29:29 +0000666 return Diags;
667}
668
Ilya Biryukov02d58702017-08-01 15:51:38 +0000669ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang,
670 std::unique_ptr<FrontendAction> Action,
671 std::vector<const Decl *> TopLevelDecls,
672 std::vector<serialization::DeclID> PendingTopLevelDecls,
673 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000674 : Clang(std::move(Clang)), Action(std::move(Action)),
675 Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)),
676 PendingTopLevelDecls(std::move(PendingTopLevelDecls)) {
677 assert(this->Clang);
678 assert(this->Action);
679}
680
Ilya Biryukov02d58702017-08-01 15:51:38 +0000681ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper)
682 : AST(std::move(Wrapper.AST)) {}
683
684ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST)
685 : AST(std::move(AST)) {}
686
687PreambleData::PreambleData(PrecompiledPreamble Preamble,
688 std::vector<serialization::DeclID> TopLevelDeclIDs,
689 std::vector<DiagWithFixIts> Diags)
Ilya Biryukov04db3682017-07-21 13:29:29 +0000690 : Preamble(std::move(Preamble)),
691 TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {}
Ilya Biryukov02d58702017-08-01 15:51:38 +0000692
693std::shared_ptr<CppFile>
694CppFile::Create(PathRef FileName, tooling::CompileCommand Command,
695 std::shared_ptr<PCHContainerOperations> PCHs) {
696 return std::shared_ptr<CppFile>(
697 new CppFile(FileName, std::move(Command), std::move(PCHs)));
698}
699
700CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
701 std::shared_ptr<PCHContainerOperations> PCHs)
702 : FileName(FileName), Command(std::move(Command)), RebuildCounter(0),
703 RebuildInProgress(false), PCHs(std::move(PCHs)) {
704
705 std::lock_guard<std::mutex> Lock(Mutex);
706 LatestAvailablePreamble = nullptr;
707 PreamblePromise.set_value(nullptr);
708 PreambleFuture = PreamblePromise.get_future();
709
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000710 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000711 ASTFuture = ASTPromise.get_future();
712}
713
714void CppFile::cancelRebuilds() {
715 std::unique_lock<std::mutex> Lock(Mutex);
716 // Cancel an ongoing rebuild, if any, and wait for it to finish.
717 ++this->RebuildCounter;
718 // Rebuild asserts that futures aren't ready if rebuild is cancelled.
719 // We want to keep this invariant.
720 if (futureIsReady(PreambleFuture)) {
721 PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>();
722 PreambleFuture = PreamblePromise.get_future();
723 }
724 if (futureIsReady(ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000725 ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000726 ASTFuture = ASTPromise.get_future();
727 }
728 // Now wait for rebuild to finish.
729 RebuildCond.wait(Lock, [this]() { return !this->RebuildInProgress; });
730
731 // Return empty results for futures.
732 PreamblePromise.set_value(nullptr);
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000733 ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000734}
735
736llvm::Optional<std::vector<DiagWithFixIts>>
737CppFile::rebuild(StringRef NewContents,
738 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
739 return deferRebuild(NewContents, std::move(VFS)).get();
740}
741
742std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
743CppFile::deferRebuild(StringRef NewContents,
744 IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
745 std::shared_ptr<const PreambleData> OldPreamble;
746 std::shared_ptr<PCHContainerOperations> PCHs;
747 unsigned RequestRebuildCounter;
748 {
749 std::unique_lock<std::mutex> Lock(Mutex);
750 // Increase RebuildCounter to cancel all ongoing FinishRebuild operations.
751 // They will try to exit as early as possible and won't call set_value on
752 // our promises.
753 RequestRebuildCounter = ++this->RebuildCounter;
754 PCHs = this->PCHs;
755
756 // Remember the preamble to be used during rebuild.
757 OldPreamble = this->LatestAvailablePreamble;
758 // Setup std::promises and std::futures for Preamble and AST. Corresponding
759 // futures will wait until the rebuild process is finished.
760 if (futureIsReady(this->PreambleFuture)) {
761 this->PreamblePromise =
762 std::promise<std::shared_ptr<const PreambleData>>();
763 this->PreambleFuture = this->PreamblePromise.get_future();
764 }
765 if (futureIsReady(this->ASTFuture)) {
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000766 this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>();
Ilya Biryukov02d58702017-08-01 15:51:38 +0000767 this->ASTFuture = this->ASTPromise.get_future();
768 }
769 } // unlock Mutex.
770
771 // A helper to function to finish the rebuild. May be run on a different
772 // thread.
773
774 // Don't let this CppFile die before rebuild is finished.
775 std::shared_ptr<CppFile> That = shared_from_this();
776 auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs,
777 That](std::string NewContents)
778 -> llvm::Optional<std::vector<DiagWithFixIts>> {
779 // Only one execution of this method is possible at a time.
780 // RebuildGuard will wait for any ongoing rebuilds to finish and will put us
781 // into a state for doing a rebuild.
782 RebuildGuard Rebuild(*That, RequestRebuildCounter);
783 if (Rebuild.wasCancelledBeforeConstruction())
784 return llvm::None;
785
786 std::vector<const char *> ArgStrs;
787 for (const auto &S : That->Command.CommandLine)
788 ArgStrs.push_back(S.c_str());
789
790 VFS->setCurrentWorkingDirectory(That->Command.Directory);
791
792 std::unique_ptr<CompilerInvocation> CI;
793 {
794 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
795 // reporting them.
796 EmptyDiagsConsumer CommandLineDiagsConsumer;
797 IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
798 CompilerInstance::createDiagnostics(new DiagnosticOptions,
799 &CommandLineDiagsConsumer, false);
800 CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS);
801 }
802 assert(CI && "Couldn't create CompilerInvocation");
803
804 std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
805 llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName);
806
807 // A helper function to rebuild the preamble or reuse the existing one. Does
808 // not mutate any fields, only does the actual computation.
809 auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> {
810 auto Bounds =
811 ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
812 if (OldPreamble && OldPreamble->Preamble.CanReuse(
813 *CI, ContentsBuffer.get(), Bounds, VFS.get())) {
814 return OldPreamble;
815 }
816
817 std::vector<DiagWithFixIts> PreambleDiags;
818 StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags);
819 IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
820 CompilerInstance::createDiagnostics(
821 &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false);
822 CppFilePreambleCallbacks SerializedDeclsCollector;
823 auto BuiltPreamble = PrecompiledPreamble::Build(
824 *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs,
825 SerializedDeclsCollector);
826
827 if (BuiltPreamble) {
828 return std::make_shared<PreambleData>(
829 std::move(*BuiltPreamble),
830 SerializedDeclsCollector.takeTopLevelDeclIDs(),
831 std::move(PreambleDiags));
832 } else {
833 return nullptr;
834 }
835 };
836
837 // Compute updated Preamble.
838 std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble();
839 // Publish the new Preamble.
840 {
841 std::lock_guard<std::mutex> Lock(That->Mutex);
842 // We always set LatestAvailablePreamble to the new value, hoping that it
843 // will still be usable in the further requests.
844 That->LatestAvailablePreamble = NewPreamble;
845 if (RequestRebuildCounter != That->RebuildCounter)
846 return llvm::None; // Our rebuild request was cancelled, do nothing.
847 That->PreamblePromise.set_value(NewPreamble);
848 } // unlock Mutex
849
850 // Prepare the Preamble and supplementary data for rebuilding AST.
851 const PrecompiledPreamble *PreambleForAST = nullptr;
852 ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None;
853 std::vector<DiagWithFixIts> Diagnostics;
854 if (NewPreamble) {
855 PreambleForAST = &NewPreamble->Preamble;
856 SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs;
857 Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(),
858 NewPreamble->Diags.end());
859 }
860
861 // Compute updated AST.
862 llvm::Optional<ParsedAST> NewAST =
863 ParsedAST::Build(std::move(CI), PreambleForAST, SerializedPreambleDecls,
864 std::move(ContentsBuffer), PCHs, VFS);
865
866 if (NewAST) {
867 Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(),
868 NewAST->getDiagnostics().end());
869 } else {
870 // Don't report even Preamble diagnostics if we coulnd't build AST.
871 Diagnostics.clear();
872 }
873
874 // Publish the new AST.
875 {
876 std::lock_guard<std::mutex> Lock(That->Mutex);
877 if (RequestRebuildCounter != That->RebuildCounter)
878 return Diagnostics; // Our rebuild request was cancelled, don't set
879 // ASTPromise.
880
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000881 That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(std::move(NewAST)));
Ilya Biryukov02d58702017-08-01 15:51:38 +0000882 } // unlock Mutex
883
884 return Diagnostics;
885 };
886
887 return std::async(std::launch::deferred, FinishRebuild, NewContents.str());
888}
889
890std::shared_future<std::shared_ptr<const PreambleData>>
891CppFile::getPreamble() const {
892 std::lock_guard<std::mutex> Lock(Mutex);
893 return PreambleFuture;
894}
895
896std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const {
897 std::lock_guard<std::mutex> Lock(Mutex);
898 return LatestAvailablePreamble;
899}
900
Ilya Biryukov6e1f3b12017-08-01 18:27:58 +0000901std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const {
Ilya Biryukov02d58702017-08-01 15:51:38 +0000902 std::lock_guard<std::mutex> Lock(Mutex);
903 return ASTFuture;
904}
905
906tooling::CompileCommand const &CppFile::getCompileCommand() const {
907 return Command;
908}
909
910CppFile::RebuildGuard::RebuildGuard(CppFile &File,
911 unsigned RequestRebuildCounter)
912 : File(File), RequestRebuildCounter(RequestRebuildCounter) {
913 std::unique_lock<std::mutex> Lock(File.Mutex);
914 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
915 if (WasCancelledBeforeConstruction)
916 return;
917
918 File.RebuildCond.wait(Lock, [&File]() { return !File.RebuildInProgress; });
919
920 WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter;
921 if (WasCancelledBeforeConstruction)
922 return;
923
924 File.RebuildInProgress = true;
925}
926
927bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const {
928 return WasCancelledBeforeConstruction;
929}
930
931CppFile::RebuildGuard::~RebuildGuard() {
932 if (WasCancelledBeforeConstruction)
933 return;
934
935 std::unique_lock<std::mutex> Lock(File.Mutex);
936 assert(File.RebuildInProgress);
937 File.RebuildInProgress = false;
938
939 if (File.RebuildCounter == RequestRebuildCounter) {
940 // Our rebuild request was successful.
941 assert(futureIsReady(File.ASTFuture));
942 assert(futureIsReady(File.PreambleFuture));
943 } else {
944 // Our rebuild request was cancelled, because further reparse was requested.
945 assert(!futureIsReady(File.ASTFuture));
946 assert(!futureIsReady(File.PreambleFuture));
947 }
948
949 Lock.unlock();
950 File.RebuildCond.notify_all();
951}