blob: c698aba0663e6d47bfc5b035cfc000c3d5e026f8 [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 {
23 HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p'
24};
25
26struct HMapHeader {
27 uint32_t Magic; // Magic word, also indicates byte order.
28 uint16_t Version; // Version number -- currently 1.
29 uint16_t Reserved; // Reserved for future use - zero for now.
30 uint32_t StringsOffset; // Offset to start of string pool.
31 uint32_t Count; // Number of entries in the string table.
32 uint32_t Capacity; // Number of buckets (always a power of 2).
33 uint32_t MaxValueLength; // Length of longest result path (excluding nul).
34 // Strings follow the buckets, at StringsOffset.
35};
36
37
38/// HeaderMap::Create - This attempts to load the specified file as a header
39/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
40/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
41/// into the string error argument and returns null.
42const HeaderMap *HeaderMap::Create(const FileEntry *FE) {
43 // If the file is too small to be a header map, ignore it.
44 unsigned FileSize = FE->getSize();
45 if (FileSize <= sizeof(HMapHeader)) return 0;
46
Chris Lattner98751312007-12-17 18:44:09 +000047 llvm::scoped_ptr<const llvm::MemoryBuffer> FileBuffer(
Chris Lattner1bfd4a62007-12-17 18:34:53 +000048 llvm::MemoryBuffer::getFile(FE->getName(), strlen(FE->getName()), 0,
49 FE->getSize()));
Chris Lattner98751312007-12-17 18:44:09 +000050 if (FileBuffer == 0) return 0; // Unreadable file?
51 const char *FileStart = FileBuffer->getBufferStart();
Chris Lattner1bfd4a62007-12-17 18:34:53 +000052
53 // We know the file is at least as big as the header, check it now.
54 const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
55
56 // Sniff it to see if it's a headermap.
57 if (Header->Version != 1 || Header->Reserved != 0)
58 return 0;
59
60 // Check the magic number.
61 bool NeedsByteSwap;
62 if (Header->Magic == HeaderMagicNumber)
63 NeedsByteSwap = false;
64 else if (Header->Magic == llvm::ByteSwap_32(HeaderMagicNumber))
65 NeedsByteSwap = true; // Mixed endianness headermap.
66 else
67 return 0; // Not a header map.
Chris Lattner98751312007-12-17 18:44:09 +000068
69 // Okay, everything looks good, create the header map.
70 HeaderMap *NewHM = new HeaderMap(FileBuffer.get(), NeedsByteSwap);
71 FileBuffer.reset(); // Don't deallocate the buffer on return.
72 return NewHM;
73}
74
75HeaderMap::~HeaderMap() {
76 delete FileBuffer;
77}
78
79
80/// getFileName - Return the filename of the headermap.
81const char *HeaderMap::getFileName() const {
82 return FileBuffer->getBufferIdentifier();
Chris Lattner0f441ab2007-12-17 08:22:46 +000083}
84
85/// LookupFile - Check to see if the specified relative filename is located in
86/// this HeaderMap. If so, open it and return its FileEntry.
87const FileEntry *HeaderMap::LookupFile(const char *FilenameStart,
88 const char *FilenameEnd,
89 FileManager &FM) const {
90 // FIXME: this needs work.
91 return 0;
92}