clang API Documentation

PTHManager.h
Go to the documentation of this file.
00001 //===--- PTHManager.h - Manager object for PTH processing -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the PTHManager interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_PTHMANAGER_H
00015 #define LLVM_CLANG_PTHMANAGER_H
00016 
00017 #include "clang/Lex/PTHLexer.h"
00018 #include "clang/Basic/LangOptions.h"
00019 #include "clang/Basic/IdentifierTable.h"
00020 #include "clang/Basic/Diagnostic.h"
00021 #include "llvm/ADT/DenseMap.h"
00022 #include "llvm/Support/Allocator.h"
00023 #include <string>
00024 
00025 namespace llvm {
00026   class MemoryBuffer;
00027 }
00028 
00029 namespace clang {
00030 
00031 class FileEntry;
00032 class PTHLexer;
00033 class DiagnosticsEngine;
00034 class FileSystemStatCache;
00035 
00036 class PTHManager : public IdentifierInfoLookup {
00037   friend class PTHLexer;
00038 
00039   /// The memory mapped PTH file.
00040   const llvm::MemoryBuffer* Buf;
00041 
00042   /// Alloc - Allocator used for IdentifierInfo objects.
00043   llvm::BumpPtrAllocator Alloc;
00044 
00045   /// IdMap - A lazily generated cache mapping from persistent identifiers to
00046   ///  IdentifierInfo*.
00047   IdentifierInfo** PerIDCache;
00048 
00049   /// FileLookup - Abstract data structure used for mapping between files
00050   ///  and token data in the PTH file.
00051   void* FileLookup;
00052 
00053   /// IdDataTable - Array representing the mapping from persistent IDs to the
00054   ///  data offset within the PTH file containing the information to
00055   ///  reconsitute an IdentifierInfo.
00056   const unsigned char* const IdDataTable;
00057 
00058   /// SortedIdTable - Abstract data structure mapping from strings to
00059   ///  persistent IDs.  This is used by get().
00060   void* StringIdLookup;
00061 
00062   /// NumIds - The number of identifiers in the PTH file.
00063   const unsigned NumIds;
00064 
00065   /// PP - The Preprocessor object that will use this PTHManager to create
00066   ///  PTHLexer objects.
00067   Preprocessor* PP;
00068 
00069   /// SpellingBase - The base offset within the PTH memory buffer that
00070   ///  contains the cached spellings for literals.
00071   const unsigned char* const SpellingBase;
00072 
00073   /// OriginalSourceFile - A null-terminated C-string that specifies the name
00074   ///  if the file (if any) that was to used to generate the PTH cache.
00075   const char* OriginalSourceFile;
00076 
00077   /// This constructor is intended to only be called by the static 'Create'
00078   /// method.
00079   PTHManager(const llvm::MemoryBuffer* buf, void* fileLookup,
00080              const unsigned char* idDataTable, IdentifierInfo** perIDCache,
00081              void* stringIdLookup, unsigned numIds,
00082              const unsigned char* spellingBase, const char *originalSourceFile);
00083 
00084   // Do not implement.
00085   PTHManager();
00086   void operator=(const PTHManager&);
00087 
00088   /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
00089   ///  spelling for a token.
00090   unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
00091 
00092   /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the
00093   ///  PTH file.
00094   inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) {
00095     // Check if the IdentifierInfo has already been resolved.
00096     if (IdentifierInfo* II = PerIDCache[PersistentID])
00097       return II;
00098     return LazilyCreateIdentifierInfo(PersistentID);
00099   }
00100   IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID);
00101 
00102 public:
00103   // The current PTH version.
00104   enum { Version = 9 };
00105 
00106   ~PTHManager();
00107 
00108   /// getOriginalSourceFile - Return the full path to the original header
00109   ///  file name that was used to generate the PTH cache.
00110   const char* getOriginalSourceFile() const {
00111     return OriginalSourceFile;
00112   }
00113 
00114   /// get - Return the identifier token info for the specified named identifier.
00115   ///  Unlike the version in IdentifierTable, this returns a pointer instead
00116   ///  of a reference.  If the pointer is NULL then the IdentifierInfo cannot
00117   ///  be found.
00118   IdentifierInfo *get(StringRef Name);
00119 
00120   /// Create - This method creates PTHManager objects.  The 'file' argument
00121   ///  is the name of the PTH file.  This method returns NULL upon failure.
00122   static PTHManager *Create(const std::string& file, DiagnosticsEngine &Diags);
00123 
00124   void setPreprocessor(Preprocessor *pp) { PP = pp; }
00125 
00126   /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
00127   ///  specified file.  This method returns NULL if no cached tokens exist.
00128   ///  It is the responsibility of the caller to 'delete' the returned object.
00129   PTHLexer *CreateLexer(FileID FID);
00130 
00131   /// createStatCache - Returns a FileSystemStatCache object for use with
00132   ///  FileManager objects.  These objects use the PTH data to speed up
00133   ///  calls to stat by memoizing their results from when the PTH file
00134   ///  was generated.
00135   FileSystemStatCache *createStatCache();
00136 };
00137 
00138 }  // end namespace clang
00139 
00140 #endif