1//===- ASTUnit.cpp - ASTUnit utility --------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// ASTUnit Implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Frontend/ASTUnit.h"
14#include "clang/AST/ASTConsumer.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/CommentCommandTraits.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclGroup.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/DeclarationName.h"
24#include "clang/AST/ExternalASTSource.h"
25#include "clang/AST/PrettyPrinter.h"
26#include "clang/AST/Type.h"
27#include "clang/AST/TypeOrdering.h"
28#include "clang/Basic/Diagnostic.h"
29#include "clang/Basic/FileManager.h"
30#include "clang/Basic/IdentifierTable.h"
31#include "clang/Basic/LLVM.h"
32#include "clang/Basic/LangOptions.h"
33#include "clang/Basic/LangStandard.h"
34#include "clang/Basic/Module.h"
35#include "clang/Basic/SourceLocation.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/TargetOptions.h"
39#include "clang/Frontend/CompilerInstance.h"
40#include "clang/Frontend/CompilerInvocation.h"
41#include "clang/Frontend/FrontendAction.h"
42#include "clang/Frontend/FrontendActions.h"
43#include "clang/Frontend/FrontendDiagnostic.h"
44#include "clang/Frontend/FrontendOptions.h"
45#include "clang/Frontend/MultiplexConsumer.h"
46#include "clang/Frontend/PrecompiledPreamble.h"
47#include "clang/Frontend/Utils.h"
48#include "clang/Lex/HeaderSearch.h"
49#include "clang/Lex/HeaderSearchOptions.h"
50#include "clang/Lex/Lexer.h"
51#include "clang/Lex/PPCallbacks.h"
52#include "clang/Lex/PreprocessingRecord.h"
53#include "clang/Lex/Preprocessor.h"
54#include "clang/Lex/PreprocessorOptions.h"
55#include "clang/Lex/Token.h"
56#include "clang/Sema/CodeCompleteConsumer.h"
57#include "clang/Sema/CodeCompleteOptions.h"
58#include "clang/Sema/Sema.h"
59#include "clang/Sema/SemaCodeCompletion.h"
60#include "clang/Serialization/ASTBitCodes.h"
61#include "clang/Serialization/ASTReader.h"
62#include "clang/Serialization/ASTWriter.h"
63#include "clang/Serialization/ContinuousRangeMap.h"
64#include "clang/Serialization/InMemoryModuleCache.h"
65#include "clang/Serialization/ModuleFile.h"
66#include "clang/Serialization/PCHContainerOperations.h"
67#include "llvm/ADT/ArrayRef.h"
68#include "llvm/ADT/DenseMap.h"
69#include "llvm/ADT/IntrusiveRefCntPtr.h"
70#include "llvm/ADT/STLExtras.h"
71#include "llvm/ADT/ScopeExit.h"
72#include "llvm/ADT/SmallVector.h"
73#include "llvm/ADT/StringMap.h"
74#include "llvm/ADT/StringRef.h"
75#include "llvm/ADT/StringSet.h"
76#include "llvm/ADT/Twine.h"
77#include "llvm/ADT/iterator_range.h"
78#include "llvm/Bitstream/BitstreamWriter.h"
79#include "llvm/Support/Allocator.h"
80#include "llvm/Support/Casting.h"
81#include "llvm/Support/CrashRecoveryContext.h"
82#include "llvm/Support/DJB.h"
83#include "llvm/Support/ErrorHandling.h"
84#include "llvm/Support/ErrorOr.h"
85#include "llvm/Support/FileSystem.h"
86#include "llvm/Support/MemoryBuffer.h"
87#include "llvm/Support/SaveAndRestore.h"
88#include "llvm/Support/Timer.h"
89#include "llvm/Support/VirtualFileSystem.h"
90#include "llvm/Support/raw_ostream.h"
91#include <algorithm>
92#include <atomic>
93#include <cassert>
94#include <cstdint>
95#include <cstdio>
96#include <cstdlib>
97#include <memory>
98#include <mutex>
99#include <optional>
100#include <string>
101#include <tuple>
102#include <utility>
103#include <vector>
104
105using namespace clang;
106
107using llvm::TimeRecord;
108
109namespace {
110
111 class SimpleTimer {
112 bool WantTiming;
113 TimeRecord Start;
114 std::string Output;
115
116 public:
117 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
118 if (WantTiming)
119 Start = TimeRecord::getCurrentTime();
120 }
121
122 ~SimpleTimer() {
123 if (WantTiming) {
124 TimeRecord Elapsed = TimeRecord::getCurrentTime();
125 Elapsed -= Start;
126 llvm::errs() << Output << ':';
127 Elapsed.print(Total: Elapsed, OS&: llvm::errs());
128 llvm::errs() << '\n';
129 }
130 }
131
132 void setOutput(const Twine &Output) {
133 if (WantTiming)
134 this->Output = Output.str();
135 }
136 };
137
138} // namespace
139
140template <class T>
141static std::unique_ptr<T> valueOrNull(llvm::ErrorOr<std::unique_ptr<T>> Val) {
142 if (!Val)
143 return nullptr;
144 return std::move(*Val);
145}
146
147template <class T>
148static bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) {
149 if (!Val)
150 return false;
151 Output = std::move(*Val);
152 return true;
153}
154
155/// Get a source buffer for \p MainFilePath, handling all file-to-file
156/// and file-to-buffer remappings inside \p Invocation.
157static std::unique_ptr<llvm::MemoryBuffer>
158getBufferForFileHandlingRemapping(const CompilerInvocation &Invocation,
159 llvm::vfs::FileSystem *VFS,
160 StringRef FilePath, bool isVolatile) {
161 const auto &PreprocessorOpts = Invocation.getPreprocessorOpts();
162
163 // Try to determine if the main file has been remapped, either from the
164 // command line (to another file) or directly through the compiler
165 // invocation (to a memory buffer).
166 llvm::MemoryBuffer *Buffer = nullptr;
167 std::unique_ptr<llvm::MemoryBuffer> BufferOwner;
168 auto FileStatus = VFS->status(Path: FilePath);
169 if (FileStatus) {
170 llvm::sys::fs::UniqueID MainFileID = FileStatus->getUniqueID();
171
172 // Check whether there is a file-file remapping of the main file
173 for (const auto &RF : PreprocessorOpts.RemappedFiles) {
174 std::string MPath(RF.first);
175 auto MPathStatus = VFS->status(Path: MPath);
176 if (MPathStatus) {
177 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
178 if (MainFileID == MID) {
179 // We found a remapping. Try to load the resulting, remapped source.
180 BufferOwner = valueOrNull(Val: VFS->getBufferForFile(Name: RF.second, FileSize: -1, RequiresNullTerminator: true, IsVolatile: isVolatile));
181 if (!BufferOwner)
182 return nullptr;
183 }
184 }
185 }
186
187 // Check whether there is a file-buffer remapping. It supercedes the
188 // file-file remapping.
189 for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
190 std::string MPath(RB.first);
191 auto MPathStatus = VFS->status(Path: MPath);
192 if (MPathStatus) {
193 llvm::sys::fs::UniqueID MID = MPathStatus->getUniqueID();
194 if (MainFileID == MID) {
195 // We found a remapping.
196 BufferOwner.reset();
197 Buffer = const_cast<llvm::MemoryBuffer *>(RB.second);
198 }
199 }
200 }
201 }
202
203 // If the main source file was not remapped, load it now.
204 if (!Buffer && !BufferOwner) {
205 BufferOwner = valueOrNull(Val: VFS->getBufferForFile(Name: FilePath, FileSize: -1, RequiresNullTerminator: true, IsVolatile: isVolatile));
206 if (!BufferOwner)
207 return nullptr;
208 }
209
210 if (BufferOwner)
211 return BufferOwner;
212 if (!Buffer)
213 return nullptr;
214 return llvm::MemoryBuffer::getMemBufferCopy(InputData: Buffer->getBuffer(), BufferName: FilePath);
215}
216
217struct ASTUnit::ASTWriterData {
218 SmallString<128> Buffer;
219 llvm::BitstreamWriter Stream;
220 ASTWriter Writer;
221
222 ASTWriterData(InMemoryModuleCache &ModuleCache)
223 : Stream(Buffer), Writer(Stream, Buffer, ModuleCache, {}) {}
224};
225
226void ASTUnit::clearFileLevelDecls() {
227 FileDecls.clear();
228}
229
230/// After failing to build a precompiled preamble (due to
231/// errors in the source that occurs in the preamble), the number of
232/// reparses during which we'll skip even trying to precompile the
233/// preamble.
234const unsigned DefaultPreambleRebuildInterval = 5;
235
236/// Tracks the number of ASTUnit objects that are currently active.
237///
238/// Used for debugging purposes only.
239static std::atomic<unsigned> ActiveASTUnitObjects;
240
241ASTUnit::ASTUnit(bool _MainFileIsAST)
242 : MainFileIsAST(_MainFileIsAST), WantTiming(getenv(name: "LIBCLANG_TIMING")),
243 ShouldCacheCodeCompletionResults(false),
244 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false),
245 UnsafeToFree(false) {
246 if (getenv(name: "LIBCLANG_OBJTRACKING"))
247 fprintf(stderr, format: "+++ %u translation units\n", ++ActiveASTUnitObjects);
248}
249
250ASTUnit::~ASTUnit() {
251 // If we loaded from an AST file, balance out the BeginSourceFile call.
252 if (MainFileIsAST && getDiagnostics().getClient()) {
253 getDiagnostics().getClient()->EndSourceFile();
254 }
255
256 clearFileLevelDecls();
257
258 // Free the buffers associated with remapped files. We are required to
259 // perform this operation here because we explicitly request that the
260 // compiler instance *not* free these buffers for each invocation of the
261 // parser.
262 if (Invocation && OwnsRemappedFileBuffers) {
263 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
264 for (const auto &RB : PPOpts.RemappedFileBuffers)
265 delete RB.second;
266 }
267
268 ClearCachedCompletionResults();
269
270 if (getenv(name: "LIBCLANG_OBJTRACKING"))
271 fprintf(stderr, format: "--- %u translation units\n", --ActiveASTUnitObjects);
272}
273
274void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) {
275 this->PP = std::move(PP);
276}
277
278void ASTUnit::enableSourceFileDiagnostics() {
279 assert(getDiagnostics().getClient() && Ctx &&
280 "Bad context for source file");
281 getDiagnostics().getClient()->BeginSourceFile(LangOpts: Ctx->getLangOpts(), PP: PP.get());
282}
283
284/// Determine the set of code-completion contexts in which this
285/// declaration should be shown.
286static uint64_t getDeclShowContexts(const NamedDecl *ND,
287 const LangOptions &LangOpts,
288 bool &IsNestedNameSpecifier) {
289 IsNestedNameSpecifier = false;
290
291 if (isa<UsingShadowDecl>(Val: ND))
292 ND = ND->getUnderlyingDecl();
293 if (!ND)
294 return 0;
295
296 uint64_t Contexts = 0;
297 if (isa<TypeDecl>(Val: ND) || isa<ObjCInterfaceDecl>(Val: ND) ||
298 isa<ClassTemplateDecl>(Val: ND) || isa<TemplateTemplateParmDecl>(Val: ND) ||
299 isa<TypeAliasTemplateDecl>(Val: ND)) {
300 // Types can appear in these contexts.
301 if (LangOpts.CPlusPlus || !isa<TagDecl>(Val: ND))
302 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel)
303 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
304 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
305 | (1LL << CodeCompletionContext::CCC_Statement)
306 | (1LL << CodeCompletionContext::CCC_Type)
307 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
308
309 // In C++, types can appear in expressions contexts (for functional casts).
310 if (LangOpts.CPlusPlus)
311 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
312
313 // In Objective-C, message sends can send interfaces. In Objective-C++,
314 // all types are available due to functional casts.
315 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(Val: ND))
316 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
317
318 // In Objective-C, you can only be a subclass of another Objective-C class
319 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(Val: ND)) {
320 // Objective-C interfaces can be used in a class property expression.
321 if (ID->getDefinition())
322 Contexts |= (1LL << CodeCompletionContext::CCC_Expression);
323 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName);
324 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCClassForwardDecl);
325 }
326
327 // Deal with tag names.
328 if (isa<EnumDecl>(Val: ND)) {
329 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag);
330
331 // Part of the nested-name-specifier in C++0x.
332 if (LangOpts.CPlusPlus11)
333 IsNestedNameSpecifier = true;
334 } else if (const auto *Record = dyn_cast<RecordDecl>(Val: ND)) {
335 if (Record->isUnion())
336 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag);
337 else
338 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
339
340 if (LangOpts.CPlusPlus)
341 IsNestedNameSpecifier = true;
342 } else if (isa<ClassTemplateDecl>(Val: ND))
343 IsNestedNameSpecifier = true;
344 } else if (isa<ValueDecl>(Val: ND) || isa<FunctionTemplateDecl>(Val: ND)) {
345 // Values can appear in these contexts.
346 Contexts = (1LL << CodeCompletionContext::CCC_Statement)
347 | (1LL << CodeCompletionContext::CCC_Expression)
348 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
349 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver);
350 } else if (isa<ObjCProtocolDecl>(Val: ND)) {
351 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName);
352 } else if (isa<ObjCCategoryDecl>(Val: ND)) {
353 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName);
354 } else if (isa<NamespaceDecl>(Val: ND) || isa<NamespaceAliasDecl>(Val: ND)) {
355 Contexts = (1LL << CodeCompletionContext::CCC_Namespace);
356
357 // Part of the nested-name-specifier.
358 IsNestedNameSpecifier = true;
359 }
360
361 return Contexts;
362}
363
364void ASTUnit::CacheCodeCompletionResults() {
365 if (!TheSema)
366 return;
367
368 SimpleTimer Timer(WantTiming);
369 Timer.setOutput("Cache global code completions for " + getMainFileName());
370
371 // Clear out the previous results.
372 ClearCachedCompletionResults();
373
374 // Gather the set of global code completions.
375 using Result = CodeCompletionResult;
376 SmallVector<Result, 8> Results;
377 CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
378 CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator);
379 TheSema->CodeCompletion().GatherGlobalCodeCompletions(
380 Allocator&: *CachedCompletionAllocator, CCTUInfo, Results);
381
382 // Translate global code completions into cached completions.
383 llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
384 CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel);
385
386 for (auto &R : Results) {
387 switch (R.Kind) {
388 case Result::RK_Declaration: {
389 bool IsNestedNameSpecifier = false;
390 CachedCodeCompletionResult CachedResult;
391 CachedResult.Completion = R.CreateCodeCompletionString(
392 S&: *TheSema, CCContext, Allocator&: *CachedCompletionAllocator, CCTUInfo,
393 IncludeBriefComments: IncludeBriefCommentsInCodeCompletion);
394 CachedResult.ShowInContexts = getDeclShowContexts(
395 ND: R.Declaration, LangOpts: Ctx->getLangOpts(), IsNestedNameSpecifier);
396 CachedResult.Priority = R.Priority;
397 CachedResult.Kind = R.CursorKind;
398 CachedResult.Availability = R.Availability;
399
400 // Keep track of the type of this completion in an ASTContext-agnostic
401 // way.
402 QualType UsageType = getDeclUsageType(C&: *Ctx, ND: R.Declaration);
403 if (UsageType.isNull()) {
404 CachedResult.TypeClass = STC_Void;
405 CachedResult.Type = 0;
406 } else {
407 CanQualType CanUsageType
408 = Ctx->getCanonicalType(T: UsageType.getUnqualifiedType());
409 CachedResult.TypeClass = getSimplifiedTypeClass(T: CanUsageType);
410
411 // Determine whether we have already seen this type. If so, we save
412 // ourselves the work of formatting the type string by using the
413 // temporary, CanQualType-based hash table to find the associated value.
414 unsigned &TypeValue = CompletionTypes[CanUsageType];
415 if (TypeValue == 0) {
416 TypeValue = CompletionTypes.size();
417 CachedCompletionTypes[QualType(CanUsageType).getAsString()]
418 = TypeValue;
419 }
420
421 CachedResult.Type = TypeValue;
422 }
423
424 CachedCompletionResults.push_back(x: CachedResult);
425
426 /// Handle nested-name-specifiers in C++.
427 if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier &&
428 !R.StartsNestedNameSpecifier) {
429 // The contexts in which a nested-name-specifier can appear in C++.
430 uint64_t NNSContexts
431 = (1LL << CodeCompletionContext::CCC_TopLevel)
432 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
433 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
434 | (1LL << CodeCompletionContext::CCC_Statement)
435 | (1LL << CodeCompletionContext::CCC_Expression)
436 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
437 | (1LL << CodeCompletionContext::CCC_EnumTag)
438 | (1LL << CodeCompletionContext::CCC_UnionTag)
439 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
440 | (1LL << CodeCompletionContext::CCC_Type)
441 | (1LL << CodeCompletionContext::CCC_SymbolOrNewName)
442 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
443
444 if (isa<NamespaceDecl>(Val: R.Declaration) ||
445 isa<NamespaceAliasDecl>(Val: R.Declaration))
446 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
447
448 if (uint64_t RemainingContexts
449 = NNSContexts & ~CachedResult.ShowInContexts) {
450 // If there any contexts where this completion can be a
451 // nested-name-specifier but isn't already an option, create a
452 // nested-name-specifier completion.
453 R.StartsNestedNameSpecifier = true;
454 CachedResult.Completion = R.CreateCodeCompletionString(
455 S&: *TheSema, CCContext, Allocator&: *CachedCompletionAllocator, CCTUInfo,
456 IncludeBriefComments: IncludeBriefCommentsInCodeCompletion);
457 CachedResult.ShowInContexts = RemainingContexts;
458 CachedResult.Priority = CCP_NestedNameSpecifier;
459 CachedResult.TypeClass = STC_Void;
460 CachedResult.Type = 0;
461 CachedCompletionResults.push_back(x: CachedResult);
462 }
463 }
464 break;
465 }
466
467 case Result::RK_Keyword:
468 case Result::RK_Pattern:
469 // Ignore keywords and patterns; we don't care, since they are so
470 // easily regenerated.
471 break;
472
473 case Result::RK_Macro: {
474 CachedCodeCompletionResult CachedResult;
475 CachedResult.Completion = R.CreateCodeCompletionString(
476 S&: *TheSema, CCContext, Allocator&: *CachedCompletionAllocator, CCTUInfo,
477 IncludeBriefComments: IncludeBriefCommentsInCodeCompletion);
478 CachedResult.ShowInContexts
479 = (1LL << CodeCompletionContext::CCC_TopLevel)
480 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
481 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
482 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
483 | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
484 | (1LL << CodeCompletionContext::CCC_Statement)
485 | (1LL << CodeCompletionContext::CCC_Expression)
486 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
487 | (1LL << CodeCompletionContext::CCC_MacroNameUse)
488 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
489 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
490 | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
491
492 CachedResult.Priority = R.Priority;
493 CachedResult.Kind = R.CursorKind;
494 CachedResult.Availability = R.Availability;
495 CachedResult.TypeClass = STC_Void;
496 CachedResult.Type = 0;
497 CachedCompletionResults.push_back(x: CachedResult);
498 break;
499 }
500 }
501 }
502
503 // Save the current top-level hash value.
504 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
505}
506
507void ASTUnit::ClearCachedCompletionResults() {
508 CachedCompletionResults.clear();
509 CachedCompletionTypes.clear();
510 CachedCompletionAllocator = nullptr;
511}
512
513namespace {
514
515/// Gathers information from ASTReader that will be used to initialize
516/// a Preprocessor.
517class ASTInfoCollector : public ASTReaderListener {
518 Preprocessor &PP;
519 ASTContext *Context;
520 HeaderSearchOptions &HSOpts;
521 PreprocessorOptions &PPOpts;
522 LangOptions &LangOpt;
523 std::shared_ptr<TargetOptions> &TargetOpts;
524 IntrusiveRefCntPtr<TargetInfo> &Target;
525 unsigned &Counter;
526 bool InitializedLanguage = false;
527 bool InitializedHeaderSearchPaths = false;
528
529public:
530 ASTInfoCollector(Preprocessor &PP, ASTContext *Context,
531 HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts,
532 LangOptions &LangOpt,
533 std::shared_ptr<TargetOptions> &TargetOpts,
534 IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter)
535 : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts),
536 LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target),
537 Counter(Counter) {}
538
539 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
540 bool AllowCompatibleDifferences) override {
541 if (InitializedLanguage)
542 return false;
543
544 // FIXME: We did similar things in ReadHeaderSearchOptions too. But such
545 // style is not scaling. Probably we need to invite some mechanism to
546 // handle such patterns generally.
547 auto PICLevel = LangOpt.PICLevel;
548 auto PIE = LangOpt.PIE;
549
550 LangOpt = LangOpts;
551
552 LangOpt.PICLevel = PICLevel;
553 LangOpt.PIE = PIE;
554
555 InitializedLanguage = true;
556
557 updated();
558 return false;
559 }
560
561 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
562 StringRef SpecificModuleCachePath,
563 bool Complain) override {
564 // llvm::SaveAndRestore doesn't support bit field.
565 auto ForceCheckCXX20ModulesInputFiles =
566 this->HSOpts.ForceCheckCXX20ModulesInputFiles;
567 llvm::SaveAndRestore X(this->HSOpts.UserEntries);
568 llvm::SaveAndRestore Y(this->HSOpts.SystemHeaderPrefixes);
569 llvm::SaveAndRestore Z(this->HSOpts.VFSOverlayFiles);
570
571 this->HSOpts = HSOpts;
572 this->HSOpts.ForceCheckCXX20ModulesInputFiles =
573 ForceCheckCXX20ModulesInputFiles;
574
575 return false;
576 }
577
578 bool ReadHeaderSearchPaths(const HeaderSearchOptions &HSOpts,
579 bool Complain) override {
580 if (InitializedHeaderSearchPaths)
581 return false;
582
583 this->HSOpts.UserEntries = HSOpts.UserEntries;
584 this->HSOpts.SystemHeaderPrefixes = HSOpts.SystemHeaderPrefixes;
585 this->HSOpts.VFSOverlayFiles = HSOpts.VFSOverlayFiles;
586
587 // Initialize the FileManager. We can't do this in update(), since that
588 // performs the initialization too late (once both target and language
589 // options are read).
590 PP.getFileManager().setVirtualFileSystem(createVFSFromOverlayFiles(
591 VFSOverlayFiles: HSOpts.VFSOverlayFiles, Diags&: PP.getDiagnostics(),
592 BaseFS: PP.getFileManager().getVirtualFileSystemPtr()));
593
594 InitializedHeaderSearchPaths = true;
595
596 return false;
597 }
598
599 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
600 bool ReadMacros, bool Complain,
601 std::string &SuggestedPredefines) override {
602 this->PPOpts = PPOpts;
603 return false;
604 }
605
606 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
607 bool AllowCompatibleDifferences) override {
608 // If we've already initialized the target, don't do it again.
609 if (Target)
610 return false;
611
612 this->TargetOpts = std::make_shared<TargetOptions>(args: TargetOpts);
613 Target =
614 TargetInfo::CreateTargetInfo(Diags&: PP.getDiagnostics(), Opts: this->TargetOpts);
615
616 updated();
617 return false;
618 }
619
620 void ReadCounter(const serialization::ModuleFile &M,
621 unsigned Value) override {
622 Counter = Value;
623 }
624
625private:
626 void updated() {
627 if (!Target || !InitializedLanguage)
628 return;
629
630 // Inform the target of the language options.
631 //
632 // FIXME: We shouldn't need to do this, the target should be immutable once
633 // created. This complexity should be lifted elsewhere.
634 Target->adjust(Diags&: PP.getDiagnostics(), Opts&: LangOpt);
635
636 // Initialize the preprocessor.
637 PP.Initialize(Target: *Target);
638
639 if (!Context)
640 return;
641
642 // Initialize the ASTContext
643 Context->InitBuiltinTypes(Target: *Target);
644
645 // Adjust printing policy based on language options.
646 Context->setPrintingPolicy(PrintingPolicy(LangOpt));
647
648 // We didn't have access to the comment options when the ASTContext was
649 // constructed, so register them now.
650 Context->getCommentCommandTraits().registerCommentOptions(
651 CommentOptions: LangOpt.CommentOpts);
652 }
653};
654
655/// Diagnostic consumer that saves each diagnostic it is given.
656class FilterAndStoreDiagnosticConsumer : public DiagnosticConsumer {
657 SmallVectorImpl<StoredDiagnostic> *StoredDiags;
658 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags;
659 bool CaptureNonErrorsFromIncludes = true;
660 const LangOptions *LangOpts = nullptr;
661 SourceManager *SourceMgr = nullptr;
662
663public:
664 FilterAndStoreDiagnosticConsumer(
665 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
666 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags,
667 bool CaptureNonErrorsFromIncludes)
668 : StoredDiags(StoredDiags), StandaloneDiags(StandaloneDiags),
669 CaptureNonErrorsFromIncludes(CaptureNonErrorsFromIncludes) {
670 assert((StoredDiags || StandaloneDiags) &&
671 "No output collections were passed to StoredDiagnosticConsumer.");
672 }
673
674 void BeginSourceFile(const LangOptions &LangOpts,
675 const Preprocessor *PP = nullptr) override {
676 this->LangOpts = &LangOpts;
677 if (PP)
678 SourceMgr = &PP->getSourceManager();
679 }
680
681 void HandleDiagnostic(DiagnosticsEngine::Level Level,
682 const Diagnostic &Info) override;
683};
684
685/// RAII object that optionally captures and filters diagnostics, if
686/// there is no diagnostic client to capture them already.
687class CaptureDroppedDiagnostics {
688 DiagnosticsEngine &Diags;
689 FilterAndStoreDiagnosticConsumer Client;
690 DiagnosticConsumer *PreviousClient = nullptr;
691 std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
692
693public:
694 CaptureDroppedDiagnostics(
695 CaptureDiagsKind CaptureDiagnostics, DiagnosticsEngine &Diags,
696 SmallVectorImpl<StoredDiagnostic> *StoredDiags,
697 SmallVectorImpl<ASTUnit::StandaloneDiagnostic> *StandaloneDiags)
698 : Diags(Diags),
699 Client(StoredDiags, StandaloneDiags,
700 CaptureDiagnostics !=
701 CaptureDiagsKind::AllWithoutNonErrorsFromIncludes) {
702 if (CaptureDiagnostics != CaptureDiagsKind::None ||
703 Diags.getClient() == nullptr) {
704 OwningPreviousClient = Diags.takeClient();
705 PreviousClient = Diags.getClient();
706 Diags.setClient(client: &Client, ShouldOwnClient: false);
707 }
708 }
709
710 ~CaptureDroppedDiagnostics() {
711 if (Diags.getClient() == &Client)
712 Diags.setClient(client: PreviousClient, ShouldOwnClient: !!OwningPreviousClient.release());
713 }
714};
715
716} // namespace
717
718static ASTUnit::StandaloneDiagnostic
719makeStandaloneDiagnostic(const LangOptions &LangOpts,
720 const StoredDiagnostic &InDiag);
721
722static bool isInMainFile(const clang::Diagnostic &D) {
723 if (!D.hasSourceManager() || !D.getLocation().isValid())
724 return false;
725
726 auto &M = D.getSourceManager();
727 return M.isWrittenInMainFile(Loc: M.getExpansionLoc(Loc: D.getLocation()));
728}
729
730void FilterAndStoreDiagnosticConsumer::HandleDiagnostic(
731 DiagnosticsEngine::Level Level, const Diagnostic &Info) {
732 // Default implementation (Warnings/errors count).
733 DiagnosticConsumer::HandleDiagnostic(DiagLevel: Level, Info);
734
735 // Only record the diagnostic if it's part of the source manager we know
736 // about. This effectively drops diagnostics from modules we're building.
737 // FIXME: In the long run, ee don't want to drop source managers from modules.
738 if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) {
739 if (!CaptureNonErrorsFromIncludes && Level <= DiagnosticsEngine::Warning &&
740 !isInMainFile(D: Info)) {
741 return;
742 }
743
744 StoredDiagnostic *ResultDiag = nullptr;
745 if (StoredDiags) {
746 StoredDiags->emplace_back(Args&: Level, Args: Info);
747 ResultDiag = &StoredDiags->back();
748 }
749
750 if (StandaloneDiags) {
751 std::optional<StoredDiagnostic> StoredDiag;
752 if (!ResultDiag) {
753 StoredDiag.emplace(args&: Level, args: Info);
754 ResultDiag = &*StoredDiag;
755 }
756 StandaloneDiags->push_back(
757 Elt: makeStandaloneDiagnostic(LangOpts: *LangOpts, InDiag: *ResultDiag));
758 }
759 }
760}
761
762IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const {
763 return Reader;
764}
765
766ASTMutationListener *ASTUnit::getASTMutationListener() {
767 if (WriterData)
768 return &WriterData->Writer;
769 return nullptr;
770}
771
772ASTDeserializationListener *ASTUnit::getDeserializationListener() {
773 if (WriterData)
774 return &WriterData->Writer;
775 return nullptr;
776}
777
778std::unique_ptr<llvm::MemoryBuffer>
779ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
780 assert(FileMgr);
781 auto Buffer = FileMgr->getBufferForFile(Filename, isVolatile: UserFilesAreVolatile);
782 if (Buffer)
783 return std::move(*Buffer);
784 if (ErrorStr)
785 *ErrorStr = Buffer.getError().message();
786 return nullptr;
787}
788
789/// Configure the diagnostics object for use with ASTUnit.
790void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
791 ASTUnit &AST,
792 CaptureDiagsKind CaptureDiagnostics) {
793 assert(Diags.get() && "no DiagnosticsEngine was provided");
794 if (CaptureDiagnostics != CaptureDiagsKind::None)
795 Diags->setClient(client: new FilterAndStoreDiagnosticConsumer(
796 &AST.StoredDiagnostics, nullptr,
797 CaptureDiagnostics != CaptureDiagsKind::AllWithoutNonErrorsFromIncludes));
798}
799
800std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
801 const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
802 WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
803 const FileSystemOptions &FileSystemOpts,
804 std::shared_ptr<HeaderSearchOptions> HSOpts,
805 std::shared_ptr<LangOptions> LangOpts, bool OnlyLocalDecls,
806 CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
807 bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
808 std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
809
810 // Recover resources if we crash before exiting this method.
811 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
812 ASTUnitCleanup(AST.get());
813 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
814 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
815 DiagCleanup(Diags.get());
816
817 ConfigureDiags(Diags, AST&: *AST, CaptureDiagnostics);
818
819 AST->LangOpts = LangOpts ? LangOpts : std::make_shared<LangOptions>();
820 AST->OnlyLocalDecls = OnlyLocalDecls;
821 AST->CaptureDiagnostics = CaptureDiagnostics;
822 AST->Diagnostics = Diags;
823 AST->FileMgr = new FileManager(FileSystemOpts, VFS);
824 AST->UserFilesAreVolatile = UserFilesAreVolatile;
825 AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
826 AST->getFileManager(),
827 UserFilesAreVolatile);
828 AST->ModuleCache = new InMemoryModuleCache;
829 AST->HSOpts = HSOpts ? HSOpts : std::make_shared<HeaderSearchOptions>();
830 AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
831 AST->HeaderInfo.reset(p: new HeaderSearch(AST->HSOpts,
832 AST->getSourceManager(),
833 AST->getDiagnostics(),
834 AST->getLangOpts(),
835 /*Target=*/nullptr));
836 AST->PPOpts = std::make_shared<PreprocessorOptions>();
837
838 // Gather Info for preprocessor construction later on.
839
840 HeaderSearch &HeaderInfo = *AST->HeaderInfo;
841
842 AST->PP = std::make_shared<Preprocessor>(
843 args&: AST->PPOpts, args&: AST->getDiagnostics(), args&: *AST->LangOpts,
844 args&: AST->getSourceManager(), args&: HeaderInfo, args&: AST->ModuleLoader,
845 /*IILookup=*/args: nullptr,
846 /*OwnsHeaderSearch=*/args: false);
847 Preprocessor &PP = *AST->PP;
848
849 if (ToLoad >= LoadASTOnly)
850 AST->Ctx = new ASTContext(*AST->LangOpts, AST->getSourceManager(),
851 PP.getIdentifierTable(), PP.getSelectorTable(),
852 PP.getBuiltinInfo(),
853 AST->getTranslationUnitKind());
854
855 DisableValidationForModuleKind disableValid =
856 DisableValidationForModuleKind::None;
857 if (::getenv(name: "LIBCLANG_DISABLE_PCH_VALIDATION"))
858 disableValid = DisableValidationForModuleKind::All;
859 AST->Reader = new ASTReader(
860 PP, *AST->ModuleCache, AST->Ctx.get(), PCHContainerRdr, {},
861 /*isysroot=*/"",
862 /*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors);
863
864 unsigned Counter = 0;
865 AST->Reader->setListener(std::make_unique<ASTInfoCollector>(
866 args&: *AST->PP, args: AST->Ctx.get(), args&: *AST->HSOpts, args&: *AST->PPOpts, args&: *AST->LangOpts,
867 args&: AST->TargetOpts, args&: AST->Target, args&: Counter));
868
869 // Attach the AST reader to the AST context as an external AST
870 // source, so that declarations will be deserialized from the
871 // AST file as needed.
872 // We need the external source to be set up before we read the AST, because
873 // eagerly-deserialized declarations may use it.
874 if (AST->Ctx)
875 AST->Ctx->setExternalSource(AST->Reader);
876
877 switch (AST->Reader->ReadAST(FileName: Filename, Type: serialization::MK_MainFile,
878 ImportLoc: SourceLocation(), ClientLoadCapabilities: ASTReader::ARR_None)) {
879 case ASTReader::Success:
880 break;
881
882 case ASTReader::Failure:
883 case ASTReader::Missing:
884 case ASTReader::OutOfDate:
885 case ASTReader::VersionMismatch:
886 case ASTReader::ConfigurationMismatch:
887 case ASTReader::HadErrors:
888 AST->getDiagnostics().Report(DiagID: diag::err_fe_unable_to_load_pch);
889 return nullptr;
890 }
891
892 AST->OriginalSourceFile = std::string(AST->Reader->getOriginalSourceFile());
893
894 PP.setCounterValue(Counter);
895
896 Module *M = HeaderInfo.lookupModule(ModuleName: AST->getLangOpts().CurrentModule);
897 if (M && AST->getLangOpts().isCompilingModule() && M->isNamedModule())
898 AST->Ctx->setCurrentNamedModule(M);
899
900 // Create an AST consumer, even though it isn't used.
901 if (ToLoad >= LoadASTOnly)
902 AST->Consumer.reset(p: new ASTConsumer);
903
904 // Create a semantic analysis object and tell the AST reader about it.
905 if (ToLoad >= LoadEverything) {
906 AST->TheSema.reset(p: new Sema(PP, *AST->Ctx, *AST->Consumer));
907 AST->TheSema->Initialize();
908 AST->Reader->InitializeSema(S&: *AST->TheSema);
909 }
910
911 // Tell the diagnostic client that we have started a source file.
912 AST->getDiagnostics().getClient()->BeginSourceFile(LangOpts: PP.getLangOpts(), PP: &PP);
913
914 return AST;
915}
916
917/// Add the given macro to the hash of all top-level entities.
918static void AddDefinedMacroToHash(const Token &MacroNameTok, unsigned &Hash) {
919 Hash = llvm::djbHash(Buffer: MacroNameTok.getIdentifierInfo()->getName(), H: Hash);
920}
921
922namespace {
923
924/// Preprocessor callback class that updates a hash value with the names
925/// of all macros that have been defined by the translation unit.
926class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
927 unsigned &Hash;
928
929public:
930 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {}
931
932 void MacroDefined(const Token &MacroNameTok,
933 const MacroDirective *MD) override {
934 AddDefinedMacroToHash(MacroNameTok, Hash);
935 }
936};
937
938} // namespace
939
940/// Add the given declaration to the hash of all top-level entities.
941static void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
942 if (!D)
943 return;
944
945 DeclContext *DC = D->getDeclContext();
946 if (!DC)
947 return;
948
949 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
950 return;
951
952 if (const auto *ND = dyn_cast<NamedDecl>(Val: D)) {
953 if (const auto *EnumD = dyn_cast<EnumDecl>(Val: D)) {
954 // For an unscoped enum include the enumerators in the hash since they
955 // enter the top-level namespace.
956 if (!EnumD->isScoped()) {
957 for (const auto *EI : EnumD->enumerators()) {
958 if (EI->getIdentifier())
959 Hash = llvm::djbHash(Buffer: EI->getIdentifier()->getName(), H: Hash);
960 }
961 }
962 }
963
964 if (ND->getIdentifier())
965 Hash = llvm::djbHash(Buffer: ND->getIdentifier()->getName(), H: Hash);
966 else if (DeclarationName Name = ND->getDeclName()) {
967 std::string NameStr = Name.getAsString();
968 Hash = llvm::djbHash(Buffer: NameStr, H: Hash);
969 }
970 return;
971 }
972
973 if (const auto *ImportD = dyn_cast<ImportDecl>(Val: D)) {
974 if (const Module *Mod = ImportD->getImportedModule()) {
975 std::string ModName = Mod->getFullModuleName();
976 Hash = llvm::djbHash(Buffer: ModName, H: Hash);
977 }
978 return;
979 }
980}
981
982namespace {
983
984class TopLevelDeclTrackerConsumer : public ASTConsumer {
985 ASTUnit &Unit;
986 unsigned &Hash;
987
988public:
989 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
990 : Unit(_Unit), Hash(Hash) {
991 Hash = 0;
992 }
993
994 void handleTopLevelDecl(Decl *D) {
995 if (!D)
996 return;
997
998 // FIXME: Currently ObjC method declarations are incorrectly being
999 // reported as top-level declarations, even though their DeclContext
1000 // is the containing ObjC @interface/@implementation. This is a
1001 // fundamental problem in the parser right now.
1002 if (isa<ObjCMethodDecl>(Val: D))
1003 return;
1004
1005 AddTopLevelDeclarationToHash(D, Hash);
1006 Unit.addTopLevelDecl(D);
1007
1008 handleFileLevelDecl(D);
1009 }
1010
1011 void handleFileLevelDecl(Decl *D) {
1012 Unit.addFileLevelDecl(D);
1013 if (auto *NSD = dyn_cast<NamespaceDecl>(Val: D)) {
1014 for (auto *I : NSD->decls())
1015 handleFileLevelDecl(D: I);
1016 }
1017 }
1018
1019 bool HandleTopLevelDecl(DeclGroupRef D) override {
1020 for (auto *TopLevelDecl : D)
1021 handleTopLevelDecl(D: TopLevelDecl);
1022 return true;
1023 }
1024
1025 // We're not interested in "interesting" decls.
1026 void HandleInterestingDecl(DeclGroupRef) override {}
1027
1028 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
1029 for (auto *TopLevelDecl : D)
1030 handleTopLevelDecl(D: TopLevelDecl);
1031 }
1032
1033 ASTMutationListener *GetASTMutationListener() override {
1034 return Unit.getASTMutationListener();
1035 }
1036
1037 ASTDeserializationListener *GetASTDeserializationListener() override {
1038 return Unit.getDeserializationListener();
1039 }
1040};
1041
1042class TopLevelDeclTrackerAction : public ASTFrontendAction {
1043public:
1044 ASTUnit &Unit;
1045
1046 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
1047 StringRef InFile) override {
1048 CI.getPreprocessor().addPPCallbacks(
1049 C: std::make_unique<MacroDefinitionTrackerPPCallbacks>(
1050 args&: Unit.getCurrentTopLevelHashValue()));
1051 return std::make_unique<TopLevelDeclTrackerConsumer>(
1052 args&: Unit, args&: Unit.getCurrentTopLevelHashValue());
1053 }
1054
1055public:
1056 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
1057
1058 bool hasCodeCompletionSupport() const override { return false; }
1059
1060 TranslationUnitKind getTranslationUnitKind() override {
1061 return Unit.getTranslationUnitKind();
1062 }
1063};
1064
1065class ASTUnitPreambleCallbacks : public PreambleCallbacks {
1066public:
1067 unsigned getHash() const { return Hash; }
1068
1069 std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
1070
1071 std::vector<LocalDeclID> takeTopLevelDeclIDs() {
1072 return std::move(TopLevelDeclIDs);
1073 }
1074
1075 void AfterPCHEmitted(ASTWriter &Writer) override {
1076 TopLevelDeclIDs.reserve(n: TopLevelDecls.size());
1077 for (const auto *D : TopLevelDecls) {
1078 // Invalid top-level decls may not have been serialized.
1079 if (D->isInvalidDecl())
1080 continue;
1081 TopLevelDeclIDs.push_back(x: Writer.getDeclID(D));
1082 }
1083 }
1084
1085 void HandleTopLevelDecl(DeclGroupRef DG) override {
1086 for (auto *D : DG) {
1087 // FIXME: Currently ObjC method declarations are incorrectly being
1088 // reported as top-level declarations, even though their DeclContext
1089 // is the containing ObjC @interface/@implementation. This is a
1090 // fundamental problem in the parser right now.
1091 if (isa<ObjCMethodDecl>(Val: D))
1092 continue;
1093 AddTopLevelDeclarationToHash(D, Hash);
1094 TopLevelDecls.push_back(x: D);
1095 }
1096 }
1097
1098 std::unique_ptr<PPCallbacks> createPPCallbacks() override {
1099 return std::make_unique<MacroDefinitionTrackerPPCallbacks>(args&: Hash);
1100 }
1101
1102private:
1103 unsigned Hash = 0;
1104 std::vector<Decl *> TopLevelDecls;
1105 std::vector<LocalDeclID> TopLevelDeclIDs;
1106 llvm::SmallVector<ASTUnit::StandaloneDiagnostic, 4> PreambleDiags;
1107};
1108
1109} // namespace
1110
1111static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1112 return StoredDiag.getLocation().isValid();
1113}
1114
1115static void
1116checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1117 // Get rid of stored diagnostics except the ones from the driver which do not
1118 // have a source location.
1119 llvm::erase_if(C&: StoredDiags, P: isNonDriverDiag);
1120}
1121
1122static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1123 StoredDiagnostics,
1124 SourceManager &SM) {
1125 // The stored diagnostic has the old source manager in it; update
1126 // the locations to refer into the new source manager. Since we've
1127 // been careful to make sure that the source manager's state
1128 // before and after are identical, so that we can reuse the source
1129 // location itself.
1130 for (auto &SD : StoredDiagnostics) {
1131 if (SD.getLocation().isValid()) {
1132 FullSourceLoc Loc(SD.getLocation(), SM);
1133 SD.setLocation(Loc);
1134 }
1135 }
1136}
1137
1138/// Parse the source file into a translation unit using the given compiler
1139/// invocation, replacing the current translation unit.
1140///
1141/// \returns True if a failure occurred that causes the ASTUnit not to
1142/// contain any translation-unit information, false otherwise.
1143bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1144 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
1145 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1146 if (!Invocation)
1147 return true;
1148
1149 if (VFS && FileMgr)
1150 assert(VFS == &FileMgr->getVirtualFileSystem() &&
1151 "VFS passed to Parse and VFS in FileMgr are different");
1152
1153 auto CCInvocation = std::make_shared<CompilerInvocation>(args&: *Invocation);
1154 if (OverrideMainBuffer) {
1155 assert(Preamble &&
1156 "No preamble was built, but OverrideMainBuffer is not null");
1157 Preamble->AddImplicitPreamble(CI&: *CCInvocation, VFS, MainFileBuffer: OverrideMainBuffer.get());
1158 // VFS may have changed...
1159 }
1160
1161 // Create the compiler instance to use for building the AST.
1162 std::unique_ptr<CompilerInstance> Clang(
1163 new CompilerInstance(std::move(PCHContainerOps)));
1164 Clang->setInvocation(CCInvocation);
1165
1166 // Clean up on error, disengage it if the function returns successfully.
1167 auto CleanOnError = llvm::make_scope_exit(F: [&]() {
1168 // Remove the overridden buffer we used for the preamble.
1169 SavedMainFileBuffer = nullptr;
1170
1171 // Keep the ownership of the data in the ASTUnit because the client may
1172 // want to see the diagnostics.
1173 transferASTDataFromCompilerInstance(CI&: *Clang);
1174 FailedParseDiagnostics.swap(RHS&: StoredDiagnostics);
1175 StoredDiagnostics.clear();
1176 NumStoredDiagnosticsFromDriver = 0;
1177 });
1178
1179 // Ensure that Clang has a FileManager with the right VFS, which may have
1180 // changed above in AddImplicitPreamble. If VFS is nullptr, rely on
1181 // createFileManager to create one.
1182 if (VFS && FileMgr && &FileMgr->getVirtualFileSystem() == VFS)
1183 Clang->setFileManager(&*FileMgr);
1184 else
1185 FileMgr = Clang->createFileManager(VFS: std::move(VFS));
1186
1187 // Recover resources if we crash before exiting this method.
1188 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1189 CICleanup(Clang.get());
1190
1191 OriginalSourceFile =
1192 std::string(Clang->getFrontendOpts().Inputs[0].getFile());
1193
1194 // Set up diagnostics, capturing any diagnostics that would
1195 // otherwise be dropped.
1196 Clang->setDiagnostics(&getDiagnostics());
1197
1198 // Create the target instance.
1199 if (!Clang->createTarget())
1200 return true;
1201
1202 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1203 "Invocation must have exactly one source file!");
1204 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1205 InputKind::Source &&
1206 "FIXME: AST inputs not yet supported here!");
1207 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1208 Language::LLVM_IR &&
1209 "IR inputs not support here!");
1210
1211 // Configure the various subsystems.
1212 LangOpts = Clang->getInvocation().LangOpts;
1213 FileSystemOpts = Clang->getFileSystemOpts();
1214
1215 ResetForParse();
1216
1217 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1218 UserFilesAreVolatile);
1219 if (!OverrideMainBuffer) {
1220 checkAndRemoveNonDriverDiags(StoredDiags&: StoredDiagnostics);
1221 TopLevelDeclsInPreamble.clear();
1222 }
1223
1224 // Create the source manager.
1225 Clang->setSourceManager(&getSourceManager());
1226
1227 // If the main file has been overridden due to the use of a preamble,
1228 // make that override happen and introduce the preamble.
1229 if (OverrideMainBuffer) {
1230 // The stored diagnostic has the old source manager in it; update
1231 // the locations to refer into the new source manager. Since we've
1232 // been careful to make sure that the source manager's state
1233 // before and after are identical, so that we can reuse the source
1234 // location itself.
1235 checkAndSanitizeDiags(StoredDiagnostics, SM&: getSourceManager());
1236
1237 // Keep track of the override buffer;
1238 SavedMainFileBuffer = std::move(OverrideMainBuffer);
1239 }
1240
1241 std::unique_ptr<TopLevelDeclTrackerAction> Act(
1242 new TopLevelDeclTrackerAction(*this));
1243
1244 // Recover resources if we crash before exiting this method.
1245 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1246 ActCleanup(Act.get());
1247
1248 if (!Act->BeginSourceFile(CI&: *Clang.get(), Input: Clang->getFrontendOpts().Inputs[0]))
1249 return true;
1250
1251 if (SavedMainFileBuffer)
1252 TranslateStoredDiagnostics(FileMgr&: getFileManager(), SrcMan&: getSourceManager(),
1253 Diags: PreambleDiagnostics, Out&: StoredDiagnostics);
1254 else
1255 PreambleSrcLocCache.clear();
1256
1257 if (llvm::Error Err = Act->Execute()) {
1258 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
1259 return true;
1260 }
1261
1262 transferASTDataFromCompilerInstance(CI&: *Clang);
1263
1264 Act->EndSourceFile();
1265
1266 FailedParseDiagnostics.clear();
1267
1268 CleanOnError.release();
1269
1270 return false;
1271}
1272
1273static std::pair<unsigned, unsigned>
1274makeStandaloneRange(CharSourceRange Range, const SourceManager &SM,
1275 const LangOptions &LangOpts) {
1276 CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts);
1277 unsigned Offset = SM.getFileOffset(SpellingLoc: FileRange.getBegin());
1278 unsigned EndOffset = SM.getFileOffset(SpellingLoc: FileRange.getEnd());
1279 return std::make_pair(x&: Offset, y&: EndOffset);
1280}
1281
1282static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM,
1283 const LangOptions &LangOpts,
1284 const FixItHint &InFix) {
1285 ASTUnit::StandaloneFixIt OutFix;
1286 OutFix.RemoveRange = makeStandaloneRange(Range: InFix.RemoveRange, SM, LangOpts);
1287 OutFix.InsertFromRange = makeStandaloneRange(Range: InFix.InsertFromRange, SM,
1288 LangOpts);
1289 OutFix.CodeToInsert = InFix.CodeToInsert;
1290 OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions;
1291 return OutFix;
1292}
1293
1294static ASTUnit::StandaloneDiagnostic
1295makeStandaloneDiagnostic(const LangOptions &LangOpts,
1296 const StoredDiagnostic &InDiag) {
1297 ASTUnit::StandaloneDiagnostic OutDiag;
1298 OutDiag.ID = InDiag.getID();
1299 OutDiag.Level = InDiag.getLevel();
1300 OutDiag.Message = std::string(InDiag.getMessage());
1301 OutDiag.LocOffset = 0;
1302 if (InDiag.getLocation().isInvalid())
1303 return OutDiag;
1304 const SourceManager &SM = InDiag.getLocation().getManager();
1305 SourceLocation FileLoc = SM.getFileLoc(Loc: InDiag.getLocation());
1306 OutDiag.Filename = std::string(SM.getFilename(SpellingLoc: FileLoc));
1307 if (OutDiag.Filename.empty())
1308 return OutDiag;
1309 OutDiag.LocOffset = SM.getFileOffset(SpellingLoc: FileLoc);
1310 for (const auto &Range : InDiag.getRanges())
1311 OutDiag.Ranges.push_back(x: makeStandaloneRange(Range, SM, LangOpts));
1312 for (const auto &FixIt : InDiag.getFixIts())
1313 OutDiag.FixIts.push_back(x: makeStandaloneFixIt(SM, LangOpts, InFix: FixIt));
1314
1315 return OutDiag;
1316}
1317
1318/// Attempt to build or re-use a precompiled preamble when (re-)parsing
1319/// the source file.
1320///
1321/// This routine will compute the preamble of the main source file. If a
1322/// non-trivial preamble is found, it will precompile that preamble into a
1323/// precompiled header so that the precompiled preamble can be used to reduce
1324/// reparsing time. If a precompiled preamble has already been constructed,
1325/// this routine will determine if it is still valid and, if so, avoid
1326/// rebuilding the precompiled preamble.
1327///
1328/// \param AllowRebuild When true (the default), this routine is
1329/// allowed to rebuild the precompiled preamble if it is found to be
1330/// out-of-date.
1331///
1332/// \param MaxLines When non-zero, the maximum number of lines that
1333/// can occur within the preamble.
1334///
1335/// \returns If the precompiled preamble can be used, returns a newly-allocated
1336/// buffer that should be used in place of the main file when doing so.
1337/// Otherwise, returns a NULL pointer.
1338std::unique_ptr<llvm::MemoryBuffer>
1339ASTUnit::getMainBufferWithPrecompiledPreamble(
1340 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1341 CompilerInvocation &PreambleInvocationIn,
1342 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild,
1343 unsigned MaxLines) {
1344 auto MainFilePath =
1345 PreambleInvocationIn.getFrontendOpts().Inputs[0].getFile();
1346 std::unique_ptr<llvm::MemoryBuffer> MainFileBuffer =
1347 getBufferForFileHandlingRemapping(Invocation: PreambleInvocationIn, VFS: VFS.get(),
1348 FilePath: MainFilePath, isVolatile: UserFilesAreVolatile);
1349 if (!MainFileBuffer)
1350 return nullptr;
1351
1352 PreambleBounds Bounds = ComputePreambleBounds(
1353 LangOpts: PreambleInvocationIn.getLangOpts(), Buffer: *MainFileBuffer, MaxLines);
1354 if (!Bounds.Size)
1355 return nullptr;
1356
1357 if (Preamble) {
1358 if (Preamble->CanReuse(Invocation: PreambleInvocationIn, MainFileBuffer: *MainFileBuffer, Bounds,
1359 VFS&: *VFS)) {
1360 // Okay! We can re-use the precompiled preamble.
1361
1362 // Set the state of the diagnostic object to mimic its state
1363 // after parsing the preamble.
1364 getDiagnostics().Reset();
1365 ProcessWarningOptions(Diags&: getDiagnostics(),
1366 Opts: PreambleInvocationIn.getDiagnosticOpts());
1367 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1368
1369 PreambleRebuildCountdown = 1;
1370 return MainFileBuffer;
1371 } else {
1372 Preamble.reset();
1373 PreambleDiagnostics.clear();
1374 TopLevelDeclsInPreamble.clear();
1375 PreambleSrcLocCache.clear();
1376 PreambleRebuildCountdown = 1;
1377 }
1378 }
1379
1380 // If the preamble rebuild counter > 1, it's because we previously
1381 // failed to build a preamble and we're not yet ready to try
1382 // again. Decrement the counter and return a failure.
1383 if (PreambleRebuildCountdown > 1) {
1384 --PreambleRebuildCountdown;
1385 return nullptr;
1386 }
1387
1388 assert(!Preamble && "No Preamble should be stored at that point");
1389 // If we aren't allowed to rebuild the precompiled preamble, just
1390 // return now.
1391 if (!AllowRebuild)
1392 return nullptr;
1393
1394 ++PreambleCounter;
1395
1396 SmallVector<StandaloneDiagnostic, 4> NewPreambleDiagsStandalone;
1397 SmallVector<StoredDiagnostic, 4> NewPreambleDiags;
1398 ASTUnitPreambleCallbacks Callbacks;
1399 {
1400 std::optional<CaptureDroppedDiagnostics> Capture;
1401 if (CaptureDiagnostics != CaptureDiagsKind::None)
1402 Capture.emplace(args&: CaptureDiagnostics, args&: *Diagnostics, args: &NewPreambleDiags,
1403 args: &NewPreambleDiagsStandalone);
1404
1405 // We did not previously compute a preamble, or it can't be reused anyway.
1406 SimpleTimer PreambleTimer(WantTiming);
1407 PreambleTimer.setOutput("Precompiling preamble");
1408
1409 const bool PreviousSkipFunctionBodies =
1410 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies;
1411 if (SkipFunctionBodies == SkipFunctionBodiesScope::Preamble)
1412 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies = true;
1413
1414 llvm::ErrorOr<PrecompiledPreamble> NewPreamble = PrecompiledPreamble::Build(
1415 Invocation: PreambleInvocationIn, MainFileBuffer: MainFileBuffer.get(), Bounds, Diagnostics&: *Diagnostics, VFS,
1416 PCHContainerOps, StoreInMemory: StorePreamblesInMemory, StoragePath: PreambleStoragePath,
1417 Callbacks);
1418
1419 PreambleInvocationIn.getFrontendOpts().SkipFunctionBodies =
1420 PreviousSkipFunctionBodies;
1421
1422 if (NewPreamble) {
1423 Preamble = std::move(*NewPreamble);
1424 PreambleRebuildCountdown = 1;
1425 } else {
1426 switch (static_cast<BuildPreambleError>(NewPreamble.getError().value())) {
1427 case BuildPreambleError::CouldntCreateTempFile:
1428 // Try again next time.
1429 PreambleRebuildCountdown = 1;
1430 return nullptr;
1431 case BuildPreambleError::CouldntCreateTargetInfo:
1432 case BuildPreambleError::BeginSourceFileFailed:
1433 case BuildPreambleError::CouldntEmitPCH:
1434 case BuildPreambleError::BadInputs:
1435 // These erros are more likely to repeat, retry after some period.
1436 PreambleRebuildCountdown = DefaultPreambleRebuildInterval;
1437 return nullptr;
1438 }
1439 llvm_unreachable("unexpected BuildPreambleError");
1440 }
1441 }
1442
1443 assert(Preamble && "Preamble wasn't built");
1444
1445 TopLevelDecls.clear();
1446 TopLevelDeclsInPreamble = Callbacks.takeTopLevelDeclIDs();
1447 PreambleTopLevelHashValue = Callbacks.getHash();
1448
1449 NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1450
1451 checkAndRemoveNonDriverDiags(StoredDiags&: NewPreambleDiags);
1452 StoredDiagnostics = std::move(NewPreambleDiags);
1453 PreambleDiagnostics = std::move(NewPreambleDiagsStandalone);
1454
1455 // If the hash of top-level entities differs from the hash of the top-level
1456 // entities the last time we rebuilt the preamble, clear out the completion
1457 // cache.
1458 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1459 CompletionCacheTopLevelHashValue = 0;
1460 PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1461 }
1462
1463 return MainFileBuffer;
1464}
1465
1466void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1467 assert(Preamble && "Should only be called when preamble was built");
1468
1469 std::vector<Decl *> Resolved;
1470 Resolved.reserve(n: TopLevelDeclsInPreamble.size());
1471 // The module file of the preamble.
1472 serialization::ModuleFile &MF = Reader->getModuleManager().getPrimaryModule();
1473 for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
1474 // Resolve the declaration ID to an actual declaration, possibly
1475 // deserializing the declaration in the process.
1476 if (Decl *D = Reader->GetLocalDecl(F&: MF, LocalID: TopLevelDecl))
1477 Resolved.push_back(x: D);
1478 }
1479 TopLevelDeclsInPreamble.clear();
1480 TopLevelDecls.insert(position: TopLevelDecls.begin(), first: Resolved.begin(), last: Resolved.end());
1481}
1482
1483void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1484 // Steal the created target, context, and preprocessor if they have been
1485 // created.
1486 assert(CI.hasInvocation() && "missing invocation");
1487 LangOpts = CI.getInvocation().LangOpts;
1488 TheSema = CI.takeSema();
1489 Consumer = CI.takeASTConsumer();
1490 if (CI.hasASTContext())
1491 Ctx = &CI.getASTContext();
1492 if (CI.hasPreprocessor())
1493 PP = CI.getPreprocessorPtr();
1494 CI.setSourceManager(nullptr);
1495 CI.setFileManager(nullptr);
1496 if (CI.hasTarget())
1497 Target = &CI.getTarget();
1498 Reader = CI.getASTReader();
1499 HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1500}
1501
1502StringRef ASTUnit::getMainFileName() const {
1503 if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1504 const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1505 if (Input.isFile())
1506 return Input.getFile();
1507 else
1508 return Input.getBuffer().getBufferIdentifier();
1509 }
1510
1511 if (SourceMgr) {
1512 if (OptionalFileEntryRef FE =
1513 SourceMgr->getFileEntryRefForID(FID: SourceMgr->getMainFileID()))
1514 return FE->getName();
1515 }
1516
1517 return {};
1518}
1519
1520StringRef ASTUnit::getASTFileName() const {
1521 if (!isMainFileAST())
1522 return {};
1523
1524 serialization::ModuleFile &
1525 Mod = Reader->getModuleManager().getPrimaryModule();
1526 return Mod.FileName;
1527}
1528
1529std::unique_ptr<ASTUnit>
1530ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
1531 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1532 CaptureDiagsKind CaptureDiagnostics,
1533 bool UserFilesAreVolatile) {
1534 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1535 ConfigureDiags(Diags, AST&: *AST, CaptureDiagnostics);
1536 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
1537 createVFSFromCompilerInvocation(CI: *CI, Diags&: *Diags);
1538 AST->Diagnostics = Diags;
1539 AST->FileSystemOpts = CI->getFileSystemOpts();
1540 AST->Invocation = std::move(CI);
1541 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1542 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1543 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1544 UserFilesAreVolatile);
1545 AST->ModuleCache = new InMemoryModuleCache;
1546
1547 return AST;
1548}
1549
1550ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(
1551 std::shared_ptr<CompilerInvocation> CI,
1552 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1553 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action,
1554 ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath,
1555 bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1556 unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults,
1557 bool UserFilesAreVolatile, std::unique_ptr<ASTUnit> *ErrAST) {
1558 assert(CI && "A CompilerInvocation is required");
1559
1560 std::unique_ptr<ASTUnit> OwnAST;
1561 ASTUnit *AST = Unit;
1562 if (!AST) {
1563 // Create the AST unit.
1564 OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile);
1565 AST = OwnAST.get();
1566 if (!AST)
1567 return nullptr;
1568 }
1569
1570 if (!ResourceFilesPath.empty()) {
1571 // Override the resources path.
1572 CI->getHeaderSearchOpts().ResourceDir = std::string(ResourceFilesPath);
1573 }
1574 AST->OnlyLocalDecls = OnlyLocalDecls;
1575 AST->CaptureDiagnostics = CaptureDiagnostics;
1576 if (PrecompilePreambleAfterNParses > 0)
1577 AST->PreambleRebuildCountdown = PrecompilePreambleAfterNParses;
1578 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1579 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1580 AST->IncludeBriefCommentsInCodeCompletion = false;
1581
1582 // Recover resources if we crash before exiting this method.
1583 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1584 ASTUnitCleanup(OwnAST.get());
1585 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1586 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1587 DiagCleanup(Diags.get());
1588
1589 // We'll manage file buffers ourselves.
1590 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1591 CI->getFrontendOpts().DisableFree = false;
1592 ProcessWarningOptions(Diags&: AST->getDiagnostics(), Opts: CI->getDiagnosticOpts());
1593
1594 // Create the compiler instance to use for building the AST.
1595 std::unique_ptr<CompilerInstance> Clang(
1596 new CompilerInstance(std::move(PCHContainerOps)));
1597
1598 // Recover resources if we crash before exiting this method.
1599 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1600 CICleanup(Clang.get());
1601
1602 Clang->setInvocation(std::move(CI));
1603 AST->OriginalSourceFile =
1604 std::string(Clang->getFrontendOpts().Inputs[0].getFile());
1605
1606 // Set up diagnostics, capturing any diagnostics that would
1607 // otherwise be dropped.
1608 Clang->setDiagnostics(&AST->getDiagnostics());
1609
1610 // Create the target instance.
1611 if (!Clang->createTarget())
1612 return nullptr;
1613
1614 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1615 "Invocation must have exactly one source file!");
1616 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
1617 InputKind::Source &&
1618 "FIXME: AST inputs not yet supported here!");
1619 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
1620 Language::LLVM_IR &&
1621 "IR inputs not support here!");
1622
1623 // Configure the various subsystems.
1624 AST->TheSema.reset();
1625 AST->Ctx = nullptr;
1626 AST->PP = nullptr;
1627 AST->Reader = nullptr;
1628
1629 // Create a file manager object to provide access to and cache the filesystem.
1630 Clang->setFileManager(&AST->getFileManager());
1631
1632 // Create the source manager.
1633 Clang->setSourceManager(&AST->getSourceManager());
1634
1635 FrontendAction *Act = Action;
1636
1637 std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct;
1638 if (!Act) {
1639 TrackerAct.reset(p: new TopLevelDeclTrackerAction(*AST));
1640 Act = TrackerAct.get();
1641 }
1642
1643 // Recover resources if we crash before exiting this method.
1644 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1645 ActCleanup(TrackerAct.get());
1646
1647 if (!Act->BeginSourceFile(CI&: *Clang.get(), Input: Clang->getFrontendOpts().Inputs[0])) {
1648 AST->transferASTDataFromCompilerInstance(CI&: *Clang);
1649 if (OwnAST && ErrAST)
1650 ErrAST->swap(u&: OwnAST);
1651
1652 return nullptr;
1653 }
1654
1655 if (Persistent && !TrackerAct) {
1656 Clang->getPreprocessor().addPPCallbacks(
1657 C: std::make_unique<MacroDefinitionTrackerPPCallbacks>(
1658 args&: AST->getCurrentTopLevelHashValue()));
1659 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
1660 if (Clang->hasASTConsumer())
1661 Consumers.push_back(x: Clang->takeASTConsumer());
1662 Consumers.push_back(x: std::make_unique<TopLevelDeclTrackerConsumer>(
1663 args&: *AST, args&: AST->getCurrentTopLevelHashValue()));
1664 Clang->setASTConsumer(
1665 std::make_unique<MultiplexConsumer>(args: std::move(Consumers)));
1666 }
1667 if (llvm::Error Err = Act->Execute()) {
1668 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
1669 AST->transferASTDataFromCompilerInstance(CI&: *Clang);
1670 if (OwnAST && ErrAST)
1671 ErrAST->swap(u&: OwnAST);
1672
1673 return nullptr;
1674 }
1675
1676 // Steal the created target, context, and preprocessor.
1677 AST->transferASTDataFromCompilerInstance(CI&: *Clang);
1678
1679 Act->EndSourceFile();
1680
1681 if (OwnAST)
1682 return OwnAST.release();
1683 else
1684 return AST;
1685}
1686
1687bool ASTUnit::LoadFromCompilerInvocation(
1688 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1689 unsigned PrecompilePreambleAfterNParses,
1690 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1691 if (!Invocation)
1692 return true;
1693
1694 assert(VFS && "VFS is null");
1695
1696 // We'll manage file buffers ourselves.
1697 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1698 Invocation->getFrontendOpts().DisableFree = false;
1699 getDiagnostics().Reset();
1700 ProcessWarningOptions(Diags&: getDiagnostics(), Opts: Invocation->getDiagnosticOpts());
1701
1702 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1703 if (PrecompilePreambleAfterNParses > 0) {
1704 PreambleRebuildCountdown = PrecompilePreambleAfterNParses;
1705 OverrideMainBuffer =
1706 getMainBufferWithPrecompiledPreamble(PCHContainerOps, PreambleInvocationIn&: *Invocation, VFS);
1707 getDiagnostics().Reset();
1708 ProcessWarningOptions(Diags&: getDiagnostics(), Opts: Invocation->getDiagnosticOpts());
1709 }
1710
1711 SimpleTimer ParsingTimer(WantTiming);
1712 ParsingTimer.setOutput("Parsing " + getMainFileName());
1713
1714 // Recover resources if we crash before exiting this method.
1715 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1716 MemBufferCleanup(OverrideMainBuffer.get());
1717
1718 return Parse(PCHContainerOps: std::move(PCHContainerOps), OverrideMainBuffer: std::move(OverrideMainBuffer), VFS);
1719}
1720
1721std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
1722 std::shared_ptr<CompilerInvocation> CI,
1723 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1724 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
1725 bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1726 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1727 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1728 bool UserFilesAreVolatile) {
1729 // Create the AST unit.
1730 std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
1731 ConfigureDiags(Diags, AST&: *AST, CaptureDiagnostics);
1732 AST->Diagnostics = Diags;
1733 AST->OnlyLocalDecls = OnlyLocalDecls;
1734 AST->CaptureDiagnostics = CaptureDiagnostics;
1735 AST->TUKind = TUKind;
1736 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1737 AST->IncludeBriefCommentsInCodeCompletion
1738 = IncludeBriefCommentsInCodeCompletion;
1739 AST->Invocation = std::move(CI);
1740 AST->FileSystemOpts = FileMgr->getFileSystemOpts();
1741 AST->FileMgr = FileMgr;
1742 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1743
1744 // Recover resources if we crash before exiting this method.
1745 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1746 ASTUnitCleanup(AST.get());
1747 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1748 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>>
1749 DiagCleanup(Diags.get());
1750
1751 if (AST->LoadFromCompilerInvocation(PCHContainerOps: std::move(PCHContainerOps),
1752 PrecompilePreambleAfterNParses,
1753 VFS: &AST->FileMgr->getVirtualFileSystem()))
1754 return nullptr;
1755 return AST;
1756}
1757
1758std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
1759 const char **ArgBegin, const char **ArgEnd,
1760 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1761 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
1762 bool StorePreamblesInMemory, StringRef PreambleStoragePath,
1763 bool OnlyLocalDecls, CaptureDiagsKind CaptureDiagnostics,
1764 ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName,
1765 unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind,
1766 bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion,
1767 bool AllowPCHWithCompilerErrors, SkipFunctionBodiesScope SkipFunctionBodies,
1768 bool SingleFileParse, bool UserFilesAreVolatile, bool ForSerialization,
1769 bool RetainExcludedConditionalBlocks, std::optional<StringRef> ModuleFormat,
1770 std::unique_ptr<ASTUnit> *ErrAST,
1771 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1772 assert(Diags.get() && "no DiagnosticsEngine was provided");
1773
1774 // If no VFS was provided, create one that tracks the physical file system.
1775 // If '-working-directory' was passed as an argument, 'createInvocation' will
1776 // set this as the current working directory of the VFS.
1777 if (!VFS)
1778 VFS = llvm::vfs::createPhysicalFileSystem();
1779
1780 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1781
1782 std::shared_ptr<CompilerInvocation> CI;
1783
1784 {
1785 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1786 &StoredDiagnostics, nullptr);
1787
1788 CreateInvocationOptions CIOpts;
1789 CIOpts.VFS = VFS;
1790 CIOpts.Diags = Diags;
1791 CIOpts.ProbePrecompiled = true; // FIXME: historical default. Needed?
1792 CI = createInvocation(Args: llvm::ArrayRef(ArgBegin, ArgEnd), Opts: std::move(CIOpts));
1793 if (!CI)
1794 return nullptr;
1795 }
1796
1797 // Override any files that need remapping
1798 for (const auto &RemappedFile : RemappedFiles) {
1799 CI->getPreprocessorOpts().addRemappedFile(From: RemappedFile.first,
1800 To: RemappedFile.second);
1801 }
1802 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1803 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1804 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1805 PPOpts.SingleFileParseMode = SingleFileParse;
1806 PPOpts.RetainExcludedConditionalBlocks = RetainExcludedConditionalBlocks;
1807
1808 // Override the resources path.
1809 CI->getHeaderSearchOpts().ResourceDir = std::string(ResourceFilesPath);
1810
1811 CI->getFrontendOpts().SkipFunctionBodies =
1812 SkipFunctionBodies == SkipFunctionBodiesScope::PreambleAndMainFile;
1813
1814 if (ModuleFormat)
1815 CI->getHeaderSearchOpts().ModuleFormat = std::string(*ModuleFormat);
1816
1817 // Create the AST unit.
1818 std::unique_ptr<ASTUnit> AST;
1819 AST.reset(p: new ASTUnit(false));
1820 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1821 AST->StoredDiagnostics.swap(RHS&: StoredDiagnostics);
1822 ConfigureDiags(Diags, AST&: *AST, CaptureDiagnostics);
1823 AST->Diagnostics = Diags;
1824 AST->FileSystemOpts = CI->getFileSystemOpts();
1825 VFS = createVFSFromCompilerInvocation(CI: *CI, Diags&: *Diags, BaseFS: VFS);
1826 AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
1827 AST->StorePreamblesInMemory = StorePreamblesInMemory;
1828 AST->PreambleStoragePath = PreambleStoragePath;
1829 AST->ModuleCache = new InMemoryModuleCache;
1830 AST->OnlyLocalDecls = OnlyLocalDecls;
1831 AST->CaptureDiagnostics = CaptureDiagnostics;
1832 AST->TUKind = TUKind;
1833 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1834 AST->IncludeBriefCommentsInCodeCompletion
1835 = IncludeBriefCommentsInCodeCompletion;
1836 AST->UserFilesAreVolatile = UserFilesAreVolatile;
1837 AST->Invocation = CI;
1838 AST->SkipFunctionBodies = SkipFunctionBodies;
1839 if (ForSerialization)
1840 AST->WriterData.reset(p: new ASTWriterData(*AST->ModuleCache));
1841 // Zero out now to ease cleanup during crash recovery.
1842 CI = nullptr;
1843 Diags = nullptr;
1844
1845 // Recover resources if we crash before exiting this method.
1846 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1847 ASTUnitCleanup(AST.get());
1848
1849 if (AST->LoadFromCompilerInvocation(PCHContainerOps: std::move(PCHContainerOps),
1850 PrecompilePreambleAfterNParses,
1851 VFS)) {
1852 // Some error occurred, if caller wants to examine diagnostics, pass it the
1853 // ASTUnit.
1854 if (ErrAST) {
1855 AST->StoredDiagnostics.swap(RHS&: AST->FailedParseDiagnostics);
1856 ErrAST->swap(u&: AST);
1857 }
1858 return nullptr;
1859 }
1860
1861 return AST;
1862}
1863
1864bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
1865 ArrayRef<RemappedFile> RemappedFiles,
1866 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
1867 if (!Invocation)
1868 return true;
1869
1870 if (!VFS) {
1871 assert(FileMgr && "FileMgr is null on Reparse call");
1872 VFS = &FileMgr->getVirtualFileSystem();
1873 }
1874
1875 clearFileLevelDecls();
1876
1877 SimpleTimer ParsingTimer(WantTiming);
1878 ParsingTimer.setOutput("Reparsing " + getMainFileName());
1879
1880 // Remap files.
1881 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1882 for (const auto &RB : PPOpts.RemappedFileBuffers)
1883 delete RB.second;
1884
1885 Invocation->getPreprocessorOpts().clearRemappedFiles();
1886 for (const auto &RemappedFile : RemappedFiles) {
1887 Invocation->getPreprocessorOpts().addRemappedFile(From: RemappedFile.first,
1888 To: RemappedFile.second);
1889 }
1890
1891 // If we have a preamble file lying around, or if we might try to
1892 // build a precompiled preamble, do so now.
1893 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
1894 if (Preamble || PreambleRebuildCountdown > 0)
1895 OverrideMainBuffer =
1896 getMainBufferWithPrecompiledPreamble(PCHContainerOps, PreambleInvocationIn&: *Invocation, VFS);
1897
1898 // Clear out the diagnostics state.
1899 FileMgr.reset();
1900 getDiagnostics().Reset();
1901 ProcessWarningOptions(Diags&: getDiagnostics(), Opts: Invocation->getDiagnosticOpts());
1902 if (OverrideMainBuffer)
1903 getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1904
1905 // Parse the sources
1906 bool Result =
1907 Parse(PCHContainerOps: std::move(PCHContainerOps), OverrideMainBuffer: std::move(OverrideMainBuffer), VFS);
1908
1909 // If we're caching global code-completion results, and the top-level
1910 // declarations have changed, clear out the code-completion cache.
1911 if (!Result && ShouldCacheCodeCompletionResults &&
1912 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1913 CacheCodeCompletionResults();
1914
1915 // We now need to clear out the completion info related to this translation
1916 // unit; it'll be recreated if necessary.
1917 CCTUInfo.reset();
1918
1919 return Result;
1920}
1921
1922void ASTUnit::ResetForParse() {
1923 SavedMainFileBuffer.reset();
1924
1925 SourceMgr.reset();
1926 TheSema.reset();
1927 Ctx.reset();
1928 PP.reset();
1929 Reader.reset();
1930
1931 TopLevelDecls.clear();
1932 clearFileLevelDecls();
1933}
1934
1935//----------------------------------------------------------------------------//
1936// Code completion
1937//----------------------------------------------------------------------------//
1938
1939namespace {
1940
1941 /// Code completion consumer that combines the cached code-completion
1942 /// results from an ASTUnit with the code-completion results provided to it,
1943 /// then passes the result on to
1944 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1945 uint64_t NormalContexts;
1946 ASTUnit &AST;
1947 CodeCompleteConsumer &Next;
1948
1949 public:
1950 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1951 const CodeCompleteOptions &CodeCompleteOpts)
1952 : CodeCompleteConsumer(CodeCompleteOpts), AST(AST), Next(Next) {
1953 // Compute the set of contexts in which we will look when we don't have
1954 // any information about the specific context.
1955 NormalContexts
1956 = (1LL << CodeCompletionContext::CCC_TopLevel)
1957 | (1LL << CodeCompletionContext::CCC_ObjCInterface)
1958 | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
1959 | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
1960 | (1LL << CodeCompletionContext::CCC_Statement)
1961 | (1LL << CodeCompletionContext::CCC_Expression)
1962 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
1963 | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
1964 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
1965 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
1966 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
1967 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
1968 | (1LL << CodeCompletionContext::CCC_Recovery);
1969
1970 if (AST.getASTContext().getLangOpts().CPlusPlus)
1971 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
1972 | (1LL << CodeCompletionContext::CCC_UnionTag)
1973 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
1974 }
1975
1976 void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1977 CodeCompletionResult *Results,
1978 unsigned NumResults) override;
1979
1980 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1981 OverloadCandidate *Candidates,
1982 unsigned NumCandidates,
1983 SourceLocation OpenParLoc,
1984 bool Braced) override {
1985 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1986 OpenParLoc, Braced);
1987 }
1988
1989 CodeCompletionAllocator &getAllocator() override {
1990 return Next.getAllocator();
1991 }
1992
1993 CodeCompletionTUInfo &getCodeCompletionTUInfo() override {
1994 return Next.getCodeCompletionTUInfo();
1995 }
1996 };
1997
1998} // namespace
1999
2000/// Helper function that computes which global names are hidden by the
2001/// local code-completion results.
2002static void CalculateHiddenNames(const CodeCompletionContext &Context,
2003 CodeCompletionResult *Results,
2004 unsigned NumResults,
2005 ASTContext &Ctx,
2006 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2007 bool OnlyTagNames = false;
2008 switch (Context.getKind()) {
2009 case CodeCompletionContext::CCC_Recovery:
2010 case CodeCompletionContext::CCC_TopLevel:
2011 case CodeCompletionContext::CCC_ObjCInterface:
2012 case CodeCompletionContext::CCC_ObjCImplementation:
2013 case CodeCompletionContext::CCC_ObjCIvarList:
2014 case CodeCompletionContext::CCC_ClassStructUnion:
2015 case CodeCompletionContext::CCC_Statement:
2016 case CodeCompletionContext::CCC_Expression:
2017 case CodeCompletionContext::CCC_ObjCMessageReceiver:
2018 case CodeCompletionContext::CCC_DotMemberAccess:
2019 case CodeCompletionContext::CCC_ArrowMemberAccess:
2020 case CodeCompletionContext::CCC_ObjCPropertyAccess:
2021 case CodeCompletionContext::CCC_Namespace:
2022 case CodeCompletionContext::CCC_Type:
2023 case CodeCompletionContext::CCC_Symbol:
2024 case CodeCompletionContext::CCC_SymbolOrNewName:
2025 case CodeCompletionContext::CCC_ParenthesizedExpression:
2026 case CodeCompletionContext::CCC_ObjCInterfaceName:
2027 case CodeCompletionContext::CCC_TopLevelOrExpression:
2028 break;
2029
2030 case CodeCompletionContext::CCC_EnumTag:
2031 case CodeCompletionContext::CCC_UnionTag:
2032 case CodeCompletionContext::CCC_ClassOrStructTag:
2033 OnlyTagNames = true;
2034 break;
2035
2036 case CodeCompletionContext::CCC_ObjCProtocolName:
2037 case CodeCompletionContext::CCC_MacroName:
2038 case CodeCompletionContext::CCC_MacroNameUse:
2039 case CodeCompletionContext::CCC_PreprocessorExpression:
2040 case CodeCompletionContext::CCC_PreprocessorDirective:
2041 case CodeCompletionContext::CCC_NaturalLanguage:
2042 case CodeCompletionContext::CCC_SelectorName:
2043 case CodeCompletionContext::CCC_TypeQualifiers:
2044 case CodeCompletionContext::CCC_Other:
2045 case CodeCompletionContext::CCC_OtherWithMacros:
2046 case CodeCompletionContext::CCC_ObjCInstanceMessage:
2047 case CodeCompletionContext::CCC_ObjCClassMessage:
2048 case CodeCompletionContext::CCC_ObjCCategoryName:
2049 case CodeCompletionContext::CCC_IncludedFile:
2050 case CodeCompletionContext::CCC_Attribute:
2051 case CodeCompletionContext::CCC_NewName:
2052 case CodeCompletionContext::CCC_ObjCClassForwardDecl:
2053 // We're looking for nothing, or we're looking for names that cannot
2054 // be hidden.
2055 return;
2056 }
2057
2058 using Result = CodeCompletionResult;
2059 for (unsigned I = 0; I != NumResults; ++I) {
2060 if (Results[I].Kind != Result::RK_Declaration)
2061 continue;
2062
2063 unsigned IDNS
2064 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2065
2066 bool Hiding = false;
2067 if (OnlyTagNames)
2068 Hiding = (IDNS & Decl::IDNS_Tag);
2069 else {
2070 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2071 Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2072 Decl::IDNS_NonMemberOperator);
2073 if (Ctx.getLangOpts().CPlusPlus)
2074 HiddenIDNS |= Decl::IDNS_Tag;
2075 Hiding = (IDNS & HiddenIDNS);
2076 }
2077
2078 if (!Hiding)
2079 continue;
2080
2081 DeclarationName Name = Results[I].Declaration->getDeclName();
2082 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2083 HiddenNames.insert(key: Identifier->getName());
2084 else
2085 HiddenNames.insert(key: Name.getAsString());
2086 }
2087}
2088
2089void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2090 CodeCompletionContext Context,
2091 CodeCompletionResult *Results,
2092 unsigned NumResults) {
2093 // Merge the results we were given with the results we cached.
2094 bool AddedResult = false;
2095 uint64_t InContexts =
2096 Context.getKind() == CodeCompletionContext::CCC_Recovery
2097 ? NormalContexts : (1LL << Context.getKind());
2098 // Contains the set of names that are hidden by "local" completion results.
2099 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2100 using Result = CodeCompletionResult;
2101 SmallVector<Result, 8> AllResults;
2102 for (ASTUnit::cached_completion_iterator
2103 C = AST.cached_completion_begin(),
2104 CEnd = AST.cached_completion_end();
2105 C != CEnd; ++C) {
2106 // If the context we are in matches any of the contexts we are
2107 // interested in, we'll add this result.
2108 if ((C->ShowInContexts & InContexts) == 0)
2109 continue;
2110
2111 // If we haven't added any results previously, do so now.
2112 if (!AddedResult) {
2113 CalculateHiddenNames(Context, Results, NumResults, Ctx&: S.Context,
2114 HiddenNames);
2115 AllResults.insert(I: AllResults.end(), From: Results, To: Results + NumResults);
2116 AddedResult = true;
2117 }
2118
2119 // Determine whether this global completion result is hidden by a local
2120 // completion result. If so, skip it.
2121 if (C->Kind != CXCursor_MacroDefinition &&
2122 HiddenNames.count(Key: C->Completion->getTypedText()))
2123 continue;
2124
2125 // Adjust priority based on similar type classes.
2126 unsigned Priority = C->Priority;
2127 CodeCompletionString *Completion = C->Completion;
2128 if (!Context.getPreferredType().isNull()) {
2129 if (C->Kind == CXCursor_MacroDefinition) {
2130 Priority = getMacroUsagePriority(MacroName: C->Completion->getTypedText(),
2131 LangOpts: S.getLangOpts(),
2132 PreferredTypeIsPointer: Context.getPreferredType()->isAnyPointerType());
2133 } else if (C->Type) {
2134 CanQualType Expected
2135 = S.Context.getCanonicalType(
2136 T: Context.getPreferredType().getUnqualifiedType());
2137 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(T: Expected);
2138 if (ExpectedSTC == C->TypeClass) {
2139 // We know this type is similar; check for an exact match.
2140 llvm::StringMap<unsigned> &CachedCompletionTypes
2141 = AST.getCachedCompletionTypes();
2142 llvm::StringMap<unsigned>::iterator Pos
2143 = CachedCompletionTypes.find(Key: QualType(Expected).getAsString());
2144 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2145 Priority /= CCF_ExactTypeMatch;
2146 else
2147 Priority /= CCF_SimilarTypeMatch;
2148 }
2149 }
2150 }
2151
2152 // Adjust the completion string, if required.
2153 if (C->Kind == CXCursor_MacroDefinition &&
2154 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2155 // Create a new code-completion string that just contains the
2156 // macro name, without its arguments.
2157 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2158 CCP_CodePattern, C->Availability);
2159 Builder.AddTypedTextChunk(Text: C->Completion->getTypedText());
2160 Priority = CCP_CodePattern;
2161 Completion = Builder.TakeString();
2162 }
2163
2164 AllResults.push_back(Elt: Result(Completion, Priority, C->Kind,
2165 C->Availability));
2166 }
2167
2168 // If we did not add any cached completion results, just forward the
2169 // results we were given to the next consumer.
2170 if (!AddedResult) {
2171 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2172 return;
2173 }
2174
2175 Next.ProcessCodeCompleteResults(S, Context, Results: AllResults.data(),
2176 NumResults: AllResults.size());
2177}
2178
2179void ASTUnit::CodeComplete(
2180 StringRef File, unsigned Line, unsigned Column,
2181 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
2182 bool IncludeCodePatterns, bool IncludeBriefComments,
2183 CodeCompleteConsumer &Consumer,
2184 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
2185 DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
2186 FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2187 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
2188 std::unique_ptr<SyntaxOnlyAction> Act) {
2189 if (!Invocation)
2190 return;
2191
2192 SimpleTimer CompletionTimer(WantTiming);
2193 CompletionTimer.setOutput("Code completion @ " + File + ":" +
2194 Twine(Line) + ":" + Twine(Column));
2195
2196 auto CCInvocation = std::make_shared<CompilerInvocation>(args&: *Invocation);
2197
2198 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2199 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2200 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2201
2202 CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2203 CachedCompletionResults.empty();
2204 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2205 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2206 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2207 CodeCompleteOpts.LoadExternal = Consumer.loadExternal();
2208 CodeCompleteOpts.IncludeFixIts = Consumer.includeFixIts();
2209
2210 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2211
2212 FrontendOpts.CodeCompletionAt.FileName = std::string(File);
2213 FrontendOpts.CodeCompletionAt.Line = Line;
2214 FrontendOpts.CodeCompletionAt.Column = Column;
2215
2216 // Set the language options appropriately.
2217 LangOpts = CCInvocation->getLangOpts();
2218
2219 // Spell-checking and warnings are wasteful during code-completion.
2220 LangOpts.SpellChecking = false;
2221 CCInvocation->getDiagnosticOpts().IgnoreWarnings = true;
2222
2223 std::unique_ptr<CompilerInstance> Clang(
2224 new CompilerInstance(PCHContainerOps));
2225
2226 // Recover resources if we crash before exiting this method.
2227 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2228 CICleanup(Clang.get());
2229
2230 auto &Inv = *CCInvocation;
2231 Clang->setInvocation(std::move(CCInvocation));
2232 OriginalSourceFile =
2233 std::string(Clang->getFrontendOpts().Inputs[0].getFile());
2234
2235 // Set up diagnostics, capturing any diagnostics produced.
2236 Clang->setDiagnostics(&Diag);
2237 CaptureDroppedDiagnostics Capture(CaptureDiagsKind::All,
2238 Clang->getDiagnostics(),
2239 &StoredDiagnostics, nullptr);
2240 ProcessWarningOptions(Diags&: Diag, Opts: Inv.getDiagnosticOpts());
2241
2242 // Create the target instance.
2243 if (!Clang->createTarget()) {
2244 Clang->setInvocation(nullptr);
2245 return;
2246 }
2247
2248 assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2249 "Invocation must have exactly one source file!");
2250 assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() ==
2251 InputKind::Source &&
2252 "FIXME: AST inputs not yet supported here!");
2253 assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() !=
2254 Language::LLVM_IR &&
2255 "IR inputs not support here!");
2256
2257 // Use the source and file managers that we were given.
2258 Clang->setFileManager(&FileMgr);
2259 Clang->setSourceManager(&SourceMgr);
2260
2261 // Remap files.
2262 PreprocessorOpts.clearRemappedFiles();
2263 PreprocessorOpts.RetainRemappedFileBuffers = true;
2264 for (const auto &RemappedFile : RemappedFiles) {
2265 PreprocessorOpts.addRemappedFile(From: RemappedFile.first, To: RemappedFile.second);
2266 OwnedBuffers.push_back(Elt: RemappedFile.second);
2267 }
2268
2269 // Use the code completion consumer we were given, but adding any cached
2270 // code-completion results.
2271 AugmentedCodeCompleteConsumer *AugmentedConsumer
2272 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2273 Clang->setCodeCompletionConsumer(AugmentedConsumer);
2274
2275 auto getUniqueID =
2276 [&FileMgr](StringRef Filename) -> std::optional<llvm::sys::fs::UniqueID> {
2277 if (auto Status = FileMgr.getVirtualFileSystem().status(Path: Filename))
2278 return Status->getUniqueID();
2279 return std::nullopt;
2280 };
2281
2282 auto hasSameUniqueID = [getUniqueID](StringRef LHS, StringRef RHS) {
2283 if (LHS == RHS)
2284 return true;
2285 if (auto LHSID = getUniqueID(LHS))
2286 if (auto RHSID = getUniqueID(RHS))
2287 return *LHSID == *RHSID;
2288 return false;
2289 };
2290
2291 // If we have a precompiled preamble, try to use it. We only allow
2292 // the use of the precompiled preamble if we're if the completion
2293 // point is within the main file, after the end of the precompiled
2294 // preamble.
2295 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer;
2296 if (Preamble && Line > 1 && hasSameUniqueID(File, OriginalSourceFile)) {
2297 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(
2298 PCHContainerOps, PreambleInvocationIn&: Inv, VFS: &FileMgr.getVirtualFileSystem(), AllowRebuild: false, MaxLines: Line - 1);
2299 }
2300
2301 // If the main file has been overridden due to the use of a preamble,
2302 // make that override happen and introduce the preamble.
2303 if (OverrideMainBuffer) {
2304 assert(Preamble &&
2305 "No preamble was built, but OverrideMainBuffer is not null");
2306
2307 IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
2308 &FileMgr.getVirtualFileSystem();
2309 Preamble->AddImplicitPreamble(CI&: Clang->getInvocation(), VFS,
2310 MainFileBuffer: OverrideMainBuffer.get());
2311 // FIXME: there is no way to update VFS if it was changed by
2312 // AddImplicitPreamble as FileMgr is accepted as a parameter by this method.
2313 // We use on-disk preambles instead and rely on FileMgr's VFS to ensure the
2314 // PCH files are always readable.
2315 OwnedBuffers.push_back(Elt: OverrideMainBuffer.release());
2316 } else {
2317 PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2318 PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2319 }
2320
2321 // Disable the preprocessing record if modules are not enabled.
2322 if (!Clang->getLangOpts().Modules)
2323 PreprocessorOpts.DetailedRecord = false;
2324
2325 if (!Act)
2326 Act.reset(p: new SyntaxOnlyAction);
2327
2328 if (Act->BeginSourceFile(CI&: *Clang.get(), Input: Clang->getFrontendOpts().Inputs[0])) {
2329 if (llvm::Error Err = Act->Execute()) {
2330 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
2331 }
2332 Act->EndSourceFile();
2333 }
2334}
2335
2336bool ASTUnit::Save(StringRef File) {
2337 if (HadModuleLoaderFatalFailure)
2338 return true;
2339
2340 // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2341 // unconditionally create a stat cache when we parse the file?
2342
2343 if (llvm::Error Err = llvm::writeToOutput(
2344 OutputFileName: File, Write: [this](llvm::raw_ostream &Out) {
2345 return serialize(OS&: Out) ? llvm::make_error<llvm::StringError>(
2346 Args: "ASTUnit serialization failed",
2347 Args: llvm::inconvertibleErrorCode())
2348 : llvm::Error::success();
2349 })) {
2350 consumeError(Err: std::move(Err));
2351 return true;
2352 }
2353 return false;
2354}
2355
2356static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl<char> &Buffer,
2357 Sema &S, raw_ostream &OS) {
2358 Writer.WriteAST(SemaRef&: S, OutputFile: std::string(), WritingModule: nullptr, isysroot: "");
2359
2360 // Write the generated bitstream to "Out".
2361 if (!Buffer.empty())
2362 OS.write(Ptr: Buffer.data(), Size: Buffer.size());
2363
2364 return false;
2365}
2366
2367bool ASTUnit::serialize(raw_ostream &OS) {
2368 if (WriterData)
2369 return serializeUnit(Writer&: WriterData->Writer, Buffer&: WriterData->Buffer, S&: getSema(), OS);
2370
2371 SmallString<128> Buffer;
2372 llvm::BitstreamWriter Stream(Buffer);
2373 InMemoryModuleCache ModuleCache;
2374 ASTWriter Writer(Stream, Buffer, ModuleCache, {});
2375 return serializeUnit(Writer, Buffer, S&: getSema(), OS);
2376}
2377
2378void ASTUnit::TranslateStoredDiagnostics(
2379 FileManager &FileMgr,
2380 SourceManager &SrcMgr,
2381 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
2382 SmallVectorImpl<StoredDiagnostic> &Out) {
2383 // Map the standalone diagnostic into the new source manager. We also need to
2384 // remap all the locations to the new view. This includes the diag location,
2385 // any associated source ranges, and the source ranges of associated fix-its.
2386 // FIXME: There should be a cleaner way to do this.
2387 SmallVector<StoredDiagnostic, 4> Result;
2388 Result.reserve(N: Diags.size());
2389
2390 for (const auto &SD : Diags) {
2391 // Rebuild the StoredDiagnostic.
2392 if (SD.Filename.empty())
2393 continue;
2394 auto FE = FileMgr.getFile(Filename: SD.Filename);
2395 if (!FE)
2396 continue;
2397 SourceLocation FileLoc;
2398 auto ItFileID = PreambleSrcLocCache.find(Key: SD.Filename);
2399 if (ItFileID == PreambleSrcLocCache.end()) {
2400 FileID FID = SrcMgr.translateFile(SourceFile: *FE);
2401 FileLoc = SrcMgr.getLocForStartOfFile(FID);
2402 PreambleSrcLocCache[SD.Filename] = FileLoc;
2403 } else {
2404 FileLoc = ItFileID->getValue();
2405 }
2406
2407 if (FileLoc.isInvalid())
2408 continue;
2409 SourceLocation L = FileLoc.getLocWithOffset(Offset: SD.LocOffset);
2410 FullSourceLoc Loc(L, SrcMgr);
2411
2412 SmallVector<CharSourceRange, 4> Ranges;
2413 Ranges.reserve(N: SD.Ranges.size());
2414 for (const auto &Range : SD.Ranges) {
2415 SourceLocation BL = FileLoc.getLocWithOffset(Offset: Range.first);
2416 SourceLocation EL = FileLoc.getLocWithOffset(Offset: Range.second);
2417 Ranges.push_back(Elt: CharSourceRange::getCharRange(B: BL, E: EL));
2418 }
2419
2420 SmallVector<FixItHint, 2> FixIts;
2421 FixIts.reserve(N: SD.FixIts.size());
2422 for (const auto &FixIt : SD.FixIts) {
2423 FixIts.push_back(Elt: FixItHint());
2424 FixItHint &FH = FixIts.back();
2425 FH.CodeToInsert = FixIt.CodeToInsert;
2426 SourceLocation BL = FileLoc.getLocWithOffset(Offset: FixIt.RemoveRange.first);
2427 SourceLocation EL = FileLoc.getLocWithOffset(Offset: FixIt.RemoveRange.second);
2428 FH.RemoveRange = CharSourceRange::getCharRange(B: BL, E: EL);
2429 }
2430
2431 Result.push_back(Elt: StoredDiagnostic(SD.Level, SD.ID,
2432 SD.Message, Loc, Ranges, FixIts));
2433 }
2434 Result.swap(RHS&: Out);
2435}
2436
2437void ASTUnit::addFileLevelDecl(Decl *D) {
2438 assert(D);
2439
2440 // We only care about local declarations.
2441 if (D->isFromASTFile())
2442 return;
2443
2444 SourceManager &SM = *SourceMgr;
2445 SourceLocation Loc = D->getLocation();
2446 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2447 return;
2448
2449 // We only keep track of the file-level declarations of each file.
2450 if (!D->getLexicalDeclContext()->isFileContext())
2451 return;
2452
2453 SourceLocation FileLoc = SM.getFileLoc(Loc);
2454 assert(SM.isLocalSourceLocation(FileLoc));
2455 FileID FID;
2456 unsigned Offset;
2457 std::tie(args&: FID, args&: Offset) = SM.getDecomposedLoc(Loc: FileLoc);
2458 if (FID.isInvalid())
2459 return;
2460
2461 std::unique_ptr<LocDeclsTy> &Decls = FileDecls[FID];
2462 if (!Decls)
2463 Decls = std::make_unique<LocDeclsTy>();
2464
2465 std::pair<unsigned, Decl *> LocDecl(Offset, D);
2466
2467 if (Decls->empty() || Decls->back().first <= Offset) {
2468 Decls->push_back(Elt: LocDecl);
2469 return;
2470 }
2471
2472 LocDeclsTy::iterator I =
2473 llvm::upper_bound(Range&: *Decls, Value&: LocDecl, C: llvm::less_first());
2474
2475 Decls->insert(I, Elt: LocDecl);
2476}
2477
2478void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2479 SmallVectorImpl<Decl *> &Decls) {
2480 if (File.isInvalid())
2481 return;
2482
2483 if (SourceMgr->isLoadedFileID(FID: File)) {
2484 assert(Ctx->getExternalSource() && "No external source!");
2485 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2486 Decls);
2487 }
2488
2489 FileDeclsTy::iterator I = FileDecls.find(Val: File);
2490 if (I == FileDecls.end())
2491 return;
2492
2493 LocDeclsTy &LocDecls = *I->second;
2494 if (LocDecls.empty())
2495 return;
2496
2497 LocDeclsTy::iterator BeginIt =
2498 llvm::partition_point(Range&: LocDecls, P: [=](std::pair<unsigned, Decl *> LD) {
2499 return LD.first < Offset;
2500 });
2501 if (BeginIt != LocDecls.begin())
2502 --BeginIt;
2503
2504 // If we are pointing at a top-level decl inside an objc container, we need
2505 // to backtrack until we find it otherwise we will fail to report that the
2506 // region overlaps with an objc container.
2507 while (BeginIt != LocDecls.begin() &&
2508 BeginIt->second->isTopLevelDeclInObjCContainer())
2509 --BeginIt;
2510
2511 LocDeclsTy::iterator EndIt = llvm::upper_bound(
2512 Range&: LocDecls, Value: std::make_pair(x: Offset + Length, y: (Decl *)nullptr),
2513 C: llvm::less_first());
2514 if (EndIt != LocDecls.end())
2515 ++EndIt;
2516
2517 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2518 Decls.push_back(Elt: DIt->second);
2519}
2520
2521SourceLocation ASTUnit::getLocation(const FileEntry *File,
2522 unsigned Line, unsigned Col) const {
2523 const SourceManager &SM = getSourceManager();
2524 SourceLocation Loc = SM.translateFileLineCol(SourceFile: File, Line, Col);
2525 return SM.getMacroArgExpandedLocation(Loc);
2526}
2527
2528SourceLocation ASTUnit::getLocation(const FileEntry *File,
2529 unsigned Offset) const {
2530 const SourceManager &SM = getSourceManager();
2531 SourceLocation FileLoc = SM.translateFileLineCol(SourceFile: File, Line: 1, Col: 1);
2532 return SM.getMacroArgExpandedLocation(Loc: FileLoc.getLocWithOffset(Offset));
2533}
2534
2535/// If \arg Loc is a loaded location from the preamble, returns
2536/// the corresponding local location of the main file, otherwise it returns
2537/// \arg Loc.
2538SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) const {
2539 FileID PreambleID;
2540 if (SourceMgr)
2541 PreambleID = SourceMgr->getPreambleFileID();
2542
2543 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2544 return Loc;
2545
2546 unsigned Offs;
2547 if (SourceMgr->isInFileID(Loc, FID: PreambleID, RelativeOffset: &Offs) && Offs < Preamble->getBounds().Size) {
2548 SourceLocation FileLoc
2549 = SourceMgr->getLocForStartOfFile(FID: SourceMgr->getMainFileID());
2550 return FileLoc.getLocWithOffset(Offset: Offs);
2551 }
2552
2553 return Loc;
2554}
2555
2556/// If \arg Loc is a local location of the main file but inside the
2557/// preamble chunk, returns the corresponding loaded location from the
2558/// preamble, otherwise it returns \arg Loc.
2559SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) const {
2560 FileID PreambleID;
2561 if (SourceMgr)
2562 PreambleID = SourceMgr->getPreambleFileID();
2563
2564 if (Loc.isInvalid() || !Preamble || PreambleID.isInvalid())
2565 return Loc;
2566
2567 unsigned Offs;
2568 if (SourceMgr->isInFileID(Loc, FID: SourceMgr->getMainFileID(), RelativeOffset: &Offs) &&
2569 Offs < Preamble->getBounds().Size) {
2570 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(FID: PreambleID);
2571 return FileLoc.getLocWithOffset(Offset: Offs);
2572 }
2573
2574 return Loc;
2575}
2576
2577bool ASTUnit::isInPreambleFileID(SourceLocation Loc) const {
2578 FileID FID;
2579 if (SourceMgr)
2580 FID = SourceMgr->getPreambleFileID();
2581
2582 if (Loc.isInvalid() || FID.isInvalid())
2583 return false;
2584
2585 return SourceMgr->isInFileID(Loc, FID);
2586}
2587
2588bool ASTUnit::isInMainFileID(SourceLocation Loc) const {
2589 FileID FID;
2590 if (SourceMgr)
2591 FID = SourceMgr->getMainFileID();
2592
2593 if (Loc.isInvalid() || FID.isInvalid())
2594 return false;
2595
2596 return SourceMgr->isInFileID(Loc, FID);
2597}
2598
2599SourceLocation ASTUnit::getEndOfPreambleFileID() const {
2600 FileID FID;
2601 if (SourceMgr)
2602 FID = SourceMgr->getPreambleFileID();
2603
2604 if (FID.isInvalid())
2605 return {};
2606
2607 return SourceMgr->getLocForEndOfFile(FID);
2608}
2609
2610SourceLocation ASTUnit::getStartOfMainFileID() const {
2611 FileID FID;
2612 if (SourceMgr)
2613 FID = SourceMgr->getMainFileID();
2614
2615 if (FID.isInvalid())
2616 return {};
2617
2618 return SourceMgr->getLocForStartOfFile(FID);
2619}
2620
2621llvm::iterator_range<PreprocessingRecord::iterator>
2622ASTUnit::getLocalPreprocessingEntities() const {
2623 if (isMainFileAST()) {
2624 serialization::ModuleFile &
2625 Mod = Reader->getModuleManager().getPrimaryModule();
2626 return Reader->getModulePreprocessedEntities(Mod);
2627 }
2628
2629 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2630 return llvm::make_range(x: PPRec->local_begin(), y: PPRec->local_end());
2631
2632 return llvm::make_range(x: PreprocessingRecord::iterator(),
2633 y: PreprocessingRecord::iterator());
2634}
2635
2636bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2637 if (isMainFileAST()) {
2638 serialization::ModuleFile &
2639 Mod = Reader->getModuleManager().getPrimaryModule();
2640 for (const auto *D : Reader->getModuleFileLevelDecls(Mod)) {
2641 if (!Fn(context, D))
2642 return false;
2643 }
2644
2645 return true;
2646 }
2647
2648 for (ASTUnit::top_level_iterator TL = top_level_begin(),
2649 TLEnd = top_level_end();
2650 TL != TLEnd; ++TL) {
2651 if (!Fn(context, *TL))
2652 return false;
2653 }
2654
2655 return true;
2656}
2657
2658OptionalFileEntryRef ASTUnit::getPCHFile() {
2659 if (!Reader)
2660 return std::nullopt;
2661
2662 serialization::ModuleFile *Mod = nullptr;
2663 Reader->getModuleManager().visit(Visitor: [&Mod](serialization::ModuleFile &M) {
2664 switch (M.Kind) {
2665 case serialization::MK_ImplicitModule:
2666 case serialization::MK_ExplicitModule:
2667 case serialization::MK_PrebuiltModule:
2668 return true; // skip dependencies.
2669 case serialization::MK_PCH:
2670 Mod = &M;
2671 return true; // found it.
2672 case serialization::MK_Preamble:
2673 return false; // look in dependencies.
2674 case serialization::MK_MainFile:
2675 return false; // look in dependencies.
2676 }
2677
2678 return true;
2679 });
2680 if (Mod)
2681 return Mod->File;
2682
2683 return std::nullopt;
2684}
2685
2686bool ASTUnit::isModuleFile() const {
2687 return isMainFileAST() && getLangOpts().isCompilingModule();
2688}
2689
2690InputKind ASTUnit::getInputKind() const {
2691 auto &LangOpts = getLangOpts();
2692
2693 Language Lang;
2694 if (LangOpts.OpenCL)
2695 Lang = Language::OpenCL;
2696 else if (LangOpts.CUDA)
2697 Lang = Language::CUDA;
2698 else if (LangOpts.RenderScript)
2699 Lang = Language::RenderScript;
2700 else if (LangOpts.CPlusPlus)
2701 Lang = LangOpts.ObjC ? Language::ObjCXX : Language::CXX;
2702 else
2703 Lang = LangOpts.ObjC ? Language::ObjC : Language::C;
2704
2705 InputKind::Format Fmt = InputKind::Source;
2706 if (LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap)
2707 Fmt = InputKind::ModuleMap;
2708
2709 // We don't know if input was preprocessed. Assume not.
2710 bool PP = false;
2711
2712 return InputKind(Lang, Fmt, PP);
2713}
2714
2715#ifndef NDEBUG
2716ASTUnit::ConcurrencyState::ConcurrencyState() {
2717 Mutex = new std::recursive_mutex;
2718}
2719
2720ASTUnit::ConcurrencyState::~ConcurrencyState() {
2721 delete static_cast<std::recursive_mutex *>(Mutex);
2722}
2723
2724void ASTUnit::ConcurrencyState::start() {
2725 bool acquired = static_cast<std::recursive_mutex *>(Mutex)->try_lock();
2726 assert(acquired && "Concurrent access to ASTUnit!");
2727}
2728
2729void ASTUnit::ConcurrencyState::finish() {
2730 static_cast<std::recursive_mutex *>(Mutex)->unlock();
2731}
2732
2733#else // NDEBUG
2734
2735ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; }
2736ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2737void ASTUnit::ConcurrencyState::start() {}
2738void ASTUnit::ConcurrencyState::finish() {}
2739
2740#endif // NDEBUG
2741