ASTUnit.h revision 521bf9c529e653ab28896d027352d3e16e2672d5
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/Frontend/TextDiagnosticBuffer.h"
20#include "clang/Basic/FileManager.h"
21#include "clang/Index/ASTLocation.h"
22#include <string>
23
24namespace clang {
25class ASTContext;
26class CompilerInvocation;
27class Decl;
28class Diagnostic;
29class FileEntry;
30class FileManager;
31class HeaderSearch;
32class Preprocessor;
33class SourceManager;
34class TargetInfo;
35class TextDiagnosticBuffer;
36
37using namespace idx;
38
39/// \brief Utility class for loading a ASTContext from a PCH file.
40///
41class ASTUnit {
42  Diagnostic Diags;
43  FileManager FileMgr;
44
45  SourceManager                     SourceMgr;
46  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
47  llvm::OwningPtr<TargetInfo>       Target;
48  llvm::OwningPtr<Preprocessor>     PP;
49  llvm::OwningPtr<ASTContext>       Ctx;
50  bool                              tempFile;
51
52  // OnlyLocalDecls - when true, walking this AST should only visit declarations
53  // that come from the AST itself, not from included precompiled headers.
54  // FIXME: This is temporary; eventually, CIndex will always do this.
55  bool                              OnlyLocalDecls;
56
57  // Critical optimization when using clang_getCursor().
58  ASTLocation LastLoc;
59
60  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
61  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
62
63public:
64  ASTUnit(DiagnosticClient *diagClient = NULL);
65  ~ASTUnit();
66
67  const SourceManager &getSourceManager() const { return SourceMgr; }
68        SourceManager &getSourceManager()       { return SourceMgr; }
69
70  const Preprocessor &getPreprocessor() const { return *PP.get(); }
71        Preprocessor &getPreprocessor()       { return *PP.get(); }
72
73  const ASTContext &getASTContext() const { return *Ctx.get(); }
74        ASTContext &getASTContext()       { return *Ctx.get(); }
75
76  const Diagnostic &getDiagnostic() const { return Diags; }
77        Diagnostic &getDiagnostic()       { return Diags; }
78
79  const FileManager &getFileManager() const { return FileMgr; }
80        FileManager &getFileManager()       { return FileMgr; }
81
82  const std::string &getOriginalSourceFileName();
83  const std::string &getPCHFileName();
84
85  void unlinkTemporaryFile() { tempFile = true; }
86
87  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
88
89  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
90  ASTLocation getLastASTLocation() const { return LastLoc; }
91
92  /// \brief Create a ASTUnit from a PCH file.
93  ///
94  /// \param Filename - The PCH file to load.
95  ///
96  /// \param DiagClient - The diagnostics client to use.  Specify NULL
97  /// to use a default client that emits warnings/errors to standard error.
98  /// The ASTUnit objects takes ownership of this object.
99  ///
100  /// \param ErrMsg - Error message to report if the PCH file could not be
101  /// loaded.
102  ///
103  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
104  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
105                                  std::string *ErrMsg = 0,
106                                  DiagnosticClient *DiagClient = NULL,
107                                  bool OnlyLocalDecls = false,
108                                  bool UseBumpAllocator = false);
109
110  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
111  /// CompilerInvocation object.
112  ///
113  /// \param CI - The compiler invocation to use; it must have exactly one input
114  /// source file.
115  ///
116  /// \param Diags - The diagnostics engine to use for reporting errors.
117  //
118  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
119  // shouldn't need to specify them at construction time.
120  static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI,
121                                             Diagnostic &Diags,
122                                             bool OnlyLocalDecls = false,
123                                             bool UseBumpAllocator = false);
124
125};
126
127} // namespace clang
128
129#endif
130