clang  16.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/Optional.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <memory>
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  Optional<StringRef> Key = getString(B.Key);
52  if (Key)
53  Callback(Key.value());
54  }
55  }
56  }
57 
58  /// If the specified relative filename is located in this HeaderMap return
59  /// the filename it is mapped to, otherwise return an empty StringRef.
60  StringRef lookupFilename(StringRef Filename,
61  SmallVectorImpl<char> &DestPath) const;
62 
63  /// Return the filename of the headermap.
64  StringRef getFileName() const;
65 
66  /// Print the contents of this headermap to stderr.
67  void dump() const;
68 
69  /// Return key for specifed path.
70  StringRef reverseLookupFilename(StringRef DestPath) const;
71 
72 private:
73  unsigned getEndianAdjustedWord(unsigned X) const;
74  const HMapHeader &getHeader() const;
75  HMapBucket getBucket(unsigned BucketNo) const;
76 
77  /// Look up the specified string in the string table. If the string index is
78  /// not valid, return None.
79  Optional<StringRef> getString(unsigned StrTabIdx) const;
80 };
81 
82 /// This class represents an Apple concept known as a 'header map'. To the
83 /// \#include file resolution process, it basically acts like a directory of
84 /// symlinks to files. Its advantages are that it is dense and more efficient
85 /// to create and process than a directory of symlinks.
86 class HeaderMap : private HeaderMapImpl {
87  HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
88  : HeaderMapImpl(std::move(File), BSwap) {}
89 
90 public:
91  /// This attempts to load the specified file as a header map. If it doesn't
92  /// look like a HeaderMap, it gives up and returns null.
93  static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
94  FileManager &FM);
95 
96  using HeaderMapImpl::dump;
101 };
102 
103 } // end namespace clang.
104 
105 #endif
clang::DeclaratorContext::File
@ File
clang::HeaderMapImpl::reverseLookupFilename
StringRef reverseLookupFilename(StringRef DestPath) const
Return key for specifed path.
Definition: HeaderMap.cpp:231
Filename
StringRef Filename
Definition: Format.cpp:2715
clang::HeaderMapImpl
Implementation for HeaderMap that doesn't depend on FileManager.
Definition: HeaderMap.h:31
clang::HMAP_EmptyBucketKey
@ HMAP_EmptyBucketKey
Definition: HeaderMapTypes.h:19
llvm::Optional
Definition: LLVM.h:40
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::HeaderMap::Create
static std::unique_ptr< HeaderMap > Create(const FileEntry *FE, FileManager &FM)
This attempts to load the specified file as a header map.
Definition: HeaderMap.cpp:50
clang::HeaderMapImpl::getFileName
StringRef getFileName() const
Return the filename of the headermap.
Definition: HeaderMap.cpp:109
clang::HeaderMap
This class represents an Apple concept known as a 'header map'.
Definition: HeaderMap.h:86
clang::HMapHeader::NumBuckets
uint32_t NumBuckets
Definition: HeaderMapTypes.h:34
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:366
clang::HeaderMapImpl::forEachKey
void forEachKey(Func Callback) const
Definition: HeaderMap.h:44
clang::HeaderMapImpl::checkHeader
static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap)
Definition: HeaderMap.cpp:65
HeaderMapTypes.h
clang::HMapBucket
Definition: HeaderMapTypes.h:22
LLVM.h
clang::HeaderMapImpl::lookupFilename
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
std
Definition: Format.h:4477
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::HeaderMapImpl::dump
void dump() const
Print the contents of this headermap to stderr.
Definition: HeaderMap.cpp:172
clang::HeaderMapImpl::HeaderMapImpl
HeaderMapImpl(std::unique_ptr< const llvm::MemoryBuffer > File, bool NeedsBSwap)
Definition: HeaderMap.h:37
llvm::SmallVectorImpl< char >
FileManager.h
clang::HMapBucket::Key
uint32_t Key
Definition: HeaderMapTypes.h:23
clang::HMapHeader
Definition: HeaderMapTypes.h:28