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/FileManager.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <memory>
22 
23 namespace clang {
24 
25 struct HMapBucket;
26 struct HMapHeader;
27 
28 /// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
29 class HeaderMapImpl {
30   std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
31   bool NeedsBSwap;
32 
33 public:
34   HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
35       : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
36 
37   // Check for a valid header and extract the byte swap.
38   static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
39 
40   /// If the specified relative filename is located in this HeaderMap return
41   /// the filename it is mapped to, otherwise return an empty StringRef.
42   StringRef lookupFilename(StringRef Filename,
43                            SmallVectorImpl<char> &DestPath) const;
44 
45   /// Return the filename of the headermap.
46   StringRef getFileName() const;
47 
48   /// Print the contents of this headermap to stderr.
49   void dump() const;
50 
51 private:
52   unsigned getEndianAdjustedWord(unsigned X) const;
53   const HMapHeader &getHeader() const;
54   HMapBucket getBucket(unsigned BucketNo) const;
55 
56   /// Look up the specified string in the string table.  If the string index is
57   /// not valid, return None.
58   Optional<StringRef> getString(unsigned StrTabIdx) const;
59 };
60 
61 /// This class represents an Apple concept known as a 'header map'.  To the
62 /// \#include file resolution process, it basically acts like a directory of
63 /// symlinks to files.  Its advantages are that it is dense and more efficient
64 /// to create and process than a directory of symlinks.
65 class HeaderMap : private HeaderMapImpl {
66   HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
67       : HeaderMapImpl(std::move(File), BSwap) {}
68 
69 public:
70   /// This attempts to load the specified file as a header map.  If it doesn't
71   /// look like a HeaderMap, it gives up and returns null.
72   static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
73                                            FileManager &FM);
74 
75   /// Check to see if the specified relative filename is located in this
76   /// HeaderMap.  If so, open it and return its FileEntry.  If RawPath is not
77   /// NULL and the file is found, RawPath will be set to the raw path at which
78   /// the file was found in the file system. For example, for a search path
79   /// ".." and a filename "../file.h" this would be "../../file.h".
80   Optional<FileEntryRef> LookupFile(StringRef Filename, FileManager &FM) const;
81 
82   using HeaderMapImpl::lookupFilename;
83   using HeaderMapImpl::getFileName;
84   using HeaderMapImpl::dump;
85 };
86 
87 } // end namespace clang.
88 
89 #endif
90