ASTUnit.h revision 206275
10Sduke//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
29330Slana//
30Sduke//                     The LLVM Compiler Infrastructure
40Sduke//
50Sduke// This file is distributed under the University of Illinois Open Source
60Sduke// License. See LICENSE.TXT for details.
70Sduke//
80Sduke//===----------------------------------------------------------------------===//
90Sduke//
100Sduke// ASTUnit utility class.
110Sduke//
120Sduke//===----------------------------------------------------------------------===//
130Sduke
140Sduke#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
150Sduke#define LLVM_CLANG_FRONTEND_ASTUNIT_H
160Sduke
170Sduke#include "clang/Lex/PreprocessingRecord.h"
180Sduke#include "clang/Basic/SourceManager.h"
192362Sohair#include "llvm/ADT/IntrusiveRefCntPtr.h"
202362Sohair#include "llvm/ADT/OwningPtr.h"
212362Sohair#include "clang/Basic/FileManager.h"
220Sduke#include "clang/Index/ASTLocation.h"
230Sduke#include "llvm/ADT/SmallVector.h"
240Sduke#include "llvm/System/Path.h"
250Sduke#include <map>
262603Smalenkov#include <string>
270Sduke#include <vector>
280Sduke#include <cassert>
290Sduke#include <utility>
300Sduke
310Sdukenamespace llvm {
320Sduke  class MemoryBuffer;
330Sduke}
340Sduke
350Sdukenamespace clang {
360Sdukeclass ASTContext;
370Sdukeclass CompilerInvocation;
380Sdukeclass Decl;
392603Smalenkovclass Diagnostic;
402603Smalenkovclass FileEntry;
412603Smalenkovclass FileManager;
422603Smalenkovclass HeaderSearch;
432603Smalenkovclass Preprocessor;
442603Smalenkovclass SourceManager;
450Sdukeclass TargetInfo;
460Sduke
470Sdukeusing namespace idx;
480Sduke
490Sduke/// \brief Utility class for loading a ASTContext from a PCH file.
500Sduke///
510Sdukeclass ASTUnit {
520Sdukepublic:
530Sduke  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
540Sduke    PreprocessedEntitiesByFileMap;
550Sdukeprivate:
560Sduke  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
570Sduke  llvm::OwningPtr<FileManager>      FileMgr;
580Sduke  llvm::OwningPtr<SourceManager>    SourceMgr;
590Sduke  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
600Sduke  llvm::OwningPtr<TargetInfo>       Target;
610Sduke  llvm::OwningPtr<Preprocessor>     PP;
620Sduke  llvm::OwningPtr<ASTContext>       Ctx;
630Sduke
640Sduke  /// Optional owned invocation, just used to make the invocation used in
650Sduke  /// LoadFromCommandLine available.
660Sduke  llvm::OwningPtr<CompilerInvocation> Invocation;
670Sduke
680Sduke  // OnlyLocalDecls - when true, walking this AST should only visit declarations
690Sduke  // that come from the AST itself, not from included precompiled headers.
700Sduke  // FIXME: This is temporary; eventually, CIndex will always do this.
710Sduke  bool                              OnlyLocalDecls;
720Sduke
730Sduke  /// Track whether the main file was loaded from an AST or not.
740Sduke  bool MainFileIsAST;
750Sduke
760Sduke  /// Track the top-level decls which appeared in an ASTUnit which was loaded
770Sduke  /// from a source file.
780Sduke  //
790Sduke  // FIXME: This is just an optimization hack to avoid deserializing large parts
800Sduke  // of a PCH file when using the Index library on an ASTUnit loaded from
810Sduke  // source. In the long term we should make the Index library use efficient and
820Sduke  // more scalable search mechanisms.
830Sduke  std::vector<Decl*> TopLevelDecls;
840Sduke
850Sduke  /// The name of the original source file used to generate this ASTUnit.
860Sduke  std::string OriginalSourceFile;
870Sduke
880Sduke  // Critical optimization when using clang_getCursor().
890Sduke  ASTLocation LastLoc;
900Sduke
910Sduke  /// \brief The set of diagnostics produced when creating this
920Sduke  /// translation unit.
930Sduke  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
940Sduke
950Sduke  /// \brief Temporary files that should be removed when the ASTUnit is
960Sduke  /// destroyed.
970Sduke  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
980Sduke
990Sduke  /// \brief A mapping from file IDs to the set of preprocessed entities
1000Sduke  /// stored in that file.
1010Sduke  ///
1020Sduke  /// FIXME: This is just an optimization hack to avoid searching through
1030Sduke  /// many preprocessed entities during cursor traversal in the CIndex library.
1040Sduke  /// Ideally, we would just be able to perform a binary search within the
1050Sduke  /// list of preprocessed entities.
1060Sduke  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
1070Sduke
1080Sduke  /// \brief Simple hack to allow us to assert that ASTUnit is not being
1090Sduke  /// used concurrently, which is not supported.
1100Sduke  ///
1110Sduke  /// Clients should create instances of the ConcurrencyCheck class whenever
1120Sduke  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
113  /// just about any usage.
114  unsigned int ConcurrencyCheckValue;
115  static const unsigned int CheckLocked = 28573289;
116  static const unsigned int CheckUnlocked = 9803453;
117
118  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
119  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
120
121  explicit ASTUnit(bool MainFileIsAST);
122
123public:
124  class ConcurrencyCheck {
125    volatile ASTUnit &Self;
126
127  public:
128    explicit ConcurrencyCheck(ASTUnit &Self)
129      : Self(Self)
130    {
131      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
132             "Concurrent access to ASTUnit!");
133      Self.ConcurrencyCheckValue = CheckLocked;
134    }
135
136    ~ConcurrencyCheck() {
137      Self.ConcurrencyCheckValue = CheckUnlocked;
138    }
139  };
140  friend class ConcurrencyCheck;
141
142  ~ASTUnit();
143
144  bool isMainFileAST() const { return MainFileIsAST; }
145
146  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
147  Diagnostic &getDiagnostics()             { return *Diagnostics; }
148
149  const SourceManager &getSourceManager() const { return *SourceMgr; }
150        SourceManager &getSourceManager()       { return *SourceMgr; }
151
152  const Preprocessor &getPreprocessor() const { return *PP.get(); }
153        Preprocessor &getPreprocessor()       { return *PP.get(); }
154
155  const ASTContext &getASTContext() const { return *Ctx.get(); }
156        ASTContext &getASTContext()       { return *Ctx.get(); }
157
158  const FileManager &getFileManager() const { return *FileMgr; }
159        FileManager &getFileManager()       { return *FileMgr; }
160
161  const std::string &getOriginalSourceFileName();
162  const std::string &getPCHFileName();
163
164  /// \brief Add a temporary file that the ASTUnit depends on.
165  ///
166  /// This file will be erased when the ASTUnit is destroyed.
167  void addTemporaryFile(const llvm::sys::Path &TempFile) {
168    TemporaryFiles.push_back(TempFile);
169  }
170
171  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
172
173  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
174  ASTLocation getLastASTLocation() const { return LastLoc; }
175
176  std::vector<Decl*> &getTopLevelDecls() {
177    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
178    return TopLevelDecls;
179  }
180  const std::vector<Decl*> &getTopLevelDecls() const {
181    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
182    return TopLevelDecls;
183  }
184
185  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
186  /// within that file.
187  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
188    return PreprocessedEntitiesByFile;
189  }
190
191  // Retrieve the diagnostics associated with this AST
192  typedef const StoredDiagnostic *stored_diag_iterator;
193  stored_diag_iterator stored_diag_begin() const {
194    return StoredDiagnostics.begin();
195  }
196  stored_diag_iterator stored_diag_end() const {
197    return StoredDiagnostics.end();
198  }
199  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
200
201  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
202    return StoredDiagnostics;
203  }
204
205  /// \brief A mapping from a file name to the memory buffer that stores the
206  /// remapped contents of that file.
207  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
208
209  /// \brief Create a ASTUnit from a PCH file.
210  ///
211  /// \param Filename - The PCH file to load.
212  ///
213  /// \param Diags - The diagnostics engine to use for reporting errors; its
214  /// lifetime is expected to extend past that of the returned ASTUnit.
215  ///
216  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
217  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
218                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
219                                  bool OnlyLocalDecls = false,
220                                  RemappedFile *RemappedFiles = 0,
221                                  unsigned NumRemappedFiles = 0,
222                                  bool CaptureDiagnostics = false);
223
224  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
225  /// CompilerInvocation object.
226  ///
227  /// \param CI - The compiler invocation to use; it must have exactly one input
228  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
229  ///
230  /// \param Diags - The diagnostics engine to use for reporting errors; its
231  /// lifetime is expected to extend past that of the returned ASTUnit.
232  //
233  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
234  // shouldn't need to specify them at construction time.
235  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
236                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
237                                             bool OnlyLocalDecls = false,
238                                             bool CaptureDiagnostics = false);
239
240  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
241  /// arguments, which must specify exactly one source file.
242  ///
243  /// \param ArgBegin - The beginning of the argument vector.
244  ///
245  /// \param ArgEnd - The end of the argument vector.
246  ///
247  /// \param Diags - The diagnostics engine to use for reporting errors; its
248  /// lifetime is expected to extend past that of the returned ASTUnit.
249  ///
250  /// \param ResourceFilesPath - The path to the compiler resource files.
251  //
252  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
253  // shouldn't need to specify them at construction time.
254  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
255                                      const char **ArgEnd,
256                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
257                                      llvm::StringRef ResourceFilesPath,
258                                      bool OnlyLocalDecls = false,
259                                      RemappedFile *RemappedFiles = 0,
260                                      unsigned NumRemappedFiles = 0,
261                                      bool CaptureDiagnostics = false);
262};
263
264} // namespace clang
265
266#endif
267