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