blob: d7ebeafdf95c34924bcdffd0e10e10375a0671e1 [file] [log] [blame]
Douglas Gregor065f8d12010-03-18 17:52:52 +00001//===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PreprocessingRecord class, which maintains a record
11// of what occurred during preprocessing, and its helpers.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/Lex/PreprocessingRecord.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Token.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000017#include "llvm/Support/Capacity.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "llvm/Support/ErrorHandling.h"
Douglas Gregor065f8d12010-03-18 17:52:52 +000019
20using namespace clang;
21
Douglas Gregoraae92242010-03-19 21:51:54 +000022ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
23
Douglas Gregorf09b6c92010-11-01 15:03:47 +000024
25InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
26 InclusionKind Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000027 StringRef FileName,
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +000028 bool InQuotes, bool ImportedModule,
29 const FileEntry *File,
Douglas Gregorf09b6c92010-11-01 15:03:47 +000030 SourceRange Range)
31 : PreprocessingDirective(InclusionDirectiveKind, Range),
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +000032 InQuotes(InQuotes), Kind(Kind), ImportedModule(ImportedModule), File(File)
Douglas Gregorf09b6c92010-11-01 15:03:47 +000033{
34 char *Memory
35 = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
36 memcpy(Memory, FileName.data(), FileName.size());
37 Memory[FileName.size()] = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000039}
40
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000041PreprocessingRecord::PreprocessingRecord(SourceManager &SM)
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000042 : SourceMgr(SM),
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000043 ExternalSource(0) {
Douglas Gregoraae92242010-03-19 21:51:54 +000044}
45
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000046/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
James Dennett303d8d42012-06-22 05:37:13 +000047/// that source range \p Range encompasses.
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000048std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
49PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
50 if (Range.isInvalid())
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000051 return std::make_pair(iterator(), iterator());
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000052
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000053 if (CachedRangeQuery.Range == Range) {
54 return std::make_pair(iterator(this, CachedRangeQuery.Result.first),
55 iterator(this, CachedRangeQuery.Result.second));
56 }
57
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000058 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000059
60 CachedRangeQuery.Range = Range;
61 CachedRangeQuery.Result = Res;
62
63 return std::make_pair(iterator(this, Res.first), iterator(this, Res.second));
64}
65
66static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
67 SourceManager &SM) {
68 assert(!FID.isInvalid());
69 if (!PPE)
70 return false;
71
72 SourceLocation Loc = PPE->getSourceRange().getBegin();
73 if (Loc.isInvalid())
74 return false;
75
76 if (SM.isInFileID(SM.getFileLoc(Loc), FID))
77 return true;
78 else
79 return false;
80}
81
82/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
83/// points to is coming from the file \arg FID.
84///
85/// Can be used to avoid implicit deserializations of preallocated
86/// preprocessed entities if we only care about entities of a specific file
James Dennett303d8d42012-06-22 05:37:13 +000087/// and not from files \#included in the range given at
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000088/// \see getPreprocessedEntitiesInRange.
89bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
90 if (FID.isInvalid())
91 return false;
92
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000093 int Pos = PPEI.Position;
94 if (Pos < 0) {
95 assert(unsigned(-Pos-1) < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000096 "Out-of bounds loaded preprocessed entity");
97 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000098 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000099 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
100 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
101
102 // See if the external source can see if the entity is in the file without
103 // deserializing it.
104 llvm::Optional<bool>
105 IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
106 if (IsInFile.hasValue())
107 return IsInFile.getValue();
108
109 // The external source did not provide a definite answer, go and deserialize
110 // the entity to check it.
111 return isPreprocessedEntityIfInFileID(
112 getLoadedPreprocessedEntity(LoadedIndex),
113 FID, SourceMgr);
114 }
115
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000116 assert(unsigned(Pos) < PreprocessedEntities.size() &&
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000117 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000118 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000119 FID, SourceMgr);
120}
121
122/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
123/// that source range \arg R encompasses.
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000124std::pair<int, int>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000125PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
126 assert(Range.isValid());
127 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
128
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000129 std::pair<unsigned, unsigned>
130 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000131
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000132 // Check if range spans local entities.
133 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000134 return std::make_pair(Local.first, Local.second);
135
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000136 std::pair<unsigned, unsigned>
137 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000138
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000139 // Check if range spans local entities.
140 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000141 return std::make_pair(Local.first, Local.second);
142
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000143 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000144
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000145 // Check if range spans loaded entities.
146 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000147 return std::make_pair(int(Loaded.first)-TotalLoaded,
148 int(Loaded.second)-TotalLoaded);
149
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000150 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000151 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000152}
153
154std::pair<unsigned, unsigned>
155PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
156 SourceRange Range) const {
157 if (Range.isInvalid())
158 return std::make_pair(0,0);
159 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
160
161 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
162 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
163 return std::make_pair(Begin, End);
164}
165
166namespace {
167
168template <SourceLocation (SourceRange::*getRangeLoc)() const>
169struct PPEntityComp {
170 const SourceManager &SM;
171
172 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
173
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000174 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
175 SourceLocation LHS = getLoc(L);
176 SourceLocation RHS = getLoc(R);
177 return SM.isBeforeInTranslationUnit(LHS, RHS);
178 }
179
180 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000181 SourceLocation LHS = getLoc(L);
182 return SM.isBeforeInTranslationUnit(LHS, RHS);
183 }
184
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000185 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000186 SourceLocation RHS = getLoc(R);
187 return SM.isBeforeInTranslationUnit(LHS, RHS);
188 }
189
190 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000191 SourceRange Range = PPE->getSourceRange();
192 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000193 }
194};
195
196}
197
198unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
199 SourceLocation Loc) const {
200 if (SourceMgr.isLoadedSourceLocation(Loc))
201 return 0;
202
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000203 size_t Count = PreprocessedEntities.size();
204 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000205 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000206 First = PreprocessedEntities.begin();
207 std::vector<PreprocessedEntity *>::const_iterator I;
208
209 // Do a binary search manually instead of using std::lower_bound because
210 // The end locations of entities may be unordered (when a macro expansion
211 // is inside another macro argument), but for this case it is not important
212 // whether we get the first macro expansion or its containing macro.
213 while (Count > 0) {
214 Half = Count/2;
215 I = First;
216 std::advance(I, Half);
217 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
218 Loc)){
219 First = I;
220 ++First;
221 Count = Count - Half - 1;
222 } else
223 Count = Half;
224 }
225
226 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000227}
228
229unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
230 SourceLocation Loc) const {
231 if (SourceMgr.isLoadedSourceLocation(Loc))
232 return 0;
233
234 std::vector<PreprocessedEntity *>::const_iterator
235 I = std::upper_bound(PreprocessedEntities.begin(),
236 PreprocessedEntities.end(),
237 Loc,
238 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
239 return I - PreprocessedEntities.begin();
240}
241
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000242PreprocessingRecord::PPEntityID
243PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000244 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000245 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000246
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000247 if (isa<MacroDefinition>(Entity)) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000248 assert((PreprocessedEntities.empty() ||
249 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
250 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000251 "a macro definition was encountered out-of-order");
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000252 PreprocessedEntities.push_back(Entity);
253 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
254 }
255
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000256 // Check normal case, this entity begin location is after the previous one.
257 if (PreprocessedEntities.empty() ||
258 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
259 PreprocessedEntities.back()->getSourceRange().getBegin())) {
260 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000261 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000262 }
263
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000264 // The entity's location is not after the previous one; this can happen with
265 // include directives that form the filename using macros, e.g:
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000266 // "#include MACRO(STUFF)"
267 // or with macro expansions inside macro arguments where the arguments are
268 // not expanded in the same order as listed, e.g:
269 // \code
270 // #define M1 1
271 // #define M2 2
272 // #define FM(x,y) y x
273 // FM(M1, M2)
274 // \endcode
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000275
276 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
277
278 // Usually there are few macro expansions when defining the filename, do a
279 // linear search for a few entities.
280 unsigned count = 0;
281 for (pp_iter RI = PreprocessedEntities.end(),
282 Begin = PreprocessedEntities.begin();
283 RI != Begin && count < 4; --RI, ++count) {
284 pp_iter I = RI;
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000285 --I;
286 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
287 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000288 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
289 return getPPEntityID(insertI - PreprocessedEntities.begin(),
290 /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000291 }
292 }
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000293
294 // Linear search unsuccessful. Do a binary search.
295 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
296 PreprocessedEntities.end(),
297 BeginLoc,
298 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
299 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
300 return getPPEntityID(insertI - PreprocessedEntities.begin(),
301 /*isLoaded=*/false);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000302}
303
Douglas Gregoraae92242010-03-19 21:51:54 +0000304void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000305 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000306 assert(!ExternalSource &&
307 "Preprocessing record already has an external source");
308 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000309}
310
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000311unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
312 unsigned Result = LoadedPreprocessedEntities.size();
313 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
314 + NumEntities);
315 return Result;
316}
317
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000318void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
319 PPEntityID PPID) {
320 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000321}
322
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000323/// \brief Retrieve the preprocessed entity at the given ID.
324PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000325 if (PPID.ID < 0) {
326 unsigned Index = -PPID.ID - 1;
327 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000328 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000329 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000330 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000331
332 if (PPID.ID == 0)
333 return 0;
334 unsigned Index = PPID.ID - 1;
335 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000336 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000337 return PreprocessedEntities[Index];
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000338}
339
340/// \brief Retrieve the loaded preprocessed entity at the given index.
341PreprocessedEntity *
342PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
343 assert(Index < LoadedPreprocessedEntities.size() &&
344 "Out-of bounds loaded preprocessed entity");
345 assert(ExternalSource && "No external source to load from");
346 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
347 if (!Entity) {
348 Entity = ExternalSource->ReadPreprocessedEntity(Index);
349 if (!Entity) // Failed to load.
350 Entity = new (*this)
351 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
352 }
353 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000354}
355
Douglas Gregor8aaca672010-03-19 21:58:23 +0000356MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000357 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000358 = MacroDefinitions.find(MI);
359 if (Pos == MacroDefinitions.end())
360 return 0;
361
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000362 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
363 if (Entity->isInvalid())
364 return 0;
365 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000366}
367
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000368void PreprocessingRecord::addMacroExpansion(const Token &Id,
369 const MacroInfo *MI,
370 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000371 // We don't record nested macro expansions.
372 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000373 return;
374
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000375 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000376 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000377 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
378 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000379 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000380 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000381}
382
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000383void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
384 const MacroInfo *MI) {
385 // This is not actually a macro expansion but record it as a macro reference.
386 if (MI)
387 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
388}
389
390void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
391 const MacroInfo *MI) {
392 // This is not actually a macro expansion but record it as a macro reference.
393 if (MI)
394 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
395}
396
397void PreprocessingRecord::Defined(const Token &MacroNameTok,
398 const MacroInfo *MI) {
399 // This is not actually a macro expansion but record it as a macro reference.
400 if (MI)
401 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
402}
403
404void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
405 SourceRange Range) {
406 addMacroExpansion(Id, MI, Range);
407}
408
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000409void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000410 const MacroInfo *MI) {
411 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
412 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000413 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000414 MacroDefinitions[MI] = addPreprocessedEntity(Def);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000415}
Douglas Gregoraae92242010-03-19 21:51:54 +0000416
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000417void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000418 const MacroInfo *MI) {
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +0000419 // Note: MI may be null (when #undef'ining an undefined macro).
420 if (MI)
421 MacroDefinitions.erase(MI);
Douglas Gregor8aaca672010-03-19 21:58:23 +0000422}
423
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000424void PreprocessingRecord::InclusionDirective(
425 SourceLocation HashLoc,
426 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000427 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000428 bool IsAngled,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000429 CharSourceRange FilenameRange,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000430 const FileEntry *File,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000431 StringRef SearchPath,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000432 StringRef RelativePath,
433 const Module *Imported) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000434 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
435
436 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
437 case tok::pp_include:
438 Kind = InclusionDirective::Include;
439 break;
440
441 case tok::pp_import:
442 Kind = InclusionDirective::Import;
443 break;
444
445 case tok::pp_include_next:
446 Kind = InclusionDirective::IncludeNext;
447 break;
448
449 case tok::pp___include_macros:
450 Kind = InclusionDirective::IncludeMacros;
451 break;
452
453 default:
454 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000455 }
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000456
457 SourceLocation EndLoc;
458 if (!IsAngled) {
459 EndLoc = FilenameRange.getBegin();
460 } else {
461 EndLoc = FilenameRange.getEnd();
462 if (FilenameRange.isCharRange())
463 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
464 // a token range.
465 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000466 clang::InclusionDirective *ID
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +0000467 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
468 (bool)Imported,
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000469 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000470 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000471}
Ted Kremenek182543a2011-07-26 21:17:24 +0000472
473size_t PreprocessingRecord::getTotalMemory() const {
474 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000475 + llvm::capacity_in_bytes(MacroDefinitions)
476 + llvm::capacity_in_bytes(PreprocessedEntities)
477 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000478}