clang  19.0.0git
HeaderMap.h
Go to the documentation of this file.
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 
17 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <memory>
23 #include <optional>
24 
25 namespace clang {
26 
27 struct HMapBucket;
28 struct HMapHeader;
29 
30 /// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
32  std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
33  bool NeedsBSwap;
34  mutable llvm::StringMap<StringRef> ReverseMap;
35 
36 public:
37  HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
38  : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
39 
40  // Check for a valid header and extract the byte swap.
41  static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
42 
43  // Make a call for every Key in the map.
44  template <typename Func> void forEachKey(Func Callback) const {
45  const HMapHeader &Hdr = getHeader();
46  unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
47 
48  for (unsigned Bucket = 0; Bucket < NumBuckets; ++Bucket) {
49  HMapBucket B = getBucket(Bucket);
50  if (B.Key != HMAP_EmptyBucketKey)
51  if (std::optional<StringRef> Key = getString(B.Key))
52  Callback(*Key);
53  }
54  }
55 
56  /// If the specified relative filename is located in this HeaderMap return
57  /// the filename it is mapped to, otherwise return an empty StringRef.
58  StringRef lookupFilename(StringRef Filename,
59  SmallVectorImpl<char> &DestPath) const;
60 
61  /// Return the filename of the headermap.
62  StringRef getFileName() const;
63 
64  /// Print the contents of this headermap to stderr.
65  void dump() const;
66 
67  /// Return key for specifed path.
68  StringRef reverseLookupFilename(StringRef DestPath) const;
69 
70 private:
71  unsigned getEndianAdjustedWord(unsigned X) const;
72  const HMapHeader &getHeader() const;
73  HMapBucket getBucket(unsigned BucketNo) const;
74 
75  /// Look up the specified string in the string table. If the string index is
76  /// not valid, return std::nullopt.
77  std::optional<StringRef> getString(unsigned StrTabIdx) const;
78 };
79 
80 /// This class represents an Apple concept known as a 'header map'. To the
81 /// \#include file resolution process, it basically acts like a directory of
82 /// symlinks to files. Its advantages are that it is dense and more efficient
83 /// to create and process than a directory of symlinks.
84 class HeaderMap : private HeaderMapImpl {
85  HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
86  : HeaderMapImpl(std::move(File), BSwap) {}
87 
88 public:
89  /// This attempts to load the specified file as a header map. If it doesn't
90  /// look like a HeaderMap, it gives up and returns null.
91  static std::unique_ptr<HeaderMap> Create(FileEntryRef FE, FileManager &FM);
92 
93  using HeaderMapImpl::dump;
98 };
99 
100 } // end namespace clang.
101 
102 #endif
Defines the clang::FileManager interface and associated types.
StringRef Filename
Definition: Format.cpp:2976
#define X(type, name)
Definition: Value.h:143
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Implementation for HeaderMap that doesn't depend on FileManager.
Definition: HeaderMap.h:31
StringRef getFileName() const
Return the filename of the headermap.
Definition: HeaderMap.cpp:109
HeaderMapImpl(std::unique_ptr< const llvm::MemoryBuffer > File, bool NeedsBSwap)
Definition: HeaderMap.h:37
void forEachKey(Func Callback) const
Definition: HeaderMap.h:44
StringRef reverseLookupFilename(StringRef DestPath) const
Return key for specifed path.
Definition: HeaderMap.cpp:231
void dump() const
Print the contents of this headermap to stderr.
Definition: HeaderMap.cpp:172
StringRef lookupFilename(StringRef Filename, SmallVectorImpl< char > &DestPath) const
If the specified relative filename is located in this HeaderMap return the filename it is mapped to,...
Definition: HeaderMap.cpp:197
static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap)
Definition: HeaderMap.cpp:66
This class represents an Apple concept known as a 'header map'.
Definition: HeaderMap.h:84
static std::unique_ptr< HeaderMap > Create(FileEntryRef FE, FileManager &FM)
This attempts to load the specified file as a header map.
Definition: HeaderMap.cpp:52
The JSON file list parser is used to communicate input to InstallAPI.
@ HMAP_EmptyBucketKey
Definition: Format.h:5433