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