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