]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Frontend/ASTUnit.cpp
MFC
[FreeBSD/FreeBSD.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/ASTContext.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/TypeOrdering.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Driver/Compilation.h"
21 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Driver/ArgList.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Frontend/FrontendActions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
29 #include "clang/Frontend/FrontendOptions.h"
30 #include "clang/Frontend/Utils.h"
31 #include "clang/Serialization/ASTReader.h"
32 #include "clang/Serialization/ASTSerializationListener.h"
33 #include "clang/Serialization/ASTWriter.h"
34 #include "clang/Lex/HeaderSearch.h"
35 #include "clang/Lex/Preprocessor.h"
36 #include "clang/Basic/TargetOptions.h"
37 #include "clang/Basic/TargetInfo.h"
38 #include "clang/Basic/Diagnostic.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/StringExtras.h"
41 #include "llvm/ADT/StringSet.h"
42 #include "llvm/Support/Atomic.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/Path.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/Timer.h"
48 #include "llvm/Support/CrashRecoveryContext.h"
49 #include <cstdlib>
50 #include <cstdio>
51 #include <sys/stat.h>
52 using namespace clang;
53
54 using llvm::TimeRecord;
55
56 namespace {
57   class SimpleTimer {
58     bool WantTiming;
59     TimeRecord Start;
60     std::string Output;
61
62   public:
63     explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
64       if (WantTiming)
65         Start = TimeRecord::getCurrentTime();
66     }
67
68     void setOutput(const llvm::Twine &Output) {
69       if (WantTiming)
70         this->Output = Output.str();
71     }
72
73     ~SimpleTimer() {
74       if (WantTiming) {
75         TimeRecord Elapsed = TimeRecord::getCurrentTime();
76         Elapsed -= Start;
77         llvm::errs() << Output << ':';
78         Elapsed.print(Elapsed, llvm::errs());
79         llvm::errs() << '\n';
80       }
81     }
82   };
83 }
84
85 /// \brief After failing to build a precompiled preamble (due to
86 /// errors in the source that occurs in the preamble), the number of
87 /// reparses during which we'll skip even trying to precompile the
88 /// preamble.
89 const unsigned DefaultPreambleRebuildInterval = 5;
90
91 /// \brief Tracks the number of ASTUnit objects that are currently active.
92 ///
93 /// Used for debugging purposes only.
94 static llvm::sys::cas_flag ActiveASTUnitObjects;
95
96 ASTUnit::ASTUnit(bool _MainFileIsAST)
97   : OnlyLocalDecls(false), CaptureDiagnostics(false),
98     MainFileIsAST(_MainFileIsAST), 
99     CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
100     OwnsRemappedFileBuffers(true),
101     NumStoredDiagnosticsFromDriver(0),
102     ConcurrencyCheckValue(CheckUnlocked), 
103     PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
104     ShouldCacheCodeCompletionResults(false),
105     CompletionCacheTopLevelHashValue(0),
106     PreambleTopLevelHashValue(0),
107     CurrentTopLevelHashValue(0),
108     UnsafeToFree(false) { 
109   if (getenv("LIBCLANG_OBJTRACKING")) {
110     llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
111     fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
112   }    
113 }
114
115 ASTUnit::~ASTUnit() {
116   ConcurrencyCheckValue = CheckLocked;
117   CleanTemporaryFiles();
118   if (!PreambleFile.empty())
119     llvm::sys::Path(PreambleFile).eraseFromDisk();
120   
121   // Free the buffers associated with remapped files. We are required to
122   // perform this operation here because we explicitly request that the
123   // compiler instance *not* free these buffers for each invocation of the
124   // parser.
125   if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
126     PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
127     for (PreprocessorOptions::remapped_file_buffer_iterator
128            FB = PPOpts.remapped_file_buffer_begin(),
129            FBEnd = PPOpts.remapped_file_buffer_end();
130          FB != FBEnd;
131          ++FB)
132       delete FB->second;
133   }
134   
135   delete SavedMainFileBuffer;
136   delete PreambleBuffer;
137
138   ClearCachedCompletionResults();  
139   
140   if (getenv("LIBCLANG_OBJTRACKING")) {
141     llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
142     fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
143   }    
144 }
145
146 void ASTUnit::CleanTemporaryFiles() {
147   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
148     TemporaryFiles[I].eraseFromDisk();
149   TemporaryFiles.clear();
150 }
151
152 /// \brief Determine the set of code-completion contexts in which this 
153 /// declaration should be shown.
154 static unsigned getDeclShowContexts(NamedDecl *ND,
155                                     const LangOptions &LangOpts,
156                                     bool &IsNestedNameSpecifier) {
157   IsNestedNameSpecifier = false;
158   
159   if (isa<UsingShadowDecl>(ND))
160     ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
161   if (!ND)
162     return 0;
163   
164   unsigned Contexts = 0;
165   if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || 
166       isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
167     // Types can appear in these contexts.
168     if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
169       Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
170                 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
171                 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
172                 | (1 << (CodeCompletionContext::CCC_Statement - 1))
173                 | (1 << (CodeCompletionContext::CCC_Type - 1))
174               | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
175
176     // In C++, types can appear in expressions contexts (for functional casts).
177     if (LangOpts.CPlusPlus)
178       Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
179     
180     // In Objective-C, message sends can send interfaces. In Objective-C++,
181     // all types are available due to functional casts.
182     if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
183       Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
184
185     // Deal with tag names.
186     if (isa<EnumDecl>(ND)) {
187       Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
188       
189       // Part of the nested-name-specifier in C++0x.
190       if (LangOpts.CPlusPlus0x)
191         IsNestedNameSpecifier = true;
192     } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
193       if (Record->isUnion())
194         Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
195       else
196         Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
197       
198       if (LangOpts.CPlusPlus)
199         IsNestedNameSpecifier = true;
200     } else if (isa<ClassTemplateDecl>(ND))
201       IsNestedNameSpecifier = true;
202   } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
203     // Values can appear in these contexts.
204     Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
205              | (1 << (CodeCompletionContext::CCC_Expression - 1))
206              | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
207              | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
208   } else if (isa<ObjCProtocolDecl>(ND)) {
209     Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
210   } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
211     Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
212    
213     // Part of the nested-name-specifier.
214     IsNestedNameSpecifier = true;
215   }
216   
217   return Contexts;
218 }
219
220 void ASTUnit::CacheCodeCompletionResults() {
221   if (!TheSema)
222     return;
223   
224   SimpleTimer Timer(WantTiming);
225   Timer.setOutput("Cache global code completions for " + getMainFileName());
226
227   // Clear out the previous results.
228   ClearCachedCompletionResults();
229   
230   // Gather the set of global code completions.
231   typedef CodeCompletionResult Result;
232   llvm::SmallVector<Result, 8> Results;
233   CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
234   TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
235   
236   // Translate global code completions into cached completions.
237   llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
238   
239   for (unsigned I = 0, N = Results.size(); I != N; ++I) {
240     switch (Results[I].Kind) {
241     case Result::RK_Declaration: {
242       bool IsNestedNameSpecifier = false;
243       CachedCodeCompletionResult CachedResult;
244       CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
245                                                     *CachedCompletionAllocator);
246       CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
247                                                         Ctx->getLangOptions(),
248                                                         IsNestedNameSpecifier);
249       CachedResult.Priority = Results[I].Priority;
250       CachedResult.Kind = Results[I].CursorKind;
251       CachedResult.Availability = Results[I].Availability;
252
253       // Keep track of the type of this completion in an ASTContext-agnostic 
254       // way.
255       QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
256       if (UsageType.isNull()) {
257         CachedResult.TypeClass = STC_Void;
258         CachedResult.Type = 0;
259       } else {
260         CanQualType CanUsageType
261           = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
262         CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
263
264         // Determine whether we have already seen this type. If so, we save
265         // ourselves the work of formatting the type string by using the 
266         // temporary, CanQualType-based hash table to find the associated value.
267         unsigned &TypeValue = CompletionTypes[CanUsageType];
268         if (TypeValue == 0) {
269           TypeValue = CompletionTypes.size();
270           CachedCompletionTypes[QualType(CanUsageType).getAsString()]
271             = TypeValue;
272         }
273         
274         CachedResult.Type = TypeValue;
275       }
276       
277       CachedCompletionResults.push_back(CachedResult);
278       
279       /// Handle nested-name-specifiers in C++.
280       if (TheSema->Context.getLangOptions().CPlusPlus && 
281           IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
282         // The contexts in which a nested-name-specifier can appear in C++.
283         unsigned NNSContexts
284           = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
285           | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
286           | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
287           | (1 << (CodeCompletionContext::CCC_Statement - 1))
288           | (1 << (CodeCompletionContext::CCC_Expression - 1))
289           | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
290           | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
291           | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
292           | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
293           | (1 << (CodeCompletionContext::CCC_Type - 1))
294           | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
295           | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
296
297         if (isa<NamespaceDecl>(Results[I].Declaration) ||
298             isa<NamespaceAliasDecl>(Results[I].Declaration))
299           NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
300
301         if (unsigned RemainingContexts 
302                                 = NNSContexts & ~CachedResult.ShowInContexts) {
303           // If there any contexts where this completion can be a 
304           // nested-name-specifier but isn't already an option, create a 
305           // nested-name-specifier completion.
306           Results[I].StartsNestedNameSpecifier = true;
307           CachedResult.Completion 
308             = Results[I].CreateCodeCompletionString(*TheSema,
309                                                     *CachedCompletionAllocator);
310           CachedResult.ShowInContexts = RemainingContexts;
311           CachedResult.Priority = CCP_NestedNameSpecifier;
312           CachedResult.TypeClass = STC_Void;
313           CachedResult.Type = 0;
314           CachedCompletionResults.push_back(CachedResult);
315         }
316       }
317       break;
318     }
319         
320     case Result::RK_Keyword:
321     case Result::RK_Pattern:
322       // Ignore keywords and patterns; we don't care, since they are so
323       // easily regenerated.
324       break;
325       
326     case Result::RK_Macro: {
327       CachedCodeCompletionResult CachedResult;
328       CachedResult.Completion 
329         = Results[I].CreateCodeCompletionString(*TheSema,
330                                                 *CachedCompletionAllocator);
331       CachedResult.ShowInContexts
332         = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
333         | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
334         | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
335         | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
336         | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
337         | (1 << (CodeCompletionContext::CCC_Statement - 1))
338         | (1 << (CodeCompletionContext::CCC_Expression - 1))
339         | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
340         | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
341         | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
342         | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
343         | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
344       
345       CachedResult.Priority = Results[I].Priority;
346       CachedResult.Kind = Results[I].CursorKind;
347       CachedResult.Availability = Results[I].Availability;
348       CachedResult.TypeClass = STC_Void;
349       CachedResult.Type = 0;
350       CachedCompletionResults.push_back(CachedResult);
351       break;
352     }
353     }
354   }
355   
356   // Save the current top-level hash value.
357   CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
358 }
359
360 void ASTUnit::ClearCachedCompletionResults() {
361   CachedCompletionResults.clear();
362   CachedCompletionTypes.clear();
363   CachedCompletionAllocator = 0;
364 }
365
366 namespace {
367
368 /// \brief Gathers information from ASTReader that will be used to initialize
369 /// a Preprocessor.
370 class ASTInfoCollector : public ASTReaderListener {
371   LangOptions &LangOpt;
372   HeaderSearch &HSI;
373   std::string &TargetTriple;
374   std::string &Predefines;
375   unsigned &Counter;
376
377   unsigned NumHeaderInfos;
378
379 public:
380   ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
381                    std::string &TargetTriple, std::string &Predefines,
382                    unsigned &Counter)
383     : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
384       Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
385
386   virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
387     LangOpt = LangOpts;
388     return false;
389   }
390
391   virtual bool ReadTargetTriple(llvm::StringRef Triple) {
392     TargetTriple = Triple;
393     return false;
394   }
395
396   virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
397                                     llvm::StringRef OriginalFileName,
398                                     std::string &SuggestedPredefines,
399                                     FileManager &FileMgr) {
400     Predefines = Buffers[0].Data;
401     for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
402       Predefines += Buffers[I].Data;
403     }
404     return false;
405   }
406
407   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
408     HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
409   }
410
411   virtual void ReadCounter(unsigned Value) {
412     Counter = Value;
413   }
414 };
415
416 class StoredDiagnosticClient : public DiagnosticClient {
417   llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
418   
419 public:
420   explicit StoredDiagnosticClient(
421                           llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
422     : StoredDiags(StoredDiags) { }
423   
424   virtual void HandleDiagnostic(Diagnostic::Level Level,
425                                 const DiagnosticInfo &Info);
426 };
427
428 /// \brief RAII object that optionally captures diagnostics, if
429 /// there is no diagnostic client to capture them already.
430 class CaptureDroppedDiagnostics {
431   Diagnostic &Diags;
432   StoredDiagnosticClient Client;
433   DiagnosticClient *PreviousClient;
434
435 public:
436   CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, 
437                           llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
438     : Diags(Diags), Client(StoredDiags), PreviousClient(0)
439   {
440     if (RequestCapture || Diags.getClient() == 0) {
441       PreviousClient = Diags.takeClient();
442       Diags.setClient(&Client);
443     }
444   }
445
446   ~CaptureDroppedDiagnostics() {
447     if (Diags.getClient() == &Client) {
448       Diags.takeClient();
449       Diags.setClient(PreviousClient);
450     }
451   }
452 };
453
454 } // anonymous namespace
455
456 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
457                                               const DiagnosticInfo &Info) {
458   // Default implementation (Warnings/errors count).
459   DiagnosticClient::HandleDiagnostic(Level, Info);
460
461   StoredDiags.push_back(StoredDiagnostic(Level, Info));
462 }
463
464 const std::string &ASTUnit::getOriginalSourceFileName() {
465   return OriginalSourceFile;
466 }
467
468 const std::string &ASTUnit::getASTFileName() {
469   assert(isMainFileAST() && "Not an ASTUnit from an AST file!");
470   return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName();
471 }
472
473 llvm::MemoryBuffer *ASTUnit::getBufferForFile(llvm::StringRef Filename,
474                                               std::string *ErrorStr) {
475   assert(FileMgr);
476   return FileMgr->getBufferForFile(Filename, ErrorStr);
477 }
478
479 /// \brief Configure the diagnostics object for use with ASTUnit.
480 void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
481                              const char **ArgBegin, const char **ArgEnd,
482                              ASTUnit &AST, bool CaptureDiagnostics) {
483   if (!Diags.getPtr()) {
484     // No diagnostics engine was provided, so create our own diagnostics object
485     // with the default options.
486     DiagnosticOptions DiagOpts;
487     DiagnosticClient *Client = 0;
488     if (CaptureDiagnostics)
489       Client = new StoredDiagnosticClient(AST.StoredDiagnostics);
490     Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin, 
491                                                 ArgBegin, Client);
492   } else if (CaptureDiagnostics) {
493     Diags->setClient(new StoredDiagnosticClient(AST.StoredDiagnostics));
494   }
495 }
496
497 ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
498                                   llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
499                                   const FileSystemOptions &FileSystemOpts,
500                                   bool OnlyLocalDecls,
501                                   RemappedFile *RemappedFiles,
502                                   unsigned NumRemappedFiles,
503                                   bool CaptureDiagnostics) {
504   llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
505
506   // Recover resources if we crash before exiting this method.
507   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
508     ASTUnitCleanup(AST.get());
509   llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
510     llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
511     DiagCleanup(Diags.getPtr());
512
513   ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
514
515   AST->OnlyLocalDecls = OnlyLocalDecls;
516   AST->CaptureDiagnostics = CaptureDiagnostics;
517   AST->Diagnostics = Diags;
518   AST->FileMgr = new FileManager(FileSystemOpts);
519   AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
520                                      AST->getFileManager());
521   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
522   
523   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
524     FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
525     if (const llvm::MemoryBuffer *
526           memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
527       // Create the file entry for the file that we're mapping from.
528       const FileEntry *FromFile
529         = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
530                                                memBuf->getBufferSize(),
531                                                0);
532       if (!FromFile) {
533         AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
534           << RemappedFiles[I].first;
535         delete memBuf;
536         continue;
537       }
538       
539       // Override the contents of the "from" file with the contents of
540       // the "to" file.
541       AST->getSourceManager().overrideFileContents(FromFile, memBuf);
542
543     } else {
544       const char *fname = fileOrBuf.get<const char *>();
545       const FileEntry *ToFile = AST->FileMgr->getFile(fname);
546       if (!ToFile) {
547         AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
548         << RemappedFiles[I].first << fname;
549         continue;
550       }
551
552       // Create the file entry for the file that we're mapping from.
553       const FileEntry *FromFile
554         = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
555                                                ToFile->getSize(),
556                                                0);
557       if (!FromFile) {
558         AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
559           << RemappedFiles[I].first;
560         delete memBuf;
561         continue;
562       }
563       
564       // Override the contents of the "from" file with the contents of
565       // the "to" file.
566       AST->getSourceManager().overrideFileContents(FromFile, ToFile);
567     }
568   }
569   
570   // Gather Info for preprocessor construction later on.
571
572   LangOptions LangInfo;
573   HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
574   std::string TargetTriple;
575   std::string Predefines;
576   unsigned Counter;
577
578   llvm::OwningPtr<ASTReader> Reader;
579
580   Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(),
581                              AST->getDiagnostics()));
582   Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple,
583                                            Predefines, Counter));
584
585   switch (Reader->ReadAST(Filename, ASTReader::MainFile)) {
586   case ASTReader::Success:
587     break;
588
589   case ASTReader::Failure:
590   case ASTReader::IgnorePCH:
591     AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
592     return NULL;
593   }
594
595   AST->OriginalSourceFile = Reader->getOriginalSourceFile();
596
597   // AST file loaded successfully. Now create the preprocessor.
598
599   // Get information about the target being compiled for.
600   //
601   // FIXME: This is broken, we should store the TargetOptions in the AST file.
602   TargetOptions TargetOpts;
603   TargetOpts.ABI = "";
604   TargetOpts.CXXABI = "";
605   TargetOpts.CPU = "";
606   TargetOpts.Features.clear();
607   TargetOpts.Triple = TargetTriple;
608   AST->Target = TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
609                                              TargetOpts);
610   AST->PP = new Preprocessor(AST->getDiagnostics(), LangInfo, *AST->Target,
611                              AST->getSourceManager(), HeaderInfo);
612   Preprocessor &PP = *AST->PP;
613
614   PP.setPredefines(Reader->getSuggestedPredefines());
615   PP.setCounterValue(Counter);
616   Reader->setPreprocessor(PP);
617
618   // Create and initialize the ASTContext.
619
620   AST->Ctx = new ASTContext(LangInfo,
621                             AST->getSourceManager(),
622                             *AST->Target,
623                             PP.getIdentifierTable(),
624                             PP.getSelectorTable(),
625                             PP.getBuiltinInfo(),
626                             /* size_reserve = */0);
627   ASTContext &Context = *AST->Ctx;
628
629   Reader->InitializeContext(Context);
630
631   // Attach the AST reader to the AST context as an external AST
632   // source, so that declarations will be deserialized from the
633   // AST file as needed.
634   ASTReader *ReaderPtr = Reader.get();
635   llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
636   Context.setExternalSource(Source);
637
638   // Create an AST consumer, even though it isn't used.
639   AST->Consumer.reset(new ASTConsumer);
640   
641   // Create a semantic analysis object and tell the AST reader about it.
642   AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
643   AST->TheSema->Initialize();
644   ReaderPtr->InitializeSema(*AST->TheSema);
645
646   return AST.take();
647 }
648
649 namespace {
650
651 /// \brief Preprocessor callback class that updates a hash value with the names 
652 /// of all macros that have been defined by the translation unit.
653 class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
654   unsigned &Hash;
655   
656 public:
657   explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
658   
659   virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
660     Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
661   }
662 };
663
664 /// \brief Add the given declaration to the hash of all top-level entities.
665 void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
666   if (!D)
667     return;
668   
669   DeclContext *DC = D->getDeclContext();
670   if (!DC)
671     return;
672   
673   if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
674     return;
675
676   if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
677     if (ND->getIdentifier())
678       Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
679     else if (DeclarationName Name = ND->getDeclName()) {
680       std::string NameStr = Name.getAsString();
681       Hash = llvm::HashString(NameStr, Hash);
682     }
683     return;
684   }
685   
686   if (ObjCForwardProtocolDecl *Forward 
687       = dyn_cast<ObjCForwardProtocolDecl>(D)) {
688     for (ObjCForwardProtocolDecl::protocol_iterator 
689          P = Forward->protocol_begin(),
690          PEnd = Forward->protocol_end();
691          P != PEnd; ++P)
692       AddTopLevelDeclarationToHash(*P, Hash);
693     return;
694   }
695   
696   if (ObjCClassDecl *Class = llvm::dyn_cast<ObjCClassDecl>(D)) {
697     for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
698          I != IEnd; ++I)
699       AddTopLevelDeclarationToHash(I->getInterface(), Hash);
700     return;
701   }
702 }
703
704 class TopLevelDeclTrackerConsumer : public ASTConsumer {
705   ASTUnit &Unit;
706   unsigned &Hash;
707   
708 public:
709   TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
710     : Unit(_Unit), Hash(Hash) {
711     Hash = 0;
712   }
713   
714   void HandleTopLevelDecl(DeclGroupRef D) {
715     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
716       Decl *D = *it;
717       // FIXME: Currently ObjC method declarations are incorrectly being
718       // reported as top-level declarations, even though their DeclContext
719       // is the containing ObjC @interface/@implementation.  This is a
720       // fundamental problem in the parser right now.
721       if (isa<ObjCMethodDecl>(D))
722         continue;
723
724       AddTopLevelDeclarationToHash(D, Hash);
725       Unit.addTopLevelDecl(D);
726     }
727   }
728
729   // We're not interested in "interesting" decls.
730   void HandleInterestingDecl(DeclGroupRef) {}
731 };
732
733 class TopLevelDeclTrackerAction : public ASTFrontendAction {
734 public:
735   ASTUnit &Unit;
736
737   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
738                                          llvm::StringRef InFile) {
739     CI.getPreprocessor().addPPCallbacks(
740      new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
741     return new TopLevelDeclTrackerConsumer(Unit, 
742                                            Unit.getCurrentTopLevelHashValue());
743   }
744
745 public:
746   TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
747
748   virtual bool hasCodeCompletionSupport() const { return false; }
749   virtual bool usesCompleteTranslationUnit()  { 
750     return Unit.isCompleteTranslationUnit(); 
751   }
752 };
753
754 class PrecompilePreambleConsumer : public PCHGenerator, 
755                                    public ASTSerializationListener {
756   ASTUnit &Unit;
757   unsigned &Hash;                                   
758   std::vector<Decl *> TopLevelDecls;
759                                      
760 public:
761   PrecompilePreambleConsumer(ASTUnit &Unit,
762                              const Preprocessor &PP, bool Chaining,
763                              const char *isysroot, llvm::raw_ostream *Out)
764     : PCHGenerator(PP, "", Chaining, isysroot, Out), Unit(Unit),
765       Hash(Unit.getCurrentTopLevelHashValue()) {
766     Hash = 0;
767   }
768
769   virtual void HandleTopLevelDecl(DeclGroupRef D) {
770     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
771       Decl *D = *it;
772       // FIXME: Currently ObjC method declarations are incorrectly being
773       // reported as top-level declarations, even though their DeclContext
774       // is the containing ObjC @interface/@implementation.  This is a
775       // fundamental problem in the parser right now.
776       if (isa<ObjCMethodDecl>(D))
777         continue;
778       AddTopLevelDeclarationToHash(D, Hash);
779       TopLevelDecls.push_back(D);
780     }
781   }
782
783   virtual void HandleTranslationUnit(ASTContext &Ctx) {
784     PCHGenerator::HandleTranslationUnit(Ctx);
785     if (!Unit.getDiagnostics().hasErrorOccurred()) {
786       // Translate the top-level declarations we captured during
787       // parsing into declaration IDs in the precompiled
788       // preamble. This will allow us to deserialize those top-level
789       // declarations when requested.
790       for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
791         Unit.addTopLevelDeclFromPreamble(
792                                       getWriter().getDeclID(TopLevelDecls[I]));
793     }
794   }
795                                      
796   virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
797                                             uint64_t Offset) {
798     Unit.addPreprocessedEntityFromPreamble(Offset);
799   }
800                                      
801   virtual ASTSerializationListener *GetASTSerializationListener() {
802     return this;
803   }
804 };
805
806 class PrecompilePreambleAction : public ASTFrontendAction {
807   ASTUnit &Unit;
808
809 public:
810   explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
811
812   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
813                                          llvm::StringRef InFile) {
814     std::string Sysroot;
815     std::string OutputFile;
816     llvm::raw_ostream *OS = 0;
817     bool Chaining;
818     if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
819                                                        OutputFile,
820                                                        OS, Chaining))
821       return 0;
822     
823     const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
824                              Sysroot.c_str() : 0;  
825     CI.getPreprocessor().addPPCallbacks(
826      new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
827     return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining,
828                                           isysroot, OS);
829   }
830
831   virtual bool hasCodeCompletionSupport() const { return false; }
832   virtual bool hasASTFileSupport() const { return false; }
833   virtual bool usesCompleteTranslationUnit() { return false; }
834 };
835
836 }
837
838 /// Parse the source file into a translation unit using the given compiler
839 /// invocation, replacing the current translation unit.
840 ///
841 /// \returns True if a failure occurred that causes the ASTUnit not to
842 /// contain any translation-unit information, false otherwise.
843 bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
844   delete SavedMainFileBuffer;
845   SavedMainFileBuffer = 0;
846   
847   if (!Invocation) {
848     delete OverrideMainBuffer;
849     return true;
850   }
851   
852   // Create the compiler instance to use for building the AST.
853   llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
854
855   // Recover resources if we crash before exiting this method.
856   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
857     CICleanup(Clang.get());
858
859   Clang->setInvocation(&*Invocation);
860   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
861     
862   // Set up diagnostics, capturing any diagnostics that would
863   // otherwise be dropped.
864   Clang->setDiagnostics(&getDiagnostics());
865   
866   // Create the target instance.
867   Clang->getTargetOpts().Features = TargetFeatures;
868   Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
869                    Clang->getTargetOpts()));
870   if (!Clang->hasTarget()) {
871     delete OverrideMainBuffer;
872     return true;
873   }
874
875   // Inform the target of the language options.
876   //
877   // FIXME: We shouldn't need to do this, the target should be immutable once
878   // created. This complexity should be lifted elsewhere.
879   Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
880   
881   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
882          "Invocation must have exactly one source file!");
883   assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
884          "FIXME: AST inputs not yet supported here!");
885   assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
886          "IR inputs not support here!");
887
888   // Configure the various subsystems.
889   // FIXME: Should we retain the previous file manager?
890   FileSystemOpts = Clang->getFileSystemOpts();
891   FileMgr = new FileManager(FileSystemOpts);
892   SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
893   TheSema.reset();
894   Ctx = 0;
895   PP = 0;
896   
897   // Clear out old caches and data.
898   TopLevelDecls.clear();
899   PreprocessedEntities.clear();
900   CleanTemporaryFiles();
901   PreprocessedEntitiesByFile.clear();
902
903   if (!OverrideMainBuffer) {
904     StoredDiagnostics.erase(
905                     StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
906                             StoredDiagnostics.end());
907     TopLevelDeclsInPreamble.clear();
908     PreprocessedEntitiesInPreamble.clear();
909   }
910
911   // Create a file manager object to provide access to and cache the filesystem.
912   Clang->setFileManager(&getFileManager());
913   
914   // Create the source manager.
915   Clang->setSourceManager(&getSourceManager());
916   
917   // If the main file has been overridden due to the use of a preamble,
918   // make that override happen and introduce the preamble.
919   PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
920   std::string PriorImplicitPCHInclude;
921   if (OverrideMainBuffer) {
922     PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
923     PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
924     PreprocessorOpts.PrecompiledPreambleBytes.second
925                                                     = PreambleEndsAtStartOfLine;
926     PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
927     PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
928     PreprocessorOpts.DisablePCHValidation = true;
929     
930     // The stored diagnostic has the old source manager in it; update
931     // the locations to refer into the new source manager. Since we've
932     // been careful to make sure that the source manager's state
933     // before and after are identical, so that we can reuse the source
934     // location itself.
935     for (unsigned I = NumStoredDiagnosticsFromDriver, 
936                   N = StoredDiagnostics.size(); 
937          I < N; ++I) {
938       FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
939                         getSourceManager());
940       StoredDiagnostics[I].setLocation(Loc);
941     }
942
943     // Keep track of the override buffer;
944     SavedMainFileBuffer = OverrideMainBuffer;
945   } else {
946     PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
947     PreprocessorOpts.PrecompiledPreambleBytes.second = false;
948   }
949   
950   llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
951     new TopLevelDeclTrackerAction(*this));
952     
953   // Recover resources if we crash before exiting this method.
954   llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
955     ActCleanup(Act.get());
956
957   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
958                             Clang->getFrontendOpts().Inputs[0].first))
959     goto error;
960   
961   Act->Execute();
962   
963   // Steal the created target, context, and preprocessor.
964   TheSema.reset(Clang->takeSema());
965   Consumer.reset(Clang->takeASTConsumer());
966   Ctx = &Clang->getASTContext();
967   PP = &Clang->getPreprocessor();
968   Clang->setSourceManager(0);
969   Clang->setFileManager(0);
970   Target = &Clang->getTarget();
971   
972   Act->EndSourceFile();
973
974   // Remove the overridden buffer we used for the preamble.
975   if (OverrideMainBuffer) {
976     PreprocessorOpts.eraseRemappedFile(
977                                PreprocessorOpts.remapped_file_buffer_end() - 1);
978     PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
979   }
980
981   return false;
982
983 error:
984   // Remove the overridden buffer we used for the preamble.
985   if (OverrideMainBuffer) {
986     PreprocessorOpts.eraseRemappedFile(
987                                PreprocessorOpts.remapped_file_buffer_end() - 1);
988     PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
989     delete OverrideMainBuffer;
990     SavedMainFileBuffer = 0;
991   }
992   
993   StoredDiagnostics.clear();
994   return true;
995 }
996
997 /// \brief Simple function to retrieve a path for a preamble precompiled header.
998 static std::string GetPreamblePCHPath() {
999   // FIXME: This is lame; sys::Path should provide this function (in particular,
1000   // it should know how to find the temporary files dir).
1001   // FIXME: This is really lame. I copied this code from the Driver!
1002   // FIXME: This is a hack so that we can override the preamble file during
1003   // crash-recovery testing, which is the only case where the preamble files
1004   // are not necessarily cleaned up. 
1005   const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1006   if (TmpFile)
1007     return TmpFile;
1008   
1009   std::string Error;
1010   const char *TmpDir = ::getenv("TMPDIR");
1011   if (!TmpDir)
1012     TmpDir = ::getenv("TEMP");
1013   if (!TmpDir)
1014     TmpDir = ::getenv("TMP");
1015 #ifdef LLVM_ON_WIN32
1016   if (!TmpDir)
1017     TmpDir = ::getenv("USERPROFILE");
1018 #endif
1019   if (!TmpDir)
1020     TmpDir = "/tmp";
1021   llvm::sys::Path P(TmpDir);
1022   P.createDirectoryOnDisk(true);
1023   P.appendComponent("preamble");
1024   P.appendSuffix("pch");
1025   if (P.createTemporaryFileOnDisk())
1026     return std::string();
1027   
1028   return P.str();
1029 }
1030
1031 /// \brief Compute the preamble for the main file, providing the source buffer
1032 /// that corresponds to the main file along with a pair (bytes, start-of-line)
1033 /// that describes the preamble.
1034 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 
1035 ASTUnit::ComputePreamble(CompilerInvocation &Invocation, 
1036                          unsigned MaxLines, bool &CreatedBuffer) {
1037   FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1038   PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1039   CreatedBuffer = false;
1040   
1041   // Try to determine if the main file has been remapped, either from the 
1042   // command line (to another file) or directly through the compiler invocation
1043   // (to a memory buffer).
1044   llvm::MemoryBuffer *Buffer = 0;
1045   llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1046   if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1047     // Check whether there is a file-file remapping of the main file
1048     for (PreprocessorOptions::remapped_file_iterator
1049           M = PreprocessorOpts.remapped_file_begin(),
1050           E = PreprocessorOpts.remapped_file_end();
1051          M != E;
1052          ++M) {
1053       llvm::sys::PathWithStatus MPath(M->first);    
1054       if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1055         if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1056           // We found a remapping. Try to load the resulting, remapped source.
1057           if (CreatedBuffer) {
1058             delete Buffer;
1059             CreatedBuffer = false;
1060           }
1061           
1062           Buffer = getBufferForFile(M->second);
1063           if (!Buffer)
1064             return std::make_pair((llvm::MemoryBuffer*)0, 
1065                                   std::make_pair(0, true));
1066           CreatedBuffer = true;
1067         }
1068       }
1069     }
1070     
1071     // Check whether there is a file-buffer remapping. It supercedes the
1072     // file-file remapping.
1073     for (PreprocessorOptions::remapped_file_buffer_iterator
1074            M = PreprocessorOpts.remapped_file_buffer_begin(),
1075            E = PreprocessorOpts.remapped_file_buffer_end();
1076          M != E;
1077          ++M) {
1078       llvm::sys::PathWithStatus MPath(M->first);    
1079       if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1080         if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1081           // We found a remapping. 
1082           if (CreatedBuffer) {
1083             delete Buffer;
1084             CreatedBuffer = false;
1085           }
1086           
1087           Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1088         }
1089       }
1090     }
1091   }
1092   
1093   // If the main source file was not remapped, load it now.
1094   if (!Buffer) {
1095     Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1096     if (!Buffer)
1097       return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));    
1098     
1099     CreatedBuffer = true;
1100   }
1101   
1102   return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines));
1103 }
1104
1105 static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1106                                                       unsigned NewSize,
1107                                                       llvm::StringRef NewName) {
1108   llvm::MemoryBuffer *Result
1109     = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1110   memcpy(const_cast<char*>(Result->getBufferStart()), 
1111          Old->getBufferStart(), Old->getBufferSize());
1112   memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), 
1113          ' ', NewSize - Old->getBufferSize() - 1);
1114   const_cast<char*>(Result->getBufferEnd())[-1] = '\n';  
1115   
1116   return Result;
1117 }
1118
1119 /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1120 /// the source file.
1121 ///
1122 /// This routine will compute the preamble of the main source file. If a
1123 /// non-trivial preamble is found, it will precompile that preamble into a 
1124 /// precompiled header so that the precompiled preamble can be used to reduce
1125 /// reparsing time. If a precompiled preamble has already been constructed,
1126 /// this routine will determine if it is still valid and, if so, avoid 
1127 /// rebuilding the precompiled preamble.
1128 ///
1129 /// \param AllowRebuild When true (the default), this routine is
1130 /// allowed to rebuild the precompiled preamble if it is found to be
1131 /// out-of-date.
1132 ///
1133 /// \param MaxLines When non-zero, the maximum number of lines that
1134 /// can occur within the preamble.
1135 ///
1136 /// \returns If the precompiled preamble can be used, returns a newly-allocated
1137 /// buffer that should be used in place of the main file when doing so.
1138 /// Otherwise, returns a NULL pointer.
1139 llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1140                                           CompilerInvocation PreambleInvocation,
1141                                                            bool AllowRebuild,
1142                                                            unsigned MaxLines) {
1143   FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts();
1144   PreprocessorOptions &PreprocessorOpts
1145     = PreambleInvocation.getPreprocessorOpts();
1146
1147   bool CreatedPreambleBuffer = false;
1148   std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble 
1149     = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1150
1151   // If ComputePreamble() Take ownership of the
1152   llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1153   if (CreatedPreambleBuffer)
1154     OwnedPreambleBuffer.reset(NewPreamble.first);
1155
1156   if (!NewPreamble.second.first) {
1157     // We couldn't find a preamble in the main source. Clear out the current
1158     // preamble, if we have one. It's obviously no good any more.
1159     Preamble.clear();
1160     if (!PreambleFile.empty()) {
1161       llvm::sys::Path(PreambleFile).eraseFromDisk();
1162       PreambleFile.clear();
1163     }
1164
1165     // The next time we actually see a preamble, precompile it.
1166     PreambleRebuildCounter = 1;
1167     return 0;
1168   }
1169   
1170   if (!Preamble.empty()) {
1171     // We've previously computed a preamble. Check whether we have the same
1172     // preamble now that we did before, and that there's enough space in
1173     // the main-file buffer within the precompiled preamble to fit the
1174     // new main file.
1175     if (Preamble.size() == NewPreamble.second.first &&
1176         PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1177         NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1178         memcmp(&Preamble[0], NewPreamble.first->getBufferStart(),
1179                NewPreamble.second.first) == 0) {
1180       // The preamble has not changed. We may be able to re-use the precompiled
1181       // preamble.
1182
1183       // Check that none of the files used by the preamble have changed.
1184       bool AnyFileChanged = false;
1185           
1186       // First, make a record of those files that have been overridden via
1187       // remapping or unsaved_files.
1188       llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1189       for (PreprocessorOptions::remapped_file_iterator
1190                 R = PreprocessorOpts.remapped_file_begin(),
1191              REnd = PreprocessorOpts.remapped_file_end();
1192            !AnyFileChanged && R != REnd;
1193            ++R) {
1194         struct stat StatBuf;
1195         if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1196           // If we can't stat the file we're remapping to, assume that something
1197           // horrible happened.
1198           AnyFileChanged = true;
1199           break;
1200         }
1201         
1202         OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, 
1203                                                    StatBuf.st_mtime);
1204       }
1205       for (PreprocessorOptions::remapped_file_buffer_iterator
1206                 R = PreprocessorOpts.remapped_file_buffer_begin(),
1207              REnd = PreprocessorOpts.remapped_file_buffer_end();
1208            !AnyFileChanged && R != REnd;
1209            ++R) {
1210         // FIXME: Should we actually compare the contents of file->buffer
1211         // remappings?
1212         OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), 
1213                                                    0);
1214       }
1215        
1216       // Check whether anything has changed.
1217       for (llvm::StringMap<std::pair<off_t, time_t> >::iterator 
1218              F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1219            !AnyFileChanged && F != FEnd; 
1220            ++F) {
1221         llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1222           = OverriddenFiles.find(F->first());
1223         if (Overridden != OverriddenFiles.end()) {
1224           // This file was remapped; check whether the newly-mapped file 
1225           // matches up with the previous mapping.
1226           if (Overridden->second != F->second)
1227             AnyFileChanged = true;
1228           continue;
1229         }
1230         
1231         // The file was not remapped; check whether it has changed on disk.
1232         struct stat StatBuf;
1233         if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1234           // If we can't stat the file, assume that something horrible happened.
1235           AnyFileChanged = true;
1236         } else if (StatBuf.st_size != F->second.first || 
1237                    StatBuf.st_mtime != F->second.second)
1238           AnyFileChanged = true;
1239       }
1240           
1241       if (!AnyFileChanged) {
1242         // Okay! We can re-use the precompiled preamble.
1243
1244         // Set the state of the diagnostic object to mimic its state
1245         // after parsing the preamble.
1246         // FIXME: This won't catch any #pragma push warning changes that
1247         // have occurred in the preamble.
1248         getDiagnostics().Reset();
1249         ProcessWarningOptions(getDiagnostics(), 
1250                               PreambleInvocation.getDiagnosticOpts());
1251         getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1252         if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble)
1253           StoredDiagnostics.erase(
1254             StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble,
1255                                   StoredDiagnostics.end());
1256
1257         // Create a version of the main file buffer that is padded to
1258         // buffer size we reserved when creating the preamble.
1259         return CreatePaddedMainFileBuffer(NewPreamble.first, 
1260                                           PreambleReservedSize,
1261                                           FrontendOpts.Inputs[0].second);
1262       }
1263     }
1264
1265     // If we aren't allowed to rebuild the precompiled preamble, just
1266     // return now.
1267     if (!AllowRebuild)
1268       return 0;
1269
1270     // We can't reuse the previously-computed preamble. Build a new one.
1271     Preamble.clear();
1272     llvm::sys::Path(PreambleFile).eraseFromDisk();
1273     PreambleRebuildCounter = 1;
1274   } else if (!AllowRebuild) {
1275     // We aren't allowed to rebuild the precompiled preamble; just
1276     // return now.
1277     return 0;
1278   }
1279
1280   // If the preamble rebuild counter > 1, it's because we previously
1281   // failed to build a preamble and we're not yet ready to try
1282   // again. Decrement the counter and return a failure.
1283   if (PreambleRebuildCounter > 1) {
1284     --PreambleRebuildCounter;
1285     return 0;
1286   }
1287
1288   // Create a temporary file for the precompiled preamble. In rare 
1289   // circumstances, this can fail.
1290   std::string PreamblePCHPath = GetPreamblePCHPath();
1291   if (PreamblePCHPath.empty()) {
1292     // Try again next time.
1293     PreambleRebuildCounter = 1;
1294     return 0;
1295   }
1296   
1297   // We did not previously compute a preamble, or it can't be reused anyway.
1298   SimpleTimer PreambleTimer(WantTiming);
1299   PreambleTimer.setOutput("Precompiling preamble");
1300   
1301   // Create a new buffer that stores the preamble. The buffer also contains
1302   // extra space for the original contents of the file (which will be present
1303   // when we actually parse the file) along with more room in case the file
1304   // grows.  
1305   PreambleReservedSize = NewPreamble.first->getBufferSize();
1306   if (PreambleReservedSize < 4096)
1307     PreambleReservedSize = 8191;
1308   else
1309     PreambleReservedSize *= 2;
1310
1311   // Save the preamble text for later; we'll need to compare against it for
1312   // subsequent reparses.
1313   Preamble.assign(NewPreamble.first->getBufferStart(), 
1314                   NewPreamble.first->getBufferStart() 
1315                                                   + NewPreamble.second.first);
1316   PreambleEndsAtStartOfLine = NewPreamble.second.second;
1317
1318   delete PreambleBuffer;
1319   PreambleBuffer
1320     = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1321                                                 FrontendOpts.Inputs[0].second);
1322   memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), 
1323          NewPreamble.first->getBufferStart(), Preamble.size());
1324   memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), 
1325          ' ', PreambleReservedSize - Preamble.size() - 1);
1326   const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';  
1327   
1328   // Remap the main source file to the preamble buffer.
1329   llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1330   PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1331   
1332   // Tell the compiler invocation to generate a temporary precompiled header.
1333   FrontendOpts.ProgramAction = frontend::GeneratePCH;
1334   FrontendOpts.ChainedPCH = true;
1335   // FIXME: Generate the precompiled header into memory?
1336   FrontendOpts.OutputFile = PreamblePCHPath;
1337   PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1338   PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1339   
1340   // Create the compiler instance to use for building the precompiled preamble.
1341   llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1342
1343   // Recover resources if we crash before exiting this method.
1344   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1345     CICleanup(Clang.get());
1346
1347   Clang->setInvocation(&PreambleInvocation);
1348   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1349   
1350   // Set up diagnostics, capturing all of the diagnostics produced.
1351   Clang->setDiagnostics(&getDiagnostics());
1352   
1353   // Create the target instance.
1354   Clang->getTargetOpts().Features = TargetFeatures;
1355   Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1356                                                Clang->getTargetOpts()));
1357   if (!Clang->hasTarget()) {
1358     llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1359     Preamble.clear();
1360     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1361     PreprocessorOpts.eraseRemappedFile(
1362                                PreprocessorOpts.remapped_file_buffer_end() - 1);
1363     return 0;
1364   }
1365   
1366   // Inform the target of the language options.
1367   //
1368   // FIXME: We shouldn't need to do this, the target should be immutable once
1369   // created. This complexity should be lifted elsewhere.
1370   Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1371   
1372   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1373          "Invocation must have exactly one source file!");
1374   assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1375          "FIXME: AST inputs not yet supported here!");
1376   assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1377          "IR inputs not support here!");
1378   
1379   // Clear out old caches and data.
1380   getDiagnostics().Reset();
1381   ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1382   StoredDiagnostics.erase(
1383                     StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
1384                           StoredDiagnostics.end());
1385   TopLevelDecls.clear();
1386   TopLevelDeclsInPreamble.clear();
1387   PreprocessedEntities.clear();
1388   PreprocessedEntitiesInPreamble.clear();
1389   
1390   // Create a file manager object to provide access to and cache the filesystem.
1391   Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1392   
1393   // Create the source manager.
1394   Clang->setSourceManager(new SourceManager(getDiagnostics(),
1395                                             Clang->getFileManager()));
1396   
1397   llvm::OwningPtr<PrecompilePreambleAction> Act;
1398   Act.reset(new PrecompilePreambleAction(*this));
1399   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1400                             Clang->getFrontendOpts().Inputs[0].first)) {
1401     llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1402     Preamble.clear();
1403     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1404     PreprocessorOpts.eraseRemappedFile(
1405                                PreprocessorOpts.remapped_file_buffer_end() - 1);
1406     return 0;
1407   }
1408   
1409   Act->Execute();
1410   Act->EndSourceFile();
1411
1412   if (Diagnostics->hasErrorOccurred()) {
1413     // There were errors parsing the preamble, so no precompiled header was
1414     // generated. Forget that we even tried.
1415     // FIXME: Should we leave a note for ourselves to try again?
1416     llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1417     Preamble.clear();
1418     TopLevelDeclsInPreamble.clear();
1419     PreprocessedEntities.clear();
1420     PreprocessedEntitiesInPreamble.clear();
1421     PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1422     PreprocessorOpts.eraseRemappedFile(
1423                                PreprocessorOpts.remapped_file_buffer_end() - 1);
1424     return 0;
1425   }
1426   
1427   // Keep track of the preamble we precompiled.
1428   PreambleFile = FrontendOpts.OutputFile;
1429   NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1430   NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1431   
1432   // Keep track of all of the files that the source manager knows about,
1433   // so we can verify whether they have changed or not.
1434   FilesInPreamble.clear();
1435   SourceManager &SourceMgr = Clang->getSourceManager();
1436   const llvm::MemoryBuffer *MainFileBuffer
1437     = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1438   for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1439                                      FEnd = SourceMgr.fileinfo_end();
1440        F != FEnd;
1441        ++F) {
1442     const FileEntry *File = F->second->OrigEntry;
1443     if (!File || F->second->getRawBuffer() == MainFileBuffer)
1444       continue;
1445     
1446     FilesInPreamble[File->getName()]
1447       = std::make_pair(F->second->getSize(), File->getModificationTime());
1448   }
1449   
1450   PreambleRebuildCounter = 1;
1451   PreprocessorOpts.eraseRemappedFile(
1452                                PreprocessorOpts.remapped_file_buffer_end() - 1);
1453   
1454   // If the hash of top-level entities differs from the hash of the top-level
1455   // entities the last time we rebuilt the preamble, clear out the completion
1456   // cache.
1457   if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1458     CompletionCacheTopLevelHashValue = 0;
1459     PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1460   }
1461   
1462   return CreatePaddedMainFileBuffer(NewPreamble.first, 
1463                                     PreambleReservedSize,
1464                                     FrontendOpts.Inputs[0].second);
1465 }
1466
1467 void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1468   std::vector<Decl *> Resolved;
1469   Resolved.reserve(TopLevelDeclsInPreamble.size());
1470   ExternalASTSource &Source = *getASTContext().getExternalSource();
1471   for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1472     // Resolve the declaration ID to an actual declaration, possibly
1473     // deserializing the declaration in the process.
1474     Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1475     if (D)
1476       Resolved.push_back(D);
1477   }
1478   TopLevelDeclsInPreamble.clear();
1479   TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1480 }
1481
1482 void ASTUnit::RealizePreprocessedEntitiesFromPreamble() {
1483   if (!PP)
1484     return;
1485   
1486   PreprocessingRecord *PPRec = PP->getPreprocessingRecord();
1487   if (!PPRec)
1488     return;
1489   
1490   ExternalPreprocessingRecordSource *External = PPRec->getExternalSource();
1491   if (!External)
1492     return;
1493
1494   for (unsigned I = 0, N = PreprocessedEntitiesInPreamble.size(); I != N; ++I) {
1495     if (PreprocessedEntity *PE
1496           = External->ReadPreprocessedEntityAtOffset(
1497                                             PreprocessedEntitiesInPreamble[I]))
1498       PreprocessedEntities.push_back(PE);
1499   }
1500   
1501   if (PreprocessedEntities.empty())
1502     return;
1503   
1504   PreprocessedEntities.insert(PreprocessedEntities.end(), 
1505                               PPRec->begin(true), PPRec->end(true));
1506 }
1507
1508 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_begin() {
1509   if (!PreprocessedEntitiesInPreamble.empty() &&
1510       PreprocessedEntities.empty())
1511     RealizePreprocessedEntitiesFromPreamble();
1512   
1513   if (PreprocessedEntities.empty())
1514     if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1515       return PPRec->begin(true);
1516   
1517   return PreprocessedEntities.begin();
1518 }
1519
1520 ASTUnit::pp_entity_iterator ASTUnit::pp_entity_end() {
1521   if (!PreprocessedEntitiesInPreamble.empty() &&
1522       PreprocessedEntities.empty())
1523     RealizePreprocessedEntitiesFromPreamble();
1524   
1525   if (PreprocessedEntities.empty())
1526     if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
1527       return PPRec->end(true);
1528   
1529   return PreprocessedEntities.end();
1530 }
1531
1532 unsigned ASTUnit::getMaxPCHLevel() const {
1533   if (!getOnlyLocalDecls())
1534     return Decl::MaxPCHLevel;
1535
1536   return 0;
1537 }
1538
1539 llvm::StringRef ASTUnit::getMainFileName() const {
1540   return Invocation->getFrontendOpts().Inputs[0].second;
1541 }
1542
1543 ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1544                          llvm::IntrusiveRefCntPtr<Diagnostic> Diags) {
1545   llvm::OwningPtr<ASTUnit> AST;
1546   AST.reset(new ASTUnit(false));
1547   ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false);
1548   AST->Diagnostics = Diags;
1549   AST->Invocation = CI;
1550   AST->FileSystemOpts = CI->getFileSystemOpts();
1551   AST->FileMgr = new FileManager(AST->FileSystemOpts);
1552   AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr);
1553
1554   return AST.take();
1555 }
1556
1557 bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1558   if (!Invocation)
1559     return true;
1560   
1561   // We'll manage file buffers ourselves.
1562   Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1563   Invocation->getFrontendOpts().DisableFree = false;
1564   ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1565
1566   // Save the target features.
1567   TargetFeatures = Invocation->getTargetOpts().Features;
1568   
1569   llvm::MemoryBuffer *OverrideMainBuffer = 0;
1570   if (PrecompilePreamble) {
1571     PreambleRebuildCounter = 2;
1572     OverrideMainBuffer
1573       = getMainBufferWithPrecompiledPreamble(*Invocation);
1574   }
1575   
1576   SimpleTimer ParsingTimer(WantTiming);
1577   ParsingTimer.setOutput("Parsing " + getMainFileName());
1578   
1579   // Recover resources if we crash before exiting this method.
1580   llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1581     MemBufferCleanup(OverrideMainBuffer);
1582   
1583   return Parse(OverrideMainBuffer);
1584 }
1585
1586 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1587                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1588                                              bool OnlyLocalDecls,
1589                                              bool CaptureDiagnostics,
1590                                              bool PrecompilePreamble,
1591                                              bool CompleteTranslationUnit,
1592                                              bool CacheCodeCompletionResults) {  
1593   // Create the AST unit.
1594   llvm::OwningPtr<ASTUnit> AST;
1595   AST.reset(new ASTUnit(false));
1596   ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1597   AST->Diagnostics = Diags;
1598   AST->OnlyLocalDecls = OnlyLocalDecls;
1599   AST->CaptureDiagnostics = CaptureDiagnostics;
1600   AST->CompleteTranslationUnit = CompleteTranslationUnit;
1601   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1602   AST->Invocation = CI;
1603   
1604   // Recover resources if we crash before exiting this method.
1605   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1606     ASTUnitCleanup(AST.get());
1607   llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1608     llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1609     DiagCleanup(Diags.getPtr());
1610
1611   return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1612 }
1613
1614 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1615                                       const char **ArgEnd,
1616                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1617                                       llvm::StringRef ResourceFilesPath,
1618                                       bool OnlyLocalDecls,
1619                                       bool CaptureDiagnostics,
1620                                       RemappedFile *RemappedFiles,
1621                                       unsigned NumRemappedFiles,
1622                                       bool RemappedFilesKeepOriginalName,
1623                                       bool PrecompilePreamble,
1624                                       bool CompleteTranslationUnit,
1625                                       bool CacheCodeCompletionResults,
1626                                       bool CXXPrecompilePreamble,
1627                                       bool CXXChainedPCH) {
1628   if (!Diags.getPtr()) {
1629     // No diagnostics engine was provided, so create our own diagnostics object
1630     // with the default options.
1631     DiagnosticOptions DiagOpts;
1632     Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin, 
1633                                                 ArgBegin);
1634   }
1635
1636   llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1637   
1638   llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1639
1640   {
1641     CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, 
1642                                       StoredDiagnostics);
1643
1644     CI = clang::createInvocationFromCommandLine(
1645                         llvm::ArrayRef<const char *>(ArgBegin, ArgEnd-ArgBegin),
1646                         Diags);
1647     if (!CI)
1648       return 0;
1649   }
1650
1651   // Override any files that need remapping
1652   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1653     FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1654     if (const llvm::MemoryBuffer *
1655             memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1656       CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1657     } else {
1658       const char *fname = fileOrBuf.get<const char *>();
1659       CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1660     }
1661   }
1662   CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1663                                                   RemappedFilesKeepOriginalName;
1664   
1665   // Override the resources path.
1666   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1667
1668   // Check whether we should precompile the preamble and/or use chained PCH.
1669   // FIXME: This is a temporary hack while we debug C++ chained PCH.
1670   if (CI->getLangOpts().CPlusPlus) {
1671     PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1672     
1673     if (PrecompilePreamble && !CXXChainedPCH &&
1674         !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1675       PrecompilePreamble = false;
1676   }
1677   
1678   // Create the AST unit.
1679   llvm::OwningPtr<ASTUnit> AST;
1680   AST.reset(new ASTUnit(false));
1681   ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1682   AST->Diagnostics = Diags;
1683
1684   AST->FileSystemOpts = CI->getFileSystemOpts();
1685   AST->FileMgr = new FileManager(AST->FileSystemOpts);
1686   AST->OnlyLocalDecls = OnlyLocalDecls;
1687   AST->CaptureDiagnostics = CaptureDiagnostics;
1688   AST->CompleteTranslationUnit = CompleteTranslationUnit;
1689   AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1690   AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1691   AST->NumStoredDiagnosticsInPreamble = StoredDiagnostics.size();
1692   AST->StoredDiagnostics.swap(StoredDiagnostics);
1693   AST->Invocation = CI;
1694   
1695   // Recover resources if we crash before exiting this method.
1696   llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1697     ASTUnitCleanup(AST.get());
1698   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1699     llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1700     CICleanup(CI.getPtr());
1701   llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1702     llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1703     DiagCleanup(Diags.getPtr());
1704
1705   return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1706 }
1707
1708 bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1709   if (!Invocation)
1710     return true;
1711   
1712   SimpleTimer ParsingTimer(WantTiming);
1713   ParsingTimer.setOutput("Reparsing " + getMainFileName());
1714
1715   // Remap files.
1716   PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1717   PPOpts.DisableStatCache = true;
1718   for (PreprocessorOptions::remapped_file_buffer_iterator 
1719          R = PPOpts.remapped_file_buffer_begin(),
1720          REnd = PPOpts.remapped_file_buffer_end();
1721        R != REnd; 
1722        ++R) {
1723     delete R->second;
1724   }
1725   Invocation->getPreprocessorOpts().clearRemappedFiles();
1726   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1727     FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1728     if (const llvm::MemoryBuffer *
1729             memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1730       Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1731                                                         memBuf);
1732     } else {
1733       const char *fname = fileOrBuf.get<const char *>();
1734       Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1735                                                         fname);
1736     }
1737   }
1738   
1739   // If we have a preamble file lying around, or if we might try to
1740   // build a precompiled preamble, do so now.
1741   llvm::MemoryBuffer *OverrideMainBuffer = 0;
1742   if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1743     OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1744     
1745   // Clear out the diagnostics state.
1746   if (!OverrideMainBuffer) {
1747     getDiagnostics().Reset();
1748     ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1749   }
1750   
1751   // Parse the sources
1752   bool Result = Parse(OverrideMainBuffer);
1753   
1754   // If we're caching global code-completion results, and the top-level 
1755   // declarations have changed, clear out the code-completion cache.
1756   if (!Result && ShouldCacheCodeCompletionResults &&
1757       CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1758     CacheCodeCompletionResults();
1759
1760   return Result;
1761 }
1762
1763 //----------------------------------------------------------------------------//
1764 // Code completion
1765 //----------------------------------------------------------------------------//
1766
1767 namespace {
1768   /// \brief Code completion consumer that combines the cached code-completion
1769   /// results from an ASTUnit with the code-completion results provided to it,
1770   /// then passes the result on to 
1771   class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1772     unsigned NormalContexts;
1773     ASTUnit &AST;
1774     CodeCompleteConsumer &Next;
1775     
1776   public:
1777     AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1778                                   bool IncludeMacros, bool IncludeCodePatterns,
1779                                   bool IncludeGlobals)
1780       : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1781                              Next.isOutputBinary()), AST(AST), Next(Next) 
1782     { 
1783       // Compute the set of contexts in which we will look when we don't have
1784       // any information about the specific context.
1785       NormalContexts 
1786         = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
1787         | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
1788         | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1789         | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1790         | (1 << (CodeCompletionContext::CCC_Statement - 1))
1791         | (1 << (CodeCompletionContext::CCC_Expression - 1))
1792         | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1793         | (1 << (CodeCompletionContext::CCC_MemberAccess - 1))
1794         | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1795         | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1796         | (1 << (CodeCompletionContext::CCC_Recovery - 1));
1797
1798       if (AST.getASTContext().getLangOptions().CPlusPlus)
1799         NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1))
1800                     | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
1801                     | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1802     }
1803     
1804     virtual void ProcessCodeCompleteResults(Sema &S, 
1805                                             CodeCompletionContext Context,
1806                                             CodeCompletionResult *Results,
1807                                             unsigned NumResults);
1808     
1809     virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1810                                            OverloadCandidate *Candidates,
1811                                            unsigned NumCandidates) { 
1812       Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1813     }
1814     
1815     virtual CodeCompletionAllocator &getAllocator() {
1816       return Next.getAllocator();
1817     }
1818   };
1819 }
1820
1821 /// \brief Helper function that computes which global names are hidden by the
1822 /// local code-completion results.
1823 static void CalculateHiddenNames(const CodeCompletionContext &Context,
1824                                  CodeCompletionResult *Results,
1825                                  unsigned NumResults,
1826                                  ASTContext &Ctx,
1827                           llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1828   bool OnlyTagNames = false;
1829   switch (Context.getKind()) {
1830   case CodeCompletionContext::CCC_Recovery:
1831   case CodeCompletionContext::CCC_TopLevel:
1832   case CodeCompletionContext::CCC_ObjCInterface:
1833   case CodeCompletionContext::CCC_ObjCImplementation:
1834   case CodeCompletionContext::CCC_ObjCIvarList:
1835   case CodeCompletionContext::CCC_ClassStructUnion:
1836   case CodeCompletionContext::CCC_Statement:
1837   case CodeCompletionContext::CCC_Expression:
1838   case CodeCompletionContext::CCC_ObjCMessageReceiver:
1839   case CodeCompletionContext::CCC_MemberAccess:
1840   case CodeCompletionContext::CCC_Namespace:
1841   case CodeCompletionContext::CCC_Type:
1842   case CodeCompletionContext::CCC_Name:
1843   case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1844   case CodeCompletionContext::CCC_ParenthesizedExpression:
1845     break;
1846     
1847   case CodeCompletionContext::CCC_EnumTag:
1848   case CodeCompletionContext::CCC_UnionTag:
1849   case CodeCompletionContext::CCC_ClassOrStructTag:
1850     OnlyTagNames = true;
1851     break;
1852     
1853   case CodeCompletionContext::CCC_ObjCProtocolName:
1854   case CodeCompletionContext::CCC_MacroName:
1855   case CodeCompletionContext::CCC_MacroNameUse:
1856   case CodeCompletionContext::CCC_PreprocessorExpression:
1857   case CodeCompletionContext::CCC_PreprocessorDirective:
1858   case CodeCompletionContext::CCC_NaturalLanguage:
1859   case CodeCompletionContext::CCC_SelectorName:
1860   case CodeCompletionContext::CCC_TypeQualifiers:
1861   case CodeCompletionContext::CCC_Other:
1862   case CodeCompletionContext::CCC_OtherWithMacros:
1863     // We're looking for nothing, or we're looking for names that cannot
1864     // be hidden.
1865     return;
1866   }
1867   
1868   typedef CodeCompletionResult Result;
1869   for (unsigned I = 0; I != NumResults; ++I) {
1870     if (Results[I].Kind != Result::RK_Declaration)
1871       continue;
1872     
1873     unsigned IDNS
1874       = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
1875
1876     bool Hiding = false;
1877     if (OnlyTagNames)
1878       Hiding = (IDNS & Decl::IDNS_Tag);
1879     else {
1880       unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | 
1881                              Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
1882                              Decl::IDNS_NonMemberOperator);
1883       if (Ctx.getLangOptions().CPlusPlus)
1884         HiddenIDNS |= Decl::IDNS_Tag;
1885       Hiding = (IDNS & HiddenIDNS);
1886     }
1887   
1888     if (!Hiding)
1889       continue;
1890     
1891     DeclarationName Name = Results[I].Declaration->getDeclName();
1892     if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
1893       HiddenNames.insert(Identifier->getName());
1894     else
1895       HiddenNames.insert(Name.getAsString());
1896   }
1897 }
1898
1899
1900 void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
1901                                             CodeCompletionContext Context,
1902                                             CodeCompletionResult *Results,
1903                                             unsigned NumResults) { 
1904   // Merge the results we were given with the results we cached.
1905   bool AddedResult = false;
1906   unsigned InContexts  
1907     = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
1908                                             : (1 << (Context.getKind() - 1)));
1909
1910   // Contains the set of names that are hidden by "local" completion results.
1911   llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
1912   typedef CodeCompletionResult Result;
1913   llvm::SmallVector<Result, 8> AllResults;
1914   for (ASTUnit::cached_completion_iterator 
1915             C = AST.cached_completion_begin(),
1916          CEnd = AST.cached_completion_end();
1917        C != CEnd; ++C) {
1918     // If the context we are in matches any of the contexts we are 
1919     // interested in, we'll add this result.
1920     if ((C->ShowInContexts & InContexts) == 0)
1921       continue;
1922     
1923     // If we haven't added any results previously, do so now.
1924     if (!AddedResult) {
1925       CalculateHiddenNames(Context, Results, NumResults, S.Context, 
1926                            HiddenNames);
1927       AllResults.insert(AllResults.end(), Results, Results + NumResults);
1928       AddedResult = true;
1929     }
1930     
1931     // Determine whether this global completion result is hidden by a local
1932     // completion result. If so, skip it.
1933     if (C->Kind != CXCursor_MacroDefinition &&
1934         HiddenNames.count(C->Completion->getTypedText()))
1935       continue;
1936     
1937     // Adjust priority based on similar type classes.
1938     unsigned Priority = C->Priority;
1939     CXCursorKind CursorKind = C->Kind;
1940     CodeCompletionString *Completion = C->Completion;
1941     if (!Context.getPreferredType().isNull()) {
1942       if (C->Kind == CXCursor_MacroDefinition) {
1943         Priority = getMacroUsagePriority(C->Completion->getTypedText(),
1944                                          S.getLangOptions(),
1945                                Context.getPreferredType()->isAnyPointerType());        
1946       } else if (C->Type) {
1947         CanQualType Expected
1948           = S.Context.getCanonicalType(
1949                                Context.getPreferredType().getUnqualifiedType());
1950         SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
1951         if (ExpectedSTC == C->TypeClass) {
1952           // We know this type is similar; check for an exact match.
1953           llvm::StringMap<unsigned> &CachedCompletionTypes
1954             = AST.getCachedCompletionTypes();
1955           llvm::StringMap<unsigned>::iterator Pos
1956             = CachedCompletionTypes.find(QualType(Expected).getAsString());
1957           if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
1958             Priority /= CCF_ExactTypeMatch;
1959           else
1960             Priority /= CCF_SimilarTypeMatch;
1961         }
1962       }
1963     }
1964     
1965     // Adjust the completion string, if required.
1966     if (C->Kind == CXCursor_MacroDefinition &&
1967         Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
1968       // Create a new code-completion string that just contains the
1969       // macro name, without its arguments.
1970       CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
1971                                     C->Availability);
1972       Builder.AddTypedTextChunk(C->Completion->getTypedText());
1973       CursorKind = CXCursor_NotImplemented;
1974       Priority = CCP_CodePattern;
1975       Completion = Builder.TakeString();
1976     }
1977     
1978     AllResults.push_back(Result(Completion, Priority, CursorKind, 
1979                                 C->Availability));
1980   }
1981   
1982   // If we did not add any cached completion results, just forward the
1983   // results we were given to the next consumer.
1984   if (!AddedResult) {
1985     Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
1986     return;
1987   }
1988   
1989   Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
1990                                   AllResults.size());
1991 }
1992
1993
1994
1995 void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
1996                            RemappedFile *RemappedFiles, 
1997                            unsigned NumRemappedFiles,
1998                            bool IncludeMacros, 
1999                            bool IncludeCodePatterns,
2000                            CodeCompleteConsumer &Consumer,
2001                            Diagnostic &Diag, LangOptions &LangOpts,
2002                            SourceManager &SourceMgr, FileManager &FileMgr,
2003                    llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2004              llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2005   if (!Invocation)
2006     return;
2007
2008   SimpleTimer CompletionTimer(WantTiming);
2009   CompletionTimer.setOutput("Code completion @ " + File + ":" +
2010                             llvm::Twine(Line) + ":" + llvm::Twine(Column));
2011
2012   llvm::IntrusiveRefCntPtr<CompilerInvocation>
2013     CCInvocation(new CompilerInvocation(*Invocation));
2014
2015   FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2016   PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2017
2018   FrontendOpts.ShowMacrosInCodeCompletion
2019     = IncludeMacros && CachedCompletionResults.empty();
2020   FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2021   FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2022     = CachedCompletionResults.empty();
2023   FrontendOpts.CodeCompletionAt.FileName = File;
2024   FrontendOpts.CodeCompletionAt.Line = Line;
2025   FrontendOpts.CodeCompletionAt.Column = Column;
2026
2027   // Set the language options appropriately.
2028   LangOpts = CCInvocation->getLangOpts();
2029
2030   llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2031
2032   // Recover resources if we crash before exiting this method.
2033   llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2034     CICleanup(Clang.get());
2035
2036   Clang->setInvocation(&*CCInvocation);
2037   OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2038     
2039   // Set up diagnostics, capturing any diagnostics produced.
2040   Clang->setDiagnostics(&Diag);
2041   ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2042   CaptureDroppedDiagnostics Capture(true, 
2043                                     Clang->getDiagnostics(), 
2044                                     StoredDiagnostics);
2045   
2046   // Create the target instance.
2047   Clang->getTargetOpts().Features = TargetFeatures;
2048   Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2049                                                Clang->getTargetOpts()));
2050   if (!Clang->hasTarget()) {
2051     Clang->setInvocation(0);
2052     return;
2053   }
2054   
2055   // Inform the target of the language options.
2056   //
2057   // FIXME: We shouldn't need to do this, the target should be immutable once
2058   // created. This complexity should be lifted elsewhere.
2059   Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2060   
2061   assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2062          "Invocation must have exactly one source file!");
2063   assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2064          "FIXME: AST inputs not yet supported here!");
2065   assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2066          "IR inputs not support here!");
2067
2068   
2069   // Use the source and file managers that we were given.
2070   Clang->setFileManager(&FileMgr);
2071   Clang->setSourceManager(&SourceMgr);
2072
2073   // Remap files.
2074   PreprocessorOpts.clearRemappedFiles();
2075   PreprocessorOpts.RetainRemappedFileBuffers = true;
2076   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2077     FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2078     if (const llvm::MemoryBuffer *
2079             memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2080       PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2081       OwnedBuffers.push_back(memBuf);
2082     } else {
2083       const char *fname = fileOrBuf.get<const char *>();
2084       PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2085     }
2086   }
2087   
2088   // Use the code completion consumer we were given, but adding any cached
2089   // code-completion results.
2090   AugmentedCodeCompleteConsumer *AugmentedConsumer
2091     = new AugmentedCodeCompleteConsumer(*this, Consumer, 
2092                                         FrontendOpts.ShowMacrosInCodeCompletion,
2093                                 FrontendOpts.ShowCodePatternsInCodeCompletion,
2094                                 FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2095   Clang->setCodeCompletionConsumer(AugmentedConsumer);
2096
2097   // If we have a precompiled preamble, try to use it. We only allow
2098   // the use of the precompiled preamble if we're if the completion
2099   // point is within the main file, after the end of the precompiled
2100   // preamble.
2101   llvm::MemoryBuffer *OverrideMainBuffer = 0;
2102   if (!PreambleFile.empty()) {
2103     using llvm::sys::FileStatus;
2104     llvm::sys::PathWithStatus CompleteFilePath(File);
2105     llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2106     if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2107       if (const FileStatus *MainStatus = MainPath.getFileStatus())
2108         if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2109           OverrideMainBuffer
2110             = getMainBufferWithPrecompiledPreamble(*CCInvocation, false, 
2111                                                    Line - 1);
2112   }
2113
2114   // If the main file has been overridden due to the use of a preamble,
2115   // make that override happen and introduce the preamble.
2116   PreprocessorOpts.DisableStatCache = true;
2117   StoredDiagnostics.insert(StoredDiagnostics.end(),
2118                            this->StoredDiagnostics.begin(),
2119              this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2120   if (OverrideMainBuffer) {
2121     PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2122     PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2123     PreprocessorOpts.PrecompiledPreambleBytes.second
2124                                                     = PreambleEndsAtStartOfLine;
2125     PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2126     PreprocessorOpts.DisablePCHValidation = true;
2127     
2128     // The stored diagnostics have the old source manager. Copy them
2129     // to our output set of stored diagnostics, updating the source
2130     // manager to the one we were given.
2131     for (unsigned I = NumStoredDiagnosticsFromDriver, 
2132                   N = this->StoredDiagnostics.size(); 
2133          I < N; ++I) {
2134       StoredDiagnostics.push_back(this->StoredDiagnostics[I]);
2135       FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr);
2136       StoredDiagnostics[I].setLocation(Loc);
2137     }
2138
2139     OwnedBuffers.push_back(OverrideMainBuffer);
2140   } else {
2141     PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2142     PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2143   }
2144
2145   llvm::OwningPtr<SyntaxOnlyAction> Act;
2146   Act.reset(new SyntaxOnlyAction);
2147   if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2148                            Clang->getFrontendOpts().Inputs[0].first)) {
2149     Act->Execute();
2150     Act->EndSourceFile();
2151   }
2152 }
2153
2154 bool ASTUnit::Save(llvm::StringRef File) {
2155   if (getDiagnostics().hasErrorOccurred())
2156     return true;
2157   
2158   // FIXME: Can we somehow regenerate the stat cache here, or do we need to 
2159   // unconditionally create a stat cache when we parse the file?
2160   std::string ErrorInfo;
2161   llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo,
2162                            llvm::raw_fd_ostream::F_Binary);
2163   if (!ErrorInfo.empty() || Out.has_error())
2164     return true;
2165
2166   serialize(Out);
2167   Out.close();
2168   return Out.has_error();
2169 }
2170
2171 bool ASTUnit::serialize(llvm::raw_ostream &OS) {
2172   if (getDiagnostics().hasErrorOccurred())
2173     return true;
2174
2175   std::vector<unsigned char> Buffer;
2176   llvm::BitstreamWriter Stream(Buffer);
2177   ASTWriter Writer(Stream);
2178   Writer.WriteAST(getSema(), 0, std::string(), 0);
2179   
2180   // Write the generated bitstream to "Out".
2181   if (!Buffer.empty())
2182     OS.write((char *)&Buffer.front(), Buffer.size());
2183
2184   return false;
2185 }