• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- ModuleMap.h - Describe the layout of modules -----------*- 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 ModuleMap interface, which describes the layout of a
11 // module as it relates to headers.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 
16 #ifndef LLVM_CLANG_LEX_MODULEMAP_H
17 #define LLVM_CLANG_LEX_MODULEMAP_H
18 
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/Module.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/StringRef.h"
27 #include <string>
28 
29 namespace clang {
30 
31 class DirectoryEntry;
32 class FileEntry;
33 class FileManager;
34 class DiagnosticConsumer;
35 class DiagnosticsEngine;
36 class HeaderSearch;
37 class ModuleMapParser;
38 
39 /// \brief A mechanism to observe the actions of the module map parser as it
40 /// reads module map files.
41 class ModuleMapCallbacks {
42 public:
~ModuleMapCallbacks()43   virtual ~ModuleMapCallbacks() {}
44 
45   /// \brief Called when a module map file has been read.
46   ///
47   /// \param FileStart A SourceLocation referring to the start of the file's
48   /// contents.
49   /// \param File The file itself.
50   /// \param IsSystem Whether this is a module map from a system include path.
moduleMapFileRead(SourceLocation FileStart,const FileEntry & File,bool IsSystem)51   virtual void moduleMapFileRead(SourceLocation FileStart,
52                                  const FileEntry &File, bool IsSystem) {}
53 };
54 
55 class ModuleMap {
56   SourceManager &SourceMgr;
57   DiagnosticsEngine &Diags;
58   const LangOptions &LangOpts;
59   const TargetInfo *Target;
60   HeaderSearch &HeaderInfo;
61 
62   llvm::SmallVector<std::unique_ptr<ModuleMapCallbacks>, 1> Callbacks;
63 
64   /// \brief The directory used for Clang-supplied, builtin include headers,
65   /// such as "stdint.h".
66   const DirectoryEntry *BuiltinIncludeDir;
67 
68   /// \brief Language options used to parse the module map itself.
69   ///
70   /// These are always simple C language options.
71   LangOptions MMapLangOpts;
72 
73   // The module that we are building; related to \c LangOptions::CurrentModule.
74   Module *CompilingModule;
75 
76 public:
77   // The module that the .cc source file is associated with.
78   Module *SourceModule;
79   std::string SourceModuleName;
80 
81 private:
82   /// \brief The top-level modules that are known.
83   llvm::StringMap<Module *> Modules;
84 
85   /// \brief The number of modules we have created in total.
86   unsigned NumCreatedModules;
87 
88 public:
89   /// \brief Flags describing the role of a module header.
90   enum ModuleHeaderRole {
91     /// \brief This header is normally included in the module.
92     NormalHeader  = 0x0,
93     /// \brief This header is included but private.
94     PrivateHeader = 0x1,
95     /// \brief This header is part of the module (for layering purposes) but
96     /// should be textually included.
97     TextualHeader = 0x2,
98     // Caution: Adding an enumerator needs other changes.
99     // Adjust the number of bits for KnownHeader::Storage.
100     // Adjust the bitfield HeaderFileInfo::HeaderRole size.
101     // Adjust the HeaderFileInfoTrait::ReadData streaming.
102     // Adjust the HeaderFileInfoTrait::EmitData streaming.
103     // Adjust ModuleMap::addHeader.
104   };
105 
106   /// \brief A header that is known to reside within a given module,
107   /// whether it was included or excluded.
108   class KnownHeader {
109     llvm::PointerIntPair<Module *, 2, ModuleHeaderRole> Storage;
110 
111   public:
KnownHeader()112     KnownHeader() : Storage(nullptr, NormalHeader) { }
KnownHeader(Module * M,ModuleHeaderRole Role)113     KnownHeader(Module *M, ModuleHeaderRole Role) : Storage(M, Role) { }
114 
115     friend bool operator==(const KnownHeader &A, const KnownHeader &B) {
116       return A.Storage == B.Storage;
117     }
118     friend bool operator!=(const KnownHeader &A, const KnownHeader &B) {
119       return A.Storage != B.Storage;
120     }
121 
122     /// \brief Retrieve the module the header is stored in.
getModule()123     Module *getModule() const { return Storage.getPointer(); }
124 
125     /// \brief The role of this header within the module.
getRole()126     ModuleHeaderRole getRole() const { return Storage.getInt(); }
127 
128     /// \brief Whether this header is available in the module.
isAvailable()129     bool isAvailable() const {
130       return getModule()->isAvailable();
131     }
132 
133     // \brief Whether this known header is valid (i.e., it has an
134     // associated module).
135     explicit operator bool() const {
136       return Storage.getPointer() != nullptr;
137     }
138   };
139 
140   typedef llvm::SmallPtrSet<const FileEntry *, 1> AdditionalModMapsSet;
141 
142 private:
143   typedef llvm::DenseMap<const FileEntry *, SmallVector<KnownHeader, 1> >
144   HeadersMap;
145 
146   /// \brief Mapping from each header to the module that owns the contents of
147   /// that header.
148   HeadersMap Headers;
149 
150   /// \brief Mapping from directories with umbrella headers to the module
151   /// that is generated from the umbrella header.
152   ///
153   /// This mapping is used to map headers that haven't explicitly been named
154   /// in the module map over to the module that includes them via its umbrella
155   /// header.
156   llvm::DenseMap<const DirectoryEntry *, Module *> UmbrellaDirs;
157 
158   /// \brief The set of attributes that can be attached to a module.
159   struct Attributes {
AttributesAttributes160     Attributes() : IsSystem(), IsExternC(), IsExhaustive() {}
161 
162     /// \brief Whether this is a system module.
163     unsigned IsSystem : 1;
164 
165     /// \brief Whether this is an extern "C" module.
166     unsigned IsExternC : 1;
167 
168     /// \brief Whether this is an exhaustive set of configuration macros.
169     unsigned IsExhaustive : 1;
170   };
171 
172   /// \brief A directory for which framework modules can be inferred.
173   struct InferredDirectory {
InferredDirectoryInferredDirectory174     InferredDirectory() : InferModules() {}
175 
176     /// \brief Whether to infer modules from this directory.
177     unsigned InferModules : 1;
178 
179     /// \brief The attributes to use for inferred modules.
180     Attributes Attrs;
181 
182     /// \brief If \c InferModules is non-zero, the module map file that allowed
183     /// inferred modules.  Otherwise, nullptr.
184     const FileEntry *ModuleMapFile;
185 
186     /// \brief The names of modules that cannot be inferred within this
187     /// directory.
188     SmallVector<std::string, 2> ExcludedModules;
189   };
190 
191   /// \brief A mapping from directories to information about inferring
192   /// framework modules from within those directories.
193   llvm::DenseMap<const DirectoryEntry *, InferredDirectory> InferredDirectories;
194 
195   /// A mapping from an inferred module to the module map that allowed the
196   /// inference.
197   llvm::DenseMap<const Module *, const FileEntry *> InferredModuleAllowedBy;
198 
199   llvm::DenseMap<const Module *, AdditionalModMapsSet> AdditionalModMaps;
200 
201   /// \brief Describes whether we haved parsed a particular file as a module
202   /// map.
203   llvm::DenseMap<const FileEntry *, bool> ParsedModuleMap;
204 
205   friend class ModuleMapParser;
206 
207   /// \brief Resolve the given export declaration into an actual export
208   /// declaration.
209   ///
210   /// \param Mod The module in which we're resolving the export declaration.
211   ///
212   /// \param Unresolved The export declaration to resolve.
213   ///
214   /// \param Complain Whether this routine should complain about unresolvable
215   /// exports.
216   ///
217   /// \returns The resolved export declaration, which will have a NULL pointer
218   /// if the export could not be resolved.
219   Module::ExportDecl
220   resolveExport(Module *Mod, const Module::UnresolvedExportDecl &Unresolved,
221                 bool Complain) const;
222 
223   /// \brief Resolve the given module id to an actual module.
224   ///
225   /// \param Id The module-id to resolve.
226   ///
227   /// \param Mod The module in which we're resolving the module-id.
228   ///
229   /// \param Complain Whether this routine should complain about unresolvable
230   /// module-ids.
231   ///
232   /// \returns The resolved module, or null if the module-id could not be
233   /// resolved.
234   Module *resolveModuleId(const ModuleId &Id, Module *Mod, bool Complain) const;
235 
236   /// \brief Looks up the modules that \p File corresponds to.
237   ///
238   /// If \p File represents a builtin header within Clang's builtin include
239   /// directory, this also loads all of the module maps to see if it will get
240   /// associated with a specific module (e.g. in /usr/include).
241   HeadersMap::iterator findKnownHeader(const FileEntry *File);
242 
243   /// \brief Searches for a module whose umbrella directory contains \p File.
244   ///
245   /// \param File The header to search for.
246   ///
247   /// \param IntermediateDirs On success, contains the set of directories
248   /// searched before finding \p File.
249   KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
250                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
251 
252   /// \brief Given that \p File is not in the Headers map, look it up within
253   /// umbrella directories and find or create a module for it.
254   KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File);
255 
256   /// \brief A convenience method to determine if \p File is (possibly nested)
257   /// in an umbrella directory.
isHeaderInUmbrellaDirs(const FileEntry * File)258   bool isHeaderInUmbrellaDirs(const FileEntry *File) {
259     SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
260     return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
261   }
262 
263   Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
264                                Attributes Attrs, Module *Parent);
265 
266 public:
267   /// \brief Construct a new module map.
268   ///
269   /// \param SourceMgr The source manager used to find module files and headers.
270   /// This source manager should be shared with the header-search mechanism,
271   /// since they will refer to the same headers.
272   ///
273   /// \param Diags A diagnostic engine used for diagnostics.
274   ///
275   /// \param LangOpts Language options for this translation unit.
276   ///
277   /// \param Target The target for this translation unit.
278   ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
279             const LangOptions &LangOpts, const TargetInfo *Target,
280             HeaderSearch &HeaderInfo);
281 
282   /// \brief Destroy the module map.
283   ///
284   ~ModuleMap();
285 
286   /// \brief Set the target information.
287   void setTarget(const TargetInfo &Target);
288 
289   /// \brief Set the directory that contains Clang-supplied include
290   /// files, such as our stdarg.h or tgmath.h.
setBuiltinIncludeDir(const DirectoryEntry * Dir)291   void setBuiltinIncludeDir(const DirectoryEntry *Dir) {
292     BuiltinIncludeDir = Dir;
293   }
294 
295   /// \brief Add a module map callback.
addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback)296   void addModuleMapCallbacks(std::unique_ptr<ModuleMapCallbacks> Callback) {
297     Callbacks.push_back(std::move(Callback));
298   }
299 
300   /// \brief Retrieve the module that owns the given header file, if any.
301   ///
302   /// \param File The header file that is likely to be included.
303   ///
304   /// \returns The module KnownHeader, which provides the module that owns the
305   /// given header file.  The KnownHeader is default constructed to indicate
306   /// that no module owns this header file.
307   KnownHeader findModuleForHeader(const FileEntry *File);
308 
309   /// \brief Retrieve all the modules that contain the given header file. This
310   /// may not include umbrella modules, nor information from external sources,
311   /// if they have not yet been inferred / loaded.
312   ///
313   /// Typically, \ref findModuleForHeader should be used instead, as it picks
314   /// the preferred module for the header.
315   ArrayRef<KnownHeader> findAllModulesForHeader(const FileEntry *File) const;
316 
317   /// \brief Reports errors if a module must not include a specific file.
318   ///
319   /// \param RequestingModule The module including a file.
320   ///
321   /// \param FilenameLoc The location of the inclusion's filename.
322   ///
323   /// \param Filename The included filename as written.
324   ///
325   /// \param File The included file.
326   void diagnoseHeaderInclusion(Module *RequestingModule,
327                                SourceLocation FilenameLoc, StringRef Filename,
328                                const FileEntry *File);
329 
330   /// \brief Determine whether the given header is part of a module
331   /// marked 'unavailable'.
332   bool isHeaderInUnavailableModule(const FileEntry *Header) const;
333 
334   /// \brief Determine whether the given header is unavailable as part
335   /// of the specified module.
336   bool isHeaderUnavailableInModule(const FileEntry *Header,
337                                    const Module *RequestingModule) const;
338 
339   /// \brief Retrieve a module with the given name.
340   ///
341   /// \param Name The name of the module to look up.
342   ///
343   /// \returns The named module, if known; otherwise, returns null.
344   Module *findModule(StringRef Name) const;
345 
346   /// \brief Retrieve a module with the given name using lexical name lookup,
347   /// starting at the given context.
348   ///
349   /// \param Name The name of the module to look up.
350   ///
351   /// \param Context The module context, from which we will perform lexical
352   /// name lookup.
353   ///
354   /// \returns The named module, if known; otherwise, returns null.
355   Module *lookupModuleUnqualified(StringRef Name, Module *Context) const;
356 
357   /// \brief Retrieve a module with the given name within the given context,
358   /// using direct (qualified) name lookup.
359   ///
360   /// \param Name The name of the module to look up.
361   ///
362   /// \param Context The module for which we will look for a submodule. If
363   /// null, we will look for a top-level module.
364   ///
365   /// \returns The named submodule, if known; otherwose, returns null.
366   Module *lookupModuleQualified(StringRef Name, Module *Context) const;
367 
368   /// \brief Find a new module or submodule, or create it if it does not already
369   /// exist.
370   ///
371   /// \param Name The name of the module to find or create.
372   ///
373   /// \param Parent The module that will act as the parent of this submodule,
374   /// or NULL to indicate that this is a top-level module.
375   ///
376   /// \param IsFramework Whether this is a framework module.
377   ///
378   /// \param IsExplicit Whether this is an explicit submodule.
379   ///
380   /// \returns The found or newly-created module, along with a boolean value
381   /// that will be true if the module is newly-created.
382   std::pair<Module *, bool> findOrCreateModule(StringRef Name, Module *Parent,
383                                                bool IsFramework,
384                                                bool IsExplicit);
385 
386   /// \brief Infer the contents of a framework module map from the given
387   /// framework directory.
388   Module *inferFrameworkModule(const DirectoryEntry *FrameworkDir,
389                                bool IsSystem, Module *Parent);
390 
391   /// \brief Retrieve the module map file containing the definition of the given
392   /// module.
393   ///
394   /// \param Module The module whose module map file will be returned, if known.
395   ///
396   /// \returns The file entry for the module map file containing the given
397   /// module, or NULL if the module definition was inferred.
398   const FileEntry *getContainingModuleMapFile(const Module *Module) const;
399 
400   /// \brief Get the module map file that (along with the module name) uniquely
401   /// identifies this module.
402   ///
403   /// The particular module that \c Name refers to may depend on how the module
404   /// was found in header search. However, the combination of \c Name and
405   /// this module map will be globally unique for top-level modules. In the case
406   /// of inferred modules, returns the module map that allowed the inference
407   /// (e.g. contained 'module *'). Otherwise, returns
408   /// getContainingModuleMapFile().
409   const FileEntry *getModuleMapFileForUniquing(const Module *M) const;
410 
411   void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap);
412 
413   /// \brief Get any module map files other than getModuleMapFileForUniquing(M)
414   /// that define submodules of a top-level module \p M. This is cheaper than
415   /// getting the module map file for each submodule individually, since the
416   /// expected number of results is very small.
getAdditionalModuleMapFiles(const Module * M)417   AdditionalModMapsSet *getAdditionalModuleMapFiles(const Module *M) {
418     auto I = AdditionalModMaps.find(M);
419     if (I == AdditionalModMaps.end())
420       return nullptr;
421     return &I->second;
422   }
423 
addAdditionalModuleMapFile(const Module * M,const FileEntry * ModuleMap)424   void addAdditionalModuleMapFile(const Module *M, const FileEntry *ModuleMap) {
425     AdditionalModMaps[M].insert(ModuleMap);
426   }
427 
428   /// \brief Resolve all of the unresolved exports in the given module.
429   ///
430   /// \param Mod The module whose exports should be resolved.
431   ///
432   /// \param Complain Whether to emit diagnostics for failures.
433   ///
434   /// \returns true if any errors were encountered while resolving exports,
435   /// false otherwise.
436   bool resolveExports(Module *Mod, bool Complain);
437 
438   /// \brief Resolve all of the unresolved uses in the given module.
439   ///
440   /// \param Mod The module whose uses should be resolved.
441   ///
442   /// \param Complain Whether to emit diagnostics for failures.
443   ///
444   /// \returns true if any errors were encountered while resolving uses,
445   /// false otherwise.
446   bool resolveUses(Module *Mod, bool Complain);
447 
448   /// \brief Resolve all of the unresolved conflicts in the given module.
449   ///
450   /// \param Mod The module whose conflicts should be resolved.
451   ///
452   /// \param Complain Whether to emit diagnostics for failures.
453   ///
454   /// \returns true if any errors were encountered while resolving conflicts,
455   /// false otherwise.
456   bool resolveConflicts(Module *Mod, bool Complain);
457 
458   /// \brief Infers the (sub)module based on the given source location and
459   /// source manager.
460   ///
461   /// \param Loc The location within the source that we are querying, along
462   /// with its source manager.
463   ///
464   /// \returns The module that owns this source location, or null if no
465   /// module owns this source location.
466   Module *inferModuleFromLocation(FullSourceLoc Loc);
467 
468   /// \brief Sets the umbrella header of the given module to the given
469   /// header.
470   void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
471                          Twine NameAsWritten);
472 
473   /// \brief Sets the umbrella directory of the given module to the given
474   /// directory.
475   void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
476                       Twine NameAsWritten);
477 
478   /// \brief Adds this header to the given module.
479   /// \param Role The role of the header wrt the module.
480   void addHeader(Module *Mod, Module::Header Header,
481                  ModuleHeaderRole Role, bool Imported = false);
482 
483   /// \brief Marks this header as being excluded from the given module.
484   void excludeHeader(Module *Mod, Module::Header Header);
485 
486   /// \brief Parse the given module map file, and record any modules we
487   /// encounter.
488   ///
489   /// \param File The file to be parsed.
490   ///
491   /// \param IsSystem Whether this module map file is in a system header
492   /// directory, and therefore should be considered a system module.
493   ///
494   /// \param HomeDir The directory in which relative paths within this module
495   ///        map file will be resolved.
496   ///
497   /// \param ExternModuleLoc The location of the "extern module" declaration
498   ///        that caused us to load this module map file, if any.
499   ///
500   /// \returns true if an error occurred, false otherwise.
501   bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
502                           const DirectoryEntry *HomeDir,
503                           SourceLocation ExternModuleLoc = SourceLocation());
504 
505   /// \brief Dump the contents of the module map, for debugging purposes.
506   void dump();
507 
508   typedef llvm::StringMap<Module *>::const_iterator module_iterator;
module_begin()509   module_iterator module_begin() const { return Modules.begin(); }
module_end()510   module_iterator module_end()   const { return Modules.end(); }
511 };
512 
513 }
514 #endif
515