ASTUnit.h revision 63daa6179ea77eb4ff7a213c891aff5f32aca03f
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 <string>
22
23namespace clang {
24  class FileManager;
25  class FileEntry;
26  class SourceManager;
27  class Diagnostic;
28  class TextDiagnosticBuffer;
29  class HeaderSearch;
30  class TargetInfo;
31  class Preprocessor;
32  class ASTContext;
33  class Decl;
34
35/// \brief Utility class for loading a ASTContext from a PCH file.
36///
37class ASTUnit {
38  Diagnostic Diags;
39  FileManager FileMgr;
40
41  SourceManager                     SourceMgr;
42  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
43  llvm::OwningPtr<TargetInfo>       Target;
44  llvm::OwningPtr<Preprocessor>     PP;
45  llvm::OwningPtr<ASTContext>       Ctx;
46  bool                              tempFile;
47
48  // OnlyLocalDecls - when true, walking this AST should only visit declarations
49  // that come from the AST itself, not from included precompiled headers.
50  // FIXME: This is temporary; eventually, CIndex will always do this.
51  bool                              OnlyLocalDecls;
52
53  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
54  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
55
56public:
57  ASTUnit(DiagnosticClient *diagClient = NULL);
58  ~ASTUnit();
59
60  const SourceManager &getSourceManager() const { return SourceMgr; }
61        SourceManager &getSourceManager()       { return SourceMgr; }
62
63  const Preprocessor &getPreprocessor() const { return *PP.get(); }
64        Preprocessor &getPreprocessor()       { return *PP.get(); }
65
66  const ASTContext &getASTContext() const { return *Ctx.get(); }
67        ASTContext &getASTContext()       { return *Ctx.get(); }
68
69  const Diagnostic &getDiagnostic() const { return Diags; }
70        Diagnostic &getDiagnostic()       { return Diags; }
71
72  const FileManager &getFileManager() const { return FileMgr; }
73        FileManager &getFileManager()       { return FileMgr; }
74
75  const std::string &getOriginalSourceFileName();
76  const std::string &getPCHFileName();
77
78  void unlinkTemporaryFile() { tempFile = true; }
79
80  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
81
82  /// \brief Create a ASTUnit from a PCH file.
83  ///
84  /// \param Filename - The PCH file to load.
85  ///
86  /// \param diagClient - The diagnostics client to use.  Specify NULL
87  /// to use a default client that emits warnings/errors to standard error.
88  /// The ASTUnit objects takes ownership of this object.
89  ///
90  /// \param FileMgr - The FileManager to use.
91  ///
92  /// \param ErrMsg - Error message to report if the PCH file could not be
93  /// loaded.
94  ///
95  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
96  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
97                                  std::string *ErrMsg = 0,
98                                  DiagnosticClient *diagClient = NULL,
99                                  bool OnlyLocalDecls = false,
100                                  bool UseBumpAllocator = false);
101};
102
103} // namespace clang
104
105#endif
106