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