1 //===--- HeaderSearch.h - Resolve Header File Locations ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the HeaderSearch interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H
15 #define LLVM_CLANG_LEX_HEADERSEARCH_H
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"
28 class DiagnosticsEngine;
29 class ExternalIdentifierLookup;
34 /// \brief The preprocessor keeps track of this information for each
35 /// file that is \#included.
36 struct HeaderFileInfo {
37 /// \brief True if this is a \#import'd or \#pragma once file.
38 unsigned isImport : 1;
40 /// \brief True if this is a \#pragma once file.
41 unsigned isPragmaOnce : 1;
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.
49 /// \brief Whether this header file info was supplied by an external source.
50 unsigned External : 1;
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;
56 /// \brief Whether this is a header inside a framework that is currently
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;
65 /// \brief The number of times the file has been included already.
66 unsigned short NumIncludes;
68 /// \brief The ID number of the controlling macro.
70 /// This ID number will be non-zero when there is a controlling
71 /// macro whose IdentifierInfo may not yet have been loaded from
73 unsigned ControllingMacroID;
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.
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
83 const IdentifierInfo *ControllingMacro;
85 /// \brief If this header came from a framework include, this is the name
90 : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
91 External(false), Resolved(false), IndexHeaderMapHeader(false),
92 NumIncludes(0), ControllingMacroID(0), ControllingMacro(0) {}
94 /// \brief Retrieve the controlling macro for this header file, if
96 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
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 ||
106 /// \brief An external source of header file information, which may supply
107 /// information about header files already included.
108 class ExternalHeaderFileInfoSource {
110 virtual ~ExternalHeaderFileInfoSource();
112 /// \brief Retrieve the header file information for the given file entry.
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;
120 /// \brief Encapsulates the information needed to find the file referenced
121 /// by a \#include or \#include_next, (sub-)framework lookup, etc.
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;
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
131 bool IsUserSpecifiedSystemFramework;
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;
147 /// \brief \#include prefixes for which the 'system header' property is
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
153 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
155 /// \brief The path to the module cache.
156 std::string ModuleCachePath;
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;
162 /// \brief Keeps track of each lookup performed by LookupFile.
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
169 llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator>
172 /// \brief Collection mapping a framework or subframework
173 /// name like "Carbon" to the Carbon.framework directory.
174 llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
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>
181 OwningPtr<IncludeAliasMap> IncludeAliases;
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;
187 /// \brief The mapping between modules and headers.
190 /// \brief Describes whether a given directory has a module map in it.
191 llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
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;
197 /// \brief Entity used to resolve the identifier IDs of controlling
198 /// macros into IdentifierInfo pointers, as needed.
199 ExternalIdentifierLookup *ExternalLookup;
201 /// \brief Entity used to look up stored header file information.
202 ExternalHeaderFileInfoSource *ExternalSource;
204 // Various statistics we track for performance analysis.
205 unsigned NumIncluded;
206 unsigned NumMultiIncludeFileOptzn;
207 unsigned NumFrameworkLookups, NumSubFrameworkLookups;
209 // HeaderSearch doesn't support default or copy construction.
210 explicit HeaderSearch();
211 explicit HeaderSearch(const HeaderSearch&);
212 void operator=(const HeaderSearch&);
214 friend class DirectoryLookup;
217 HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags,
218 const LangOptions &LangOpts, const TargetInfo *Target);
221 FileManager &getFileMgr() const { return FileMgr; }
223 /// \brief Interface for setting the file search paths.
224 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
225 unsigned angledDirIdx, unsigned systemDirIdx,
226 bool noCurDirSearch) {
227 assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
228 "Directory indicies are unordered");
230 AngledDirIdx = angledDirIdx;
231 SystemDirIdx = systemDirIdx;
232 NoCurDirSearch = noCurDirSearch;
233 //LookupFileCache.clear();
236 /// \brief Add an additional search path.
237 void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
238 unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
239 SearchDirs.insert(SearchDirs.begin() + idx, dir);
245 /// \brief Set the list of system header prefixes.
246 void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) {
247 SystemHeaderPrefixes.assign(P.begin(), P.end());
250 /// \brief Checks whether the map exists or not.
251 bool HasIncludeAliasMap() const {
252 return IncludeAliases;
255 /// \brief Map the source include name to the dest include name.
257 /// The Source should include the angle brackets or quotes, the dest
258 /// should not. This allows for distinction between <> and "" headers.
259 void AddIncludeAlias(StringRef Source, StringRef Dest) {
261 IncludeAliases.reset(new IncludeAliasMap);
262 (*IncludeAliases)[Source] = Dest;
265 /// MapHeaderToIncludeAlias - Maps one header file name to a different header
266 /// file name, for use with the include_alias pragma. Note that the source
267 /// file name should include the angle brackets or quotes. Returns StringRef
268 /// as null if the header cannot be mapped.
269 StringRef MapHeaderToIncludeAlias(StringRef Source) {
270 assert(IncludeAliases && "Trying to map headers when there's no map");
272 // Do any filename replacements before anything else
273 IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source);
274 if (Iter != IncludeAliases->end())
279 /// \brief Set the path to the module cache.
280 void setModuleCachePath(StringRef CachePath) {
281 ModuleCachePath = CachePath;
284 /// \brief Retrieve the path to the module cache.
285 StringRef getModuleCachePath() const { return ModuleCachePath; }
287 /// \brief Forget everything we know about headers so far.
288 void ClearFileInfo() {
292 void SetExternalLookup(ExternalIdentifierLookup *EIL) {
293 ExternalLookup = EIL;
296 ExternalIdentifierLookup *getExternalLookup() const {
297 return ExternalLookup;
300 /// \brief Set the external source of header information.
301 void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
305 /// \brief Set the target information for the header search, if not
307 void setTarget(const TargetInfo &Target);
309 /// \brief Given a "foo" or \<foo> reference, look up the indicated file,
310 /// return null on failure.
312 /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
313 /// the file was found in, or null if not applicable.
315 /// \param isAngled indicates whether the file reference is a <> reference.
317 /// \param CurDir If non-null, the file was found in the specified directory
318 /// search location. This is used to implement \#include_next.
320 /// \param CurFileEnt If non-null, indicates where the \#including file is, in
321 /// case a relative search is needed.
323 /// \param SearchPath If non-null, will be set to the search path relative
324 /// to which the file was found. If the include path is absolute, SearchPath
325 /// will be set to an empty string.
327 /// \param RelativePath If non-null, will be set to the path relative to
328 /// SearchPath at which the file was found. This only differs from the
329 /// Filename for framework includes.
331 /// \param SuggestedModule If non-null, and the file found is semantically
332 /// part of a known module, this will be set to the module that should
333 /// be imported instead of preprocessing/parsing the file found.
334 const FileEntry *LookupFile(StringRef Filename, bool isAngled,
335 const DirectoryLookup *FromDir,
336 const DirectoryLookup *&CurDir,
337 const FileEntry *CurFileEnt,
338 SmallVectorImpl<char> *SearchPath,
339 SmallVectorImpl<char> *RelativePath,
340 Module **SuggestedModule,
341 bool SkipCache = false);
343 /// \brief Look up a subframework for the specified \#include file.
345 /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
346 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
347 /// HIToolbox is a subframework within Carbon.framework. If so, return
348 /// the FileEntry for the designated file, otherwise return null.
349 const FileEntry *LookupSubframeworkHeader(
351 const FileEntry *RelativeFileEnt,
352 SmallVectorImpl<char> *SearchPath,
353 SmallVectorImpl<char> *RelativePath);
355 /// \brief Look up the specified framework name in our framework cache.
356 /// \returns The DirectoryEntry it is in if we know, null otherwise.
357 FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
358 return FrameworkMap.GetOrCreateValue(FWName).getValue();
361 /// \brief Mark the specified file as a target of of a \#include,
362 /// \#include_next, or \#import directive.
364 /// \return false if \#including the file will have no effect or true
365 /// if we should include it.
366 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
369 /// \brief Return whether the specified file is a normal header,
370 /// a system header, or a C++ friendly system header.
371 SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
372 return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
375 /// \brief Mark the specified file as a "once only" file, e.g. due to
377 void MarkFileIncludeOnce(const FileEntry *File) {
378 HeaderFileInfo &FI = getFileInfo(File);
380 FI.isPragmaOnce = true;
383 /// \brief Mark the specified file as a system header, e.g. due to
384 /// \#pragma GCC system_header.
385 void MarkFileSystemHeader(const FileEntry *File) {
386 getFileInfo(File).DirInfo = SrcMgr::C_System;
389 /// \brief Increment the count for the number of times the specified
390 /// FileEntry has been entered.
391 void IncrementIncludeCount(const FileEntry *File) {
392 ++getFileInfo(File).NumIncludes;
395 /// \brief Mark the specified file as having a controlling macro.
397 /// This is used by the multiple-include optimization to eliminate
398 /// no-op \#includes.
399 void SetFileControllingMacro(const FileEntry *File,
400 const IdentifierInfo *ControllingMacro) {
401 getFileInfo(File).ControllingMacro = ControllingMacro;
404 /// \brief Determine whether this file is intended to be safe from
405 /// multiple inclusions, e.g., it has \#pragma once or a controlling
408 /// This routine does not consider the effect of \#import
409 bool isFileMultipleIncludeGuarded(const FileEntry *File);
411 /// CreateHeaderMap - This method returns a HeaderMap for the specified
412 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
413 const HeaderMap *CreateHeaderMap(const FileEntry *FE);
415 /// \brief Retrieve the name of the module file that should be used to
416 /// load the given module.
418 /// \param Module The module whose module file name will be returned.
420 /// \returns The name of the module file that corresponds to this module,
421 /// or an empty string if this module does not correspond to any module file.
422 std::string getModuleFileName(Module *Module);
424 /// \brief Retrieve the name of the module file that should be used to
425 /// load a module with the given name.
427 /// \param ModuleName The module whose module file name will be returned.
429 /// \returns The name of the module file that corresponds to this module,
430 /// or an empty string if this module does not correspond to any module file.
431 std::string getModuleFileName(StringRef ModuleName);
433 /// \brief Lookup a module Search for a module with the given name.
435 /// \param ModuleName The name of the module we're looking for.
437 /// \param AllowSearch Whether we are allowed to search in the various
438 /// search directories to produce a module definition. If not, this lookup
439 /// will only return an already-known module.
441 /// \returns The module with the given name.
442 Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
444 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
446 /// \brief Determine whether there is a module map that may map the header
447 /// with the given file name to a (sub)module.
449 /// \param Filename The name of the file.
451 /// \param Root The "root" directory, at which we should stop looking for
453 bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root);
455 /// \brief Retrieve the module that corresponds to the given file, if any.
457 /// \param File The header that we wish to map to a module.
458 Module *findModuleForHeader(const FileEntry *File);
460 /// \brief Read the contents of the given module map file.
462 /// \param File The module map file.
464 /// \returns true if an error occurred, false otherwise.
465 bool loadModuleMapFile(const FileEntry *File);
467 /// \brief Collect the set of all known, top-level modules.
469 /// \param Modules Will be filled with the set of known, top-level modules.
470 void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules);
473 /// \brief Retrieve a module with the given name, which may be part of the
476 /// \param Name The name of the module to retrieve.
478 /// \param Dir The framework directory (e.g., ModuleName.framework).
480 /// \param IsSystem Whether the framework directory is part of the system
483 /// \returns The module, if found; otherwise, null.
484 Module *loadFrameworkModule(StringRef Name,
485 const DirectoryEntry *Dir,
489 /// \brief Retrieve the module map.
490 ModuleMap &getModuleMap() { return ModMap; }
492 unsigned header_file_size() const { return FileInfo.size(); }
494 // Used by ASTReader.
495 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
497 /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
498 const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
499 return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
502 // Used by external tools
503 typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
504 search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
505 search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
506 unsigned search_dir_size() const { return SearchDirs.size(); }
508 search_dir_iterator quoted_dir_begin() const {
509 return SearchDirs.begin();
511 search_dir_iterator quoted_dir_end() const {
512 return SearchDirs.begin() + AngledDirIdx;
515 search_dir_iterator angled_dir_begin() const {
516 return SearchDirs.begin() + AngledDirIdx;
518 search_dir_iterator angled_dir_end() const {
519 return SearchDirs.begin() + SystemDirIdx;
522 search_dir_iterator system_dir_begin() const {
523 return SearchDirs.begin() + SystemDirIdx;
525 search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
527 /// \brief Retrieve a uniqued framework name.
528 StringRef getUniqueFrameworkName(StringRef Framework);
532 size_t getTotalMemory() const;
534 static std::string NormalizeDashIncludePath(StringRef File,
535 FileManager &FileMgr);
538 /// \brief Describes what happened when we tried to load a module map file.
539 enum LoadModuleMapResult {
540 /// \brief The module map file had already been loaded.
542 /// \brief The module map file was loaded by this invocation.
544 /// \brief There is was directory with the given name.
546 /// \brief There was either no module map file or the module map file was
551 /// \brief Try to load the module map file in the given directory.
553 /// \param DirName The name of the directory where we will look for a module
556 /// \returns The result of attempting to load the module map file from the
558 LoadModuleMapResult loadModuleMapFile(StringRef DirName);
560 /// \brief Try to load the module map file in the given directory.
562 /// \param Dir The directory where we will look for a module map file.
564 /// \returns The result of attempting to load the module map file from the
566 LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir);
568 /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
569 HeaderFileInfo &getFileInfo(const FileEntry *FE);
572 } // end namespace clang