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