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