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