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