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