ASTUnit.h revision c7822dbf3c01a2a5f837cff82ba7889ea755daca
14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// License. See LICENSE.TXT for details.
74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// ASTUnit utility class.
114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis
140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#define LLVM_CLANG_FRONTEND_ASTUNIT_H
160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1731b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
180853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
1936c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff#include "clang/Frontend/TextDiagnosticBuffer.h"
2036c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff#include "clang/Basic/FileManager.h"
21f96b524306ccfa623235d375deee79637bd38f29Steve Naroff#include "clang/Index/ASTLocation.h"
220853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
230853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
240853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
25521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
26521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
27521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
28521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
29521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
30521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
31521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
32521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
33521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
34521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
35521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TextDiagnosticBuffer;
360853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
37f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
38f96b524306ccfa623235d375deee79637bd38f29Steve Naroff
390853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis/// \brief Utility class for loading a ASTContext from a PCH file.
400853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
4236c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff  Diagnostic Diags;
4336c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff  FileManager FileMgr;
4436c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff
4531b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  SourceManager                     SourceMgr;
460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
470853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<TargetInfo>       Target;
480853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<Preprocessor>     PP;
490853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<ASTContext>       Ctx;
50e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  bool                              tempFile;
51e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff
527d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
537d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
547d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
557d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
56c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
57c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  // Track whether the main file was loaded from an AST or not.
58c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
59c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
6068d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
6168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
6268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
63f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
64f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
65f96b524306ccfa623235d375deee79637bd38f29Steve Naroff
6631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
6731b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
70c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  ASTUnit(bool MainFileIsAST, DiagnosticClient *diagClient = NULL);
710853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
720853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
73c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
74c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
7531b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  const SourceManager &getSourceManager() const { return SourceMgr; }
7631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar        SourceManager &getSourceManager()       { return SourceMgr; }
770853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
780853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
790853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
810853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
820853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
830853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
8431b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  const Diagnostic &getDiagnostic() const { return Diags; }
8531b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar        Diagnostic &getDiagnostic()       { return Diags; }
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8736c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff  const FileManager &getFileManager() const { return FileMgr; }
8836c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff        FileManager &getFileManager()       { return FileMgr; }
8936c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff
9077accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
91e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  const std::string &getPCHFileName();
92b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
93e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  void unlinkTemporaryFile() { tempFile = true; }
94e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff
957d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
967d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor
97f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
98f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
99f96b524306ccfa623235d375deee79637bd38f29Steve Naroff
1000853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  /// \brief Create a ASTUnit from a PCH file.
1010853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
10231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \param Filename - The PCH file to load.
1030853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
104521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param DiagClient - The diagnostics client to use.  Specify NULL
105fc0622155fa61349698a8fd0053773c37d9f7ac4Ted Kremenek  /// to use a default client that emits warnings/errors to standard error.
106fc0622155fa61349698a8fd0053773c37d9f7ac4Ted Kremenek  /// The ASTUnit objects takes ownership of this object.
1070853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
10831b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \param ErrMsg - Error message to report if the PCH file could not be
10931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// loaded.
11031b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
11131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
1120853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
1137d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor                                  std::string *ErrMsg = 0,
114521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar                                  DiagnosticClient *DiagClient = NULL,
1155cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
1165cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool UseBumpAllocator = false);
117521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
118521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
119521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
120521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
121521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
122521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// source file.
123521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
124521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors.
125521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
126521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
127521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
128521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI,
129521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar                                             Diagnostic &Diags,
13068ea2ac7fd98a5a44c7a5b04c54076cf794531cbDaniel Dunbar                                             bool OnlyLocalDecls = false);
131521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
1327b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
1337b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
1347b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1357b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
1367b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1377b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
1387b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1397b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors.
1407b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1417b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param Argv0 - The program path (from argv[0]), for finding the builtin
1427b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// compiler path.
1437b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1447b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param MainAddr - The address of main (or some other function in the main
1457b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// executable), for finding the builtin compiler path.
1467b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
1477b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
1487b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
1497b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
1507b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
1517b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      Diagnostic &Diags,
1527b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char *Arg0,
1537b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      void *MainAddr,
1547b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
1557b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool UseBumpAllocator = false);
1560853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
1570853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1580853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
1590853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1600853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
161