Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:56

0001 //===--- HeaderMap.h - A file that acts like dir of symlinks ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the HeaderMap interface.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_LEX_HEADERMAP_H
0014 #define LLVM_CLANG_LEX_HEADERMAP_H
0015 
0016 #include "clang/Basic/FileManager.h"
0017 #include "clang/Basic/LLVM.h"
0018 #include "clang/Lex/HeaderMapTypes.h"
0019 #include "llvm/ADT/StringMap.h"
0020 #include "llvm/Support/Compiler.h"
0021 #include "llvm/Support/MemoryBuffer.h"
0022 #include <memory>
0023 #include <optional>
0024 
0025 namespace clang {
0026 
0027 struct HMapBucket;
0028 struct HMapHeader;
0029 
0030 /// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
0031 class HeaderMapImpl {
0032   std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
0033   bool NeedsBSwap;
0034   mutable llvm::StringMap<StringRef> ReverseMap;
0035 
0036 public:
0037   HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
0038       : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
0039 
0040   // Check for a valid header and extract the byte swap.
0041   static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
0042 
0043   // Make a call for every Key in the map.
0044   template <typename Func> void forEachKey(Func Callback) const {
0045     const HMapHeader &Hdr = getHeader();
0046     unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
0047 
0048     for (unsigned Bucket = 0; Bucket < NumBuckets; ++Bucket) {
0049       HMapBucket B = getBucket(Bucket);
0050       if (B.Key != HMAP_EmptyBucketKey)
0051         if (std::optional<StringRef> Key = getString(B.Key))
0052           Callback(*Key);
0053     }
0054   }
0055 
0056   /// If the specified relative filename is located in this HeaderMap return
0057   /// the filename it is mapped to, otherwise return an empty StringRef.
0058   StringRef lookupFilename(StringRef Filename,
0059                            SmallVectorImpl<char> &DestPath) const;
0060 
0061   /// Return the filename of the headermap.
0062   StringRef getFileName() const;
0063 
0064   /// Print the contents of this headermap to stderr.
0065   void dump() const;
0066 
0067   /// Return key for specifed path.
0068   StringRef reverseLookupFilename(StringRef DestPath) const;
0069 
0070 private:
0071   unsigned getEndianAdjustedWord(unsigned X) const;
0072   const HMapHeader &getHeader() const;
0073   HMapBucket getBucket(unsigned BucketNo) const;
0074 
0075   /// Look up the specified string in the string table.  If the string index is
0076   /// not valid, return std::nullopt.
0077   std::optional<StringRef> getString(unsigned StrTabIdx) const;
0078 };
0079 
0080 /// This class represents an Apple concept known as a 'header map'.  To the
0081 /// \#include file resolution process, it basically acts like a directory of
0082 /// symlinks to files.  Its advantages are that it is dense and more efficient
0083 /// to create and process than a directory of symlinks.
0084 class HeaderMap : private HeaderMapImpl {
0085   HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
0086       : HeaderMapImpl(std::move(File), BSwap) {}
0087 
0088 public:
0089   /// This attempts to load the specified file as a header map.  If it doesn't
0090   /// look like a HeaderMap, it gives up and returns null.
0091   static std::unique_ptr<HeaderMap> Create(FileEntryRef FE, FileManager &FM);
0092 
0093   using HeaderMapImpl::dump;
0094   using HeaderMapImpl::forEachKey;
0095   using HeaderMapImpl::getFileName;
0096   using HeaderMapImpl::lookupFilename;
0097   using HeaderMapImpl::reverseLookupFilename;
0098 };
0099 
0100 } // end namespace clang.
0101 
0102 #endif