• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
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 utility class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
15 #define LLVM_CLANG_FRONTEND_ASTUNIT_H
16 
17 #include "clang/Serialization/ASTBitCodes.h"
18 #include "clang/Sema/Sema.h"
19 #include "clang/Sema/CodeCompleteConsumer.h"
20 #include "clang/Lex/ModuleLoader.h"
21 #include "clang/Lex/PreprocessingRecord.h"
22 #include "clang/Basic/LangOptions.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/Basic/FileManager.h"
25 #include "clang/Basic/FileSystemOptions.h"
26 #include "clang-c/Index.h"
27 #include "llvm/ADT/IntrusiveRefCntPtr.h"
28 #include "llvm/ADT/OwningPtr.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/Support/Path.h"
32 #include <map>
33 #include <string>
34 #include <vector>
35 #include <cassert>
36 #include <utility>
37 #include <sys/types.h>
38 
39 namespace llvm {
40   class MemoryBuffer;
41 }
42 
43 namespace clang {
44 class ASTContext;
45 class ASTReader;
46 class CodeCompleteConsumer;
47 class CompilerInvocation;
48 class CompilerInstance;
49 class Decl;
50 class DiagnosticsEngine;
51 class FileEntry;
52 class FileManager;
53 class HeaderSearch;
54 class Preprocessor;
55 class SourceManager;
56 class TargetInfo;
57 class ASTFrontendAction;
58 
59 /// \brief Utility class for loading a ASTContext from an AST file.
60 ///
61 class ASTUnit : public ModuleLoader {
62 private:
63   IntrusiveRefCntPtr<LangOptions>       LangOpts;
64   IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
65   IntrusiveRefCntPtr<FileManager>       FileMgr;
66   IntrusiveRefCntPtr<SourceManager>     SourceMgr;
67   OwningPtr<HeaderSearch>               HeaderInfo;
68   IntrusiveRefCntPtr<TargetInfo>        Target;
69   IntrusiveRefCntPtr<Preprocessor>      PP;
70   IntrusiveRefCntPtr<ASTContext>        Ctx;
71   ASTReader *Reader;
72 
73   FileSystemOptions FileSystemOpts;
74 
75   /// \brief The AST consumer that received information about the translation
76   /// unit as it was parsed or loaded.
77   OwningPtr<ASTConsumer> Consumer;
78 
79   /// \brief The semantic analysis object used to type-check the translation
80   /// unit.
81   OwningPtr<Sema> TheSema;
82 
83   /// Optional owned invocation, just used to make the invocation used in
84   /// LoadFromCommandLine available.
85   IntrusiveRefCntPtr<CompilerInvocation> Invocation;
86 
87   /// \brief The set of target features.
88   ///
89   /// FIXME: each time we reparse, we need to restore the set of target
90   /// features from this vector, because TargetInfo::CreateTargetInfo()
91   /// mangles the target options in place. Yuck!
92   std::vector<std::string> TargetFeatures;
93 
94   // OnlyLocalDecls - when true, walking this AST should only visit declarations
95   // that come from the AST itself, not from included precompiled headers.
96   // FIXME: This is temporary; eventually, CIndex will always do this.
97   bool                              OnlyLocalDecls;
98 
99   /// \brief Whether to capture any diagnostics produced.
100   bool CaptureDiagnostics;
101 
102   /// \brief Track whether the main file was loaded from an AST or not.
103   bool MainFileIsAST;
104 
105   /// \brief What kind of translation unit this AST represents.
106   TranslationUnitKind TUKind;
107 
108   /// \brief Whether we should time each operation.
109   bool WantTiming;
110 
111   /// \brief Whether the ASTUnit should delete the remapped buffers.
112   bool OwnsRemappedFileBuffers;
113 
114   /// Track the top-level decls which appeared in an ASTUnit which was loaded
115   /// from a source file.
116   //
117   // FIXME: This is just an optimization hack to avoid deserializing large parts
118   // of a PCH file when using the Index library on an ASTUnit loaded from
119   // source. In the long term we should make the Index library use efficient and
120   // more scalable search mechanisms.
121   std::vector<Decl*> TopLevelDecls;
122 
123   /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
124   typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
125   typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
126 
127   /// \brief Map from FileID to the file-level declarations that it contains.
128   /// The files and decls are only local (and non-preamble) ones.
129   FileDeclsTy FileDecls;
130 
131   /// The name of the original source file used to generate this ASTUnit.
132   std::string OriginalSourceFile;
133 
134   /// \brief The set of diagnostics produced when creating the preamble.
135   SmallVector<StoredDiagnostic, 4> PreambleDiagnostics;
136 
137   /// \brief The set of diagnostics produced when creating this
138   /// translation unit.
139   SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
140 
141   /// \brief The set of diagnostics produced when failing to parse, e.g. due
142   /// to failure to load the PCH.
143   SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
144 
145   /// \brief The number of stored diagnostics that come from the driver
146   /// itself.
147   ///
148   /// Diagnostics that come from the driver are retained from one parse to
149   /// the next.
150   unsigned NumStoredDiagnosticsFromDriver;
151 
152   /// \brief Counter that determines when we want to try building a
153   /// precompiled preamble.
154   ///
155   /// If zero, we will never build a precompiled preamble. Otherwise,
156   /// it's treated as a counter that decrements each time we reparse
157   /// without the benefit of a precompiled preamble. When it hits 1,
158   /// we'll attempt to rebuild the precompiled header. This way, if
159   /// building the precompiled preamble fails, we won't try again for
160   /// some number of calls.
161   unsigned PreambleRebuildCounter;
162 
163 public:
164   class PreambleData {
165     const FileEntry *File;
166     std::vector<char> Buffer;
167     mutable unsigned NumLines;
168 
169   public:
PreambleData()170     PreambleData() : File(0), NumLines(0) { }
171 
assign(const FileEntry * F,const char * begin,const char * end)172     void assign(const FileEntry *F, const char *begin, const char *end) {
173       File = F;
174       Buffer.assign(begin, end);
175       NumLines = 0;
176     }
177 
clear()178     void clear() { Buffer.clear(); File = 0; NumLines = 0; }
179 
size()180     size_t size() const { return Buffer.size(); }
empty()181     bool empty() const { return Buffer.empty(); }
182 
getBufferStart()183     const char *getBufferStart() const { return &Buffer[0]; }
184 
getNumLines()185     unsigned getNumLines() const {
186       if (NumLines)
187         return NumLines;
188       countLines();
189       return NumLines;
190     }
191 
getSourceRange(const SourceManager & SM)192     SourceRange getSourceRange(const SourceManager &SM) const {
193       SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
194       return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
195     }
196 
197   private:
198     void countLines() const;
199   };
200 
getPreambleData()201   const PreambleData &getPreambleData() const {
202     return Preamble;
203   }
204 
205 private:
206 
207   /// \brief The contents of the preamble that has been precompiled to
208   /// \c PreambleFile.
209   PreambleData Preamble;
210 
211   /// \brief Whether the preamble ends at the start of a new line.
212   ///
213   /// Used to inform the lexer as to whether it's starting at the beginning of
214   /// a line after skipping the preamble.
215   bool PreambleEndsAtStartOfLine;
216 
217   /// \brief The size of the source buffer that we've reserved for the main
218   /// file within the precompiled preamble.
219   unsigned PreambleReservedSize;
220 
221   /// \brief Keeps track of the files that were used when computing the
222   /// preamble, with both their buffer size and their modification time.
223   ///
224   /// If any of the files have changed from one compile to the next,
225   /// the preamble must be thrown away.
226   llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
227 
228   /// \brief When non-NULL, this is the buffer used to store the contents of
229   /// the main file when it has been padded for use with the precompiled
230   /// preamble.
231   llvm::MemoryBuffer *SavedMainFileBuffer;
232 
233   /// \brief When non-NULL, this is the buffer used to store the
234   /// contents of the preamble when it has been padded to build the
235   /// precompiled preamble.
236   llvm::MemoryBuffer *PreambleBuffer;
237 
238   /// \brief The number of warnings that occurred while parsing the preamble.
239   ///
240   /// This value will be used to restore the state of the \c DiagnosticsEngine
241   /// object when re-using the precompiled preamble. Note that only the
242   /// number of warnings matters, since we will not save the preamble
243   /// when any errors are present.
244   unsigned NumWarningsInPreamble;
245 
246   /// \brief A list of the serialization ID numbers for each of the top-level
247   /// declarations parsed within the precompiled preamble.
248   std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
249 
250   /// \brief Whether we should be caching code-completion results.
251   bool ShouldCacheCodeCompletionResults;
252 
253   /// \brief The language options used when we load an AST file.
254   LangOptions ASTFileLangOpts;
255 
256   static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags,
257                              const char **ArgBegin, const char **ArgEnd,
258                              ASTUnit &AST, bool CaptureDiagnostics);
259 
260   void TranslateStoredDiagnostics(ASTReader *MMan, StringRef ModName,
261                                   SourceManager &SrcMan,
262                       const SmallVectorImpl<StoredDiagnostic> &Diags,
263                             SmallVectorImpl<StoredDiagnostic> &Out);
264 
265   void clearFileLevelDecls();
266 
267 public:
268   /// \brief A cached code-completion result, which may be introduced in one of
269   /// many different contexts.
270   struct CachedCodeCompletionResult {
271     /// \brief The code-completion string corresponding to this completion
272     /// result.
273     CodeCompletionString *Completion;
274 
275     /// \brief A bitmask that indicates which code-completion contexts should
276     /// contain this completion result.
277     ///
278     /// The bits in the bitmask correspond to the values of
279     /// CodeCompleteContext::Kind. To map from a completion context kind to a
280     /// bit, subtract one from the completion context kind and shift 1 by that
281     /// number of bits. Many completions can occur in several different
282     /// contexts.
283     unsigned ShowInContexts;
284 
285     /// \brief The priority given to this code-completion result.
286     unsigned Priority;
287 
288     /// \brief The libclang cursor kind corresponding to this code-completion
289     /// result.
290     CXCursorKind Kind;
291 
292     /// \brief The availability of this code-completion result.
293     CXAvailabilityKind Availability;
294 
295     /// \brief The simplified type class for a non-macro completion result.
296     SimplifiedTypeClass TypeClass;
297 
298     /// \brief The type of a non-macro completion result, stored as a unique
299     /// integer used by the string map of cached completion types.
300     ///
301     /// This value will be zero if the type is not known, or a unique value
302     /// determined by the formatted type string. Se \c CachedCompletionTypes
303     /// for more information.
304     unsigned Type;
305   };
306 
307   /// \brief Retrieve the mapping from formatted type names to unique type
308   /// identifiers.
getCachedCompletionTypes()309   llvm::StringMap<unsigned> &getCachedCompletionTypes() {
310     return CachedCompletionTypes;
311   }
312 
313   /// \brief Retrieve the allocator used to cache global code completions.
314   IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
getCachedCompletionAllocator()315   getCachedCompletionAllocator() {
316     return CachedCompletionAllocator;
317   }
318 
getCodeCompletionTUInfo()319   CodeCompletionTUInfo &getCodeCompletionTUInfo() {
320     if (!CCTUInfo)
321       CCTUInfo.reset(new CodeCompletionTUInfo(
322                                             new GlobalCodeCompletionAllocator));
323     return *CCTUInfo;
324   }
325 
326 private:
327   /// \brief Allocator used to store cached code completions.
328   IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
329     CachedCompletionAllocator;
330 
331   OwningPtr<CodeCompletionTUInfo> CCTUInfo;
332 
333   /// \brief The set of cached code-completion results.
334   std::vector<CachedCodeCompletionResult> CachedCompletionResults;
335 
336   /// \brief A mapping from the formatted type name to a unique number for that
337   /// type, which is used for type equality comparisons.
338   llvm::StringMap<unsigned> CachedCompletionTypes;
339 
340   /// \brief A string hash of the top-level declaration and macro definition
341   /// names processed the last time that we reparsed the file.
342   ///
343   /// This hash value is used to determine when we need to refresh the
344   /// global code-completion cache.
345   unsigned CompletionCacheTopLevelHashValue;
346 
347   /// \brief A string hash of the top-level declaration and macro definition
348   /// names processed the last time that we reparsed the precompiled preamble.
349   ///
350   /// This hash value is used to determine when we need to refresh the
351   /// global code-completion cache after a rebuild of the precompiled preamble.
352   unsigned PreambleTopLevelHashValue;
353 
354   /// \brief The current hash value for the top-level declaration and macro
355   /// definition names
356   unsigned CurrentTopLevelHashValue;
357 
358   /// \brief Bit used by CIndex to mark when a translation unit may be in an
359   /// inconsistent state, and is not safe to free.
360   unsigned UnsafeToFree : 1;
361 
362   /// \brief Cache any "global" code-completion results, so that we can avoid
363   /// recomputing them with each completion.
364   void CacheCodeCompletionResults();
365 
366   /// \brief Clear out and deallocate
367   void ClearCachedCompletionResults();
368 
369   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
370   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
371 
372   explicit ASTUnit(bool MainFileIsAST);
373 
374   void CleanTemporaryFiles();
375   bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
376 
377   std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
378   ComputePreamble(CompilerInvocation &Invocation,
379                   unsigned MaxLines, bool &CreatedBuffer);
380 
381   llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
382                                const CompilerInvocation &PreambleInvocationIn,
383                                                      bool AllowRebuild = true,
384                                                         unsigned MaxLines = 0);
385   void RealizeTopLevelDeclsFromPreamble();
386 
387   /// \brief Transfers ownership of the objects (like SourceManager) from
388   /// \param CI to this ASTUnit.
389   void transferASTDataFromCompilerInstance(CompilerInstance &CI);
390 
391   /// \brief Allows us to assert that ASTUnit is not being used concurrently,
392   /// which is not supported.
393   ///
394   /// Clients should create instances of the ConcurrencyCheck class whenever
395   /// using the ASTUnit in a way that isn't intended to be concurrent, which is
396   /// just about any usage.
397   /// Becomes a noop in release mode; only useful for debug mode checking.
398   class ConcurrencyState {
399     void *Mutex; // a llvm::sys::MutexImpl in debug;
400 
401   public:
402     ConcurrencyState();
403     ~ConcurrencyState();
404 
405     void start();
406     void finish();
407   };
408   ConcurrencyState ConcurrencyCheckValue;
409 
410 public:
411   class ConcurrencyCheck {
412     ASTUnit &Self;
413 
414   public:
ConcurrencyCheck(ASTUnit & Self)415     explicit ConcurrencyCheck(ASTUnit &Self)
416       : Self(Self)
417     {
418       Self.ConcurrencyCheckValue.start();
419     }
~ConcurrencyCheck()420     ~ConcurrencyCheck() {
421       Self.ConcurrencyCheckValue.finish();
422     }
423   };
424   friend class ConcurrencyCheck;
425 
426   ~ASTUnit();
427 
isMainFileAST()428   bool isMainFileAST() const { return MainFileIsAST; }
429 
isUnsafeToFree()430   bool isUnsafeToFree() const { return UnsafeToFree; }
setUnsafeToFree(bool Value)431   void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
432 
getDiagnostics()433   const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
getDiagnostics()434   DiagnosticsEngine &getDiagnostics()             { return *Diagnostics; }
435 
getSourceManager()436   const SourceManager &getSourceManager() const { return *SourceMgr; }
getSourceManager()437         SourceManager &getSourceManager()       { return *SourceMgr; }
438 
getPreprocessor()439   const Preprocessor &getPreprocessor() const { return *PP; }
getPreprocessor()440         Preprocessor &getPreprocessor()       { return *PP; }
441 
getASTContext()442   const ASTContext &getASTContext() const { return *Ctx; }
getASTContext()443         ASTContext &getASTContext()       { return *Ctx; }
444 
setASTContext(ASTContext * ctx)445   void setASTContext(ASTContext *ctx) { Ctx = ctx; }
446   void setPreprocessor(Preprocessor *pp);
447 
hasSema()448   bool hasSema() const { return TheSema; }
getSema()449   Sema &getSema() const {
450     assert(TheSema && "ASTUnit does not have a Sema object!");
451     return *TheSema;
452   }
453 
getFileManager()454   const FileManager &getFileManager() const { return *FileMgr; }
getFileManager()455         FileManager &getFileManager()       { return *FileMgr; }
456 
getFileSystemOpts()457   const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
458 
459   const std::string &getOriginalSourceFileName();
460 
461   /// \brief Add a temporary file that the ASTUnit depends on.
462   ///
463   /// This file will be erased when the ASTUnit is destroyed.
464   void addTemporaryFile(const llvm::sys::Path &TempFile);
465 
getOnlyLocalDecls()466   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
467 
getOwnsRemappedFileBuffers()468   bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
setOwnsRemappedFileBuffers(bool val)469   void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
470 
471   StringRef getMainFileName() const;
472 
473   typedef std::vector<Decl *>::iterator top_level_iterator;
474 
top_level_begin()475   top_level_iterator top_level_begin() {
476     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
477     if (!TopLevelDeclsInPreamble.empty())
478       RealizeTopLevelDeclsFromPreamble();
479     return TopLevelDecls.begin();
480   }
481 
top_level_end()482   top_level_iterator top_level_end() {
483     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
484     if (!TopLevelDeclsInPreamble.empty())
485       RealizeTopLevelDeclsFromPreamble();
486     return TopLevelDecls.end();
487   }
488 
top_level_size()489   std::size_t top_level_size() const {
490     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
491     return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
492   }
493 
top_level_empty()494   bool top_level_empty() const {
495     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
496     return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
497   }
498 
499   /// \brief Add a new top-level declaration.
addTopLevelDecl(Decl * D)500   void addTopLevelDecl(Decl *D) {
501     TopLevelDecls.push_back(D);
502   }
503 
504   /// \brief Add a new local file-level declaration.
505   void addFileLevelDecl(Decl *D);
506 
507   /// \brief Get the decls that are contained in a file in the Offset/Length
508   /// range. \arg Length can be 0 to indicate a point at \arg Offset instead of
509   /// a range.
510   void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
511                            SmallVectorImpl<Decl *> &Decls);
512 
513   /// \brief Add a new top-level declaration, identified by its ID in
514   /// the precompiled preamble.
addTopLevelDeclFromPreamble(serialization::DeclID D)515   void addTopLevelDeclFromPreamble(serialization::DeclID D) {
516     TopLevelDeclsInPreamble.push_back(D);
517   }
518 
519   /// \brief Retrieve a reference to the current top-level name hash value.
520   ///
521   /// Note: This is used internally by the top-level tracking action
getCurrentTopLevelHashValue()522   unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
523 
524   /// \brief Get the source location for the given file:line:col triplet.
525   ///
526   /// The difference with SourceManager::getLocation is that this method checks
527   /// whether the requested location points inside the precompiled preamble
528   /// in which case the returned source location will be a "loaded" one.
529   SourceLocation getLocation(const FileEntry *File,
530                              unsigned Line, unsigned Col) const;
531 
532   /// \brief Get the source location for the given file:offset pair.
533   SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
534 
535   /// \brief If \arg Loc is a loaded location from the preamble, returns
536   /// the corresponding local location of the main file, otherwise it returns
537   /// \arg Loc.
538   SourceLocation mapLocationFromPreamble(SourceLocation Loc);
539 
540   /// \brief If \arg Loc is a local location of the main file but inside the
541   /// preamble chunk, returns the corresponding loaded location from the
542   /// preamble, otherwise it returns \arg Loc.
543   SourceLocation mapLocationToPreamble(SourceLocation Loc);
544 
545   bool isInPreambleFileID(SourceLocation Loc);
546   bool isInMainFileID(SourceLocation Loc);
547   SourceLocation getStartOfMainFileID();
548   SourceLocation getEndOfPreambleFileID();
549 
550   /// \brief \see mapLocationFromPreamble.
mapRangeFromPreamble(SourceRange R)551   SourceRange mapRangeFromPreamble(SourceRange R) {
552     return SourceRange(mapLocationFromPreamble(R.getBegin()),
553                        mapLocationFromPreamble(R.getEnd()));
554   }
555 
556   /// \brief \see mapLocationToPreamble.
mapRangeToPreamble(SourceRange R)557   SourceRange mapRangeToPreamble(SourceRange R) {
558     return SourceRange(mapLocationToPreamble(R.getBegin()),
559                        mapLocationToPreamble(R.getEnd()));
560   }
561 
562   // Retrieve the diagnostics associated with this AST
563   typedef StoredDiagnostic *stored_diag_iterator;
564   typedef const StoredDiagnostic *stored_diag_const_iterator;
stored_diag_begin()565   stored_diag_const_iterator stored_diag_begin() const {
566     return StoredDiagnostics.begin();
567   }
stored_diag_begin()568   stored_diag_iterator stored_diag_begin() {
569     return StoredDiagnostics.begin();
570   }
stored_diag_end()571   stored_diag_const_iterator stored_diag_end() const {
572     return StoredDiagnostics.end();
573   }
stored_diag_end()574   stored_diag_iterator stored_diag_end() {
575     return StoredDiagnostics.end();
576   }
stored_diag_size()577   unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
578 
stored_diag_afterDriver_begin()579   stored_diag_iterator stored_diag_afterDriver_begin() {
580     if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
581       NumStoredDiagnosticsFromDriver = 0;
582     return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
583   }
584 
585   typedef std::vector<CachedCodeCompletionResult>::iterator
586     cached_completion_iterator;
587 
cached_completion_begin()588   cached_completion_iterator cached_completion_begin() {
589     return CachedCompletionResults.begin();
590   }
591 
cached_completion_end()592   cached_completion_iterator cached_completion_end() {
593     return CachedCompletionResults.end();
594   }
595 
cached_completion_size()596   unsigned cached_completion_size() const {
597     return CachedCompletionResults.size();
598   }
599 
600   llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
601                                        std::string *ErrorStr = 0);
602 
603   /// \brief Determine what kind of translation unit this AST represents.
getTranslationUnitKind()604   TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
605 
606   typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
607       FilenameOrMemBuf;
608   /// \brief A mapping from a file name to the memory buffer that stores the
609   /// remapped contents of that file.
610   typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
611 
612   /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
613   static ASTUnit *create(CompilerInvocation *CI,
614                          IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
615                          bool CaptureDiagnostics = false);
616 
617   /// \brief Create a ASTUnit from an AST file.
618   ///
619   /// \param Filename - The AST file to load.
620   ///
621   /// \param Diags - The diagnostics engine to use for reporting errors; its
622   /// lifetime is expected to extend past that of the returned ASTUnit.
623   ///
624   /// \returns - The initialized ASTUnit or null if the AST failed to load.
625   static ASTUnit *LoadFromASTFile(const std::string &Filename,
626                               IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
627                                   const FileSystemOptions &FileSystemOpts,
628                                   bool OnlyLocalDecls = false,
629                                   RemappedFile *RemappedFiles = 0,
630                                   unsigned NumRemappedFiles = 0,
631                                   bool CaptureDiagnostics = false,
632                                   bool AllowPCHWithCompilerErrors = false);
633 
634 private:
635   /// \brief Helper function for \c LoadFromCompilerInvocation() and
636   /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
637   ///
638   /// \param PrecompilePreamble Whether to precompile the preamble of this
639   /// translation unit, to improve the performance of reparsing.
640   ///
641   /// \returns \c true if a catastrophic failure occurred (which means that the
642   /// \c ASTUnit itself is invalid), or \c false otherwise.
643   bool LoadFromCompilerInvocation(bool PrecompilePreamble);
644 
645 public:
646 
647   /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
648   /// object, by invoking the optionally provided ASTFrontendAction.
649   ///
650   /// \param CI - The compiler invocation to use; it must have exactly one input
651   /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
652   ///
653   /// \param Diags - The diagnostics engine to use for reporting errors; its
654   /// lifetime is expected to extend past that of the returned ASTUnit.
655   ///
656   /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
657   /// transfered.
658   ///
659   /// \param Unit - optionally an already created ASTUnit. Its ownership is not
660   /// transfered.
661   ///
662   /// \param Persistent - if true the returned ASTUnit will be complete.
663   /// false means the caller is only interested in getting info through the
664   /// provided \see Action.
665   ///
666   /// \param ErrAST - If non-null and parsing failed without any AST to return
667   /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
668   /// mainly to allow the caller to see the diagnostics.
669   /// This will only receive an ASTUnit if a new one was created. If an already
670   /// created ASTUnit was passed in \param Unit then the caller can check that.
671   ///
672   static ASTUnit *LoadFromCompilerInvocationAction(CompilerInvocation *CI,
673                               IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
674                                              ASTFrontendAction *Action = 0,
675                                              ASTUnit *Unit = 0,
676                                              bool Persistent = true,
677                                       StringRef ResourceFilesPath = StringRef(),
678                                              bool OnlyLocalDecls = false,
679                                              bool CaptureDiagnostics = false,
680                                              bool PrecompilePreamble = false,
681                                        bool CacheCodeCompletionResults = false,
682                                        OwningPtr<ASTUnit> *ErrAST = 0);
683 
684   /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
685   /// CompilerInvocation object.
686   ///
687   /// \param CI - The compiler invocation to use; it must have exactly one input
688   /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
689   ///
690   /// \param Diags - The diagnostics engine to use for reporting errors; its
691   /// lifetime is expected to extend past that of the returned ASTUnit.
692   //
693   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
694   // shouldn't need to specify them at construction time.
695   static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
696                               IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
697                                              bool OnlyLocalDecls = false,
698                                              bool CaptureDiagnostics = false,
699                                              bool PrecompilePreamble = false,
700                                       TranslationUnitKind TUKind = TU_Complete,
701                                        bool CacheCodeCompletionResults = false);
702 
703   /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
704   /// arguments, which must specify exactly one source file.
705   ///
706   /// \param ArgBegin - The beginning of the argument vector.
707   ///
708   /// \param ArgEnd - The end of the argument vector.
709   ///
710   /// \param Diags - The diagnostics engine to use for reporting errors; its
711   /// lifetime is expected to extend past that of the returned ASTUnit.
712   ///
713   /// \param ResourceFilesPath - The path to the compiler resource files.
714   ///
715   /// \param ErrAST - If non-null and parsing failed without any AST to return
716   /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
717   /// mainly to allow the caller to see the diagnostics.
718   ///
719   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
720   // shouldn't need to specify them at construction time.
721   static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
722                                       const char **ArgEnd,
723                               IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
724                                       StringRef ResourceFilesPath,
725                                       bool OnlyLocalDecls = false,
726                                       bool CaptureDiagnostics = false,
727                                       RemappedFile *RemappedFiles = 0,
728                                       unsigned NumRemappedFiles = 0,
729                                       bool RemappedFilesKeepOriginalName = true,
730                                       bool PrecompilePreamble = false,
731                                       TranslationUnitKind TUKind = TU_Complete,
732                                       bool CacheCodeCompletionResults = false,
733                                       bool AllowPCHWithCompilerErrors = false,
734                                       bool SkipFunctionBodies = false,
735                                       OwningPtr<ASTUnit> *ErrAST = 0);
736 
737   /// \brief Reparse the source files using the same command-line options that
738   /// were originally used to produce this translation unit.
739   ///
740   /// \returns True if a failure occurred that causes the ASTUnit not to
741   /// contain any translation-unit information, false otherwise.
742   bool Reparse(RemappedFile *RemappedFiles = 0,
743                unsigned NumRemappedFiles = 0);
744 
745   /// \brief Perform code completion at the given file, line, and
746   /// column within this translation unit.
747   ///
748   /// \param File The file in which code completion will occur.
749   ///
750   /// \param Line The line at which code completion will occur.
751   ///
752   /// \param Column The column at which code completion will occur.
753   ///
754   /// \param IncludeMacros Whether to include macros in the code-completion
755   /// results.
756   ///
757   /// \param IncludeCodePatterns Whether to include code patterns (such as a
758   /// for loop) in the code-completion results.
759   ///
760   /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
761   /// OwnedBuffers parameters are all disgusting hacks. They will go away.
762   void CodeComplete(StringRef File, unsigned Line, unsigned Column,
763                     RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
764                     bool IncludeMacros, bool IncludeCodePatterns,
765                     CodeCompleteConsumer &Consumer,
766                     DiagnosticsEngine &Diag, LangOptions &LangOpts,
767                     SourceManager &SourceMgr, FileManager &FileMgr,
768                     SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
769               SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
770 
771   /// \brief Save this translation unit to a file with the given name.
772   ///
773   /// \returns An indication of whether the save was successful or not.
774   CXSaveError Save(StringRef File);
775 
776   /// \brief Serialize this translation unit with the given output stream.
777   ///
778   /// \returns True if an error occurred, false otherwise.
779   bool serialize(raw_ostream &OS);
780 
loadModule(SourceLocation ImportLoc,ModuleIdPath Path,Module::NameVisibilityKind Visibility,bool IsInclusionDirective)781   virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
782                              Module::NameVisibilityKind Visibility,
783                              bool IsInclusionDirective) {
784     // ASTUnit doesn't know how to load modules (not that this matters).
785     return 0;
786   }
787 };
788 
789 } // namespace clang
790 
791 #endif
792