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