ASTUnit.h revision 4db64a461cb3442934afe43c83ed3f17f7c11c1d
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/Basic/SourceManager.h"
18#include "llvm/ADT/OwningPtr.h"
19#include "clang/Basic/FileManager.h"
20#include "clang/Index/ASTLocation.h"
21#include <string>
22#include <vector>
23#include <cassert>
24#include <utility>
25
26namespace llvm {
27  class MemoryBuffer;
28}
29
30namespace clang {
31class ASTContext;
32class CompilerInvocation;
33class Decl;
34class Diagnostic;
35class FileEntry;
36class FileManager;
37class HeaderSearch;
38class Preprocessor;
39class SourceManager;
40class TargetInfo;
41
42using namespace idx;
43
44/// \brief Utility class for loading a ASTContext from a PCH file.
45///
46class ASTUnit {
47  FileManager FileMgr;
48
49  SourceManager                     SourceMgr;
50  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
51  llvm::OwningPtr<TargetInfo>       Target;
52  llvm::OwningPtr<Preprocessor>     PP;
53  llvm::OwningPtr<ASTContext>       Ctx;
54  bool                              tempFile;
55
56  // OnlyLocalDecls - when true, walking this AST should only visit declarations
57  // that come from the AST itself, not from included precompiled headers.
58  // FIXME: This is temporary; eventually, CIndex will always do this.
59  bool                              OnlyLocalDecls;
60
61  /// Track whether the main file was loaded from an AST or not.
62  bool MainFileIsAST;
63
64  /// Track the top-level decls which appeared in an ASTUnit which was loaded
65  /// from a source file.
66  //
67  // FIXME: This is just an optimization hack to avoid deserializing large parts
68  // of a PCH file when using the Index library on an ASTUnit loaded from
69  // source. In the long term we should make the Index library use efficient and
70  // more scalable search mechanisms.
71  std::vector<Decl*> TopLevelDecls;
72
73  /// The name of the original source file used to generate this ASTUnit.
74  std::string OriginalSourceFile;
75
76  // Critical optimization when using clang_getCursor().
77  ASTLocation LastLoc;
78
79  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
80  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
81
82public:
83  ASTUnit(bool MainFileIsAST);
84  ~ASTUnit();
85
86  bool isMainFileAST() const { return MainFileIsAST; }
87
88  const SourceManager &getSourceManager() const { return SourceMgr; }
89        SourceManager &getSourceManager()       { return SourceMgr; }
90
91  const Preprocessor &getPreprocessor() const { return *PP.get(); }
92        Preprocessor &getPreprocessor()       { return *PP.get(); }
93
94  const ASTContext &getASTContext() const { return *Ctx.get(); }
95        ASTContext &getASTContext()       { return *Ctx.get(); }
96
97  const FileManager &getFileManager() const { return FileMgr; }
98        FileManager &getFileManager()       { return FileMgr; }
99
100  const std::string &getOriginalSourceFileName();
101  const std::string &getPCHFileName();
102
103  void unlinkTemporaryFile() { tempFile = true; }
104
105  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
106
107  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
108  ASTLocation getLastASTLocation() const { return LastLoc; }
109
110  std::vector<Decl*> &getTopLevelDecls() {
111    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
112    return TopLevelDecls;
113  }
114  const std::vector<Decl*> &getTopLevelDecls() const {
115    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
116    return TopLevelDecls;
117  }
118
119  /// \brief A mapping from a file name to the memory buffer that stores the
120  /// remapped contents of that file.
121  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
122
123  /// \brief Create a ASTUnit from a PCH file.
124  ///
125  /// \param Filename - The PCH file to load.
126  ///
127  /// \param Diags - The diagnostics engine to use for reporting errors; its
128  /// lifetime is expected to extend past that of the returned ASTUnit.
129  ///
130  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
131  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
132                                  Diagnostic &Diags,
133                                  bool OnlyLocalDecls = false,
134                                  bool UseBumpAllocator = false,
135                                  RemappedFile *RemappedFiles = 0,
136                                  unsigned NumRemappedFiles = 0);
137
138  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
139  /// CompilerInvocation object.
140  ///
141  /// \param CI - The compiler invocation to use; it must have exactly one input
142  /// source file.
143  ///
144  /// \param Diags - The diagnostics engine to use for reporting errors; its
145  /// lifetime is expected to extend past that of the returned ASTUnit.
146  //
147  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
148  // shouldn't need to specify them at construction time.
149  static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI,
150                                             Diagnostic &Diags,
151                                             bool OnlyLocalDecls = false);
152
153  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
154  /// arguments, which must specify exactly one source file.
155  ///
156  /// \param ArgBegin - The beginning of the argument vector.
157  ///
158  /// \param ArgEnd - The end of the argument vector.
159  ///
160  /// \param Diags - The diagnostics engine to use for reporting errors; its
161  /// lifetime is expected to extend past that of the returned ASTUnit.
162  ///
163  /// \param ResourceFilesPath - The path to the compiler resource files.
164  //
165  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
166  // shouldn't need to specify them at construction time.
167  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
168                                      const char **ArgEnd,
169                                      Diagnostic &Diags,
170                                      llvm::StringRef ResourceFilesPath,
171                                      bool OnlyLocalDecls = false,
172                                      bool UseBumpAllocator = false,
173                                      RemappedFile *RemappedFiles = 0,
174                                      unsigned NumRemappedFiles = 0);
175};
176
177} // namespace clang
178
179#endif
180