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