• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- GlobalModuleIndex.h - Global Module Index --------------*- 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 // This file defines the GlobalModuleIndex class, which manages a global index
11 // containing all of the identifiers known to the various modules within a given
12 // subdirectory of the module cache. It is used to improve the performance of
13 // queries such as "do any modules know about this identifier?"
14 //
15 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_SERIALIZATION_GLOBAL_MODULE_INDEX_H
17 #define LLVM_CLANG_SERIALIZATION_GLOBAL_MODULE_INDEX_H
18 
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include <memory>
25 #include <utility>
26 
27 namespace llvm {
28 class BitstreamCursor;
29 class MemoryBuffer;
30 }
31 
32 namespace clang {
33 
34 class DirectoryEntry;
35 class FileEntry;
36 class FileManager;
37 class IdentifierIterator;
38 
39 namespace serialization {
40   class ModuleFile;
41 }
42 
43 using llvm::SmallVector;
44 using llvm::SmallVectorImpl;
45 using llvm::StringRef;
46 using serialization::ModuleFile;
47 
48 /// \brief A global index for a set of module files, providing information about
49 /// the identifiers within those module files.
50 ///
51 /// The global index is an aid for name lookup into modules, offering a central
52 /// place where one can look for identifiers determine which
53 /// module files contain any information about that identifier. This
54 /// allows the client to restrict the search to only those module files known
55 /// to have a information about that identifier, improving performance. Moreover,
56 /// the global module index may know about module files that have not been
57 /// imported, and can be queried to determine which modules the current
58 /// translation could or should load to fix a problem.
59 class GlobalModuleIndex {
60   /// \brief Buffer containing the index file, which is lazily accessed so long
61   /// as the global module index is live.
62   std::unique_ptr<llvm::MemoryBuffer> Buffer;
63 
64   /// \brief The hash table.
65   ///
66   /// This pointer actually points to a IdentifierIndexTable object,
67   /// but that type is only accessible within the implementation of
68   /// GlobalModuleIndex.
69   void *IdentifierIndex;
70 
71   /// \brief Information about a given module file.
72   struct ModuleInfo {
ModuleInfoModuleInfo73     ModuleInfo() : File(), Size(), ModTime() { }
74 
75     /// \brief The module file, once it has been resolved.
76     ModuleFile *File;
77 
78     /// \brief The module file name.
79     std::string FileName;
80 
81     /// \brief Size of the module file at the time the global index was built.
82     off_t Size;
83 
84     /// \brief Modification time of the module file at the time the global
85     /// index was built.
86     time_t ModTime;
87 
88     /// \brief The module IDs on which this module directly depends.
89     /// FIXME: We don't really need a vector here.
90     llvm::SmallVector<unsigned, 4> Dependencies;
91   };
92 
93   /// \brief A mapping from module IDs to information about each module.
94   ///
95   /// This vector may have gaps, if module files have been removed or have
96   /// been updated since the index was built. A gap is indicated by an empty
97   /// file name.
98   llvm::SmallVector<ModuleInfo, 16> Modules;
99 
100   /// \brief Lazily-populated mapping from module files to their
101   /// corresponding index into the \c Modules vector.
102   llvm::DenseMap<ModuleFile *, unsigned> ModulesByFile;
103 
104   /// \brief The set of modules that have not yet been resolved.
105   ///
106   /// The string is just the name of the module itself, which maps to the
107   /// module ID.
108   llvm::StringMap<unsigned> UnresolvedModules;
109 
110   /// \brief The number of identifier lookups we performed.
111   unsigned NumIdentifierLookups;
112 
113   /// \brief The number of identifier lookup hits, where we recognize the
114   /// identifier.
115   unsigned NumIdentifierLookupHits;
116 
117   /// \brief Internal constructor. Use \c readIndex() to read an index.
118   explicit GlobalModuleIndex(llvm::MemoryBuffer *Buffer,
119                              llvm::BitstreamCursor Cursor);
120 
121   GlobalModuleIndex(const GlobalModuleIndex &) LLVM_DELETED_FUNCTION;
122   GlobalModuleIndex &operator=(const GlobalModuleIndex &) LLVM_DELETED_FUNCTION;
123 
124 public:
125   ~GlobalModuleIndex();
126 
127   /// \brief An error code returned when trying to read an index.
128   enum ErrorCode {
129     /// \brief No error occurred.
130     EC_None,
131     /// \brief No index was found.
132     EC_NotFound,
133     /// \brief Some other process is currently building the index; it is not
134     /// available yet.
135     EC_Building,
136     /// \brief There was an unspecified I/O error reading or writing the index.
137     EC_IOError
138   };
139 
140   /// \brief Read a global index file for the given directory.
141   ///
142   /// \param Path The path to the specific module cache where the module files
143   /// for the intended configuration reside.
144   ///
145   /// \returns A pair containing the global module index (if it exists) and
146   /// the error code.
147   static std::pair<GlobalModuleIndex *, ErrorCode>
148   readIndex(StringRef Path);
149 
150   /// \brief Returns an iterator for identifiers stored in the index table.
151   ///
152   /// The caller accepts ownership of the returned object.
153   IdentifierIterator *createIdentifierIterator() const;
154 
155   /// \brief Retrieve the set of modules that have up-to-date indexes.
156   ///
157   /// \param ModuleFiles Will be populated with the set of module files that
158   /// have been indexed.
159   void getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles);
160 
161   /// \brief Retrieve the set of module files on which the given module file
162   /// directly depends.
163   void getModuleDependencies(ModuleFile *File,
164                              SmallVectorImpl<ModuleFile *> &Dependencies);
165 
166   /// \brief A set of module files in which we found a result.
167   typedef llvm::SmallPtrSet<ModuleFile *, 4> HitSet;
168 
169   /// \brief Look for all of the module files with information about the given
170   /// identifier, e.g., a global function, variable, or type with that name.
171   ///
172   /// \param Name The identifier to look for.
173   ///
174   /// \param Hits Will be populated with the set of module files that have
175   /// information about this name.
176   ///
177   /// \returns true if the identifier is known to the index, false otherwise.
178   bool lookupIdentifier(StringRef Name, HitSet &Hits);
179 
180   /// \brief Note that the given module file has been loaded.
181   ///
182   /// \returns false if the global module index has information about this
183   /// module file, and true otherwise.
184   bool loadedModuleFile(ModuleFile *File);
185 
186   /// \brief Print statistics to standard error.
187   void printStats();
188 
189   /// \brief Print debugging view to standard error.
190   void dump();
191 
192   /// \brief Write a global index into the given
193   ///
194   /// \param FileMgr The file manager to use to load module files.
195   ///
196   /// \param Path The path to the directory containing module files, into
197   /// which the global index will be written.
198   static ErrorCode writeIndex(FileManager &FileMgr, StringRef Path);
199 };
200 
201 }
202 
203 #endif
204