ASTUnit.cpp revision f0c06a3fa424a7bf858d95f6e83c392dbb31ea43
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<std::string, 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    llvm::sys::fs::remove(TemporaryFiles[I]);
169  TemporaryFiles.clear();
170}
171
172void OnDiskData::CleanPreambleFile() {
173  if (!PreambleFile.empty()) {
174    llvm::sys::fs::remove(PreambleFile);
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(StringRef 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  if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) {
889    if (Module *Mod = ImportD->getImportedModule()) {
890      std::string ModName = Mod->getFullModuleName();
891      Hash = llvm::HashString(ModName, Hash);
892    }
893    return;
894  }
895}
896
897class TopLevelDeclTrackerConsumer : public ASTConsumer {
898  ASTUnit &Unit;
899  unsigned &Hash;
900
901public:
902  TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash)
903    : Unit(_Unit), Hash(Hash) {
904    Hash = 0;
905  }
906
907  void handleTopLevelDecl(Decl *D) {
908    if (!D)
909      return;
910
911    // FIXME: Currently ObjC method declarations are incorrectly being
912    // reported as top-level declarations, even though their DeclContext
913    // is the containing ObjC @interface/@implementation.  This is a
914    // fundamental problem in the parser right now.
915    if (isa<ObjCMethodDecl>(D))
916      return;
917
918    AddTopLevelDeclarationToHash(D, Hash);
919    Unit.addTopLevelDecl(D);
920
921    handleFileLevelDecl(D);
922  }
923
924  void handleFileLevelDecl(Decl *D) {
925    Unit.addFileLevelDecl(D);
926    if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) {
927      for (NamespaceDecl::decl_iterator
928             I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I)
929        handleFileLevelDecl(*I);
930    }
931  }
932
933  bool HandleTopLevelDecl(DeclGroupRef D) {
934    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
935      handleTopLevelDecl(*it);
936    return true;
937  }
938
939  // We're not interested in "interesting" decls.
940  void HandleInterestingDecl(DeclGroupRef) {}
941
942  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
943    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
944      handleTopLevelDecl(*it);
945  }
946
947  virtual ASTMutationListener *GetASTMutationListener() {
948    return Unit.getASTMutationListener();
949  }
950
951  virtual ASTDeserializationListener *GetASTDeserializationListener() {
952    return Unit.getDeserializationListener();
953  }
954};
955
956class TopLevelDeclTrackerAction : public ASTFrontendAction {
957public:
958  ASTUnit &Unit;
959
960  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
961                                         StringRef InFile) {
962    CI.getPreprocessor().addPPCallbacks(
963     new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
964    return new TopLevelDeclTrackerConsumer(Unit,
965                                           Unit.getCurrentTopLevelHashValue());
966  }
967
968public:
969  TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
970
971  virtual bool hasCodeCompletionSupport() const { return false; }
972  virtual TranslationUnitKind getTranslationUnitKind()  {
973    return Unit.getTranslationUnitKind();
974  }
975};
976
977class PrecompilePreambleAction : public ASTFrontendAction {
978  ASTUnit &Unit;
979  bool HasEmittedPreamblePCH;
980
981public:
982  explicit PrecompilePreambleAction(ASTUnit &Unit)
983      : Unit(Unit), HasEmittedPreamblePCH(false) {}
984
985  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
986                                         StringRef InFile);
987  bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; }
988  void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; }
989  virtual bool shouldEraseOutputFiles() { return !hasEmittedPreamblePCH(); }
990
991  virtual bool hasCodeCompletionSupport() const { return false; }
992  virtual bool hasASTFileSupport() const { return false; }
993  virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
994};
995
996class PrecompilePreambleConsumer : public PCHGenerator {
997  ASTUnit &Unit;
998  unsigned &Hash;
999  std::vector<Decl *> TopLevelDecls;
1000  PrecompilePreambleAction *Action;
1001
1002public:
1003  PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action,
1004                             const Preprocessor &PP, StringRef isysroot,
1005                             raw_ostream *Out)
1006    : PCHGenerator(PP, "", 0, isysroot, Out, /*AllowASTWithErrors=*/true),
1007      Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action) {
1008    Hash = 0;
1009  }
1010
1011  virtual bool HandleTopLevelDecl(DeclGroupRef D) {
1012    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
1013      Decl *D = *it;
1014      // FIXME: Currently ObjC method declarations are incorrectly being
1015      // reported as top-level declarations, even though their DeclContext
1016      // is the containing ObjC @interface/@implementation.  This is a
1017      // fundamental problem in the parser right now.
1018      if (isa<ObjCMethodDecl>(D))
1019        continue;
1020      AddTopLevelDeclarationToHash(D, Hash);
1021      TopLevelDecls.push_back(D);
1022    }
1023    return true;
1024  }
1025
1026  virtual void HandleTranslationUnit(ASTContext &Ctx) {
1027    PCHGenerator::HandleTranslationUnit(Ctx);
1028    if (hasEmittedPCH()) {
1029      // Translate the top-level declarations we captured during
1030      // parsing into declaration IDs in the precompiled
1031      // preamble. This will allow us to deserialize those top-level
1032      // declarations when requested.
1033      for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
1034        Unit.addTopLevelDeclFromPreamble(
1035                                      getWriter().getDeclID(TopLevelDecls[I]));
1036
1037      Action->setHasEmittedPreamblePCH();
1038    }
1039  }
1040};
1041
1042}
1043
1044ASTConsumer *PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI,
1045                                                         StringRef InFile) {
1046  std::string Sysroot;
1047  std::string OutputFile;
1048  raw_ostream *OS = 0;
1049  if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
1050                                                     OutputFile, OS))
1051    return 0;
1052
1053  if (!CI.getFrontendOpts().RelocatablePCH)
1054    Sysroot.clear();
1055
1056  CI.getPreprocessor().addPPCallbacks(new MacroDefinitionTrackerPPCallbacks(
1057      Unit.getCurrentTopLevelHashValue()));
1058  return new PrecompilePreambleConsumer(Unit, this, CI.getPreprocessor(),
1059                                        Sysroot, OS);
1060}
1061
1062static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) {
1063  return StoredDiag.getLocation().isValid();
1064}
1065
1066static void
1067checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) {
1068  // Get rid of stored diagnostics except the ones from the driver which do not
1069  // have a source location.
1070  StoredDiags.erase(
1071      std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag),
1072      StoredDiags.end());
1073}
1074
1075static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> &
1076                                                              StoredDiagnostics,
1077                                  SourceManager &SM) {
1078  // The stored diagnostic has the old source manager in it; update
1079  // the locations to refer into the new source manager. Since we've
1080  // been careful to make sure that the source manager's state
1081  // before and after are identical, so that we can reuse the source
1082  // location itself.
1083  for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) {
1084    if (StoredDiagnostics[I].getLocation().isValid()) {
1085      FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM);
1086      StoredDiagnostics[I].setLocation(Loc);
1087    }
1088  }
1089}
1090
1091/// Parse the source file into a translation unit using the given compiler
1092/// invocation, replacing the current translation unit.
1093///
1094/// \returns True if a failure occurred that causes the ASTUnit not to
1095/// contain any translation-unit information, false otherwise.
1096bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
1097  delete SavedMainFileBuffer;
1098  SavedMainFileBuffer = 0;
1099
1100  if (!Invocation) {
1101    delete OverrideMainBuffer;
1102    return true;
1103  }
1104
1105  // Create the compiler instance to use for building the AST.
1106  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1107
1108  // Recover resources if we crash before exiting this method.
1109  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1110    CICleanup(Clang.get());
1111
1112  IntrusiveRefCntPtr<CompilerInvocation>
1113    CCInvocation(new CompilerInvocation(*Invocation));
1114
1115  Clang->setInvocation(CCInvocation.getPtr());
1116  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1117
1118  // Set up diagnostics, capturing any diagnostics that would
1119  // otherwise be dropped.
1120  Clang->setDiagnostics(&getDiagnostics());
1121
1122  // Create the target instance.
1123  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1124                   &Clang->getTargetOpts()));
1125  if (!Clang->hasTarget()) {
1126    delete OverrideMainBuffer;
1127    return true;
1128  }
1129
1130  // Inform the target of the language options.
1131  //
1132  // FIXME: We shouldn't need to do this, the target should be immutable once
1133  // created. This complexity should be lifted elsewhere.
1134  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1135
1136  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1137         "Invocation must have exactly one source file!");
1138  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1139         "FIXME: AST inputs not yet supported here!");
1140  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1141         "IR inputs not support here!");
1142
1143  // Configure the various subsystems.
1144  // FIXME: Should we retain the previous file manager?
1145  LangOpts = &Clang->getLangOpts();
1146  FileSystemOpts = Clang->getFileSystemOpts();
1147  FileMgr = new FileManager(FileSystemOpts);
1148  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr,
1149                                UserFilesAreVolatile);
1150  TheSema.reset();
1151  Ctx = 0;
1152  PP = 0;
1153  Reader = 0;
1154
1155  // Clear out old caches and data.
1156  TopLevelDecls.clear();
1157  clearFileLevelDecls();
1158  CleanTemporaryFiles();
1159
1160  if (!OverrideMainBuffer) {
1161    checkAndRemoveNonDriverDiags(StoredDiagnostics);
1162    TopLevelDeclsInPreamble.clear();
1163  }
1164
1165  // Create a file manager object to provide access to and cache the filesystem.
1166  Clang->setFileManager(&getFileManager());
1167
1168  // Create the source manager.
1169  Clang->setSourceManager(&getSourceManager());
1170
1171  // If the main file has been overridden due to the use of a preamble,
1172  // make that override happen and introduce the preamble.
1173  PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
1174  if (OverrideMainBuffer) {
1175    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
1176    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
1177    PreprocessorOpts.PrecompiledPreambleBytes.second
1178                                                    = PreambleEndsAtStartOfLine;
1179    PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
1180    PreprocessorOpts.DisablePCHValidation = true;
1181
1182    // The stored diagnostic has the old source manager in it; update
1183    // the locations to refer into the new source manager. Since we've
1184    // been careful to make sure that the source manager's state
1185    // before and after are identical, so that we can reuse the source
1186    // location itself.
1187    checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
1188
1189    // Keep track of the override buffer;
1190    SavedMainFileBuffer = OverrideMainBuffer;
1191  }
1192
1193  OwningPtr<TopLevelDeclTrackerAction> Act(
1194    new TopLevelDeclTrackerAction(*this));
1195
1196  // Recover resources if we crash before exiting this method.
1197  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1198    ActCleanup(Act.get());
1199
1200  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
1201    goto error;
1202
1203  if (OverrideMainBuffer) {
1204    std::string ModName = getPreambleFile(this);
1205    TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
1206                               getSourceManager(), PreambleDiagnostics,
1207                               StoredDiagnostics);
1208  }
1209
1210  if (!Act->Execute())
1211    goto error;
1212
1213  transferASTDataFromCompilerInstance(*Clang);
1214
1215  Act->EndSourceFile();
1216
1217  FailedParseDiagnostics.clear();
1218
1219  return false;
1220
1221error:
1222  // Remove the overridden buffer we used for the preamble.
1223  if (OverrideMainBuffer) {
1224    delete OverrideMainBuffer;
1225    SavedMainFileBuffer = 0;
1226  }
1227
1228  // Keep the ownership of the data in the ASTUnit because the client may
1229  // want to see the diagnostics.
1230  transferASTDataFromCompilerInstance(*Clang);
1231  FailedParseDiagnostics.swap(StoredDiagnostics);
1232  StoredDiagnostics.clear();
1233  NumStoredDiagnosticsFromDriver = 0;
1234  return true;
1235}
1236
1237/// \brief Simple function to retrieve a path for a preamble precompiled header.
1238static std::string GetPreamblePCHPath() {
1239  // FIXME: This is a hack so that we can override the preamble file during
1240  // crash-recovery testing, which is the only case where the preamble files
1241  // are not necessarily cleaned up.
1242  const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1243  if (TmpFile)
1244    return TmpFile;
1245
1246  SmallString<128> Path;
1247  llvm::sys::fs::createTemporaryFile("preamble", "pch", Path);
1248
1249  return Path.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  std::string MainFilePath(FrontendOpts.Inputs[0].getFile());
1267  uint64_t MainFileID;
1268  if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) {
1269    // Check whether there is a file-file remapping of the main file
1270    for (PreprocessorOptions::remapped_file_iterator
1271          M = PreprocessorOpts.remapped_file_begin(),
1272          E = PreprocessorOpts.remapped_file_end();
1273         M != E;
1274         ++M) {
1275      std::string MPath(M->first);
1276      uint64_t MID;
1277      if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1278        if (MainFileID == MID) {
1279          // We found a remapping. Try to load the resulting, remapped source.
1280          if (CreatedBuffer) {
1281            delete Buffer;
1282            CreatedBuffer = false;
1283          }
1284
1285          Buffer = getBufferForFile(M->second);
1286          if (!Buffer)
1287            return std::make_pair((llvm::MemoryBuffer*)0,
1288                                  std::make_pair(0, true));
1289          CreatedBuffer = true;
1290        }
1291      }
1292    }
1293
1294    // Check whether there is a file-buffer remapping. It supercedes the
1295    // file-file remapping.
1296    for (PreprocessorOptions::remapped_file_buffer_iterator
1297           M = PreprocessorOpts.remapped_file_buffer_begin(),
1298           E = PreprocessorOpts.remapped_file_buffer_end();
1299         M != E;
1300         ++M) {
1301      std::string MPath(M->first);
1302      uint64_t MID;
1303      if (!llvm::sys::fs::getUniqueID(MPath, MID)) {
1304        if (MainFileID == MID) {
1305          // We found a remapping.
1306          if (CreatedBuffer) {
1307            delete Buffer;
1308            CreatedBuffer = false;
1309          }
1310
1311          Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1312        }
1313      }
1314    }
1315  }
1316
1317  // If the main source file was not remapped, load it now.
1318  if (!Buffer) {
1319    Buffer = getBufferForFile(FrontendOpts.Inputs[0].getFile());
1320    if (!Buffer)
1321      return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1322
1323    CreatedBuffer = true;
1324  }
1325
1326  return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1327                                                       *Invocation.getLangOpts(),
1328                                                       MaxLines));
1329}
1330
1331static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old,
1332                                                      unsigned NewSize,
1333                                                      StringRef NewName) {
1334  llvm::MemoryBuffer *Result
1335    = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName);
1336  memcpy(const_cast<char*>(Result->getBufferStart()),
1337         Old->getBufferStart(), Old->getBufferSize());
1338  memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(),
1339         ' ', NewSize - Old->getBufferSize() - 1);
1340  const_cast<char*>(Result->getBufferEnd())[-1] = '\n';
1341
1342  return Result;
1343}
1344
1345/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing
1346/// the source file.
1347///
1348/// This routine will compute the preamble of the main source file. If a
1349/// non-trivial preamble is found, it will precompile that preamble into a
1350/// precompiled header so that the precompiled preamble can be used to reduce
1351/// reparsing time. If a precompiled preamble has already been constructed,
1352/// this routine will determine if it is still valid and, if so, avoid
1353/// rebuilding the precompiled preamble.
1354///
1355/// \param AllowRebuild When true (the default), this routine is
1356/// allowed to rebuild the precompiled preamble if it is found to be
1357/// out-of-date.
1358///
1359/// \param MaxLines When non-zero, the maximum number of lines that
1360/// can occur within the preamble.
1361///
1362/// \returns If the precompiled preamble can be used, returns a newly-allocated
1363/// buffer that should be used in place of the main file when doing so.
1364/// Otherwise, returns a NULL pointer.
1365llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble(
1366                              const CompilerInvocation &PreambleInvocationIn,
1367                                                           bool AllowRebuild,
1368                                                           unsigned MaxLines) {
1369
1370  IntrusiveRefCntPtr<CompilerInvocation>
1371    PreambleInvocation(new CompilerInvocation(PreambleInvocationIn));
1372  FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts();
1373  PreprocessorOptions &PreprocessorOpts
1374    = PreambleInvocation->getPreprocessorOpts();
1375
1376  bool CreatedPreambleBuffer = false;
1377  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble
1378    = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer);
1379
1380  // If ComputePreamble() Take ownership of the preamble buffer.
1381  OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer;
1382  if (CreatedPreambleBuffer)
1383    OwnedPreambleBuffer.reset(NewPreamble.first);
1384
1385  if (!NewPreamble.second.first) {
1386    // We couldn't find a preamble in the main source. Clear out the current
1387    // preamble, if we have one. It's obviously no good any more.
1388    Preamble.clear();
1389    erasePreambleFile(this);
1390
1391    // The next time we actually see a preamble, precompile it.
1392    PreambleRebuildCounter = 1;
1393    return 0;
1394  }
1395
1396  if (!Preamble.empty()) {
1397    // We've previously computed a preamble. Check whether we have the same
1398    // preamble now that we did before, and that there's enough space in
1399    // the main-file buffer within the precompiled preamble to fit the
1400    // new main file.
1401    if (Preamble.size() == NewPreamble.second.first &&
1402        PreambleEndsAtStartOfLine == NewPreamble.second.second &&
1403        NewPreamble.first->getBufferSize() < PreambleReservedSize-2 &&
1404        memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(),
1405               NewPreamble.second.first) == 0) {
1406      // The preamble has not changed. We may be able to re-use the precompiled
1407      // preamble.
1408
1409      // Check that none of the files used by the preamble have changed.
1410      bool AnyFileChanged = false;
1411
1412      // First, make a record of those files that have been overridden via
1413      // remapping or unsaved_files.
1414      llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles;
1415      for (PreprocessorOptions::remapped_file_iterator
1416                R = PreprocessorOpts.remapped_file_begin(),
1417             REnd = PreprocessorOpts.remapped_file_end();
1418           !AnyFileChanged && R != REnd;
1419           ++R) {
1420        struct stat StatBuf;
1421        if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) {
1422          // If we can't stat the file we're remapping to, assume that something
1423          // horrible happened.
1424          AnyFileChanged = true;
1425          break;
1426        }
1427
1428        OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size,
1429                                                   StatBuf.st_mtime);
1430      }
1431      for (PreprocessorOptions::remapped_file_buffer_iterator
1432                R = PreprocessorOpts.remapped_file_buffer_begin(),
1433             REnd = PreprocessorOpts.remapped_file_buffer_end();
1434           !AnyFileChanged && R != REnd;
1435           ++R) {
1436        // FIXME: Should we actually compare the contents of file->buffer
1437        // remappings?
1438        OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(),
1439                                                   0);
1440      }
1441
1442      // Check whether anything has changed.
1443      for (llvm::StringMap<std::pair<off_t, time_t> >::iterator
1444             F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end();
1445           !AnyFileChanged && F != FEnd;
1446           ++F) {
1447        llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden
1448          = OverriddenFiles.find(F->first());
1449        if (Overridden != OverriddenFiles.end()) {
1450          // This file was remapped; check whether the newly-mapped file
1451          // matches up with the previous mapping.
1452          if (Overridden->second != F->second)
1453            AnyFileChanged = true;
1454          continue;
1455        }
1456
1457        // The file was not remapped; check whether it has changed on disk.
1458        struct stat StatBuf;
1459        if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) {
1460          // If we can't stat the file, assume that something horrible happened.
1461          AnyFileChanged = true;
1462        } else if (StatBuf.st_size != F->second.first ||
1463                   StatBuf.st_mtime != F->second.second)
1464          AnyFileChanged = true;
1465      }
1466
1467      if (!AnyFileChanged) {
1468        // Okay! We can re-use the precompiled preamble.
1469
1470        // Set the state of the diagnostic object to mimic its state
1471        // after parsing the preamble.
1472        getDiagnostics().Reset();
1473        ProcessWarningOptions(getDiagnostics(),
1474                              PreambleInvocation->getDiagnosticOpts());
1475        getDiagnostics().setNumWarnings(NumWarningsInPreamble);
1476
1477        // Create a version of the main file buffer that is padded to
1478        // buffer size we reserved when creating the preamble.
1479        return CreatePaddedMainFileBuffer(NewPreamble.first,
1480                                          PreambleReservedSize,
1481                                          FrontendOpts.Inputs[0].getFile());
1482      }
1483    }
1484
1485    // If we aren't allowed to rebuild the precompiled preamble, just
1486    // return now.
1487    if (!AllowRebuild)
1488      return 0;
1489
1490    // We can't reuse the previously-computed preamble. Build a new one.
1491    Preamble.clear();
1492    PreambleDiagnostics.clear();
1493    erasePreambleFile(this);
1494    PreambleRebuildCounter = 1;
1495  } else if (!AllowRebuild) {
1496    // We aren't allowed to rebuild the precompiled preamble; just
1497    // return now.
1498    return 0;
1499  }
1500
1501  // If the preamble rebuild counter > 1, it's because we previously
1502  // failed to build a preamble and we're not yet ready to try
1503  // again. Decrement the counter and return a failure.
1504  if (PreambleRebuildCounter > 1) {
1505    --PreambleRebuildCounter;
1506    return 0;
1507  }
1508
1509  // Create a temporary file for the precompiled preamble. In rare
1510  // circumstances, this can fail.
1511  std::string PreamblePCHPath = GetPreamblePCHPath();
1512  if (PreamblePCHPath.empty()) {
1513    // Try again next time.
1514    PreambleRebuildCounter = 1;
1515    return 0;
1516  }
1517
1518  // We did not previously compute a preamble, or it can't be reused anyway.
1519  SimpleTimer PreambleTimer(WantTiming);
1520  PreambleTimer.setOutput("Precompiling preamble");
1521
1522  // Create a new buffer that stores the preamble. The buffer also contains
1523  // extra space for the original contents of the file (which will be present
1524  // when we actually parse the file) along with more room in case the file
1525  // grows.
1526  PreambleReservedSize = NewPreamble.first->getBufferSize();
1527  if (PreambleReservedSize < 4096)
1528    PreambleReservedSize = 8191;
1529  else
1530    PreambleReservedSize *= 2;
1531
1532  // Save the preamble text for later; we'll need to compare against it for
1533  // subsequent reparses.
1534  StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].getFile();
1535  Preamble.assign(FileMgr->getFile(MainFilename),
1536                  NewPreamble.first->getBufferStart(),
1537                  NewPreamble.first->getBufferStart()
1538                                                  + NewPreamble.second.first);
1539  PreambleEndsAtStartOfLine = NewPreamble.second.second;
1540
1541  delete PreambleBuffer;
1542  PreambleBuffer
1543    = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize,
1544                                                FrontendOpts.Inputs[0].getFile());
1545  memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()),
1546         NewPreamble.first->getBufferStart(), Preamble.size());
1547  memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(),
1548         ' ', PreambleReservedSize - Preamble.size() - 1);
1549  const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n';
1550
1551  // Remap the main source file to the preamble buffer.
1552  StringRef MainFilePath = FrontendOpts.Inputs[0].getFile();
1553  PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer);
1554
1555  // Tell the compiler invocation to generate a temporary precompiled header.
1556  FrontendOpts.ProgramAction = frontend::GeneratePCH;
1557  // FIXME: Generate the precompiled header into memory?
1558  FrontendOpts.OutputFile = PreamblePCHPath;
1559  PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
1560  PreprocessorOpts.PrecompiledPreambleBytes.second = false;
1561
1562  // Create the compiler instance to use for building the precompiled preamble.
1563  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1564
1565  // Recover resources if we crash before exiting this method.
1566  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1567    CICleanup(Clang.get());
1568
1569  Clang->setInvocation(&*PreambleInvocation);
1570  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1571
1572  // Set up diagnostics, capturing all of the diagnostics produced.
1573  Clang->setDiagnostics(&getDiagnostics());
1574
1575  // Create the target instance.
1576  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1577                                                &Clang->getTargetOpts()));
1578  if (!Clang->hasTarget()) {
1579    llvm::sys::fs::remove(FrontendOpts.OutputFile);
1580    Preamble.clear();
1581    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1582    PreprocessorOpts.eraseRemappedFile(
1583                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1584    return 0;
1585  }
1586
1587  // Inform the target of the language options.
1588  //
1589  // FIXME: We shouldn't need to do this, the target should be immutable once
1590  // created. This complexity should be lifted elsewhere.
1591  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1592
1593  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1594         "Invocation must have exactly one source file!");
1595  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1596         "FIXME: AST inputs not yet supported here!");
1597  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1598         "IR inputs not support here!");
1599
1600  // Clear out old caches and data.
1601  getDiagnostics().Reset();
1602  ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts());
1603  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1604  TopLevelDecls.clear();
1605  TopLevelDeclsInPreamble.clear();
1606
1607  // Create a file manager object to provide access to and cache the filesystem.
1608  Clang->setFileManager(new FileManager(Clang->getFileSystemOpts()));
1609
1610  // Create the source manager.
1611  Clang->setSourceManager(new SourceManager(getDiagnostics(),
1612                                            Clang->getFileManager()));
1613
1614  OwningPtr<PrecompilePreambleAction> Act;
1615  Act.reset(new PrecompilePreambleAction(*this));
1616  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1617    llvm::sys::fs::remove(FrontendOpts.OutputFile);
1618    Preamble.clear();
1619    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1620    PreprocessorOpts.eraseRemappedFile(
1621                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1622    return 0;
1623  }
1624
1625  Act->Execute();
1626  Act->EndSourceFile();
1627
1628  if (!Act->hasEmittedPreamblePCH()) {
1629    // The preamble PCH failed (e.g. there was a module loading fatal error),
1630    // so no precompiled header was generated. Forget that we even tried.
1631    // FIXME: Should we leave a note for ourselves to try again?
1632    llvm::sys::fs::remove(FrontendOpts.OutputFile);
1633    Preamble.clear();
1634    TopLevelDeclsInPreamble.clear();
1635    PreambleRebuildCounter = DefaultPreambleRebuildInterval;
1636    PreprocessorOpts.eraseRemappedFile(
1637                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1638    return 0;
1639  }
1640
1641  // Transfer any diagnostics generated when parsing the preamble into the set
1642  // of preamble diagnostics.
1643  PreambleDiagnostics.clear();
1644  PreambleDiagnostics.insert(PreambleDiagnostics.end(),
1645                            stored_diag_afterDriver_begin(), stored_diag_end());
1646  checkAndRemoveNonDriverDiags(StoredDiagnostics);
1647
1648  // Keep track of the preamble we precompiled.
1649  setPreambleFile(this, FrontendOpts.OutputFile);
1650  NumWarningsInPreamble = getDiagnostics().getNumWarnings();
1651
1652  // Keep track of all of the files that the source manager knows about,
1653  // so we can verify whether they have changed or not.
1654  FilesInPreamble.clear();
1655  SourceManager &SourceMgr = Clang->getSourceManager();
1656  const llvm::MemoryBuffer *MainFileBuffer
1657    = SourceMgr.getBuffer(SourceMgr.getMainFileID());
1658  for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(),
1659                                     FEnd = SourceMgr.fileinfo_end();
1660       F != FEnd;
1661       ++F) {
1662    const FileEntry *File = F->second->OrigEntry;
1663    if (!File || F->second->getRawBuffer() == MainFileBuffer)
1664      continue;
1665
1666    FilesInPreamble[File->getName()]
1667      = std::make_pair(F->second->getSize(), File->getModificationTime());
1668  }
1669
1670  PreambleRebuildCounter = 1;
1671  PreprocessorOpts.eraseRemappedFile(
1672                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1673
1674  // If the hash of top-level entities differs from the hash of the top-level
1675  // entities the last time we rebuilt the preamble, clear out the completion
1676  // cache.
1677  if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) {
1678    CompletionCacheTopLevelHashValue = 0;
1679    PreambleTopLevelHashValue = CurrentTopLevelHashValue;
1680  }
1681
1682  return CreatePaddedMainFileBuffer(NewPreamble.first,
1683                                    PreambleReservedSize,
1684                                    FrontendOpts.Inputs[0].getFile());
1685}
1686
1687void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
1688  std::vector<Decl *> Resolved;
1689  Resolved.reserve(TopLevelDeclsInPreamble.size());
1690  ExternalASTSource &Source = *getASTContext().getExternalSource();
1691  for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) {
1692    // Resolve the declaration ID to an actual declaration, possibly
1693    // deserializing the declaration in the process.
1694    Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]);
1695    if (D)
1696      Resolved.push_back(D);
1697  }
1698  TopLevelDeclsInPreamble.clear();
1699  TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end());
1700}
1701
1702void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) {
1703  // Steal the created target, context, and preprocessor.
1704  TheSema.reset(CI.takeSema());
1705  Consumer.reset(CI.takeASTConsumer());
1706  Ctx = &CI.getASTContext();
1707  PP = &CI.getPreprocessor();
1708  CI.setSourceManager(0);
1709  CI.setFileManager(0);
1710  Target = &CI.getTarget();
1711  Reader = CI.getModuleManager();
1712  HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure();
1713}
1714
1715StringRef ASTUnit::getMainFileName() const {
1716  if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) {
1717    const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0];
1718    if (Input.isFile())
1719      return Input.getFile();
1720    else
1721      return Input.getBuffer()->getBufferIdentifier();
1722  }
1723
1724  if (SourceMgr) {
1725    if (const FileEntry *
1726          FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1727      return FE->getName();
1728  }
1729
1730  return StringRef();
1731}
1732
1733StringRef ASTUnit::getASTFileName() const {
1734  if (!isMainFileAST())
1735    return StringRef();
1736
1737  serialization::ModuleFile &
1738    Mod = Reader->getModuleManager().getPrimaryModule();
1739  return Mod.FileName;
1740}
1741
1742ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1743                         IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1744                         bool CaptureDiagnostics,
1745                         bool UserFilesAreVolatile) {
1746  OwningPtr<ASTUnit> AST;
1747  AST.reset(new ASTUnit(false));
1748  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1749  AST->Diagnostics = Diags;
1750  AST->Invocation = CI;
1751  AST->FileSystemOpts = CI->getFileSystemOpts();
1752  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1753  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1754  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1755                                     UserFilesAreVolatile);
1756
1757  return AST.take();
1758}
1759
1760ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1761                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1762                                             ASTFrontendAction *Action,
1763                                             ASTUnit *Unit,
1764                                             bool Persistent,
1765                                             StringRef ResourceFilesPath,
1766                                             bool OnlyLocalDecls,
1767                                             bool CaptureDiagnostics,
1768                                             bool PrecompilePreamble,
1769                                             bool CacheCodeCompletionResults,
1770                                    bool IncludeBriefCommentsInCodeCompletion,
1771                                             bool UserFilesAreVolatile,
1772                                             OwningPtr<ASTUnit> *ErrAST) {
1773  assert(CI && "A CompilerInvocation is required");
1774
1775  OwningPtr<ASTUnit> OwnAST;
1776  ASTUnit *AST = Unit;
1777  if (!AST) {
1778    // Create the AST unit.
1779    OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1780    AST = OwnAST.get();
1781  }
1782
1783  if (!ResourceFilesPath.empty()) {
1784    // Override the resources path.
1785    CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1786  }
1787  AST->OnlyLocalDecls = OnlyLocalDecls;
1788  AST->CaptureDiagnostics = CaptureDiagnostics;
1789  if (PrecompilePreamble)
1790    AST->PreambleRebuildCounter = 2;
1791  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1792  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1793  AST->IncludeBriefCommentsInCodeCompletion
1794    = IncludeBriefCommentsInCodeCompletion;
1795
1796  // Recover resources if we crash before exiting this method.
1797  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1798    ASTUnitCleanup(OwnAST.get());
1799  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1800    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1801    DiagCleanup(Diags.getPtr());
1802
1803  // We'll manage file buffers ourselves.
1804  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1805  CI->getFrontendOpts().DisableFree = false;
1806  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1807
1808  // Create the compiler instance to use for building the AST.
1809  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1810
1811  // Recover resources if we crash before exiting this method.
1812  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1813    CICleanup(Clang.get());
1814
1815  Clang->setInvocation(CI);
1816  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
1817
1818  // Set up diagnostics, capturing any diagnostics that would
1819  // otherwise be dropped.
1820  Clang->setDiagnostics(&AST->getDiagnostics());
1821
1822  // Create the target instance.
1823  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1824                                                &Clang->getTargetOpts()));
1825  if (!Clang->hasTarget())
1826    return 0;
1827
1828  // Inform the target of the language options.
1829  //
1830  // FIXME: We shouldn't need to do this, the target should be immutable once
1831  // created. This complexity should be lifted elsewhere.
1832  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1833
1834  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1835         "Invocation must have exactly one source file!");
1836  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
1837         "FIXME: AST inputs not yet supported here!");
1838  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
1839         "IR inputs not supported here!");
1840
1841  // Configure the various subsystems.
1842  AST->TheSema.reset();
1843  AST->Ctx = 0;
1844  AST->PP = 0;
1845  AST->Reader = 0;
1846
1847  // Create a file manager object to provide access to and cache the filesystem.
1848  Clang->setFileManager(&AST->getFileManager());
1849
1850  // Create the source manager.
1851  Clang->setSourceManager(&AST->getSourceManager());
1852
1853  ASTFrontendAction *Act = Action;
1854
1855  OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1856  if (!Act) {
1857    TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1858    Act = TrackerAct.get();
1859  }
1860
1861  // Recover resources if we crash before exiting this method.
1862  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1863    ActCleanup(TrackerAct.get());
1864
1865  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1866    AST->transferASTDataFromCompilerInstance(*Clang);
1867    if (OwnAST && ErrAST)
1868      ErrAST->swap(OwnAST);
1869
1870    return 0;
1871  }
1872
1873  if (Persistent && !TrackerAct) {
1874    Clang->getPreprocessor().addPPCallbacks(
1875     new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
1876    std::vector<ASTConsumer*> Consumers;
1877    if (Clang->hasASTConsumer())
1878      Consumers.push_back(Clang->takeASTConsumer());
1879    Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
1880                                           AST->getCurrentTopLevelHashValue()));
1881    Clang->setASTConsumer(new MultiplexConsumer(Consumers));
1882  }
1883  if (!Act->Execute()) {
1884    AST->transferASTDataFromCompilerInstance(*Clang);
1885    if (OwnAST && ErrAST)
1886      ErrAST->swap(OwnAST);
1887
1888    return 0;
1889  }
1890
1891  // Steal the created target, context, and preprocessor.
1892  AST->transferASTDataFromCompilerInstance(*Clang);
1893
1894  Act->EndSourceFile();
1895
1896  if (OwnAST)
1897    return OwnAST.take();
1898  else
1899    return AST;
1900}
1901
1902bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1903  if (!Invocation)
1904    return true;
1905
1906  // We'll manage file buffers ourselves.
1907  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1908  Invocation->getFrontendOpts().DisableFree = false;
1909  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1910
1911  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1912  if (PrecompilePreamble) {
1913    PreambleRebuildCounter = 2;
1914    OverrideMainBuffer
1915      = getMainBufferWithPrecompiledPreamble(*Invocation);
1916  }
1917
1918  SimpleTimer ParsingTimer(WantTiming);
1919  ParsingTimer.setOutput("Parsing " + getMainFileName());
1920
1921  // Recover resources if we crash before exiting this method.
1922  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1923    MemBufferCleanup(OverrideMainBuffer);
1924
1925  return Parse(OverrideMainBuffer);
1926}
1927
1928ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1929                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1930                                             bool OnlyLocalDecls,
1931                                             bool CaptureDiagnostics,
1932                                             bool PrecompilePreamble,
1933                                             TranslationUnitKind TUKind,
1934                                             bool CacheCodeCompletionResults,
1935                                    bool IncludeBriefCommentsInCodeCompletion,
1936                                             bool UserFilesAreVolatile) {
1937  // Create the AST unit.
1938  OwningPtr<ASTUnit> AST;
1939  AST.reset(new ASTUnit(false));
1940  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1941  AST->Diagnostics = Diags;
1942  AST->OnlyLocalDecls = OnlyLocalDecls;
1943  AST->CaptureDiagnostics = CaptureDiagnostics;
1944  AST->TUKind = TUKind;
1945  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1946  AST->IncludeBriefCommentsInCodeCompletion
1947    = IncludeBriefCommentsInCodeCompletion;
1948  AST->Invocation = CI;
1949  AST->FileSystemOpts = CI->getFileSystemOpts();
1950  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1951  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1952
1953  // Recover resources if we crash before exiting this method.
1954  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1955    ASTUnitCleanup(AST.get());
1956  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1957    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1958    DiagCleanup(Diags.getPtr());
1959
1960  return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1961}
1962
1963ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1964                                      const char **ArgEnd,
1965                                    IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1966                                      StringRef ResourceFilesPath,
1967                                      bool OnlyLocalDecls,
1968                                      bool CaptureDiagnostics,
1969                                      RemappedFile *RemappedFiles,
1970                                      unsigned NumRemappedFiles,
1971                                      bool RemappedFilesKeepOriginalName,
1972                                      bool PrecompilePreamble,
1973                                      TranslationUnitKind TUKind,
1974                                      bool CacheCodeCompletionResults,
1975                                      bool IncludeBriefCommentsInCodeCompletion,
1976                                      bool AllowPCHWithCompilerErrors,
1977                                      bool SkipFunctionBodies,
1978                                      bool UserFilesAreVolatile,
1979                                      bool ForSerialization,
1980                                      OwningPtr<ASTUnit> *ErrAST) {
1981  if (!Diags.getPtr()) {
1982    // No diagnostics engine was provided, so create our own diagnostics object
1983    // with the default options.
1984    Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
1985  }
1986
1987  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1988
1989  IntrusiveRefCntPtr<CompilerInvocation> CI;
1990
1991  {
1992
1993    CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1994                                      StoredDiagnostics);
1995
1996    CI = clang::createInvocationFromCommandLine(
1997                                           llvm::makeArrayRef(ArgBegin, ArgEnd),
1998                                           Diags);
1999    if (!CI)
2000      return 0;
2001  }
2002
2003  // Override any files that need remapping
2004  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2005    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2006    if (const llvm::MemoryBuffer *
2007            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2008      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
2009    } else {
2010      const char *fname = fileOrBuf.get<const char *>();
2011      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
2012    }
2013  }
2014  PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
2015  PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
2016  PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
2017
2018  // Override the resources path.
2019  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
2020
2021  CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
2022
2023  // Create the AST unit.
2024  OwningPtr<ASTUnit> AST;
2025  AST.reset(new ASTUnit(false));
2026  ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
2027  AST->Diagnostics = Diags;
2028  Diags = 0; // Zero out now to ease cleanup during crash recovery.
2029  AST->FileSystemOpts = CI->getFileSystemOpts();
2030  AST->FileMgr = new FileManager(AST->FileSystemOpts);
2031  AST->OnlyLocalDecls = OnlyLocalDecls;
2032  AST->CaptureDiagnostics = CaptureDiagnostics;
2033  AST->TUKind = TUKind;
2034  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
2035  AST->IncludeBriefCommentsInCodeCompletion
2036    = IncludeBriefCommentsInCodeCompletion;
2037  AST->UserFilesAreVolatile = UserFilesAreVolatile;
2038  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
2039  AST->StoredDiagnostics.swap(StoredDiagnostics);
2040  AST->Invocation = CI;
2041  if (ForSerialization)
2042    AST->WriterData.reset(new ASTWriterData());
2043  CI = 0; // Zero out now to ease cleanup during crash recovery.
2044
2045  // Recover resources if we crash before exiting this method.
2046  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2047    ASTUnitCleanup(AST.get());
2048
2049  if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
2050    // Some error occurred, if caller wants to examine diagnostics, pass it the
2051    // ASTUnit.
2052    if (ErrAST) {
2053      AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2054      ErrAST->swap(AST);
2055    }
2056    return 0;
2057  }
2058
2059  return AST.take();
2060}
2061
2062bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
2063  if (!Invocation)
2064    return true;
2065
2066  clearFileLevelDecls();
2067
2068  SimpleTimer ParsingTimer(WantTiming);
2069  ParsingTimer.setOutput("Reparsing " + getMainFileName());
2070
2071  // Remap files.
2072  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2073  for (PreprocessorOptions::remapped_file_buffer_iterator
2074         R = PPOpts.remapped_file_buffer_begin(),
2075         REnd = PPOpts.remapped_file_buffer_end();
2076       R != REnd;
2077       ++R) {
2078    delete R->second;
2079  }
2080  Invocation->getPreprocessorOpts().clearRemappedFiles();
2081  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2082    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2083    if (const llvm::MemoryBuffer *
2084            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2085      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2086                                                        memBuf);
2087    } else {
2088      const char *fname = fileOrBuf.get<const char *>();
2089      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2090                                                        fname);
2091    }
2092  }
2093
2094  // If we have a preamble file lying around, or if we might try to
2095  // build a precompiled preamble, do so now.
2096  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2097  if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2098    OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
2099
2100  // Clear out the diagnostics state.
2101  getDiagnostics().Reset();
2102  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2103  if (OverrideMainBuffer)
2104    getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2105
2106  // Parse the sources
2107  bool Result = Parse(OverrideMainBuffer);
2108
2109  // If we're caching global code-completion results, and the top-level
2110  // declarations have changed, clear out the code-completion cache.
2111  if (!Result && ShouldCacheCodeCompletionResults &&
2112      CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2113    CacheCodeCompletionResults();
2114
2115  // We now need to clear out the completion info related to this translation
2116  // unit; it'll be recreated if necessary.
2117  CCTUInfo.reset();
2118
2119  return Result;
2120}
2121
2122//----------------------------------------------------------------------------//
2123// Code completion
2124//----------------------------------------------------------------------------//
2125
2126namespace {
2127  /// \brief Code completion consumer that combines the cached code-completion
2128  /// results from an ASTUnit with the code-completion results provided to it,
2129  /// then passes the result on to
2130  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2131    uint64_t NormalContexts;
2132    ASTUnit &AST;
2133    CodeCompleteConsumer &Next;
2134
2135  public:
2136    AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2137                                  const CodeCompleteOptions &CodeCompleteOpts)
2138      : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2139        AST(AST), Next(Next)
2140    {
2141      // Compute the set of contexts in which we will look when we don't have
2142      // any information about the specific context.
2143      NormalContexts
2144        = (1LL << CodeCompletionContext::CCC_TopLevel)
2145        | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2146        | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2147        | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2148        | (1LL << CodeCompletionContext::CCC_Statement)
2149        | (1LL << CodeCompletionContext::CCC_Expression)
2150        | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2151        | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2152        | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2153        | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2154        | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2155        | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2156        | (1LL << CodeCompletionContext::CCC_Recovery);
2157
2158      if (AST.getASTContext().getLangOpts().CPlusPlus)
2159        NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2160                       |  (1LL << CodeCompletionContext::CCC_UnionTag)
2161                       |  (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2162    }
2163
2164    virtual void ProcessCodeCompleteResults(Sema &S,
2165                                            CodeCompletionContext Context,
2166                                            CodeCompletionResult *Results,
2167                                            unsigned NumResults);
2168
2169    virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2170                                           OverloadCandidate *Candidates,
2171                                           unsigned NumCandidates) {
2172      Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2173    }
2174
2175    virtual CodeCompletionAllocator &getAllocator() {
2176      return Next.getAllocator();
2177    }
2178
2179    virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() {
2180      return Next.getCodeCompletionTUInfo();
2181    }
2182  };
2183}
2184
2185/// \brief Helper function that computes which global names are hidden by the
2186/// local code-completion results.
2187static void CalculateHiddenNames(const CodeCompletionContext &Context,
2188                                 CodeCompletionResult *Results,
2189                                 unsigned NumResults,
2190                                 ASTContext &Ctx,
2191                          llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2192  bool OnlyTagNames = false;
2193  switch (Context.getKind()) {
2194  case CodeCompletionContext::CCC_Recovery:
2195  case CodeCompletionContext::CCC_TopLevel:
2196  case CodeCompletionContext::CCC_ObjCInterface:
2197  case CodeCompletionContext::CCC_ObjCImplementation:
2198  case CodeCompletionContext::CCC_ObjCIvarList:
2199  case CodeCompletionContext::CCC_ClassStructUnion:
2200  case CodeCompletionContext::CCC_Statement:
2201  case CodeCompletionContext::CCC_Expression:
2202  case CodeCompletionContext::CCC_ObjCMessageReceiver:
2203  case CodeCompletionContext::CCC_DotMemberAccess:
2204  case CodeCompletionContext::CCC_ArrowMemberAccess:
2205  case CodeCompletionContext::CCC_ObjCPropertyAccess:
2206  case CodeCompletionContext::CCC_Namespace:
2207  case CodeCompletionContext::CCC_Type:
2208  case CodeCompletionContext::CCC_Name:
2209  case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2210  case CodeCompletionContext::CCC_ParenthesizedExpression:
2211  case CodeCompletionContext::CCC_ObjCInterfaceName:
2212    break;
2213
2214  case CodeCompletionContext::CCC_EnumTag:
2215  case CodeCompletionContext::CCC_UnionTag:
2216  case CodeCompletionContext::CCC_ClassOrStructTag:
2217    OnlyTagNames = true;
2218    break;
2219
2220  case CodeCompletionContext::CCC_ObjCProtocolName:
2221  case CodeCompletionContext::CCC_MacroName:
2222  case CodeCompletionContext::CCC_MacroNameUse:
2223  case CodeCompletionContext::CCC_PreprocessorExpression:
2224  case CodeCompletionContext::CCC_PreprocessorDirective:
2225  case CodeCompletionContext::CCC_NaturalLanguage:
2226  case CodeCompletionContext::CCC_SelectorName:
2227  case CodeCompletionContext::CCC_TypeQualifiers:
2228  case CodeCompletionContext::CCC_Other:
2229  case CodeCompletionContext::CCC_OtherWithMacros:
2230  case CodeCompletionContext::CCC_ObjCInstanceMessage:
2231  case CodeCompletionContext::CCC_ObjCClassMessage:
2232  case CodeCompletionContext::CCC_ObjCCategoryName:
2233    // We're looking for nothing, or we're looking for names that cannot
2234    // be hidden.
2235    return;
2236  }
2237
2238  typedef CodeCompletionResult Result;
2239  for (unsigned I = 0; I != NumResults; ++I) {
2240    if (Results[I].Kind != Result::RK_Declaration)
2241      continue;
2242
2243    unsigned IDNS
2244      = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2245
2246    bool Hiding = false;
2247    if (OnlyTagNames)
2248      Hiding = (IDNS & Decl::IDNS_Tag);
2249    else {
2250      unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2251                             Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2252                             Decl::IDNS_NonMemberOperator);
2253      if (Ctx.getLangOpts().CPlusPlus)
2254        HiddenIDNS |= Decl::IDNS_Tag;
2255      Hiding = (IDNS & HiddenIDNS);
2256    }
2257
2258    if (!Hiding)
2259      continue;
2260
2261    DeclarationName Name = Results[I].Declaration->getDeclName();
2262    if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2263      HiddenNames.insert(Identifier->getName());
2264    else
2265      HiddenNames.insert(Name.getAsString());
2266  }
2267}
2268
2269
2270void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2271                                            CodeCompletionContext Context,
2272                                            CodeCompletionResult *Results,
2273                                            unsigned NumResults) {
2274  // Merge the results we were given with the results we cached.
2275  bool AddedResult = false;
2276  uint64_t InContexts =
2277      Context.getKind() == CodeCompletionContext::CCC_Recovery
2278        ? NormalContexts : (1LL << Context.getKind());
2279  // Contains the set of names that are hidden by "local" completion results.
2280  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2281  typedef CodeCompletionResult Result;
2282  SmallVector<Result, 8> AllResults;
2283  for (ASTUnit::cached_completion_iterator
2284            C = AST.cached_completion_begin(),
2285         CEnd = AST.cached_completion_end();
2286       C != CEnd; ++C) {
2287    // If the context we are in matches any of the contexts we are
2288    // interested in, we'll add this result.
2289    if ((C->ShowInContexts & InContexts) == 0)
2290      continue;
2291
2292    // If we haven't added any results previously, do so now.
2293    if (!AddedResult) {
2294      CalculateHiddenNames(Context, Results, NumResults, S.Context,
2295                           HiddenNames);
2296      AllResults.insert(AllResults.end(), Results, Results + NumResults);
2297      AddedResult = true;
2298    }
2299
2300    // Determine whether this global completion result is hidden by a local
2301    // completion result. If so, skip it.
2302    if (C->Kind != CXCursor_MacroDefinition &&
2303        HiddenNames.count(C->Completion->getTypedText()))
2304      continue;
2305
2306    // Adjust priority based on similar type classes.
2307    unsigned Priority = C->Priority;
2308    CodeCompletionString *Completion = C->Completion;
2309    if (!Context.getPreferredType().isNull()) {
2310      if (C->Kind == CXCursor_MacroDefinition) {
2311        Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2312                                         S.getLangOpts(),
2313                               Context.getPreferredType()->isAnyPointerType());
2314      } else if (C->Type) {
2315        CanQualType Expected
2316          = S.Context.getCanonicalType(
2317                               Context.getPreferredType().getUnqualifiedType());
2318        SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2319        if (ExpectedSTC == C->TypeClass) {
2320          // We know this type is similar; check for an exact match.
2321          llvm::StringMap<unsigned> &CachedCompletionTypes
2322            = AST.getCachedCompletionTypes();
2323          llvm::StringMap<unsigned>::iterator Pos
2324            = CachedCompletionTypes.find(QualType(Expected).getAsString());
2325          if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2326            Priority /= CCF_ExactTypeMatch;
2327          else
2328            Priority /= CCF_SimilarTypeMatch;
2329        }
2330      }
2331    }
2332
2333    // Adjust the completion string, if required.
2334    if (C->Kind == CXCursor_MacroDefinition &&
2335        Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2336      // Create a new code-completion string that just contains the
2337      // macro name, without its arguments.
2338      CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2339                                    CCP_CodePattern, C->Availability);
2340      Builder.AddTypedTextChunk(C->Completion->getTypedText());
2341      Priority = CCP_CodePattern;
2342      Completion = Builder.TakeString();
2343    }
2344
2345    AllResults.push_back(Result(Completion, Priority, C->Kind,
2346                                C->Availability));
2347  }
2348
2349  // If we did not add any cached completion results, just forward the
2350  // results we were given to the next consumer.
2351  if (!AddedResult) {
2352    Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2353    return;
2354  }
2355
2356  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2357                                  AllResults.size());
2358}
2359
2360
2361
2362void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2363                           RemappedFile *RemappedFiles,
2364                           unsigned NumRemappedFiles,
2365                           bool IncludeMacros,
2366                           bool IncludeCodePatterns,
2367                           bool IncludeBriefComments,
2368                           CodeCompleteConsumer &Consumer,
2369                           DiagnosticsEngine &Diag, LangOptions &LangOpts,
2370                           SourceManager &SourceMgr, FileManager &FileMgr,
2371                   SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2372             SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2373  if (!Invocation)
2374    return;
2375
2376  SimpleTimer CompletionTimer(WantTiming);
2377  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2378                            Twine(Line) + ":" + Twine(Column));
2379
2380  IntrusiveRefCntPtr<CompilerInvocation>
2381    CCInvocation(new CompilerInvocation(*Invocation));
2382
2383  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2384  CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2385  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2386
2387  CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2388                                   CachedCompletionResults.empty();
2389  CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2390  CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2391  CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2392
2393  assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2394
2395  FrontendOpts.CodeCompletionAt.FileName = File;
2396  FrontendOpts.CodeCompletionAt.Line = Line;
2397  FrontendOpts.CodeCompletionAt.Column = Column;
2398
2399  // Set the language options appropriately.
2400  LangOpts = *CCInvocation->getLangOpts();
2401
2402  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2403
2404  // Recover resources if we crash before exiting this method.
2405  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2406    CICleanup(Clang.get());
2407
2408  Clang->setInvocation(&*CCInvocation);
2409  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile();
2410
2411  // Set up diagnostics, capturing any diagnostics produced.
2412  Clang->setDiagnostics(&Diag);
2413  CaptureDroppedDiagnostics Capture(true,
2414                                    Clang->getDiagnostics(),
2415                                    StoredDiagnostics);
2416  ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2417
2418  // Create the target instance.
2419  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2420                                                &Clang->getTargetOpts()));
2421  if (!Clang->hasTarget()) {
2422    Clang->setInvocation(0);
2423    return;
2424  }
2425
2426  // Inform the target of the language options.
2427  //
2428  // FIXME: We shouldn't need to do this, the target should be immutable once
2429  // created. This complexity should be lifted elsewhere.
2430  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2431
2432  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2433         "Invocation must have exactly one source file!");
2434  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST &&
2435         "FIXME: AST inputs not yet supported here!");
2436  assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR &&
2437         "IR inputs not support here!");
2438
2439
2440  // Use the source and file managers that we were given.
2441  Clang->setFileManager(&FileMgr);
2442  Clang->setSourceManager(&SourceMgr);
2443
2444  // Remap files.
2445  PreprocessorOpts.clearRemappedFiles();
2446  PreprocessorOpts.RetainRemappedFileBuffers = true;
2447  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2448    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2449    if (const llvm::MemoryBuffer *
2450            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2451      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2452      OwnedBuffers.push_back(memBuf);
2453    } else {
2454      const char *fname = fileOrBuf.get<const char *>();
2455      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2456    }
2457  }
2458
2459  // Use the code completion consumer we were given, but adding any cached
2460  // code-completion results.
2461  AugmentedCodeCompleteConsumer *AugmentedConsumer
2462    = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2463  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2464
2465  // If we have a precompiled preamble, try to use it. We only allow
2466  // the use of the precompiled preamble if we're if the completion
2467  // point is within the main file, after the end of the precompiled
2468  // preamble.
2469  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2470  if (!getPreambleFile(this).empty()) {
2471    std::string CompleteFilePath(File);
2472    uint64_t CompleteFileID;
2473
2474    if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) {
2475      std::string MainPath(OriginalSourceFile);
2476      uint64_t MainID;
2477      if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) {
2478        if (CompleteFileID == MainID && Line > 1)
2479          OverrideMainBuffer
2480            = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2481                                                   Line - 1);
2482      }
2483    }
2484  }
2485
2486  // If the main file has been overridden due to the use of a preamble,
2487  // make that override happen and introduce the preamble.
2488  if (OverrideMainBuffer) {
2489    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2490    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2491    PreprocessorOpts.PrecompiledPreambleBytes.second
2492                                                    = PreambleEndsAtStartOfLine;
2493    PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2494    PreprocessorOpts.DisablePCHValidation = true;
2495
2496    OwnedBuffers.push_back(OverrideMainBuffer);
2497  } else {
2498    PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2499    PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2500  }
2501
2502  // Disable the preprocessing record if modules are not enabled.
2503  if (!Clang->getLangOpts().Modules)
2504    PreprocessorOpts.DetailedRecord = false;
2505
2506  OwningPtr<SyntaxOnlyAction> Act;
2507  Act.reset(new SyntaxOnlyAction);
2508  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2509    Act->Execute();
2510    Act->EndSourceFile();
2511  }
2512}
2513
2514bool ASTUnit::Save(StringRef File) {
2515  if (HadModuleLoaderFatalFailure)
2516    return true;
2517
2518  // Write to a temporary file and later rename it to the actual file, to avoid
2519  // possible race conditions.
2520  SmallString<128> TempPath;
2521  TempPath = File;
2522  TempPath += "-%%%%%%%%";
2523  int fd;
2524  if (llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath))
2525    return true;
2526
2527  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2528  // unconditionally create a stat cache when we parse the file?
2529  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2530
2531  serialize(Out);
2532  Out.close();
2533  if (Out.has_error()) {
2534    Out.clear_error();
2535    return true;
2536  }
2537
2538  if (llvm::sys::fs::rename(TempPath.str(), File)) {
2539    bool exists;
2540    llvm::sys::fs::remove(TempPath.str(), exists);
2541    return true;
2542  }
2543
2544  return false;
2545}
2546
2547static bool serializeUnit(ASTWriter &Writer,
2548                          SmallVectorImpl<char> &Buffer,
2549                          Sema &S,
2550                          bool hasErrors,
2551                          raw_ostream &OS) {
2552  Writer.WriteAST(S, std::string(), 0, "", hasErrors);
2553
2554  // Write the generated bitstream to "Out".
2555  if (!Buffer.empty())
2556    OS.write(Buffer.data(), Buffer.size());
2557
2558  return false;
2559}
2560
2561bool ASTUnit::serialize(raw_ostream &OS) {
2562  bool hasErrors = getDiagnostics().hasErrorOccurred();
2563
2564  if (WriterData)
2565    return serializeUnit(WriterData->Writer, WriterData->Buffer,
2566                         getSema(), hasErrors, OS);
2567
2568  SmallString<128> Buffer;
2569  llvm::BitstreamWriter Stream(Buffer);
2570  ASTWriter Writer(Stream);
2571  return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2572}
2573
2574typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2575
2576static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2577  unsigned Raw = L.getRawEncoding();
2578  const unsigned MacroBit = 1U << 31;
2579  L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2580      ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2581}
2582
2583void ASTUnit::TranslateStoredDiagnostics(
2584                          ASTReader *MMan,
2585                          StringRef ModName,
2586                          SourceManager &SrcMgr,
2587                          const SmallVectorImpl<StoredDiagnostic> &Diags,
2588                          SmallVectorImpl<StoredDiagnostic> &Out) {
2589  // The stored diagnostic has the old source manager in it; update
2590  // the locations to refer into the new source manager. We also need to remap
2591  // all the locations to the new view. This includes the diag location, any
2592  // associated source ranges, and the source ranges of associated fix-its.
2593  // FIXME: There should be a cleaner way to do this.
2594
2595  SmallVector<StoredDiagnostic, 4> Result;
2596  Result.reserve(Diags.size());
2597  assert(MMan && "Don't have a module manager");
2598  serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
2599  assert(Mod && "Don't have preamble module");
2600  SLocRemap &Remap = Mod->SLocRemap;
2601  for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2602    // Rebuild the StoredDiagnostic.
2603    const StoredDiagnostic &SD = Diags[I];
2604    SourceLocation L = SD.getLocation();
2605    TranslateSLoc(L, Remap);
2606    FullSourceLoc Loc(L, SrcMgr);
2607
2608    SmallVector<CharSourceRange, 4> Ranges;
2609    Ranges.reserve(SD.range_size());
2610    for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2611                                          E = SD.range_end();
2612         I != E; ++I) {
2613      SourceLocation BL = I->getBegin();
2614      TranslateSLoc(BL, Remap);
2615      SourceLocation EL = I->getEnd();
2616      TranslateSLoc(EL, Remap);
2617      Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2618    }
2619
2620    SmallVector<FixItHint, 2> FixIts;
2621    FixIts.reserve(SD.fixit_size());
2622    for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2623                                          E = SD.fixit_end();
2624         I != E; ++I) {
2625      FixIts.push_back(FixItHint());
2626      FixItHint &FH = FixIts.back();
2627      FH.CodeToInsert = I->CodeToInsert;
2628      SourceLocation BL = I->RemoveRange.getBegin();
2629      TranslateSLoc(BL, Remap);
2630      SourceLocation EL = I->RemoveRange.getEnd();
2631      TranslateSLoc(EL, Remap);
2632      FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2633                                       I->RemoveRange.isTokenRange());
2634    }
2635
2636    Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2637                                      SD.getMessage(), Loc, Ranges, FixIts));
2638  }
2639  Result.swap(Out);
2640}
2641
2642static inline bool compLocDecl(std::pair<unsigned, Decl *> L,
2643                               std::pair<unsigned, Decl *> R) {
2644  return L.first < R.first;
2645}
2646
2647void ASTUnit::addFileLevelDecl(Decl *D) {
2648  assert(D);
2649
2650  // We only care about local declarations.
2651  if (D->isFromASTFile())
2652    return;
2653
2654  SourceManager &SM = *SourceMgr;
2655  SourceLocation Loc = D->getLocation();
2656  if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2657    return;
2658
2659  // We only keep track of the file-level declarations of each file.
2660  if (!D->getLexicalDeclContext()->isFileContext())
2661    return;
2662
2663  SourceLocation FileLoc = SM.getFileLoc(Loc);
2664  assert(SM.isLocalSourceLocation(FileLoc));
2665  FileID FID;
2666  unsigned Offset;
2667  llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2668  if (FID.isInvalid())
2669    return;
2670
2671  LocDeclsTy *&Decls = FileDecls[FID];
2672  if (!Decls)
2673    Decls = new LocDeclsTy();
2674
2675  std::pair<unsigned, Decl *> LocDecl(Offset, D);
2676
2677  if (Decls->empty() || Decls->back().first <= Offset) {
2678    Decls->push_back(LocDecl);
2679    return;
2680  }
2681
2682  LocDeclsTy::iterator
2683    I = std::upper_bound(Decls->begin(), Decls->end(), LocDecl, compLocDecl);
2684
2685  Decls->insert(I, LocDecl);
2686}
2687
2688void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2689                                  SmallVectorImpl<Decl *> &Decls) {
2690  if (File.isInvalid())
2691    return;
2692
2693  if (SourceMgr->isLoadedFileID(File)) {
2694    assert(Ctx->getExternalSource() && "No external source!");
2695    return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2696                                                         Decls);
2697  }
2698
2699  FileDeclsTy::iterator I = FileDecls.find(File);
2700  if (I == FileDecls.end())
2701    return;
2702
2703  LocDeclsTy &LocDecls = *I->second;
2704  if (LocDecls.empty())
2705    return;
2706
2707  LocDeclsTy::iterator
2708    BeginIt = std::lower_bound(LocDecls.begin(), LocDecls.end(),
2709                               std::make_pair(Offset, (Decl*)0), compLocDecl);
2710  if (BeginIt != LocDecls.begin())
2711    --BeginIt;
2712
2713  // If we are pointing at a top-level decl inside an objc container, we need
2714  // to backtrack until we find it otherwise we will fail to report that the
2715  // region overlaps with an objc container.
2716  while (BeginIt != LocDecls.begin() &&
2717         BeginIt->second->isTopLevelDeclInObjCContainer())
2718    --BeginIt;
2719
2720  LocDeclsTy::iterator
2721    EndIt = std::upper_bound(LocDecls.begin(), LocDecls.end(),
2722                             std::make_pair(Offset+Length, (Decl*)0),
2723                             compLocDecl);
2724  if (EndIt != LocDecls.end())
2725    ++EndIt;
2726
2727  for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2728    Decls.push_back(DIt->second);
2729}
2730
2731SourceLocation ASTUnit::getLocation(const FileEntry *File,
2732                                    unsigned Line, unsigned Col) const {
2733  const SourceManager &SM = getSourceManager();
2734  SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2735  return SM.getMacroArgExpandedLocation(Loc);
2736}
2737
2738SourceLocation ASTUnit::getLocation(const FileEntry *File,
2739                                    unsigned Offset) const {
2740  const SourceManager &SM = getSourceManager();
2741  SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2742  return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2743}
2744
2745/// \brief If \arg Loc is a loaded location from the preamble, returns
2746/// the corresponding local location of the main file, otherwise it returns
2747/// \arg Loc.
2748SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2749  FileID PreambleID;
2750  if (SourceMgr)
2751    PreambleID = SourceMgr->getPreambleFileID();
2752
2753  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2754    return Loc;
2755
2756  unsigned Offs;
2757  if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2758    SourceLocation FileLoc
2759        = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2760    return FileLoc.getLocWithOffset(Offs);
2761  }
2762
2763  return Loc;
2764}
2765
2766/// \brief If \arg Loc is a local location of the main file but inside the
2767/// preamble chunk, returns the corresponding loaded location from the
2768/// preamble, otherwise it returns \arg Loc.
2769SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2770  FileID PreambleID;
2771  if (SourceMgr)
2772    PreambleID = SourceMgr->getPreambleFileID();
2773
2774  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2775    return Loc;
2776
2777  unsigned Offs;
2778  if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2779      Offs < Preamble.size()) {
2780    SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2781    return FileLoc.getLocWithOffset(Offs);
2782  }
2783
2784  return Loc;
2785}
2786
2787bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2788  FileID FID;
2789  if (SourceMgr)
2790    FID = SourceMgr->getPreambleFileID();
2791
2792  if (Loc.isInvalid() || FID.isInvalid())
2793    return false;
2794
2795  return SourceMgr->isInFileID(Loc, FID);
2796}
2797
2798bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2799  FileID FID;
2800  if (SourceMgr)
2801    FID = SourceMgr->getMainFileID();
2802
2803  if (Loc.isInvalid() || FID.isInvalid())
2804    return false;
2805
2806  return SourceMgr->isInFileID(Loc, FID);
2807}
2808
2809SourceLocation ASTUnit::getEndOfPreambleFileID() {
2810  FileID FID;
2811  if (SourceMgr)
2812    FID = SourceMgr->getPreambleFileID();
2813
2814  if (FID.isInvalid())
2815    return SourceLocation();
2816
2817  return SourceMgr->getLocForEndOfFile(FID);
2818}
2819
2820SourceLocation ASTUnit::getStartOfMainFileID() {
2821  FileID FID;
2822  if (SourceMgr)
2823    FID = SourceMgr->getMainFileID();
2824
2825  if (FID.isInvalid())
2826    return SourceLocation();
2827
2828  return SourceMgr->getLocForStartOfFile(FID);
2829}
2830
2831std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
2832ASTUnit::getLocalPreprocessingEntities() const {
2833  if (isMainFileAST()) {
2834    serialization::ModuleFile &
2835      Mod = Reader->getModuleManager().getPrimaryModule();
2836    return Reader->getModulePreprocessedEntities(Mod);
2837  }
2838
2839  if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2840    return std::make_pair(PPRec->local_begin(), PPRec->local_end());
2841
2842  return std::make_pair(PreprocessingRecord::iterator(),
2843                        PreprocessingRecord::iterator());
2844}
2845
2846bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2847  if (isMainFileAST()) {
2848    serialization::ModuleFile &
2849      Mod = Reader->getModuleManager().getPrimaryModule();
2850    ASTReader::ModuleDeclIterator MDI, MDE;
2851    llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
2852    for (; MDI != MDE; ++MDI) {
2853      if (!Fn(context, *MDI))
2854        return false;
2855    }
2856
2857    return true;
2858  }
2859
2860  for (ASTUnit::top_level_iterator TL = top_level_begin(),
2861                                TLEnd = top_level_end();
2862         TL != TLEnd; ++TL) {
2863    if (!Fn(context, *TL))
2864      return false;
2865  }
2866
2867  return true;
2868}
2869
2870namespace {
2871struct PCHLocatorInfo {
2872  serialization::ModuleFile *Mod;
2873  PCHLocatorInfo() : Mod(0) {}
2874};
2875}
2876
2877static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2878  PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2879  switch (M.Kind) {
2880  case serialization::MK_Module:
2881    return true; // skip dependencies.
2882  case serialization::MK_PCH:
2883    Info.Mod = &M;
2884    return true; // found it.
2885  case serialization::MK_Preamble:
2886    return false; // look in dependencies.
2887  case serialization::MK_MainFile:
2888    return false; // look in dependencies.
2889  }
2890
2891  return true;
2892}
2893
2894const FileEntry *ASTUnit::getPCHFile() {
2895  if (!Reader)
2896    return 0;
2897
2898  PCHLocatorInfo Info;
2899  Reader->getModuleManager().visit(PCHLocator, &Info);
2900  if (Info.Mod)
2901    return Info.Mod->File;
2902
2903  return 0;
2904}
2905
2906bool ASTUnit::isModuleFile() {
2907  return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2908}
2909
2910void ASTUnit::PreambleData::countLines() const {
2911  NumLines = 0;
2912  if (empty())
2913    return;
2914
2915  for (std::vector<char>::const_iterator
2916         I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2917    if (*I == '\n')
2918      ++NumLines;
2919  }
2920  if (Buffer.back() != '\n')
2921    ++NumLines;
2922}
2923
2924#ifndef NDEBUG
2925ASTUnit::ConcurrencyState::ConcurrencyState() {
2926  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2927}
2928
2929ASTUnit::ConcurrencyState::~ConcurrencyState() {
2930  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2931}
2932
2933void ASTUnit::ConcurrencyState::start() {
2934  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2935  assert(acquired && "Concurrent access to ASTUnit!");
2936}
2937
2938void ASTUnit::ConcurrencyState::finish() {
2939  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2940}
2941
2942#else // NDEBUG
2943
2944ASTUnit::ConcurrencyState::ConcurrencyState() {}
2945ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2946void ASTUnit::ConcurrencyState::start() {}
2947void ASTUnit::ConcurrencyState::finish() {}
2948
2949#endif
2950