1 | //===--- HeaderMap.h - A file that acts like dir of symlinks ----*- C++ -*-===// |
---|---|
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the HeaderMap interface. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_LEX_HEADERMAP_H |
14 | #define LLVM_CLANG_LEX_HEADERMAP_H |
15 | |
16 | #include "clang/Basic/LLVM.h" |
17 | #include "llvm/ADT/Optional.h" |
18 | #include "llvm/Support/Compiler.h" |
19 | #include "llvm/Support/MemoryBuffer.h" |
20 | #include <memory> |
21 | |
22 | namespace clang { |
23 | |
24 | class FileEntry; |
25 | class FileManager; |
26 | struct HMapBucket; |
27 | struct HMapHeader; |
28 | |
29 | /// Implementation for \a HeaderMap that doesn't depend on \a FileManager. |
30 | class HeaderMapImpl { |
31 | std::unique_ptr<const llvm::MemoryBuffer> FileBuffer; |
32 | bool NeedsBSwap; |
33 | |
34 | public: |
35 | HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap) |
36 | : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {} |
37 | |
38 | // Check for a valid header and extract the byte swap. |
39 | static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap); |
40 | |
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; |
45 | |
46 | /// Return the filename of the headermap. |
47 | StringRef getFileName() const; |
48 | |
49 | /// Print the contents of this headermap to stderr. |
50 | void dump() const; |
51 | |
52 | private: |
53 | unsigned getEndianAdjustedWord(unsigned X) const; |
54 | const HMapHeader &getHeader() const; |
55 | HMapBucket getBucket(unsigned BucketNo) const; |
56 | |
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; |
60 | }; |
61 | |
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) {} |
69 | |
70 | public: |
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, |
74 | FileManager &FM); |
75 | |
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; |
82 | |
83 | using HeaderMapImpl::lookupFilename; |
84 | using HeaderMapImpl::getFileName; |
85 | using HeaderMapImpl::dump; |
86 | }; |
87 | |
88 | } // end namespace clang. |
89 | |
90 | #endif |
91 |