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