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