ASTUnit.cpp revision 9293ba8e26fcba18505b273ecc9b66645133fcce
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    TUKind(TU_Complete), 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 TranslationUnitKind getTranslationUnitKind()  {
763    return Unit.getTranslationUnitKind();
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, const Preprocessor &PP,
775                             StringRef isysroot, raw_ostream *Out)
776    : PCHGenerator(PP, "", isysroot, Out), Unit(Unit),
777      Hash(Unit.getCurrentTopLevelHashValue()) {
778    Hash = 0;
779  }
780
781  virtual void HandleTopLevelDecl(DeclGroupRef D) {
782    for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
783      Decl *D = *it;
784      // FIXME: Currently ObjC method declarations are incorrectly being
785      // reported as top-level declarations, even though their DeclContext
786      // is the containing ObjC @interface/@implementation.  This is a
787      // fundamental problem in the parser right now.
788      if (isa<ObjCMethodDecl>(D))
789        continue;
790      AddTopLevelDeclarationToHash(D, Hash);
791      TopLevelDecls.push_back(D);
792    }
793  }
794
795  virtual void HandleTranslationUnit(ASTContext &Ctx) {
796    PCHGenerator::HandleTranslationUnit(Ctx);
797    if (!Unit.getDiagnostics().hasErrorOccurred()) {
798      // Translate the top-level declarations we captured during
799      // parsing into declaration IDs in the precompiled
800      // preamble. This will allow us to deserialize those top-level
801      // declarations when requested.
802      for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
803        Unit.addTopLevelDeclFromPreamble(
804                                      getWriter().getDeclID(TopLevelDecls[I]));
805    }
806  }
807
808  virtual void SerializedPreprocessedEntity(PreprocessedEntity *Entity,
809                                            uint64_t Offset) {
810    Unit.addPreprocessedEntityFromPreamble(Offset);
811  }
812
813  virtual ASTSerializationListener *GetASTSerializationListener() {
814    return this;
815  }
816};
817
818class PrecompilePreambleAction : public ASTFrontendAction {
819  ASTUnit &Unit;
820
821public:
822  explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {}
823
824  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
825                                         StringRef InFile) {
826    std::string Sysroot;
827    std::string OutputFile;
828    raw_ostream *OS = 0;
829    if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot,
830                                                       OutputFile,
831                                                       OS))
832      return 0;
833
834    if (!CI.getFrontendOpts().RelocatablePCH)
835      Sysroot.clear();
836
837    CI.getPreprocessor().addPPCallbacks(
838     new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue()));
839    return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot,
840                                          OS);
841  }
842
843  virtual bool hasCodeCompletionSupport() const { return false; }
844  virtual bool hasASTFileSupport() const { return false; }
845  virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
846};
847
848}
849
850/// Parse the source file into a translation unit using the given compiler
851/// invocation, replacing the current translation unit.
852///
853/// \returns True if a failure occurred that causes the ASTUnit not to
854/// contain any translation-unit information, false otherwise.
855bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) {
856  delete SavedMainFileBuffer;
857  SavedMainFileBuffer = 0;
858
859  if (!Invocation) {
860    delete OverrideMainBuffer;
861    return true;
862  }
863
864  // Create the compiler instance to use for building the AST.
865  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
866
867  // Recover resources if we crash before exiting this method.
868  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
869    CICleanup(Clang.get());
870
871  Clang->setInvocation(&*Invocation);
872  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
873
874  // Set up diagnostics, capturing any diagnostics that would
875  // otherwise be dropped.
876  Clang->setDiagnostics(&getDiagnostics());
877
878  // Create the target instance.
879  Clang->getTargetOpts().Features = TargetFeatures;
880  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
881                   Clang->getTargetOpts()));
882  if (!Clang->hasTarget()) {
883    delete OverrideMainBuffer;
884    return true;
885  }
886
887  // Inform the target of the language options.
888  //
889  // FIXME: We shouldn't need to do this, the target should be immutable once
890  // created. This complexity should be lifted elsewhere.
891  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
892
893  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
894         "Invocation must have exactly one source file!");
895  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
896         "FIXME: AST inputs not yet supported here!");
897  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
898         "IR inputs not support here!");
899
900  // Configure the various subsystems.
901  // FIXME: Should we retain the previous file manager?
902  FileSystemOpts = Clang->getFileSystemOpts();
903  FileMgr = new FileManager(FileSystemOpts);
904  SourceMgr = new SourceManager(getDiagnostics(), *FileMgr);
905  TheSema.reset();
906  Ctx = 0;
907  PP = 0;
908
909  // Clear out old caches and data.
910  TopLevelDecls.clear();
911  PreprocessedEntities.clear();
912  CleanTemporaryFiles();
913  PreprocessedEntitiesByFile.clear();
914
915  if (!OverrideMainBuffer) {
916    StoredDiagnostics.erase(
917                    StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver,
918                            StoredDiagnostics.end());
919    TopLevelDeclsInPreamble.clear();
920    PreprocessedEntitiesInPreamble.clear();
921  }
922
923  // Create a file manager object to provide access to and cache the filesystem.
924  Clang->setFileManager(&getFileManager());
925
926  // Create the source manager.
927  Clang->setSourceManager(&getSourceManager());
928
929  // If the main file has been overridden due to the use of a preamble,
930  // make that override happen and introduce the preamble.
931  PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts();
932  PreprocessorOpts.DetailedRecordIncludesNestedMacroExpansions
933    = NestedMacroExpansions;
934  std::string PriorImplicitPCHInclude;
935  if (OverrideMainBuffer) {
936    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
937    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
938    PreprocessorOpts.PrecompiledPreambleBytes.second
939                                                    = PreambleEndsAtStartOfLine;
940    PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude;
941    PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
942    PreprocessorOpts.DisablePCHValidation = true;
943
944    // The stored diagnostic has the old source manager in it; update
945    // the locations to refer into the new source manager. Since we've
946    // been careful to make sure that the source manager's state
947    // before and after are identical, so that we can reuse the source
948    // location itself.
949    for (unsigned I = NumStoredDiagnosticsFromDriver,
950                  N = StoredDiagnostics.size();
951         I < N; ++I) {
952      FullSourceLoc Loc(StoredDiagnostics[I].getLocation(),
953                        getSourceManager());
954      StoredDiagnostics[I].setLocation(Loc);
955    }
956
957    // Keep track of the override buffer;
958    SavedMainFileBuffer = OverrideMainBuffer;
959  } else {
960    PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
961    PreprocessorOpts.PrecompiledPreambleBytes.second = false;
962  }
963
964  llvm::OwningPtr<TopLevelDeclTrackerAction> Act(
965    new TopLevelDeclTrackerAction(*this));
966
967  // Recover resources if we crash before exiting this method.
968  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
969    ActCleanup(Act.get());
970
971  if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
972                            Clang->getFrontendOpts().Inputs[0].first))
973    goto error;
974
975  if (OverrideMainBuffer) {
976    std::string ModName = PreambleFile;
977    TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
978                               getSourceManager(), PreambleDiagnostics,
979                               StoredDiagnostics);
980  }
981
982  Act->Execute();
983
984  // Steal the created target, context, and preprocessor.
985  TheSema.reset(Clang->takeSema());
986  Consumer.reset(Clang->takeASTConsumer());
987  Ctx = &Clang->getASTContext();
988  PP = &Clang->getPreprocessor();
989  Clang->setSourceManager(0);
990  Clang->setFileManager(0);
991  Target = &Clang->getTarget();
992
993  Act->EndSourceFile();
994
995  // Remove the overridden buffer we used for the preamble.
996  if (OverrideMainBuffer) {
997    PreprocessorOpts.eraseRemappedFile(
998                               PreprocessorOpts.remapped_file_buffer_end() - 1);
999    PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
1000  }
1001
1002  return false;
1003
1004error:
1005  // Remove the overridden buffer we used for the preamble.
1006  if (OverrideMainBuffer) {
1007    PreprocessorOpts.eraseRemappedFile(
1008                               PreprocessorOpts.remapped_file_buffer_end() - 1);
1009    PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude;
1010    delete OverrideMainBuffer;
1011    SavedMainFileBuffer = 0;
1012  }
1013
1014  StoredDiagnostics.clear();
1015  return true;
1016}
1017
1018/// \brief Simple function to retrieve a path for a preamble precompiled header.
1019static std::string GetPreamblePCHPath() {
1020  // FIXME: This is lame; sys::Path should provide this function (in particular,
1021  // it should know how to find the temporary files dir).
1022  // FIXME: This is really lame. I copied this code from the Driver!
1023  // FIXME: This is a hack so that we can override the preamble file during
1024  // crash-recovery testing, which is the only case where the preamble files
1025  // are not necessarily cleaned up.
1026  const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE");
1027  if (TmpFile)
1028    return TmpFile;
1029
1030  std::string Error;
1031  const char *TmpDir = ::getenv("TMPDIR");
1032  if (!TmpDir)
1033    TmpDir = ::getenv("TEMP");
1034  if (!TmpDir)
1035    TmpDir = ::getenv("TMP");
1036#ifdef LLVM_ON_WIN32
1037  if (!TmpDir)
1038    TmpDir = ::getenv("USERPROFILE");
1039#endif
1040  if (!TmpDir)
1041    TmpDir = "/tmp";
1042  llvm::sys::Path P(TmpDir);
1043  P.createDirectoryOnDisk(true);
1044  P.appendComponent("preamble");
1045  P.appendSuffix("pch");
1046  if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0))
1047    return std::string();
1048
1049  return P.str();
1050}
1051
1052/// \brief Compute the preamble for the main file, providing the source buffer
1053/// that corresponds to the main file along with a pair (bytes, start-of-line)
1054/// that describes the preamble.
1055std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
1056ASTUnit::ComputePreamble(CompilerInvocation &Invocation,
1057                         unsigned MaxLines, bool &CreatedBuffer) {
1058  FrontendOptions &FrontendOpts = Invocation.getFrontendOpts();
1059  PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts();
1060  CreatedBuffer = false;
1061
1062  // Try to determine if the main file has been remapped, either from the
1063  // command line (to another file) or directly through the compiler invocation
1064  // (to a memory buffer).
1065  llvm::MemoryBuffer *Buffer = 0;
1066  llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second);
1067  if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) {
1068    // Check whether there is a file-file remapping of the main file
1069    for (PreprocessorOptions::remapped_file_iterator
1070          M = PreprocessorOpts.remapped_file_begin(),
1071          E = PreprocessorOpts.remapped_file_end();
1072         M != E;
1073         ++M) {
1074      llvm::sys::PathWithStatus MPath(M->first);
1075      if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1076        if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1077          // We found a remapping. Try to load the resulting, remapped source.
1078          if (CreatedBuffer) {
1079            delete Buffer;
1080            CreatedBuffer = false;
1081          }
1082
1083          Buffer = getBufferForFile(M->second);
1084          if (!Buffer)
1085            return std::make_pair((llvm::MemoryBuffer*)0,
1086                                  std::make_pair(0, true));
1087          CreatedBuffer = true;
1088        }
1089      }
1090    }
1091
1092    // Check whether there is a file-buffer remapping. It supercedes the
1093    // file-file remapping.
1094    for (PreprocessorOptions::remapped_file_buffer_iterator
1095           M = PreprocessorOpts.remapped_file_buffer_begin(),
1096           E = PreprocessorOpts.remapped_file_buffer_end();
1097         M != E;
1098         ++M) {
1099      llvm::sys::PathWithStatus MPath(M->first);
1100      if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) {
1101        if (MainFileStatus->uniqueID == MStatus->uniqueID) {
1102          // We found a remapping.
1103          if (CreatedBuffer) {
1104            delete Buffer;
1105            CreatedBuffer = false;
1106          }
1107
1108          Buffer = const_cast<llvm::MemoryBuffer *>(M->second);
1109        }
1110      }
1111    }
1112  }
1113
1114  // If the main source file was not remapped, load it now.
1115  if (!Buffer) {
1116    Buffer = getBufferForFile(FrontendOpts.Inputs[0].second);
1117    if (!Buffer)
1118      return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true));
1119
1120    CreatedBuffer = true;
1121  }
1122
1123  return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer,
1124                                                       Invocation.getLangOpts(),
1125                                                       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->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
1594  AST->ShouldCacheCodeCompletionResults = false;
1595  AST->Invocation = CI;
1596
1597  // Recover resources if we crash before exiting this method.
1598  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1599    ASTUnitCleanup(AST.get());
1600  llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1601    llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1602    DiagCleanup(Diags.getPtr());
1603
1604  // We'll manage file buffers ourselves.
1605  CI->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1606  CI->getFrontendOpts().DisableFree = false;
1607  ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts());
1608
1609  // Save the target features.
1610  AST->TargetFeatures = CI->getTargetOpts().Features;
1611
1612  // Create the compiler instance to use for building the AST.
1613  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
1614
1615  // Recover resources if we crash before exiting this method.
1616  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
1617    CICleanup(Clang.get());
1618
1619  Clang->setInvocation(CI);
1620  AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
1621
1622  // Set up diagnostics, capturing any diagnostics that would
1623  // otherwise be dropped.
1624  Clang->setDiagnostics(&AST->getDiagnostics());
1625
1626  // Create the target instance.
1627  Clang->getTargetOpts().Features = AST->TargetFeatures;
1628  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
1629                   Clang->getTargetOpts()));
1630  if (!Clang->hasTarget())
1631    return 0;
1632
1633  // Inform the target of the language options.
1634  //
1635  // FIXME: We shouldn't need to do this, the target should be immutable once
1636  // created. This complexity should be lifted elsewhere.
1637  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
1638
1639  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
1640         "Invocation must have exactly one source file!");
1641  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
1642         "FIXME: AST inputs not yet supported here!");
1643  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
1644         "IR inputs not supported here!");
1645
1646  // Configure the various subsystems.
1647  AST->FileSystemOpts = Clang->getFileSystemOpts();
1648  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1649  AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr);
1650  AST->TheSema.reset();
1651  AST->Ctx = 0;
1652  AST->PP = 0;
1653
1654  // Create a file manager object to provide access to and cache the filesystem.
1655  Clang->setFileManager(&AST->getFileManager());
1656
1657  // Create the source manager.
1658  Clang->setSourceManager(&AST->getSourceManager());
1659
1660  ASTFrontendAction *Act = Action;
1661
1662  llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct;
1663  if (!Act) {
1664    TrackerAct.reset(new TopLevelDeclTrackerAction(*AST));
1665    Act = TrackerAct.get();
1666  }
1667
1668  // Recover resources if we crash before exiting this method.
1669  llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
1670    ActCleanup(TrackerAct.get());
1671
1672  if (!Act->BeginSourceFile(*Clang.get(),
1673                            Clang->getFrontendOpts().Inputs[0].second,
1674                            Clang->getFrontendOpts().Inputs[0].first))
1675    return 0;
1676
1677  Act->Execute();
1678
1679  // Steal the created target, context, and preprocessor.
1680  AST->TheSema.reset(Clang->takeSema());
1681  AST->Consumer.reset(Clang->takeASTConsumer());
1682  AST->Ctx = &Clang->getASTContext();
1683  AST->PP = &Clang->getPreprocessor();
1684  Clang->setSourceManager(0);
1685  Clang->setFileManager(0);
1686  AST->Target = &Clang->getTarget();
1687
1688  Act->EndSourceFile();
1689
1690  return AST.take();
1691}
1692
1693bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) {
1694  if (!Invocation)
1695    return true;
1696
1697  // We'll manage file buffers ourselves.
1698  Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true;
1699  Invocation->getFrontendOpts().DisableFree = false;
1700  ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1701
1702  // Save the target features.
1703  TargetFeatures = Invocation->getTargetOpts().Features;
1704
1705  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1706  if (PrecompilePreamble) {
1707    PreambleRebuildCounter = 2;
1708    OverrideMainBuffer
1709      = getMainBufferWithPrecompiledPreamble(*Invocation);
1710  }
1711
1712  SimpleTimer ParsingTimer(WantTiming);
1713  ParsingTimer.setOutput("Parsing " + getMainFileName());
1714
1715  // Recover resources if we crash before exiting this method.
1716  llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer>
1717    MemBufferCleanup(OverrideMainBuffer);
1718
1719  return Parse(OverrideMainBuffer);
1720}
1721
1722ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
1723                                   llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1724                                             bool OnlyLocalDecls,
1725                                             bool CaptureDiagnostics,
1726                                             bool PrecompilePreamble,
1727                                             TranslationUnitKind TUKind,
1728                                             bool CacheCodeCompletionResults,
1729                                             bool NestedMacroExpansions) {
1730  // Create the AST unit.
1731  llvm::OwningPtr<ASTUnit> AST;
1732  AST.reset(new ASTUnit(false));
1733  ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics);
1734  AST->Diagnostics = Diags;
1735  AST->OnlyLocalDecls = OnlyLocalDecls;
1736  AST->CaptureDiagnostics = CaptureDiagnostics;
1737  AST->TUKind = TUKind;
1738  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1739  AST->Invocation = CI;
1740  AST->NestedMacroExpansions = NestedMacroExpansions;
1741
1742  // Recover resources if we crash before exiting this method.
1743  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1744    ASTUnitCleanup(AST.get());
1745  llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1746    llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1747    DiagCleanup(Diags.getPtr());
1748
1749  return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take();
1750}
1751
1752ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
1753                                      const char **ArgEnd,
1754                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
1755                                      StringRef ResourceFilesPath,
1756                                      bool OnlyLocalDecls,
1757                                      bool CaptureDiagnostics,
1758                                      RemappedFile *RemappedFiles,
1759                                      unsigned NumRemappedFiles,
1760                                      bool RemappedFilesKeepOriginalName,
1761                                      bool PrecompilePreamble,
1762                                      TranslationUnitKind TUKind,
1763                                      bool CacheCodeCompletionResults,
1764                                      bool CXXPrecompilePreamble,
1765                                      bool CXXChainedPCH,
1766                                      bool NestedMacroExpansions) {
1767  if (!Diags.getPtr()) {
1768    // No diagnostics engine was provided, so create our own diagnostics object
1769    // with the default options.
1770    DiagnosticOptions DiagOpts;
1771    Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin,
1772                                                ArgBegin);
1773  }
1774
1775  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
1776
1777  llvm::IntrusiveRefCntPtr<CompilerInvocation> CI;
1778
1779  {
1780
1781    CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags,
1782                                      StoredDiagnostics);
1783
1784    CI = clang::createInvocationFromCommandLine(
1785                                           llvm::makeArrayRef(ArgBegin, ArgEnd),
1786                                           Diags);
1787    if (!CI)
1788      return 0;
1789  }
1790
1791  // Override any files that need remapping
1792  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1793    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1794    if (const llvm::MemoryBuffer *
1795            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1796      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf);
1797    } else {
1798      const char *fname = fileOrBuf.get<const char *>();
1799      CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname);
1800    }
1801  }
1802  CI->getPreprocessorOpts().RemappedFilesKeepOriginalName =
1803                                                  RemappedFilesKeepOriginalName;
1804
1805  // Override the resources path.
1806  CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
1807
1808  // Check whether we should precompile the preamble and/or use chained PCH.
1809  // FIXME: This is a temporary hack while we debug C++ chained PCH.
1810  if (CI->getLangOpts().CPlusPlus) {
1811    PrecompilePreamble = PrecompilePreamble && CXXPrecompilePreamble;
1812
1813    if (PrecompilePreamble && !CXXChainedPCH &&
1814        !CI->getPreprocessorOpts().ImplicitPCHInclude.empty())
1815      PrecompilePreamble = false;
1816  }
1817
1818  // Create the AST unit.
1819  llvm::OwningPtr<ASTUnit> AST;
1820  AST.reset(new ASTUnit(false));
1821  ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics);
1822  AST->Diagnostics = Diags;
1823
1824  AST->FileSystemOpts = CI->getFileSystemOpts();
1825  AST->FileMgr = new FileManager(AST->FileSystemOpts);
1826  AST->OnlyLocalDecls = OnlyLocalDecls;
1827  AST->CaptureDiagnostics = CaptureDiagnostics;
1828  AST->TUKind = TUKind;
1829  AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
1830  AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
1831  AST->StoredDiagnostics.swap(StoredDiagnostics);
1832  AST->Invocation = CI;
1833  AST->NestedMacroExpansions = NestedMacroExpansions;
1834
1835  // Recover resources if we crash before exiting this method.
1836  llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit>
1837    ASTUnitCleanup(AST.get());
1838  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation,
1839    llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> >
1840    CICleanup(CI.getPtr());
1841  llvm::CrashRecoveryContextCleanupRegistrar<Diagnostic,
1842    llvm::CrashRecoveryContextReleaseRefCleanup<Diagnostic> >
1843    DiagCleanup(Diags.getPtr());
1844
1845  return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take();
1846}
1847
1848bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
1849  if (!Invocation)
1850    return true;
1851
1852  SimpleTimer ParsingTimer(WantTiming);
1853  ParsingTimer.setOutput("Reparsing " + getMainFileName());
1854
1855  // Remap files.
1856  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1857  PPOpts.DisableStatCache = true;
1858  for (PreprocessorOptions::remapped_file_buffer_iterator
1859         R = PPOpts.remapped_file_buffer_begin(),
1860         REnd = PPOpts.remapped_file_buffer_end();
1861       R != REnd;
1862       ++R) {
1863    delete R->second;
1864  }
1865  Invocation->getPreprocessorOpts().clearRemappedFiles();
1866  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
1867    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
1868    if (const llvm::MemoryBuffer *
1869            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
1870      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1871                                                        memBuf);
1872    } else {
1873      const char *fname = fileOrBuf.get<const char *>();
1874      Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
1875                                                        fname);
1876    }
1877  }
1878
1879  // If we have a preamble file lying around, or if we might try to
1880  // build a precompiled preamble, do so now.
1881  llvm::MemoryBuffer *OverrideMainBuffer = 0;
1882  if (!PreambleFile.empty() || PreambleRebuildCounter > 0)
1883    OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation);
1884
1885  // Clear out the diagnostics state.
1886  if (!OverrideMainBuffer) {
1887    getDiagnostics().Reset();
1888    ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts());
1889  }
1890
1891  // Parse the sources
1892  bool Result = Parse(OverrideMainBuffer);
1893
1894  // If we're caching global code-completion results, and the top-level
1895  // declarations have changed, clear out the code-completion cache.
1896  if (!Result && ShouldCacheCodeCompletionResults &&
1897      CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
1898    CacheCodeCompletionResults();
1899
1900  // We now need to clear out the completion allocator for
1901  // clang_getCursorCompletionString; it'll be recreated if necessary.
1902  CursorCompletionAllocator = 0;
1903
1904  return Result;
1905}
1906
1907//----------------------------------------------------------------------------//
1908// Code completion
1909//----------------------------------------------------------------------------//
1910
1911namespace {
1912  /// \brief Code completion consumer that combines the cached code-completion
1913  /// results from an ASTUnit with the code-completion results provided to it,
1914  /// then passes the result on to
1915  class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer {
1916    unsigned long long NormalContexts;
1917    ASTUnit &AST;
1918    CodeCompleteConsumer &Next;
1919
1920  public:
1921    AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next,
1922                                  bool IncludeMacros, bool IncludeCodePatterns,
1923                                  bool IncludeGlobals)
1924      : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
1925                             Next.isOutputBinary()), AST(AST), Next(Next)
1926    {
1927      // Compute the set of contexts in which we will look when we don't have
1928      // any information about the specific context.
1929      NormalContexts
1930        = (1LL << (CodeCompletionContext::CCC_TopLevel - 1))
1931        | (1LL << (CodeCompletionContext::CCC_ObjCInterface - 1))
1932        | (1LL << (CodeCompletionContext::CCC_ObjCImplementation - 1))
1933        | (1LL << (CodeCompletionContext::CCC_ObjCIvarList - 1))
1934        | (1LL << (CodeCompletionContext::CCC_Statement - 1))
1935        | (1LL << (CodeCompletionContext::CCC_Expression - 1))
1936        | (1LL << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
1937        | (1LL << (CodeCompletionContext::CCC_DotMemberAccess - 1))
1938        | (1LL << (CodeCompletionContext::CCC_ArrowMemberAccess - 1))
1939        | (1LL << (CodeCompletionContext::CCC_ObjCPropertyAccess - 1))
1940        | (1LL << (CodeCompletionContext::CCC_ObjCProtocolName - 1))
1941        | (1LL << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
1942        | (1LL << (CodeCompletionContext::CCC_Recovery - 1));
1943
1944      if (AST.getASTContext().getLangOptions().CPlusPlus)
1945        NormalContexts |= (1LL << (CodeCompletionContext::CCC_EnumTag - 1))
1946                   | (1LL << (CodeCompletionContext::CCC_UnionTag - 1))
1947                   | (1LL << (CodeCompletionContext::CCC_ClassOrStructTag - 1));
1948    }
1949
1950    virtual void ProcessCodeCompleteResults(Sema &S,
1951                                            CodeCompletionContext Context,
1952                                            CodeCompletionResult *Results,
1953                                            unsigned NumResults);
1954
1955    virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1956                                           OverloadCandidate *Candidates,
1957                                           unsigned NumCandidates) {
1958      Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1959    }
1960
1961    virtual CodeCompletionAllocator &getAllocator() {
1962      return Next.getAllocator();
1963    }
1964  };
1965}
1966
1967/// \brief Helper function that computes which global names are hidden by the
1968/// local code-completion results.
1969static void CalculateHiddenNames(const CodeCompletionContext &Context,
1970                                 CodeCompletionResult *Results,
1971                                 unsigned NumResults,
1972                                 ASTContext &Ctx,
1973                          llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){
1974  bool OnlyTagNames = false;
1975  switch (Context.getKind()) {
1976  case CodeCompletionContext::CCC_Recovery:
1977  case CodeCompletionContext::CCC_TopLevel:
1978  case CodeCompletionContext::CCC_ObjCInterface:
1979  case CodeCompletionContext::CCC_ObjCImplementation:
1980  case CodeCompletionContext::CCC_ObjCIvarList:
1981  case CodeCompletionContext::CCC_ClassStructUnion:
1982  case CodeCompletionContext::CCC_Statement:
1983  case CodeCompletionContext::CCC_Expression:
1984  case CodeCompletionContext::CCC_ObjCMessageReceiver:
1985  case CodeCompletionContext::CCC_DotMemberAccess:
1986  case CodeCompletionContext::CCC_ArrowMemberAccess:
1987  case CodeCompletionContext::CCC_ObjCPropertyAccess:
1988  case CodeCompletionContext::CCC_Namespace:
1989  case CodeCompletionContext::CCC_Type:
1990  case CodeCompletionContext::CCC_Name:
1991  case CodeCompletionContext::CCC_PotentiallyQualifiedName:
1992  case CodeCompletionContext::CCC_ParenthesizedExpression:
1993  case CodeCompletionContext::CCC_ObjCInterfaceName:
1994    break;
1995
1996  case CodeCompletionContext::CCC_EnumTag:
1997  case CodeCompletionContext::CCC_UnionTag:
1998  case CodeCompletionContext::CCC_ClassOrStructTag:
1999    OnlyTagNames = true;
2000    break;
2001
2002  case CodeCompletionContext::CCC_ObjCProtocolName:
2003  case CodeCompletionContext::CCC_MacroName:
2004  case CodeCompletionContext::CCC_MacroNameUse:
2005  case CodeCompletionContext::CCC_PreprocessorExpression:
2006  case CodeCompletionContext::CCC_PreprocessorDirective:
2007  case CodeCompletionContext::CCC_NaturalLanguage:
2008  case CodeCompletionContext::CCC_SelectorName:
2009  case CodeCompletionContext::CCC_TypeQualifiers:
2010  case CodeCompletionContext::CCC_Other:
2011  case CodeCompletionContext::CCC_OtherWithMacros:
2012  case CodeCompletionContext::CCC_ObjCInstanceMessage:
2013  case CodeCompletionContext::CCC_ObjCClassMessage:
2014  case CodeCompletionContext::CCC_ObjCCategoryName:
2015    // We're looking for nothing, or we're looking for names that cannot
2016    // be hidden.
2017    return;
2018  }
2019
2020  typedef CodeCompletionResult Result;
2021  for (unsigned I = 0; I != NumResults; ++I) {
2022    if (Results[I].Kind != Result::RK_Declaration)
2023      continue;
2024
2025    unsigned IDNS
2026      = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace();
2027
2028    bool Hiding = false;
2029    if (OnlyTagNames)
2030      Hiding = (IDNS & Decl::IDNS_Tag);
2031    else {
2032      unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member |
2033                             Decl::IDNS_Namespace | Decl::IDNS_Ordinary |
2034                             Decl::IDNS_NonMemberOperator);
2035      if (Ctx.getLangOptions().CPlusPlus)
2036        HiddenIDNS |= Decl::IDNS_Tag;
2037      Hiding = (IDNS & HiddenIDNS);
2038    }
2039
2040    if (!Hiding)
2041      continue;
2042
2043    DeclarationName Name = Results[I].Declaration->getDeclName();
2044    if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo())
2045      HiddenNames.insert(Identifier->getName());
2046    else
2047      HiddenNames.insert(Name.getAsString());
2048  }
2049}
2050
2051
2052void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S,
2053                                            CodeCompletionContext Context,
2054                                            CodeCompletionResult *Results,
2055                                            unsigned NumResults) {
2056  // Merge the results we were given with the results we cached.
2057  bool AddedResult = false;
2058  unsigned InContexts
2059    = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts
2060                                        : (1ULL << (Context.getKind() - 1)));
2061  // Contains the set of names that are hidden by "local" completion results.
2062  llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames;
2063  typedef CodeCompletionResult Result;
2064  SmallVector<Result, 8> AllResults;
2065  for (ASTUnit::cached_completion_iterator
2066            C = AST.cached_completion_begin(),
2067         CEnd = AST.cached_completion_end();
2068       C != CEnd; ++C) {
2069    // If the context we are in matches any of the contexts we are
2070    // interested in, we'll add this result.
2071    if ((C->ShowInContexts & InContexts) == 0)
2072      continue;
2073
2074    // If we haven't added any results previously, do so now.
2075    if (!AddedResult) {
2076      CalculateHiddenNames(Context, Results, NumResults, S.Context,
2077                           HiddenNames);
2078      AllResults.insert(AllResults.end(), Results, Results + NumResults);
2079      AddedResult = true;
2080    }
2081
2082    // Determine whether this global completion result is hidden by a local
2083    // completion result. If so, skip it.
2084    if (C->Kind != CXCursor_MacroDefinition &&
2085        HiddenNames.count(C->Completion->getTypedText()))
2086      continue;
2087
2088    // Adjust priority based on similar type classes.
2089    unsigned Priority = C->Priority;
2090    CXCursorKind CursorKind = C->Kind;
2091    CodeCompletionString *Completion = C->Completion;
2092    if (!Context.getPreferredType().isNull()) {
2093      if (C->Kind == CXCursor_MacroDefinition) {
2094        Priority = getMacroUsagePriority(C->Completion->getTypedText(),
2095                                         S.getLangOptions(),
2096                               Context.getPreferredType()->isAnyPointerType());
2097      } else if (C->Type) {
2098        CanQualType Expected
2099          = S.Context.getCanonicalType(
2100                               Context.getPreferredType().getUnqualifiedType());
2101        SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected);
2102        if (ExpectedSTC == C->TypeClass) {
2103          // We know this type is similar; check for an exact match.
2104          llvm::StringMap<unsigned> &CachedCompletionTypes
2105            = AST.getCachedCompletionTypes();
2106          llvm::StringMap<unsigned>::iterator Pos
2107            = CachedCompletionTypes.find(QualType(Expected).getAsString());
2108          if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type)
2109            Priority /= CCF_ExactTypeMatch;
2110          else
2111            Priority /= CCF_SimilarTypeMatch;
2112        }
2113      }
2114    }
2115
2116    // Adjust the completion string, if required.
2117    if (C->Kind == CXCursor_MacroDefinition &&
2118        Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) {
2119      // Create a new code-completion string that just contains the
2120      // macro name, without its arguments.
2121      CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern,
2122                                    C->Availability);
2123      Builder.AddTypedTextChunk(C->Completion->getTypedText());
2124      CursorKind = CXCursor_NotImplemented;
2125      Priority = CCP_CodePattern;
2126      Completion = Builder.TakeString();
2127    }
2128
2129    AllResults.push_back(Result(Completion, Priority, CursorKind,
2130                                C->Availability));
2131  }
2132
2133  // If we did not add any cached completion results, just forward the
2134  // results we were given to the next consumer.
2135  if (!AddedResult) {
2136    Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
2137    return;
2138  }
2139
2140  Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
2141                                  AllResults.size());
2142}
2143
2144
2145
2146void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column,
2147                           RemappedFile *RemappedFiles,
2148                           unsigned NumRemappedFiles,
2149                           bool IncludeMacros,
2150                           bool IncludeCodePatterns,
2151                           CodeCompleteConsumer &Consumer,
2152                           Diagnostic &Diag, LangOptions &LangOpts,
2153                           SourceManager &SourceMgr, FileManager &FileMgr,
2154                   SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
2155             SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
2156  if (!Invocation)
2157    return;
2158
2159  SimpleTimer CompletionTimer(WantTiming);
2160  CompletionTimer.setOutput("Code completion @ " + File + ":" +
2161                            Twine(Line) + ":" + Twine(Column));
2162
2163  llvm::IntrusiveRefCntPtr<CompilerInvocation>
2164    CCInvocation(new CompilerInvocation(*Invocation));
2165
2166  FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts();
2167  PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts();
2168
2169  FrontendOpts.ShowMacrosInCodeCompletion
2170    = IncludeMacros && CachedCompletionResults.empty();
2171  FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns;
2172  FrontendOpts.ShowGlobalSymbolsInCodeCompletion
2173    = CachedCompletionResults.empty();
2174  FrontendOpts.CodeCompletionAt.FileName = File;
2175  FrontendOpts.CodeCompletionAt.Line = Line;
2176  FrontendOpts.CodeCompletionAt.Column = Column;
2177
2178  // Set the language options appropriately.
2179  LangOpts = CCInvocation->getLangOpts();
2180
2181  llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
2182
2183  // Recover resources if we crash before exiting this method.
2184  llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance>
2185    CICleanup(Clang.get());
2186
2187  Clang->setInvocation(&*CCInvocation);
2188  OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second;
2189
2190  // Set up diagnostics, capturing any diagnostics produced.
2191  Clang->setDiagnostics(&Diag);
2192  ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts());
2193  CaptureDroppedDiagnostics Capture(true,
2194                                    Clang->getDiagnostics(),
2195                                    StoredDiagnostics);
2196
2197  // Create the target instance.
2198  Clang->getTargetOpts().Features = TargetFeatures;
2199  Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(),
2200                                               Clang->getTargetOpts()));
2201  if (!Clang->hasTarget()) {
2202    Clang->setInvocation(0);
2203    return;
2204  }
2205
2206  // Inform the target of the language options.
2207  //
2208  // FIXME: We shouldn't need to do this, the target should be immutable once
2209  // created. This complexity should be lifted elsewhere.
2210  Clang->getTarget().setForcedLangOptions(Clang->getLangOpts());
2211
2212  assert(Clang->getFrontendOpts().Inputs.size() == 1 &&
2213         "Invocation must have exactly one source file!");
2214  assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST &&
2215         "FIXME: AST inputs not yet supported here!");
2216  assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
2217         "IR inputs not support here!");
2218
2219
2220  // Use the source and file managers that we were given.
2221  Clang->setFileManager(&FileMgr);
2222  Clang->setSourceManager(&SourceMgr);
2223
2224  // Remap files.
2225  PreprocessorOpts.clearRemappedFiles();
2226  PreprocessorOpts.RetainRemappedFileBuffers = true;
2227  for (unsigned I = 0; I != NumRemappedFiles; ++I) {
2228    FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second;
2229    if (const llvm::MemoryBuffer *
2230            memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) {
2231      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf);
2232      OwnedBuffers.push_back(memBuf);
2233    } else {
2234      const char *fname = fileOrBuf.get<const char *>();
2235      PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname);
2236    }
2237  }
2238
2239  // Use the code completion consumer we were given, but adding any cached
2240  // code-completion results.
2241  AugmentedCodeCompleteConsumer *AugmentedConsumer
2242    = new AugmentedCodeCompleteConsumer(*this, Consumer,
2243                                        FrontendOpts.ShowMacrosInCodeCompletion,
2244                                FrontendOpts.ShowCodePatternsInCodeCompletion,
2245                                FrontendOpts.ShowGlobalSymbolsInCodeCompletion);
2246  Clang->setCodeCompletionConsumer(AugmentedConsumer);
2247
2248  // If we have a precompiled preamble, try to use it. We only allow
2249  // the use of the precompiled preamble if we're if the completion
2250  // point is within the main file, after the end of the precompiled
2251  // preamble.
2252  llvm::MemoryBuffer *OverrideMainBuffer = 0;
2253  if (!PreambleFile.empty()) {
2254    using llvm::sys::FileStatus;
2255    llvm::sys::PathWithStatus CompleteFilePath(File);
2256    llvm::sys::PathWithStatus MainPath(OriginalSourceFile);
2257    if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus())
2258      if (const FileStatus *MainStatus = MainPath.getFileStatus())
2259        if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID())
2260          OverrideMainBuffer
2261            = getMainBufferWithPrecompiledPreamble(*CCInvocation, false,
2262                                                   Line - 1);
2263  }
2264
2265  // If the main file has been overridden due to the use of a preamble,
2266  // make that override happen and introduce the preamble.
2267  PreprocessorOpts.DisableStatCache = true;
2268  StoredDiagnostics.insert(StoredDiagnostics.end(),
2269                           this->StoredDiagnostics.begin(),
2270             this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver);
2271  if (OverrideMainBuffer) {
2272    PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer);
2273    PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size();
2274    PreprocessorOpts.PrecompiledPreambleBytes.second
2275                                                    = PreambleEndsAtStartOfLine;
2276    PreprocessorOpts.ImplicitPCHInclude = PreambleFile;
2277    PreprocessorOpts.DisablePCHValidation = true;
2278
2279    OwnedBuffers.push_back(OverrideMainBuffer);
2280  } else {
2281    PreprocessorOpts.PrecompiledPreambleBytes.first = 0;
2282    PreprocessorOpts.PrecompiledPreambleBytes.second = false;
2283  }
2284
2285  // Disable the preprocessing record
2286  PreprocessorOpts.DetailedRecord = false;
2287
2288  llvm::OwningPtr<SyntaxOnlyAction> Act;
2289  Act.reset(new SyntaxOnlyAction);
2290  if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second,
2291                           Clang->getFrontendOpts().Inputs[0].first)) {
2292    if (OverrideMainBuffer) {
2293      std::string ModName = PreambleFile;
2294      TranslateStoredDiagnostics(Clang->getModuleManager(), ModName,
2295                                 getSourceManager(), PreambleDiagnostics,
2296                                 StoredDiagnostics);
2297    }
2298    Act->Execute();
2299    Act->EndSourceFile();
2300  }
2301}
2302
2303CXSaveError ASTUnit::Save(StringRef File) {
2304  if (getDiagnostics().hasUnrecoverableErrorOccurred())
2305    return CXSaveError_TranslationErrors;
2306
2307  // Write to a temporary file and later rename it to the actual file, to avoid
2308  // possible race conditions.
2309  llvm::SmallString<128> TempPath;
2310  TempPath = File;
2311  TempPath += "-%%%%%%%%";
2312  int fd;
2313  if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
2314                                 /*makeAbsolute=*/false))
2315    return CXSaveError_Unknown;
2316
2317  // FIXME: Can we somehow regenerate the stat cache here, or do we need to
2318  // unconditionally create a stat cache when we parse the file?
2319  llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true);
2320
2321  serialize(Out);
2322  Out.close();
2323  if (Out.has_error())
2324    return CXSaveError_Unknown;
2325
2326  if (llvm::error_code ec = llvm::sys::fs::rename(TempPath.str(), File)) {
2327    bool exists;
2328    llvm::sys::fs::remove(TempPath.str(), exists);
2329    return CXSaveError_Unknown;
2330  }
2331
2332  return CXSaveError_None;
2333}
2334
2335bool ASTUnit::serialize(raw_ostream &OS) {
2336  if (getDiagnostics().hasErrorOccurred())
2337    return true;
2338
2339  std::vector<unsigned char> Buffer;
2340  llvm::BitstreamWriter Stream(Buffer);
2341  ASTWriter Writer(Stream);
2342  Writer.WriteAST(getSema(), 0, std::string(), "");
2343
2344  // Write the generated bitstream to "Out".
2345  if (!Buffer.empty())
2346    OS.write((char *)&Buffer.front(), Buffer.size());
2347
2348  return false;
2349}
2350
2351typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap;
2352
2353static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) {
2354  unsigned Raw = L.getRawEncoding();
2355  const unsigned MacroBit = 1U << 31;
2356  L = SourceLocation::getFromRawEncoding((Raw & MacroBit) |
2357      ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second));
2358}
2359
2360void ASTUnit::TranslateStoredDiagnostics(
2361                          ASTReader *MMan,
2362                          StringRef ModName,
2363                          SourceManager &SrcMgr,
2364                          const SmallVectorImpl<StoredDiagnostic> &Diags,
2365                          SmallVectorImpl<StoredDiagnostic> &Out) {
2366  // The stored diagnostic has the old source manager in it; update
2367  // the locations to refer into the new source manager. We also need to remap
2368  // all the locations to the new view. This includes the diag location, any
2369  // associated source ranges, and the source ranges of associated fix-its.
2370  // FIXME: There should be a cleaner way to do this.
2371
2372  SmallVector<StoredDiagnostic, 4> Result;
2373  Result.reserve(Diags.size());
2374  assert(MMan && "Don't have a module manager");
2375  serialization::Module *Mod = MMan->ModuleMgr.lookup(ModName);
2376  assert(Mod && "Don't have preamble module");
2377  SLocRemap &Remap = Mod->SLocRemap;
2378  for (unsigned I = 0, N = Diags.size(); I != N; ++I) {
2379    // Rebuild the StoredDiagnostic.
2380    const StoredDiagnostic &SD = Diags[I];
2381    SourceLocation L = SD.getLocation();
2382    TranslateSLoc(L, Remap);
2383    FullSourceLoc Loc(L, SrcMgr);
2384
2385    SmallVector<CharSourceRange, 4> Ranges;
2386    Ranges.reserve(SD.range_size());
2387    for (StoredDiagnostic::range_iterator I = SD.range_begin(),
2388                                          E = SD.range_end();
2389         I != E; ++I) {
2390      SourceLocation BL = I->getBegin();
2391      TranslateSLoc(BL, Remap);
2392      SourceLocation EL = I->getEnd();
2393      TranslateSLoc(EL, Remap);
2394      Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange()));
2395    }
2396
2397    SmallVector<FixItHint, 2> FixIts;
2398    FixIts.reserve(SD.fixit_size());
2399    for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(),
2400                                          E = SD.fixit_end();
2401         I != E; ++I) {
2402      FixIts.push_back(FixItHint());
2403      FixItHint &FH = FixIts.back();
2404      FH.CodeToInsert = I->CodeToInsert;
2405      SourceLocation BL = I->RemoveRange.getBegin();
2406      TranslateSLoc(BL, Remap);
2407      SourceLocation EL = I->RemoveRange.getEnd();
2408      TranslateSLoc(EL, Remap);
2409      FH.RemoveRange = CharSourceRange(SourceRange(BL, EL),
2410                                       I->RemoveRange.isTokenRange());
2411    }
2412
2413    Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(),
2414                                      SD.getMessage(), Loc, Ranges, FixIts));
2415  }
2416  Result.swap(Out);
2417}
2418