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