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