ASTUnit.h revision 4cd912aa94656697a44c3ebb159f05060300524e
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/Index/ASTLocation.h"
18#include "clang/Serialization/ASTBitCodes.h"
19#include "clang/Sema/Sema.h"
20#include "clang/Sema/CodeCompleteConsumer.h"
21#include "clang/Lex/PreprocessingRecord.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
24#include "clang-c/Index.h"
25#include "llvm/ADT/IntrusiveRefCntPtr.h"
26#include "llvm/ADT/OwningPtr.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/System/Path.h"
30#include "llvm/Support/Timer.h"
31#include <map>
32#include <string>
33#include <vector>
34#include <cassert>
35#include <utility>
36#include <sys/types.h>
37
38namespace llvm {
39  class MemoryBuffer;
40}
41
42namespace clang {
43class ASTContext;
44class CodeCompleteConsumer;
45class CompilerInvocation;
46class Decl;
47class Diagnostic;
48class FileEntry;
49class FileManager;
50class HeaderSearch;
51class Preprocessor;
52class SourceManager;
53class TargetInfo;
54
55using namespace idx;
56
57/// \brief Utility class for loading a ASTContext from an AST file.
58///
59class ASTUnit {
60public:
61  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
62    PreprocessedEntitiesByFileMap;
63
64private:
65  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
66  llvm::OwningPtr<FileManager>      FileMgr;
67  llvm::OwningPtr<SourceManager>    SourceMgr;
68  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
69  llvm::OwningPtr<TargetInfo>       Target;
70  llvm::OwningPtr<Preprocessor>     PP;
71  llvm::OwningPtr<ASTContext>       Ctx;
72
73  /// \brief The AST consumer that received information about the translation
74  /// unit as it was parsed or loaded.
75  llvm::OwningPtr<ASTConsumer> Consumer;
76
77  /// \brief The semantic analysis object used to type-check the translation
78  /// unit.
79  llvm::OwningPtr<Sema> TheSema;
80
81  /// Optional owned invocation, just used to make the invocation used in
82  /// LoadFromCommandLine available.
83  llvm::OwningPtr<CompilerInvocation> Invocation;
84
85  // OnlyLocalDecls - when true, walking this AST should only visit declarations
86  // that come from the AST itself, not from included precompiled headers.
87  // FIXME: This is temporary; eventually, CIndex will always do this.
88  bool                              OnlyLocalDecls;
89
90  /// \brief Whether to capture any diagnostics produced.
91  bool CaptureDiagnostics;
92
93  /// \brief Track whether the main file was loaded from an AST or not.
94  bool MainFileIsAST;
95
96  /// \brief Whether this AST represents a complete translation unit.
97  bool CompleteTranslationUnit;
98
99  /// Track the top-level decls which appeared in an ASTUnit which was loaded
100  /// from a source file.
101  //
102  // FIXME: This is just an optimization hack to avoid deserializing large parts
103  // of a PCH file when using the Index library on an ASTUnit loaded from
104  // source. In the long term we should make the Index library use efficient and
105  // more scalable search mechanisms.
106  std::vector<Decl*> TopLevelDecls;
107
108  /// The name of the original source file used to generate this ASTUnit.
109  std::string OriginalSourceFile;
110
111  // Critical optimization when using clang_getCursor().
112  ASTLocation LastLoc;
113
114  /// \brief The set of diagnostics produced when creating this
115  /// translation unit.
116  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
117
118  /// \brief The number of stored diagnostics that come from the driver
119  /// itself.
120  ///
121  /// Diagnostics that come from the driver are retained from one parse to
122  /// the next.
123  unsigned NumStoredDiagnosticsFromDriver;
124
125  /// \brief Temporary files that should be removed when the ASTUnit is
126  /// destroyed.
127  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
128
129  /// \brief A mapping from file IDs to the set of preprocessed entities
130  /// stored in that file.
131  ///
132  /// FIXME: This is just an optimization hack to avoid searching through
133  /// many preprocessed entities during cursor traversal in the CIndex library.
134  /// Ideally, we would just be able to perform a binary search within the
135  /// list of preprocessed entities.
136  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
137
138  /// \brief Simple hack to allow us to assert that ASTUnit is not being
139  /// used concurrently, which is not supported.
140  ///
141  /// Clients should create instances of the ConcurrencyCheck class whenever
142  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
143  /// just about any usage.
144  unsigned int ConcurrencyCheckValue;
145  static const unsigned int CheckLocked = 28573289;
146  static const unsigned int CheckUnlocked = 9803453;
147
148  /// \brief Counter that determines when we want to try building a
149  /// precompiled preamble.
150  ///
151  /// If zero, we will never build a precompiled preamble. Otherwise,
152  /// it's treated as a counter that decrements each time we reparse
153  /// without the benefit of a precompiled preamble. When it hits 1,
154  /// we'll attempt to rebuild the precompiled header. This way, if
155  /// building the precompiled preamble fails, we won't try again for
156  /// some number of calls.
157  unsigned PreambleRebuildCounter;
158
159  /// \brief The file in which the precompiled preamble is stored.
160  std::string PreambleFile;
161
162  /// \brief The contents of the preamble that has been precompiled to
163  /// \c PreambleFile.
164  std::vector<char> Preamble;
165
166  /// \brief Whether the preamble ends at the start of a new line.
167  ///
168  /// Used to inform the lexer as to whether it's starting at the beginning of
169  /// a line after skipping the preamble.
170  bool PreambleEndsAtStartOfLine;
171
172  /// \brief The size of the source buffer that we've reserved for the main
173  /// file within the precompiled preamble.
174  unsigned PreambleReservedSize;
175
176  /// \brief Keeps track of the files that were used when computing the
177  /// preamble, with both their buffer size and their modification time.
178  ///
179  /// If any of the files have changed from one compile to the next,
180  /// the preamble must be thrown away.
181  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
182
183  /// \brief When non-NULL, this is the buffer used to store the contents of
184  /// the main file when it has been padded for use with the precompiled
185  /// preamble.
186  llvm::MemoryBuffer *SavedMainFileBuffer;
187
188  /// \brief When non-NULL, this is the buffer used to store the
189  /// contents of the preamble when it has been padded to build the
190  /// precompiled preamble.
191  llvm::MemoryBuffer *PreambleBuffer;
192
193  /// \brief The number of warnings that occurred while parsing the preamble.
194  ///
195  /// This value will be used to restore the state of the \c Diagnostic object
196  /// when re-using the precompiled preamble. Note that only the
197  /// number of warnings matters, since we will not save the preamble
198  /// when any errors are present.
199  unsigned NumWarningsInPreamble;
200
201  /// \brief The number of diagnostics that were stored when parsing
202  /// the precompiled preamble.
203  ///
204  /// This value is used to determine how many of the stored
205  /// diagnostics should be retained when reparsing in the presence of
206  /// a precompiled preamble.
207  unsigned NumStoredDiagnosticsInPreamble;
208
209  /// \brief The group of timers associated with this translation unit.
210  llvm::OwningPtr<llvm::TimerGroup> TimerGroup;
211
212  /// \brief A list of the serialization ID numbers for each of the top-level
213  /// declarations parsed within the precompiled preamble.
214  std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
215
216  ///
217  /// \defgroup CodeCompleteCaching Code-completion caching
218  ///
219  /// \{
220  ///
221
222  /// \brief Whether we should be caching code-completion results.
223  bool ShouldCacheCodeCompletionResults;
224
225public:
226  /// \brief A cached code-completion result, which may be introduced in one of
227  /// many different contexts.
228  struct CachedCodeCompletionResult {
229    /// \brief The code-completion string corresponding to this completion
230    /// result.
231    CodeCompletionString *Completion;
232
233    /// \brief A bitmask that indicates which code-completion contexts should
234    /// contain this completion result.
235    ///
236    /// The bits in the bitmask correspond to the values of
237    /// CodeCompleteContext::Kind. To map from a completion context kind to a
238    /// bit, subtract one from the completion context kind and shift 1 by that
239    /// number of bits. Many completions can occur in several different
240    /// contexts.
241    unsigned ShowInContexts;
242
243    /// \brief The priority given to this code-completion result.
244    unsigned Priority;
245
246    /// \brief The libclang cursor kind corresponding to this code-completion
247    /// result.
248    CXCursorKind Kind;
249
250    /// \brief The availability of this code-completion result.
251    CXAvailabilityKind Availability;
252
253    /// \brief The simplified type class for a non-macro completion result.
254    SimplifiedTypeClass TypeClass;
255
256    /// \brief The type of a non-macro completion result, stored as a unique
257    /// integer used by the string map of cached completion types.
258    ///
259    /// This value will be zero if the type is not known, or a unique value
260    /// determined by the formatted type string. Se \c CachedCompletionTypes
261    /// for more information.
262    unsigned Type;
263  };
264
265  /// \brief Retrieve the mapping from formatted type names to unique type
266  /// identifiers.
267  llvm::StringMap<unsigned> &getCachedCompletionTypes() {
268    return CachedCompletionTypes;
269  }
270
271private:
272  /// \brief The set of cached code-completion results.
273  std::vector<CachedCodeCompletionResult> CachedCompletionResults;
274
275  /// \brief A mapping from the formatted type name to a unique number for that
276  /// type, which is used for type equality comparisons.
277  llvm::StringMap<unsigned> CachedCompletionTypes;
278
279  /// \brief The number of top-level declarations present the last time we
280  /// cached code-completion results.
281  ///
282  /// The value is used to help detect when we should repopulate the global
283  /// completion cache.
284  unsigned NumTopLevelDeclsAtLastCompletionCache;
285
286  /// \brief The number of reparses left until we'll consider updating the
287  /// code-completion cache.
288  ///
289  /// This is meant to avoid thrashing during reparsing, by not allowing the
290  /// code-completion cache to be updated on every reparse.
291  unsigned CacheCodeCompletionCoolDown;
292
293  /// \brief Bit used by CIndex to mark when a translation unit may be in an
294  /// inconsistent state, and is not safe to free.
295  unsigned UnsafeToFree : 1;
296
297  /// \brief Cache any "global" code-completion results, so that we can avoid
298  /// recomputing them with each completion.
299  void CacheCodeCompletionResults();
300
301  /// \brief Clear out and deallocate
302  void ClearCachedCompletionResults();
303
304  ///
305  /// \}
306  ///
307
308  /// \brief The timers we've created from the various parses, reparses, etc.
309  /// involved in this translation unit.
310  std::vector<llvm::Timer *> Timers;
311
312  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
313  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
314
315  explicit ASTUnit(bool MainFileIsAST);
316
317  void CleanTemporaryFiles();
318  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
319
320  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
321  ComputePreamble(CompilerInvocation &Invocation,
322                  unsigned MaxLines, bool &CreatedBuffer);
323
324  llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
325                                         CompilerInvocation PreambleInvocation,
326                                                     bool AllowRebuild = true,
327                                                        unsigned MaxLines = 0);
328  void RealizeTopLevelDeclsFromPreamble();
329
330public:
331  class ConcurrencyCheck {
332    volatile ASTUnit &Self;
333
334  public:
335    explicit ConcurrencyCheck(ASTUnit &Self)
336      : Self(Self)
337    {
338      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
339             "Concurrent access to ASTUnit!");
340      Self.ConcurrencyCheckValue = CheckLocked;
341    }
342
343    ~ConcurrencyCheck() {
344      Self.ConcurrencyCheckValue = CheckUnlocked;
345    }
346  };
347  friend class ConcurrencyCheck;
348
349  ~ASTUnit();
350
351  bool isMainFileAST() const { return MainFileIsAST; }
352
353  bool isUnsafeToFree() const { return UnsafeToFree; }
354  void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
355
356  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
357  Diagnostic &getDiagnostics()             { return *Diagnostics; }
358
359  const SourceManager &getSourceManager() const { return *SourceMgr; }
360        SourceManager &getSourceManager()       { return *SourceMgr; }
361
362  const Preprocessor &getPreprocessor() const { return *PP.get(); }
363        Preprocessor &getPreprocessor()       { return *PP.get(); }
364
365  const ASTContext &getASTContext() const { return *Ctx.get(); }
366        ASTContext &getASTContext()       { return *Ctx.get(); }
367
368  bool hasSema() const { return TheSema; }
369  Sema &getSema() const {
370    assert(TheSema && "ASTUnit does not have a Sema object!");
371    return *TheSema;
372  }
373
374  const FileManager &getFileManager() const { return *FileMgr; }
375        FileManager &getFileManager()       { return *FileMgr; }
376
377  const std::string &getOriginalSourceFileName();
378  const std::string &getASTFileName();
379
380  /// \brief Add a temporary file that the ASTUnit depends on.
381  ///
382  /// This file will be erased when the ASTUnit is destroyed.
383  void addTemporaryFile(const llvm::sys::Path &TempFile) {
384    TemporaryFiles.push_back(TempFile);
385  }
386
387  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
388
389  /// \brief Retrieve the maximum PCH level of declarations that a
390  /// traversal of the translation unit should consider.
391  unsigned getMaxPCHLevel() const;
392
393  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
394  ASTLocation getLastASTLocation() const { return LastLoc; }
395
396  typedef std::vector<Decl *>::iterator top_level_iterator;
397
398  top_level_iterator top_level_begin() {
399    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
400    if (!TopLevelDeclsInPreamble.empty())
401      RealizeTopLevelDeclsFromPreamble();
402    return TopLevelDecls.begin();
403  }
404
405  top_level_iterator top_level_end() {
406    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
407    if (!TopLevelDeclsInPreamble.empty())
408      RealizeTopLevelDeclsFromPreamble();
409    return TopLevelDecls.end();
410  }
411
412  std::size_t top_level_size() const {
413    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
414    return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
415  }
416
417  bool top_level_empty() const {
418    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
419    return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
420  }
421
422  /// \brief Add a new top-level declaration.
423  void addTopLevelDecl(Decl *D) {
424    TopLevelDecls.push_back(D);
425  }
426
427  /// \brief Add a new top-level declaration, identified by its ID in
428  /// the precompiled preamble.
429  void addTopLevelDeclFromPreamble(serialization::DeclID D) {
430    TopLevelDeclsInPreamble.push_back(D);
431  }
432
433  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
434  /// within that file.
435  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
436    return PreprocessedEntitiesByFile;
437  }
438
439  // Retrieve the diagnostics associated with this AST
440  typedef const StoredDiagnostic *stored_diag_iterator;
441  stored_diag_iterator stored_diag_begin() const {
442    return StoredDiagnostics.begin();
443  }
444  stored_diag_iterator stored_diag_end() const {
445    return StoredDiagnostics.end();
446  }
447  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
448
449  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
450    return StoredDiagnostics;
451  }
452
453  typedef std::vector<CachedCodeCompletionResult>::iterator
454    cached_completion_iterator;
455
456  cached_completion_iterator cached_completion_begin() {
457    return CachedCompletionResults.begin();
458  }
459
460  cached_completion_iterator cached_completion_end() {
461    return CachedCompletionResults.end();
462  }
463
464  unsigned cached_completion_size() const {
465    return CachedCompletionResults.size();
466  }
467
468  /// \brief Whether this AST represents a complete translation unit.
469  ///
470  /// If false, this AST is only a partial translation unit, e.g., one
471  /// that might still be used as a precompiled header or preamble.
472  bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
473
474  /// \brief A mapping from a file name to the memory buffer that stores the
475  /// remapped contents of that file.
476  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
477
478  /// \brief Create a ASTUnit from an AST file.
479  ///
480  /// \param Filename - The AST file to load.
481  ///
482  /// \param Diags - The diagnostics engine to use for reporting errors; its
483  /// lifetime is expected to extend past that of the returned ASTUnit.
484  ///
485  /// \returns - The initialized ASTUnit or null if the AST failed to load.
486  static ASTUnit *LoadFromASTFile(const std::string &Filename,
487                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
488                                  bool OnlyLocalDecls = false,
489                                  RemappedFile *RemappedFiles = 0,
490                                  unsigned NumRemappedFiles = 0,
491                                  bool CaptureDiagnostics = false);
492
493private:
494  /// \brief Helper function for \c LoadFromCompilerInvocation() and
495  /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
496  ///
497  /// \param PrecompilePreamble Whether to precompile the preamble of this
498  /// translation unit, to improve the performance of reparsing.
499  ///
500  /// \returns \c true if a catastrophic failure occurred (which means that the
501  /// \c ASTUnit itself is invalid), or \c false otherwise.
502  bool LoadFromCompilerInvocation(bool PrecompilePreamble);
503
504public:
505
506  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
507  /// CompilerInvocation object.
508  ///
509  /// \param CI - The compiler invocation to use; it must have exactly one input
510  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
511  ///
512  /// \param Diags - The diagnostics engine to use for reporting errors; its
513  /// lifetime is expected to extend past that of the returned ASTUnit.
514  //
515  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
516  // shouldn't need to specify them at construction time.
517  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
518                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
519                                             bool OnlyLocalDecls = false,
520                                             bool CaptureDiagnostics = false,
521                                             bool PrecompilePreamble = false,
522                                          bool CompleteTranslationUnit = true,
523                                       bool CacheCodeCompletionResults = false);
524
525  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
526  /// arguments, which must specify exactly one source file.
527  ///
528  /// \param ArgBegin - The beginning of the argument vector.
529  ///
530  /// \param ArgEnd - The end of the argument vector.
531  ///
532  /// \param Diags - The diagnostics engine to use for reporting errors; its
533  /// lifetime is expected to extend past that of the returned ASTUnit.
534  ///
535  /// \param ResourceFilesPath - The path to the compiler resource files.
536  //
537  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
538  // shouldn't need to specify them at construction time.
539  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
540                                      const char **ArgEnd,
541                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
542                                      llvm::StringRef ResourceFilesPath,
543                                      bool OnlyLocalDecls = false,
544                                      RemappedFile *RemappedFiles = 0,
545                                      unsigned NumRemappedFiles = 0,
546                                      bool CaptureDiagnostics = false,
547                                      bool PrecompilePreamble = false,
548                                      bool CompleteTranslationUnit = true,
549                                      bool CacheCodeCompletionResults = false);
550
551  /// \brief Reparse the source files using the same command-line options that
552  /// were originally used to produce this translation unit.
553  ///
554  /// \returns True if a failure occurred that causes the ASTUnit not to
555  /// contain any translation-unit information, false otherwise.
556  bool Reparse(RemappedFile *RemappedFiles = 0,
557               unsigned NumRemappedFiles = 0);
558
559  /// \brief Perform code completion at the given file, line, and
560  /// column within this translation unit.
561  ///
562  /// \param File The file in which code completion will occur.
563  ///
564  /// \param Line The line at which code completion will occur.
565  ///
566  /// \param Column The column at which code completion will occur.
567  ///
568  /// \param IncludeMacros Whether to include macros in the code-completion
569  /// results.
570  ///
571  /// \param IncludeCodePatterns Whether to include code patterns (such as a
572  /// for loop) in the code-completion results.
573  ///
574  /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
575  /// OwnedBuffers parameters are all disgusting hacks. They will go away.
576  void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
577                    RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
578                    bool IncludeMacros, bool IncludeCodePatterns,
579                    CodeCompleteConsumer &Consumer,
580                    Diagnostic &Diag, LangOptions &LangOpts,
581                    SourceManager &SourceMgr, FileManager &FileMgr,
582                    llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
583              llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
584
585  /// \brief Save this translation unit to a file with the given name.
586  ///
587  /// \returns True if an error occurred, false otherwise.
588  bool Save(llvm::StringRef File);
589};
590
591} // namespace clang
592
593#endif
594