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