Coverage Report

Created: 2019-07-24 05:18

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