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