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