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