clang API Documentation
00001 //===--- HeaderSearch.h - Resolve Header File Locations ---------*- 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 HeaderSearch interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H 00015 #define LLVM_CLANG_LEX_HEADERSEARCH_H 00016 00017 #include "clang/Lex/DirectoryLookup.h" 00018 #include "clang/Lex/ModuleMap.h" 00019 #include "llvm/ADT/StringMap.h" 00020 #include "llvm/ADT/StringSet.h" 00021 #include "llvm/Support/Allocator.h" 00022 #include "llvm/ADT/OwningPtr.h" 00023 #include <vector> 00024 00025 namespace clang { 00026 00027 class DiagnosticsEngine; 00028 class ExternalIdentifierLookup; 00029 class FileEntry; 00030 class FileManager; 00031 class IdentifierInfo; 00032 00033 /// HeaderFileInfo - The preprocessor keeps track of this information for each 00034 /// file that is #included. 00035 struct HeaderFileInfo { 00036 /// isImport - True if this is a #import'd or #pragma once file. 00037 unsigned isImport : 1; 00038 00039 /// isPragmaOnce - True if this is #pragma once file. 00040 unsigned isPragmaOnce : 1; 00041 00042 /// DirInfo - Keep track of whether this is a system header, and if so, 00043 /// whether it is C++ clean or not. This can be set by the include paths or 00044 /// by #pragma gcc system_header. This is an instance of 00045 /// SrcMgr::CharacteristicKind. 00046 unsigned DirInfo : 2; 00047 00048 /// \brief Whether this header file info was supplied by an external source. 00049 unsigned External : 1; 00050 00051 /// \brief Whether this structure is considered to already have been 00052 /// "resolved", meaning that it was loaded from the external source. 00053 unsigned Resolved : 1; 00054 00055 /// \brief Whether this is a header inside a framework that is currently 00056 /// being built. 00057 /// 00058 /// When a framework is being built, the headers have not yet been placed 00059 /// into the appropriate framework subdirectories, and therefore are 00060 /// provided via a header map. This bit indicates when this is one of 00061 /// those framework headers. 00062 unsigned IndexHeaderMapHeader : 1; 00063 00064 /// NumIncludes - This is the number of times the file has been included 00065 /// already. 00066 unsigned short NumIncludes; 00067 00068 /// \brief The ID number of the controlling macro. 00069 /// 00070 /// This ID number will be non-zero when there is a controlling 00071 /// macro whose IdentifierInfo may not yet have been loaded from 00072 /// external storage. 00073 unsigned ControllingMacroID; 00074 00075 /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard 00076 /// that protects the entire contents of the file, this is the identifier 00077 /// for the macro that controls whether or not it has any effect. 00078 /// 00079 /// Note: Most clients should use getControllingMacro() to access 00080 /// the controlling macro of this header, since 00081 /// getControllingMacro() is able to load a controlling macro from 00082 /// external storage. 00083 const IdentifierInfo *ControllingMacro; 00084 00085 /// \brief If this header came from a framework include, this is the name 00086 /// of the framework. 00087 StringRef Framework; 00088 00089 HeaderFileInfo() 00090 : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User), 00091 External(false), Resolved(false), IndexHeaderMapHeader(false), 00092 NumIncludes(0), ControllingMacroID(0), ControllingMacro(0) {} 00093 00094 /// \brief Retrieve the controlling macro for this header file, if 00095 /// any. 00096 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External); 00097 00098 /// \brief Determine whether this is a non-default header file info, e.g., 00099 /// it corresponds to an actual header we've included or tried to include. 00100 bool isNonDefault() const { 00101 return isImport || isPragmaOnce || NumIncludes || ControllingMacro || 00102 ControllingMacroID; 00103 } 00104 }; 00105 00106 /// \brief An external source of header file information, which may supply 00107 /// information about header files already included. 00108 class ExternalHeaderFileInfoSource { 00109 public: 00110 virtual ~ExternalHeaderFileInfoSource(); 00111 00112 /// \brief Retrieve the header file information for the given file entry. 00113 /// 00114 /// \returns Header file information for the given file entry, with the 00115 /// \c External bit set. If the file entry is not known, return a 00116 /// default-constructed \c HeaderFileInfo. 00117 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0; 00118 }; 00119 00120 /// HeaderSearch - This class encapsulates the information needed to find the 00121 /// file referenced by a #include or #include_next, (sub-)framework lookup, etc. 00122 class HeaderSearch { 00123 /// This structure is used to record entries in our framework cache. 00124 struct FrameworkCacheEntry { 00125 /// The directory entry which should be used for the cached framework. 00126 const DirectoryEntry *Directory; 00127 00128 /// Whether this framework has been "user-specified" to be treated as if it 00129 /// were a system framework (even if it was found outside a system framework 00130 /// directory). 00131 bool IsUserSpecifiedSystemFramework; 00132 }; 00133 00134 FileManager &FileMgr; 00135 DiagnosticsEngine &Diags; 00136 /// #include search path information. Requests for #include "x" search the 00137 /// directory of the #including file first, then each directory in SearchDirs 00138 /// consecutively. Requests for <x> search the current dir first, then each 00139 /// directory in SearchDirs, starting at AngledDirIdx, consecutively. If 00140 /// NoCurDirSearch is true, then the check for the file in the current 00141 /// directory is suppressed. 00142 std::vector<DirectoryLookup> SearchDirs; 00143 unsigned AngledDirIdx; 00144 unsigned SystemDirIdx; 00145 bool NoCurDirSearch; 00146 00147 /// \brief The path to the module cache. 00148 std::string ModuleCachePath; 00149 00150 /// FileInfo - This contains all of the preprocessor-specific data about files 00151 /// that are included. The vector is indexed by the FileEntry's UID. 00152 /// 00153 std::vector<HeaderFileInfo> FileInfo; 00154 00155 /// LookupFileCache - This is keeps track of each lookup performed by 00156 /// LookupFile. The first part of the value is the starting index in 00157 /// SearchDirs that the cached search was performed from. If there is a hit 00158 /// and this value doesn't match the current query, the cache has to be 00159 /// ignored. The second value is the entry in SearchDirs that satisfied the 00160 /// query. 00161 llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator> 00162 LookupFileCache; 00163 00164 /// FrameworkMap - This is a collection mapping a framework or subframework 00165 /// name like "Carbon" to the Carbon.framework directory. 00166 llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap; 00167 00168 /// IncludeAliases - maps include file names (including the quotes or 00169 /// angle brackets) to other include file names. This is used to support the 00170 /// include_alias pragma for Microsoft compatibility. 00171 typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator> 00172 IncludeAliasMap; 00173 OwningPtr<IncludeAliasMap> IncludeAliases; 00174 00175 /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing 00176 /// headermaps. This vector owns the headermap. 00177 std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; 00178 00179 /// \brief The mapping between modules and headers. 00180 ModuleMap ModMap; 00181 00182 /// \brief Describes whether a given directory has a module map in it. 00183 llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap; 00184 00185 /// \brief Uniqued set of framework names, which is used to track which 00186 /// headers were included as framework headers. 00187 llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames; 00188 00189 /// \brief Entity used to resolve the identifier IDs of controlling 00190 /// macros into IdentifierInfo pointers, as needed. 00191 ExternalIdentifierLookup *ExternalLookup; 00192 00193 /// \brief Entity used to look up stored header file information. 00194 ExternalHeaderFileInfoSource *ExternalSource; 00195 00196 // Various statistics we track for performance analysis. 00197 unsigned NumIncluded; 00198 unsigned NumMultiIncludeFileOptzn; 00199 unsigned NumFrameworkLookups, NumSubFrameworkLookups; 00200 00201 // HeaderSearch doesn't support default or copy construction. 00202 explicit HeaderSearch(); 00203 explicit HeaderSearch(const HeaderSearch&); 00204 void operator=(const HeaderSearch&); 00205 00206 friend class DirectoryLookup; 00207 00208 public: 00209 HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags, 00210 const LangOptions &LangOpts, const TargetInfo *Target); 00211 ~HeaderSearch(); 00212 00213 FileManager &getFileMgr() const { return FileMgr; } 00214 00215 /// SetSearchPaths - Interface for setting the file search paths. 00216 /// 00217 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 00218 unsigned angledDirIdx, unsigned systemDirIdx, 00219 bool noCurDirSearch) { 00220 assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() && 00221 "Directory indicies are unordered"); 00222 SearchDirs = dirs; 00223 AngledDirIdx = angledDirIdx; 00224 SystemDirIdx = systemDirIdx; 00225 NoCurDirSearch = noCurDirSearch; 00226 //LookupFileCache.clear(); 00227 } 00228 00229 /// AddSearchPath - Add an additional search path. 00230 void AddSearchPath(const DirectoryLookup &dir, bool isAngled) { 00231 unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx; 00232 SearchDirs.insert(SearchDirs.begin() + idx, dir); 00233 if (!isAngled) 00234 AngledDirIdx++; 00235 SystemDirIdx++; 00236 } 00237 00238 /// HasIncludeAliasMap - Checks whether the map exists or not 00239 bool HasIncludeAliasMap() const { 00240 return IncludeAliases; 00241 } 00242 00243 /// AddIncludeAlias - Map the source include name to the dest include name. 00244 /// The Source should include the angle brackets or quotes, the dest 00245 /// should not. This allows for distinction between <> and "" headers. 00246 void AddIncludeAlias(StringRef Source, StringRef Dest) { 00247 if (!IncludeAliases) 00248 IncludeAliases.reset(new IncludeAliasMap); 00249 (*IncludeAliases)[Source] = Dest; 00250 } 00251 00252 /// MapHeaderToIncludeAlias - Maps one header file name to a different header 00253 /// file name, for use with the include_alias pragma. Note that the source 00254 /// file name should include the angle brackets or quotes. Returns StringRef 00255 /// as null if the header cannot be mapped. 00256 StringRef MapHeaderToIncludeAlias(StringRef Source) { 00257 assert(IncludeAliases && "Trying to map headers when there's no map"); 00258 00259 // Do any filename replacements before anything else 00260 IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source); 00261 if (Iter != IncludeAliases->end()) 00262 return Iter->second; 00263 return StringRef(); 00264 } 00265 00266 /// \brief Set the path to the module cache. 00267 void setModuleCachePath(StringRef CachePath) { 00268 ModuleCachePath = CachePath; 00269 } 00270 00271 /// \brief Retrieve the path to the module cache. 00272 StringRef getModuleCachePath() const { return ModuleCachePath; } 00273 00274 /// ClearFileInfo - Forget everything we know about headers so far. 00275 void ClearFileInfo() { 00276 FileInfo.clear(); 00277 } 00278 00279 void SetExternalLookup(ExternalIdentifierLookup *EIL) { 00280 ExternalLookup = EIL; 00281 } 00282 00283 ExternalIdentifierLookup *getExternalLookup() const { 00284 return ExternalLookup; 00285 } 00286 00287 /// \brief Set the external source of header information. 00288 void SetExternalSource(ExternalHeaderFileInfoSource *ES) { 00289 ExternalSource = ES; 00290 } 00291 00292 /// \brief Set the target information for the header search, if not 00293 /// already known. 00294 void setTarget(const TargetInfo &Target); 00295 00296 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 00297 /// return null on failure. 00298 /// 00299 /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member 00300 /// the file was found in, or null if not applicable. 00301 /// 00302 /// \param isAngled indicates whether the file reference is a <> reference. 00303 /// 00304 /// \param CurDir If non-null, the file was found in the specified directory 00305 /// search location. This is used to implement #include_next. 00306 /// 00307 /// \param CurFileEnt If non-null, indicates where the #including file is, in 00308 /// case a relative search is needed. 00309 /// 00310 /// \param SearchPath If non-null, will be set to the search path relative 00311 /// to which the file was found. If the include path is absolute, SearchPath 00312 /// will be set to an empty string. 00313 /// 00314 /// \param RelativePath If non-null, will be set to the path relative to 00315 /// SearchPath at which the file was found. This only differs from the 00316 /// Filename for framework includes. 00317 /// 00318 /// \param SuggestedModule If non-null, and the file found is semantically 00319 /// part of a known module, this will be set to the module that should 00320 /// be imported instead of preprocessing/parsing the file found. 00321 const FileEntry *LookupFile(StringRef Filename, bool isAngled, 00322 const DirectoryLookup *FromDir, 00323 const DirectoryLookup *&CurDir, 00324 const FileEntry *CurFileEnt, 00325 SmallVectorImpl<char> *SearchPath, 00326 SmallVectorImpl<char> *RelativePath, 00327 Module **SuggestedModule, 00328 bool SkipCache = false); 00329 00330 /// LookupSubframeworkHeader - Look up a subframework for the specified 00331 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 00332 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 00333 /// is a subframework within Carbon.framework. If so, return the FileEntry 00334 /// for the designated file, otherwise return null. 00335 const FileEntry *LookupSubframeworkHeader( 00336 StringRef Filename, 00337 const FileEntry *RelativeFileEnt, 00338 SmallVectorImpl<char> *SearchPath, 00339 SmallVectorImpl<char> *RelativePath); 00340 00341 /// LookupFrameworkCache - Look up the specified framework name in our 00342 /// framework cache, returning the DirectoryEntry it is in if we know, 00343 /// otherwise, return null. 00344 FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) { 00345 return FrameworkMap.GetOrCreateValue(FWName).getValue(); 00346 } 00347 00348 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 00349 /// #include, #include_next, or #import directive. Return false if #including 00350 /// the file will have no effect or true if we should include it. 00351 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 00352 00353 00354 /// getFileDirFlavor - Return whether the specified file is a normal header, 00355 /// a system header, or a C++ friendly system header. 00356 SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) { 00357 return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo; 00358 } 00359 00360 /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g. 00361 /// due to #pragma once. 00362 void MarkFileIncludeOnce(const FileEntry *File) { 00363 HeaderFileInfo &FI = getFileInfo(File); 00364 FI.isImport = true; 00365 FI.isPragmaOnce = true; 00366 } 00367 00368 /// MarkFileSystemHeader - Mark the specified file as a system header, e.g. 00369 /// due to #pragma GCC system_header. 00370 void MarkFileSystemHeader(const FileEntry *File) { 00371 getFileInfo(File).DirInfo = SrcMgr::C_System; 00372 } 00373 00374 /// IncrementIncludeCount - Increment the count for the number of times the 00375 /// specified FileEntry has been entered. 00376 void IncrementIncludeCount(const FileEntry *File) { 00377 ++getFileInfo(File).NumIncludes; 00378 } 00379 00380 /// SetFileControllingMacro - Mark the specified file as having a controlling 00381 /// macro. This is used by the multiple-include optimization to eliminate 00382 /// no-op #includes. 00383 void SetFileControllingMacro(const FileEntry *File, 00384 const IdentifierInfo *ControllingMacro) { 00385 getFileInfo(File).ControllingMacro = ControllingMacro; 00386 } 00387 00388 /// \brief Determine whether this file is intended to be safe from 00389 /// multiple inclusions, e.g., it has #pragma once or a controlling 00390 /// macro. 00391 /// 00392 /// This routine does not consider the effect of #import 00393 bool isFileMultipleIncludeGuarded(const FileEntry *File); 00394 00395 /// CreateHeaderMap - This method returns a HeaderMap for the specified 00396 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 00397 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 00398 00399 /// \brief Retrieve the name of the module file that should be used to 00400 /// load the given module. 00401 /// 00402 /// \param Module The module whose module file name will be returned. 00403 /// 00404 /// \returns The name of the module file that corresponds to this module, 00405 /// or an empty string if this module does not correspond to any module file. 00406 std::string getModuleFileName(Module *Module); 00407 00408 /// \brief Retrieve the name of the module file that should be used to 00409 /// load a module with the given name. 00410 /// 00411 /// \param Module The module whose module file name will be returned. 00412 /// 00413 /// \returns The name of the module file that corresponds to this module, 00414 /// or an empty string if this module does not correspond to any module file. 00415 std::string getModuleFileName(StringRef ModuleName); 00416 00417 /// \brief Lookup a module Search for a module with the given name. 00418 /// 00419 /// \param ModuleName The name of the module we're looking for. 00420 /// 00421 /// \param AllowSearch Whether we are allowed to search in the various 00422 /// search directories to produce a module definition. If not, this lookup 00423 /// will only return an already-known module. 00424 /// 00425 /// \returns The module with the given name. 00426 Module *lookupModule(StringRef ModuleName, bool AllowSearch = true); 00427 00428 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 00429 00430 /// \brief Determine whether there is a module map that may map the header 00431 /// with the given file name to a (sub)module. 00432 /// 00433 /// \param Filename The name of the file. 00434 /// 00435 /// \param Root The "root" directory, at which we should stop looking for 00436 /// module maps. 00437 bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root); 00438 00439 /// \brief Retrieve the module that corresponds to the given file, if any. 00440 /// 00441 /// \param File The header that we wish to map to a module. 00442 Module *findModuleForHeader(const FileEntry *File); 00443 00444 /// \brief Read the contents of the given module map file. 00445 /// 00446 /// \param File The module map file. 00447 /// 00448 /// \param OnlyModule If non-NULL, this will receive the 00449 /// 00450 /// \returns true if an error occurred, false otherwise. 00451 bool loadModuleMapFile(const FileEntry *File); 00452 00453 /// \brief Collect the set of all known, top-level modules. 00454 /// 00455 /// \param Modules Will be filled with the set of known, top-level modules. 00456 void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules); 00457 00458 private: 00459 /// \brief Retrieve a module with the given name, which may be part of the 00460 /// given framework. 00461 /// 00462 /// \param Name The name of the module to retrieve. 00463 /// 00464 /// \param Dir The framework directory (e.g., ModuleName.framework). 00465 /// 00466 /// \param IsSystem Whether the framework directory is part of the system 00467 /// frameworks. 00468 /// 00469 /// \returns The module, if found; otherwise, null. 00470 Module *loadFrameworkModule(StringRef Name, 00471 const DirectoryEntry *Dir, 00472 bool IsSystem); 00473 00474 public: 00475 /// \brief Retrieve the module map. 00476 ModuleMap &getModuleMap() { return ModMap; } 00477 00478 unsigned header_file_size() const { return FileInfo.size(); } 00479 00480 // Used by ASTReader. 00481 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID); 00482 00483 /// getFileInfo - Return the HeaderFileInfo structure for the specified 00484 /// FileEntry. 00485 const HeaderFileInfo &getFileInfo(const FileEntry *FE) const { 00486 return const_cast<HeaderSearch*>(this)->getFileInfo(FE); 00487 } 00488 00489 // Used by external tools 00490 typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator; 00491 search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); } 00492 search_dir_iterator search_dir_end() const { return SearchDirs.end(); } 00493 unsigned search_dir_size() const { return SearchDirs.size(); } 00494 00495 search_dir_iterator quoted_dir_begin() const { 00496 return SearchDirs.begin(); 00497 } 00498 search_dir_iterator quoted_dir_end() const { 00499 return SearchDirs.begin() + AngledDirIdx; 00500 } 00501 00502 search_dir_iterator angled_dir_begin() const { 00503 return SearchDirs.begin() + AngledDirIdx; 00504 } 00505 search_dir_iterator angled_dir_end() const { 00506 return SearchDirs.begin() + SystemDirIdx; 00507 } 00508 00509 search_dir_iterator system_dir_begin() const { 00510 return SearchDirs.begin() + SystemDirIdx; 00511 } 00512 search_dir_iterator system_dir_end() const { return SearchDirs.end(); } 00513 00514 /// \brief Retrieve a uniqued framework name. 00515 StringRef getUniqueFrameworkName(StringRef Framework); 00516 00517 void PrintStats(); 00518 00519 size_t getTotalMemory() const; 00520 00521 static std::string NormalizeDashIncludePath(StringRef File, 00522 FileManager &FileMgr); 00523 00524 private: 00525 /// \brief Describes what happened when we tried to load a module map file. 00526 enum LoadModuleMapResult { 00527 /// \brief The module map file had already been loaded. 00528 LMM_AlreadyLoaded, 00529 /// \brief The module map file was loaded by this invocation. 00530 LMM_NewlyLoaded, 00531 /// \brief There is was directory with the given name. 00532 LMM_NoDirectory, 00533 /// \brief There was either no module map file or the module map file was 00534 /// invalid. 00535 LMM_InvalidModuleMap 00536 }; 00537 00538 /// \brief Try to load the module map file in the given directory. 00539 /// 00540 /// \param DirName The name of the directory where we will look for a module 00541 /// map file. 00542 /// 00543 /// \returns The result of attempting to load the module map file from the 00544 /// named directory. 00545 LoadModuleMapResult loadModuleMapFile(StringRef DirName); 00546 00547 /// \brief Try to load the module map file in the given directory. 00548 /// 00549 /// \param Dir The directory where we will look for a module map file. 00550 /// 00551 /// \returns The result of attempting to load the module map file from the 00552 /// named directory. 00553 LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir); 00554 00555 /// getFileInfo - Return the HeaderFileInfo structure for the specified 00556 /// FileEntry. 00557 HeaderFileInfo &getFileInfo(const FileEntry *FE); 00558 }; 00559 00560 } // end namespace clang 00561 00562 #endif