HeaderSearch.h revision 1ecf0e6e271f3046bc53264318c47eae0fb80afd
1//===--- HeaderSearch.h - Resolve Header File Locations ---------*- 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 HeaderSearch interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_LEX_HEADERSEARCH_H 15#define LLVM_CLANG_LEX_HEADERSEARCH_H 16 17#include "clang/Lex/DirectoryLookup.h" 18#include "clang/Lex/ModuleMap.h" 19#include "llvm/ADT/ArrayRef.h" 20#include "llvm/ADT/StringMap.h" 21#include "llvm/ADT/StringSet.h" 22#include "llvm/Support/Allocator.h" 23#include "llvm/ADT/OwningPtr.h" 24#include <vector> 25 26namespace clang { 27 28class DiagnosticsEngine; 29class ExternalIdentifierLookup; 30class FileEntry; 31class FileManager; 32class IdentifierInfo; 33 34/// \brief The preprocessor keeps track of this information for each 35/// file that is \#included. 36struct HeaderFileInfo { 37 /// \brief True if this is a \#import'd or \#pragma once file. 38 unsigned isImport : 1; 39 40 /// \brief True if this is a \#pragma once file. 41 unsigned isPragmaOnce : 1; 42 43 /// DirInfo - Keep track of whether this is a system header, and if so, 44 /// whether it is C++ clean or not. This can be set by the include paths or 45 /// by \#pragma gcc system_header. This is an instance of 46 /// SrcMgr::CharacteristicKind. 47 unsigned DirInfo : 2; 48 49 /// \brief Whether this header file info was supplied by an external source. 50 unsigned External : 1; 51 52 /// \brief Whether this structure is considered to already have been 53 /// "resolved", meaning that it was loaded from the external source. 54 unsigned Resolved : 1; 55 56 /// \brief Whether this is a header inside a framework that is currently 57 /// being built. 58 /// 59 /// When a framework is being built, the headers have not yet been placed 60 /// into the appropriate framework subdirectories, and therefore are 61 /// provided via a header map. This bit indicates when this is one of 62 /// those framework headers. 63 unsigned IndexHeaderMapHeader : 1; 64 65 /// \brief The number of times the file has been included already. 66 unsigned short NumIncludes; 67 68 /// \brief The ID number of the controlling macro. 69 /// 70 /// This ID number will be non-zero when there is a controlling 71 /// macro whose IdentifierInfo may not yet have been loaded from 72 /// external storage. 73 unsigned ControllingMacroID; 74 75 /// If this file has a \#ifndef XXX (or equivalent) guard that 76 /// protects the entire contents of the file, this is the identifier 77 /// for the macro that controls whether or not it has any effect. 78 /// 79 /// Note: Most clients should use getControllingMacro() to access 80 /// the controlling macro of this header, since 81 /// getControllingMacro() is able to load a controlling macro from 82 /// external storage. 83 const IdentifierInfo *ControllingMacro; 84 85 /// \brief If this header came from a framework include, this is the name 86 /// of the framework. 87 StringRef Framework; 88 89 HeaderFileInfo() 90 : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User), 91 External(false), Resolved(false), IndexHeaderMapHeader(false), 92 NumIncludes(0), ControllingMacroID(0), ControllingMacro(0) {} 93 94 /// \brief Retrieve the controlling macro for this header file, if 95 /// any. 96 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External); 97 98 /// \brief Determine whether this is a non-default header file info, e.g., 99 /// it corresponds to an actual header we've included or tried to include. 100 bool isNonDefault() const { 101 return isImport || isPragmaOnce || NumIncludes || ControllingMacro || 102 ControllingMacroID; 103 } 104}; 105 106/// \brief An external source of header file information, which may supply 107/// information about header files already included. 108class ExternalHeaderFileInfoSource { 109public: 110 virtual ~ExternalHeaderFileInfoSource(); 111 112 /// \brief Retrieve the header file information for the given file entry. 113 /// 114 /// \returns Header file information for the given file entry, with the 115 /// \c External bit set. If the file entry is not known, return a 116 /// default-constructed \c HeaderFileInfo. 117 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0; 118}; 119 120/// \brief Encapsulates the information needed to find the file referenced 121/// by a \#include or \#include_next, (sub-)framework lookup, etc. 122class HeaderSearch { 123 /// This structure is used to record entries in our framework cache. 124 struct FrameworkCacheEntry { 125 /// The directory entry which should be used for the cached framework. 126 const DirectoryEntry *Directory; 127 128 /// Whether this framework has been "user-specified" to be treated as if it 129 /// were a system framework (even if it was found outside a system framework 130 /// directory). 131 bool IsUserSpecifiedSystemFramework; 132 }; 133 134 FileManager &FileMgr; 135 /// \#include search path information. Requests for \#include "x" search the 136 /// directory of the \#including file first, then each directory in SearchDirs 137 /// consecutively. Requests for <x> search the current dir first, then each 138 /// directory in SearchDirs, starting at AngledDirIdx, consecutively. If 139 /// NoCurDirSearch is true, then the check for the file in the current 140 /// directory is suppressed. 141 std::vector<DirectoryLookup> SearchDirs; 142 unsigned AngledDirIdx; 143 unsigned SystemDirIdx; 144 bool NoCurDirSearch; 145 146 /// \brief \#include prefixes for which the 'system header' property is 147 /// overridden. 148 /// 149 /// For a \#include "x" or \#include \<x> directive, the last string in this 150 /// list which is a prefix of 'x' determines whether the file is treated as 151 /// a system header. 152 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes; 153 154 /// \brief The path to the module cache. 155 std::string ModuleCachePath; 156 157 /// \brief All of the preprocessor-specific data about files that are 158 /// included, indexed by the FileEntry's UID. 159 std::vector<HeaderFileInfo> FileInfo; 160 161 /// \brief Keeps track of each lookup performed by LookupFile. 162 /// 163 /// The first part of the value is the starting index in SearchDirs 164 /// that the cached search was performed from. If there is a hit and 165 /// this value doesn't match the current query, the cache has to be 166 /// ignored. The second value is the entry in SearchDirs that satisfied 167 /// the query. 168 llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator> 169 LookupFileCache; 170 171 /// \brief Collection mapping a framework or subframework 172 /// name like "Carbon" to the Carbon.framework directory. 173 llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap; 174 175 /// IncludeAliases - maps include file names (including the quotes or 176 /// angle brackets) to other include file names. This is used to support the 177 /// include_alias pragma for Microsoft compatibility. 178 typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator> 179 IncludeAliasMap; 180 OwningPtr<IncludeAliasMap> IncludeAliases; 181 182 /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing 183 /// headermaps. This vector owns the headermap. 184 std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; 185 186 /// \brief The mapping between modules and headers. 187 ModuleMap ModMap; 188 189 /// \brief Describes whether a given directory has a module map in it. 190 llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap; 191 192 /// \brief Uniqued set of framework names, which is used to track which 193 /// headers were included as framework headers. 194 llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames; 195 196 /// \brief Entity used to resolve the identifier IDs of controlling 197 /// macros into IdentifierInfo pointers, as needed. 198 ExternalIdentifierLookup *ExternalLookup; 199 200 /// \brief Entity used to look up stored header file information. 201 ExternalHeaderFileInfoSource *ExternalSource; 202 203 // Various statistics we track for performance analysis. 204 unsigned NumIncluded; 205 unsigned NumMultiIncludeFileOptzn; 206 unsigned NumFrameworkLookups, NumSubFrameworkLookups; 207 208 // HeaderSearch doesn't support default or copy construction. 209 HeaderSearch(const HeaderSearch&) LLVM_DELETED_FUNCTION; 210 void operator=(const HeaderSearch&) LLVM_DELETED_FUNCTION; 211 212 friend class DirectoryLookup; 213 214public: 215 HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags, 216 const LangOptions &LangOpts, const TargetInfo *Target); 217 ~HeaderSearch(); 218 219 FileManager &getFileMgr() const { return FileMgr; } 220 221 /// \brief Interface for setting the file search paths. 222 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 223 unsigned angledDirIdx, unsigned systemDirIdx, 224 bool noCurDirSearch) { 225 assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() && 226 "Directory indicies are unordered"); 227 SearchDirs = dirs; 228 AngledDirIdx = angledDirIdx; 229 SystemDirIdx = systemDirIdx; 230 NoCurDirSearch = noCurDirSearch; 231 //LookupFileCache.clear(); 232 } 233 234 /// \brief Add an additional search path. 235 void AddSearchPath(const DirectoryLookup &dir, bool isAngled) { 236 unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx; 237 SearchDirs.insert(SearchDirs.begin() + idx, dir); 238 if (!isAngled) 239 AngledDirIdx++; 240 SystemDirIdx++; 241 } 242 243 /// \brief Set the list of system header prefixes. 244 void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) { 245 SystemHeaderPrefixes.assign(P.begin(), P.end()); 246 } 247 248 /// \brief Checks whether the map exists or not. 249 bool HasIncludeAliasMap() const { 250 return IncludeAliases; 251 } 252 253 /// \brief Map the source include name to the dest include name. 254 /// 255 /// The Source should include the angle brackets or quotes, the dest 256 /// should not. This allows for distinction between <> and "" headers. 257 void AddIncludeAlias(StringRef Source, StringRef Dest) { 258 if (!IncludeAliases) 259 IncludeAliases.reset(new IncludeAliasMap); 260 (*IncludeAliases)[Source] = Dest; 261 } 262 263 /// MapHeaderToIncludeAlias - Maps one header file name to a different header 264 /// file name, for use with the include_alias pragma. Note that the source 265 /// file name should include the angle brackets or quotes. Returns StringRef 266 /// as null if the header cannot be mapped. 267 StringRef MapHeaderToIncludeAlias(StringRef Source) { 268 assert(IncludeAliases && "Trying to map headers when there's no map"); 269 270 // Do any filename replacements before anything else 271 IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source); 272 if (Iter != IncludeAliases->end()) 273 return Iter->second; 274 return StringRef(); 275 } 276 277 /// \brief Set the path to the module cache. 278 void setModuleCachePath(StringRef CachePath) { 279 ModuleCachePath = CachePath; 280 } 281 282 /// \brief Retrieve the path to the module cache. 283 StringRef getModuleCachePath() const { return ModuleCachePath; } 284 285 /// \brief Consider modules when including files from this directory. 286 void setDirectoryHasModuleMap(const DirectoryEntry* Dir) { 287 DirectoryHasModuleMap[Dir] = true; 288 } 289 290 /// \brief Forget everything we know about headers so far. 291 void ClearFileInfo() { 292 FileInfo.clear(); 293 } 294 295 void SetExternalLookup(ExternalIdentifierLookup *EIL) { 296 ExternalLookup = EIL; 297 } 298 299 ExternalIdentifierLookup *getExternalLookup() const { 300 return ExternalLookup; 301 } 302 303 /// \brief Set the external source of header information. 304 void SetExternalSource(ExternalHeaderFileInfoSource *ES) { 305 ExternalSource = ES; 306 } 307 308 /// \brief Set the target information for the header search, if not 309 /// already known. 310 void setTarget(const TargetInfo &Target); 311 312 /// \brief Given a "foo" or \<foo> reference, look up the indicated file, 313 /// return null on failure. 314 /// 315 /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member 316 /// the file was found in, or null if not applicable. 317 /// 318 /// \param isAngled indicates whether the file reference is a <> reference. 319 /// 320 /// \param CurDir If non-null, the file was found in the specified directory 321 /// search location. This is used to implement \#include_next. 322 /// 323 /// \param CurFileEnt If non-null, indicates where the \#including file is, in 324 /// case a relative search is needed. 325 /// 326 /// \param SearchPath If non-null, will be set to the search path relative 327 /// to which the file was found. If the include path is absolute, SearchPath 328 /// will be set to an empty string. 329 /// 330 /// \param RelativePath If non-null, will be set to the path relative to 331 /// SearchPath at which the file was found. This only differs from the 332 /// Filename for framework includes. 333 /// 334 /// \param SuggestedModule If non-null, and the file found is semantically 335 /// part of a known module, this will be set to the module that should 336 /// be imported instead of preprocessing/parsing the file found. 337 const FileEntry *LookupFile(StringRef Filename, bool isAngled, 338 const DirectoryLookup *FromDir, 339 const DirectoryLookup *&CurDir, 340 const FileEntry *CurFileEnt, 341 SmallVectorImpl<char> *SearchPath, 342 SmallVectorImpl<char> *RelativePath, 343 Module **SuggestedModule, 344 bool SkipCache = false); 345 346 /// \brief Look up a subframework for the specified \#include file. 347 /// 348 /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from 349 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if 350 /// HIToolbox is a subframework within Carbon.framework. If so, return 351 /// the FileEntry for the designated file, otherwise return null. 352 const FileEntry *LookupSubframeworkHeader( 353 StringRef Filename, 354 const FileEntry *RelativeFileEnt, 355 SmallVectorImpl<char> *SearchPath, 356 SmallVectorImpl<char> *RelativePath); 357 358 /// \brief Look up the specified framework name in our framework cache. 359 /// \returns The DirectoryEntry it is in if we know, null otherwise. 360 FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) { 361 return FrameworkMap.GetOrCreateValue(FWName).getValue(); 362 } 363 364 /// \brief Mark the specified file as a target of of a \#include, 365 /// \#include_next, or \#import directive. 366 /// 367 /// \return false if \#including the file will have no effect or true 368 /// if we should include it. 369 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 370 371 372 /// \brief Return whether the specified file is a normal header, 373 /// a system header, or a C++ friendly system header. 374 SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) { 375 return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo; 376 } 377 378 /// \brief Mark the specified file as a "once only" file, e.g. due to 379 /// \#pragma once. 380 void MarkFileIncludeOnce(const FileEntry *File) { 381 HeaderFileInfo &FI = getFileInfo(File); 382 FI.isImport = true; 383 FI.isPragmaOnce = true; 384 } 385 386 /// \brief Mark the specified file as a system header, e.g. due to 387 /// \#pragma GCC system_header. 388 void MarkFileSystemHeader(const FileEntry *File) { 389 getFileInfo(File).DirInfo = SrcMgr::C_System; 390 } 391 392 /// \brief Increment the count for the number of times the specified 393 /// FileEntry has been entered. 394 void IncrementIncludeCount(const FileEntry *File) { 395 ++getFileInfo(File).NumIncludes; 396 } 397 398 /// \brief Mark the specified file as having a controlling macro. 399 /// 400 /// This is used by the multiple-include optimization to eliminate 401 /// no-op \#includes. 402 void SetFileControllingMacro(const FileEntry *File, 403 const IdentifierInfo *ControllingMacro) { 404 getFileInfo(File).ControllingMacro = ControllingMacro; 405 } 406 407 /// \brief Determine whether this file is intended to be safe from 408 /// multiple inclusions, e.g., it has \#pragma once or a controlling 409 /// macro. 410 /// 411 /// This routine does not consider the effect of \#import 412 bool isFileMultipleIncludeGuarded(const FileEntry *File); 413 414 /// CreateHeaderMap - This method returns a HeaderMap for the specified 415 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. 416 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 417 418 /// \brief Retrieve the name of the module file that should be used to 419 /// load the given module. 420 /// 421 /// \param Module The module whose module file name will be returned. 422 /// 423 /// \returns The name of the module file that corresponds to this module, 424 /// or an empty string if this module does not correspond to any module file. 425 std::string getModuleFileName(Module *Module); 426 427 /// \brief Retrieve the name of the module file that should be used to 428 /// load a module with the given name. 429 /// 430 /// \param ModuleName The module whose module file name will be returned. 431 /// 432 /// \returns The name of the module file that corresponds to this module, 433 /// or an empty string if this module does not correspond to any module file. 434 std::string getModuleFileName(StringRef ModuleName); 435 436 /// \brief Lookup a module Search for a module with the given name. 437 /// 438 /// \param ModuleName The name of the module we're looking for. 439 /// 440 /// \param AllowSearch Whether we are allowed to search in the various 441 /// search directories to produce a module definition. If not, this lookup 442 /// will only return an already-known module. 443 /// 444 /// \returns The module with the given name. 445 Module *lookupModule(StringRef ModuleName, bool AllowSearch = true); 446 447 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 448 449 /// \brief Determine whether there is a module map that may map the header 450 /// with the given file name to a (sub)module. 451 /// 452 /// \param Filename The name of the file. 453 /// 454 /// \param Root The "root" directory, at which we should stop looking for 455 /// module maps. 456 bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root); 457 458 /// \brief Retrieve the module that corresponds to the given file, if any. 459 /// 460 /// \param File The header that we wish to map to a module. 461 Module *findModuleForHeader(const FileEntry *File); 462 463 /// \brief Read the contents of the given module map file. 464 /// 465 /// \param File The module map file. 466 /// 467 /// \returns true if an error occurred, false otherwise. 468 bool loadModuleMapFile(const FileEntry *File); 469 470 /// \brief Collect the set of all known, top-level modules. 471 /// 472 /// \param Modules Will be filled with the set of known, top-level modules. 473 void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules); 474 475private: 476 /// \brief Retrieve a module with the given name, which may be part of the 477 /// given framework. 478 /// 479 /// \param Name The name of the module to retrieve. 480 /// 481 /// \param Dir The framework directory (e.g., ModuleName.framework). 482 /// 483 /// \param IsSystem Whether the framework directory is part of the system 484 /// frameworks. 485 /// 486 /// \returns The module, if found; otherwise, null. 487 Module *loadFrameworkModule(StringRef Name, 488 const DirectoryEntry *Dir, 489 bool IsSystem); 490 491public: 492 /// \brief Retrieve the module map. 493 ModuleMap &getModuleMap() { return ModMap; } 494 495 unsigned header_file_size() const { return FileInfo.size(); } 496 497 // Used by ASTReader. 498 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID); 499 500 /// \brief Return the HeaderFileInfo structure for the specified FileEntry. 501 const HeaderFileInfo &getFileInfo(const FileEntry *FE) const { 502 return const_cast<HeaderSearch*>(this)->getFileInfo(FE); 503 } 504 505 // Used by external tools 506 typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator; 507 search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); } 508 search_dir_iterator search_dir_end() const { return SearchDirs.end(); } 509 unsigned search_dir_size() const { return SearchDirs.size(); } 510 511 search_dir_iterator quoted_dir_begin() const { 512 return SearchDirs.begin(); 513 } 514 search_dir_iterator quoted_dir_end() const { 515 return SearchDirs.begin() + AngledDirIdx; 516 } 517 518 search_dir_iterator angled_dir_begin() const { 519 return SearchDirs.begin() + AngledDirIdx; 520 } 521 search_dir_iterator angled_dir_end() const { 522 return SearchDirs.begin() + SystemDirIdx; 523 } 524 525 search_dir_iterator system_dir_begin() const { 526 return SearchDirs.begin() + SystemDirIdx; 527 } 528 search_dir_iterator system_dir_end() const { return SearchDirs.end(); } 529 530 /// \brief Retrieve a uniqued framework name. 531 StringRef getUniqueFrameworkName(StringRef Framework); 532 533 void PrintStats(); 534 535 size_t getTotalMemory() const; 536 537 static std::string NormalizeDashIncludePath(StringRef File, 538 FileManager &FileMgr); 539 540private: 541 /// \brief Describes what happened when we tried to load a module map file. 542 enum LoadModuleMapResult { 543 /// \brief The module map file had already been loaded. 544 LMM_AlreadyLoaded, 545 /// \brief The module map file was loaded by this invocation. 546 LMM_NewlyLoaded, 547 /// \brief There is was directory with the given name. 548 LMM_NoDirectory, 549 /// \brief There was either no module map file or the module map file was 550 /// invalid. 551 LMM_InvalidModuleMap 552 }; 553 554 /// \brief Try to load the module map file in the given directory. 555 /// 556 /// \param DirName The name of the directory where we will look for a module 557 /// map file. 558 /// 559 /// \returns The result of attempting to load the module map file from the 560 /// named directory. 561 LoadModuleMapResult loadModuleMapFile(StringRef DirName); 562 563 /// \brief Try to load the module map file in the given directory. 564 /// 565 /// \param Dir The directory where we will look for a module map file. 566 /// 567 /// \returns The result of attempting to load the module map file from the 568 /// named directory. 569 LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir); 570 571 /// \brief Return the HeaderFileInfo structure for the specified FileEntry. 572 HeaderFileInfo &getFileInfo(const FileEntry *FE); 573}; 574 575} // end namespace clang 576 577#endif 578