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