clang  6.0.0svn
ASTUnit.h
Go to the documentation of this file.
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"
24 #include "clang/Lex/ModuleLoader.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 <cassert>
34 #include <memory>
35 #include <string>
36 #include <sys/types.h>
37 #include <utility>
38 #include <vector>
39 
40 namespace llvm {
41  class MemoryBuffer;
42 }
43 
44 namespace clang {
45 class Sema;
46 class ASTContext;
47 class ASTReader;
48 class CompilerInvocation;
49 class CompilerInstance;
50 class Decl;
51 class DiagnosticsEngine;
52 class FileEntry;
53 class FileManager;
54 class HeaderSearch;
55 class InputKind;
56 class MemoryBufferCache;
57 class Preprocessor;
58 class PreprocessorOptions;
59 class PCHContainerOperations;
60 class PCHContainerReader;
61 class TargetInfo;
62 class FrontendAction;
63 class ASTDeserializationListener;
64 
65 namespace vfs {
66 class FileSystem;
67 }
68 
69 /// \brief Utility class for loading a ASTContext from an AST file.
70 ///
71 class ASTUnit {
72 public:
73  struct StandaloneFixIt {
74  std::pair<unsigned, unsigned> RemoveRange;
75  std::pair<unsigned, unsigned> InsertFromRange;
76  std::string CodeToInsert;
78  };
79 
81  unsigned ID;
83  std::string Message;
84  std::string Filename;
85  unsigned LocOffset;
86  std::vector<std::pair<unsigned, unsigned> > Ranges;
87  std::vector<StandaloneFixIt> FixIts;
88  };
89 
90 private:
91  std::shared_ptr<LangOptions> LangOpts;
96  std::unique_ptr<HeaderSearch> HeaderInfo;
98  std::shared_ptr<Preprocessor> PP;
100  std::shared_ptr<TargetOptions> TargetOpts;
101  std::shared_ptr<HeaderSearchOptions> HSOpts;
102  std::shared_ptr<PreprocessorOptions> PPOpts;
104  bool HadModuleLoaderFatalFailure;
105 
106  struct ASTWriterData;
107  std::unique_ptr<ASTWriterData> WriterData;
108 
109  FileSystemOptions FileSystemOpts;
110 
111  /// \brief The AST consumer that received information about the translation
112  /// unit as it was parsed or loaded.
113  std::unique_ptr<ASTConsumer> Consumer;
114 
115  /// \brief The semantic analysis object used to type-check the translation
116  /// unit.
117  std::unique_ptr<Sema> TheSema;
118 
119  /// Optional owned invocation, just used to make the invocation used in
120  /// LoadFromCommandLine available.
121  std::shared_ptr<CompilerInvocation> Invocation;
122 
123  /// Fake module loader: the AST unit doesn't need to load any modules.
125 
126  // OnlyLocalDecls - when true, walking this AST should only visit declarations
127  // that come from the AST itself, not from included precompiled headers.
128  // FIXME: This is temporary; eventually, CIndex will always do this.
129  bool OnlyLocalDecls;
130 
131  /// \brief Whether to capture any diagnostics produced.
132  bool CaptureDiagnostics;
133 
134  /// \brief Track whether the main file was loaded from an AST or not.
135  bool MainFileIsAST;
136 
137  /// \brief What kind of translation unit this AST represents.
138  TranslationUnitKind TUKind;
139 
140  /// \brief Whether we should time each operation.
141  bool WantTiming;
142 
143  /// \brief Whether the ASTUnit should delete the remapped buffers.
144  bool OwnsRemappedFileBuffers;
145 
146  /// Track the top-level decls which appeared in an ASTUnit which was loaded
147  /// from a source file.
148  //
149  // FIXME: This is just an optimization hack to avoid deserializing large parts
150  // of a PCH file when using the Index library on an ASTUnit loaded from
151  // source. In the long term we should make the Index library use efficient and
152  // more scalable search mechanisms.
153  std::vector<Decl*> TopLevelDecls;
154 
155  /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
157  typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
158 
159  /// \brief Map from FileID to the file-level declarations that it contains.
160  /// The files and decls are only local (and non-preamble) ones.
161  FileDeclsTy FileDecls;
162 
163  /// The name of the original source file used to generate this ASTUnit.
164  std::string OriginalSourceFile;
165 
166  /// \brief The set of diagnostics produced when creating the preamble.
167  SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
168 
169  /// \brief The set of diagnostics produced when creating this
170  /// translation unit.
171  SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
172 
173  /// \brief The set of diagnostics produced when failing to parse, e.g. due
174  /// to failure to load the PCH.
175  SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
176 
177  /// \brief The number of stored diagnostics that come from the driver
178  /// itself.
179  ///
180  /// Diagnostics that come from the driver are retained from one parse to
181  /// the next.
182  unsigned NumStoredDiagnosticsFromDriver;
183 
184  /// \brief Counter that determines when we want to try building a
185  /// precompiled preamble.
186  ///
187  /// If zero, we will never build a precompiled preamble. Otherwise,
188  /// it's treated as a counter that decrements each time we reparse
189  /// without the benefit of a precompiled preamble. When it hits 1,
190  /// we'll attempt to rebuild the precompiled header. This way, if
191  /// building the precompiled preamble fails, we won't try again for
192  /// some number of calls.
193  unsigned PreambleRebuildCounter;
194 
195  /// \brief Cache pairs "filename - source location"
196  ///
197  /// Cache contains only source locations from preamble so it is
198  /// guaranteed that they stay valid when the SourceManager is recreated.
199  /// This cache is used when loading preambule to increase performance
200  /// of that loading. It must be cleared when preamble is recreated.
201  llvm::StringMap<SourceLocation> PreambleSrcLocCache;
202 
203 private:
204  /// The contents of the preamble.
206 
207  /// \brief When non-NULL, this is the buffer used to store the contents of
208  /// the main file when it has been padded for use with the precompiled
209  /// preamble.
210  std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
211 
212  /// \brief The number of warnings that occurred while parsing the preamble.
213  ///
214  /// This value will be used to restore the state of the \c DiagnosticsEngine
215  /// object when re-using the precompiled preamble. Note that only the
216  /// number of warnings matters, since we will not save the preamble
217  /// when any errors are present.
218  unsigned NumWarningsInPreamble;
219 
220  /// \brief A list of the serialization ID numbers for each of the top-level
221  /// declarations parsed within the precompiled preamble.
222  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
223 
224  /// \brief Whether we should be caching code-completion results.
225  bool ShouldCacheCodeCompletionResults : 1;
226 
227  /// \brief Whether to include brief documentation within the set of code
228  /// completions cached.
229  bool IncludeBriefCommentsInCodeCompletion : 1;
230 
231  /// \brief True if non-system source files should be treated as volatile
232  /// (likely to change while trying to use them).
233  bool UserFilesAreVolatile : 1;
234 
235  static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
236  ASTUnit &AST, bool CaptureDiagnostics);
237 
238  void TranslateStoredDiagnostics(FileManager &FileMgr,
239  SourceManager &SrcMan,
242 
243  void clearFileLevelDecls();
244 
245 public:
246  /// \brief A cached code-completion result, which may be introduced in one of
247  /// many different contexts.
249  /// \brief The code-completion string corresponding to this completion
250  /// result.
252 
253  /// \brief A bitmask that indicates which code-completion contexts should
254  /// contain this completion result.
255  ///
256  /// The bits in the bitmask correspond to the values of
257  /// CodeCompleteContext::Kind. To map from a completion context kind to a
258  /// bit, shift 1 by that number of bits. Many completions can occur in
259  /// several different contexts.
260  uint64_t ShowInContexts;
261 
262  /// \brief The priority given to this code-completion result.
263  unsigned Priority;
264 
265  /// \brief The libclang cursor kind corresponding to this code-completion
266  /// result.
268 
269  /// \brief The availability of this code-completion result.
271 
272  /// \brief The simplified type class for a non-macro completion result.
274 
275  /// \brief The type of a non-macro completion result, stored as a unique
276  /// integer used by the string map of cached completion types.
277  ///
278  /// This value will be zero if the type is not known, or a unique value
279  /// determined by the formatted type string. Se \c CachedCompletionTypes
280  /// for more information.
281  unsigned Type;
282  };
283 
284  /// \brief Retrieve the mapping from formatted type names to unique type
285  /// identifiers.
286  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
287  return CachedCompletionTypes;
288  }
289 
290  /// \brief Retrieve the allocator used to cache global code completions.
291  std::shared_ptr<GlobalCodeCompletionAllocator>
293  return CachedCompletionAllocator;
294  }
295 
297  if (!CCTUInfo)
298  CCTUInfo = llvm::make_unique<CodeCompletionTUInfo>(
299  std::make_shared<GlobalCodeCompletionAllocator>());
300  return *CCTUInfo;
301  }
302 
303 private:
304  /// \brief Allocator used to store cached code completions.
305  std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
306 
307  std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
308 
309  /// \brief The set of cached code-completion results.
310  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
311 
312  /// \brief A mapping from the formatted type name to a unique number for that
313  /// type, which is used for type equality comparisons.
314  llvm::StringMap<unsigned> CachedCompletionTypes;
315 
316  /// \brief A string hash of the top-level declaration and macro definition
317  /// names processed the last time that we reparsed the file.
318  ///
319  /// This hash value is used to determine when we need to refresh the
320  /// global code-completion cache.
321  unsigned CompletionCacheTopLevelHashValue;
322 
323  /// \brief A string hash of the top-level declaration and macro definition
324  /// names processed the last time that we reparsed the precompiled preamble.
325  ///
326  /// This hash value is used to determine when we need to refresh the
327  /// global code-completion cache after a rebuild of the precompiled preamble.
328  unsigned PreambleTopLevelHashValue;
329 
330  /// \brief The current hash value for the top-level declaration and macro
331  /// definition names
332  unsigned CurrentTopLevelHashValue;
333 
334  /// \brief Bit used by CIndex to mark when a translation unit may be in an
335  /// inconsistent state, and is not safe to free.
336  unsigned UnsafeToFree : 1;
337 
338  /// \brief Cache any "global" code-completion results, so that we can avoid
339  /// recomputing them with each completion.
340  void CacheCodeCompletionResults();
341 
342  /// \brief Clear out and deallocate
343  void ClearCachedCompletionResults();
344 
345  ASTUnit(const ASTUnit &) = delete;
346  void operator=(const ASTUnit &) = delete;
347 
348  explicit ASTUnit(bool MainFileIsAST);
349 
350  bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
351  std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
353 
354  std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
355  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
356  const CompilerInvocation &PreambleInvocationIn,
357  IntrusiveRefCntPtr<vfs::FileSystem> VFS, bool AllowRebuild = true,
358  unsigned MaxLines = 0);
359  void RealizeTopLevelDeclsFromPreamble();
360 
361  /// \brief Transfers ownership of the objects (like SourceManager) from
362  /// \param CI to this ASTUnit.
363  void transferASTDataFromCompilerInstance(CompilerInstance &CI);
364 
365  /// \brief Allows us to assert that ASTUnit is not being used concurrently,
366  /// which is not supported.
367  ///
368  /// Clients should create instances of the ConcurrencyCheck class whenever
369  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
370  /// just about any usage.
371  /// Becomes a noop in release mode; only useful for debug mode checking.
372  class ConcurrencyState {
373  void *Mutex; // a llvm::sys::MutexImpl in debug;
374 
375  public:
376  ConcurrencyState();
377  ~ConcurrencyState();
378 
379  void start();
380  void finish();
381  };
382  ConcurrencyState ConcurrencyCheckValue;
383 
384 public:
386  ASTUnit &Self;
387 
388  public:
389  explicit ConcurrencyCheck(ASTUnit &Self)
390  : Self(Self)
391  {
392  Self.ConcurrencyCheckValue.start();
393  }
395  Self.ConcurrencyCheckValue.finish();
396  }
397  };
398  friend class ConcurrencyCheck;
399 
400  ~ASTUnit();
401 
402  bool isMainFileAST() const { return MainFileIsAST; }
403 
404  bool isUnsafeToFree() const { return UnsafeToFree; }
405  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
406 
407  const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
408  DiagnosticsEngine &getDiagnostics() { return *Diagnostics; }
409 
410  const SourceManager &getSourceManager() const { return *SourceMgr; }
411  SourceManager &getSourceManager() { return *SourceMgr; }
412 
413  const Preprocessor &getPreprocessor() const { return *PP; }
414  Preprocessor &getPreprocessor() { return *PP; }
415  std::shared_ptr<Preprocessor> getPreprocessorPtr() const { return PP; }
416 
417  const ASTContext &getASTContext() const { return *Ctx; }
418  ASTContext &getASTContext() { return *Ctx; }
419 
420  void setASTContext(ASTContext *ctx) { Ctx = ctx; }
421  void setPreprocessor(std::shared_ptr<Preprocessor> pp);
422 
423  bool hasSema() const { return (bool)TheSema; }
424  Sema &getSema() const {
425  assert(TheSema && "ASTUnit does not have a Sema object!");
426  return *TheSema;
427  }
428 
429  const LangOptions &getLangOpts() const {
430  assert(LangOpts && "ASTUnit does not have language options");
431  return *LangOpts;
432  }
433 
435  assert(HSOpts && "ASTUnit does not have header search options");
436  return *HSOpts;
437  }
438 
440  assert(PPOpts && "ASTUnit does not have preprocessor options");
441  return *PPOpts;
442  }
443 
444  const FileManager &getFileManager() const { return *FileMgr; }
445  FileManager &getFileManager() { return *FileMgr; }
446 
447  const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
448 
449  IntrusiveRefCntPtr<ASTReader> getASTReader() const;
450 
451  StringRef getOriginalSourceFileName() const {
452  return OriginalSourceFile;
453  }
454 
455  ASTMutationListener *getASTMutationListener();
456  ASTDeserializationListener *getDeserializationListener();
457 
458  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
459 
460  bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
461  void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
462 
463  StringRef getMainFileName() const;
464 
465  /// \brief If this ASTUnit came from an AST file, returns the filename for it.
466  StringRef getASTFileName() const;
467 
468  typedef std::vector<Decl *>::iterator top_level_iterator;
469 
470  top_level_iterator top_level_begin() {
471  assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
472  if (!TopLevelDeclsInPreamble.empty())
473  RealizeTopLevelDeclsFromPreamble();
474  return TopLevelDecls.begin();
475  }
476 
477  top_level_iterator top_level_end() {
478  assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
479  if (!TopLevelDeclsInPreamble.empty())
480  RealizeTopLevelDeclsFromPreamble();
481  return TopLevelDecls.end();
482  }
483 
485  assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
486  return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
487  }
488 
489  bool top_level_empty() const {
490  assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
491  return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
492  }
493 
494  /// \brief Add a new top-level declaration.
496  TopLevelDecls.push_back(D);
497  }
498 
499  /// \brief Add a new local file-level declaration.
500  void addFileLevelDecl(Decl *D);
501 
502  /// \brief Get the decls that are contained in a file in the Offset/Length
503  /// range. \p Length can be 0 to indicate a point at \p Offset instead of
504  /// a range.
505  void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
506  SmallVectorImpl<Decl *> &Decls);
507 
508  /// \brief Retrieve a reference to the current top-level name hash value.
509  ///
510  /// Note: This is used internally by the top-level tracking action
511  unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
512 
513  /// \brief Get the source location for the given file:line:col triplet.
514  ///
515  /// The difference with SourceManager::getLocation is that this method checks
516  /// whether the requested location points inside the precompiled preamble
517  /// in which case the returned source location will be a "loaded" one.
518  SourceLocation getLocation(const FileEntry *File,
519  unsigned Line, unsigned Col) const;
520 
521  /// \brief Get the source location for the given file:offset pair.
522  SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
523 
524  /// \brief If \p Loc is a loaded location from the preamble, returns
525  /// the corresponding local location of the main file, otherwise it returns
526  /// \p Loc.
527  SourceLocation mapLocationFromPreamble(SourceLocation Loc) const;
528 
529  /// \brief If \p Loc is a local location of the main file but inside the
530  /// preamble chunk, returns the corresponding loaded location from the
531  /// preamble, otherwise it returns \p Loc.
532  SourceLocation mapLocationToPreamble(SourceLocation Loc) const;
533 
534  bool isInPreambleFileID(SourceLocation Loc) const;
535  bool isInMainFileID(SourceLocation Loc) const;
536  SourceLocation getStartOfMainFileID() const;
537  SourceLocation getEndOfPreambleFileID() const;
538 
539  /// \see mapLocationFromPreamble.
541  return SourceRange(mapLocationFromPreamble(R.getBegin()),
542  mapLocationFromPreamble(R.getEnd()));
543  }
544 
545  /// \see mapLocationToPreamble.
547  return SourceRange(mapLocationToPreamble(R.getBegin()),
548  mapLocationToPreamble(R.getEnd()));
549  }
550 
551  // Retrieve the diagnostics associated with this AST
554  stored_diag_const_iterator stored_diag_begin() const {
555  return StoredDiagnostics.begin();
556  }
557  stored_diag_iterator stored_diag_begin() {
558  return StoredDiagnostics.begin();
559  }
560  stored_diag_const_iterator stored_diag_end() const {
561  return StoredDiagnostics.end();
562  }
563  stored_diag_iterator stored_diag_end() {
564  return StoredDiagnostics.end();
565  }
566  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
567 
568  stored_diag_iterator stored_diag_afterDriver_begin() {
569  if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
570  NumStoredDiagnosticsFromDriver = 0;
571  return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
572  }
573 
574  typedef std::vector<CachedCodeCompletionResult>::iterator
576 
578  return CachedCompletionResults.begin();
579  }
580 
582  return CachedCompletionResults.end();
583  }
584 
585  unsigned cached_completion_size() const {
586  return CachedCompletionResults.size();
587  }
588 
589  /// \brief Returns an iterator range for the local preprocessing entities
590  /// of the local Preprocessor, if this is a parsed source file, or the loaded
591  /// preprocessing entities of the primary module if this is an AST file.
592  llvm::iterator_range<PreprocessingRecord::iterator>
593  getLocalPreprocessingEntities() const;
594 
595  /// \brief Type for a function iterating over a number of declarations.
596  /// \returns true to continue iteration and false to abort.
597  typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
598 
599  /// \brief Iterate over local declarations (locally parsed if this is a parsed
600  /// source file or the loaded declarations of the primary module if this is an
601  /// AST file).
602  /// \returns true if the iteration was complete or false if it was aborted.
603  bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
604 
605  /// \brief Get the PCH file if one was included.
606  const FileEntry *getPCHFile();
607 
608  /// \brief Returns true if the ASTUnit was constructed from a serialized
609  /// module file.
610  bool isModuleFile() const;
611 
612  std::unique_ptr<llvm::MemoryBuffer>
613  getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
614 
615  /// \brief Determine what kind of translation unit this AST represents.
616  TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
617 
618  /// \brief Determine the input kind this AST unit represents.
619  InputKind getInputKind() const;
620 
621  /// \brief A mapping from a file name to the memory buffer that stores the
622  /// remapped contents of that file.
623  typedef std::pair<std::string, llvm::MemoryBuffer *> RemappedFile;
624 
625  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
626  static std::unique_ptr<ASTUnit>
627  create(std::shared_ptr<CompilerInvocation> CI,
628  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, bool CaptureDiagnostics,
629  bool UserFilesAreVolatile);
630 
631  enum WhatToLoad {
632  /// Load options and the preprocessor state.
634  /// Load the AST, but do not restore Sema state.
636  /// Load everything, including Sema.
637  LoadEverything
638  };
639 
640  /// \brief Create a ASTUnit from an AST file.
641  ///
642  /// \param Filename - The AST file to load.
643  ///
644  /// \param PCHContainerRdr - The PCHContainerOperations to use for loading and
645  /// creating modules.
646  /// \param Diags - The diagnostics engine to use for reporting errors; its
647  /// lifetime is expected to extend past that of the returned ASTUnit.
648  ///
649  /// \returns - The initialized ASTUnit or null if the AST failed to load.
650  static std::unique_ptr<ASTUnit> LoadFromASTFile(
651  const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
653  const FileSystemOptions &FileSystemOpts, bool UseDebugInfo = false,
654  bool OnlyLocalDecls = false, ArrayRef<RemappedFile> RemappedFiles = None,
655  bool CaptureDiagnostics = false, bool AllowPCHWithCompilerErrors = false,
656  bool UserFilesAreVolatile = false);
657 
658 private:
659  /// \brief Helper function for \c LoadFromCompilerInvocation() and
660  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
661  ///
662  /// \param PrecompilePreambleAfterNParses After how many parses the preamble
663  /// of this translation unit should be precompiled, to improve the performance
664  /// of reparsing. Set to zero to disable preambles.
665  ///
666  /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
667  /// preamble is saved to a temporary directory on a RealFileSystem, so in order
668  /// for it to be loaded correctly, VFS should have access to it(i.e., be an
669  /// overlay over RealFileSystem).
670  ///
671  /// \returns \c true if a catastrophic failure occurred (which means that the
672  /// \c ASTUnit itself is invalid), or \c false otherwise.
673  bool LoadFromCompilerInvocation(
674  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
675  unsigned PrecompilePreambleAfterNParses,
677 
678 public:
679 
680  /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
681  /// object, by invoking the optionally provided ASTFrontendAction.
682  ///
683  /// \param CI - The compiler invocation to use; it must have exactly one input
684  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
685  ///
686  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
687  /// creating modules.
688  ///
689  /// \param Diags - The diagnostics engine to use for reporting errors; its
690  /// lifetime is expected to extend past that of the returned ASTUnit.
691  ///
692  /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
693  /// transferred.
694  ///
695  /// \param Unit - optionally an already created ASTUnit. Its ownership is not
696  /// transferred.
697  ///
698  /// \param Persistent - if true the returned ASTUnit will be complete.
699  /// false means the caller is only interested in getting info through the
700  /// provided \see Action.
701  ///
702  /// \param ErrAST - If non-null and parsing failed without any AST to return
703  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
704  /// mainly to allow the caller to see the diagnostics.
705  /// This will only receive an ASTUnit if a new one was created. If an already
706  /// created ASTUnit was passed in \p Unit then the caller can check that.
707  ///
708  static ASTUnit *LoadFromCompilerInvocationAction(
709  std::shared_ptr<CompilerInvocation> CI,
710  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
712  FrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
713  bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
714  bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
715  unsigned PrecompilePreambleAfterNParses = 0,
716  bool CacheCodeCompletionResults = false,
717  bool IncludeBriefCommentsInCodeCompletion = false,
718  bool UserFilesAreVolatile = false,
719  std::unique_ptr<ASTUnit> *ErrAST = nullptr);
720 
721  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
722  /// CompilerInvocation object.
723  ///
724  /// \param CI - The compiler invocation to use; it must have exactly one input
725  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
726  ///
727  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
728  /// creating modules.
729  ///
730  /// \param Diags - The diagnostics engine to use for reporting errors; its
731  /// lifetime is expected to extend past that of the returned ASTUnit.
732  //
733  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
734  // shouldn't need to specify them at construction time.
735  static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
736  std::shared_ptr<CompilerInvocation> CI,
737  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
739  bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
740  unsigned PrecompilePreambleAfterNParses = 0,
742  bool CacheCodeCompletionResults = false,
743  bool IncludeBriefCommentsInCodeCompletion = false,
744  bool UserFilesAreVolatile = false);
745 
746  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
747  /// arguments, which must specify exactly one source file.
748  ///
749  /// \param ArgBegin - The beginning of the argument vector.
750  ///
751  /// \param ArgEnd - The end of the argument vector.
752  ///
753  /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
754  /// creating modules.
755  ///
756  /// \param Diags - The diagnostics engine to use for reporting errors; its
757  /// lifetime is expected to extend past that of the returned ASTUnit.
758  ///
759  /// \param ResourceFilesPath - The path to the compiler resource files.
760  ///
761  /// \param ModuleFormat - If provided, uses the specific module format.
762  ///
763  /// \param ErrAST - If non-null and parsing failed without any AST to return
764  /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
765  /// mainly to allow the caller to see the diagnostics.
766  ///
767  /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
768  /// preamble is saved to a temporary directory on a RealFileSystem, so in order
769  /// for it to be loaded correctly, VFS should have access to it(i.e., be an
770  /// overlay over RealFileSystem). RealFileSystem will be used if \p VFS is nullptr.
771  ///
772  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
773  // shouldn't need to specify them at construction time.
774  static ASTUnit *LoadFromCommandLine(
775  const char **ArgBegin, const char **ArgEnd,
776  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
777  IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
778  bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
779  ArrayRef<RemappedFile> RemappedFiles = None,
780  bool RemappedFilesKeepOriginalName = true,
781  unsigned PrecompilePreambleAfterNParses = 0,
783  bool CacheCodeCompletionResults = false,
784  bool IncludeBriefCommentsInCodeCompletion = false,
785  bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,
786  bool SingleFileParse = false,
787  bool UserFilesAreVolatile = false, bool ForSerialization = false,
788  llvm::Optional<StringRef> ModuleFormat = llvm::None,
789  std::unique_ptr<ASTUnit> *ErrAST = nullptr,
791 
792  /// \brief Reparse the source files using the same command-line options that
793  /// were originally used to produce this translation unit.
794  ///
795  /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
796  /// preamble is saved to a temporary directory on a RealFileSystem, so in order
797  /// for it to be loaded correctly, VFS should give an access to this(i.e. be an
798  /// overlay over RealFileSystem). FileMgr->getVirtualFileSystem() will be used if
799  /// \p VFS is nullptr.
800  ///
801  /// \returns True if a failure occurred that causes the ASTUnit not to
802  /// contain any translation-unit information, false otherwise.
803  bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
804  ArrayRef<RemappedFile> RemappedFiles = None,
806 
807  /// \brief Free data that will be re-generated on the next parse.
808  ///
809  /// Preamble-related data is not affected.
810  void ResetForParse();
811 
812  /// \brief Perform code completion at the given file, line, and
813  /// column within this translation unit.
814  ///
815  /// \param File The file in which code completion will occur.
816  ///
817  /// \param Line The line at which code completion will occur.
818  ///
819  /// \param Column The column at which code completion will occur.
820  ///
821  /// \param IncludeMacros Whether to include macros in the code-completion
822  /// results.
823  ///
824  /// \param IncludeCodePatterns Whether to include code patterns (such as a
825  /// for loop) in the code-completion results.
826  ///
827  /// \param IncludeBriefComments Whether to include brief documentation within
828  /// the set of code completions returned.
829  ///
830  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
831  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
832  void CodeComplete(StringRef File, unsigned Line, unsigned Column,
833  ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
834  bool IncludeCodePatterns, bool IncludeBriefComments,
835  CodeCompleteConsumer &Consumer,
836  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
837  DiagnosticsEngine &Diag, LangOptions &LangOpts,
838  SourceManager &SourceMgr, FileManager &FileMgr,
839  SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
841 
842  /// \brief Save this translation unit to a file with the given name.
843  ///
844  /// \returns true if there was a file error or false if the save was
845  /// successful.
846  bool Save(StringRef File);
847 
848  /// \brief Serialize this translation unit with the given output stream.
849  ///
850  /// \returns True if an error occurred, false otherwise.
851  bool serialize(raw_ostream &OS);
852 };
853 
854 } // namespace clang
855 
856 #endif
SourceRange mapRangeToPreamble(SourceRange R) const
Definition: ASTUnit.h:546
stored_diag_iterator stored_diag_afterDriver_begin()
Definition: ASTUnit.h:568
Defines the clang::ASTContext interface.
CXAvailabilityKind
Describes the availability of a particular entity, which indicates whether the use of this entity wil...
Definition: Index.h:131
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
const LangOptions & getLangOpts() const
Definition: ASTUnit.h:429
std::pair< unsigned, unsigned > InsertFromRange
Definition: ASTUnit.h:75
void addTopLevelDecl(Decl *D)
Add a new top-level declaration.
Definition: ASTUnit.h:495
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c.h:60
const Preprocessor & getPreprocessor() const
Definition: ASTUnit.h:413
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
const PreprocessorOptions & getPreprocessorOpts() const
Definition: ASTUnit.h:439
CXAvailabilityKind Availability
The availability of this code-completion result.
Definition: ASTUnit.h:270
Defines the SourceManager interface.
Abstract base class for actions which can be performed by the frontend.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
const FileManager & getFileManager() const
Definition: ASTUnit.h:444
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1346
FileManager & getFileManager()
Definition: ASTUnit.h:445
A module loader that doesn&#39;t know how to load modules.
Definition: ModuleLoader.h:151
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
const FileSystemOptions & getFileSystemOpts() const
Definition: ASTUnit.h:447
std::vector< std::pair< unsigned, unsigned > > Ranges
Definition: ASTUnit.h:86
cached_completion_iterator cached_completion_end()
Definition: ASTUnit.h:581
unsigned Type
The type of a non-macro completion result, stored as a unique integer used by the string map of cache...
Definition: ASTUnit.h:281
CXCursorKind Kind
The libclang cursor kind corresponding to this code-completion result.
Definition: ASTUnit.h:267
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:71
A "string" used to describe how code completion can be performed for an entity.
Preprocessor & getPreprocessor()
Definition: ASTUnit.h:414
stored_diag_const_iterator stored_diag_begin() const
Definition: ASTUnit.h:554
unsigned stored_diag_size() const
Definition: ASTUnit.h:566
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
DiagnosticsEngine::Level Level
Definition: ASTUnit.h:82
Load the AST, but do not restore Sema state.
Definition: ASTUnit.h:635
uint64_t ShowInContexts
A bitmask that indicates which code-completion contexts should contain this completion result...
Definition: ASTUnit.h:260
uint32_t Offset
Definition: CacheTokens.cpp:43
Load options and the preprocessor state.
Definition: ASTUnit.h:633
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:147
bool hasSema() const
Definition: ASTUnit.h:423
ConcurrencyCheck(ASTUnit &Self)
Definition: ASTUnit.h:389
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
CXCursorKind
Describes the kind of entity that a cursor refers to.
Definition: Index.h:1665
void setASTContext(ASTContext *ctx)
Definition: ASTUnit.h:420
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:274
StringRef Filename
Definition: Format.cpp:1316
std::size_t top_level_size() const
Definition: ASTUnit.h:484
std::shared_ptr< GlobalCodeCompletionAllocator > getCachedCompletionAllocator()
Retrieve the allocator used to cache global code completions.
Definition: ASTUnit.h:292
CodeCompletionTUInfo & getCodeCompletionTUInfo()
Definition: ASTUnit.h:296
Defines the clang::LangOptions interface.
bool isMainFileAST() const
Definition: ASTUnit.h:402
std::pair< unsigned, unsigned > RemoveRange
Definition: ASTUnit.h:74
unsigned & getCurrentTopLevelHashValue()
Retrieve a reference to the current top-level name hash value.
Definition: ASTUnit.h:511
const AnnotatedLine * Line
std::vector< StandaloneFixIt > FixIts
Definition: ASTUnit.h:87
#define bool
Definition: stdbool.h:31
stored_diag_const_iterator stored_diag_end() const
Definition: ASTUnit.h:560
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
SourceLocation getEnd() const
void setOwnsRemappedFileBuffers(bool val)
Definition: ASTUnit.h:461
top_level_iterator top_level_begin()
Definition: ASTUnit.h:470
const StoredDiagnostic * stored_diag_const_iterator
Definition: ASTUnit.h:553
bool getOnlyLocalDecls() const
Definition: ASTUnit.h:458
bool top_level_empty() const
Definition: ASTUnit.h:489
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
StringRef getOriginalSourceFileName() const
Definition: ASTUnit.h:451
std::pair< std::string, llvm::MemoryBuffer * > RemappedFile
A mapping from a file name to the memory buffer that stores the remapped contents of that file...
Definition: ASTUnit.h:623
bool isUnsafeToFree() const
Definition: ASTUnit.h:404
llvm::StringMap< unsigned > & getCachedCompletionTypes()
Retrieve the mapping from formatted type names to unique type identifiers.
Definition: ASTUnit.h:286
Sema & getSema() const
Definition: ASTUnit.h:424
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
The kind of a file that we&#39;ve been handed as an input.
void setUnsafeToFree(bool Value)
Definition: ASTUnit.h:405
Defines the clang::TargetOptions class.
TranslationUnitKind getTranslationUnitKind() const
Determine what kind of translation unit this AST represents.
Definition: ASTUnit.h:616
StoredDiagnostic * stored_diag_iterator
Definition: ASTUnit.h:552
bool getOwnsRemappedFileBuffers() const
Definition: ASTUnit.h:460
Abstract interface for a consumer of code-completion information.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
CodeCompletionString * Completion
The code-completion string corresponding to this completion result.
Definition: ASTUnit.h:251
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
std::vector< Decl * >::iterator top_level_iterator
Definition: ASTUnit.h:468
ASTContext & getASTContext()
Definition: ASTUnit.h:418
Helper class for holding the data necessary to invoke the compiler.
const HeaderSearchOptions & getHeaderSearchOpts() const
Definition: ASTUnit.h:434
SourceRange mapRangeFromPreamble(SourceRange R) const
Definition: ASTUnit.h:540
top_level_iterator top_level_end()
Definition: ASTUnit.h:477
Abstract interface for a module loader.
Definition: ModuleLoader.h:69
cached_completion_iterator cached_completion_begin()
Definition: ASTUnit.h:577
unsigned cached_completion_size() const
Definition: ASTUnit.h:585
Defines the clang::FileSystemOptions interface.
SourceManager & getSourceManager()
Definition: ASTUnit.h:411
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
Definition: ASTUnit.h:415
const ASTContext & getASTContext() const
Definition: ASTUnit.h:417
Keeps track of options that affect how file operations are performed.
DiagnosticsEngine & getDiagnostics()
Definition: ASTUnit.h:408
const DiagnosticsEngine & getDiagnostics() const
Definition: ASTUnit.h:407
const SourceManager & getSourceManager() const
Definition: ASTUnit.h:410
SimplifiedTypeClass TypeClass
The simplified type class for a non-macro completion result.
Definition: ASTUnit.h:273
stored_diag_iterator stored_diag_end()
Definition: ASTUnit.h:563
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:150
A cached code-completion result, which may be introduced in one of many different contexts...
Definition: ASTUnit.h:248
std::vector< CachedCodeCompletionResult >::iterator cached_completion_iterator
Definition: ASTUnit.h:575
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:241
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
The translation unit is a complete translation unit.
Definition: LangOptions.h:243
SimplifiedTypeClass
A simplified classification of types used when determining "similar" types for code completion...
unsigned Priority
The priority given to this code-completion result.
Definition: ASTUnit.h:263
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
stored_diag_iterator stored_diag_begin()
Definition: ASTUnit.h:557
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:98