ASTUnit.cpp revision 51f564f80d9f71e175635b452ffeeeff899e9bf1
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/ArgList.h"
24#include "clang/Driver/Options.h"
25#include "clang/Driver/Tool.h"
26#include "clang/Frontend/CompilerInstance.h"
27#include "clang/Frontend/FrontendActions.h"
28#include "clang/Frontend/FrontendDiagnostic.h"
29#include "clang/Frontend/FrontendOptions.h"
30#include "clang/Frontend/MultiplexConsumer.h"
31#include "clang/Frontend/Utils.h"
32#include "clang/Serialization/ASTReader.h"
33#include "clang/Serialization/ASTWriter.h"
34#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/Preprocessor.h"
36#include "clang/Basic/TargetOptions.h"
37#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/Diagnostic.h"
39#include "llvm/ADT/ArrayRef.h"
40#include "llvm/ADT/StringExtras.h"
41#include "llvm/ADT/StringSet.h"
42#include "llvm/Support/Atomic.h"
43#include "llvm/Support/MemoryBuffer.h"
44#include "llvm/Support/Host.h"
45#include "llvm/Support/Path.h"
46#include "llvm/Support/raw_ostream.h"
47#include "llvm/Support/Timer.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/Mutex.h"
50#include "llvm/Support/MutexGuard.h"
51#include "llvm/Support/CrashRecoveryContext.h"
52#include <cstdlib>
53#include <cstdio>
54#include <sys/stat.h>
55using namespace clang;
56
57using llvm::TimeRecord;
58
59namespace {
60  class SimpleTimer {
61    bool WantTiming;
62    TimeRecord Start;
63    std::string Output;
64
65  public:
66    explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) {
67      if (WantTiming)
68        Start = TimeRecord::getCurrentTime();
69    }
70
71    void setOutput(const Twine &Output) {
72      if (WantTiming)
73        this->Output = Output.str();
74    }
75
76    ~SimpleTimer() {
77      if (WantTiming) {
78        TimeRecord Elapsed = TimeRecord::getCurrentTime();
79        Elapsed -= Start;
80        llvm::errs() << Output << ':';
81        Elapsed.print(Elapsed, llvm::errs());
82        llvm::errs() << '\n';
83      }
84    }
85  };
86
87  struct OnDiskData {
88    /// \brief The file in which the precompiled preamble is stored.
89    std::string PreambleFile;
90
91    /// \brief Temporary files that should be removed when the ASTUnit is
92    /// destroyed.
93    SmallVector<llvm::sys::Path, 4> TemporaryFiles;
94
95    /// \brief Erase temporary files.
96    void CleanTemporaryFiles();
97
98    /// \brief Erase the preamble file.
99    void CleanPreambleFile();
100
101    /// \brief Erase temporary files and the preamble file.
102    void Cleanup();
103  };
104}
105
106static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
107  static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
108  return M;
109}
110
111static void cleanupOnDiskMapAtExit(void);
112
113typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap;
114static OnDiskDataMap &getOnDiskDataMap() {
115  static OnDiskDataMap M;
116  static bool hasRegisteredAtExit = false;
117  if (!hasRegisteredAtExit) {
118    hasRegisteredAtExit = true;
119    atexit(cleanupOnDiskMapAtExit);
120  }
121  return M;
122}
123
124static void cleanupOnDiskMapAtExit(void) {
125  // No mutex required here since we are leaving the program.
126  OnDiskDataMap &M = getOnDiskDataMap();
127  for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) {
128    // We don't worry about freeing the memory associated with OnDiskDataMap.
129    // All we care about is erasing stale files.
130    I->second->Cleanup();
131  }
132}
133
134static OnDiskData &getOnDiskData(const ASTUnit *AU) {
135  // We require the mutex since we are modifying the structure of the
136  // DenseMap.
137  llvm::MutexGuard Guard(getOnDiskMutex());
138  OnDiskDataMap &M = getOnDiskDataMap();
139  OnDiskData *&D = M[AU];
140  if (!D)
141    D = new OnDiskData();
142  return *D;
143}
144
145static void erasePreambleFile(const ASTUnit *AU) {
146  getOnDiskData(AU).CleanPreambleFile();
147}
148
149static void removeOnDiskEntry(const ASTUnit *AU) {
150  // We require the mutex since we are modifying the structure of the
151  // DenseMap.
152  llvm::MutexGuard Guard(getOnDiskMutex());
153  OnDiskDataMap &M = getOnDiskDataMap();
154  OnDiskDataMap::iterator I = M.find(AU);
155  if (I != M.end()) {
156    I->second->Cleanup();
157    delete I->second;
158    M.erase(AU);
159  }
160}
161
162static void setPreambleFile(const ASTUnit *AU, llvm::StringRef preambleFile) {
163  getOnDiskData(AU).PreambleFile = preambleFile;
164}
165
166static const std::string &getPreambleFile(const ASTUnit *AU) {
167  return getOnDiskData(AU).PreambleFile;
168}
169
170void OnDiskData::CleanTemporaryFiles() {
171  for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
172    TemporaryFiles[I].eraseFromDisk();
173  TemporaryFiles.clear();
174}
175
176void OnDiskData::CleanPreambleFile() {
177  if (!PreambleFile.empty()) {
178    llvm::sys::Path(PreambleFile).eraseFromDisk();
179    PreambleFile.clear();
180  }
181}
182
183void OnDiskData::Cleanup() {
184  CleanTemporaryFiles();
185  CleanPreambleFile();
186}
187
188void ASTUnit::clearFileLevelDecls() {
189  for (FileDeclsTy::iterator
190         I = FileDecls.begin(), E = FileDecls.end(); I != E; ++I)
191    delete I->second;
192  FileDecls.clear();
193}
194
195void ASTUnit::CleanTemporaryFiles() {
196  getOnDiskData(this).CleanTemporaryFiles();
197}
198
199void ASTUnit::addTemporaryFile(const llvm::sys::Path &TempFile) {
200  getOnDiskData(this).TemporaryFiles.push_back(TempFile);
201}
202
203/// \brief After failing to build a precompiled preamble (due to
204/// errors in the source that occurs in the preamble), the number of
205/// reparses during which we'll skip even trying to precompile the
206/// preamble.
207const unsigned DefaultPreambleRebuildInterval = 5;
208
209/// \brief Tracks the number of ASTUnit objects that are currently active.
210///
211/// Used for debugging purposes only.
212static llvm::sys::cas_flag ActiveASTUnitObjects;
213
214ASTUnit::ASTUnit(bool _MainFileIsAST)
215  : Reader(0), OnlyLocalDecls(false), CaptureDiagnostics(false),
216    MainFileIsAST(_MainFileIsAST),
217    TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
218    OwnsRemappedFileBuffers(true),
219    NumStoredDiagnosticsFromDriver(0),
220    PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0),
221    NumWarningsInPreamble(0),
222    ShouldCacheCodeCompletionResults(false),
223    NestedMacroExpansions(true),
224    CompletionCacheTopLevelHashValue(0),
225    PreambleTopLevelHashValue(0),
226    CurrentTopLevelHashValue(0),
227    UnsafeToFree(false) {
228  if (getenv("LIBCLANG_OBJTRACKING")) {
229    llvm::sys::AtomicIncrement(&ActiveASTUnitObjects);
230    fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects);
231  }
232}
233
234ASTUnit::~ASTUnit() {
235  clearFileLevelDecls();
236
237  // Clean up the temporary files and the preamble file.
238  removeOnDiskEntry(this);
239
240  // Free the buffers associated with remapped files. We are required to
241  // perform this operation here because we explicitly request that the
242  // compiler instance *not* free these buffers for each invocation of the
243  // parser.
244  if (Invocation.getPtr() && OwnsRemappedFileBuffers) {
245    PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
246    for (PreprocessorOptions::remapped_file_buffer_iterator
247           FB = PPOpts.remapped_file_buffer_begin(),
248           FBEnd = PPOpts.remapped_file_buffer_end();
249         FB != FBEnd;
250         ++FB)
251      delete FB->second;
252  }
253
254  delete SavedMainFileBuffer;
255  delete PreambleBuffer;
256
257  ClearCachedCompletionResults();
258
259  if (getenv("LIBCLANG_OBJTRACKING")) {
260    llvm::sys::AtomicDecrement(&ActiveASTUnitObjects);
261    fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects);
262  }
263}
264
265/// \brief Determine the set of code-completion contexts in which this
266/// declaration should be shown.
267static unsigned getDeclShowContexts(NamedDecl *ND,
268                                    const LangOptions &LangOpts,
269                                    bool &IsNestedNameSpecifier) {
270  IsNestedNameSpecifier = false;
271
272  if (isa<UsingShadowDecl>(ND))
273    ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl());
274  if (!ND)
275    return 0;
276
277  unsigned Contexts = 0;
278  if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) ||
279      isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) {
280    // Types can appear in these contexts.
281    if (LangOpts.CPlusPlus || !isa<TagDecl>(ND))
282      Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1))
283                | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
284                | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
285                | (1 << (CodeCompletionContext::CCC_Statement - 1))
286                | (1 << (CodeCompletionContext::CCC_Type - 1))
287              | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
288
289    // In C++, types can appear in expressions contexts (for functional casts).
290    if (LangOpts.CPlusPlus)
291      Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1));
292
293    // In Objective-C, message sends can send interfaces. In Objective-C++,
294    // all types are available due to functional casts.
295    if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND))
296      Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
297
298    // In Objective-C, you can only be a subclass of another Objective-C class
299    if (isa<ObjCInterfaceDecl>(ND))
300      Contexts |= (1 << (CodeCompletionContext::CCC_ObjCInterfaceName - 1));
301
302    // Deal with tag names.
303    if (isa<EnumDecl>(ND)) {
304      Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1));
305
306      // Part of the nested-name-specifier in C++0x.
307      if (LangOpts.CPlusPlus0x)
308        IsNestedNameSpecifier = true;
309    } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) {
310      if (Record->isUnion())
311        Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1));
312      else
313        Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
314
315      if (LangOpts.CPlusPlus)
316        IsNestedNameSpecifier = true;
317    } else if (isa<ClassTemplateDecl>(ND))
318      IsNestedNameSpecifier = true;
319  } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) {
320    // Values can appear in these contexts.
321    Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1))
322             | (1 << (CodeCompletionContext::CCC_Expression - 1))
323             | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
324             | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1));
325  } else if (isa<ObjCProtocolDecl>(ND)) {
326    Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1));
327  } else if (isa<ObjCCategoryDecl>(ND)) {
328    Contexts = (1 << (CodeCompletionContext::CCC_ObjCCategoryName - 1));
329  } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) {
330    Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1));
331
332    // Part of the nested-name-specifier.
333    IsNestedNameSpecifier = true;
334  }
335
336  return Contexts;
337}
338
339void ASTUnit::CacheCodeCompletionResults() {
340  if (!TheSema)
341    return;
342
343  SimpleTimer Timer(WantTiming);
344  Timer.setOutput("Cache global code completions for " + getMainFileName());
345
346  // Clear out the previous results.
347  ClearCachedCompletionResults();
348
349  // Gather the set of global code completions.
350  typedef CodeCompletionResult Result;
351  SmallVector<Result, 8> Results;
352  CachedCompletionAllocator = new GlobalCodeCompletionAllocator;
353  TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results);
354
355  // Translate global code completions into cached completions.
356  llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
357
358  for (unsigned I = 0, N = Results.size(); I != N; ++I) {
359    switch (Results[I].Kind) {
360    case Result::RK_Declaration: {
361      bool IsNestedNameSpecifier = false;
362      CachedCodeCompletionResult CachedResult;
363      CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
364                                                    *CachedCompletionAllocator);
365      CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
366                                                        Ctx->getLangOptions(),
367                                                        IsNestedNameSpecifier);
368      CachedResult.Priority = Results[I].Priority;
369      CachedResult.Kind = Results[I].CursorKind;
370      CachedResult.Availability = Results[I].Availability;
371
372      // Keep track of the type of this completion in an ASTContext-agnostic
373      // way.
374      QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
375      if (UsageType.isNull()) {
376        CachedResult.TypeClass = STC_Void;
377        CachedResult.Type = 0;
378      } else {
379        CanQualType CanUsageType
380          = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
381        CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
382
383        // Determine whether we have already seen this type. If so, we save
384        // ourselves the work of formatting the type string by using the
385        // temporary, CanQualType-based hash table to find the associated value.
386        unsigned &TypeValue = CompletionTypes[CanUsageType];
387        if (TypeValue == 0) {
388          TypeValue = CompletionTypes.size();
389          CachedCompletionTypes[QualType(CanUsageType).getAsString()]
390            = TypeValue;
391        }
392
393        CachedResult.Type = TypeValue;
394      }
395
396      CachedCompletionResults.push_back(CachedResult);
397
398      /// Handle nested-name-specifiers in C++.
399      if (TheSema->Context.getLangOptions().CPlusPlus &&
400          IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
401        // The contexts in which a nested-name-specifier can appear in C++.
402        unsigned NNSContexts
403          = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
404          | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
405          | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
406          | (1 << (CodeCompletionContext::CCC_Statement - 1))
407          | (1 << (CodeCompletionContext::CCC_Expression - 1))
408          | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
409          | (1 << (CodeCompletionContext::CCC_EnumTag - 1))
410          | (1 << (CodeCompletionContext::CCC_UnionTag - 1))
411          | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1))
412          | (1 << (CodeCompletionContext::CCC_Type - 1))
413          | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1))
414          | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
415
416        if (isa<NamespaceDecl>(Results[I].Declaration) ||
417            isa<NamespaceAliasDecl>(Results[I].Declaration))
418          NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1));
419
420        if (unsigned RemainingContexts
421                                = NNSContexts & ~CachedResult.ShowInContexts) {
422          // If there any contexts where this completion can be a
423          // nested-name-specifier but isn't already an option, create a
424          // nested-name-specifier completion.
425          Results[I].StartsNestedNameSpecifier = true;
426          CachedResult.Completion
427            = Results[I].CreateCodeCompletionString(*TheSema,
428                                                    *CachedCompletionAllocator);
429          CachedResult.ShowInContexts = RemainingContexts;
430          CachedResult.Priority = CCP_NestedNameSpecifier;
431          CachedResult.TypeClass = STC_Void;
432          CachedResult.Type = 0;
433          CachedCompletionResults.push_back(CachedResult);
434        }
435      }
436      break;
437    }
438
439    case Result::RK_Keyword:
440    case Result::RK_Pattern:
441      // Ignore keywords and patterns; we don't care, since they are so
442      // easily regenerated.
443      break;
444
445    case Result::RK_Macro: {
446      CachedCodeCompletionResult CachedResult;
447      CachedResult.Completion
448        = Results[I].CreateCodeCompletionString(*TheSema,
449                                                *CachedCompletionAllocator);
450      CachedResult.ShowInContexts
451        = (1 << (CodeCompletionContext::CCC_TopLevel - 1))
452        | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1))
453        | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1))
454        | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1))
455        | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1))
456        | (1 << (CodeCompletionContext::CCC_Statement - 1))
457        | (1 << (CodeCompletionContext::CCC_Expression - 1))
458        | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
459        | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
460        | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
461        | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
462        | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
463
464      CachedResult.Priority = Results[I].Priority;
465      CachedResult.Kind = Results[I].CursorKind;
466      CachedResult.Availability = Results[I].Availability;
467      CachedResult.TypeClass = STC_Void;
468      CachedResult.Type = 0;
469      CachedCompletionResults.push_back(CachedResult);
470      break;
471    }
472    }
473  }
474
475  // Save the current top-level hash value.
476  CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
477}
478
479void ASTUnit::ClearCachedCompletionResults() {
480  CachedCompletionResults.clear();
481  CachedCompletionTypes.clear();
482  CachedCompletionAllocator = 0;
483}
484
485namespace {
486
487/// \brief Gathers information from ASTReader that will be used to initialize
488/// a Preprocessor.
489class ASTInfoCollector : public ASTReaderListener {
490  Preprocessor &PP;
491  ASTContext &Context;
492  LangOptions &LangOpt;
493  HeaderSearch &HSI;
494  llvm::IntrusiveRefCntPtr<TargetInfo> &Target;
495  std::string &Predefines;
496  unsigned &Counter;
497
498  unsigned NumHeaderInfos;
499
500  bool InitializedLanguage;
501public:
502  ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
503                   HeaderSearch &HSI,
504                   llvm::IntrusiveRefCntPtr<TargetInfo> &Target,
505                   std::string &Predefines,
506                   unsigned &Counter)
507    : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI), Target(Target),
508      Predefines(Predefines), Counter(Counter), NumHeaderInfos(0),
509      InitializedLanguage(false) {}
510
511  virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
512    if (InitializedLanguage)
513      return false;
514
515    LangOpt = LangOpts;
516
517    // Initialize the preprocessor.
518    PP.Initialize(*Target);
519
520    // Initialize the ASTContext
521    Context.InitBuiltinTypes(*Target);
522
523    InitializedLanguage = true;
524    return false;
525  }
526
527  virtual bool ReadTargetTriple(StringRef Triple) {
528    // If we've already initialized the target, don't do it again.
529    if (Target)
530      return false;
531
532    // FIXME: This is broken, we should store the TargetOptions in the AST file.
533    TargetOptions TargetOpts;
534    TargetOpts.ABI = "";
535    TargetOpts.CXXABI = "";
536    TargetOpts.CPU = "";
537    TargetOpts.Features.clear();
538    TargetOpts.Triple = Triple;
539    Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(), TargetOpts);
540    return false;
541  }
542
543  virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
544                                    StringRef OriginalFileName,
545                                    std::string &SuggestedPredefines,
546                                    FileManager &FileMgr) {
547    Predefines = Buffers[0].Data;
548    for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
549      Predefines += Buffers[I].Data;
550    }
551    return false;
552  }
553
554  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
555    HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
556  }
557
558  virtual void ReadCounter(unsigned Value) {
559    Counter = Value;
560  }
561};
562
563class StoredDiagnosticConsumer : public DiagnosticConsumer {
564  SmallVectorImpl<StoredDiagnostic> &StoredDiags;
565
566public:
567  explicit StoredDiagnosticConsumer(
568                          SmallVectorImpl<StoredDiagnostic> &StoredDiags)
569    : StoredDiags(StoredDiags) { }
570
571  virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
572                                const Diagnostic &Info);
573
574  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
575    // Just drop any diagnostics that come from cloned consumers; they'll
576    // have different source managers anyway.
577    return new IgnoringDiagConsumer();
578  }
579};
580
581/// \brief RAII object that optionally captures diagnostics, if
582/// there is no diagnostic client to capture them already.
583class CaptureDroppedDiagnostics {
584  DiagnosticsEngine &Diags;
585  StoredDiagnosticConsumer Client;
586  DiagnosticConsumer *PreviousClient;
587
588public:
589  CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
590                          SmallVectorImpl<StoredDiagnostic> &StoredDiags)
591    : Diags(Diags), Client(StoredDiags), PreviousClient(0)
592  {
593    if (RequestCapture || Diags.getClient() == 0) {
594      PreviousClient = Diags.takeClient();
595      Diags.setClient(&Client);
596    }
597  }
598
599  ~CaptureDroppedDiagnostics() {
600    if (Diags.getClient() == &Client) {
601      Diags.takeClient();
602      Diags.setClient(PreviousClient);
603    }
604  }
605};
606
607} // anonymous namespace
608
609void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
610                                              const Diagnostic &Info) {
611  // Default implementation (Warnings/errors count).
612  DiagnosticConsumer::HandleDiagnostic(Level, Info);
613
614  StoredDiags.push_back(StoredDiagnostic(Level, Info));
615}
616
617const std::string &ASTUnit::getOriginalSourceFileName() {
618  return OriginalSourceFile;
619}
620
621llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
622                                              std::string *ErrorStr) {
623  assert(FileMgr);
624  return FileMgr->getBufferForFile(Filename, ErrorStr);
625}
626
627/// \brief Configure the diagnostics object for use with ASTUnit.
628void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
629                             const char **ArgBegin, const char **ArgEnd,
630                             ASTUnit &AST, bool CaptureDiagnostics) {
631  if (!Diags.getPtr()) {
632    // No diagnostics engine was provided, so create our own diagnostics object
633    // with the default options.
634    DiagnosticOptions DiagOpts;
635    DiagnosticConsumer *Client = 0;
636    if (CaptureDiagnostics)
637      Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
638    Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin,
639                                                ArgBegin, Client);
640  } else if (CaptureDiagnostics) {
641    Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
642  }
643}
644
645ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
646                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
647                                  const FileSystemOptions &FileSystemOpts,
648                                  bool OnlyLocalDecls,
649                                  RemappedFile *RemappedFiles,
650                                  unsigned NumRemappedFiles,
651                                  bool CaptureDiagnostics) {
652  llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
653
654  // Recover resources if we crash before exiting this method.
655  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
656    ASTUnitCleanup(AST.get());
657  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
658    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
659    DiagCleanup(Diags.getPtr());
660
661  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
662
663  AST->OnlyLocalDecls = OnlyLocalDecls;
664  AST->CaptureDiagnostics = CaptureDiagnostics;
665  AST->Diagnostics = Diags;
666  AST->FileMgr = new FileManager(FileSystemOpts);
667  AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
668                                     AST->getFileManager());
669  AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager(),
670                                         AST->getDiagnostics(),
671                                         AST->ASTFileLangOpts));
672
673  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
674    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
675    if (const llvm::MemoryBuffer *
676          memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
677      // Create the file entry for the file that we're mapping from.
678      const FileEntry *FromFile
679        = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
680                                               memBuf->getBufferSize(),
681                                               0);
682      if (!FromFile) {
683        AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
684          << RemappedFiles[I].first;
685        delete memBuf;
686        continue;
687      }
688
689      // Override the contents of the "from" file with the contents of
690      // the "to" file.
691      AST->getSourceManager().overrideFileContents(FromFile, memBuf);
692
693    } else {
694      const char *fname = fileOrBuf.get<const char *>();
695      const FileEntry *ToFile = AST->FileMgr->getFile(fname);
696      if (!ToFile) {
697        AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
698        << RemappedFiles[I].first << fname;
699        continue;
700      }
701
702      // Create the file entry for the file that we're mapping from.
703      const FileEntry *FromFile
704        = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
705                                               ToFile->getSize(),
706                                               0);
707      if (!FromFile) {
708        AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
709          << RemappedFiles[I].first;
710        delete memBuf;
711        continue;
712      }
713
714      // Override the contents of the "from" file with the contents of
715      // the "to" file.
716      AST->getSourceManager().overrideFileContents(FromFile, ToFile);
717    }
718  }
719
720  // Gather Info for preprocessor construction later on.
721
722  HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
723  std::string Predefines;
724  unsigned Counter;
725
726  llvm::OwningPtr<ASTReader> Reader;
727
728  AST->PP = new Preprocessor(AST->getDiagnostics(), AST->ASTFileLangOpts,
729                             /*Target=*/0, AST->getSourceManager(), HeaderInfo,
730                             *AST,
731                             /*IILookup=*/0,
732                             /*OwnsHeaderSearch=*/false,
733                             /*DelayInitialization=*/true);
734  Preprocessor &PP = *AST->PP;
735
736  AST->Ctx = new ASTContext(AST->ASTFileLangOpts,
737                            AST->getSourceManager(),
738                            /*Target=*/0,
739                            PP.getIdentifierTable(),
740                            PP.getSelectorTable(),
741                            PP.getBuiltinInfo(),
742                            /* size_reserve = */0,
743                            /*DelayInitialization=*/true);
744  ASTContext &Context = *AST->Ctx;
745
746  Reader.reset(new ASTReader(PP, Context));
747
748  // Recover resources if we crash before exiting this method.
749  llvm::CrashRecoveryContextCleanupRegistrar<ASTReader>
750    ReaderCleanup(Reader.get());
751
752  Reader->setListener(new ASTInfoCollector(*AST->PP, Context,
753                                           AST->ASTFileLangOpts, HeaderInfo,
754                                           AST->Target, Predefines, Counter));
755
756  switch (Reader->ReadAST(Filename, serialization::MK_MainFile)) {
757  case ASTReader::Success:
758    break;
759
760  case ASTReader::Failure:
761  case ASTReader::IgnorePCH:
762    AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
763    return NULL;
764  }
765
766  AST->OriginalSourceFile = Reader->getOriginalSourceFile();
767
768  PP.setPredefines(Reader->getSuggestedPredefines());
769  PP.setCounterValue(Counter);
770
771  // Attach the AST reader to the AST context as an external AST
772  // source, so that declarations will be deserialized from the
773  // AST file as needed.
774  ASTReader *ReaderPtr = Reader.get();
775  llvm::OwningPtr<ExternalASTSource> Source(Reader.take());
776
777  // Unregister the cleanup for ASTReader.  It will get cleaned up
778  // by the ASTUnit cleanup.
779  ReaderCleanup.unregister();
780
781  Context.setExternalSource(Source);
782
783  // Create an AST consumer, even though it isn't used.
784  AST->Consumer.reset(new ASTConsumer);
785
786  // Create a semantic analysis object and tell the AST reader about it.
787  AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer));
788  AST->TheSema->Initialize();
789  ReaderPtr->InitializeSema(*AST->TheSema);
790  AST->Reader = ReaderPtr;
791
792  return AST.take();
793}
794
795namespace {
796
797/// \brief Preprocessor callback class that updates a hash value with the names
798/// of all macros that have been defined by the translation unit.
799class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
800  unsigned &Hash;
801
802public:
803  explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
804
805  virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
806    Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
807  }
808};
809
810/// \brief Add the given declaration to the hash of all top-level entities.
811void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) {
812  if (!D)
813    return;
814
815  DeclContext *DC = D->getDeclContext();
816  if (!DC)
817    return;
818
819  if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit()))
820    return;
821
822  if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
823    if (ND->getIdentifier())
824      Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash);
825    else if (DeclarationName Name = ND->getDeclName()) {
826      std::string NameStr = Name.getAsString();
827      Hash = llvm::HashString(NameStr, Hash);
828    }
829    return;
830  }
831
832  if (ObjCForwardProtocolDecl *Forward
833      = dyn_cast<ObjCForwardProtocolDecl>(D)) {
834    for (ObjCForwardProtocolDecl::protocol_iterator
835         P = Forward->protocol_begin(),
836         PEnd = Forward->protocol_end();
837         P != PEnd; ++P)
838      AddTopLevelDeclarationToHash(*P, Hash);
839    return;
840  }
841}
842
843class TopLevelDeclTrackerConsumer : public ASTConsumer {
844  ASTUnit &Unit;
845  unsigned &Hash;
846
847public:
848  TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
849    : Unit(_Unit), Hash(Hash) {
850    Hash = 0;
851  }
852
853  void handleTopLevelDecl(Decl *D) {
854    if (!D)
855      return;
856
857    // FIXME: Currently ObjC method declarations are incorrectly being
858    // reported as top-level declarations, even though their DeclContext
859    // is the containing ObjC @interface/@implementation.  This is a
860    // fundamental problem in the parser right now.
861    if (isa<ObjCMethodDecl>(D))
862      return;
863
864    AddTopLevelDeclarationToHash(D, Hash);
865    Unit.addTopLevelDecl(D);
866
867    handleFileLevelDecl(D);
868  }
869
870  void handleFileLevelDecl(Decl *D) {
871    Unit.addFileLevelDecl(D);
872    if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
873      for (NamespaceDecl::decl_iterator
874             I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I)
875        handleFileLevelDecl(*I);
876    }
877  }
878
879  bool HandleTopLevelDecl(DeclGroupRef D) {
880    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
881      handleTopLevelDecl(*it);
882    return true;
883  }
884
885  // We're not interested in "interesting" decls.
886  void HandleInterestingDecl(DeclGroupRef) {}
887
888  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
889    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
890      handleTopLevelDecl(*it);
891  }
892};
893
894class TopLevelDeclTrackerAction : public ASTFrontendAction {
895public:
896  ASTUnit &Unit;
897
898  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
899                                         StringRef InFile) {
900    CI.getPreprocessor().addPPCallbacks(
901     new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
902    return new TopLevelDeclTrackerConsumer(Unit,
903                                           Unit.getCurrentTopLevelHashValue());
904  }
905
906public:
907  TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
908
909  virtual bool hasCodeCompletionSupport() const { return false; }
910  virtual TranslationUnitKind getTranslationUnitKind()  {
911    return Unit.getTranslationUnitKind();
912  }
913};
914
915class PrecompilePreambleConsumer : public PCHGenerator {
916  ASTUnit &Unit;
917  unsigned &Hash;
918  std::vector<Decl *> TopLevelDecls;
919
920public:
921  PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP,
922                             StringRef isysroot, raw_ostream *Out)
923    : PCHGenerator(PP, "", 0, isysroot, Out), Unit(Unit),
924      Hash(Unit.getCurrentTopLevelHashValue()) {
925    Hash = 0;
926  }
927
928  virtual bool HandleTopLevelDecl(DeclGroupRef D) {
929    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
930      Decl *D = *it;
931      // FIXME: Currently ObjC method declarations are incorrectly being
932      // reported as top-level declarations, even though their DeclContext
933      // is the containing ObjC @interface/@implementation.  This is a
934      // fundamental problem in the parser right now.
935      if (isa<ObjCMethodDecl>(D))
936        continue;
937      AddTopLevelDeclarationToHash(D, Hash);
938      TopLevelDecls.push_back(D);
939    }
940    return true;
941  }
942
943  virtual void HandleTranslationUnit(ASTContext &Ctx) {
944    PCHGenerator::HandleTranslationUnit(Ctx);
945    if (!Unit.getDiagnostics().hasErrorOccurred()) {
946      // Translate the top-level declarations we captured during
947      // parsing into declaration IDs in the precompiled
948      // preamble. This will allow us to deserialize those top-level
949      // declarations when requested.
950      for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
951        Unit.addTopLevelDeclFromPreamble(
952                                      getWriter().getDeclID(TopLevelDecls[I]));
953    }
954  }
955};
956
957class PrecompilePreambleAction : public ASTFrontendAction {
958  ASTUnit &Unit;
959
960public:
961  explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
962
963  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
964                                         StringRef InFile) {
965    std::string Sysroot;
966    std::string OutputFile;
967    raw_ostream *OS = 0;
968    if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
969                                                       OutputFile,
970                                                       OS))
971      return 0;
972
973    if (!CI.getFrontendOpts().RelocatablePCH)
974      Sysroot.clear();
975
976    CI.getPreprocessor().addPPCallbacks(
977     new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
978    return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot,
979                                          OS);
980  }
981
982  virtual bool hasCodeCompletionSupport() const { return false; }
983  virtual bool hasASTFileSupport() const { return false; }
984  virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
985};
986
987}
988
989/// Parse the source file into a translation unit using the given compiler
990/// invocation, replacing the current translation unit.
991///
992/// \returns True if a failure occurred that causes the ASTUnit not to
993/// contain any translation-unit information, false otherwise.
994bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
995  delete SavedMainFileBuffer;
996  SavedMainFileBuffer = 0;
997
998  if (!Invocation) {
999    delete OverrideMainBuffer;
1000    return true;
1001  }
1002
1003  // Create the compiler instance to use for building the AST.
1004  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1005
1006  // Recover resources if we crash before exiting this method.
1007  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1008    CICleanup(Clang.get());
1009
1010  llvm::IntrusiveRefCntPtr<CompilerInvocation>
1011    CCInvocation(new CompilerInvocation(*Invocation));
1012
1013  Clang->setInvocation(CCInvocation.getPtr());
1014  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1015
1016  // Set up diagnostics, capturing any diagnostics that would
1017  // otherwise be dropped.
1018  Clang->setDiagnostics(&getDiagnostics());
1019
1020  // Create the target instance.
1021  Clang->getTargetOpts().Features = TargetFeatures;
1022  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1023                   Clang->getTargetOpts()));
1024  if (!Clang->hasTarget()) {
1025    delete OverrideMainBuffer;
1026    return true;
1027  }
1028
1029  // Inform the target of the language options.
1030  //
1031  // FIXME: We shouldn't need to do this, the target should be immutable once
1032  // created. This complexity should be lifted elsewhere.
1033  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1034
1035  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1036         "Invocation must have exactly one source file!");
1037  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1038         "FIXME: AST inputs not yet supported here!");
1039  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1040         "IR inputs not support here!");
1041
1042  // Configure the various subsystems.
1043  // FIXME: Should we retain the previous file manager?
1044  LangOpts = &Clang->getLangOpts();
1045  FileSystemOpts = Clang->getFileSystemOpts();
1046  FileMgr = new FileManager(FileSystemOpts);
1047  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
1048  TheSema.reset();
1049  Ctx = 0;
1050  PP = 0;
1051  Reader = 0;
1052
1053  // Clear out old caches and data.
1054  TopLevelDecls.clear();
1055  clearFileLevelDecls();
1056  CleanTemporaryFiles();
1057
1058  if (!OverrideMainBuffer) {
1059    StoredDiagnostics.erase(stored_diag_afterDriver_begin(), stored_diag_end());
1060    TopLevelDeclsInPreamble.clear();
1061  }
1062
1063  // Create a file manager object to provide access to and cache the filesystem.
1064  Clang->setFileManager(&getFileManager());
1065
1066  // Create the source manager.
1067  Clang->setSourceManager(&getSourceManager());
1068
1069  // If the main file has been overridden due to the use of a preamble,
1070  // make that override happen and introduce the preamble.
1071  PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1072  PreprocessorOpts.DetailedRecordIncludesNestedMacroExpansions
1073    = NestedMacroExpansions;
1074  if (OverrideMainBuffer) {
1075    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
1076    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1077    PreprocessorOpts.PrecompiledPreambleBytes.second
1078                                                    = PreambleEndsAtStartOfLine;
1079    PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1080    PreprocessorOpts.DisablePCHValidation = true;
1081
1082    // The stored diagnostic has the old source manager in it; update
1083    // the locations to refer into the new source manager. Since we've
1084    // been careful to make sure that the source manager's state
1085    // before and after are identical, so that we can reuse the source
1086    // location itself.
1087    for (unsigned I = NumStoredDiagnosticsFromDriver,
1088                  N = StoredDiagnostics.size();
1089         I < N; ++I) {
1090      FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
1091                        getSourceManager());
1092      StoredDiagnostics[I].setLocation(Loc);
1093    }
1094
1095    // Keep track of the override buffer;
1096    SavedMainFileBuffer = OverrideMainBuffer;
1097  }
1098
1099  llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
1100    new TopLevelDeclTrackerAction(*this));
1101
1102  // Recover resources if we crash before exiting this method.
1103  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1104    ActCleanup(Act.get());
1105
1106  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1107                            Clang->getFrontendOpts().Inputs[0].first))
1108    goto error;
1109
1110  if (OverrideMainBuffer) {
1111    std::string ModName = getPreambleFile(this);
1112    TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
1113                               getSourceManager(), PreambleDiagnostics,
1114                               StoredDiagnostics);
1115  }
1116
1117  Act->Execute();
1118
1119  // Steal the created target, context, and preprocessor.
1120  TheSema.reset(Clang->takeSema());
1121  Consumer.reset(Clang->takeASTConsumer());
1122  Ctx = &Clang->getASTContext();
1123  PP = &Clang->getPreprocessor();
1124  Clang->setSourceManager(0);
1125  Clang->setFileManager(0);
1126  Target = &Clang->getTarget();
1127  Reader = Clang->getModuleManager();
1128
1129  Act->EndSourceFile();
1130
1131  return false;
1132
1133error:
1134  // Remove the overridden buffer we used for the preamble.
1135  if (OverrideMainBuffer) {
1136    delete OverrideMainBuffer;
1137    SavedMainFileBuffer = 0;
1138  }
1139
1140  StoredDiagnostics.clear();
1141  NumStoredDiagnosticsFromDriver = 0;
1142  return true;
1143}
1144
1145/// \brief Simple function to retrieve a path for a preamble precompiled header.
1146static std::string GetPreamblePCHPath() {
1147  // FIXME: This is lame; sys::Path should provide this function (in particular,
1148  // it should know how to find the temporary files dir).
1149  // FIXME: This is really lame. I copied this code from the Driver!
1150  // FIXME: This is a hack so that we can override the preamble file during
1151  // crash-recovery testing, which is the only case where the preamble files
1152  // are not necessarily cleaned up.
1153  const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1154  if (TmpFile)
1155    return TmpFile;
1156
1157  std::string Error;
1158  const char *TmpDir = ::getenv("TMPDIR");
1159  if (!TmpDir)
1160    TmpDir = ::getenv("TEMP");
1161  if (!TmpDir)
1162    TmpDir = ::getenv("TMP");
1163#ifdef LLVM_ON_WIN32
1164  if (!TmpDir)
1165    TmpDir = ::getenv("USERPROFILE");
1166#endif
1167  if (!TmpDir)
1168    TmpDir = "/tmp";
1169  llvm::sys::Path P(TmpDir);
1170  P.createDirectoryOnDisk(true);
1171  P.appendComponent("preamble");
1172  P.appendSuffix("pch");
1173  if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0))
1174    return std::string();
1175
1176  return P.str();
1177}
1178
1179/// \brief Compute the preamble for the main file, providing the source buffer
1180/// that corresponds to the main file along with a pair (bytes, start-of-line)
1181/// that describes the preamble.
1182std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1183ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1184                         unsigned MaxLines, bool &CreatedBuffer) {
1185  FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1186  PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1187  CreatedBuffer = false;
1188
1189  // Try to determine if the main file has been remapped, either from the
1190  // command line (to another file) or directly through the compiler invocation
1191  // (to a memory buffer).
1192  llvm::MemoryBuffer *Buffer = 0;
1193  llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1194  if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1195    // Check whether there is a file-file remapping of the main file
1196    for (PreprocessorOptions::remapped_file_iterator
1197          M = PreprocessorOpts.remapped_file_begin(),
1198          E = PreprocessorOpts.remapped_file_end();
1199         M != E;
1200         ++M) {
1201      llvm::sys::PathWithStatus MPath(M->first);
1202      if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1203        if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1204          // We found a remapping. Try to load the resulting, remapped source.
1205          if (CreatedBuffer) {
1206            delete Buffer;
1207            CreatedBuffer = false;
1208          }
1209
1210          Buffer = getBufferForFile(M->second);
1211          if (!Buffer)
1212            return std::make_pair((llvm::MemoryBuffer*)0,
1213                                  std::make_pair(0, true));
1214          CreatedBuffer = true;
1215        }
1216      }
1217    }
1218
1219    // Check whether there is a file-buffer remapping. It supercedes the
1220    // file-file remapping.
1221    for (PreprocessorOptions::remapped_file_buffer_iterator
1222           M = PreprocessorOpts.remapped_file_buffer_begin(),
1223           E = PreprocessorOpts.remapped_file_buffer_end();
1224         M != E;
1225         ++M) {
1226      llvm::sys::PathWithStatus MPath(M->first);
1227      if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1228        if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1229          // We found a remapping.
1230          if (CreatedBuffer) {
1231            delete Buffer;
1232            CreatedBuffer = false;
1233          }
1234
1235          Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1236        }
1237      }
1238    }
1239  }
1240
1241  // If the main source file was not remapped, load it now.
1242  if (!Buffer) {
1243    Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1244    if (!Buffer)
1245      return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1246
1247    CreatedBuffer = true;
1248  }
1249
1250  return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1251                                                       *Invocation.getLangOpts(),
1252                                                       MaxLines));
1253}
1254
1255static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1256                                                      unsigned NewSize,
1257                                                      StringRef NewName) {
1258  llvm::MemoryBuffer *Result
1259    = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1260  memcpy(const_cast<char*>(Result->getBufferStart()),
1261         Old->getBufferStart(), Old->getBufferSize());
1262  memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1263         ' ', NewSize - Old->getBufferSize() - 1);
1264  const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1265
1266  return Result;
1267}
1268
1269/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1270/// the source file.
1271///
1272/// This routine will compute the preamble of the main source file. If a
1273/// non-trivial preamble is found, it will precompile that preamble into a
1274/// precompiled header so that the precompiled preamble can be used to reduce
1275/// reparsing time. If a precompiled preamble has already been constructed,
1276/// this routine will determine if it is still valid and, if so, avoid
1277/// rebuilding the precompiled preamble.
1278///
1279/// \param AllowRebuild When true (the default), this routine is
1280/// allowed to rebuild the precompiled preamble if it is found to be
1281/// out-of-date.
1282///
1283/// \param MaxLines When non-zero, the maximum number of lines that
1284/// can occur within the preamble.
1285///
1286/// \returns If the precompiled preamble can be used, returns a newly-allocated
1287/// buffer that should be used in place of the main file when doing so.
1288/// Otherwise, returns a NULL pointer.
1289llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1290                              const CompilerInvocation &PreambleInvocationIn,
1291                                                           bool AllowRebuild,
1292                                                           unsigned MaxLines) {
1293
1294  llvm::IntrusiveRefCntPtr<CompilerInvocation>
1295    PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1296  FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1297  PreprocessorOptions &PreprocessorOpts
1298    = PreambleInvocation->getPreprocessorOpts();
1299
1300  bool CreatedPreambleBuffer = false;
1301  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1302    = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1303
1304  // If ComputePreamble() Take ownership of the preamble buffer.
1305  llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1306  if (CreatedPreambleBuffer)
1307    OwnedPreambleBuffer.reset(NewPreamble.first);
1308
1309  if (!NewPreamble.second.first) {
1310    // We couldn't find a preamble in the main source. Clear out the current
1311    // preamble, if we have one. It's obviously no good any more.
1312    Preamble.clear();
1313    erasePreambleFile(this);
1314
1315    // The next time we actually see a preamble, precompile it.
1316    PreambleRebuildCounter = 1;
1317    return 0;
1318  }
1319
1320  if (!Preamble.empty()) {
1321    // We've previously computed a preamble. Check whether we have the same
1322    // preamble now that we did before, and that there's enough space in
1323    // the main-file buffer within the precompiled preamble to fit the
1324    // new main file.
1325    if (Preamble.size() == NewPreamble.second.first &&
1326        PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1327        NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1328        memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
1329               NewPreamble.second.first) == 0) {
1330      // The preamble has not changed. We may be able to re-use the precompiled
1331      // preamble.
1332
1333      // Check that none of the files used by the preamble have changed.
1334      bool AnyFileChanged = false;
1335
1336      // First, make a record of those files that have been overridden via
1337      // remapping or unsaved_files.
1338      llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1339      for (PreprocessorOptions::remapped_file_iterator
1340                R = PreprocessorOpts.remapped_file_begin(),
1341             REnd = PreprocessorOpts.remapped_file_end();
1342           !AnyFileChanged && R != REnd;
1343           ++R) {
1344        struct stat StatBuf;
1345        if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1346          // If we can't stat the file we're remapping to, assume that something
1347          // horrible happened.
1348          AnyFileChanged = true;
1349          break;
1350        }
1351
1352        OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1353                                                   StatBuf.st_mtime);
1354      }
1355      for (PreprocessorOptions::remapped_file_buffer_iterator
1356                R = PreprocessorOpts.remapped_file_buffer_begin(),
1357             REnd = PreprocessorOpts.remapped_file_buffer_end();
1358           !AnyFileChanged && R != REnd;
1359           ++R) {
1360        // FIXME: Should we actually compare the contents of file->buffer
1361        // remappings?
1362        OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1363                                                   0);
1364      }
1365
1366      // Check whether anything has changed.
1367      for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1368             F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1369           !AnyFileChanged && F != FEnd;
1370           ++F) {
1371        llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1372          = OverriddenFiles.find(F->first());
1373        if (Overridden != OverriddenFiles.end()) {
1374          // This file was remapped; check whether the newly-mapped file
1375          // matches up with the previous mapping.
1376          if (Overridden->second != F->second)
1377            AnyFileChanged = true;
1378          continue;
1379        }
1380
1381        // The file was not remapped; check whether it has changed on disk.
1382        struct stat StatBuf;
1383        if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1384          // If we can't stat the file, assume that something horrible happened.
1385          AnyFileChanged = true;
1386        } else if (StatBuf.st_size != F->second.first ||
1387                   StatBuf.st_mtime != F->second.second)
1388          AnyFileChanged = true;
1389      }
1390
1391      if (!AnyFileChanged) {
1392        // Okay! We can re-use the precompiled preamble.
1393
1394        // Set the state of the diagnostic object to mimic its state
1395        // after parsing the preamble.
1396        getDiagnostics().Reset();
1397        ProcessWarningOptions(getDiagnostics(),
1398                              PreambleInvocation->getDiagnosticOpts());
1399        getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1400
1401        // Create a version of the main file buffer that is padded to
1402        // buffer size we reserved when creating the preamble.
1403        return CreatePaddedMainFileBuffer(NewPreamble.first,
1404                                          PreambleReservedSize,
1405                                          FrontendOpts.Inputs[0].second);
1406      }
1407    }
1408
1409    // If we aren't allowed to rebuild the precompiled preamble, just
1410    // return now.
1411    if (!AllowRebuild)
1412      return 0;
1413
1414    // We can't reuse the previously-computed preamble. Build a new one.
1415    Preamble.clear();
1416    PreambleDiagnostics.clear();
1417    erasePreambleFile(this);
1418    PreambleRebuildCounter = 1;
1419  } else if (!AllowRebuild) {
1420    // We aren't allowed to rebuild the precompiled preamble; just
1421    // return now.
1422    return 0;
1423  }
1424
1425  // If the preamble rebuild counter > 1, it's because we previously
1426  // failed to build a preamble and we're not yet ready to try
1427  // again. Decrement the counter and return a failure.
1428  if (PreambleRebuildCounter > 1) {
1429    --PreambleRebuildCounter;
1430    return 0;
1431  }
1432
1433  // Create a temporary file for the precompiled preamble. In rare
1434  // circumstances, this can fail.
1435  std::string PreamblePCHPath = GetPreamblePCHPath();
1436  if (PreamblePCHPath.empty()) {
1437    // Try again next time.
1438    PreambleRebuildCounter = 1;
1439    return 0;
1440  }
1441
1442  // We did not previously compute a preamble, or it can't be reused anyway.
1443  SimpleTimer PreambleTimer(WantTiming);
1444  PreambleTimer.setOutput("Precompiling preamble");
1445
1446  // Create a new buffer that stores the preamble. The buffer also contains
1447  // extra space for the original contents of the file (which will be present
1448  // when we actually parse the file) along with more room in case the file
1449  // grows.
1450  PreambleReservedSize = NewPreamble.first->getBufferSize();
1451  if (PreambleReservedSize < 4096)
1452    PreambleReservedSize = 8191;
1453  else
1454    PreambleReservedSize *= 2;
1455
1456  // Save the preamble text for later; we'll need to compare against it for
1457  // subsequent reparses.
1458  StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].second;
1459  Preamble.assign(FileMgr->getFile(MainFilename),
1460                  NewPreamble.first->getBufferStart(),
1461                  NewPreamble.first->getBufferStart()
1462                                                  + NewPreamble.second.first);
1463  PreambleEndsAtStartOfLine = NewPreamble.second.second;
1464
1465  delete PreambleBuffer;
1466  PreambleBuffer
1467    = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1468                                                FrontendOpts.Inputs[0].second);
1469  memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1470         NewPreamble.first->getBufferStart(), Preamble.size());
1471  memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1472         ' ', PreambleReservedSize - Preamble.size() - 1);
1473  const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1474
1475  // Remap the main source file to the preamble buffer.
1476  llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1477  PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer);
1478
1479  // Tell the compiler invocation to generate a temporary precompiled header.
1480  FrontendOpts.ProgramAction = frontend::GeneratePCH;
1481  // FIXME: Generate the precompiled header into memory?
1482  FrontendOpts.OutputFile = PreamblePCHPath;
1483  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1484  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1485
1486  // Create the compiler instance to use for building the precompiled preamble.
1487  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1488
1489  // Recover resources if we crash before exiting this method.
1490  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1491    CICleanup(Clang.get());
1492
1493  Clang->setInvocation(&*PreambleInvocation);
1494  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1495
1496  // Set up diagnostics, capturing all of the diagnostics produced.
1497  Clang->setDiagnostics(&getDiagnostics());
1498
1499  // Create the target instance.
1500  Clang->getTargetOpts().Features = TargetFeatures;
1501  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1502                                               Clang->getTargetOpts()));
1503  if (!Clang->hasTarget()) {
1504    llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1505    Preamble.clear();
1506    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1507    PreprocessorOpts.eraseRemappedFile(
1508                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1509    return 0;
1510  }
1511
1512  // Inform the target of the language options.
1513  //
1514  // FIXME: We shouldn't need to do this, the target should be immutable once
1515  // created. This complexity should be lifted elsewhere.
1516  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1517
1518  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1519         "Invocation must have exactly one source file!");
1520  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1521         "FIXME: AST inputs not yet supported here!");
1522  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1523         "IR inputs not support here!");
1524
1525  // Clear out old caches and data.
1526  getDiagnostics().Reset();
1527  ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1528  StoredDiagnostics.erase(stored_diag_afterDriver_begin(), stored_diag_end());
1529  TopLevelDecls.clear();
1530  TopLevelDeclsInPreamble.clear();
1531
1532  // Create a file manager object to provide access to and cache the filesystem.
1533  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1534
1535  // Create the source manager.
1536  Clang->setSourceManager(new SourceManager(getDiagnostics(),
1537                                            Clang->getFileManager()));
1538
1539  llvm::OwningPtr<PrecompilePreambleAction> Act;
1540  Act.reset(new PrecompilePreambleAction(*this));
1541  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
1542                            Clang->getFrontendOpts().Inputs[0].first)) {
1543    llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1544    Preamble.clear();
1545    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1546    PreprocessorOpts.eraseRemappedFile(
1547                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1548    return 0;
1549  }
1550
1551  Act->Execute();
1552  Act->EndSourceFile();
1553
1554  if (Diagnostics->hasErrorOccurred()) {
1555    // There were errors parsing the preamble, so no precompiled header was
1556    // generated. Forget that we even tried.
1557    // FIXME: Should we leave a note for ourselves to try again?
1558    llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk();
1559    Preamble.clear();
1560    TopLevelDeclsInPreamble.clear();
1561    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1562    PreprocessorOpts.eraseRemappedFile(
1563                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1564    return 0;
1565  }
1566
1567  // Transfer any diagnostics generated when parsing the preamble into the set
1568  // of preamble diagnostics.
1569  PreambleDiagnostics.clear();
1570  PreambleDiagnostics.insert(PreambleDiagnostics.end(),
1571                            stored_diag_afterDriver_begin(), stored_diag_end());
1572  StoredDiagnostics.erase(stored_diag_afterDriver_begin(), stored_diag_end());
1573
1574  // Keep track of the preamble we precompiled.
1575  setPreambleFile(this, FrontendOpts.OutputFile);
1576  NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1577
1578  // Keep track of all of the files that the source manager knows about,
1579  // so we can verify whether they have changed or not.
1580  FilesInPreamble.clear();
1581  SourceManager &SourceMgr = Clang->getSourceManager();
1582  const llvm::MemoryBuffer *MainFileBuffer
1583    = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1584  for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1585                                     FEnd = SourceMgr.fileinfo_end();
1586       F != FEnd;
1587       ++F) {
1588    const FileEntry *File = F->second->OrigEntry;
1589    if (!File || F->second->getRawBuffer() == MainFileBuffer)
1590      continue;
1591
1592    FilesInPreamble[File->getName()]
1593      = std::make_pair(F->second->getSize(), File->getModificationTime());
1594  }
1595
1596  PreambleRebuildCounter = 1;
1597  PreprocessorOpts.eraseRemappedFile(
1598                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1599
1600  // If the hash of top-level entities differs from the hash of the top-level
1601  // entities the last time we rebuilt the preamble, clear out the completion
1602  // cache.
1603  if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1604    CompletionCacheTopLevelHashValue = 0;
1605    PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1606  }
1607
1608  return CreatePaddedMainFileBuffer(NewPreamble.first,
1609                                    PreambleReservedSize,
1610                                    FrontendOpts.Inputs[0].second);
1611}
1612
1613void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1614  std::vector<Decl *> Resolved;
1615  Resolved.reserve(TopLevelDeclsInPreamble.size());
1616  ExternalASTSource &Source = *getASTContext().getExternalSource();
1617  for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1618    // Resolve the declaration ID to an actual declaration, possibly
1619    // deserializing the declaration in the process.
1620    Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1621    if (D)
1622      Resolved.push_back(D);
1623  }
1624  TopLevelDeclsInPreamble.clear();
1625  TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1626}
1627
1628StringRef ASTUnit::getMainFileName() const {
1629  return Invocation->getFrontendOpts().Inputs[0].second;
1630}
1631
1632ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1633                         llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1634                         bool CaptureDiagnostics) {
1635  llvm::OwningPtr<ASTUnit> AST;
1636  AST.reset(new ASTUnit(false));
1637  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1638  AST->Diagnostics = Diags;
1639  AST->Invocation = CI;
1640  AST->FileSystemOpts = CI->getFileSystemOpts();
1641  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1642  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1643
1644  return AST.take();
1645}
1646
1647ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1648                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1649                                             ASTFrontendAction *Action,
1650                                             ASTUnit *Unit,
1651                                             bool Persistent,
1652                                             StringRef ResourceFilesPath,
1653                                             bool OnlyLocalDecls,
1654                                             bool CaptureDiagnostics,
1655                                             bool PrecompilePreamble,
1656                                             bool CacheCodeCompletionResults) {
1657  assert(CI && "A CompilerInvocation is required");
1658
1659  llvm::OwningPtr<ASTUnit> OwnAST;
1660  ASTUnit *AST = Unit;
1661  if (!AST) {
1662    // Create the AST unit.
1663    OwnAST.reset(create(CI, Diags, CaptureDiagnostics));
1664    AST = OwnAST.get();
1665  }
1666
1667  if (!ResourceFilesPath.empty()) {
1668    // Override the resources path.
1669    CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1670  }
1671  AST->OnlyLocalDecls = OnlyLocalDecls;
1672  AST->CaptureDiagnostics = CaptureDiagnostics;
1673  if (PrecompilePreamble)
1674    AST->PreambleRebuildCounter = 2;
1675  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1676  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1677
1678  // Recover resources if we crash before exiting this method.
1679  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1680    ASTUnitCleanup(OwnAST.get());
1681  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1682    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1683    DiagCleanup(Diags.getPtr());
1684
1685  // We'll manage file buffers ourselves.
1686  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1687  CI->getFrontendOpts().DisableFree = false;
1688  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1689
1690  // Save the target features.
1691  AST->TargetFeatures = CI->getTargetOpts().Features;
1692
1693  // Create the compiler instance to use for building the AST.
1694  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1695
1696  // Recover resources if we crash before exiting this method.
1697  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1698    CICleanup(Clang.get());
1699
1700  Clang->setInvocation(CI);
1701  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1702
1703  // Set up diagnostics, capturing any diagnostics that would
1704  // otherwise be dropped.
1705  Clang->setDiagnostics(&AST->getDiagnostics());
1706
1707  // Create the target instance.
1708  Clang->getTargetOpts().Features = AST->TargetFeatures;
1709  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1710                   Clang->getTargetOpts()));
1711  if (!Clang->hasTarget())
1712    return 0;
1713
1714  // Inform the target of the language options.
1715  //
1716  // FIXME: We shouldn't need to do this, the target should be immutable once
1717  // created. This complexity should be lifted elsewhere.
1718  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1719
1720  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1721         "Invocation must have exactly one source file!");
1722  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1723         "FIXME: AST inputs not yet supported here!");
1724  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1725         "IR inputs not supported here!");
1726
1727  // Configure the various subsystems.
1728  AST->TheSema.reset();
1729  AST->Ctx = 0;
1730  AST->PP = 0;
1731  AST->Reader = 0;
1732
1733  // Create a file manager object to provide access to and cache the filesystem.
1734  Clang->setFileManager(&AST->getFileManager());
1735
1736  // Create the source manager.
1737  Clang->setSourceManager(&AST->getSourceManager());
1738
1739  ASTFrontendAction *Act = Action;
1740
1741  llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1742  if (!Act) {
1743    TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1744    Act = TrackerAct.get();
1745  }
1746
1747  // Recover resources if we crash before exiting this method.
1748  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1749    ActCleanup(TrackerAct.get());
1750
1751  if (!Act->BeginSourceFile(*Clang.get(),
1752                            Clang->getFrontendOpts().Inputs[0].second,
1753                            Clang->getFrontendOpts().Inputs[0].first))
1754    return 0;
1755
1756  if (Persistent && !TrackerAct) {
1757    Clang->getPreprocessor().addPPCallbacks(
1758     new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
1759    std::vector<ASTConsumer*> Consumers;
1760    if (Clang->hasASTConsumer())
1761      Consumers.push_back(Clang->takeASTConsumer());
1762    Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
1763                                           AST->getCurrentTopLevelHashValue()));
1764    Clang->setASTConsumer(new MultiplexConsumer(Consumers));
1765  }
1766  Act->Execute();
1767
1768  // Steal the created target, context, and preprocessor.
1769  AST->TheSema.reset(Clang->takeSema());
1770  AST->Consumer.reset(Clang->takeASTConsumer());
1771  AST->Ctx = &Clang->getASTContext();
1772  AST->PP = &Clang->getPreprocessor();
1773  Clang->setSourceManager(0);
1774  Clang->setFileManager(0);
1775  AST->Target = &Clang->getTarget();
1776  AST->Reader = Clang->getModuleManager();
1777
1778  Act->EndSourceFile();
1779
1780  if (OwnAST)
1781    return OwnAST.take();
1782  else
1783    return AST;
1784}
1785
1786bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1787  if (!Invocation)
1788    return true;
1789
1790  // We'll manage file buffers ourselves.
1791  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1792  Invocation->getFrontendOpts().DisableFree = false;
1793  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1794
1795  // Save the target features.
1796  TargetFeatures = Invocation->getTargetOpts().Features;
1797
1798  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1799  if (PrecompilePreamble) {
1800    PreambleRebuildCounter = 2;
1801    OverrideMainBuffer
1802      = getMainBufferWithPrecompiledPreamble(*Invocation);
1803  }
1804
1805  SimpleTimer ParsingTimer(WantTiming);
1806  ParsingTimer.setOutput("Parsing " + getMainFileName());
1807
1808  // Recover resources if we crash before exiting this method.
1809  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1810    MemBufferCleanup(OverrideMainBuffer);
1811
1812  return Parse(OverrideMainBuffer);
1813}
1814
1815ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1816                              llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1817                                             bool OnlyLocalDecls,
1818                                             bool CaptureDiagnostics,
1819                                             bool PrecompilePreamble,
1820                                             TranslationUnitKind TUKind,
1821                                             bool CacheCodeCompletionResults,
1822                                             bool NestedMacroExpansions) {
1823  // Create the AST unit.
1824  llvm::OwningPtr<ASTUnit> AST;
1825  AST.reset(new ASTUnit(false));
1826  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1827  AST->Diagnostics = Diags;
1828  AST->OnlyLocalDecls = OnlyLocalDecls;
1829  AST->CaptureDiagnostics = CaptureDiagnostics;
1830  AST->TUKind = TUKind;
1831  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1832  AST->Invocation = CI;
1833  AST->NestedMacroExpansions = NestedMacroExpansions;
1834
1835  // Recover resources if we crash before exiting this method.
1836  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1837    ASTUnitCleanup(AST.get());
1838  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1839    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1840    DiagCleanup(Diags.getPtr());
1841
1842  return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1843}
1844
1845ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1846                                      const char **ArgEnd,
1847                                    llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1848                                      StringRef ResourceFilesPath,
1849                                      bool OnlyLocalDecls,
1850                                      bool CaptureDiagnostics,
1851                                      RemappedFile *RemappedFiles,
1852                                      unsigned NumRemappedFiles,
1853                                      bool RemappedFilesKeepOriginalName,
1854                                      bool PrecompilePreamble,
1855                                      TranslationUnitKind TUKind,
1856                                      bool CacheCodeCompletionResults,
1857                                      bool NestedMacroExpansions) {
1858  if (!Diags.getPtr()) {
1859    // No diagnostics engine was provided, so create our own diagnostics object
1860    // with the default options.
1861    DiagnosticOptions DiagOpts;
1862    Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1863                                                ArgBegin);
1864  }
1865
1866  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1867
1868  llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1869
1870  {
1871
1872    CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1873                                      StoredDiagnostics);
1874
1875    CI = clang::createInvocationFromCommandLine(
1876                                           llvm::makeArrayRef(ArgBegin, ArgEnd),
1877                                           Diags);
1878    if (!CI)
1879      return 0;
1880  }
1881
1882  // Override any files that need remapping
1883  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1884    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1885    if (const llvm::MemoryBuffer *
1886            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1887      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1888    } else {
1889      const char *fname = fileOrBuf.get<const char *>();
1890      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1891    }
1892  }
1893  CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1894                                                  RemappedFilesKeepOriginalName;
1895
1896  // Override the resources path.
1897  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1898
1899  // Create the AST unit.
1900  llvm::OwningPtr<ASTUnit> AST;
1901  AST.reset(new ASTUnit(false));
1902  ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1903  AST->Diagnostics = Diags;
1904  Diags = 0; // Zero out now to ease cleanup during crash recovery.
1905  AST->FileSystemOpts = CI->getFileSystemOpts();
1906  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1907  AST->OnlyLocalDecls = OnlyLocalDecls;
1908  AST->CaptureDiagnostics = CaptureDiagnostics;
1909  AST->TUKind = TUKind;
1910  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1911  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1912  AST->StoredDiagnostics.swap(StoredDiagnostics);
1913  AST->Invocation = CI;
1914  CI = 0; // Zero out now to ease cleanup during crash recovery.
1915  AST->NestedMacroExpansions = NestedMacroExpansions;
1916
1917  // Recover resources if we crash before exiting this method.
1918  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1919    ASTUnitCleanup(AST.get());
1920
1921  return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1922}
1923
1924bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1925  if (!Invocation)
1926    return true;
1927
1928  clearFileLevelDecls();
1929
1930  SimpleTimer ParsingTimer(WantTiming);
1931  ParsingTimer.setOutput("Reparsing " + getMainFileName());
1932
1933  // Remap files.
1934  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1935  PPOpts.DisableStatCache = true;
1936  for (PreprocessorOptions::remapped_file_buffer_iterator
1937         R = PPOpts.remapped_file_buffer_begin(),
1938         REnd = PPOpts.remapped_file_buffer_end();
1939       R != REnd;
1940       ++R) {
1941    delete R->second;
1942  }
1943  Invocation->getPreprocessorOpts().clearRemappedFiles();
1944  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1945    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1946    if (const llvm::MemoryBuffer *
1947            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1948      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1949                                                        memBuf);
1950    } else {
1951      const char *fname = fileOrBuf.get<const char *>();
1952      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1953                                                        fname);
1954    }
1955  }
1956
1957  // If we have a preamble file lying around, or if we might try to
1958  // build a precompiled preamble, do so now.
1959  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1960  if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
1961    OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1962
1963  // Clear out the diagnostics state.
1964  getDiagnostics().Reset();
1965  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1966  if (OverrideMainBuffer)
1967    getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1968
1969  // Parse the sources
1970  bool Result = Parse(OverrideMainBuffer);
1971
1972  // If we're caching global code-completion results, and the top-level
1973  // declarations have changed, clear out the code-completion cache.
1974  if (!Result && ShouldCacheCodeCompletionResults &&
1975      CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1976    CacheCodeCompletionResults();
1977
1978  // We now need to clear out the completion allocator for
1979  // clang_getCursorCompletionString; it'll be recreated if necessary.
1980  CursorCompletionAllocator = 0;
1981
1982  return Result;
1983}
1984
1985//----------------------------------------------------------------------------//
1986// Code completion
1987//----------------------------------------------------------------------------//
1988
1989namespace {
1990  /// \brief Code completion consumer that combines the cached code-completion
1991  /// results from an ASTUnit with the code-completion results provided to it,
1992  /// then passes the result on to
1993  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1994    unsigned long long NormalContexts;
1995    ASTUnit &AST;
1996    CodeCompleteConsumer &Next;
1997
1998  public:
1999    AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2000                                  bool IncludeMacros, bool IncludeCodePatterns,
2001                                  bool IncludeGlobals)
2002      : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
2003                             Next.isOutputBinary()), AST(AST), Next(Next)
2004    {
2005      // Compute the set of contexts in which we will look when we don't have
2006      // any information about the specific context.
2007      NormalContexts
2008        = (1LL << (CodeCompletionContext::CCC_TopLevel - 1))
2009        | (1LL << (CodeCompletionContext::CCC_ObjCInterface - 1))
2010        | (1LL << (CodeCompletionContext::CCC_ObjCImplementation - 1))
2011        | (1LL << (CodeCompletionContext::CCC_ObjCIvarList - 1))
2012        | (1LL << (CodeCompletionContext::CCC_Statement - 1))
2013        | (1LL << (CodeCompletionContext::CCC_Expression - 1))
2014        | (1LL << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
2015        | (1LL << (CodeCompletionContext::CCC_DotMemberAccess - 1))
2016        | (1LL << (CodeCompletionContext::CCC_ArrowMemberAccess - 1))
2017        | (1LL << (CodeCompletionContext::CCC_ObjCPropertyAccess - 1))
2018        | (1LL << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
2019        | (1LL << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
2020        | (1LL << (CodeCompletionContext::CCC_Recovery - 1));
2021
2022      if (AST.getASTContext().getLangOptions().CPlusPlus)
2023        NormalContexts |= (1LL << (CodeCompletionContext::CCC_EnumTag - 1))
2024                   | (1LL << (CodeCompletionContext::CCC_UnionTag - 1))
2025                   | (1LL << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
2026    }
2027
2028    virtual void ProcessCodeCompleteResults(Sema &S,
2029                                            CodeCompletionContext Context,
2030                                            CodeCompletionResult *Results,
2031                                            unsigned NumResults);
2032
2033    virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2034                                           OverloadCandidate *Candidates,
2035                                           unsigned NumCandidates) {
2036      Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2037    }
2038
2039    virtual CodeCompletionAllocator &getAllocator() {
2040      return Next.getAllocator();
2041    }
2042  };
2043}
2044
2045/// \brief Helper function that computes which global names are hidden by the
2046/// local code-completion results.
2047static void CalculateHiddenNames(const CodeCompletionContext &Context,
2048                                 CodeCompletionResult *Results,
2049                                 unsigned NumResults,
2050                                 ASTContext &Ctx,
2051                          llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2052  bool OnlyTagNames = false;
2053  switch (Context.getKind()) {
2054  case CodeCompletionContext::CCC_Recovery:
2055  case CodeCompletionContext::CCC_TopLevel:
2056  case CodeCompletionContext::CCC_ObjCInterface:
2057  case CodeCompletionContext::CCC_ObjCImplementation:
2058  case CodeCompletionContext::CCC_ObjCIvarList:
2059  case CodeCompletionContext::CCC_ClassStructUnion:
2060  case CodeCompletionContext::CCC_Statement:
2061  case CodeCompletionContext::CCC_Expression:
2062  case CodeCompletionContext::CCC_ObjCMessageReceiver:
2063  case CodeCompletionContext::CCC_DotMemberAccess:
2064  case CodeCompletionContext::CCC_ArrowMemberAccess:
2065  case CodeCompletionContext::CCC_ObjCPropertyAccess:
2066  case CodeCompletionContext::CCC_Namespace:
2067  case CodeCompletionContext::CCC_Type:
2068  case CodeCompletionContext::CCC_Name:
2069  case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2070  case CodeCompletionContext::CCC_ParenthesizedExpression:
2071  case CodeCompletionContext::CCC_ObjCInterfaceName:
2072    break;
2073
2074  case CodeCompletionContext::CCC_EnumTag:
2075  case CodeCompletionContext::CCC_UnionTag:
2076  case CodeCompletionContext::CCC_ClassOrStructTag:
2077    OnlyTagNames = true;
2078    break;
2079
2080  case CodeCompletionContext::CCC_ObjCProtocolName:
2081  case CodeCompletionContext::CCC_MacroName:
2082  case CodeCompletionContext::CCC_MacroNameUse:
2083  case CodeCompletionContext::CCC_PreprocessorExpression:
2084  case CodeCompletionContext::CCC_PreprocessorDirective:
2085  case CodeCompletionContext::CCC_NaturalLanguage:
2086  case CodeCompletionContext::CCC_SelectorName:
2087  case CodeCompletionContext::CCC_TypeQualifiers:
2088  case CodeCompletionContext::CCC_Other:
2089  case CodeCompletionContext::CCC_OtherWithMacros:
2090  case CodeCompletionContext::CCC_ObjCInstanceMessage:
2091  case CodeCompletionContext::CCC_ObjCClassMessage:
2092  case CodeCompletionContext::CCC_ObjCCategoryName:
2093    // We're looking for nothing, or we're looking for names that cannot
2094    // be hidden.
2095    return;
2096  }
2097
2098  typedef CodeCompletionResult Result;
2099  for (unsigned I = 0; I != NumResults; ++I) {
2100    if (Results[I].Kind != Result::RK_Declaration)
2101      continue;
2102
2103    unsigned IDNS
2104      = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2105
2106    bool Hiding = false;
2107    if (OnlyTagNames)
2108      Hiding = (IDNS & Decl::IDNS_Tag);
2109    else {
2110      unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2111                             Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2112                             Decl::IDNS_NonMemberOperator);
2113      if (Ctx.getLangOptions().CPlusPlus)
2114        HiddenIDNS |= Decl::IDNS_Tag;
2115      Hiding = (IDNS & HiddenIDNS);
2116    }
2117
2118    if (!Hiding)
2119      continue;
2120
2121    DeclarationName Name = Results[I].Declaration->getDeclName();
2122    if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2123      HiddenNames.insert(Identifier->getName());
2124    else
2125      HiddenNames.insert(Name.getAsString());
2126  }
2127}
2128
2129
2130void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2131                                            CodeCompletionContext Context,
2132                                            CodeCompletionResult *Results,
2133                                            unsigned NumResults) {
2134  // Merge the results we were given with the results we cached.
2135  bool AddedResult = false;
2136  unsigned InContexts
2137    = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
2138                                        : (1ULL << (Context.getKind() - 1)));
2139  // Contains the set of names that are hidden by "local" completion results.
2140  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2141  typedef CodeCompletionResult Result;
2142  SmallVector<Result, 8> AllResults;
2143  for (ASTUnit::cached_completion_iterator
2144            C = AST.cached_completion_begin(),
2145         CEnd = AST.cached_completion_end();
2146       C != CEnd; ++C) {
2147    // If the context we are in matches any of the contexts we are
2148    // interested in, we'll add this result.
2149    if ((C->ShowInContexts & InContexts) == 0)
2150      continue;
2151
2152    // If we haven't added any results previously, do so now.
2153    if (!AddedResult) {
2154      CalculateHiddenNames(Context, Results, NumResults, S.Context,
2155                           HiddenNames);
2156      AllResults.insert(AllResults.end(), Results, Results + NumResults);
2157      AddedResult = true;
2158    }
2159
2160    // Determine whether this global completion result is hidden by a local
2161    // completion result. If so, skip it.
2162    if (C->Kind != CXCursor_MacroDefinition &&
2163        HiddenNames.count(C->Completion->getTypedText()))
2164      continue;
2165
2166    // Adjust priority based on similar type classes.
2167    unsigned Priority = C->Priority;
2168    CXCursorKind CursorKind = C->Kind;
2169    CodeCompletionString *Completion = C->Completion;
2170    if (!Context.getPreferredType().isNull()) {
2171      if (C->Kind == CXCursor_MacroDefinition) {
2172        Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2173                                         S.getLangOptions(),
2174                               Context.getPreferredType()->isAnyPointerType());
2175      } else if (C->Type) {
2176        CanQualType Expected
2177          = S.Context.getCanonicalType(
2178                               Context.getPreferredType().getUnqualifiedType());
2179        SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2180        if (ExpectedSTC == C->TypeClass) {
2181          // We know this type is similar; check for an exact match.
2182          llvm::StringMap<unsigned> &CachedCompletionTypes
2183            = AST.getCachedCompletionTypes();
2184          llvm::StringMap<unsigned>::iterator Pos
2185            = CachedCompletionTypes.find(QualType(Expected).getAsString());
2186          if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2187            Priority /= CCF_ExactTypeMatch;
2188          else
2189            Priority /= CCF_SimilarTypeMatch;
2190        }
2191      }
2192    }
2193
2194    // Adjust the completion string, if required.
2195    if (C->Kind == CXCursor_MacroDefinition &&
2196        Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2197      // Create a new code-completion string that just contains the
2198      // macro name, without its arguments.
2199      CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2200                                    C->Availability);
2201      Builder.AddTypedTextChunk(C->Completion->getTypedText());
2202      CursorKind = CXCursor_NotImplemented;
2203      Priority = CCP_CodePattern;
2204      Completion = Builder.TakeString();
2205    }
2206
2207    AllResults.push_back(Result(Completion, Priority, CursorKind,
2208                                C->Availability));
2209  }
2210
2211  // If we did not add any cached completion results, just forward the
2212  // results we were given to the next consumer.
2213  if (!AddedResult) {
2214    Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2215    return;
2216  }
2217
2218  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2219                                  AllResults.size());
2220}
2221
2222
2223
2224void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2225                           RemappedFile *RemappedFiles,
2226                           unsigned NumRemappedFiles,
2227                           bool IncludeMacros,
2228                           bool IncludeCodePatterns,
2229                           CodeCompleteConsumer &Consumer,
2230                           DiagnosticsEngine &Diag, LangOptions &LangOpts,
2231                           SourceManager &SourceMgr, FileManager &FileMgr,
2232                   SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2233             SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2234  if (!Invocation)
2235    return;
2236
2237  SimpleTimer CompletionTimer(WantTiming);
2238  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2239                            Twine(Line) + ":" + Twine(Column));
2240
2241  llvm::IntrusiveRefCntPtr<CompilerInvocation>
2242    CCInvocation(new CompilerInvocation(*Invocation));
2243
2244  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2245  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2246
2247  FrontendOpts.ShowMacrosInCodeCompletion
2248    = IncludeMacros && CachedCompletionResults.empty();
2249  FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2250  FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2251    = CachedCompletionResults.empty();
2252  FrontendOpts.CodeCompletionAt.FileName = File;
2253  FrontendOpts.CodeCompletionAt.Line = Line;
2254  FrontendOpts.CodeCompletionAt.Column = Column;
2255
2256  // Set the language options appropriately.
2257  LangOpts = *CCInvocation->getLangOpts();
2258
2259  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2260
2261  // Recover resources if we crash before exiting this method.
2262  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2263    CICleanup(Clang.get());
2264
2265  Clang->setInvocation(&*CCInvocation);
2266  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2267
2268  // Set up diagnostics, capturing any diagnostics produced.
2269  Clang->setDiagnostics(&Diag);
2270  ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2271  CaptureDroppedDiagnostics Capture(true,
2272                                    Clang->getDiagnostics(),
2273                                    StoredDiagnostics);
2274
2275  // Create the target instance.
2276  Clang->getTargetOpts().Features = TargetFeatures;
2277  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2278                                               Clang->getTargetOpts()));
2279  if (!Clang->hasTarget()) {
2280    Clang->setInvocation(0);
2281    return;
2282  }
2283
2284  // Inform the target of the language options.
2285  //
2286  // FIXME: We shouldn't need to do this, the target should be immutable once
2287  // created. This complexity should be lifted elsewhere.
2288  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2289
2290  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2291         "Invocation must have exactly one source file!");
2292  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2293         "FIXME: AST inputs not yet supported here!");
2294  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2295         "IR inputs not support here!");
2296
2297
2298  // Use the source and file managers that we were given.
2299  Clang->setFileManager(&FileMgr);
2300  Clang->setSourceManager(&SourceMgr);
2301
2302  // Remap files.
2303  PreprocessorOpts.clearRemappedFiles();
2304  PreprocessorOpts.RetainRemappedFileBuffers = true;
2305  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2306    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2307    if (const llvm::MemoryBuffer *
2308            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2309      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2310      OwnedBuffers.push_back(memBuf);
2311    } else {
2312      const char *fname = fileOrBuf.get<const char *>();
2313      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2314    }
2315  }
2316
2317  // Use the code completion consumer we were given, but adding any cached
2318  // code-completion results.
2319  AugmentedCodeCompleteConsumer *AugmentedConsumer
2320    = new AugmentedCodeCompleteConsumer(*this, Consumer,
2321                                        FrontendOpts.ShowMacrosInCodeCompletion,
2322                                FrontendOpts.ShowCodePatternsInCodeCompletion,
2323                                FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2324  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2325
2326  // If we have a precompiled preamble, try to use it. We only allow
2327  // the use of the precompiled preamble if we're if the completion
2328  // point is within the main file, after the end of the precompiled
2329  // preamble.
2330  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2331  if (!getPreambleFile(this).empty()) {
2332    using llvm::sys::FileStatus;
2333    llvm::sys::PathWithStatus CompleteFilePath(File);
2334    llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2335    if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2336      if (const FileStatus *MainStatus = MainPath.getFileStatus())
2337        if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() &&
2338            Line > 1)
2339          OverrideMainBuffer
2340            = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2341                                                   Line - 1);
2342  }
2343
2344  // If the main file has been overridden due to the use of a preamble,
2345  // make that override happen and introduce the preamble.
2346  PreprocessorOpts.DisableStatCache = true;
2347  StoredDiagnostics.insert(StoredDiagnostics.end(),
2348                           stored_diag_begin(),
2349                           stored_diag_afterDriver_begin());
2350  if (OverrideMainBuffer) {
2351    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2352    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2353    PreprocessorOpts.PrecompiledPreambleBytes.second
2354                                                    = PreambleEndsAtStartOfLine;
2355    PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2356    PreprocessorOpts.DisablePCHValidation = true;
2357
2358    OwnedBuffers.push_back(OverrideMainBuffer);
2359  } else {
2360    PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2361    PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2362  }
2363
2364  // Disable the preprocessing record
2365  PreprocessorOpts.DetailedRecord = false;
2366
2367  llvm::OwningPtr<SyntaxOnlyAction> Act;
2368  Act.reset(new SyntaxOnlyAction);
2369  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2370                           Clang->getFrontendOpts().Inputs[0].first)) {
2371    if (OverrideMainBuffer) {
2372      std::string ModName = getPreambleFile(this);
2373      TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
2374                                 getSourceManager(), PreambleDiagnostics,
2375                                 StoredDiagnostics);
2376    }
2377    Act->Execute();
2378    Act->EndSourceFile();
2379  }
2380}
2381
2382CXSaveError ASTUnit::Save(StringRef File) {
2383  if (getDiagnostics().hasUnrecoverableErrorOccurred())
2384    return CXSaveError_TranslationErrors;
2385
2386  // Write to a temporary file and later rename it to the actual file, to avoid
2387  // possible race conditions.
2388  llvm::SmallString<128> TempPath;
2389  TempPath = File;
2390  TempPath += "-%%%%%%%%";
2391  int fd;
2392  if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
2393                                 /*makeAbsolute=*/false))
2394    return CXSaveError_Unknown;
2395
2396  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2397  // unconditionally create a stat cache when we parse the file?
2398  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2399
2400  serialize(Out);
2401  Out.close();
2402  if (Out.has_error())
2403    return CXSaveError_Unknown;
2404
2405  if (llvm::sys::fs::rename(TempPath.str(), File)) {
2406    bool exists;
2407    llvm::sys::fs::remove(TempPath.str(), exists);
2408    return CXSaveError_Unknown;
2409  }
2410
2411  return CXSaveError_None;
2412}
2413
2414bool ASTUnit::serialize(raw_ostream &OS) {
2415  if (getDiagnostics().hasErrorOccurred())
2416    return true;
2417
2418  std::vector<unsigned char> Buffer;
2419  llvm::BitstreamWriter Stream(Buffer);
2420  ASTWriter Writer(Stream);
2421  // FIXME: Handle modules
2422  Writer.WriteAST(getSema(), 0, std::string(), 0, "");
2423
2424  // Write the generated bitstream to "Out".
2425  if (!Buffer.empty())
2426    OS.write((char *)&Buffer.front(), Buffer.size());
2427
2428  return false;
2429}
2430
2431typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2432
2433static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2434  unsigned Raw = L.getRawEncoding();
2435  const unsigned MacroBit = 1U << 31;
2436  L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2437      ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2438}
2439
2440void ASTUnit::TranslateStoredDiagnostics(
2441                          ASTReader *MMan,
2442                          StringRef ModName,
2443                          SourceManager &SrcMgr,
2444                          const SmallVectorImpl<StoredDiagnostic> &Diags,
2445                          SmallVectorImpl<StoredDiagnostic> &Out) {
2446  // The stored diagnostic has the old source manager in it; update
2447  // the locations to refer into the new source manager. We also need to remap
2448  // all the locations to the new view. This includes the diag location, any
2449  // associated source ranges, and the source ranges of associated fix-its.
2450  // FIXME: There should be a cleaner way to do this.
2451
2452  SmallVector<StoredDiagnostic, 4> Result;
2453  Result.reserve(Diags.size());
2454  assert(MMan && "Don't have a module manager");
2455  serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
2456  assert(Mod && "Don't have preamble module");
2457  SLocRemap &Remap = Mod->SLocRemap;
2458  for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2459    // Rebuild the StoredDiagnostic.
2460    const StoredDiagnostic &SD = Diags[I];
2461    SourceLocation L = SD.getLocation();
2462    TranslateSLoc(L, Remap);
2463    FullSourceLoc Loc(L, SrcMgr);
2464
2465    SmallVector<CharSourceRange, 4> Ranges;
2466    Ranges.reserve(SD.range_size());
2467    for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2468                                          E = SD.range_end();
2469         I != E; ++I) {
2470      SourceLocation BL = I->getBegin();
2471      TranslateSLoc(BL, Remap);
2472      SourceLocation EL = I->getEnd();
2473      TranslateSLoc(EL, Remap);
2474      Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2475    }
2476
2477    SmallVector<FixItHint, 2> FixIts;
2478    FixIts.reserve(SD.fixit_size());
2479    for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2480                                          E = SD.fixit_end();
2481         I != E; ++I) {
2482      FixIts.push_back(FixItHint());
2483      FixItHint &FH = FixIts.back();
2484      FH.CodeToInsert = I->CodeToInsert;
2485      SourceLocation BL = I->RemoveRange.getBegin();
2486      TranslateSLoc(BL, Remap);
2487      SourceLocation EL = I->RemoveRange.getEnd();
2488      TranslateSLoc(EL, Remap);
2489      FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2490                                       I->RemoveRange.isTokenRange());
2491    }
2492
2493    Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2494                                      SD.getMessage(), Loc, Ranges, FixIts));
2495  }
2496  Result.swap(Out);
2497}
2498
2499static inline bool compLocDecl(std::pair<unsigned, Decl *> L,
2500                               std::pair<unsigned, Decl *> R) {
2501  return L.first < R.first;
2502}
2503
2504void ASTUnit::addFileLevelDecl(Decl *D) {
2505  assert(D);
2506
2507  // We only care about local declarations.
2508  if (D->isFromASTFile())
2509    return;
2510
2511  SourceManager &SM = *SourceMgr;
2512  SourceLocation Loc = D->getLocation();
2513  if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2514    return;
2515
2516  // We only keep track of the file-level declarations of each file.
2517  if (!D->getLexicalDeclContext()->isFileContext())
2518    return;
2519
2520  SourceLocation FileLoc = SM.getFileLoc(Loc);
2521  assert(SM.isLocalSourceLocation(FileLoc));
2522  FileID FID;
2523  unsigned Offset;
2524  llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2525  if (FID.isInvalid())
2526    return;
2527
2528  LocDeclsTy *&Decls = FileDecls[FID];
2529  if (!Decls)
2530    Decls = new LocDeclsTy();
2531
2532  std::pair<unsigned, Decl *> LocDecl(Offset, D);
2533
2534  if (Decls->empty() || Decls->back().first <= Offset) {
2535    Decls->push_back(LocDecl);
2536    return;
2537  }
2538
2539  LocDeclsTy::iterator
2540    I = std::upper_bound(Decls->begin(), Decls->end(), LocDecl, compLocDecl);
2541
2542  Decls->insert(I, LocDecl);
2543}
2544
2545void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2546                                  SmallVectorImpl<Decl *> &Decls) {
2547  if (File.isInvalid())
2548    return;
2549
2550  if (SourceMgr->isLoadedFileID(File)) {
2551    assert(Ctx->getExternalSource() && "No external source!");
2552    return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2553                                                         Decls);
2554  }
2555
2556  FileDeclsTy::iterator I = FileDecls.find(File);
2557  if (I == FileDecls.end())
2558    return;
2559
2560  LocDeclsTy &LocDecls = *I->second;
2561  if (LocDecls.empty())
2562    return;
2563
2564  LocDeclsTy::iterator
2565    BeginIt = std::lower_bound(LocDecls.begin(), LocDecls.end(),
2566                               std::make_pair(Offset, (Decl*)0), compLocDecl);
2567  if (BeginIt != LocDecls.begin())
2568    --BeginIt;
2569
2570  // If we are pointing at a top-level decl inside an objc container, we need
2571  // to backtrack until we find it otherwise we will fail to report that the
2572  // region overlaps with an objc container.
2573  while (BeginIt != LocDecls.begin() &&
2574         BeginIt->second->isTopLevelDeclInObjCContainer())
2575    --BeginIt;
2576
2577  LocDeclsTy::iterator
2578    EndIt = std::upper_bound(LocDecls.begin(), LocDecls.end(),
2579                             std::make_pair(Offset+Length, (Decl*)0),
2580                             compLocDecl);
2581  if (EndIt != LocDecls.end())
2582    ++EndIt;
2583
2584  for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2585    Decls.push_back(DIt->second);
2586}
2587
2588SourceLocation ASTUnit::getLocation(const FileEntry *File,
2589                                    unsigned Line, unsigned Col) const {
2590  const SourceManager &SM = getSourceManager();
2591  SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2592  return SM.getMacroArgExpandedLocation(Loc);
2593}
2594
2595SourceLocation ASTUnit::getLocation(const FileEntry *File,
2596                                    unsigned Offset) const {
2597  const SourceManager &SM = getSourceManager();
2598  SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2599  return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2600}
2601
2602/// \brief If \arg Loc is a loaded location from the preamble, returns
2603/// the corresponding local location of the main file, otherwise it returns
2604/// \arg Loc.
2605SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2606  FileID PreambleID;
2607  if (SourceMgr)
2608    PreambleID = SourceMgr->getPreambleFileID();
2609
2610  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2611    return Loc;
2612
2613  unsigned Offs;
2614  if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2615    SourceLocation FileLoc
2616        = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2617    return FileLoc.getLocWithOffset(Offs);
2618  }
2619
2620  return Loc;
2621}
2622
2623/// \brief If \arg Loc is a local location of the main file but inside the
2624/// preamble chunk, returns the corresponding loaded location from the
2625/// preamble, otherwise it returns \arg Loc.
2626SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2627  FileID PreambleID;
2628  if (SourceMgr)
2629    PreambleID = SourceMgr->getPreambleFileID();
2630
2631  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2632    return Loc;
2633
2634  unsigned Offs;
2635  if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2636      Offs < Preamble.size()) {
2637    SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2638    return FileLoc.getLocWithOffset(Offs);
2639  }
2640
2641  return Loc;
2642}
2643
2644bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2645  FileID FID;
2646  if (SourceMgr)
2647    FID = SourceMgr->getPreambleFileID();
2648
2649  if (Loc.isInvalid() || FID.isInvalid())
2650    return false;
2651
2652  return SourceMgr->isInFileID(Loc, FID);
2653}
2654
2655bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2656  FileID FID;
2657  if (SourceMgr)
2658    FID = SourceMgr->getMainFileID();
2659
2660  if (Loc.isInvalid() || FID.isInvalid())
2661    return false;
2662
2663  return SourceMgr->isInFileID(Loc, FID);
2664}
2665
2666SourceLocation ASTUnit::getEndOfPreambleFileID() {
2667  FileID FID;
2668  if (SourceMgr)
2669    FID = SourceMgr->getPreambleFileID();
2670
2671  if (FID.isInvalid())
2672    return SourceLocation();
2673
2674  return SourceMgr->getLocForEndOfFile(FID);
2675}
2676
2677SourceLocation ASTUnit::getStartOfMainFileID() {
2678  FileID FID;
2679  if (SourceMgr)
2680    FID = SourceMgr->getMainFileID();
2681
2682  if (FID.isInvalid())
2683    return SourceLocation();
2684
2685  return SourceMgr->getLocForStartOfFile(FID);
2686}
2687
2688void ASTUnit::PreambleData::countLines() const {
2689  NumLines = 0;
2690  if (empty())
2691    return;
2692
2693  for (std::vector<char>::const_iterator
2694         I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2695    if (*I == '\n')
2696      ++NumLines;
2697  }
2698  if (Buffer.back() != '\n')
2699    ++NumLines;
2700}
2701
2702#ifndef NDEBUG
2703ASTUnit::ConcurrencyState::ConcurrencyState() {
2704  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2705}
2706
2707ASTUnit::ConcurrencyState::~ConcurrencyState() {
2708  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2709}
2710
2711void ASTUnit::ConcurrencyState::start() {
2712  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2713  assert(acquired && "Concurrent access to ASTUnit!");
2714}
2715
2716void ASTUnit::ConcurrencyState::finish() {
2717  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2718}
2719
2720#else // NDEBUG
2721
2722ASTUnit::ConcurrencyState::ConcurrencyState() {}
2723ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2724void ASTUnit::ConcurrencyState::start() {}
2725void ASTUnit::ConcurrencyState::finish() {}
2726
2727#endif
2728