Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_SOURCEMANAGER_H |
| 15 | #define LLVM_CLANG_SOURCEMANAGER_H |
| 16 | |
| 17 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Allocator.h" |
Chandler Carruth | 9f8eb20 | 2009-10-26 01:37:10 +0000 | [diff] [blame] | 19 | #include "llvm/System/DataTypes.h" |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include <vector> |
Chris Lattner | 9dc62f0 | 2007-07-12 15:32:57 +0000 | [diff] [blame] | 23 | #include <cassert> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | class MemoryBuffer; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 27 | class StringRef; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | namespace clang { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 32 | class Diagnostic; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | class SourceManager; |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 34 | class FileManager; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | class FileEntry; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 36 | class LineTableInfo; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 38 | /// SrcMgr - Public enums and private classes that are part of the |
| 39 | /// SourceManager implementation. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | /// |
| 41 | namespace SrcMgr { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 42 | /// CharacteristicKind - This is used to represent whether a file or directory |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 43 | /// holds normal user code, system code, or system code which is implicitly |
| 44 | /// 'extern "C"' in C++ mode. Entire directories can be tagged with this |
| 45 | /// (this is maintained by DirectoryLookup and friends) as can specific |
| 46 | /// FileIDInfos when a #pragma system_header is seen or various other cases. |
| 47 | /// |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 48 | enum CharacteristicKind { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 49 | C_User, C_System, C_ExternCSystem |
| 50 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 52 | /// ContentCache - Once instance of this struct is kept for every file |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 53 | /// loaded or used. This object owns the MemoryBuffer object. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 54 | class ContentCache { |
| 55 | /// Buffer - The actual buffer containing the characters from the input |
| 56 | /// file. This is owned by the ContentCache object. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 57 | mutable const llvm::MemoryBuffer *Buffer; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 58 | |
| 59 | public: |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 60 | /// Reference to the file entry. This reference does not own |
| 61 | /// the FileEntry object. It is possible for this to be NULL if |
| 62 | /// the ContentCache encapsulates an imaginary text buffer. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 63 | const FileEntry *Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 65 | /// SourceLineCache - A bump pointer allocated array of offsets for each |
| 66 | /// source line. This is lazily computed. This is owned by the |
| 67 | /// SourceManager BumpPointerAllocator object. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 68 | unsigned *SourceLineCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 70 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 71 | /// if SourceLineCache is non-null. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | unsigned NumLines; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 74 | /// getBuffer - Returns the memory buffer for the associated content. |
| 75 | /// |
| 76 | /// \param Diag Object through which diagnostics will be emitted it the |
| 77 | /// buffer cannot be retrieved. |
| 78 | /// |
| 79 | /// \param Invalid If non-NULL, will be set \c true if an error occurred. |
| 80 | const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag, |
| 81 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 83 | /// getSize - Returns the size of the content encapsulated by this |
| 84 | /// ContentCache. This can be the size of the source file or the size of an |
| 85 | /// arbitrary scratch buffer. If the ContentCache encapsulates a source |
| 86 | /// file this size is retrieved from the file's FileEntry. |
| 87 | unsigned getSize() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 89 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 90 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 91 | /// instantiated. |
| 92 | unsigned getSizeBytesMapped() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 94 | void setBuffer(const llvm::MemoryBuffer *B) { |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 95 | assert(!Buffer && "MemoryBuffer already set."); |
| 96 | Buffer = B; |
| 97 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 99 | /// \brief Replace the existing buffer (which will be deleted) |
| 100 | /// with the given buffer. |
| 101 | void replaceBuffer(const llvm::MemoryBuffer *B); |
| 102 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 103 | ContentCache(const FileEntry *Ent = 0) |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 104 | : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {} |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 105 | |
| 106 | ~ContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 108 | /// The copy ctor does not allow copies where source object has either |
| 109 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
| 110 | /// is not transfered, so this is a logical error. |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 111 | ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) { |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 112 | Entry = RHS.Entry; |
| 113 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 114 | assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0 |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 115 | && "Passed ContentCache object cannot own a buffer."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
| 117 | NumLines = RHS.NumLines; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 120 | private: |
| 121 | // Disable assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | ContentCache &operator=(const ContentCache& RHS); |
| 123 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 125 | /// FileInfo - Information about a FileID, basically just the logical file |
| 126 | /// that it represents and include stack information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 128 | /// Each FileInfo has include stack information, indicating where it came |
| 129 | /// from. This information encodes the #include chain that a token was |
| 130 | /// instantiated from. The main include file has an invalid IncludeLoc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 132 | /// FileInfos contain a "ContentCache *", with the contents of the file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 134 | class FileInfo { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | /// IncludeLoc - The location of the #include that brought in this file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 136 | /// This is an invalid SLOC for the main file (top of the #include chain). |
| 137 | unsigned IncludeLoc; // Really a SourceLocation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 139 | /// Data - This contains the ContentCache* and the bits indicating the |
| 140 | /// characteristic of the file and whether it has #line info, all bitmangled |
| 141 | /// together. |
| 142 | uintptr_t Data; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 143 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 144 | /// get - Return a FileInfo object. |
| 145 | static FileInfo get(SourceLocation IL, const ContentCache *Con, |
| 146 | CharacteristicKind FileCharacter) { |
| 147 | FileInfo X; |
| 148 | X.IncludeLoc = IL.getRawEncoding(); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 149 | X.Data = (uintptr_t)Con; |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 150 | assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 151 | assert((unsigned)FileCharacter < 4 && "invalid file character"); |
| 152 | X.Data |= (unsigned)FileCharacter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | return X; |
| 154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 156 | SourceLocation getIncludeLoc() const { |
| 157 | return SourceLocation::getFromRawEncoding(IncludeLoc); |
| 158 | } |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 159 | const ContentCache* getContentCache() const { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 160 | return reinterpret_cast<const ContentCache*>(Data & ~7UL); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 163 | /// getCharacteristic - Return whether this is a system header or not. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | CharacteristicKind getFileCharacteristic() const { |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 165 | return (CharacteristicKind)(Data & 3); |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 167 | |
| 168 | /// hasLineDirectives - Return true if this FileID has #line directives in |
| 169 | /// it. |
| 170 | bool hasLineDirectives() const { return (Data & 4) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 172 | /// setHasLineDirectives - Set the flag that indicates that this FileID has |
| 173 | /// line table entries associated with it. |
| 174 | void setHasLineDirectives() { |
| 175 | Data |= 4; |
| 176 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 177 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 179 | /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation |
| 180 | /// location - where the token was ultimately instantiated, and the |
| 181 | /// SpellingLoc - where the actual character data for the token came from. |
| 182 | class InstantiationInfo { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 183 | // Really these are all SourceLocations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 185 | /// SpellingLoc - Where the spelling for the token can be found. |
| 186 | unsigned SpellingLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 188 | /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 189 | /// indicate the start and end of the instantiation. In object-like macros, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 190 | /// these will be the same. In a function-like macro instantiation, the |
| 191 | /// start will be the identifier and the end will be the ')'. |
| 192 | unsigned InstantiationLocStart, InstantiationLocEnd; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 193 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 194 | SourceLocation getSpellingLoc() const { |
| 195 | return SourceLocation::getFromRawEncoding(SpellingLoc); |
| 196 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 197 | SourceLocation getInstantiationLocStart() const { |
| 198 | return SourceLocation::getFromRawEncoding(InstantiationLocStart); |
| 199 | } |
| 200 | SourceLocation getInstantiationLocEnd() const { |
| 201 | return SourceLocation::getFromRawEncoding(InstantiationLocEnd); |
| 202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 204 | std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const { |
| 205 | return std::make_pair(getInstantiationLocStart(), |
| 206 | getInstantiationLocEnd()); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 209 | /// get - Return a InstantiationInfo for an expansion. IL specifies |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 210 | /// the instantiation location (where the macro is expanded), and SL |
| 211 | /// specifies the spelling location (where the characters from the token |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 212 | /// come from). IL and PL can both refer to normal File SLocs or |
| 213 | /// instantiation locations. |
| 214 | static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd, |
| 215 | SourceLocation SL) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 216 | InstantiationInfo X; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 217 | X.SpellingLoc = SL.getRawEncoding(); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 218 | X.InstantiationLocStart = ILStart.getRawEncoding(); |
| 219 | X.InstantiationLocEnd = ILEnd.getRawEncoding(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 220 | return X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | } |
| 222 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 224 | /// SLocEntry - This is a discriminated union of FileInfo and |
| 225 | /// InstantiationInfo. SourceManager keeps an array of these objects, and |
| 226 | /// they are uniquely identified by the FileID datatype. |
| 227 | class SLocEntry { |
| 228 | unsigned Offset; // low bit is set for instantiation info. |
| 229 | union { |
| 230 | FileInfo File; |
| 231 | InstantiationInfo Instantiation; |
| 232 | }; |
| 233 | public: |
| 234 | unsigned getOffset() const { return Offset >> 1; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 236 | bool isInstantiation() const { return Offset & 1; } |
| 237 | bool isFile() const { return !isInstantiation(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 239 | const FileInfo &getFile() const { |
| 240 | assert(isFile() && "Not a file SLocEntry!"); |
| 241 | return File; |
| 242 | } |
| 243 | |
| 244 | const InstantiationInfo &getInstantiation() const { |
| 245 | assert(isInstantiation() && "Not an instantiation SLocEntry!"); |
| 246 | return Instantiation; |
| 247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 249 | static SLocEntry get(unsigned Offset, const FileInfo &FI) { |
| 250 | SLocEntry E; |
| 251 | E.Offset = Offset << 1; |
| 252 | E.File = FI; |
| 253 | return E; |
| 254 | } |
| 255 | |
| 256 | static SLocEntry get(unsigned Offset, const InstantiationInfo &II) { |
| 257 | SLocEntry E; |
| 258 | E.Offset = (Offset << 1) | 1; |
| 259 | E.Instantiation = II; |
| 260 | return E; |
| 261 | } |
| 262 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } // end SrcMgr namespace. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 264 | |
| 265 | /// \brief External source of source location entries. |
| 266 | class ExternalSLocEntrySource { |
| 267 | public: |
| 268 | virtual ~ExternalSLocEntrySource(); |
| 269 | |
| 270 | /// \brief Read the source location entry with index ID. |
| 271 | virtual void ReadSLocEntry(unsigned ID) = 0; |
| 272 | }; |
| 273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | /// SourceManager - This file handles loading and caching of source files into |
| 275 | /// memory. This object owns the MemoryBuffer objects for all of the loaded |
| 276 | /// files and assigns unique FileID's for each unique #include chain. |
| 277 | /// |
| 278 | /// The SourceManager can be queried for information about SourceLocation |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 279 | /// objects, turning them into either spelling or instantiation locations. |
| 280 | /// Spelling locations represent where the bytes corresponding to a token came |
| 281 | /// from and instantiation locations represent where the location is in the |
| 282 | /// user's view. In the case of a macro expansion, for example, the spelling |
| 283 | /// location indicates where the expanded token came from and the instantiation |
| 284 | /// location specifies where it was expanded. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | class SourceManager { |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 286 | /// \brief Diagnostic object. |
| 287 | Diagnostic &Diag; |
| 288 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 289 | mutable llvm::BumpPtrAllocator ContentCacheAlloc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 292 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 293 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | /// non-null, FileEntry pointers. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 295 | llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | /// MemBufferInfos - Information about various memory buffers that we have |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 298 | /// read in. All FileEntry* within the stored ContentCache objects are NULL, |
| 299 | /// as they do not refer to a file. |
| 300 | std::vector<SrcMgr::ContentCache*> MemBufferInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 302 | /// SLocEntryTable - This is an array of SLocEntry's that we have created. |
| 303 | /// FileID is an index into this vector. This array is sorted by the offset. |
| 304 | std::vector<SrcMgr::SLocEntry> SLocEntryTable; |
| 305 | /// NextOffset - This is the next available offset that a new SLocEntry can |
| 306 | /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry. |
| 307 | unsigned NextOffset; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 308 | |
| 309 | /// \brief If source location entries are being lazily loaded from |
| 310 | /// an external source, this vector indicates whether the Ith source |
| 311 | /// location entry has already been loaded from the external storage. |
| 312 | std::vector<bool> SLocEntryLoaded; |
| 313 | |
| 314 | /// \brief An external source for source location entries. |
| 315 | ExternalSLocEntrySource *ExternalSLocEntries; |
| 316 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 317 | /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. |
| 318 | /// LastFileIDLookup records the last FileID looked up or created, because it |
| 319 | /// is very common to look up many tokens from the same file. |
| 320 | mutable FileID LastFileIDLookup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 322 | /// LineTable - This holds information for #line directives. It is referenced |
| 323 | /// by indices from SLocEntryTable. |
| 324 | LineTableInfo *LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 326 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 327 | /// method which is used to speedup getLineNumber calls to nearby locations. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 328 | mutable FileID LastLineNoFileIDQuery; |
Chris Lattner | f812a45 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 329 | mutable SrcMgr::ContentCache *LastLineNoContentCache; |
| 330 | mutable unsigned LastLineNoFilePos; |
| 331 | mutable unsigned LastLineNoResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 333 | /// MainFileID - The file ID for the main source file of the translation unit. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 334 | FileID MainFileID; |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 335 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 336 | // Statistics for -print-stats. |
| 337 | mutable unsigned NumLinearScans, NumBinaryProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 339 | // Cache results for the isBeforeInTranslationUnit method. |
| 340 | mutable FileID LastLFIDForBeforeTUCheck; |
| 341 | mutable FileID LastRFIDForBeforeTUCheck; |
| 342 | mutable bool LastResForBeforeTUCheck; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 344 | // SourceManager doesn't support copy construction. |
| 345 | explicit SourceManager(const SourceManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | void operator=(const SourceManager&); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | public: |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 348 | SourceManager(Diagnostic &Diag) |
| 349 | : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 350 | NumBinaryProbes(0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 351 | clearIDTables(); |
| 352 | } |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 353 | ~SourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 355 | void clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 357 | //===--------------------------------------------------------------------===// |
| 358 | // MainFileID creation and querying methods. |
| 359 | //===--------------------------------------------------------------------===// |
| 360 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 361 | /// getMainFileID - Returns the FileID of the main source file. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 362 | FileID getMainFileID() const { return MainFileID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 364 | /// createMainFileID - Create the FileID for the main source file. |
| 365 | FileID createMainFileID(const FileEntry *SourceFile, |
| 366 | SourceLocation IncludePos) { |
| 367 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 368 | MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User); |
| 369 | return MainFileID; |
| 370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 372 | //===--------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 373 | // Methods to create new FileID's and instantiations. |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 374 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | /// createFileID - Create a new FileID that represents the specified file |
| 377 | /// being #included from the specified IncludePosition. This returns 0 on |
| 378 | /// error and translates NULL into standard input. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | /// PreallocateID should be non-zero to specify which a pre-allocated, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 380 | /// lazily computed source location is being filled in by this operation. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 381 | FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 382 | SrcMgr::CharacteristicKind FileCharacter, |
| 383 | unsigned PreallocatedID = 0, |
| 384 | unsigned Offset = 0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 385 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 386 | if (IR == 0) return FileID(); // Error opening file? |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 387 | return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 391 | /// specified memory buffer. This does no caching of the buffer and takes |
| 392 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 393 | FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, |
| 394 | unsigned PreallocatedID = 0, |
| 395 | unsigned Offset = 0) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 396 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 397 | SrcMgr::C_User, PreallocatedID, Offset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 400 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 401 | /// that will represent the FileID for the main source. One example |
| 402 | /// of when this would be used is when the main source is read from STDIN. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 403 | FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 404 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
Chris Lattner | 7697b5c | 2007-12-20 01:38:17 +0000 | [diff] [blame] | 405 | MainFileID = createFileIDForMemBuffer(Buffer); |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 406 | return MainFileID; |
| 407 | } |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 408 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 409 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 410 | /// that a token at Loc should actually be referenced from InstantiationLoc. |
| 411 | /// TokLength is the length of the token being instantiated. |
| 412 | SourceLocation createInstantiationLoc(SourceLocation Loc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 413 | SourceLocation InstantiationLocStart, |
| 414 | SourceLocation InstantiationLocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 415 | unsigned TokLength, |
| 416 | unsigned PreallocatedID = 0, |
| 417 | unsigned Offset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 419 | /// \brief Retrieve the memory buffer associated with the given file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 420 | /// |
| 421 | /// \param Invalid If non-NULL, will be set \c true if an error |
| 422 | /// occurs while retrieving the memory buffer. |
| 423 | const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, |
| 424 | bool *Invalid = 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 425 | |
| 426 | /// \brief Override the contents of the given source file by providing an |
| 427 | /// already-allocated buffer. |
| 428 | /// |
| 429 | /// \param SourceFile the source file whose contents will be override. |
| 430 | /// |
| 431 | /// \param Buffer the memory buffer whose contents will be used as the |
| 432 | /// data in the given source file. |
| 433 | /// |
| 434 | /// \returns true if an error occurred, false otherwise. |
| 435 | bool overrideFileContents(const FileEntry *SourceFile, |
| 436 | const llvm::MemoryBuffer *Buffer); |
| 437 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 438 | //===--------------------------------------------------------------------===// |
| 439 | // FileID manipulation methods. |
| 440 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 2ffb14f | 2009-12-06 09:19:25 +0000 | [diff] [blame] | 442 | /// getBuffer - Return the buffer for the specified FileID. If there is an |
| 443 | /// error opening this buffer the first time, this manufactures a temporary |
| 444 | /// buffer and returns a non-empty error string. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 445 | const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { |
| 446 | return getSLocEntry(FID).getFile().getContentCache()->getBuffer(Diag, |
| 447 | Invalid); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 450 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 451 | const FileEntry *getFileEntryForID(FileID FID) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 452 | return getSLocEntry(FID).getFile().getContentCache()->Entry; |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 455 | /// getBufferData - Return a pointer to the start and end of the source buffer |
| 456 | /// data for the specified FileID. |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 457 | /// |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 458 | /// \param FID The file ID whose contents will be returned. |
| 459 | /// \param Invalid If non-NULL, will be set true if an error occurred. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame^] | 460 | llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const; |
| 461 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 463 | //===--------------------------------------------------------------------===// |
| 464 | // SourceLocation manipulation methods. |
| 465 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 668ab1a | 2009-03-13 01:05:57 +0000 | [diff] [blame] | 467 | /// getFileID - Return the FileID for a SourceLocation. This is a very |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 468 | /// hot method that is used for all SourceManager queries that start with a |
| 469 | /// SourceLocation object. It is responsible for finding the entry in |
| 470 | /// SLocEntryTable which contains the specified location. |
| 471 | /// |
| 472 | FileID getFileID(SourceLocation SpellingLoc) const { |
| 473 | unsigned SLocOffset = SpellingLoc.getOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 475 | // If our one-entry cache covers this offset, just return it. |
| 476 | if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) |
| 477 | return LastFileIDLookup; |
| 478 | |
| 479 | return getFileIDSlow(SLocOffset); |
| 480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 482 | /// getLocForStartOfFile - Return the source location corresponding to the |
| 483 | /// first byte of the specified file. |
| 484 | SourceLocation getLocForStartOfFile(FileID FID) const { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 485 | assert(FID.ID < SLocEntryTable.size() && "FileID out of range"); |
| 486 | assert(getSLocEntry(FID).isFile() && "FileID is not a file"); |
| 487 | unsigned FileOffset = getSLocEntry(FID).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 488 | return SourceLocation::getFileLoc(FileOffset); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 491 | /// getInstantiationLoc - Given a SourceLocation object, return the |
| 492 | /// instantiation location referenced by the ID. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 493 | SourceLocation getInstantiationLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 494 | // Handle the non-mapped case inline, defer to out of line code to handle |
| 495 | // instantiations. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 496 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 497 | return getInstantiationLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 500 | /// getImmediateInstantiationRange - Loc is required to be an instantiation |
| 501 | /// location. Return the start/end of the instantiation information. |
| 502 | std::pair<SourceLocation,SourceLocation> |
| 503 | getImmediateInstantiationRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 505 | /// getInstantiationRange - Given a SourceLocation object, return the |
| 506 | /// range of tokens covered by the instantiation in the ultimate file. |
| 507 | std::pair<SourceLocation,SourceLocation> |
| 508 | getInstantiationRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
| 510 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 511 | /// getSpellingLoc - Given a SourceLocation object, return the spelling |
| 512 | /// location referenced by the ID. This is the place where the characters |
| 513 | /// that make up the lexed token can be found. |
| 514 | SourceLocation getSpellingLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 515 | // Handle the non-mapped case inline, defer to out of line code to handle |
| 516 | // instantiations. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 517 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 518 | return getSpellingLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 521 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 522 | /// spelling location referenced by the ID. This is the first level down |
| 523 | /// towards the place where the characters that make up the lexed token can be |
| 524 | /// found. This should not generally be used by clients. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 526 | |
| 527 | /// getDecomposedLoc - Decompose the specified location into a raw FileID + |
| 528 | /// Offset pair. The first element is the FileID, the second is the |
| 529 | /// offset from the start of the buffer of the location. |
| 530 | std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { |
| 531 | FileID FID = getFileID(Loc); |
| 532 | return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); |
| 533 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 535 | /// getDecomposedInstantiationLoc - Decompose the specified location into a |
| 536 | /// raw FileID + Offset pair. If the location is an instantiation record, |
| 537 | /// walk through it until we find the final location instantiated. |
| 538 | std::pair<FileID, unsigned> |
| 539 | getDecomposedInstantiationLoc(SourceLocation Loc) const { |
| 540 | FileID FID = getFileID(Loc); |
| 541 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 543 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 544 | if (Loc.isFileID()) |
| 545 | return std::make_pair(FID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 547 | return getDecomposedInstantiationLocSlowCase(E, Offset); |
| 548 | } |
| 549 | |
| 550 | /// getDecomposedSpellingLoc - Decompose the specified location into a raw |
| 551 | /// FileID + Offset pair. If the location is an instantiation record, walk |
| 552 | /// through it until we find its spelling record. |
| 553 | std::pair<FileID, unsigned> |
| 554 | getDecomposedSpellingLoc(SourceLocation Loc) const { |
| 555 | FileID FID = getFileID(Loc); |
| 556 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 558 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 559 | if (Loc.isFileID()) |
| 560 | return std::make_pair(FID, Offset); |
| 561 | return getDecomposedSpellingLocSlowCase(E, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 564 | /// getFileOffset - This method returns the offset from the start |
| 565 | /// of the file that the specified SourceLocation represents. This is not very |
| 566 | /// meaningful for a macro ID. |
| 567 | unsigned getFileOffset(SourceLocation SpellingLoc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 568 | return getDecomposedLoc(SpellingLoc).second; |
| 569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
| 571 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 572 | //===--------------------------------------------------------------------===// |
| 573 | // Queries about the code at a SourceLocation. |
| 574 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 576 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 577 | /// in the appropriate spelling MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 578 | /// |
| 579 | /// \param Invalid If non-NULL, will be set \c true if an error occurs. |
| 580 | const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 582 | /// getColumnNumber - Return the column # for the specified file position. |
| 583 | /// This is significantly cheaper to compute than the line number. This |
| 584 | /// returns zero if the column number isn't known. This may only be called on |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 585 | /// a file sloc, so you must choose a spelling or instantiation location |
| 586 | /// before calling this method. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 587 | unsigned getColumnNumber(FileID FID, unsigned FilePos, |
| 588 | bool *Invalid = 0) const; |
| 589 | unsigned getSpellingColumnNumber(SourceLocation Loc, |
| 590 | bool *Invalid = 0) const; |
| 591 | unsigned getInstantiationColumnNumber(SourceLocation Loc, |
| 592 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
| 594 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 595 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 596 | /// for the position indicated. This requires building and caching a table of |
| 597 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 598 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 599 | unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 601 | unsigned getInstantiationLineNumber(SourceLocation Loc, |
| 602 | bool *Invalid = 0) const; |
| 603 | unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 605 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 606 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 607 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 608 | const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 610 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 612 | /// header, or an "implicit extern C" system header. |
| 613 | /// |
| 614 | /// This state can be modified with flags on GNU linemarker directives like: |
| 615 | /// # 4 "foo.h" 3 |
| 616 | /// which changes all source locations in the current file after that to be |
| 617 | /// considered to be from a system header. |
| 618 | SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 620 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 621 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 622 | /// or GNU line marker directives. This provides a view on the data that a |
| 623 | /// user should see in diagnostics, for example. |
| 624 | /// |
| 625 | /// Note that a presumed location is always given as the instantiation point |
| 626 | /// of an instantiation location, not at the spelling location. |
| 627 | PresumedLoc getPresumedLoc(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 629 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 630 | /// the same file. |
| 631 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 632 | return getFileID(Loc1) == getFileID(Loc2); |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 635 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 636 | /// the main file. |
| 637 | bool isFromMainFile(SourceLocation Loc) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 638 | return getFileID(Loc) == getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 641 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 642 | bool isInSystemHeader(SourceLocation Loc) const { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 643 | return getFileCharacteristic(Loc) != SrcMgr::C_User; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 644 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 0d45658 | 2009-06-13 23:31:51 +0000 | [diff] [blame] | 646 | /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" |
| 647 | /// system header. |
| 648 | bool isInExternCSystemHeader(SourceLocation Loc) const { |
| 649 | return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; |
| 650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 652 | //===--------------------------------------------------------------------===// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 653 | // Line Table Manipulation Routines |
| 654 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 656 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | /// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 658 | unsigned getLineTableFilenameID(const char *Ptr, unsigned Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 660 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 661 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 662 | /// unspecified. |
| 663 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 664 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | bool IsFileEntry, bool IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 666 | bool IsSystemHeader, bool IsExternCHeader); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 667 | |
| 668 | /// \brief Determine if the source manager has a line table. |
| 669 | bool hasLineTable() const { return LineTable != 0; } |
| 670 | |
| 671 | /// \brief Retrieve the stored line table. |
| 672 | LineTableInfo &getLineTable(); |
| 673 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 674 | //===--------------------------------------------------------------------===// |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 675 | // Other miscellaneous methods. |
| 676 | //===--------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 677 | |
| 678 | /// \brief Get the source location for the given file:line:col triplet. |
| 679 | /// |
| 680 | /// If the source file is included multiple times, the source location will |
| 681 | /// be based upon the first inclusion. |
| 682 | SourceLocation getLocation(const FileEntry *SourceFile, |
| 683 | unsigned Line, unsigned Col) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 685 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 686 | /// |
| 687 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 688 | bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; |
| 689 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 690 | // Iterators over FileInfos. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 691 | typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> |
| 692 | ::const_iterator fileinfo_iterator; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 693 | fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } |
| 694 | fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 695 | bool hasFileInfo(const FileEntry *File) const { |
| 696 | return FileInfos.find(File) != FileInfos.end(); |
| 697 | } |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | /// PrintStats - Print statistics to stderr. |
| 700 | /// |
| 701 | void PrintStats() const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 703 | unsigned sloc_entry_size() const { return SLocEntryTable.size(); } |
Kovarththanan Rajaratnam | 2d684c2 | 2010-03-15 17:31:29 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 705 | // FIXME: Exposing this is a little gross; what we want is a good way |
| 706 | // to iterate the entries that were not defined in a PCH file (or |
| 707 | // any other external source). |
| 708 | unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 709 | |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 710 | const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const { |
| 711 | assert(ID < SLocEntryTable.size() && "Invalid id"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | if (ExternalSLocEntries && |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 713 | ID < SLocEntryLoaded.size() && |
| 714 | !SLocEntryLoaded[ID]) |
| 715 | ExternalSLocEntries->ReadSLocEntry(ID); |
| 716 | return SLocEntryTable[ID]; |
| 717 | } |
Kovarththanan Rajaratnam | 2d684c2 | 2010-03-15 17:31:29 +0000 | [diff] [blame] | 718 | |
| 719 | const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const { |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 720 | return getSLocEntry(FID.ID); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 723 | unsigned getNextOffset() const { return NextOffset; } |
| 724 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 725 | /// \brief Preallocate some number of source location entries, which |
| 726 | /// will be loaded as needed from the given external source. |
| 727 | void PreallocateSLocEntries(ExternalSLocEntrySource *Source, |
| 728 | unsigned NumSLocEntries, |
| 729 | unsigned NextOffset); |
| 730 | |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 731 | /// \brief Clear out any preallocated source location entries that |
| 732 | /// haven't already been loaded. |
| 733 | void ClearPreallocatedSLocEntries(); |
| 734 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 735 | private: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 736 | /// isOffsetInFileID - Return true if the specified FileID contains the |
| 737 | /// specified SourceLocation offset. This is a very hot method. |
| 738 | inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { |
| 739 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); |
| 740 | // If the entry is after the offset, it can't contain it. |
| 741 | if (SLocOffset < Entry.getOffset()) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 743 | // If this is the last entry than it does. Otherwise, the entry after it |
| 744 | // has to not include it. |
| 745 | if (FID.ID+1 == SLocEntryTable.size()) return true; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 746 | |
| 747 | return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 748 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 750 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 751 | /// include position. This works regardless of whether the ContentCache |
| 752 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 753 | FileID createFileID(const SrcMgr::ContentCache* File, |
| 754 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 755 | SrcMgr::CharacteristicKind DirCharacter, |
| 756 | unsigned PreallocatedID = 0, |
| 757 | unsigned Offset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 759 | const SrcMgr::ContentCache * |
| 760 | getOrCreateContentCache(const FileEntry *SourceFile); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 761 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 762 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 763 | /// memory buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | const SrcMgr::ContentCache* |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 765 | createMemBufferContentCache(const llvm::MemoryBuffer *Buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 767 | FileID getFileIDSlow(unsigned SLocOffset) const; |
| 768 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 769 | SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const; |
| 770 | SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; |
| 771 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 772 | std::pair<FileID, unsigned> |
| 773 | getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | unsigned Offset) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 775 | std::pair<FileID, unsigned> |
| 776 | getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 777 | unsigned Offset) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | }; |
| 779 | |
| 780 | |
| 781 | } // end namespace clang |
| 782 | |
| 783 | #endif |