HeaderMap.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- HeaderMap.h - A file that acts like dir of symlinks ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the HeaderMap interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_LEX_HEADERMAP_H
  18. #define LLVM_CLANG_LEX_HEADERMAP_H
  19. #include "clang/Basic/FileManager.h"
  20. #include "clang/Basic/LLVM.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/ADT/StringMap.h"
  23. #include "llvm/Support/Compiler.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include <memory>
  26. namespace clang {
  27. struct HMapBucket;
  28. struct HMapHeader;
  29. /// Implementation for \a HeaderMap that doesn't depend on \a FileManager.
  30. class HeaderMapImpl {
  31. std::unique_ptr<const llvm::MemoryBuffer> FileBuffer;
  32. bool NeedsBSwap;
  33. mutable llvm::StringMap<StringRef> ReverseMap;
  34. public:
  35. HeaderMapImpl(std::unique_ptr<const llvm::MemoryBuffer> File, bool NeedsBSwap)
  36. : FileBuffer(std::move(File)), NeedsBSwap(NeedsBSwap) {}
  37. // Check for a valid header and extract the byte swap.
  38. static bool checkHeader(const llvm::MemoryBuffer &File, bool &NeedsByteSwap);
  39. /// If the specified relative filename is located in this HeaderMap return
  40. /// the filename it is mapped to, otherwise return an empty StringRef.
  41. StringRef lookupFilename(StringRef Filename,
  42. SmallVectorImpl<char> &DestPath) const;
  43. /// Return the filename of the headermap.
  44. StringRef getFileName() const;
  45. /// Print the contents of this headermap to stderr.
  46. void dump() const;
  47. /// Return key for specifed path.
  48. StringRef reverseLookupFilename(StringRef DestPath) const;
  49. private:
  50. unsigned getEndianAdjustedWord(unsigned X) const;
  51. const HMapHeader &getHeader() const;
  52. HMapBucket getBucket(unsigned BucketNo) const;
  53. /// Look up the specified string in the string table. If the string index is
  54. /// not valid, return None.
  55. Optional<StringRef> getString(unsigned StrTabIdx) const;
  56. };
  57. /// This class represents an Apple concept known as a 'header map'. To the
  58. /// \#include file resolution process, it basically acts like a directory of
  59. /// symlinks to files. Its advantages are that it is dense and more efficient
  60. /// to create and process than a directory of symlinks.
  61. class HeaderMap : private HeaderMapImpl {
  62. HeaderMap(std::unique_ptr<const llvm::MemoryBuffer> File, bool BSwap)
  63. : HeaderMapImpl(std::move(File), BSwap) {}
  64. public:
  65. /// This attempts to load the specified file as a header map. If it doesn't
  66. /// look like a HeaderMap, it gives up and returns null.
  67. static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
  68. FileManager &FM);
  69. using HeaderMapImpl::dump;
  70. using HeaderMapImpl::getFileName;
  71. using HeaderMapImpl::lookupFilename;
  72. using HeaderMapImpl::reverseLookupFilename;
  73. };
  74. } // end namespace clang.
  75. #endif
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif