zcov: / include/clang/Frontend/ASTUnit.h


Files: 1 Branches Taken: 50.0% 1 / 2
Generated: 2010-02-10 01:31 Branches Executed: 100.0% 2 / 2
Line Coverage: 100.0% 10 / 10


Programs: 8 Runs 9296


       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/Basic/FileManager.h"
      20                 : #include "clang/Index/ASTLocation.h"
      21                 : #include <string>
      22                 : #include <vector>
      23                 : #include <cassert>
      24                 : #include <utility>
      25                 : 
      26                 : namespace llvm {
      27                 :   class MemoryBuffer;
      28                 : }
      29                 : 
      30                 : namespace clang {
      31                 : class ASTContext;
      32                 : class CompilerInvocation;
      33                 : class Decl;
      34                 : class Diagnostic;
      35                 : class FileEntry;
      36                 : class FileManager;
      37                 : class HeaderSearch;
      38                 : class Preprocessor;
      39                 : class SourceManager;
      40                 : class TargetInfo;
      41                 : 
      42                 : using namespace idx;
      43                 : 
      44                 : /// \brief Utility class for loading a ASTContext from a PCH file.
      45                 : ///
      46                 : class ASTUnit {
      47                 :   FileManager FileMgr;
      48                 : 
      49                 :   SourceManager                     SourceMgr;
      50                 :   llvm::OwningPtr<HeaderSearch>     HeaderInfo;
      51                 :   llvm::OwningPtr<TargetInfo>       Target;
      52                 :   llvm::OwningPtr<Preprocessor>     PP;
      53                 :   llvm::OwningPtr<ASTContext>       Ctx;
      54                 :   bool                              tempFile;
      55                 : 
      56                 :   /// Optional owned invocation, just used to make the invocation used in
      57                 :   /// LoadFromCommandLine available.
      58                 :   llvm::OwningPtr<CompilerInvocation> Invocation;
      59                 : 
      60                 :   // OnlyLocalDecls - when true, walking this AST should only visit declarations
      61                 :   // that come from the AST itself, not from included precompiled headers.
      62                 :   // FIXME: This is temporary; eventually, CIndex will always do this.
      63                 :   bool                              OnlyLocalDecls;
      64                 : 
      65                 :   /// Track whether the main file was loaded from an AST or not.
      66                 :   bool MainFileIsAST;
      67                 : 
      68                 :   /// Track the top-level decls which appeared in an ASTUnit which was loaded
      69                 :   /// from a source file.
      70                 :   //
      71                 :   // FIXME: This is just an optimization hack to avoid deserializing large parts
      72                 :   // of a PCH file when using the Index library on an ASTUnit loaded from
      73                 :   // source. In the long term we should make the Index library use efficient and
      74                 :   // more scalable search mechanisms.
      75                 :   std::vector<Decl*> TopLevelDecls;
      76                 : 
      77                 :   /// The name of the original source file used to generate this ASTUnit.
      78                 :   std::string OriginalSourceFile;
      79                 : 
      80                 :   // Critical optimization when using clang_getCursor().
      81                 :   ASTLocation LastLoc;
      82                 : 
      83                 :   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
      84                 :   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
      85                 : 
      86                 : public:
      87                 :   ASTUnit(bool MainFileIsAST);
      88                 :   ~ASTUnit();
      89                 : 
      90            12865:   bool isMainFileAST() const { return MainFileIsAST; }
      91                 : 
      92                 :   const SourceManager &getSourceManager() const { return SourceMgr; }
      93           135835:         SourceManager &getSourceManager()       { return SourceMgr; }
      94                 : 
      95                 :   const Preprocessor &getPreprocessor() const { return *PP.get(); }
      96           135725:         Preprocessor &getPreprocessor()       { return *PP.get(); }
      97                 : 
      98                 :   const ASTContext &getASTContext() const { return *Ctx.get(); }
      99            28512:         ASTContext &getASTContext()       { return *Ctx.get(); }
     100                 : 
     101                 :   const FileManager &getFileManager() const { return FileMgr; }
     102               48:         FileManager &getFileManager()       { return FileMgr; }
     103                 : 
     104                 :   const std::string &getOriginalSourceFileName();
     105                 :   const std::string &getPCHFileName();
     106                 : 
     107                1:   void unlinkTemporaryFile() { tempFile = true; }
     108                 : 
     109               19:   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
     110                 : 
     111                 :   void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
     112                 :   ASTLocation getLastASTLocation() const { return LastLoc; }
     113                 : 
     114               24:   std::vector<Decl*> &getTopLevelDecls() {
                       24: branch 1 taken
                        0: branch 2 not taken
     115               24:     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
     116               24:     return TopLevelDecls;
     117                 :   }
     118                 :   const std::vector<Decl*> &getTopLevelDecls() const {
     119                 :     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
     120                 :     return TopLevelDecls;
     121                 :   }
     122                 : 
     123                 :   /// \brief A mapping from a file name to the memory buffer that stores the
     124                 :   /// remapped contents of that file.
     125                 :   typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
     126                 :   
     127                 :   /// \brief Create a ASTUnit from a PCH file.
     128                 :   ///
     129                 :   /// \param Filename - The PCH file to load.
     130                 :   ///
     131                 :   /// \param Diags - The diagnostics engine to use for reporting errors; its
     132                 :   /// lifetime is expected to extend past that of the returned ASTUnit.
     133                 :   ///
     134                 :   /// \returns - The initialized ASTUnit or null if the PCH failed to load.
     135                 :   static ASTUnit *LoadFromPCHFile(const std::string &Filename,
     136                 :                                   Diagnostic &Diags,
     137                 :                                   bool OnlyLocalDecls = false,
     138                 :                                   bool UseBumpAllocator = false,
     139                 :                                   RemappedFile *RemappedFiles = 0,
     140                 :                                   unsigned NumRemappedFiles = 0);
     141                 : 
     142                 :   /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
     143                 :   /// CompilerInvocation object.
     144                 :   ///
     145                 :   /// \param CI - The compiler invocation to use; it must have exactly one input
     146                 :   /// source file. The caller is responsible for ensuring the lifetime of the
     147                 :   /// invocation extends past that of the returned ASTUnit.
     148                 :   ///
     149                 :   /// \param Diags - The diagnostics engine to use for reporting errors; its
     150                 :   /// lifetime is expected to extend past that of the returned ASTUnit.
     151                 :   //
     152                 :   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
     153                 :   // shouldn't need to specify them at construction time.
     154                 :   static ASTUnit *LoadFromCompilerInvocation(const CompilerInvocation &CI,
     155                 :                                              Diagnostic &Diags,
     156                 :                                              bool OnlyLocalDecls = false);
     157                 : 
     158                 :   /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
     159                 :   /// arguments, which must specify exactly one source file.
     160                 :   ///
     161                 :   /// \param ArgBegin - The beginning of the argument vector.
     162                 :   ///
     163                 :   /// \param ArgEnd - The end of the argument vector.
     164                 :   ///
     165                 :   /// \param Diags - The diagnostics engine to use for reporting errors; its
     166                 :   /// lifetime is expected to extend past that of the returned ASTUnit.
     167                 :   ///
     168                 :   /// \param ResourceFilesPath - The path to the compiler resource files.
     169                 :   //
     170                 :   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
     171                 :   // shouldn't need to specify them at construction time.
     172                 :   static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
     173                 :                                       const char **ArgEnd,
     174                 :                                       Diagnostic &Diags,
     175                 :                                       llvm::StringRef ResourceFilesPath,
     176                 :                                       bool OnlyLocalDecls = false,
     177                 :                                       bool UseBumpAllocator = false,
     178                 :                                       RemappedFile *RemappedFiles = 0,
     179                 :                                       unsigned NumRemappedFiles = 0);
     180                 : };
     181                 : 
     182                 : } // namespace clang
     183                 : 
     184                 : #endif

Generated: 2010-02-10 01:31 by zcov