Chris Lattner | 0f441ab | 2007-12-17 08:22:46 +0000 | [diff] [blame] | 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" |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame^] | 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" |
Chris Lattner | 0f441ab | 2007-12-17 08:22:46 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame^] | 22 | enum { |
| 23 | HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p' |
| 24 | }; |
| 25 | |
| 26 | struct 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. |
| 42 | const 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 | |
| 47 | llvm::scoped_ptr<const llvm::MemoryBuffer> File( |
| 48 | llvm::MemoryBuffer::getFile(FE->getName(), strlen(FE->getName()), 0, |
| 49 | FE->getSize())); |
| 50 | if (File == 0) return 0; // Unreadable file? |
| 51 | const char *FileStart = File->getBufferStart(); |
| 52 | |
| 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. |
| 68 | |
Chris Lattner | 0f441ab | 2007-12-17 08:22:46 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /// LookupFile - Check to see if the specified relative filename is located in |
| 73 | /// this HeaderMap. If so, open it and return its FileEntry. |
| 74 | const FileEntry *HeaderMap::LookupFile(const char *FilenameStart, |
| 75 | const char *FilenameEnd, |
| 76 | FileManager &FM) const { |
| 77 | // FIXME: this needs work. |
| 78 | return 0; |
| 79 | } |