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