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