@@ -67,7 +67,9 @@ namespace SrcMgr {
67
67
// / the SourceLocation's offset from the start of the buffer.
68
68
// / 2. Macro Expansions. These indicate that the logical location is
69
69
// / totally different than the physical location. The logical source
70
- // / location is specified with an explicit SourceLocation object.
70
+ // / location is specified by the IncludeLoc. The physical location is
71
+ // / the FilePos of the token's SourceLocation combined with the FileID
72
+ // / from MacroTokenFileID.
71
73
// /
72
74
struct FileIDInfo {
73
75
enum FileIDType {
@@ -76,7 +78,7 @@ namespace SrcMgr {
76
78
};
77
79
78
80
// / The type of this FileID.
79
- FileIDType IDType : 2 ;
81
+ FileIDType IDType;
80
82
81
83
// / IncludeLoc - The location of the #include that brought in this file.
82
84
// / This SourceLocation object has a FileId of 0 for the main file.
@@ -96,9 +98,9 @@ namespace SrcMgr {
96
98
const InfoRec *Info;
97
99
} NormalBuffer;
98
100
99
- // / MacroTokenLoc - This is the raw encoding of a SourceLocation which
100
- // / indicates the physical location of the macro token.
101
- unsigned MacroTokenLoc ;
101
+ // / MacroTokenFileID - This is the File ID that contains the characters
102
+ // / that make up the expanded token.
103
+ unsigned MacroTokenFileID ;
102
104
} u;
103
105
104
106
// / getNormalBuffer - Return a FileIDInfo object for a normal buffer
@@ -114,13 +116,14 @@ namespace SrcMgr {
114
116
}
115
117
116
118
// / getMacroExpansion - Return a FileID for a macro expansion. IL specifies
117
- // / the instantiation location,
119
+ // / the instantiation location, and MacroFID specifies the FileID that the
120
+ // / token's characters come from.
118
121
static FileIDInfo getMacroExpansion (SourceLocation IL,
119
- SourceLocation TokenLoc ) {
122
+ unsigned MacroFID ) {
120
123
FileIDInfo X;
121
124
X.IDType = MacroExpansion;
122
125
X.IncludeLoc = IL;
123
- X.u .MacroTokenLoc = TokenLoc. getRawEncoding () ;
126
+ X.u .MacroTokenFileID = MacroFID ;
124
127
return X;
125
128
}
126
129
@@ -172,10 +175,15 @@ class SourceManager {
172
175
return createFileID (createMemBufferInfoRec (Buffer), SourceLocation ());
173
176
}
174
177
178
+ // / createFileIDForMacroExp - Return a new FileID for a macro expansion at
179
+ // / SourcePos, where the macro token character came from PhysicalFileID.
180
+ // /
181
+ unsigned createFileIDForMacroExp (SourceLocation SourcePos,
182
+ unsigned PhysicalFileID);
175
183
176
184
// / getBuffer - Return the buffer for the specified FileID.
177
185
// /
178
- const SourceBuffer *getBuffer (unsigned FileID) {
186
+ const SourceBuffer *getBuffer (unsigned FileID) const {
179
187
return getFileInfo (FileID)->Buffer ;
180
188
}
181
189
@@ -187,12 +195,18 @@ class SourceManager {
187
195
}
188
196
189
197
// / getFilePos - This (efficient) method returns the offset from the start of
190
- // / the file that the specified SourceLocation represents.
198
+ // / the file that the specified SourceLocation represents. This returns the
199
+ // / location of the physical character data, not the logical file position.
191
200
unsigned getFilePos (SourceLocation IncludePos) const {
192
- assert (IncludePos.getFileID ()-1 < FileIDs.size () && " Invalid FileID!" );
201
+ const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo (IncludePos.getFileID ());
202
+
203
+ // For Macros, the physical loc is specified by the MacroTokenFileID.
204
+ if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
205
+ FIDInfo = &FileIDs[FIDInfo->u .MacroTokenFileID -1 ];
206
+
193
207
// If this file has been split up into chunks, factor in the chunk number
194
208
// that the FileID references.
195
- unsigned ChunkNo=FileIDs[IncludePos. getFileID ()- 1 ]. getNormalBufferChunkNo ();
209
+ unsigned ChunkNo = FIDInfo-> getNormalBufferChunkNo ();
196
210
return IncludePos.getRawFilePos () +
197
211
(ChunkNo << SourceLocation::FilePosBits);
198
212
}
@@ -237,9 +251,21 @@ class SourceManager {
237
251
// / buffer. This does no caching.
238
252
const SrcMgr::InfoRec *createMemBufferInfoRec (const SourceBuffer *Buffer);
239
253
240
- const SrcMgr::InfoRec * getInfoRec (unsigned FileID) const {
254
+ const SrcMgr::FileIDInfo * getFIDInfo (unsigned FileID) const {
241
255
assert (FileID-1 < FileIDs.size () && " Invalid FileID!" );
242
- return FileIDs[FileID-1 ].getNormalBufferInfo ();
256
+ return &FileIDs[FileID-1 ];
257
+ }
258
+
259
+ // / Return the InfoRec structure for the specified FileID. This is always the
260
+ // / physical reference for the ID.
261
+ const SrcMgr::InfoRec *getInfoRec (unsigned FileID) const {
262
+ const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo (FileID);
263
+
264
+ // For Macros, the physical loc is specified by the MacroTokenFileID.
265
+ if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
266
+ FIDInfo = &FileIDs[FIDInfo->u .MacroTokenFileID -1 ];
267
+
268
+ return FIDInfo->getNormalBufferInfo ();
243
269
}
244
270
245
271
SrcMgr::FileInfo *getFileInfo (unsigned FileID) const {
0 commit comments