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