HeaderMap.cpp revision ba8326517548d1d7773c4040aef4a4d91bb99df0
1//===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the HeaderMap interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/HeaderMap.h"
15#include "clang/Basic/FileManager.h"
16#include "llvm/ADT/scoped_ptr.h"
17#include "llvm/Support/DataTypes.h"
18#include "llvm/Support/MathExtras.h"
19#include "llvm/Support/MemoryBuffer.h"
20using namespace clang;
21
22enum {
23  HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
24  HeaderVersion = 1
25};
26
27struct HMapHeader {
28  uint32_t Magic;           // Magic word, also indicates byte order.
29  uint16_t Version;         // Version number -- currently 1.
30  uint16_t Reserved;        // Reserved for future use - zero for now.
31  uint32_t StringsOffset;   // Offset to start of string pool.
32  uint32_t Count;           // Number of entries in the string table.
33  uint32_t Capacity;        // Number of buckets (always a power of 2).
34  uint32_t MaxValueLength;  // Length of longest result path (excluding nul).
35  // Strings follow the buckets, at StringsOffset.
36};
37
38
39/// HeaderMap::Create - This attempts to load the specified file as a header
40/// map.  If it doesn't look like a HeaderMap, it gives up and returns null.
41/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
42/// into the string error argument and returns null.
43const HeaderMap *HeaderMap::Create(const FileEntry *FE) {
44  // If the file is too small to be a header map, ignore it.
45  unsigned FileSize = FE->getSize();
46  if (FileSize <= sizeof(HMapHeader)) return 0;
47
48  llvm::scoped_ptr<const llvm::MemoryBuffer> FileBuffer(
49    llvm::MemoryBuffer::getFile(FE->getName(), strlen(FE->getName()), 0,
50                                FE->getSize()));
51  if (FileBuffer == 0) return 0;  // Unreadable file?
52  const char *FileStart = FileBuffer->getBufferStart();
53
54  // We know the file is at least as big as the header, check it now.
55  const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
56
57  // Sniff it to see if it's a headermap by checking the magic number and
58  // version.
59  bool NeedsByteSwap;
60  if (Header->Magic == HeaderMagicNumber && Header->Version == HeaderVersion)
61    NeedsByteSwap = false;
62  else if (Header->Magic == llvm::ByteSwap_32(HeaderMagicNumber) &&
63           Header->Version == llvm::ByteSwap_16(HeaderVersion))
64    NeedsByteSwap = true;  // Mixed endianness headermap.
65  else
66    return 0;  // Not a header map.
67
68  if (Header->Reserved != 0) return 0;
69
70  // Okay, everything looks good, create the header map.
71  return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
72}
73
74HeaderMap::~HeaderMap() {
75  delete FileBuffer;
76}
77
78
79/// getFileName - Return the filename of the headermap.
80const char *HeaderMap::getFileName() const {
81  return FileBuffer->getBufferIdentifier();
82}
83
84/// LookupFile - Check to see if the specified relative filename is located in
85/// this HeaderMap.  If so, open it and return its FileEntry.
86const FileEntry *HeaderMap::LookupFile(const char *FilenameStart,
87                                       const char *FilenameEnd,
88                                       FileManager &FM) const {
89  // FIXME: this needs work.
90  return 0;
91}
92