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

source code of clang/lib/Frontend/ASTUnit.cpp