blob: db5a9f93cf0378da8332b5c01616ce605b8a017e [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) {
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +000095 if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
96 assert(0 && "Out-of bounds loaded preprocessed entity");
97 return false;
98 }
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000099 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000100 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000101 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
102 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
103
104 // See if the external source can see if the entity is in the file without
105 // deserializing it.
David Blaikie05785d12013-02-20 22:23:23 +0000106 Optional<bool> IsInFile =
107 ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000108 if (IsInFile.hasValue())
109 return IsInFile.getValue();
110
111 // The external source did not provide a definite answer, go and deserialize
112 // the entity to check it.
113 return isPreprocessedEntityIfInFileID(
114 getLoadedPreprocessedEntity(LoadedIndex),
115 FID, SourceMgr);
116 }
117
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +0000118 if (unsigned(Pos) >= PreprocessedEntities.size()) {
119 assert(0 && "Out-of bounds local preprocessed entity");
120 return false;
121 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000122 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000123 FID, SourceMgr);
124}
125
126/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
127/// that source range \arg R encompasses.
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000128std::pair<int, int>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000129PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
130 assert(Range.isValid());
131 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
132
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000133 std::pair<unsigned, unsigned>
134 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000135
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000136 // Check if range spans local entities.
137 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000138 return std::make_pair(Local.first, Local.second);
139
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000140 std::pair<unsigned, unsigned>
141 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000142
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000143 // Check if range spans local entities.
144 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000145 return std::make_pair(Local.first, Local.second);
146
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000147 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000148
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000149 // Check if range spans loaded entities.
150 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000151 return std::make_pair(int(Loaded.first)-TotalLoaded,
152 int(Loaded.second)-TotalLoaded);
153
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000154 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000155 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000156}
157
158std::pair<unsigned, unsigned>
159PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
160 SourceRange Range) const {
161 if (Range.isInvalid())
162 return std::make_pair(0,0);
163 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
164
165 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
166 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
167 return std::make_pair(Begin, End);
168}
169
170namespace {
171
172template <SourceLocation (SourceRange::*getRangeLoc)() const>
173struct PPEntityComp {
174 const SourceManager &SM;
175
176 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
177
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000178 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
179 SourceLocation LHS = getLoc(L);
180 SourceLocation RHS = getLoc(R);
181 return SM.isBeforeInTranslationUnit(LHS, RHS);
182 }
183
184 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000185 SourceLocation LHS = getLoc(L);
186 return SM.isBeforeInTranslationUnit(LHS, RHS);
187 }
188
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000189 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000190 SourceLocation RHS = getLoc(R);
191 return SM.isBeforeInTranslationUnit(LHS, RHS);
192 }
193
194 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000195 SourceRange Range = PPE->getSourceRange();
196 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000197 }
198};
199
200}
201
202unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
203 SourceLocation Loc) const {
204 if (SourceMgr.isLoadedSourceLocation(Loc))
205 return 0;
206
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000207 size_t Count = PreprocessedEntities.size();
208 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000209 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000210 First = PreprocessedEntities.begin();
211 std::vector<PreprocessedEntity *>::const_iterator I;
212
213 // Do a binary search manually instead of using std::lower_bound because
214 // The end locations of entities may be unordered (when a macro expansion
215 // is inside another macro argument), but for this case it is not important
216 // whether we get the first macro expansion or its containing macro.
217 while (Count > 0) {
218 Half = Count/2;
219 I = First;
220 std::advance(I, Half);
221 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
222 Loc)){
223 First = I;
224 ++First;
225 Count = Count - Half - 1;
226 } else
227 Count = Half;
228 }
229
230 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000231}
232
233unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
234 SourceLocation Loc) const {
235 if (SourceMgr.isLoadedSourceLocation(Loc))
236 return 0;
237
238 std::vector<PreprocessedEntity *>::const_iterator
239 I = std::upper_bound(PreprocessedEntities.begin(),
240 PreprocessedEntities.end(),
241 Loc,
242 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
243 return I - PreprocessedEntities.begin();
244}
245
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000246PreprocessingRecord::PPEntityID
247PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000248 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000249 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000250
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000251 if (isa<MacroDefinition>(Entity)) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000252 assert((PreprocessedEntities.empty() ||
253 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
254 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000255 "a macro definition was encountered out-of-order");
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000256 PreprocessedEntities.push_back(Entity);
257 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
258 }
259
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000260 // Check normal case, this entity begin location is after the previous one.
261 if (PreprocessedEntities.empty() ||
262 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
263 PreprocessedEntities.back()->getSourceRange().getBegin())) {
264 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000265 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000266 }
267
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000268 // The entity's location is not after the previous one; this can happen with
269 // include directives that form the filename using macros, e.g:
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000270 // "#include MACRO(STUFF)"
271 // or with macro expansions inside macro arguments where the arguments are
272 // not expanded in the same order as listed, e.g:
273 // \code
274 // #define M1 1
275 // #define M2 2
276 // #define FM(x,y) y x
277 // FM(M1, M2)
278 // \endcode
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000279
280 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
281
282 // Usually there are few macro expansions when defining the filename, do a
283 // linear search for a few entities.
284 unsigned count = 0;
285 for (pp_iter RI = PreprocessedEntities.end(),
286 Begin = PreprocessedEntities.begin();
287 RI != Begin && count < 4; --RI, ++count) {
288 pp_iter I = RI;
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000289 --I;
290 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
291 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000292 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
293 return getPPEntityID(insertI - PreprocessedEntities.begin(),
294 /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000295 }
296 }
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000297
298 // Linear search unsuccessful. Do a binary search.
299 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
300 PreprocessedEntities.end(),
301 BeginLoc,
302 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
303 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
304 return getPPEntityID(insertI - PreprocessedEntities.begin(),
305 /*isLoaded=*/false);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000306}
307
Douglas Gregoraae92242010-03-19 21:51:54 +0000308void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000309 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000310 assert(!ExternalSource &&
311 "Preprocessing record already has an external source");
312 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000313}
314
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000315unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
316 unsigned Result = LoadedPreprocessedEntities.size();
317 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
318 + NumEntities);
319 return Result;
320}
321
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000322void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000323 MacroDefinition *Def) {
324 MacroDefinitions[Macro] = Def;
Douglas Gregoraae92242010-03-19 21:51:54 +0000325}
326
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000327/// \brief Retrieve the preprocessed entity at the given ID.
328PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000329 if (PPID.ID < 0) {
330 unsigned Index = -PPID.ID - 1;
331 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000332 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000333 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000334 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000335
336 if (PPID.ID == 0)
337 return 0;
338 unsigned Index = PPID.ID - 1;
339 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000340 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000341 return PreprocessedEntities[Index];
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000342}
343
344/// \brief Retrieve the loaded preprocessed entity at the given index.
345PreprocessedEntity *
346PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
347 assert(Index < LoadedPreprocessedEntities.size() &&
348 "Out-of bounds loaded preprocessed entity");
349 assert(ExternalSource && "No external source to load from");
350 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
351 if (!Entity) {
352 Entity = ExternalSource->ReadPreprocessedEntity(Index);
353 if (!Entity) // Failed to load.
354 Entity = new (*this)
355 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
356 }
357 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000358}
359
Douglas Gregor8aaca672010-03-19 21:58:23 +0000360MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000361 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000362 = MacroDefinitions.find(MI);
363 if (Pos == MacroDefinitions.end())
364 return 0;
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000365
366 return Pos->second;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000367}
368
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000369void PreprocessingRecord::addMacroExpansion(const Token &Id,
370 const MacroInfo *MI,
371 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000372 // We don't record nested macro expansions.
373 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000374 return;
375
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000376 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000377 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000378 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
379 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000380 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000381 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000382}
383
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000384void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000385 const MacroDirective *MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000386 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000387 if (MD)
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000388 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
389 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000390}
391
392void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000393 const MacroDirective *MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000394 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000395 if (MD)
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000396 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
397 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000398}
399
400void PreprocessingRecord::Defined(const Token &MacroNameTok,
John Thompsoncda95fe2013-07-19 18:50:04 +0000401 const MacroDirective *MD,
402 SourceRange Range) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000403 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000404 if (MD)
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000405 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
406 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000407}
408
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +0000409void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) {
410 SkippedRanges.push_back(Range);
411}
412
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000413void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000414 SourceRange Range,
415 const MacroArgs *Args) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000416 addMacroExpansion(Id, MD->getMacroInfo(), Range);
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000417}
418
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000419void PreprocessingRecord::MacroDefined(const Token &Id,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000420 const MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000421 const MacroInfo *MI = MD->getMacroInfo();
Douglas Gregor7dc87222010-03-19 17:12:43 +0000422 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
423 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000424 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000425 addPreprocessedEntity(Def);
426 MacroDefinitions[MI] = Def;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000427}
Douglas Gregoraae92242010-03-19 21:51:54 +0000428
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000429void PreprocessingRecord::MacroUndefined(const Token &Id,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000430 const MacroDirective *MD) {
Argyrios Kyrtzidis99b0a6a2013-01-16 16:52:44 +0000431 // Note: MI may be null (when #undef'ining an undefined macro).
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000432 if (MD)
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000433 MacroDefinitions.erase(MD->getMacroInfo());
Douglas Gregor8aaca672010-03-19 21:58:23 +0000434}
435
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000436void PreprocessingRecord::InclusionDirective(
437 SourceLocation HashLoc,
438 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000439 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000440 bool IsAngled,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000441 CharSourceRange FilenameRange,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000442 const FileEntry *File,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000443 StringRef SearchPath,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000444 StringRef RelativePath,
445 const Module *Imported) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000446 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
447
448 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
449 case tok::pp_include:
450 Kind = InclusionDirective::Include;
451 break;
452
453 case tok::pp_import:
454 Kind = InclusionDirective::Import;
455 break;
456
457 case tok::pp_include_next:
458 Kind = InclusionDirective::IncludeNext;
459 break;
460
461 case tok::pp___include_macros:
462 Kind = InclusionDirective::IncludeMacros;
463 break;
464
465 default:
466 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000467 }
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000468
469 SourceLocation EndLoc;
470 if (!IsAngled) {
471 EndLoc = FilenameRange.getBegin();
472 } else {
473 EndLoc = FilenameRange.getEnd();
474 if (FilenameRange.isCharRange())
475 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
476 // a token range.
477 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000478 clang::InclusionDirective *ID
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +0000479 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
480 (bool)Imported,
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000481 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000482 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000483}
Ted Kremenek182543a2011-07-26 21:17:24 +0000484
485size_t PreprocessingRecord::getTotalMemory() const {
486 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000487 + llvm::capacity_in_bytes(MacroDefinitions)
488 + llvm::capacity_in_bytes(PreprocessedEntities)
489 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000490}