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