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