1 //===--- HeaderMap.h - A file that acts like dir of symlinks ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the HeaderMap interface.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LEX_HEADERMAP_H
14 #define LLVM_CLANG_LEX_HEADERMAP_H
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/MemoryBuffer.h"
29 /// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
31 std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
35 HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
36 : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
38 // Check for a valid header and extract the byte swap.
39 static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
41 /// If the specified relative filename is located in this HeaderMap return
42 /// the filename it is mapped to, otherwise return an empty StringRef.
43 StringRef lookupFilename(StringRef Filename,
44 SmallVectorImpl<char> &DestPath) const;
46 /// Return the filename of the headermap.
47 StringRef getFileName() const;
49 /// Print the contents of this headermap to stderr.
53 unsigned getEndianAdjustedWord(unsigned X) const;
54 const HMapHeader &getHeader() const;
55 HMapBucket getBucket(unsigned BucketNo) const;
57 /// Look up the specified string in the string table. If the string index is
58 /// not valid, return None.
59 Optional<StringRef> getString(unsigned StrTabIdx) const;
62 /// This class represents an Apple concept known as a 'header map'. To the
63 /// \#include file resolution process, it basically acts like a directory of
64 /// symlinks to files. Its advantages are that it is dense and more efficient
65 /// to create and process than a directory of symlinks.
66 class HeaderMap : private HeaderMapImpl {
67 HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
68 : HeaderMapImpl(std::move(File), BSwap) {}
71 /// This attempts to load the specified file as a header map. If it doesn't
72 /// look like a HeaderMap, it gives up and returns null.
73 static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
76 /// Check to see if the specified relative filename is located in this
77 /// HeaderMap. If so, open it and return its FileEntry. If RawPath is not
78 /// NULL and the file is found, RawPath will be set to the raw path at which
79 /// the file was found in the file system. For example, for a search path
80 /// ".." and a filename "../file.h" this would be "../../file.h".
81 const FileEntry *LookupFile(StringRef Filename, FileManager &FM) const;
83 using HeaderMapImpl::lookupFilename;
84 using HeaderMapImpl::getFileName;
85 using HeaderMapImpl::dump;
88 } // end namespace clang.