ASTUnit.cpp revision 25893e0930c1241c3fdafc6f1b8661443bb19692
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/ASTContext.h"
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/DeclVisitor.h"
18#include "clang/AST/TypeOrdering.h"
19#include "clang/AST/StmtVisitor.h"
20#include "clang/Frontend/CompilerInstance.h"
21#include "clang/Frontend/FrontendActions.h"
22#include "clang/Frontend/FrontendDiagnostic.h"
23#include "clang/Frontend/FrontendOptions.h"
24#include "clang/Frontend/MultiplexConsumer.h"
25#include "clang/Frontend/Utils.h"
26#include "clang/Serialization/ASTReader.h"
27#include "clang/Serialization/ASTWriter.h"
28#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/Preprocessor.h"
30#include "clang/Lex/PreprocessorOptions.h"
31#include "clang/Basic/TargetOptions.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Basic/Diagnostic.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/MemoryBuffer.h"
39#include "llvm/Support/Host.h"
40#include "llvm/Support/Path.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Support/Timer.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/Mutex.h"
45#include "llvm/Support/MutexGuard.h"
46#include "llvm/Support/CrashRecoveryContext.h"
47#include <cstdlib>
48#include <cstdio>
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(void);
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(void) {
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.CPlusPlus0x)
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  TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator,
360                                       getCodeCompletionTUInfo(), Results);
361
362  // Translate global code completions into cached completions.
363  llvm::DenseMap<CanQualType, unsigned> CompletionTypes;
364
365  for (unsigned I = 0, N = Results.size(); I != N; ++I) {
366    switch (Results[I].Kind) {
367    case Result::RK_Declaration: {
368      bool IsNestedNameSpecifier = false;
369      CachedCodeCompletionResult CachedResult;
370      CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema,
371                                                    *CachedCompletionAllocator,
372                                                    getCodeCompletionTUInfo(),
373                                          IncludeBriefCommentsInCodeCompletion);
374      CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration,
375                                                        Ctx->getLangOpts(),
376                                                        IsNestedNameSpecifier);
377      CachedResult.Priority = Results[I].Priority;
378      CachedResult.Kind = Results[I].CursorKind;
379      CachedResult.Availability = Results[I].Availability;
380
381      // Keep track of the type of this completion in an ASTContext-agnostic
382      // way.
383      QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration);
384      if (UsageType.isNull()) {
385        CachedResult.TypeClass = STC_Void;
386        CachedResult.Type = 0;
387      } else {
388        CanQualType CanUsageType
389          = Ctx->getCanonicalType(UsageType.getUnqualifiedType());
390        CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType);
391
392        // Determine whether we have already seen this type. If so, we save
393        // ourselves the work of formatting the type string by using the
394        // temporary, CanQualType-based hash table to find the associated value.
395        unsigned &TypeValue = CompletionTypes[CanUsageType];
396        if (TypeValue == 0) {
397          TypeValue = CompletionTypes.size();
398          CachedCompletionTypes[QualType(CanUsageType).getAsString()]
399            = TypeValue;
400        }
401
402        CachedResult.Type = TypeValue;
403      }
404
405      CachedCompletionResults.push_back(CachedResult);
406
407      /// Handle nested-name-specifiers in C++.
408      if (TheSema->Context.getLangOpts().CPlusPlus &&
409          IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) {
410        // The contexts in which a nested-name-specifier can appear in C++.
411        uint64_t NNSContexts
412          = (1LL << CodeCompletionContext::CCC_TopLevel)
413          | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
414          | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
415          | (1LL << CodeCompletionContext::CCC_Statement)
416          | (1LL << CodeCompletionContext::CCC_Expression)
417          | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
418          | (1LL << CodeCompletionContext::CCC_EnumTag)
419          | (1LL << CodeCompletionContext::CCC_UnionTag)
420          | (1LL << CodeCompletionContext::CCC_ClassOrStructTag)
421          | (1LL << CodeCompletionContext::CCC_Type)
422          | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName)
423          | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression);
424
425        if (isa<NamespaceDecl>(Results[I].Declaration) ||
426            isa<NamespaceAliasDecl>(Results[I].Declaration))
427          NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace);
428
429        if (unsigned RemainingContexts
430                                = NNSContexts & ~CachedResult.ShowInContexts) {
431          // If there any contexts where this completion can be a
432          // nested-name-specifier but isn't already an option, create a
433          // nested-name-specifier completion.
434          Results[I].StartsNestedNameSpecifier = true;
435          CachedResult.Completion
436            = Results[I].CreateCodeCompletionString(*TheSema,
437                                                    *CachedCompletionAllocator,
438                                                    getCodeCompletionTUInfo(),
439                                        IncludeBriefCommentsInCodeCompletion);
440          CachedResult.ShowInContexts = RemainingContexts;
441          CachedResult.Priority = CCP_NestedNameSpecifier;
442          CachedResult.TypeClass = STC_Void;
443          CachedResult.Type = 0;
444          CachedCompletionResults.push_back(CachedResult);
445        }
446      }
447      break;
448    }
449
450    case Result::RK_Keyword:
451    case Result::RK_Pattern:
452      // Ignore keywords and patterns; we don't care, since they are so
453      // easily regenerated.
454      break;
455
456    case Result::RK_Macro: {
457      CachedCodeCompletionResult CachedResult;
458      CachedResult.Completion
459        = Results[I].CreateCodeCompletionString(*TheSema,
460                                                *CachedCompletionAllocator,
461                                                getCodeCompletionTUInfo(),
462                                          IncludeBriefCommentsInCodeCompletion);
463      CachedResult.ShowInContexts
464        = (1LL << CodeCompletionContext::CCC_TopLevel)
465        | (1LL << CodeCompletionContext::CCC_ObjCInterface)
466        | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
467        | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
468        | (1LL << CodeCompletionContext::CCC_ClassStructUnion)
469        | (1LL << CodeCompletionContext::CCC_Statement)
470        | (1LL << CodeCompletionContext::CCC_Expression)
471        | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
472        | (1LL << CodeCompletionContext::CCC_MacroNameUse)
473        | (1LL << CodeCompletionContext::CCC_PreprocessorExpression)
474        | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
475        | (1LL << CodeCompletionContext::CCC_OtherWithMacros);
476
477      CachedResult.Priority = Results[I].Priority;
478      CachedResult.Kind = Results[I].CursorKind;
479      CachedResult.Availability = Results[I].Availability;
480      CachedResult.TypeClass = STC_Void;
481      CachedResult.Type = 0;
482      CachedCompletionResults.push_back(CachedResult);
483      break;
484    }
485    }
486  }
487
488  // Save the current top-level hash value.
489  CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue;
490}
491
492void ASTUnit::ClearCachedCompletionResults() {
493  CachedCompletionResults.clear();
494  CachedCompletionTypes.clear();
495  CachedCompletionAllocator = 0;
496}
497
498namespace {
499
500/// \brief Gathers information from ASTReader that will be used to initialize
501/// a Preprocessor.
502class ASTInfoCollector : public ASTReaderListener {
503  Preprocessor &PP;
504  ASTContext &Context;
505  LangOptions &LangOpt;
506  HeaderSearch &HSI;
507  IntrusiveRefCntPtr<TargetOptions> &TargetOpts;
508  IntrusiveRefCntPtr<TargetInfo> &Target;
509  unsigned &Counter;
510
511  unsigned NumHeaderInfos;
512
513  bool InitializedLanguage;
514public:
515  ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt,
516                   HeaderSearch &HSI,
517                   IntrusiveRefCntPtr<TargetOptions> &TargetOpts,
518                   IntrusiveRefCntPtr<TargetInfo> &Target,
519                   unsigned &Counter)
520    : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI),
521      TargetOpts(TargetOpts), Target(Target),
522      Counter(Counter), NumHeaderInfos(0),
523      InitializedLanguage(false) {}
524
525  virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
526                                   bool Complain) {
527    if (InitializedLanguage)
528      return false;
529
530    LangOpt = LangOpts;
531    InitializedLanguage = true;
532
533    updated();
534    return false;
535  }
536
537  virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
538                                 bool Complain) {
539    // If we've already initialized the target, don't do it again.
540    if (Target)
541      return false;
542
543    this->TargetOpts = new TargetOptions(TargetOpts);
544    Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(),
545                                          *this->TargetOpts);
546
547    updated();
548    return false;
549  }
550
551  virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
552    HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
553  }
554
555  virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) {
556    Counter = Value;
557  }
558
559private:
560  void updated() {
561    if (!Target || !InitializedLanguage)
562      return;
563
564    // Inform the target of the language options.
565    //
566    // FIXME: We shouldn't need to do this, the target should be immutable once
567    // created. This complexity should be lifted elsewhere.
568    Target->setForcedLangOptions(LangOpt);
569
570    // Initialize the preprocessor.
571    PP.Initialize(*Target);
572
573    // Initialize the ASTContext
574    Context.InitBuiltinTypes(*Target);
575  }
576};
577
578class StoredDiagnosticConsumer : public DiagnosticConsumer {
579  SmallVectorImpl<StoredDiagnostic> &StoredDiags;
580
581public:
582  explicit StoredDiagnosticConsumer(
583                          SmallVectorImpl<StoredDiagnostic> &StoredDiags)
584    : StoredDiags(StoredDiags) { }
585
586  virtual void HandleDiagnostic(DiagnosticsEngine::Level Level,
587                                const Diagnostic &Info);
588
589  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
590    // Just drop any diagnostics that come from cloned consumers; they'll
591    // have different source managers anyway.
592    // FIXME: We'd like to be able to capture these somehow, even if it's just
593    // file/line/column, because they could occur when parsing module maps or
594    // building modules on-demand.
595    return new IgnoringDiagConsumer();
596  }
597};
598
599/// \brief RAII object that optionally captures diagnostics, if
600/// there is no diagnostic client to capture them already.
601class CaptureDroppedDiagnostics {
602  DiagnosticsEngine &Diags;
603  StoredDiagnosticConsumer Client;
604  DiagnosticConsumer *PreviousClient;
605
606public:
607  CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
608                          SmallVectorImpl<StoredDiagnostic> &StoredDiags)
609    : Diags(Diags), Client(StoredDiags), PreviousClient(0)
610  {
611    if (RequestCapture || Diags.getClient() == 0) {
612      PreviousClient = Diags.takeClient();
613      Diags.setClient(&Client);
614    }
615  }
616
617  ~CaptureDroppedDiagnostics() {
618    if (Diags.getClient() == &Client) {
619      Diags.takeClient();
620      Diags.setClient(PreviousClient);
621    }
622  }
623};
624
625} // anonymous namespace
626
627void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level,
628                                              const Diagnostic &Info) {
629  // Default implementation (Warnings/errors count).
630  DiagnosticConsumer::HandleDiagnostic(Level, Info);
631
632  StoredDiags.push_back(StoredDiagnostic(Level, Info));
633}
634
635ASTDeserializationListener *ASTUnit::getDeserializationListener() {
636  if (WriterData)
637    return &WriterData->Writer;
638  return 0;
639}
640
641llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
642                                              std::string *ErrorStr) {
643  assert(FileMgr);
644  return FileMgr->getBufferForFile(Filename, ErrorStr);
645}
646
647/// \brief Configure the diagnostics object for use with ASTUnit.
648void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
649                             const char **ArgBegin, const char **ArgEnd,
650                             ASTUnit &AST, bool CaptureDiagnostics) {
651  if (!Diags.getPtr()) {
652    // No diagnostics engine was provided, so create our own diagnostics object
653    // with the default options.
654    DiagnosticConsumer *Client = 0;
655    if (CaptureDiagnostics)
656      Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics);
657    Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(),
658                                                ArgEnd-ArgBegin,
659                                                ArgBegin, Client,
660                                                /*ShouldOwnClient=*/true,
661                                                /*ShouldCloneClient=*/false);
662  } else if (CaptureDiagnostics) {
663    Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics));
664  }
665}
666
667ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename,
668                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
669                                  const FileSystemOptions &FileSystemOpts,
670                                  bool OnlyLocalDecls,
671                                  RemappedFile *RemappedFiles,
672                                  unsigned NumRemappedFiles,
673                                  bool CaptureDiagnostics,
674                                  bool AllowPCHWithCompilerErrors,
675                                  bool UserFilesAreVolatile) {
676  OwningPtr<ASTUnit> AST(new ASTUnit(true));
677
678  // Recover resources if we crash before exiting this method.
679  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
680    ASTUnitCleanup(AST.get());
681  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
682    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
683    DiagCleanup(Diags.getPtr());
684
685  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
686
687  AST->OnlyLocalDecls = OnlyLocalDecls;
688  AST->CaptureDiagnostics = CaptureDiagnostics;
689  AST->Diagnostics = Diags;
690  AST->FileMgr = new FileManager(FileSystemOpts);
691  AST->UserFilesAreVolatile = UserFilesAreVolatile;
692  AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
693                                     AST->getFileManager(),
694                                     UserFilesAreVolatile);
695  AST->HSOpts = new HeaderSearchOptions();
696
697  AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts,
698                                         AST->getFileManager(),
699                                         AST->getDiagnostics(),
700                                         AST->ASTFileLangOpts,
701                                         /*Target=*/0));
702
703  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
704    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
705    if (const llvm::MemoryBuffer *
706          memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
707      // Create the file entry for the file that we're mapping from.
708      const FileEntry *FromFile
709        = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
710                                               memBuf->getBufferSize(),
711                                               0);
712      if (!FromFile) {
713        AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
714          << RemappedFiles[I].first;
715        delete memBuf;
716        continue;
717      }
718
719      // Override the contents of the "from" file with the contents of
720      // the "to" file.
721      AST->getSourceManager().overrideFileContents(FromFile, memBuf);
722
723    } else {
724      const char *fname = fileOrBuf.get<const char *>();
725      const FileEntry *ToFile = AST->FileMgr->getFile(fname);
726      if (!ToFile) {
727        AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file)
728        << RemappedFiles[I].first << fname;
729        continue;
730      }
731
732      // Create the file entry for the file that we're mapping from.
733      const FileEntry *FromFile
734        = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
735                                               ToFile->getSize(),
736                                               0);
737      if (!FromFile) {
738        AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
739          << RemappedFiles[I].first;
740        delete memBuf;
741        continue;
742      }
743
744      // Override the contents of the "from" file with the contents of
745      // the "to" file.
746      AST->getSourceManager().overrideFileContents(FromFile, ToFile);
747    }
748  }
749
750  // Gather Info for preprocessor construction later on.
751
752  HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
753  unsigned Counter;
754
755  OwningPtr<ASTReader> Reader;
756
757  AST->PP = new Preprocessor(new PreprocessorOptions(),
758                             AST->getDiagnostics(), AST->ASTFileLangOpts,
759                             /*Target=*/0, AST->getSourceManager(), HeaderInfo,
760                             *AST,
761                             /*IILookup=*/0,
762                             /*OwnsHeaderSearch=*/false,
763                             /*DelayInitialization=*/true);
764  Preprocessor &PP = *AST->PP;
765
766  AST->Ctx = new ASTContext(AST->ASTFileLangOpts,
767                            AST->getSourceManager(),
768                            /*Target=*/0,
769                            PP.getIdentifierTable(),
770                            PP.getSelectorTable(),
771                            PP.getBuiltinInfo(),
772                            /* size_reserve = */0,
773                            /*DelayInitialization=*/true);
774  ASTContext &Context = *AST->Ctx;
775
776  bool disableValid = false;
777  if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
778    disableValid = true;
779  Reader.reset(new ASTReader(PP, Context,
780                             /*isysroot=*/"",
781                             /*DisableValidation=*/disableValid,
782                             /*DisableStatCache=*/false,
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                          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].File;
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].Kind != IK_AST &&
1100         "FIXME: AST inputs not yet supported here!");
1101  assert(Clang->getFrontendOpts().Inputs[0].Kind != 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].File);
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].File);
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].File);
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].File;
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].File);
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].File);
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].File;
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].Kind != IK_AST &&
1573         "FIXME: AST inputs not yet supported here!");
1574  assert(Clang->getFrontendOpts().Inputs[0].Kind != 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].File);
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  return Invocation->getFrontendOpts().Inputs[0].File;
1693}
1694
1695ASTUnit *ASTUnit::create(CompilerInvocation *CI,
1696                         IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1697                         bool CaptureDiagnostics,
1698                         bool UserFilesAreVolatile) {
1699  OwningPtr<ASTUnit> AST;
1700  AST.reset(new ASTUnit(false));
1701  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1702  AST->Diagnostics = Diags;
1703  AST->Invocation = CI;
1704  AST->FileSystemOpts = CI->getFileSystemOpts();
1705  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1706  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1707  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
1708                                     UserFilesAreVolatile);
1709
1710  return AST.take();
1711}
1712
1713ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
1714                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1715                                             ASTFrontendAction *Action,
1716                                             ASTUnit *Unit,
1717                                             bool Persistent,
1718                                             StringRef ResourceFilesPath,
1719                                             bool OnlyLocalDecls,
1720                                             bool CaptureDiagnostics,
1721                                             bool PrecompilePreamble,
1722                                             bool CacheCodeCompletionResults,
1723                                    bool IncludeBriefCommentsInCodeCompletion,
1724                                             bool UserFilesAreVolatile,
1725                                             OwningPtr<ASTUnit> *ErrAST) {
1726  assert(CI && "A CompilerInvocation is required");
1727
1728  OwningPtr<ASTUnit> OwnAST;
1729  ASTUnit *AST = Unit;
1730  if (!AST) {
1731    // Create the AST unit.
1732    OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile));
1733    AST = OwnAST.get();
1734  }
1735
1736  if (!ResourceFilesPath.empty()) {
1737    // Override the resources path.
1738    CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1739  }
1740  AST->OnlyLocalDecls = OnlyLocalDecls;
1741  AST->CaptureDiagnostics = CaptureDiagnostics;
1742  if (PrecompilePreamble)
1743    AST->PreambleRebuildCounter = 2;
1744  AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1745  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1746  AST->IncludeBriefCommentsInCodeCompletion
1747    = IncludeBriefCommentsInCodeCompletion;
1748
1749  // Recover resources if we crash before exiting this method.
1750  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1751    ASTUnitCleanup(OwnAST.get());
1752  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1753    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1754    DiagCleanup(Diags.getPtr());
1755
1756  // We'll manage file buffers ourselves.
1757  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1758  CI->getFrontendOpts().DisableFree = false;
1759  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1760
1761  // Create the compiler instance to use for building the AST.
1762  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1763
1764  // Recover resources if we crash before exiting this method.
1765  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1766    CICleanup(Clang.get());
1767
1768  Clang->setInvocation(CI);
1769  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File;
1770
1771  // Set up diagnostics, capturing any diagnostics that would
1772  // otherwise be dropped.
1773  Clang->setDiagnostics(&AST->getDiagnostics());
1774
1775  // Create the target instance.
1776  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1777                   Clang->getTargetOpts()));
1778  if (!Clang->hasTarget())
1779    return 0;
1780
1781  // Inform the target of the language options.
1782  //
1783  // FIXME: We shouldn't need to do this, the target should be immutable once
1784  // created. This complexity should be lifted elsewhere.
1785  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1786
1787  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1788         "Invocation must have exactly one source file!");
1789  assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST &&
1790         "FIXME: AST inputs not yet supported here!");
1791  assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR &&
1792         "IR inputs not supported here!");
1793
1794  // Configure the various subsystems.
1795  AST->TheSema.reset();
1796  AST->Ctx = 0;
1797  AST->PP = 0;
1798  AST->Reader = 0;
1799
1800  // Create a file manager object to provide access to and cache the filesystem.
1801  Clang->setFileManager(&AST->getFileManager());
1802
1803  // Create the source manager.
1804  Clang->setSourceManager(&AST->getSourceManager());
1805
1806  ASTFrontendAction *Act = Action;
1807
1808  OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1809  if (!Act) {
1810    TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1811    Act = TrackerAct.get();
1812  }
1813
1814  // Recover resources if we crash before exiting this method.
1815  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1816    ActCleanup(TrackerAct.get());
1817
1818  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
1819    AST->transferASTDataFromCompilerInstance(*Clang);
1820    if (OwnAST && ErrAST)
1821      ErrAST->swap(OwnAST);
1822
1823    return 0;
1824  }
1825
1826  if (Persistent && !TrackerAct) {
1827    Clang->getPreprocessor().addPPCallbacks(
1828     new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue()));
1829    std::vector<ASTConsumer*> Consumers;
1830    if (Clang->hasASTConsumer())
1831      Consumers.push_back(Clang->takeASTConsumer());
1832    Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST,
1833                                           AST->getCurrentTopLevelHashValue()));
1834    Clang->setASTConsumer(new MultiplexConsumer(Consumers));
1835  }
1836  if (!Act->Execute()) {
1837    AST->transferASTDataFromCompilerInstance(*Clang);
1838    if (OwnAST && ErrAST)
1839      ErrAST->swap(OwnAST);
1840
1841    return 0;
1842  }
1843
1844  // Steal the created target, context, and preprocessor.
1845  AST->transferASTDataFromCompilerInstance(*Clang);
1846
1847  Act->EndSourceFile();
1848
1849  if (OwnAST)
1850    return OwnAST.take();
1851  else
1852    return AST;
1853}
1854
1855bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1856  if (!Invocation)
1857    return true;
1858
1859  // We'll manage file buffers ourselves.
1860  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1861  Invocation->getFrontendOpts().DisableFree = false;
1862  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1863
1864  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1865  if (PrecompilePreamble) {
1866    PreambleRebuildCounter = 2;
1867    OverrideMainBuffer
1868      = getMainBufferWithPrecompiledPreamble(*Invocation);
1869  }
1870
1871  SimpleTimer ParsingTimer(WantTiming);
1872  ParsingTimer.setOutput("Parsing " + getMainFileName());
1873
1874  // Recover resources if we crash before exiting this method.
1875  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1876    MemBufferCleanup(OverrideMainBuffer);
1877
1878  return Parse(OverrideMainBuffer);
1879}
1880
1881ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1882                              IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1883                                             bool OnlyLocalDecls,
1884                                             bool CaptureDiagnostics,
1885                                             bool PrecompilePreamble,
1886                                             TranslationUnitKind TUKind,
1887                                             bool CacheCodeCompletionResults,
1888                                    bool IncludeBriefCommentsInCodeCompletion,
1889                                             bool UserFilesAreVolatile) {
1890  // Create the AST unit.
1891  OwningPtr<ASTUnit> AST;
1892  AST.reset(new ASTUnit(false));
1893  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1894  AST->Diagnostics = Diags;
1895  AST->OnlyLocalDecls = OnlyLocalDecls;
1896  AST->CaptureDiagnostics = CaptureDiagnostics;
1897  AST->TUKind = TUKind;
1898  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1899  AST->IncludeBriefCommentsInCodeCompletion
1900    = IncludeBriefCommentsInCodeCompletion;
1901  AST->Invocation = CI;
1902  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1903
1904  // Recover resources if we crash before exiting this method.
1905  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1906    ASTUnitCleanup(AST.get());
1907  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
1908    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
1909    DiagCleanup(Diags.getPtr());
1910
1911  return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1912}
1913
1914ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1915                                      const char **ArgEnd,
1916                                    IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
1917                                      StringRef ResourceFilesPath,
1918                                      bool OnlyLocalDecls,
1919                                      bool CaptureDiagnostics,
1920                                      RemappedFile *RemappedFiles,
1921                                      unsigned NumRemappedFiles,
1922                                      bool RemappedFilesKeepOriginalName,
1923                                      bool PrecompilePreamble,
1924                                      TranslationUnitKind TUKind,
1925                                      bool CacheCodeCompletionResults,
1926                                      bool IncludeBriefCommentsInCodeCompletion,
1927                                      bool AllowPCHWithCompilerErrors,
1928                                      bool SkipFunctionBodies,
1929                                      bool UserFilesAreVolatile,
1930                                      bool ForSerialization,
1931                                      OwningPtr<ASTUnit> *ErrAST) {
1932  if (!Diags.getPtr()) {
1933    // No diagnostics engine was provided, so create our own diagnostics object
1934    // with the default options.
1935    Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(),
1936                                                ArgEnd - ArgBegin,
1937                                                ArgBegin);
1938  }
1939
1940  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1941
1942  IntrusiveRefCntPtr<CompilerInvocation> CI;
1943
1944  {
1945
1946    CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1947                                      StoredDiagnostics);
1948
1949    CI = clang::createInvocationFromCommandLine(
1950                                           llvm::makeArrayRef(ArgBegin, ArgEnd),
1951                                           Diags);
1952    if (!CI)
1953      return 0;
1954  }
1955
1956  // Override any files that need remapping
1957  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1958    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1959    if (const llvm::MemoryBuffer *
1960            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1961      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1962    } else {
1963      const char *fname = fileOrBuf.get<const char *>();
1964      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1965    }
1966  }
1967  PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
1968  PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName;
1969  PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors;
1970
1971  // Override the resources path.
1972  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1973
1974  CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies;
1975
1976  // Create the AST unit.
1977  OwningPtr<ASTUnit> AST;
1978  AST.reset(new ASTUnit(false));
1979  ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1980  AST->Diagnostics = Diags;
1981  Diags = 0; // Zero out now to ease cleanup during crash recovery.
1982  AST->FileSystemOpts = CI->getFileSystemOpts();
1983  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1984  AST->OnlyLocalDecls = OnlyLocalDecls;
1985  AST->CaptureDiagnostics = CaptureDiagnostics;
1986  AST->TUKind = TUKind;
1987  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1988  AST->IncludeBriefCommentsInCodeCompletion
1989    = IncludeBriefCommentsInCodeCompletion;
1990  AST->UserFilesAreVolatile = UserFilesAreVolatile;
1991  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1992  AST->StoredDiagnostics.swap(StoredDiagnostics);
1993  AST->Invocation = CI;
1994  if (ForSerialization)
1995    AST->WriterData.reset(new ASTWriterData());
1996  CI = 0; // Zero out now to ease cleanup during crash recovery.
1997
1998  // Recover resources if we crash before exiting this method.
1999  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
2000    ASTUnitCleanup(AST.get());
2001
2002  if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) {
2003    // Some error occurred, if caller wants to examine diagnostics, pass it the
2004    // ASTUnit.
2005    if (ErrAST) {
2006      AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics);
2007      ErrAST->swap(AST);
2008    }
2009    return 0;
2010  }
2011
2012  return AST.take();
2013}
2014
2015bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
2016  if (!Invocation)
2017    return true;
2018
2019  clearFileLevelDecls();
2020
2021  SimpleTimer ParsingTimer(WantTiming);
2022  ParsingTimer.setOutput("Reparsing " + getMainFileName());
2023
2024  // Remap files.
2025  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
2026  PPOpts.DisableStatCache = true;
2027  for (PreprocessorOptions::remapped_file_buffer_iterator
2028         R = PPOpts.remapped_file_buffer_begin(),
2029         REnd = PPOpts.remapped_file_buffer_end();
2030       R != REnd;
2031       ++R) {
2032    delete R->second;
2033  }
2034  Invocation->getPreprocessorOpts().clearRemappedFiles();
2035  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2036    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2037    if (const llvm::MemoryBuffer *
2038            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2039      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2040                                                        memBuf);
2041    } else {
2042      const char *fname = fileOrBuf.get<const char *>();
2043      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
2044                                                        fname);
2045    }
2046  }
2047
2048  // If we have a preamble file lying around, or if we might try to
2049  // build a precompiled preamble, do so now.
2050  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2051  if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0)
2052    OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
2053
2054  // Clear out the diagnostics state.
2055  getDiagnostics().Reset();
2056  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
2057  if (OverrideMainBuffer)
2058    getDiagnostics().setNumWarnings(NumWarningsInPreamble);
2059
2060  // Parse the sources
2061  bool Result = Parse(OverrideMainBuffer);
2062
2063  // If we're caching global code-completion results, and the top-level
2064  // declarations have changed, clear out the code-completion cache.
2065  if (!Result && ShouldCacheCodeCompletionResults &&
2066      CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
2067    CacheCodeCompletionResults();
2068
2069  // We now need to clear out the completion info related to this translation
2070  // unit; it'll be recreated if necessary.
2071  CCTUInfo.reset();
2072
2073  return Result;
2074}
2075
2076//----------------------------------------------------------------------------//
2077// Code completion
2078//----------------------------------------------------------------------------//
2079
2080namespace {
2081  /// \brief Code completion consumer that combines the cached code-completion
2082  /// results from an ASTUnit with the code-completion results provided to it,
2083  /// then passes the result on to
2084  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
2085    uint64_t NormalContexts;
2086    ASTUnit &AST;
2087    CodeCompleteConsumer &Next;
2088
2089  public:
2090    AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
2091                                  const CodeCompleteOptions &CodeCompleteOpts)
2092      : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()),
2093        AST(AST), Next(Next)
2094    {
2095      // Compute the set of contexts in which we will look when we don't have
2096      // any information about the specific context.
2097      NormalContexts
2098        = (1LL << CodeCompletionContext::CCC_TopLevel)
2099        | (1LL << CodeCompletionContext::CCC_ObjCInterface)
2100        | (1LL << CodeCompletionContext::CCC_ObjCImplementation)
2101        | (1LL << CodeCompletionContext::CCC_ObjCIvarList)
2102        | (1LL << CodeCompletionContext::CCC_Statement)
2103        | (1LL << CodeCompletionContext::CCC_Expression)
2104        | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver)
2105        | (1LL << CodeCompletionContext::CCC_DotMemberAccess)
2106        | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess)
2107        | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess)
2108        | (1LL << CodeCompletionContext::CCC_ObjCProtocolName)
2109        | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression)
2110        | (1LL << CodeCompletionContext::CCC_Recovery);
2111
2112      if (AST.getASTContext().getLangOpts().CPlusPlus)
2113        NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag)
2114                       |  (1LL << CodeCompletionContext::CCC_UnionTag)
2115                       |  (1LL << CodeCompletionContext::CCC_ClassOrStructTag);
2116    }
2117
2118    virtual void ProcessCodeCompleteResults(Sema &S,
2119                                            CodeCompletionContext Context,
2120                                            CodeCompletionResult *Results,
2121                                            unsigned NumResults);
2122
2123    virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
2124                                           OverloadCandidate *Candidates,
2125                                           unsigned NumCandidates) {
2126      Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
2127    }
2128
2129    virtual CodeCompletionAllocator &getAllocator() {
2130      return Next.getAllocator();
2131    }
2132
2133    virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() {
2134      return Next.getCodeCompletionTUInfo();
2135    }
2136  };
2137}
2138
2139/// \brief Helper function that computes which global names are hidden by the
2140/// local code-completion results.
2141static void CalculateHiddenNames(const CodeCompletionContext &Context,
2142                                 CodeCompletionResult *Results,
2143                                 unsigned NumResults,
2144                                 ASTContext &Ctx,
2145                          llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
2146  bool OnlyTagNames = false;
2147  switch (Context.getKind()) {
2148  case CodeCompletionContext::CCC_Recovery:
2149  case CodeCompletionContext::CCC_TopLevel:
2150  case CodeCompletionContext::CCC_ObjCInterface:
2151  case CodeCompletionContext::CCC_ObjCImplementation:
2152  case CodeCompletionContext::CCC_ObjCIvarList:
2153  case CodeCompletionContext::CCC_ClassStructUnion:
2154  case CodeCompletionContext::CCC_Statement:
2155  case CodeCompletionContext::CCC_Expression:
2156  case CodeCompletionContext::CCC_ObjCMessageReceiver:
2157  case CodeCompletionContext::CCC_DotMemberAccess:
2158  case CodeCompletionContext::CCC_ArrowMemberAccess:
2159  case CodeCompletionContext::CCC_ObjCPropertyAccess:
2160  case CodeCompletionContext::CCC_Namespace:
2161  case CodeCompletionContext::CCC_Type:
2162  case CodeCompletionContext::CCC_Name:
2163  case CodeCompletionContext::CCC_PotentiallyQualifiedName:
2164  case CodeCompletionContext::CCC_ParenthesizedExpression:
2165  case CodeCompletionContext::CCC_ObjCInterfaceName:
2166    break;
2167
2168  case CodeCompletionContext::CCC_EnumTag:
2169  case CodeCompletionContext::CCC_UnionTag:
2170  case CodeCompletionContext::CCC_ClassOrStructTag:
2171    OnlyTagNames = true;
2172    break;
2173
2174  case CodeCompletionContext::CCC_ObjCProtocolName:
2175  case CodeCompletionContext::CCC_MacroName:
2176  case CodeCompletionContext::CCC_MacroNameUse:
2177  case CodeCompletionContext::CCC_PreprocessorExpression:
2178  case CodeCompletionContext::CCC_PreprocessorDirective:
2179  case CodeCompletionContext::CCC_NaturalLanguage:
2180  case CodeCompletionContext::CCC_SelectorName:
2181  case CodeCompletionContext::CCC_TypeQualifiers:
2182  case CodeCompletionContext::CCC_Other:
2183  case CodeCompletionContext::CCC_OtherWithMacros:
2184  case CodeCompletionContext::CCC_ObjCInstanceMessage:
2185  case CodeCompletionContext::CCC_ObjCClassMessage:
2186  case CodeCompletionContext::CCC_ObjCCategoryName:
2187    // We're looking for nothing, or we're looking for names that cannot
2188    // be hidden.
2189    return;
2190  }
2191
2192  typedef CodeCompletionResult Result;
2193  for (unsigned I = 0; I != NumResults; ++I) {
2194    if (Results[I].Kind != Result::RK_Declaration)
2195      continue;
2196
2197    unsigned IDNS
2198      = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2199
2200    bool Hiding = false;
2201    if (OnlyTagNames)
2202      Hiding = (IDNS & Decl::IDNS_Tag);
2203    else {
2204      unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2205                             Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2206                             Decl::IDNS_NonMemberOperator);
2207      if (Ctx.getLangOpts().CPlusPlus)
2208        HiddenIDNS |= Decl::IDNS_Tag;
2209      Hiding = (IDNS & HiddenIDNS);
2210    }
2211
2212    if (!Hiding)
2213      continue;
2214
2215    DeclarationName Name = Results[I].Declaration->getDeclName();
2216    if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2217      HiddenNames.insert(Identifier->getName());
2218    else
2219      HiddenNames.insert(Name.getAsString());
2220  }
2221}
2222
2223
2224void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2225                                            CodeCompletionContext Context,
2226                                            CodeCompletionResult *Results,
2227                                            unsigned NumResults) {
2228  // Merge the results we were given with the results we cached.
2229  bool AddedResult = false;
2230  uint64_t InContexts =
2231      Context.getKind() == CodeCompletionContext::CCC_Recovery
2232        ? NormalContexts : (1LL << Context.getKind());
2233  // Contains the set of names that are hidden by "local" completion results.
2234  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2235  typedef CodeCompletionResult Result;
2236  SmallVector<Result, 8> AllResults;
2237  for (ASTUnit::cached_completion_iterator
2238            C = AST.cached_completion_begin(),
2239         CEnd = AST.cached_completion_end();
2240       C != CEnd; ++C) {
2241    // If the context we are in matches any of the contexts we are
2242    // interested in, we'll add this result.
2243    if ((C->ShowInContexts & InContexts) == 0)
2244      continue;
2245
2246    // If we haven't added any results previously, do so now.
2247    if (!AddedResult) {
2248      CalculateHiddenNames(Context, Results, NumResults, S.Context,
2249                           HiddenNames);
2250      AllResults.insert(AllResults.end(), Results, Results + NumResults);
2251      AddedResult = true;
2252    }
2253
2254    // Determine whether this global completion result is hidden by a local
2255    // completion result. If so, skip it.
2256    if (C->Kind != CXCursor_MacroDefinition &&
2257        HiddenNames.count(C->Completion->getTypedText()))
2258      continue;
2259
2260    // Adjust priority based on similar type classes.
2261    unsigned Priority = C->Priority;
2262    CodeCompletionString *Completion = C->Completion;
2263    if (!Context.getPreferredType().isNull()) {
2264      if (C->Kind == CXCursor_MacroDefinition) {
2265        Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2266                                         S.getLangOpts(),
2267                               Context.getPreferredType()->isAnyPointerType());
2268      } else if (C->Type) {
2269        CanQualType Expected
2270          = S.Context.getCanonicalType(
2271                               Context.getPreferredType().getUnqualifiedType());
2272        SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2273        if (ExpectedSTC == C->TypeClass) {
2274          // We know this type is similar; check for an exact match.
2275          llvm::StringMap<unsigned> &CachedCompletionTypes
2276            = AST.getCachedCompletionTypes();
2277          llvm::StringMap<unsigned>::iterator Pos
2278            = CachedCompletionTypes.find(QualType(Expected).getAsString());
2279          if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2280            Priority /= CCF_ExactTypeMatch;
2281          else
2282            Priority /= CCF_SimilarTypeMatch;
2283        }
2284      }
2285    }
2286
2287    // Adjust the completion string, if required.
2288    if (C->Kind == CXCursor_MacroDefinition &&
2289        Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2290      // Create a new code-completion string that just contains the
2291      // macro name, without its arguments.
2292      CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(),
2293                                    CCP_CodePattern, C->Availability);
2294      Builder.AddTypedTextChunk(C->Completion->getTypedText());
2295      Priority = CCP_CodePattern;
2296      Completion = Builder.TakeString();
2297    }
2298
2299    AllResults.push_back(Result(Completion, Priority, C->Kind,
2300                                C->Availability));
2301  }
2302
2303  // If we did not add any cached completion results, just forward the
2304  // results we were given to the next consumer.
2305  if (!AddedResult) {
2306    Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2307    return;
2308  }
2309
2310  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2311                                  AllResults.size());
2312}
2313
2314
2315
2316void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2317                           RemappedFile *RemappedFiles,
2318                           unsigned NumRemappedFiles,
2319                           bool IncludeMacros,
2320                           bool IncludeCodePatterns,
2321                           bool IncludeBriefComments,
2322                           CodeCompleteConsumer &Consumer,
2323                           DiagnosticsEngine &Diag, LangOptions &LangOpts,
2324                           SourceManager &SourceMgr, FileManager &FileMgr,
2325                   SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2326             SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2327  if (!Invocation)
2328    return;
2329
2330  SimpleTimer CompletionTimer(WantTiming);
2331  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2332                            Twine(Line) + ":" + Twine(Column));
2333
2334  IntrusiveRefCntPtr<CompilerInvocation>
2335    CCInvocation(new CompilerInvocation(*Invocation));
2336
2337  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2338  CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts;
2339  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2340
2341  CodeCompleteOpts.IncludeMacros = IncludeMacros &&
2342                                   CachedCompletionResults.empty();
2343  CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns;
2344  CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty();
2345  CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments;
2346
2347  assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion);
2348
2349  FrontendOpts.CodeCompletionAt.FileName = File;
2350  FrontendOpts.CodeCompletionAt.Line = Line;
2351  FrontendOpts.CodeCompletionAt.Column = Column;
2352
2353  // Set the language options appropriately.
2354  LangOpts = *CCInvocation->getLangOpts();
2355
2356  OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2357
2358  // Recover resources if we crash before exiting this method.
2359  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2360    CICleanup(Clang.get());
2361
2362  Clang->setInvocation(&*CCInvocation);
2363  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File;
2364
2365  // Set up diagnostics, capturing any diagnostics produced.
2366  Clang->setDiagnostics(&Diag);
2367  ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2368  CaptureDroppedDiagnostics Capture(true,
2369                                    Clang->getDiagnostics(),
2370                                    StoredDiagnostics);
2371
2372  // Create the target instance.
2373  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2374                                               Clang->getTargetOpts()));
2375  if (!Clang->hasTarget()) {
2376    Clang->setInvocation(0);
2377    return;
2378  }
2379
2380  // Inform the target of the language options.
2381  //
2382  // FIXME: We shouldn't need to do this, the target should be immutable once
2383  // created. This complexity should be lifted elsewhere.
2384  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2385
2386  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2387         "Invocation must have exactly one source file!");
2388  assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST &&
2389         "FIXME: AST inputs not yet supported here!");
2390  assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR &&
2391         "IR inputs not support here!");
2392
2393
2394  // Use the source and file managers that we were given.
2395  Clang->setFileManager(&FileMgr);
2396  Clang->setSourceManager(&SourceMgr);
2397
2398  // Remap files.
2399  PreprocessorOpts.clearRemappedFiles();
2400  PreprocessorOpts.RetainRemappedFileBuffers = true;
2401  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2402    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2403    if (const llvm::MemoryBuffer *
2404            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2405      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2406      OwnedBuffers.push_back(memBuf);
2407    } else {
2408      const char *fname = fileOrBuf.get<const char *>();
2409      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2410    }
2411  }
2412
2413  // Use the code completion consumer we were given, but adding any cached
2414  // code-completion results.
2415  AugmentedCodeCompleteConsumer *AugmentedConsumer
2416    = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts);
2417  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2418
2419  // If we have a precompiled preamble, try to use it. We only allow
2420  // the use of the precompiled preamble if we're if the completion
2421  // point is within the main file, after the end of the precompiled
2422  // preamble.
2423  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2424  if (!getPreambleFile(this).empty()) {
2425    using llvm::sys::FileStatus;
2426    llvm::sys::PathWithStatus CompleteFilePath(File);
2427    llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2428    if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2429      if (const FileStatus *MainStatus = MainPath.getFileStatus())
2430        if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() &&
2431            Line > 1)
2432          OverrideMainBuffer
2433            = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2434                                                   Line - 1);
2435  }
2436
2437  // If the main file has been overridden due to the use of a preamble,
2438  // make that override happen and introduce the preamble.
2439  PreprocessorOpts.DisableStatCache = true;
2440  StoredDiagnostics.insert(StoredDiagnostics.end(),
2441                           stored_diag_begin(),
2442                           stored_diag_afterDriver_begin());
2443  if (OverrideMainBuffer) {
2444    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2445    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2446    PreprocessorOpts.PrecompiledPreambleBytes.second
2447                                                    = PreambleEndsAtStartOfLine;
2448    PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this);
2449    PreprocessorOpts.DisablePCHValidation = true;
2450
2451    OwnedBuffers.push_back(OverrideMainBuffer);
2452  } else {
2453    PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2454    PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2455  }
2456
2457  // Disable the preprocessing record
2458  PreprocessorOpts.DetailedRecord = false;
2459
2460  OwningPtr<SyntaxOnlyAction> Act;
2461  Act.reset(new SyntaxOnlyAction);
2462  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) {
2463    if (OverrideMainBuffer) {
2464      std::string ModName = getPreambleFile(this);
2465      TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
2466                                 getSourceManager(), PreambleDiagnostics,
2467                                 StoredDiagnostics);
2468    }
2469    Act->Execute();
2470    Act->EndSourceFile();
2471  }
2472
2473  checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
2474}
2475
2476bool ASTUnit::Save(StringRef File) {
2477  // Write to a temporary file and later rename it to the actual file, to avoid
2478  // possible race conditions.
2479  SmallString<128> TempPath;
2480  TempPath = File;
2481  TempPath += "-%%%%%%%%";
2482  int fd;
2483  if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
2484                                 /*makeAbsolute=*/false))
2485    return true;
2486
2487  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2488  // unconditionally create a stat cache when we parse the file?
2489  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2490
2491  serialize(Out);
2492  Out.close();
2493  if (Out.has_error()) {
2494    Out.clear_error();
2495    return true;
2496  }
2497
2498  if (llvm::sys::fs::rename(TempPath.str(), File)) {
2499    bool exists;
2500    llvm::sys::fs::remove(TempPath.str(), exists);
2501    return true;
2502  }
2503
2504  return false;
2505}
2506
2507static bool serializeUnit(ASTWriter &Writer,
2508                          SmallVectorImpl<char> &Buffer,
2509                          Sema &S,
2510                          bool hasErrors,
2511                          raw_ostream &OS) {
2512  Writer.WriteAST(S, 0, std::string(), 0, "", hasErrors);
2513
2514  // Write the generated bitstream to "Out".
2515  if (!Buffer.empty())
2516    OS.write(Buffer.data(), Buffer.size());
2517
2518  return false;
2519}
2520
2521bool ASTUnit::serialize(raw_ostream &OS) {
2522  bool hasErrors = getDiagnostics().hasErrorOccurred();
2523
2524  if (WriterData)
2525    return serializeUnit(WriterData->Writer, WriterData->Buffer,
2526                         getSema(), hasErrors, OS);
2527
2528  SmallString<128> Buffer;
2529  llvm::BitstreamWriter Stream(Buffer);
2530  ASTWriter Writer(Stream);
2531  return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS);
2532}
2533
2534typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2535
2536static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2537  unsigned Raw = L.getRawEncoding();
2538  const unsigned MacroBit = 1U << 31;
2539  L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2540      ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2541}
2542
2543void ASTUnit::TranslateStoredDiagnostics(
2544                          ASTReader *MMan,
2545                          StringRef ModName,
2546                          SourceManager &SrcMgr,
2547                          const SmallVectorImpl<StoredDiagnostic> &Diags,
2548                          SmallVectorImpl<StoredDiagnostic> &Out) {
2549  // The stored diagnostic has the old source manager in it; update
2550  // the locations to refer into the new source manager. We also need to remap
2551  // all the locations to the new view. This includes the diag location, any
2552  // associated source ranges, and the source ranges of associated fix-its.
2553  // FIXME: There should be a cleaner way to do this.
2554
2555  SmallVector<StoredDiagnostic, 4> Result;
2556  Result.reserve(Diags.size());
2557  assert(MMan && "Don't have a module manager");
2558  serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName);
2559  assert(Mod && "Don't have preamble module");
2560  SLocRemap &Remap = Mod->SLocRemap;
2561  for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2562    // Rebuild the StoredDiagnostic.
2563    const StoredDiagnostic &SD = Diags[I];
2564    SourceLocation L = SD.getLocation();
2565    TranslateSLoc(L, Remap);
2566    FullSourceLoc Loc(L, SrcMgr);
2567
2568    SmallVector<CharSourceRange, 4> Ranges;
2569    Ranges.reserve(SD.range_size());
2570    for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2571                                          E = SD.range_end();
2572         I != E; ++I) {
2573      SourceLocation BL = I->getBegin();
2574      TranslateSLoc(BL, Remap);
2575      SourceLocation EL = I->getEnd();
2576      TranslateSLoc(EL, Remap);
2577      Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2578    }
2579
2580    SmallVector<FixItHint, 2> FixIts;
2581    FixIts.reserve(SD.fixit_size());
2582    for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2583                                          E = SD.fixit_end();
2584         I != E; ++I) {
2585      FixIts.push_back(FixItHint());
2586      FixItHint &FH = FixIts.back();
2587      FH.CodeToInsert = I->CodeToInsert;
2588      SourceLocation BL = I->RemoveRange.getBegin();
2589      TranslateSLoc(BL, Remap);
2590      SourceLocation EL = I->RemoveRange.getEnd();
2591      TranslateSLoc(EL, Remap);
2592      FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2593                                       I->RemoveRange.isTokenRange());
2594    }
2595
2596    Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2597                                      SD.getMessage(), Loc, Ranges, FixIts));
2598  }
2599  Result.swap(Out);
2600}
2601
2602static inline bool compLocDecl(std::pair<unsigned, Decl *> L,
2603                               std::pair<unsigned, Decl *> R) {
2604  return L.first < R.first;
2605}
2606
2607void ASTUnit::addFileLevelDecl(Decl *D) {
2608  assert(D);
2609
2610  // We only care about local declarations.
2611  if (D->isFromASTFile())
2612    return;
2613
2614  SourceManager &SM = *SourceMgr;
2615  SourceLocation Loc = D->getLocation();
2616  if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc))
2617    return;
2618
2619  // We only keep track of the file-level declarations of each file.
2620  if (!D->getLexicalDeclContext()->isFileContext())
2621    return;
2622
2623  SourceLocation FileLoc = SM.getFileLoc(Loc);
2624  assert(SM.isLocalSourceLocation(FileLoc));
2625  FileID FID;
2626  unsigned Offset;
2627  llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
2628  if (FID.isInvalid())
2629    return;
2630
2631  LocDeclsTy *&Decls = FileDecls[FID];
2632  if (!Decls)
2633    Decls = new LocDeclsTy();
2634
2635  std::pair<unsigned, Decl *> LocDecl(Offset, D);
2636
2637  if (Decls->empty() || Decls->back().first <= Offset) {
2638    Decls->push_back(LocDecl);
2639    return;
2640  }
2641
2642  LocDeclsTy::iterator
2643    I = std::upper_bound(Decls->begin(), Decls->end(), LocDecl, compLocDecl);
2644
2645  Decls->insert(I, LocDecl);
2646}
2647
2648void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
2649                                  SmallVectorImpl<Decl *> &Decls) {
2650  if (File.isInvalid())
2651    return;
2652
2653  if (SourceMgr->isLoadedFileID(File)) {
2654    assert(Ctx->getExternalSource() && "No external source!");
2655    return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length,
2656                                                         Decls);
2657  }
2658
2659  FileDeclsTy::iterator I = FileDecls.find(File);
2660  if (I == FileDecls.end())
2661    return;
2662
2663  LocDeclsTy &LocDecls = *I->second;
2664  if (LocDecls.empty())
2665    return;
2666
2667  LocDeclsTy::iterator
2668    BeginIt = std::lower_bound(LocDecls.begin(), LocDecls.end(),
2669                               std::make_pair(Offset, (Decl*)0), compLocDecl);
2670  if (BeginIt != LocDecls.begin())
2671    --BeginIt;
2672
2673  // If we are pointing at a top-level decl inside an objc container, we need
2674  // to backtrack until we find it otherwise we will fail to report that the
2675  // region overlaps with an objc container.
2676  while (BeginIt != LocDecls.begin() &&
2677         BeginIt->second->isTopLevelDeclInObjCContainer())
2678    --BeginIt;
2679
2680  LocDeclsTy::iterator
2681    EndIt = std::upper_bound(LocDecls.begin(), LocDecls.end(),
2682                             std::make_pair(Offset+Length, (Decl*)0),
2683                             compLocDecl);
2684  if (EndIt != LocDecls.end())
2685    ++EndIt;
2686
2687  for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt)
2688    Decls.push_back(DIt->second);
2689}
2690
2691SourceLocation ASTUnit::getLocation(const FileEntry *File,
2692                                    unsigned Line, unsigned Col) const {
2693  const SourceManager &SM = getSourceManager();
2694  SourceLocation Loc = SM.translateFileLineCol(File, Line, Col);
2695  return SM.getMacroArgExpandedLocation(Loc);
2696}
2697
2698SourceLocation ASTUnit::getLocation(const FileEntry *File,
2699                                    unsigned Offset) const {
2700  const SourceManager &SM = getSourceManager();
2701  SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1);
2702  return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset));
2703}
2704
2705/// \brief If \arg Loc is a loaded location from the preamble, returns
2706/// the corresponding local location of the main file, otherwise it returns
2707/// \arg Loc.
2708SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) {
2709  FileID PreambleID;
2710  if (SourceMgr)
2711    PreambleID = SourceMgr->getPreambleFileID();
2712
2713  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2714    return Loc;
2715
2716  unsigned Offs;
2717  if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) {
2718    SourceLocation FileLoc
2719        = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID());
2720    return FileLoc.getLocWithOffset(Offs);
2721  }
2722
2723  return Loc;
2724}
2725
2726/// \brief If \arg Loc is a local location of the main file but inside the
2727/// preamble chunk, returns the corresponding loaded location from the
2728/// preamble, otherwise it returns \arg Loc.
2729SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) {
2730  FileID PreambleID;
2731  if (SourceMgr)
2732    PreambleID = SourceMgr->getPreambleFileID();
2733
2734  if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid())
2735    return Loc;
2736
2737  unsigned Offs;
2738  if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) &&
2739      Offs < Preamble.size()) {
2740    SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID);
2741    return FileLoc.getLocWithOffset(Offs);
2742  }
2743
2744  return Loc;
2745}
2746
2747bool ASTUnit::isInPreambleFileID(SourceLocation Loc) {
2748  FileID FID;
2749  if (SourceMgr)
2750    FID = SourceMgr->getPreambleFileID();
2751
2752  if (Loc.isInvalid() || FID.isInvalid())
2753    return false;
2754
2755  return SourceMgr->isInFileID(Loc, FID);
2756}
2757
2758bool ASTUnit::isInMainFileID(SourceLocation Loc) {
2759  FileID FID;
2760  if (SourceMgr)
2761    FID = SourceMgr->getMainFileID();
2762
2763  if (Loc.isInvalid() || FID.isInvalid())
2764    return false;
2765
2766  return SourceMgr->isInFileID(Loc, FID);
2767}
2768
2769SourceLocation ASTUnit::getEndOfPreambleFileID() {
2770  FileID FID;
2771  if (SourceMgr)
2772    FID = SourceMgr->getPreambleFileID();
2773
2774  if (FID.isInvalid())
2775    return SourceLocation();
2776
2777  return SourceMgr->getLocForEndOfFile(FID);
2778}
2779
2780SourceLocation ASTUnit::getStartOfMainFileID() {
2781  FileID FID;
2782  if (SourceMgr)
2783    FID = SourceMgr->getMainFileID();
2784
2785  if (FID.isInvalid())
2786    return SourceLocation();
2787
2788  return SourceMgr->getLocForStartOfFile(FID);
2789}
2790
2791std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
2792ASTUnit::getLocalPreprocessingEntities() const {
2793  if (isMainFileAST()) {
2794    serialization::ModuleFile &
2795      Mod = Reader->getModuleManager().getPrimaryModule();
2796    return Reader->getModulePreprocessedEntities(Mod);
2797  }
2798
2799  if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord())
2800    return std::make_pair(PPRec->local_begin(), PPRec->local_end());
2801
2802  return std::make_pair(PreprocessingRecord::iterator(),
2803                        PreprocessingRecord::iterator());
2804}
2805
2806bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
2807  if (isMainFileAST()) {
2808    serialization::ModuleFile &
2809      Mod = Reader->getModuleManager().getPrimaryModule();
2810    ASTReader::ModuleDeclIterator MDI, MDE;
2811    llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod);
2812    for (; MDI != MDE; ++MDI) {
2813      if (!Fn(context, *MDI))
2814        return false;
2815    }
2816
2817    return true;
2818  }
2819
2820  for (ASTUnit::top_level_iterator TL = top_level_begin(),
2821                                TLEnd = top_level_end();
2822         TL != TLEnd; ++TL) {
2823    if (!Fn(context, *TL))
2824      return false;
2825  }
2826
2827  return true;
2828}
2829
2830namespace {
2831struct PCHLocatorInfo {
2832  serialization::ModuleFile *Mod;
2833  PCHLocatorInfo() : Mod(0) {}
2834};
2835}
2836
2837static bool PCHLocator(serialization::ModuleFile &M, void *UserData) {
2838  PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData);
2839  switch (M.Kind) {
2840  case serialization::MK_Module:
2841    return true; // skip dependencies.
2842  case serialization::MK_PCH:
2843    Info.Mod = &M;
2844    return true; // found it.
2845  case serialization::MK_Preamble:
2846    return false; // look in dependencies.
2847  case serialization::MK_MainFile:
2848    return false; // look in dependencies.
2849  }
2850
2851  return true;
2852}
2853
2854const FileEntry *ASTUnit::getPCHFile() {
2855  if (!Reader)
2856    return 0;
2857
2858  PCHLocatorInfo Info;
2859  Reader->getModuleManager().visit(PCHLocator, &Info);
2860  if (Info.Mod)
2861    return Info.Mod->File;
2862
2863  return 0;
2864}
2865
2866bool ASTUnit::isModuleFile() {
2867  return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
2868}
2869
2870void ASTUnit::PreambleData::countLines() const {
2871  NumLines = 0;
2872  if (empty())
2873    return;
2874
2875  for (std::vector<char>::const_iterator
2876         I = Buffer.begin(), E = Buffer.end(); I != E; ++I) {
2877    if (*I == '\n')
2878      ++NumLines;
2879  }
2880  if (Buffer.back() != '\n')
2881    ++NumLines;
2882}
2883
2884#ifndef NDEBUG
2885ASTUnit::ConcurrencyState::ConcurrencyState() {
2886  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
2887}
2888
2889ASTUnit::ConcurrencyState::~ConcurrencyState() {
2890  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
2891}
2892
2893void ASTUnit::ConcurrencyState::start() {
2894  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
2895  assert(acquired && "Concurrent access to ASTUnit!");
2896}
2897
2898void ASTUnit::ConcurrencyState::finish() {
2899  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
2900}
2901
2902#else // NDEBUG
2903
2904ASTUnit::ConcurrencyState::ConcurrencyState() {}
2905ASTUnit::ConcurrencyState::~ConcurrencyState() {}
2906void ASTUnit::ConcurrencyState::start() {}
2907void ASTUnit::ConcurrencyState::finish() {}
2908
2909#endif
2910