blob: abd1f9ee0c8b5af08466fd4f17a55d4f85dfddab [file] [log] [blame]
Chris Lattner0f441ab2007-12-17 08:22:46 +00001//===--- 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"
Chris Lattner1bfd4a62007-12-17 18:34:53 +000015#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"
Chris Lattner0f441ab2007-12-17 08:22:46 +000020using namespace clang;
21
Chris Lattner1bfd4a62007-12-17 18:34:53 +000022enum {
Chris Lattnerba832652007-12-17 18:59:44 +000023 HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
24 HeaderVersion = 1
Chris Lattner1bfd4a62007-12-17 18:34:53 +000025};
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
Chris Lattner98751312007-12-17 18:44:09 +000048 llvm::scoped_ptr<const llvm::MemoryBuffer> FileBuffer(
Chris Lattner1bfd4a62007-12-17 18:34:53 +000049 llvm::MemoryBuffer::getFile(FE->getName(), strlen(FE->getName()), 0,
50 FE->getSize()));
Chris Lattner98751312007-12-17 18:44:09 +000051 if (FileBuffer == 0) return 0; // Unreadable file?
52 const char *FileStart = FileBuffer->getBufferStart();
Chris Lattner1bfd4a62007-12-17 18:34:53 +000053
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
Chris Lattnerba832652007-12-17 18:59:44 +000057 // Sniff it to see if it's a headermap by checking the magic number and
58 // version.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000059 bool NeedsByteSwap;
Chris Lattnerba832652007-12-17 18:59:44 +000060 if (Header->Magic == HeaderMagicNumber && Header->Version == HeaderVersion)
Chris Lattner1bfd4a62007-12-17 18:34:53 +000061 NeedsByteSwap = false;
Chris Lattnerba832652007-12-17 18:59:44 +000062 else if (Header->Magic == llvm::ByteSwap_32(HeaderMagicNumber) &&
63 Header->Version == llvm::ByteSwap_16(HeaderVersion))
Chris Lattner1bfd4a62007-12-17 18:34:53 +000064 NeedsByteSwap = true; // Mixed endianness headermap.
65 else
66 return 0; // Not a header map.
Chris Lattnerba832652007-12-17 18:59:44 +000067
68 if (Header->Reserved != 0) return 0;
Chris Lattner98751312007-12-17 18:44:09 +000069
70 // Okay, everything looks good, create the header map.
Chris Lattnerba832652007-12-17 18:59:44 +000071 return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
Chris Lattner98751312007-12-17 18:44:09 +000072}
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();
Chris Lattner0f441ab2007-12-17 08:22:46 +000082}
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}