blob: 497af0b3dadb40b21262454d9057c3f8096cb372 [file] [log] [blame]
Douglas Gregor4ae8f292010-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 Kremeneke3c20a82011-07-27 18:41:20 +000017#include "llvm/Support/Capacity.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "llvm/Support/ErrorHandling.h"
Douglas Gregor4ae8f292010-03-18 17:52:52 +000019
20using namespace clang;
21
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000022ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
23
Douglas Gregor4ab829c2010-11-01 15:03:47 +000024
25InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
26 InclusionKind Kind,
Chris Lattner5f9e2722011-07-23 10:55:15 +000027 StringRef FileName,
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +000028 bool InQuotes, bool ImportedModule,
29 const FileEntry *File,
Douglas Gregor4ab829c2010-11-01 15:03:47 +000030 SourceRange Range)
31 : PreprocessingDirective(InclusionDirectiveKind, Range),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +000032 InQuotes(InQuotes), Kind(Kind), ImportedModule(ImportedModule), File(File)
Douglas Gregor4ab829c2010-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 Lattner5f9e2722011-07-23 10:55:15 +000038 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregor4ab829c2010-11-01 15:03:47 +000039}
40
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +000041PreprocessingRecord::PreprocessingRecord(SourceManager &SM)
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +000042 : SourceMgr(SM),
Argyrios Kyrtzidis37ed1272012-12-04 07:27:05 +000043 ExternalSource(0) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000044}
45
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +000046/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
James Dennettcd6b34b2012-06-22 05:37:13 +000047/// that source range \p Range encompasses.
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +000048std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
49PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
50 if (Range.isInvalid())
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000051 return std::make_pair(iterator(), iterator());
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +000052
Argyrios Kyrtzidisf226ff92011-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 Kyrtzidisf03b8882012-10-05 00:22:28 +000058 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range);
Argyrios Kyrtzidisf226ff92011-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 Dennettcd6b34b2012-06-22 05:37:13 +000087/// and not from files \#included in the range given at
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000088/// \see getPreprocessedEntitiesInRange.
89bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
90 if (FID.isInvalid())
91 return false;
92
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +000093 int Pos = PPEI.Position;
94 if (Pos < 0) {
95 assert(unsigned(-Pos-1) < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000096 "Out-of bounds loaded preprocessed entity");
97 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +000098 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidisf226ff92011-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 Kyrtzidisf03b8882012-10-05 00:22:28 +0000116 assert(unsigned(Pos) < PreprocessedEntities.size() &&
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000117 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000118 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidisf226ff92011-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 Kyrtzidisf03b8882012-10-05 00:22:28 +0000124std::pair<int, int>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000125PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
126 assert(Range.isValid());
127 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
128
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000129 std::pair<unsigned, unsigned>
130 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000131
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000132 // Check if range spans local entities.
133 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000134 return std::make_pair(Local.first, Local.second);
135
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000136 std::pair<unsigned, unsigned>
137 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000138
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000139 // Check if range spans local entities.
140 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000141 return std::make_pair(Local.first, Local.second);
142
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000143 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000144
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000145 // Check if range spans loaded entities.
146 if (Local.first == Local.second)
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000147 return std::make_pair(int(Loaded.first)-TotalLoaded,
148 int(Loaded.second)-TotalLoaded);
149
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000150 // Range spands loaded and local entities.
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000151 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis2dbaca72011-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 Kramer196e71e2011-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 Kyrtzidis2dbaca72011-09-19 20:40:25 +0000181 SourceLocation LHS = getLoc(L);
182 return SM.isBeforeInTranslationUnit(LHS, RHS);
183 }
184
Benjamin Kramer196e71e2011-09-21 16:58:20 +0000185 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis2dbaca72011-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 Kyrtzidis0e322ff2011-09-19 22:02:08 +0000191 SourceRange Range = PPE->getSourceRange();
192 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis2dbaca72011-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 Kyrtzidis4cd06342011-09-22 21:17:02 +0000203 size_t Count = PreprocessedEntities.size();
204 size_t Half;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000205 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidis4cd06342011-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 Kyrtzidis2dbaca72011-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 Kyrtzidisdb81d382012-03-27 18:47:48 +0000242PreprocessingRecord::PPEntityID
243PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis2502efd2011-09-20 23:27:33 +0000244 assert(Entity);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000245 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000246
247 if (!isa<class InclusionDirective>(Entity)) {
248 assert((PreprocessedEntities.empty() ||
249 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
250 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
251 "a macro directive was encountered out-of-order");
252 PreprocessedEntities.push_back(Entity);
253 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
254 }
255
Argyrios Kyrtzidis209dfbe2011-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 Kyrtzidisdb81d382012-03-27 18:47:48 +0000261 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000262 }
263
Argyrios Kyrtzidisdb81d382012-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:
266 // "#include MACRO(STUFF)".
267
268 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
269
270 // Usually there are few macro expansions when defining the filename, do a
271 // linear search for a few entities.
272 unsigned count = 0;
273 for (pp_iter RI = PreprocessedEntities.end(),
274 Begin = PreprocessedEntities.begin();
275 RI != Begin && count < 4; --RI, ++count) {
276 pp_iter I = RI;
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000277 --I;
278 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
279 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000280 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
281 return getPPEntityID(insertI - PreprocessedEntities.begin(),
282 /*isLoaded=*/false);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000283 }
284 }
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000285
286 // Linear search unsuccessful. Do a binary search.
287 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
288 PreprocessedEntities.end(),
289 BeginLoc,
290 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
291 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
292 return getPPEntityID(insertI - PreprocessedEntities.begin(),
293 /*isLoaded=*/false);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000294}
295
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000296void PreprocessingRecord::SetExternalSource(
Douglas Gregor4c30bb12011-07-21 00:47:40 +0000297 ExternalPreprocessingRecordSource &Source) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000298 assert(!ExternalSource &&
299 "Preprocessing record already has an external source");
300 ExternalSource = &Source;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000301}
302
Douglas Gregor4c30bb12011-07-21 00:47:40 +0000303unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
304 unsigned Result = LoadedPreprocessedEntities.size();
305 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
306 + NumEntities);
307 return Result;
308}
309
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000310void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
311 PPEntityID PPID) {
312 MacroDefinitions[Macro] = PPID;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000313}
314
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000315/// \brief Retrieve the preprocessed entity at the given ID.
316PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000317 if (PPID.ID < 0) {
318 unsigned Index = -PPID.ID - 1;
319 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000320 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000321 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000322 }
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000323
324 if (PPID.ID == 0)
325 return 0;
326 unsigned Index = PPID.ID - 1;
327 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000328 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000329 return PreprocessedEntities[Index];
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000330}
331
332/// \brief Retrieve the loaded preprocessed entity at the given index.
333PreprocessedEntity *
334PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
335 assert(Index < LoadedPreprocessedEntities.size() &&
336 "Out-of bounds loaded preprocessed entity");
337 assert(ExternalSource && "No external source to load from");
338 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
339 if (!Entity) {
340 Entity = ExternalSource->ReadPreprocessedEntity(Index);
341 if (!Entity) // Failed to load.
342 Entity = new (*this)
343 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
344 }
345 return Entity;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000346}
347
Douglas Gregor1b058e82010-03-19 21:58:23 +0000348MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000349 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000350 = MacroDefinitions.find(MI);
351 if (Pos == MacroDefinitions.end())
352 return 0;
353
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000354 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
355 if (Entity->isInvalid())
356 return 0;
357 return cast<MacroDefinition>(Entity);
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000358}
359
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000360void PreprocessingRecord::addMacroExpansion(const Token &Id,
361 const MacroInfo *MI,
362 SourceRange Range) {
Argyrios Kyrtzidise1d43302012-02-25 02:41:16 +0000363 // We don't record nested macro expansions.
364 if (Id.getLocation().isMacroID())
Douglas Gregordca8ee82011-05-06 16:33:08 +0000365 return;
366
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000367 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000368 addPreprocessedEntity(
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000369 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
370 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000371 addPreprocessedEntity(
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000372 new (*this) MacroExpansion(Def, Range));
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000373}
374
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000375void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
376 const MacroInfo *MI) {
377 // This is not actually a macro expansion but record it as a macro reference.
378 if (MI)
379 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
380}
381
382void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
383 const MacroInfo *MI) {
384 // This is not actually a macro expansion but record it as a macro reference.
385 if (MI)
386 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
387}
388
389void PreprocessingRecord::Defined(const Token &MacroNameTok,
390 const MacroInfo *MI) {
391 // This is not actually a macro expansion but record it as a macro reference.
392 if (MI)
393 addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
394}
395
396void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
397 SourceRange Range) {
398 addMacroExpansion(Id, MI, Range);
399}
400
Craig Silverstein2aa92672010-11-19 21:33:15 +0000401void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000402 const MacroInfo *MI) {
403 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
404 MacroDefinition *Def
Argyrios Kyrtzidisde4e0a82011-09-20 22:14:48 +0000405 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000406 MacroDefinitions[MI] = addPreprocessedEntity(Def);
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000407}
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000408
Craig Silverstein2aa92672010-11-19 21:33:15 +0000409void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor1b058e82010-03-19 21:58:23 +0000410 const MacroInfo *MI) {
Benjamin Kramer7e423922012-03-24 18:22:12 +0000411 MacroDefinitions.erase(MI);
Douglas Gregor1b058e82010-03-19 21:58:23 +0000412}
413
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000414void PreprocessingRecord::InclusionDirective(
415 SourceLocation HashLoc,
416 const clang::Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000417 StringRef FileName,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000418 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000419 CharSourceRange FilenameRange,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000420 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000421 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +0000422 StringRef RelativePath,
423 const Module *Imported) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000424 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
425
426 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
427 case tok::pp_include:
428 Kind = InclusionDirective::Include;
429 break;
430
431 case tok::pp_import:
432 Kind = InclusionDirective::Import;
433 break;
434
435 case tok::pp_include_next:
436 Kind = InclusionDirective::IncludeNext;
437 break;
438
439 case tok::pp___include_macros:
440 Kind = InclusionDirective::IncludeMacros;
441 break;
442
443 default:
444 llvm_unreachable("Unknown include directive kind");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000445 }
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000446
447 SourceLocation EndLoc;
448 if (!IsAngled) {
449 EndLoc = FilenameRange.getBegin();
450 } else {
451 EndLoc = FilenameRange.getEnd();
452 if (FilenameRange.isCharRange())
453 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
454 // a token range.
455 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000456 clang::InclusionDirective *ID
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +0000457 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
458 (bool)Imported,
Douglas Gregor4ab829c2010-11-01 15:03:47 +0000459 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000460 addPreprocessedEntity(ID);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000461}
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000462
463size_t PreprocessingRecord::getTotalMemory() const {
464 return BumpAlloc.getTotalMemory()
Ted Kremeneke3c20a82011-07-27 18:41:20 +0000465 + llvm::capacity_in_bytes(MacroDefinitions)
466 + llvm::capacity_in_bytes(PreprocessedEntities)
467 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000468}