Clang Project

clang_source_code/include/clang/Lex/HeaderMap.h
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
22namespace clang {
23
24class FileEntry;
25class FileManager;
26struct HMapBucket;
27struct HMapHeader;
28
29/// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
30class HeaderMapImpl {
31  std::unique_ptr<const llvm::MemoryBufferFileBuffer;
32  bool NeedsBSwap;
33
34public:
35  HeaderMapImpl(std::unique_ptr<const llvm::MemoryBufferFilebool 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 &Filebool &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> &DestPathconst;
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
52private:
53  unsigned getEndianAdjustedWord(unsigned Xconst;
54  const HMapHeader &getHeader() const;
55  HMapBucket getBucket(unsigned BucketNoconst;
56
57  /// Look up the specified string in the string table.  If the string index is
58  /// not valid, return None.
59  Optional<StringRefgetString(unsigned StrTabIdxconst;
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.
66class HeaderMap : private HeaderMapImpl {
67  HeaderMap(std::unique_ptr<const llvm::MemoryBufferFilebool BSwap)
68      : HeaderMapImpl(std::move(File), BSwap) {}
69
70public:
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<HeaderMapCreate(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 FilenameFileManager &FMconst;
82
83  using HeaderMapImpl::lookupFilename;
84  using HeaderMapImpl::getFileName;
85  using HeaderMapImpl::dump;
86};
87
88// end namespace clang.
89
90#endif
91
clang::HeaderMapImpl::FileBuffer
clang::HeaderMapImpl::NeedsBSwap
clang::HeaderMapImpl::checkHeader
clang::HeaderMapImpl::lookupFilename
clang::HeaderMapImpl::getFileName
clang::HeaderMapImpl::dump
clang::HeaderMapImpl::getEndianAdjustedWord
clang::HeaderMapImpl::getHeader
clang::HeaderMapImpl::getBucket
clang::HeaderMapImpl::getString
clang::HeaderMap::Create
clang::HeaderMap::LookupFile
clang::HeaderMapImpl::checkHeader