1193326Sed//===--- HeaderMap.h - A file that acts like dir of symlinks ----*- C++ -*-===//
2193326Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193326Sed//
7193326Sed//===----------------------------------------------------------------------===//
8193326Sed//
9193326Sed// This file defines the HeaderMap interface.
10193326Sed//
11193326Sed//===----------------------------------------------------------------------===//
12193326Sed
13193326Sed#ifndef LLVM_CLANG_LEX_HEADERMAP_H
14193326Sed#define LLVM_CLANG_LEX_HEADERMAP_H
15193326Sed
16360784Sdim#include "clang/Basic/FileManager.h"
17226633Sdim#include "clang/Basic/LLVM.h"
18309124Sdim#include "llvm/ADT/Optional.h"
19243830Sdim#include "llvm/Support/Compiler.h"
20309124Sdim#include "llvm/Support/MemoryBuffer.h"
21280031Sdim#include <memory>
22226633Sdim
23193326Sednamespace clang {
24193326Sed
25309124Sdimstruct HMapBucket;
26309124Sdimstruct HMapHeader;
27198092Srdivacky
28309124Sdim/// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
29309124Sdimclass HeaderMapImpl {
30280031Sdim  std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
31193326Sed  bool NeedsBSwap;
32198092Srdivacky
33193326Sedpublic:
34309124Sdim  HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
35309124Sdim      : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
36198092Srdivacky
37309124Sdim  // Check for a valid header and extract the byte swap.
38309124Sdim  static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
39198092Srdivacky
40276479Sdim  /// If the specified relative filename is located in this HeaderMap return
41276479Sdim  /// the filename it is mapped to, otherwise return an empty StringRef.
42276479Sdim  StringRef lookupFilename(StringRef Filename,
43276479Sdim                           SmallVectorImpl<char> &DestPath) const;
44276479Sdim
45309124Sdim  /// Return the filename of the headermap.
46314564Sdim  StringRef getFileName() const;
47198092Srdivacky
48309124Sdim  /// Print the contents of this headermap to stderr.
49193326Sed  void dump() const;
50198092Srdivacky
51193326Sedprivate:
52193326Sed  unsigned getEndianAdjustedWord(unsigned X) const;
53193326Sed  const HMapHeader &getHeader() const;
54193326Sed  HMapBucket getBucket(unsigned BucketNo) const;
55309124Sdim
56309124Sdim  /// Look up the specified string in the string table.  If the string index is
57309124Sdim  /// not valid, return None.
58309124Sdim  Optional<StringRef> getString(unsigned StrTabIdx) const;
59193326Sed};
60193326Sed
61309124Sdim/// This class represents an Apple concept known as a 'header map'.  To the
62309124Sdim/// \#include file resolution process, it basically acts like a directory of
63309124Sdim/// symlinks to files.  Its advantages are that it is dense and more efficient
64309124Sdim/// to create and process than a directory of symlinks.
65309124Sdimclass HeaderMap : private HeaderMapImpl {
66309124Sdim  HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
67309124Sdim      : HeaderMapImpl(std::move(File), BSwap) {}
68309124Sdim
69309124Sdimpublic:
70309124Sdim  /// This attempts to load the specified file as a header map.  If it doesn't
71309124Sdim  /// look like a HeaderMap, it gives up and returns null.
72344779Sdim  static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
73344779Sdim                                           FileManager &FM);
74309124Sdim
75309124Sdim  /// Check to see if the specified relative filename is located in this
76309124Sdim  /// HeaderMap.  If so, open it and return its FileEntry.  If RawPath is not
77309124Sdim  /// NULL and the file is found, RawPath will be set to the raw path at which
78309124Sdim  /// the file was found in the file system. For example, for a search path
79309124Sdim  /// ".." and a filename "../file.h" this would be "../../file.h".
80360784Sdim  Optional<FileEntryRef> LookupFile(StringRef Filename, FileManager &FM) const;
81309124Sdim
82309124Sdim  using HeaderMapImpl::lookupFilename;
83309124Sdim  using HeaderMapImpl::getFileName;
84309124Sdim  using HeaderMapImpl::dump;
85309124Sdim};
86309124Sdim
87193326Sed} // end namespace clang.
88193326Sed
89193326Sed#endif
90