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