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