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 | |
Chris Lattner | d47d3b0 | 2011-07-23 10:35:09 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LLVM.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Allocator.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/DataTypes.h" |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PointerIntPair.h" |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/PointerUnion.h" |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 26 | #include <map> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include <vector> |
Chris Lattner | 9dc62f0 | 2007-07-12 15:32:57 +0000 | [diff] [blame] | 28 | #include <cassert> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 32 | class DiagnosticsEngine; |
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; |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 37 | class LangOptions; |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 38 | class ASTWriter; |
| 39 | class ASTReader; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 40 | |
Eric Christopher | 29f3942 | 2011-09-08 17:15:01 +0000 | [diff] [blame] | 41 | /// There are three different types of locations in a file: a spelling |
| 42 | /// location, an expansion location, and a presumed location. |
| 43 | /// |
| 44 | /// Given an example of: |
| 45 | /// #define min(x, y) x < y ? x : y |
| 46 | /// |
| 47 | /// and then later on a use of min: |
Eric Christopher | 29f3942 | 2011-09-08 17:15:01 +0000 | [diff] [blame] | 48 | /// #line 17 |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 49 | /// return min(a, b); |
Eric Christopher | 29f3942 | 2011-09-08 17:15:01 +0000 | [diff] [blame] | 50 | /// |
| 51 | /// The expansion location is the line in the source code where the macro |
| 52 | /// was expanded (the return statement), the spelling location is the |
| 53 | /// location in the source where the macro was originally defined, |
| 54 | /// and the presumed location is where the line directive states that |
| 55 | /// the line is 17, or any other line. |
| 56 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 57 | /// SrcMgr - Public enums and private classes that are part of the |
| 58 | /// SourceManager implementation. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | /// |
| 60 | namespace SrcMgr { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 61 | /// CharacteristicKind - This is used to represent whether a file or directory |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 62 | /// holds normal user code, system code, or system code which is implicitly |
| 63 | /// 'extern "C"' in C++ mode. Entire directories can be tagged with this |
| 64 | /// (this is maintained by DirectoryLookup and friends) as can specific |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 65 | /// FileInfos when a #pragma system_header is seen or various other cases. |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 66 | /// |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 67 | enum CharacteristicKind { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 68 | C_User, C_System, C_ExternCSystem |
| 69 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Dan Gohman | 4710a8e | 2010-08-25 21:59:25 +0000 | [diff] [blame] | 71 | /// ContentCache - One instance of this struct is kept for every file |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 72 | /// loaded or used. This object owns the MemoryBuffer object. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 73 | class ContentCache { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 74 | enum CCFlags { |
| 75 | /// \brief Whether the buffer is invalid. |
| 76 | InvalidFlag = 0x01, |
| 77 | /// \brief Whether the buffer should not be freed on destruction. |
| 78 | DoNotFreeFlag = 0x02 |
| 79 | }; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 81 | /// Buffer - The actual buffer containing the characters from the input |
| 82 | /// file. This is owned by the ContentCache object. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 83 | /// The bits indicate indicates whether the buffer is invalid. |
| 84 | mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 85 | |
| 86 | public: |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 87 | /// Reference to the file entry representing this ContentCache. |
| 88 | /// This reference does not own the FileEntry object. |
| 89 | /// It is possible for this to be NULL if |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 90 | /// the ContentCache encapsulates an imaginary text buffer. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 91 | const FileEntry *OrigEntry; |
| 92 | |
| 93 | /// \brief References the file which the contents were actually loaded from. |
| 94 | /// Can be different from 'Entry' if we overridden the contents of one file |
| 95 | /// with the contents of another file. |
| 96 | const FileEntry *ContentsEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 98 | /// SourceLineCache - A bump pointer allocated array of offsets for each |
| 99 | /// source line. This is lazily computed. This is owned by the |
| 100 | /// SourceManager BumpPointerAllocator object. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 101 | unsigned *SourceLineCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 103 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 104 | /// if SourceLineCache is non-null. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | unsigned NumLines; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 106 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 107 | /// getBuffer - Returns the memory buffer for the associated content. |
| 108 | /// |
Jonathan D. Turner | a92d7e7 | 2011-06-16 20:47:21 +0000 | [diff] [blame] | 109 | /// \param Diag Object through which diagnostics will be emitted if the |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 110 | /// buffer cannot be retrieved. |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 111 | /// |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 112 | /// \param Loc If specified, is the location that invalid file diagnostics |
| 113 | /// will be emitted at. |
| 114 | /// |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 115 | /// \param Invalid If non-NULL, will be set \c true if an error occurred. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 116 | const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 117 | const SourceManager &SM, |
| 118 | SourceLocation Loc = SourceLocation(), |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 119 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 121 | /// getSize - Returns the size of the content encapsulated by this |
| 122 | /// ContentCache. This can be the size of the source file or the size of an |
| 123 | /// arbitrary scratch buffer. If the ContentCache encapsulates a source |
| 124 | /// file this size is retrieved from the file's FileEntry. |
| 125 | unsigned getSize() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 127 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 128 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 129 | /// expanded. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 130 | unsigned getSizeBytesMapped() const; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 132 | /// Returns the kind of memory used to back the memory buffer for |
| 133 | /// this content cache. This is used for performance analysis. |
| 134 | llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 136 | void setBuffer(const llvm::MemoryBuffer *B) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 137 | assert(!Buffer.getPointer() && "MemoryBuffer already set."); |
| 138 | Buffer.setPointer(B); |
| 139 | Buffer.setInt(false); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 140 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 142 | /// \brief Get the underlying buffer, returning NULL if the buffer is not |
| 143 | /// yet available. |
| 144 | const llvm::MemoryBuffer *getRawBuffer() const { |
| 145 | return Buffer.getPointer(); |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 148 | /// \brief Replace the existing buffer (which will be deleted) |
| 149 | /// with the given buffer. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 150 | void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 152 | /// \brief Determine whether the buffer itself is invalid. |
| 153 | bool isBufferInvalid() const { |
| 154 | return Buffer.getInt() & InvalidFlag; |
| 155 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 156 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 157 | /// \brief Determine whether the buffer should be freed. |
| 158 | bool shouldFreeBuffer() const { |
| 159 | return (Buffer.getInt() & DoNotFreeFlag) == 0; |
| 160 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 162 | ContentCache(const FileEntry *Ent = 0) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 163 | : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 164 | SourceLineCache(0), NumLines(0) {} |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 165 | |
| 166 | ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) |
| 167 | : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 168 | SourceLineCache(0), NumLines(0) {} |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 169 | |
| 170 | ~ContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 172 | /// The copy ctor does not allow copies where source object has either |
| 173 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 174 | /// is not transferred, so this is a logical error. |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 175 | ContentCache(const ContentCache &RHS) |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 176 | : Buffer(0, false), SourceLineCache(0) |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 177 | { |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 178 | OrigEntry = RHS.OrigEntry; |
| 179 | ContentsEntry = RHS.ContentsEntry; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 180 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 181 | assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 && |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 182 | "Passed ContentCache object cannot own a buffer."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
| 184 | NumLines = RHS.NumLines; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 187 | private: |
| 188 | // Disable assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | ContentCache &operator=(const ContentCache& RHS); |
| 190 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 192 | /// FileInfo - Information about a FileID, basically just the logical file |
| 193 | /// that it represents and include stack information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 195 | /// Each FileInfo has include stack information, indicating where it came |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 196 | /// from. This information encodes the #include chain that a token was |
| 197 | /// expanded from. The main include file has an invalid IncludeLoc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 199 | /// FileInfos contain a "ContentCache *", with the contents of the file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 201 | class FileInfo { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | /// IncludeLoc - The location of the #include that brought in this file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 203 | /// This is an invalid SLOC for the main file (top of the #include chain). |
| 204 | unsigned IncludeLoc; // Really a SourceLocation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 206 | /// \brief Number of FileIDs (files and macros) that were created during |
| 207 | /// preprocessing of this #include, including this SLocEntry. |
| 208 | /// Zero means the preprocessor didn't provide such info for this SLocEntry. |
| 209 | unsigned NumCreatedFIDs; |
| 210 | |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 211 | /// Data - This contains the ContentCache* and the bits indicating the |
| 212 | /// characteristic of the file and whether it has #line info, all bitmangled |
| 213 | /// together. |
| 214 | uintptr_t Data; |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 215 | |
Argyrios Kyrtzidis | 21032df | 2011-08-21 23:49:52 +0000 | [diff] [blame] | 216 | friend class clang::SourceManager; |
| 217 | friend class clang::ASTWriter; |
| 218 | friend class clang::ASTReader; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 219 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 220 | /// get - Return a FileInfo object. |
| 221 | static FileInfo get(SourceLocation IL, const ContentCache *Con, |
| 222 | CharacteristicKind FileCharacter) { |
| 223 | FileInfo X; |
| 224 | X.IncludeLoc = IL.getRawEncoding(); |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 225 | X.NumCreatedFIDs = 0; |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 226 | X.Data = (uintptr_t)Con; |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 227 | assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 228 | assert((unsigned)FileCharacter < 4 && "invalid file character"); |
| 229 | X.Data |= (unsigned)FileCharacter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | return X; |
| 231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 233 | SourceLocation getIncludeLoc() const { |
| 234 | return SourceLocation::getFromRawEncoding(IncludeLoc); |
| 235 | } |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 236 | const ContentCache* getContentCache() const { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 237 | return reinterpret_cast<const ContentCache*>(Data & ~7UL); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 240 | /// getCharacteristic - Return whether this is a system header or not. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | CharacteristicKind getFileCharacteristic() const { |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 242 | return (CharacteristicKind)(Data & 3); |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 244 | |
| 245 | /// hasLineDirectives - Return true if this FileID has #line directives in |
| 246 | /// it. |
| 247 | bool hasLineDirectives() const { return (Data & 4) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 249 | /// setHasLineDirectives - Set the flag that indicates that this FileID has |
| 250 | /// line table entries associated with it. |
| 251 | void setHasLineDirectives() { |
| 252 | Data |= 4; |
| 253 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 254 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 256 | /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where |
| 257 | /// the token was ultimately expanded, and the SpellingLoc - where the actual |
| 258 | /// character data for the token came from. |
| 259 | class ExpansionInfo { |
| 260 | // Really these are all SourceLocations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 262 | /// SpellingLoc - Where the spelling for the token can be found. |
| 263 | unsigned SpellingLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 265 | /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these |
| 266 | /// indicate the start and end of the expansion. In object-like macros, |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 267 | /// these will be the same. In a function-like macro expansion, the start |
| 268 | /// will be the identifier and the end will be the ')'. Finally, in |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 269 | /// macro-argument instantitions, the end will be 'SourceLocation()', an |
| 270 | /// invalid location. |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 271 | unsigned ExpansionLocStart, ExpansionLocEnd; |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 273 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 274 | SourceLocation getSpellingLoc() const { |
| 275 | return SourceLocation::getFromRawEncoding(SpellingLoc); |
| 276 | } |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 277 | SourceLocation getExpansionLocStart() const { |
| 278 | return SourceLocation::getFromRawEncoding(ExpansionLocStart); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 279 | } |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 280 | SourceLocation getExpansionLocEnd() const { |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 281 | SourceLocation EndLoc = |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 282 | SourceLocation::getFromRawEncoding(ExpansionLocEnd); |
| 283 | return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 286 | std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { |
| 287 | return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chandler Carruth | 96d3589 | 2011-07-26 03:03:00 +0000 | [diff] [blame] | 290 | bool isMacroArgExpansion() const { |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 291 | // Note that this needs to return false for default constructed objects. |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 292 | return getExpansionLocStart().isValid() && |
| 293 | SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 296 | /// create - Return a ExpansionInfo for an expansion. Start and End specify |
| 297 | /// the expansion range (where the macro is expanded), and SpellingLoc |
| 298 | /// specifies the spelling location (where the characters from the token |
| 299 | /// come from). All three can refer to normal File SLocs or expansion |
| 300 | /// locations. |
| 301 | static ExpansionInfo create(SourceLocation SpellingLoc, |
| 302 | SourceLocation Start, SourceLocation End) { |
| 303 | ExpansionInfo X; |
| 304 | X.SpellingLoc = SpellingLoc.getRawEncoding(); |
| 305 | X.ExpansionLocStart = Start.getRawEncoding(); |
| 306 | X.ExpansionLocEnd = End.getRawEncoding(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 307 | return X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | } |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 309 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 310 | /// createForMacroArg - Return a special ExpansionInfo for the expansion of |
| 311 | /// a macro argument into a function-like macro's body. ExpansionLoc |
| 312 | /// specifies the expansion location (where the macro is expanded). This |
| 313 | /// doesn't need to be a range because a macro is always expanded at |
| 314 | /// a macro parameter reference, and macro parameters are always exactly |
| 315 | /// one token. SpellingLoc specifies the spelling location (where the |
| 316 | /// characters from the token come from). ExpansionLoc and SpellingLoc can |
| 317 | /// both refer to normal File SLocs or expansion locations. |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 318 | /// |
| 319 | /// Given the code: |
| 320 | /// \code |
| 321 | /// #define F(x) f(x) |
| 322 | /// F(42); |
| 323 | /// \endcode |
| 324 | /// |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 325 | /// When expanding '\c F(42)', the '\c x' would call this with an |
| 326 | /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its |
| 327 | /// location in the definition of '\c F'. |
| 328 | static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, |
| 329 | SourceLocation ExpansionLoc) { |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 330 | // We store an intentionally invalid source location for the end of the |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 331 | // expansion range to mark that this is a macro argument ion rather than |
| 332 | // a normal one. |
| 333 | return create(SpellingLoc, ExpansionLoc, SourceLocation()); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 334 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 337 | /// SLocEntry - This is a discriminated union of FileInfo and |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 338 | /// ExpansionInfo. SourceManager keeps an array of these objects, and |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 339 | /// they are uniquely identified by the FileID datatype. |
| 340 | class SLocEntry { |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 341 | unsigned Offset; // low bit is set for expansion info. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 342 | union { |
| 343 | FileInfo File; |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 344 | ExpansionInfo Expansion; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 345 | }; |
| 346 | public: |
| 347 | unsigned getOffset() const { return Offset >> 1; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 349 | bool isExpansion() const { return Offset & 1; } |
| 350 | bool isFile() const { return !isExpansion(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 352 | const FileInfo &getFile() const { |
| 353 | assert(isFile() && "Not a file SLocEntry!"); |
| 354 | return File; |
| 355 | } |
| 356 | |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 357 | const ExpansionInfo &getExpansion() const { |
| 358 | assert(isExpansion() && "Not a macro expansion SLocEntry!"); |
| 359 | return Expansion; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 360 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 362 | static SLocEntry get(unsigned Offset, const FileInfo &FI) { |
| 363 | SLocEntry E; |
| 364 | E.Offset = Offset << 1; |
| 365 | E.File = FI; |
| 366 | return E; |
| 367 | } |
| 368 | |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 369 | static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 370 | SLocEntry E; |
| 371 | E.Offset = (Offset << 1) | 1; |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 372 | E.Expansion = Expansion; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 373 | return E; |
| 374 | } |
| 375 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | } // end SrcMgr namespace. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 377 | |
| 378 | /// \brief External source of source location entries. |
| 379 | class ExternalSLocEntrySource { |
| 380 | public: |
| 381 | virtual ~ExternalSLocEntrySource(); |
| 382 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 383 | /// \brief Read the source location entry with index ID, which will always be |
| 384 | /// less than -1. |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 385 | /// |
| 386 | /// \returns true if an error occurred that prevented the source-location |
| 387 | /// entry from being loaded. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 388 | virtual bool ReadSLocEntry(int ID) = 0; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 389 | }; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 390 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 391 | |
| 392 | /// IsBeforeInTranslationUnitCache - This class holds the cache used by |
| 393 | /// isBeforeInTranslationUnit. The cache structure is complex enough to be |
| 394 | /// worth breaking out of SourceManager. |
| 395 | class IsBeforeInTranslationUnitCache { |
| 396 | /// L/R QueryFID - These are the FID's of the cached query. If these match up |
| 397 | /// with a subsequent query, the result can be reused. |
| 398 | FileID LQueryFID, RQueryFID; |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 399 | |
| 400 | /// \brief True if LQueryFID was created before RQueryFID. This is used |
| 401 | /// to compare macro expansion locations. |
| 402 | bool IsLQFIDBeforeRQFID; |
| 403 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 404 | /// CommonFID - This is the file found in common between the two #include |
| 405 | /// traces. It is the nearest common ancestor of the #include tree. |
| 406 | FileID CommonFID; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 407 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 408 | /// L/R CommonOffset - This is the offset of the previous query in CommonFID. |
| 409 | /// Usually, this represents the location of the #include for QueryFID, but if |
| 410 | /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a |
| 411 | /// random token in the parent. |
| 412 | unsigned LCommonOffset, RCommonOffset; |
| 413 | public: |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 414 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 415 | /// isCacheValid - Return true if the currently cached values match up with |
| 416 | /// the specified LHS/RHS query. If not, we can't use the cache. |
| 417 | bool isCacheValid(FileID LHS, FileID RHS) const { |
| 418 | return LQueryFID == LHS && RQueryFID == RHS; |
| 419 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 420 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 421 | /// getCachedResult - If the cache is valid, compute the result given the |
| 422 | /// specified offsets in the LHS/RHS FID's. |
| 423 | bool getCachedResult(unsigned LOffset, unsigned ROffset) const { |
| 424 | // If one of the query files is the common file, use the offset. Otherwise, |
| 425 | // use the #include loc in the common file. |
| 426 | if (LQueryFID != CommonFID) LOffset = LCommonOffset; |
| 427 | if (RQueryFID != CommonFID) ROffset = RCommonOffset; |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 428 | |
| 429 | // It is common for multiple macro expansions to be "included" from the same |
| 430 | // location (expansion location), in which case use the order of the FileIDs |
Argyrios Kyrtzidis | 4d1cbcf | 2011-09-19 20:39:51 +0000 | [diff] [blame] | 431 | // to determine which came first. This will also take care the case where |
| 432 | // one of the locations points at the inclusion/expansion point of the other |
| 433 | // in which case its FileID will come before the other. |
| 434 | if (LOffset == ROffset && |
| 435 | (LQueryFID != CommonFID || RQueryFID != CommonFID)) |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 436 | return IsLQFIDBeforeRQFID; |
| 437 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 438 | return LOffset < ROffset; |
| 439 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 440 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 441 | // Set up a new query. |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 442 | void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { |
| 443 | assert(LHS != RHS); |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 444 | LQueryFID = LHS; |
| 445 | RQueryFID = RHS; |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 446 | IsLQFIDBeforeRQFID = isLFIDBeforeRFID; |
| 447 | } |
| 448 | |
| 449 | void clear() { |
| 450 | LQueryFID = RQueryFID = FileID(); |
| 451 | IsLQFIDBeforeRQFID = false; |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 452 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 453 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 454 | void setCommonLoc(FileID commonFID, unsigned lCommonOffset, |
| 455 | unsigned rCommonOffset) { |
| 456 | CommonFID = commonFID; |
| 457 | LCommonOffset = lCommonOffset; |
| 458 | RCommonOffset = rCommonOffset; |
| 459 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 460 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 461 | }; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 463 | /// \brief This class handles loading and caching of source files into memory. |
| 464 | /// |
| 465 | /// This object owns the MemoryBuffer objects for all of the loaded |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | /// files and assigns unique FileID's for each unique #include chain. |
| 467 | /// |
| 468 | /// The SourceManager can be queried for information about SourceLocation |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 469 | /// objects, turning them into either spelling or expansion locations. Spelling |
| 470 | /// locations represent where the bytes corresponding to a token came from and |
| 471 | /// expansion locations represent where the location is in the user's view. In |
| 472 | /// the case of a macro expansion, for example, the spelling location indicates |
| 473 | /// where the expanded token came from and the expansion location specifies |
| 474 | /// where it was expanded. |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 475 | class SourceManager : public llvm::RefCountedBase<SourceManager> { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 476 | /// \brief DiagnosticsEngine object. |
| 477 | DiagnosticsEngine &Diag; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 478 | |
| 479 | FileManager &FileMgr; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 481 | mutable llvm::BumpPtrAllocator ContentCacheAlloc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 484 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 485 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | /// non-null, FileEntry pointers. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 487 | llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 489 | /// \brief True if the ContentCache for files that are overriden by other |
| 490 | /// files, should report the original file name. Defaults to true. |
| 491 | bool OverridenFilesKeepOriginalName; |
| 492 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 493 | /// \brief Files that have been overriden with the contents from another file. |
| 494 | llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; |
| 495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | /// MemBufferInfos - Information about various memory buffers that we have |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 497 | /// read in. All FileEntry* within the stored ContentCache objects are NULL, |
| 498 | /// as they do not refer to a file. |
| 499 | std::vector<SrcMgr::ContentCache*> MemBufferInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 501 | /// \brief The table of SLocEntries that are local to this module. |
| 502 | /// |
| 503 | /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 504 | /// expansion. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 505 | std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 507 | /// \brief The table of SLocEntries that are loaded from other modules. |
| 508 | /// |
| 509 | /// Negative FileIDs are indexes into this table. To get from ID to an index, |
| 510 | /// use (-ID - 2). |
| 511 | std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; |
| 512 | |
| 513 | /// \brief The starting offset of the next local SLocEntry. |
| 514 | /// |
| 515 | /// This is LocalSLocEntryTable.back().Offset + the size of that entry. |
| 516 | unsigned NextLocalOffset; |
| 517 | |
| 518 | /// \brief The starting offset of the latest batch of loaded SLocEntries. |
| 519 | /// |
| 520 | /// This is LoadedSLocEntryTable.back().Offset, except that that entry might |
| 521 | /// not have been loaded, so that value would be unknown. |
| 522 | unsigned CurrentLoadedOffset; |
| 523 | |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 524 | /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset |
| 525 | /// starts at 2^31. |
| 526 | static const unsigned MaxLoadedOffset = 1U << 31U; |
| 527 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 528 | /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable |
| 529 | /// have already been loaded from the external source. |
| 530 | /// |
| 531 | /// Same indexing as LoadedSLocEntryTable. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 532 | std::vector<bool> SLocEntryLoaded; |
| 533 | |
| 534 | /// \brief An external source for source location entries. |
| 535 | ExternalSLocEntrySource *ExternalSLocEntries; |
| 536 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 537 | /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. |
| 538 | /// LastFileIDLookup records the last FileID looked up or created, because it |
| 539 | /// is very common to look up many tokens from the same file. |
| 540 | mutable FileID LastFileIDLookup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 542 | /// LineTable - This holds information for #line directives. It is referenced |
| 543 | /// by indices from SLocEntryTable. |
| 544 | LineTableInfo *LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 546 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 547 | /// method which is used to speedup getLineNumber calls to nearby locations. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 548 | mutable FileID LastLineNoFileIDQuery; |
Chris Lattner | f812a45 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 549 | mutable SrcMgr::ContentCache *LastLineNoContentCache; |
| 550 | mutable unsigned LastLineNoFilePos; |
| 551 | mutable unsigned LastLineNoResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 553 | /// 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] | 554 | FileID MainFileID; |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 555 | |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 556 | /// \brief The file ID for the precompiled preamble there is one. |
| 557 | FileID PreambleFileID; |
| 558 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 559 | // Statistics for -print-stats. |
| 560 | mutable unsigned NumLinearScans, NumBinaryProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 562 | // Cache results for the isBeforeInTranslationUnit method. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 563 | mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 565 | // Cache for the "fake" buffer used for error-recovery purposes. |
| 566 | mutable llvm::MemoryBuffer *FakeBufferForRecovery; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 567 | |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 568 | /// \brief Lazily computed map of macro argument chunks to their expanded |
| 569 | /// source location. |
| 570 | typedef std::map<unsigned, SourceLocation> MacroArgsMap; |
| 571 | |
David Blaikie | 70042f5 | 2011-10-20 01:45:20 +0000 | [diff] [blame] | 572 | mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap; |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 573 | |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 574 | // SourceManager doesn't support copy construction. |
| 575 | explicit SourceManager(const SourceManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | void operator=(const SourceManager&); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | public: |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 578 | SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 579 | ~SourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 581 | void clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 583 | DiagnosticsEngine &getDiagnostics() const { return Diag; } |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 584 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 585 | FileManager &getFileManager() const { return FileMgr; } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 586 | |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 587 | /// \brief Set true if the SourceManager should report the original file name |
| 588 | /// for contents of files that were overriden by other files.Defaults to true. |
| 589 | void setOverridenFilesKeepOriginalName(bool value) { |
| 590 | OverridenFilesKeepOriginalName = value; |
| 591 | } |
| 592 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 593 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 594 | /// that will represent the FileID for the main source. One example |
| 595 | /// of when this would be used is when the main source is read from STDIN. |
| 596 | FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 597 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 598 | MainFileID = createFileIDForMemBuffer(Buffer); |
| 599 | return MainFileID; |
| 600 | } |
| 601 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 602 | //===--------------------------------------------------------------------===// |
| 603 | // MainFileID creation and querying methods. |
| 604 | //===--------------------------------------------------------------------===// |
| 605 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 606 | /// getMainFileID - Returns the FileID of the main source file. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 607 | FileID getMainFileID() const { return MainFileID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 609 | /// createMainFileID - Create the FileID for the main source file. |
Dan Gohman | f155dfa | 2010-08-27 15:44:11 +0000 | [diff] [blame] | 610 | FileID createMainFileID(const FileEntry *SourceFile) { |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 611 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
Dan Gohman | f155dfa | 2010-08-27 15:44:11 +0000 | [diff] [blame] | 612 | MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 613 | return MainFileID; |
| 614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 616 | /// \brief Set the file ID for the precompiled preamble. |
| 617 | void setPreambleFileID(FileID Preamble) { |
| 618 | assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); |
| 619 | PreambleFileID = Preamble; |
Douglas Gregor | 414cb64 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 620 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 621 | |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 622 | /// \brief Get the file ID for the precompiled preamble if there is one. |
| 623 | FileID getPreambleFileID() const { return PreambleFileID; } |
| 624 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 625 | //===--------------------------------------------------------------------===// |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 626 | // Methods to create new FileID's and macro expansions. |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 627 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | /// createFileID - Create a new FileID that represents the specified file |
Peter Collingbourne | d57b7ff | 2011-06-30 16:41:03 +0000 | [diff] [blame] | 630 | /// being #included from the specified IncludePosition. This translates NULL |
| 631 | /// into standard input. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 632 | FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 633 | SrcMgr::CharacteristicKind FileCharacter, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 634 | int LoadedID = 0, unsigned LoadedOffset = 0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 635 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 636 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 637 | return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 641 | /// specified memory buffer. This does no caching of the buffer and takes |
| 642 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 643 | FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, |
Axel Naumann | f453cb9 | 2011-10-31 11:02:24 +0000 | [diff] [blame] | 644 | int LoadedID = 0, unsigned LoadedOffset = 0, |
| 645 | SourceLocation IncludeLoc = SourceLocation()) { |
| 646 | return createFileID(createMemBufferContentCache(Buffer), IncludeLoc, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 647 | SrcMgr::C_User, LoadedID, LoadedOffset); |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 649 | |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 650 | /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the |
| 651 | /// fact that a token from SpellingLoc should actually be referenced from |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 652 | /// ExpansionLoc, and that it represents the expansion of a macro argument |
| 653 | /// into the function-like macro body. |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 654 | SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, |
| 655 | SourceLocation ExpansionLoc, |
| 656 | unsigned TokLength); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 657 | |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 658 | /// createExpansionLoc - Return a new SourceLocation that encodes the fact |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 659 | /// that a token from SpellingLoc should actually be referenced from |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 660 | /// ExpansionLoc. |
| 661 | SourceLocation createExpansionLoc(SourceLocation Loc, |
| 662 | SourceLocation ExpansionLocStart, |
| 663 | SourceLocation ExpansionLocEnd, |
| 664 | unsigned TokLength, |
| 665 | int LoadedID = 0, |
| 666 | unsigned LoadedOffset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 668 | /// \brief Retrieve the memory buffer associated with the given file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 669 | /// |
| 670 | /// \param Invalid If non-NULL, will be set \c true if an error |
| 671 | /// occurs while retrieving the memory buffer. |
| 672 | const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, |
| 673 | bool *Invalid = 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 674 | |
| 675 | /// \brief Override the contents of the given source file by providing an |
| 676 | /// already-allocated buffer. |
| 677 | /// |
Dan Gohman | afbf5f8 | 2010-08-26 02:27:03 +0000 | [diff] [blame] | 678 | /// \param SourceFile the source file whose contents will be overriden. |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 679 | /// |
| 680 | /// \param Buffer the memory buffer whose contents will be used as the |
| 681 | /// data in the given source file. |
| 682 | /// |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 683 | /// \param DoNotFree If true, then the buffer will not be freed when the |
| 684 | /// source manager is destroyed. |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 685 | void overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 686 | const llvm::MemoryBuffer *Buffer, |
| 687 | bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 688 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 689 | /// \brief Override the the given source file with another one. |
| 690 | /// |
| 691 | /// \param SourceFile the source file which will be overriden. |
| 692 | /// |
| 693 | /// \param NewFile the file whose contents will be used as the |
| 694 | /// data instead of the contents of the given source file. |
| 695 | void overrideFileContents(const FileEntry *SourceFile, |
| 696 | const FileEntry *NewFile); |
| 697 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 698 | //===--------------------------------------------------------------------===// |
| 699 | // FileID manipulation methods. |
| 700 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Daniel Dunbar | 2ffb14f | 2009-12-06 09:19:25 +0000 | [diff] [blame] | 702 | /// getBuffer - Return the buffer for the specified FileID. If there is an |
| 703 | /// error opening this buffer the first time, this manufactures a temporary |
| 704 | /// buffer and returns a non-empty error string. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 705 | const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, |
| 706 | bool *Invalid = 0) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 707 | bool MyInvalid = false; |
| 708 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 709 | if (MyInvalid || !Entry.isFile()) { |
| 710 | if (Invalid) |
| 711 | *Invalid = true; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 713 | return getFakeBufferForRecovery(); |
| 714 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 715 | |
| 716 | return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 717 | Invalid); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 720 | const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 721 | bool MyInvalid = false; |
| 722 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 723 | if (MyInvalid || !Entry.isFile()) { |
| 724 | if (Invalid) |
| 725 | *Invalid = true; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 727 | return getFakeBufferForRecovery(); |
| 728 | } |
| 729 | |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 730 | return Entry.getFile().getContentCache()->getBuffer(Diag, *this, |
| 731 | SourceLocation(), |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 732 | Invalid); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 733 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 735 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 736 | const FileEntry *getFileEntryForID(FileID FID) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 737 | bool MyInvalid = false; |
| 738 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 739 | if (MyInvalid || !Entry.isFile()) |
| 740 | return 0; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 742 | return Entry.getFile().getContentCache()->OrigEntry; |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 745 | /// Returns the FileEntry record for the provided SLocEntry. |
| 746 | const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const |
| 747 | { |
| 748 | return sloc.getFile().getContentCache()->OrigEntry; |
| 749 | } |
| 750 | |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 751 | /// getBufferData - Return a StringRef to the source buffer data for the |
| 752 | /// specified FileID. |
| 753 | /// |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 754 | /// \param FID The file ID whose contents will be returned. |
| 755 | /// \param Invalid If non-NULL, will be set true if an error occurred. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 756 | StringRef getBufferData(FileID FID, bool *Invalid = 0) const; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 757 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 758 | /// \brief Get the number of FileIDs (files and macros) that were created |
| 759 | /// during preprocessing of \arg FID, including it. |
| 760 | unsigned getNumCreatedFIDsForFileID(FileID FID) const { |
| 761 | bool Invalid = false; |
| 762 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 763 | if (Invalid || !Entry.isFile()) |
| 764 | return 0; |
| 765 | |
| 766 | return Entry.getFile().NumCreatedFIDs; |
| 767 | } |
| 768 | |
| 769 | /// \brief Set the number of FileIDs (files and macros) that were created |
| 770 | /// during preprocessing of \arg FID, including it. |
| 771 | void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const { |
| 772 | bool Invalid = false; |
| 773 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 774 | if (Invalid || !Entry.isFile()) |
| 775 | return; |
| 776 | |
| 777 | assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!"); |
| 778 | const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs; |
| 779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 781 | //===--------------------------------------------------------------------===// |
| 782 | // SourceLocation manipulation methods. |
| 783 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Chris Lattner | 668ab1a | 2009-03-13 01:05:57 +0000 | [diff] [blame] | 785 | /// getFileID - Return the FileID for a SourceLocation. This is a very |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 786 | /// hot method that is used for all SourceManager queries that start with a |
| 787 | /// SourceLocation object. It is responsible for finding the entry in |
| 788 | /// SLocEntryTable which contains the specified location. |
| 789 | /// |
| 790 | FileID getFileID(SourceLocation SpellingLoc) const { |
| 791 | unsigned SLocOffset = SpellingLoc.getOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 793 | // If our one-entry cache covers this offset, just return it. |
| 794 | if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) |
| 795 | return LastFileIDLookup; |
| 796 | |
| 797 | return getFileIDSlow(SLocOffset); |
| 798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 800 | /// getLocForStartOfFile - Return the source location corresponding to the |
| 801 | /// first byte of the specified file. |
| 802 | SourceLocation getLocForStartOfFile(FileID FID) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 803 | bool Invalid = false; |
| 804 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 805 | if (Invalid || !Entry.isFile()) |
| 806 | return SourceLocation(); |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 808 | unsigned FileOffset = Entry.getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 809 | return SourceLocation::getFileLoc(FileOffset); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 810 | } |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 811 | |
| 812 | /// \brief Return the source location corresponding to the last byte of the |
| 813 | /// specified file. |
| 814 | SourceLocation getLocForEndOfFile(FileID FID) const { |
| 815 | bool Invalid = false; |
| 816 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 817 | if (Invalid || !Entry.isFile()) |
| 818 | return SourceLocation(); |
| 819 | |
| 820 | unsigned FileOffset = Entry.getOffset(); |
| 821 | return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1); |
| 822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 824 | /// \brief Returns the include location if \arg FID is a #include'd file |
| 825 | /// otherwise it returns an invalid location. |
| 826 | SourceLocation getIncludeLoc(FileID FID) const { |
| 827 | bool Invalid = false; |
| 828 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 829 | if (Invalid || !Entry.isFile()) |
| 830 | return SourceLocation(); |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 831 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 832 | return Entry.getFile().getIncludeLoc(); |
| 833 | } |
| 834 | |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 835 | /// getExpansionLoc - Given a SourceLocation object, return the expansion |
| 836 | /// location referenced by the ID. |
| 837 | SourceLocation getExpansionLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 838 | // Handle the non-mapped case inline, defer to out of line code to handle |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 839 | // expansions. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 840 | if (Loc.isFileID()) return Loc; |
Chandler Carruth | f84ef95 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 841 | return getExpansionLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 842 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Argyrios Kyrtzidis | 796dbfb | 2011-10-12 07:07:40 +0000 | [diff] [blame] | 844 | /// \brief Given \arg Loc, if it is a macro location return the expansion |
| 845 | /// location or the spelling location, depending on if it comes from a |
| 846 | /// macro argument or not. |
| 847 | SourceLocation getFileLoc(SourceLocation Loc) const { |
| 848 | if (Loc.isFileID()) return Loc; |
| 849 | return getFileLocSlowCase(Loc); |
| 850 | } |
| 851 | |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 852 | /// getImmediateExpansionRange - Loc is required to be an expansion location. |
| 853 | /// Return the start/end of the expansion information. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 854 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 855 | getImmediateExpansionRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 857 | /// getExpansionRange - Given a SourceLocation object, return the range of |
| 858 | /// tokens covered by the expansion the ultimate file. |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 859 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 860 | getExpansionRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
| 862 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 863 | /// getSpellingLoc - Given a SourceLocation object, return the spelling |
| 864 | /// location referenced by the ID. This is the place where the characters |
| 865 | /// that make up the lexed token can be found. |
| 866 | SourceLocation getSpellingLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 867 | // Handle the non-mapped case inline, defer to out of line code to handle |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 868 | // expansions. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 869 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 870 | return getSpellingLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 871 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 873 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 874 | /// spelling location referenced by the ID. This is the first level down |
| 875 | /// towards the place where the characters that make up the lexed token can be |
| 876 | /// found. This should not generally be used by clients. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 878 | |
| 879 | /// getDecomposedLoc - Decompose the specified location into a raw FileID + |
| 880 | /// Offset pair. The first element is the FileID, the second is the |
| 881 | /// offset from the start of the buffer of the location. |
| 882 | std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { |
| 883 | FileID FID = getFileID(Loc); |
Argyrios Kyrtzidis | a246d27 | 2011-11-04 23:43:06 +0000 | [diff] [blame^] | 884 | bool Invalid = false; |
| 885 | const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid); |
| 886 | if (Invalid) |
| 887 | return std::make_pair(FileID(), 0); |
| 888 | return std::make_pair(FID, Loc.getOffset()-E.getOffset()); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 891 | /// getDecomposedExpansionLoc - Decompose the specified location into a raw |
| 892 | /// FileID + Offset pair. If the location is an expansion record, walk |
| 893 | /// through it until we find the final location expanded. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 894 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 895 | getDecomposedExpansionLoc(SourceLocation Loc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 896 | FileID FID = getFileID(Loc); |
Argyrios Kyrtzidis | a246d27 | 2011-11-04 23:43:06 +0000 | [diff] [blame^] | 897 | bool Invalid = false; |
| 898 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); |
| 899 | if (Invalid) |
| 900 | return std::make_pair(FileID(), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 902 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 903 | if (Loc.isFileID()) |
| 904 | return std::make_pair(FID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 906 | return getDecomposedExpansionLocSlowCase(E); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /// getDecomposedSpellingLoc - Decompose the specified location into a raw |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 910 | /// FileID + Offset pair. If the location is an expansion record, walk |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 911 | /// through it until we find its spelling record. |
| 912 | std::pair<FileID, unsigned> |
| 913 | getDecomposedSpellingLoc(SourceLocation Loc) const { |
| 914 | FileID FID = getFileID(Loc); |
Argyrios Kyrtzidis | a246d27 | 2011-11-04 23:43:06 +0000 | [diff] [blame^] | 915 | bool Invalid = false; |
| 916 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); |
| 917 | if (Invalid) |
| 918 | return std::make_pair(FileID(), 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 920 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 921 | if (Loc.isFileID()) |
| 922 | return std::make_pair(FID, Offset); |
| 923 | return getDecomposedSpellingLocSlowCase(E, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 926 | /// getFileOffset - This method returns the offset from the start |
| 927 | /// of the file that the specified SourceLocation represents. This is not very |
| 928 | /// meaningful for a macro ID. |
| 929 | unsigned getFileOffset(SourceLocation SpellingLoc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 930 | return getDecomposedLoc(SpellingLoc).second; |
| 931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Chandler Carruth | 96d3589 | 2011-07-26 03:03:00 +0000 | [diff] [blame] | 933 | /// isMacroArgExpansion - This method tests whether the given source location |
| 934 | /// represents a macro argument's expansion into the function-like macro |
| 935 | /// definition. Such source locations only appear inside of the expansion |
| 936 | /// locations representing where a particular function-like macro was |
| 937 | /// expanded. |
| 938 | bool isMacroArgExpansion(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
Argyrios Kyrtzidis | 499ea55 | 2011-08-23 21:02:38 +0000 | [diff] [blame] | 940 | /// \brief Returns true if \arg Loc is inside the [\arg Start, +\arg Length) |
| 941 | /// chunk of the source location address space. |
| 942 | /// If it's true and \arg RelativeOffset is non-null, it will be set to the |
| 943 | /// relative offset of \arg Loc inside the chunk. |
| 944 | bool isInSLocAddrSpace(SourceLocation Loc, |
| 945 | SourceLocation Start, unsigned Length, |
| 946 | unsigned *RelativeOffset = 0) const { |
| 947 | assert(((Start.getOffset() < NextLocalOffset && |
| 948 | Start.getOffset()+Length <= NextLocalOffset) || |
| 949 | (Start.getOffset() >= CurrentLoadedOffset && |
| 950 | Start.getOffset()+Length < MaxLoadedOffset)) && |
| 951 | "Chunk is not valid SLoc address space"); |
| 952 | unsigned LocOffs = Loc.getOffset(); |
| 953 | unsigned BeginOffs = Start.getOffset(); |
| 954 | unsigned EndOffs = BeginOffs + Length; |
| 955 | if (LocOffs >= BeginOffs && LocOffs < EndOffs) { |
| 956 | if (RelativeOffset) |
| 957 | *RelativeOffset = LocOffs - BeginOffs; |
| 958 | return true; |
| 959 | } |
| 960 | |
| 961 | return false; |
| 962 | } |
| 963 | |
Argyrios Kyrtzidis | b6c465e | 2011-08-23 21:02:41 +0000 | [diff] [blame] | 964 | /// \brief Return true if both \arg LHS and \arg RHS are in the local source |
| 965 | /// location address space or the loaded one. If it's true and |
| 966 | /// \arg RelativeOffset is non-null, it will be set to the offset of \arg RHS |
| 967 | /// relative to \arg LHS. |
| 968 | bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, |
| 969 | int *RelativeOffset) const { |
| 970 | unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); |
| 971 | bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; |
| 972 | bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; |
| 973 | |
| 974 | if (LHSLoaded == RHSLoaded) { |
| 975 | if (RelativeOffset) |
| 976 | *RelativeOffset = RHSOffs - LHSOffs; |
| 977 | return true; |
| 978 | } |
| 979 | |
| 980 | return false; |
| 981 | } |
| 982 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 983 | //===--------------------------------------------------------------------===// |
| 984 | // Queries about the code at a SourceLocation. |
| 985 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 988 | /// in the appropriate spelling MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 989 | /// |
| 990 | /// \param Invalid If non-NULL, will be set \c true if an error occurs. |
| 991 | const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 993 | /// getColumnNumber - Return the column # for the specified file position. |
| 994 | /// This is significantly cheaper to compute than the line number. This |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 995 | /// returns zero if the column number isn't known. This may only be called |
| 996 | /// on a file sloc, so you must choose a spelling or expansion location |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 997 | /// before calling this method. |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 998 | unsigned getColumnNumber(FileID FID, unsigned FilePos, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 999 | bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1000 | unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 1001 | unsigned getExpansionColumnNumber(SourceLocation Loc, |
Chandler Carruth | b49dcd2 | 2011-07-25 20:59:15 +0000 | [diff] [blame] | 1002 | bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1003 | unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
| 1005 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 1006 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | /// for the position indicated. This requires building and caching a table of |
| 1008 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 1009 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1010 | unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1011 | unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 1012 | unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1013 | unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1015 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 1016 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 1017 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1018 | const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1020 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1022 | /// header, or an "implicit extern C" system header. |
| 1023 | /// |
| 1024 | /// This state can be modified with flags on GNU linemarker directives like: |
| 1025 | /// # 4 "foo.h" 3 |
| 1026 | /// which changes all source locations in the current file after that to be |
| 1027 | /// considered to be from a system header. |
| 1028 | SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1030 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 1031 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 1032 | /// or GNU line marker directives. This provides a view on the data that a |
| 1033 | /// user should see in diagnostics, for example. |
| 1034 | /// |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1035 | /// Note that a presumed location is always given as the expansion point of |
| 1036 | /// an expansion location, not at the spelling location. |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 1037 | /// |
| 1038 | /// \returns The presumed location of the specified SourceLocation. If the |
| 1039 | /// presumed location cannot be calculate (e.g., because \p Loc is invalid |
| 1040 | /// or the file containing \p Loc has changed on disk), returns an invalid |
| 1041 | /// presumed location. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1042 | PresumedLoc getPresumedLoc(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 1044 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 1045 | /// the same file. |
| 1046 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 1047 | return getFileID(Loc1) == getFileID(Loc2); |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 1048 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 1050 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 1051 | /// the main file. |
| 1052 | bool isFromMainFile(SourceLocation Loc) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 1053 | return getFileID(Loc) == getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1056 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 1057 | bool isInSystemHeader(SourceLocation Loc) const { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1058 | return getFileCharacteristic(Loc) != SrcMgr::C_User; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 0d45658 | 2009-06-13 23:31:51 +0000 | [diff] [blame] | 1061 | /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" |
| 1062 | /// system header. |
| 1063 | bool isInExternCSystemHeader(SourceLocation Loc) const { |
| 1064 | return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; |
| 1065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Argyrios Kyrtzidis | 54232ad | 2011-08-19 22:34:01 +0000 | [diff] [blame] | 1067 | /// \brief The size of the SLocEnty that \arg FID represents. |
Argyrios Kyrtzidis | 984e42c | 2011-08-23 21:02:28 +0000 | [diff] [blame] | 1068 | unsigned getFileIDSize(FileID FID) const; |
Argyrios Kyrtzidis | 54232ad | 2011-08-19 22:34:01 +0000 | [diff] [blame] | 1069 | |
Argyrios Kyrtzidis | d60a34a | 2011-08-19 22:34:17 +0000 | [diff] [blame] | 1070 | /// \brief Given a specific FileID, returns true if \arg Loc is inside that |
| 1071 | /// FileID chunk and sets relative offset (offset of \arg Loc from beginning |
| 1072 | /// of FileID) to \arg relativeOffset. |
| 1073 | bool isInFileID(SourceLocation Loc, FileID FID, |
| 1074 | unsigned *RelativeOffset = 0) const { |
Argyrios Kyrtzidis | d7cb46c | 2011-08-23 21:02:45 +0000 | [diff] [blame] | 1075 | unsigned Offs = Loc.getOffset(); |
| 1076 | if (isOffsetInFileID(FID, Offs)) { |
| 1077 | if (RelativeOffset) |
| 1078 | *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); |
| 1079 | return true; |
| 1080 | } |
Argyrios Kyrtzidis | d60a34a | 2011-08-19 22:34:17 +0000 | [diff] [blame] | 1081 | |
Argyrios Kyrtzidis | d7cb46c | 2011-08-23 21:02:45 +0000 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
Argyrios Kyrtzidis | 469244a | 2011-05-28 03:56:11 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 1085 | //===--------------------------------------------------------------------===// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1086 | // Line Table Manipulation Routines |
| 1087 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1089 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | /// |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 1091 | unsigned getLineTableFilenameID(StringRef Str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 1093 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 1094 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 1095 | /// unspecified. |
| 1096 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 1097 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | bool IsFileEntry, bool IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 1099 | bool IsSystemHeader, bool IsExternCHeader); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1100 | |
| 1101 | /// \brief Determine if the source manager has a line table. |
| 1102 | bool hasLineTable() const { return LineTable != 0; } |
| 1103 | |
| 1104 | /// \brief Retrieve the stored line table. |
| 1105 | LineTableInfo &getLineTable(); |
| 1106 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 1107 | //===--------------------------------------------------------------------===// |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 1108 | // Queries for performance analysis. |
| 1109 | //===--------------------------------------------------------------------===// |
| 1110 | |
| 1111 | /// Return the total amount of physical memory allocated by the |
| 1112 | /// ContentCache allocator. |
| 1113 | size_t getContentCacheSize() const { |
| 1114 | return ContentCacheAlloc.getTotalMemory(); |
| 1115 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1116 | |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 1117 | struct MemoryBufferSizes { |
| 1118 | const size_t malloc_bytes; |
| 1119 | const size_t mmap_bytes; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1120 | |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 1121 | MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) |
| 1122 | : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} |
| 1123 | }; |
| 1124 | |
| 1125 | /// Return the amount of memory used by memory buffers, breaking down |
| 1126 | /// by heap-backed versus mmap'ed memory. |
| 1127 | MemoryBufferSizes getMemoryBufferSizes() const; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1128 | |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 1129 | // Return the amount of memory used for various side tables and |
| 1130 | // data structures in the SourceManager. |
| 1131 | size_t getDataStructureSizes() const; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 1132 | |
| 1133 | //===--------------------------------------------------------------------===// |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 1134 | // Other miscellaneous methods. |
| 1135 | //===--------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1136 | |
| 1137 | /// \brief Get the source location for the given file:line:col triplet. |
| 1138 | /// |
| 1139 | /// If the source file is included multiple times, the source location will |
| 1140 | /// be based upon the first inclusion. |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1141 | SourceLocation translateFileLineCol(const FileEntry *SourceFile, |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1142 | unsigned Line, unsigned Col) const; |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1143 | |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1144 | /// \brief Get the FileID for the given file. |
| 1145 | /// |
| 1146 | /// If the source file is included multiple times, the FileID will be the |
| 1147 | /// first inclusion. |
| 1148 | FileID translateFile(const FileEntry *SourceFile) const; |
| 1149 | |
Argyrios Kyrtzidis | efa2ff8 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1150 | /// \brief Get the source location in \arg FID for the given line:col. |
| 1151 | /// Returns null location if \arg FID is not a file SLocEntry. |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1152 | SourceLocation translateLineCol(FileID FID, |
| 1153 | unsigned Line, unsigned Col) const; |
Argyrios Kyrtzidis | efa2ff8 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1154 | |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1155 | /// \brief If \arg Loc points inside a function macro argument, the returned |
| 1156 | /// location will be the macro location in which the argument was expanded. |
| 1157 | /// If a macro argument is used multiple times, the expanded location will |
| 1158 | /// be at the first expansion of the argument. |
| 1159 | /// e.g. |
| 1160 | /// MY_MACRO(foo); |
| 1161 | /// ^ |
| 1162 | /// Passing a file location pointing at 'foo', will yield a macro location |
| 1163 | /// where 'foo' was expanded into. |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1164 | SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1166 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 1167 | /// |
| 1168 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 1169 | bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; |
| 1170 | |
Argyrios Kyrtzidis | aec230d | 2011-09-01 20:53:18 +0000 | [diff] [blame] | 1171 | /// \brief Comparison function class. |
| 1172 | class LocBeforeThanCompare : public std::binary_function<SourceLocation, |
| 1173 | SourceLocation, bool> { |
| 1174 | SourceManager &SM; |
| 1175 | |
| 1176 | public: |
| 1177 | explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { } |
| 1178 | |
| 1179 | bool operator()(SourceLocation LHS, SourceLocation RHS) const { |
| 1180 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 1181 | } |
| 1182 | }; |
| 1183 | |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 1184 | /// \brief Determines the order of 2 source locations in the "source location |
| 1185 | /// address space". |
Argyrios Kyrtzidis | 5d579e7 | 2011-08-23 21:02:35 +0000 | [diff] [blame] | 1186 | bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { |
| 1187 | return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /// \brief Determines the order of a source location and a source location |
| 1191 | /// offset in the "source location address space". |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1192 | /// |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1193 | /// Note that we always consider source locations loaded from |
Argyrios Kyrtzidis | 5d579e7 | 2011-08-23 21:02:35 +0000 | [diff] [blame] | 1194 | bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1195 | unsigned LHSOffset = LHS.getOffset(); |
| 1196 | bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; |
| 1197 | bool RHSLoaded = RHS >= CurrentLoadedOffset; |
| 1198 | if (LHSLoaded == RHSLoaded) |
Argyrios Kyrtzidis | 5d579e7 | 2011-08-23 21:02:35 +0000 | [diff] [blame] | 1199 | return LHSOffset < RHS; |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1201 | return LHSLoaded; |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1204 | // Iterators over FileInfos. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1205 | typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> |
| 1206 | ::const_iterator fileinfo_iterator; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1207 | fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } |
| 1208 | fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1209 | bool hasFileInfo(const FileEntry *File) const { |
| 1210 | return FileInfos.find(File) != FileInfos.end(); |
| 1211 | } |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | /// PrintStats - Print statistics to stderr. |
| 1214 | /// |
| 1215 | void PrintStats() const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1217 | /// \brief Get the number of local SLocEntries we have. |
| 1218 | unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1219 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1220 | /// \brief Get a local SLocEntry. This is exposed for indexing. |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1221 | const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1222 | bool *Invalid = 0) const { |
| 1223 | assert(Index < LocalSLocEntryTable.size() && "Invalid index"); |
| 1224 | return LocalSLocEntryTable[Index]; |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1225 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1226 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1227 | /// \brief Get the number of loaded SLocEntries we have. |
| 1228 | unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1230 | /// \brief Get a loaded SLocEntry. This is exposed for indexing. |
David Blaikie | 70042f5 | 2011-10-20 01:45:20 +0000 | [diff] [blame] | 1231 | const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, |
| 1232 | bool *Invalid = 0) const { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1233 | assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); |
| 1234 | if (!SLocEntryLoaded[Index]) |
| 1235 | ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2)); |
| 1236 | return LoadedSLocEntryTable[Index]; |
| 1237 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1239 | const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { |
Argyrios Kyrtzidis | c705d25 | 2011-10-18 21:59:54 +0000 | [diff] [blame] | 1240 | if (FID.ID == 0 || FID.ID == -1) { |
| 1241 | if (Invalid) *Invalid = true; |
| 1242 | return LocalSLocEntryTable[0]; |
| 1243 | } |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1244 | return getSLocEntryByID(FID.ID); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1247 | unsigned getNextLocalOffset() const { return NextLocalOffset; } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1248 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1249 | void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { |
| 1250 | assert(LoadedSLocEntryTable.empty() && |
| 1251 | "Invalidating existing loaded entries"); |
| 1252 | ExternalSLocEntries = Source; |
| 1253 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1255 | /// \brief Allocate a number of loaded SLocEntries, which will be actually |
| 1256 | /// loaded on demand from the external source. |
| 1257 | /// |
| 1258 | /// NumSLocEntries will be allocated, which occupy a total of TotalSize space |
| 1259 | /// in the global source view. The lowest ID and the base offset of the |
| 1260 | /// entries will be returned. |
| 1261 | std::pair<int, unsigned> |
| 1262 | AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1263 | |
Argyrios Kyrtzidis | aa6edae | 2011-09-19 20:40:05 +0000 | [diff] [blame] | 1264 | /// \brief Returns true if \arg Loc came from a PCH/Module. |
| 1265 | bool isLoadedSourceLocation(SourceLocation Loc) const { |
| 1266 | return Loc.getOffset() >= CurrentLoadedOffset; |
| 1267 | } |
| 1268 | |
| 1269 | /// \brief Returns true if \arg Loc did not come from a PCH/Module. |
| 1270 | bool isLocalSourceLocation(SourceLocation Loc) const { |
| 1271 | return Loc.getOffset() < NextLocalOffset; |
| 1272 | } |
| 1273 | |
Argyrios Kyrtzidis | 7186991 | 2011-10-31 07:20:03 +0000 | [diff] [blame] | 1274 | /// \brief Returns true if \arg FID came from a PCH/Module. |
| 1275 | bool isLoadedFileID(FileID FID) const { |
| 1276 | assert(FID.ID != -1 && "Using FileID sentinel value"); |
| 1277 | return FID.ID < 0; |
| 1278 | } |
| 1279 | |
| 1280 | /// \brief Returns true if \arg FID did not come from a PCH/Module. |
| 1281 | bool isLocalFileID(FileID FID) const { |
| 1282 | return !isLoadedFileID(FID); |
| 1283 | } |
| 1284 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1285 | private: |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1286 | const llvm::MemoryBuffer *getFakeBufferForRecovery() const; |
| 1287 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1288 | /// \brief Get the entry with the given unwrapped FileID. |
| 1289 | const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { |
| 1290 | assert(ID != -1 && "Using FileID sentinel value"); |
| 1291 | if (ID < 0) |
| 1292 | return getLoadedSLocEntryByID(ID); |
| 1293 | return getLocalSLocEntry(static_cast<unsigned>(ID)); |
| 1294 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1296 | const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const { |
| 1297 | return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2)); |
| 1298 | } |
Eric Christopher | 5330ee0 | 2011-09-08 23:28:19 +0000 | [diff] [blame] | 1299 | |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 1300 | /// createExpansionLoc - Implements the common elements of storing an |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1301 | /// expansion info struct into the SLocEntry table and producing a source |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1302 | /// location that refers to it. |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 1303 | SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 1304 | unsigned TokLength, |
| 1305 | int LoadedID = 0, |
| 1306 | unsigned LoadedOffset = 0); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1308 | /// isOffsetInFileID - Return true if the specified FileID contains the |
| 1309 | /// specified SourceLocation offset. This is a very hot method. |
| 1310 | inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { |
| 1311 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); |
| 1312 | // If the entry is after the offset, it can't contain it. |
| 1313 | if (SLocOffset < Entry.getOffset()) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1315 | // If this is the very last entry then it does. |
| 1316 | if (FID.ID == -2) |
| 1317 | return true; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1318 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1319 | // If it is the last local entry, then it does if the location is local. |
| 1320 | if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) { |
| 1321 | return SLocOffset < NextLocalOffset; |
| 1322 | } |
| 1323 | |
| 1324 | // Otherwise, the entry after it has to not include it. This works for both |
| 1325 | // local and loaded entries. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1326 | return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1329 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 1330 | /// include position. This works regardless of whether the ContentCache |
| 1331 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1332 | FileID createFileID(const SrcMgr::ContentCache* File, |
| 1333 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1334 | SrcMgr::CharacteristicKind DirCharacter, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1335 | int LoadedID, unsigned LoadedOffset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1337 | const SrcMgr::ContentCache * |
| 1338 | getOrCreateContentCache(const FileEntry *SourceFile); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 1339 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1340 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 1341 | /// memory buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | const SrcMgr::ContentCache* |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1343 | createMemBufferContentCache(const llvm::MemoryBuffer *Buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1345 | FileID getFileIDSlow(unsigned SLocOffset) const; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1346 | FileID getFileIDLocal(unsigned SLocOffset) const; |
| 1347 | FileID getFileIDLoaded(unsigned SLocOffset) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1348 | |
Chandler Carruth | f84ef95 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 1349 | SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 1350 | SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; |
Argyrios Kyrtzidis | 796dbfb | 2011-10-12 07:07:40 +0000 | [diff] [blame] | 1351 | SourceLocation getFileLocSlowCase(SourceLocation Loc) const; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1353 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1354 | getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1355 | std::pair<FileID, unsigned> |
| 1356 | getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 1357 | unsigned Offset) const; |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1358 | void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; |
Argyrios Kyrtzidis | ac1ffcc | 2011-09-19 20:39:54 +0000 | [diff] [blame] | 1359 | |
| 1360 | friend class ASTReader; |
| 1361 | friend class ASTWriter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | }; |
| 1363 | |
| 1364 | |
| 1365 | } // end namespace clang |
| 1366 | |
| 1367 | #endif |