Coverage Report

Created: 2023-11-11 10:31

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