ASTUnit.h revision 807b06157a1a5c050520fc194d32f16d22d423a8
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/Basic/FileManager.h"
20f96b524306ccfa623235d375deee79637bd38f29Steve Naroff#include "clang/Index/ASTLocation.h"
210853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
22f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
23f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
244db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
254db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
264db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
274db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
284db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
290853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
300853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
31521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
32521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
33521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
34521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
35521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
36521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
37521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
38521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
39521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
40521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
42f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
43f96b524306ccfa623235d375deee79637bd38f29Steve Naroff
440853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis/// \brief Utility class for loading a ASTContext from a PCH file.
450853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
460853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
4736c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff  FileManager FileMgr;
4836c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff
4931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  SourceManager                     SourceMgr;
500853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
510853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<TargetInfo>       Target;
520853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<Preprocessor>     PP;
530853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<ASTContext>       Ctx;
54e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  bool                              tempFile;
55f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
56807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
57807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
58807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  llvm::OwningPtr<CompilerInvocation> Invocation;
59807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar
607d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
617d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
627d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
637d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
64c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
65f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track whether the main file was loaded from an AST or not.
66c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
67c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
68f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
69f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
70f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
71f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
72f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
73f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
74f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
75f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
76f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
7768d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
7868d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
7968d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
80f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
81f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
82f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
8331b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
8431b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
860853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
875262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  ASTUnit(bool MainFileIsAST);
880853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
890853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
90c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
91c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
9231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  const SourceManager &getSourceManager() const { return SourceMgr; }
9331b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar        SourceManager &getSourceManager()       { return SourceMgr; }
940853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
950853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
960853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
980853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
990853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
1000853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
10136c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff  const FileManager &getFileManager() const { return FileMgr; }
10236c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff        FileManager &getFileManager()       { return FileMgr; }
103f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
10477accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
105e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  const std::string &getPCHFileName();
106b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
107e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  void unlinkTemporaryFile() { tempFile = true; }
108f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
1097d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
110f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
111f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
112f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
113f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
114f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> &getTopLevelDecls() {
115f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
116f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
117f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
118f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  const std::vector<Decl*> &getTopLevelDecls() const {
119f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
120f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
121f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
122f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
1234db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
1244db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
1254db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
1264db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
1270853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  /// \brief Create a ASTUnit from a PCH file.
1280853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
12931b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \param Filename - The PCH file to load.
1300853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
1315262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
1325262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
13331b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
13431b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
1350853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
1365262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar                                  Diagnostic &Diags,
1375cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
1384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  bool UseBumpAllocator = false,
1394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
1404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  unsigned NumRemappedFiles = 0);
141521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
142521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
143521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
144521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
145521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
146807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// source file. The caller is responsible for ensuring the lifetime of the
147807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// invocation extends past that of the returned ASTUnit.
148521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
1495262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
1505262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
151521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
152521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
153521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
154521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI,
155521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar                                             Diagnostic &Diags,
15668ea2ac7fd98a5a44c7a5b04c54076cf794531cbDaniel Dunbar                                             bool OnlyLocalDecls = false);
157521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
1587b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
1597b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
1607b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1617b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
1627b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1637b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
1647b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
1655262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
1665262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
1677b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
168869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
1697b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
1707b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
1717b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
1727b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
1737b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
1747b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      Diagnostic &Diags,
175869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar                                      llvm::StringRef ResourceFilesPath,
1767b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
1774db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      bool UseBumpAllocator = false,
1784db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
1794db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      unsigned NumRemappedFiles = 0);
1800853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
1810853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1820853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
1830853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
1840853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
185