blob: dafcbbe58c91421ecc092e31536c6fe0d7f908e9 [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),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043 ExternalSource(nullptr) {
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.
Stephen Hines0e2c34f2015-03-23 12:09:02 -070048llvm::iterator_range<PreprocessingRecord::iterator>
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +000049PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
50 if (Range.isInvalid())
Stephen Hines0e2c34f2015-03-23 12:09:02 -070051 return llvm::make_range(iterator(), iterator());
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +000052
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000053 if (CachedRangeQuery.Range == Range) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070054 return llvm::make_range(iterator(this, CachedRangeQuery.Result.first),
55 iterator(this, CachedRangeQuery.Result.second));
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000056 }
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;
Stephen Hines0e2c34f2015-03-23 12:09:02 -070062
63 return llvm::make_range(iterator(this, Res.first),
64 iterator(this, Res.second));
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000065}
66
67static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
68 SourceManager &SM) {
69 assert(!FID.isInvalid());
70 if (!PPE)
71 return false;
72
73 SourceLocation Loc = PPE->getSourceRange().getBegin();
74 if (Loc.isInvalid())
75 return false;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070076
77 return SM.isInFileID(SM.getFileLoc(Loc), FID);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000078}
79
80/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
81/// points to is coming from the file \arg FID.
82///
83/// Can be used to avoid implicit deserializations of preallocated
84/// preprocessed entities if we only care about entities of a specific file
James Dennettcd6b34b2012-06-22 05:37:13 +000085/// and not from files \#included in the range given at
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000086/// \see getPreprocessedEntitiesInRange.
87bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
88 if (FID.isInvalid())
89 return false;
90
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070091 int Pos = std::distance(iterator(this, 0), PPEI);
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +000092 if (Pos < 0) {
Argyrios Kyrtzidis064d88e2013-02-12 21:41:23 +000093 if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
94 assert(0 && "Out-of bounds loaded preprocessed entity");
95 return false;
96 }
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +000097 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.
David Blaikiedc84cd52013-02-20 22:23:23 +0000104 Optional<bool> IsInFile =
105 ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000106 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 Kyrtzidis064d88e2013-02-12 21:41:23 +0000116 if (unsigned(Pos) >= PreprocessedEntities.size()) {
117 assert(0 && "Out-of bounds local preprocessed entity");
118 return false;
119 }
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000120 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000121 FID, SourceMgr);
122}
123
124/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
125/// that source range \arg R encompasses.
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000126std::pair<int, int>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000127PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
128 assert(Range.isValid());
129 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
130
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000131 std::pair<unsigned, unsigned>
132 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000133
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000134 // Check if range spans local entities.
135 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000136 return std::make_pair(Local.first, Local.second);
137
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000138 std::pair<unsigned, unsigned>
139 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000140
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000141 // Check if range spans local entities.
142 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000143 return std::make_pair(Local.first, Local.second);
144
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000145 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000146
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000147 // Check if range spans loaded entities.
148 if (Local.first == Local.second)
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000149 return std::make_pair(int(Loaded.first)-TotalLoaded,
150 int(Loaded.second)-TotalLoaded);
151
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000152 // Range spands loaded and local entities.
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000153 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000154}
155
156std::pair<unsigned, unsigned>
157PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
158 SourceRange Range) const {
159 if (Range.isInvalid())
160 return std::make_pair(0,0);
161 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
162
163 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
164 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
165 return std::make_pair(Begin, End);
166}
167
168namespace {
169
170template <SourceLocation (SourceRange::*getRangeLoc)() const>
171struct PPEntityComp {
172 const SourceManager &SM;
173
174 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
175
Benjamin Kramer196e71e2011-09-21 16:58:20 +0000176 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
177 SourceLocation LHS = getLoc(L);
178 SourceLocation RHS = getLoc(R);
179 return SM.isBeforeInTranslationUnit(LHS, RHS);
180 }
181
182 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000183 SourceLocation LHS = getLoc(L);
184 return SM.isBeforeInTranslationUnit(LHS, RHS);
185 }
186
Benjamin Kramer196e71e2011-09-21 16:58:20 +0000187 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000188 SourceLocation RHS = getLoc(R);
189 return SM.isBeforeInTranslationUnit(LHS, RHS);
190 }
191
192 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidis0e322ff2011-09-19 22:02:08 +0000193 SourceRange Range = PPE->getSourceRange();
194 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000195 }
196};
197
198}
199
200unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
201 SourceLocation Loc) const {
202 if (SourceMgr.isLoadedSourceLocation(Loc))
203 return 0;
204
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +0000205 size_t Count = PreprocessedEntities.size();
206 size_t Half;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000207 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +0000208 First = PreprocessedEntities.begin();
209 std::vector<PreprocessedEntity *>::const_iterator I;
210
211 // Do a binary search manually instead of using std::lower_bound because
212 // The end locations of entities may be unordered (when a macro expansion
213 // is inside another macro argument), but for this case it is not important
214 // whether we get the first macro expansion or its containing macro.
215 while (Count > 0) {
216 Half = Count/2;
217 I = First;
218 std::advance(I, Half);
219 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
220 Loc)){
221 First = I;
222 ++First;
223 Count = Count - Half - 1;
224 } else
225 Count = Half;
226 }
227
228 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000229}
230
231unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
232 SourceLocation Loc) const {
233 if (SourceMgr.isLoadedSourceLocation(Loc))
234 return 0;
235
236 std::vector<PreprocessedEntity *>::const_iterator
237 I = std::upper_bound(PreprocessedEntities.begin(),
238 PreprocessedEntities.end(),
239 Loc,
240 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
241 return I - PreprocessedEntities.begin();
242}
243
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000244PreprocessingRecord::PPEntityID
245PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis2502efd2011-09-20 23:27:33 +0000246 assert(Entity);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000247 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000248
Argyrios Kyrtzidisd017e422013-01-09 23:22:20 +0000249 if (isa<MacroDefinition>(Entity)) {
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000250 assert((PreprocessedEntities.empty() ||
251 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
252 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
Argyrios Kyrtzidisd017e422013-01-09 23:22:20 +0000253 "a macro definition was encountered out-of-order");
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000254 PreprocessedEntities.push_back(Entity);
255 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
256 }
257
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000258 // Check normal case, this entity begin location is after the previous one.
259 if (PreprocessedEntities.empty() ||
260 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
261 PreprocessedEntities.back()->getSourceRange().getBegin())) {
262 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000263 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000264 }
265
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000266 // The entity's location is not after the previous one; this can happen with
267 // include directives that form the filename using macros, e.g:
Argyrios Kyrtzidisd017e422013-01-09 23:22:20 +0000268 // "#include MACRO(STUFF)"
269 // or with macro expansions inside macro arguments where the arguments are
270 // not expanded in the same order as listed, e.g:
271 // \code
272 // #define M1 1
273 // #define M2 2
274 // #define FM(x,y) y x
275 // FM(M1, M2)
276 // \endcode
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000277
278 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
279
280 // Usually there are few macro expansions when defining the filename, do a
281 // linear search for a few entities.
282 unsigned count = 0;
283 for (pp_iter RI = PreprocessedEntities.end(),
284 Begin = PreprocessedEntities.begin();
285 RI != Begin && count < 4; --RI, ++count) {
286 pp_iter I = RI;
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000287 --I;
288 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
289 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000290 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
291 return getPPEntityID(insertI - PreprocessedEntities.begin(),
292 /*isLoaded=*/false);
Argyrios Kyrtzidis209dfbe2011-10-12 17:36:33 +0000293 }
294 }
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000295
296 // Linear search unsuccessful. Do a binary search.
297 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
298 PreprocessedEntities.end(),
299 BeginLoc,
300 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
301 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
302 return getPPEntityID(insertI - PreprocessedEntities.begin(),
303 /*isLoaded=*/false);
Douglas Gregor4ae8f292010-03-18 17:52:52 +0000304}
305
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000306void PreprocessingRecord::SetExternalSource(
Douglas Gregor4c30bb12011-07-21 00:47:40 +0000307 ExternalPreprocessingRecordSource &Source) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000308 assert(!ExternalSource &&
309 "Preprocessing record already has an external source");
310 ExternalSource = &Source;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000311}
312
Douglas Gregor4c30bb12011-07-21 00:47:40 +0000313unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
314 unsigned Result = LoadedPreprocessedEntities.size();
315 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
316 + NumEntities);
317 return Result;
318}
319
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000320void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +0000321 MacroDefinition *Def) {
322 MacroDefinitions[Macro] = Def;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000323}
324
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000325/// \brief Retrieve the preprocessed entity at the given ID.
326PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000327 if (PPID.ID < 0) {
328 unsigned Index = -PPID.ID - 1;
329 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000330 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000331 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000332 }
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000333
334 if (PPID.ID == 0)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700335 return nullptr;
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000336 unsigned Index = PPID.ID - 1;
337 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000338 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisf03b8882012-10-05 00:22:28 +0000339 return PreprocessedEntities[Index];
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000340}
341
342/// \brief Retrieve the loaded preprocessed entity at the given index.
343PreprocessedEntity *
344PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
345 assert(Index < LoadedPreprocessedEntities.size() &&
346 "Out-of bounds loaded preprocessed entity");
347 assert(ExternalSource && "No external source to load from");
348 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
349 if (!Entity) {
350 Entity = ExternalSource->ReadPreprocessedEntity(Index);
351 if (!Entity) // Failed to load.
352 Entity = new (*this)
353 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
354 }
355 return Entity;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000356}
357
Douglas Gregor1b058e82010-03-19 21:58:23 +0000358MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +0000359 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000360 = MacroDefinitions.find(MI);
361 if (Pos == MacroDefinitions.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700362 return nullptr;
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +0000363
364 return Pos->second;
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000365}
366
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000367void PreprocessingRecord::addMacroExpansion(const Token &Id,
368 const MacroInfo *MI,
369 SourceRange Range) {
Argyrios Kyrtzidise1d43302012-02-25 02:41:16 +0000370 // We don't record nested macro expansions.
371 if (Id.getLocation().isMacroID())
Douglas Gregordca8ee82011-05-06 16:33:08 +0000372 return;
373
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000374 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000375 addPreprocessedEntity(
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000376 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
377 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000378 addPreprocessedEntity(
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +0000379 new (*this) MacroExpansion(Def, Range));
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000380}
381
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000382void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000383 const MacroDirective *MD) {
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000384 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000385 if (MD)
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000386 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
387 MacroNameTok.getLocation());
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000388}
389
390void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000391 const MacroDirective *MD) {
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000392 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000393 if (MD)
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000394 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
395 MacroNameTok.getLocation());
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000396}
397
398void PreprocessingRecord::Defined(const Token &MacroNameTok,
John Thompson6fb63ab2013-07-19 18:50:04 +0000399 const MacroDirective *MD,
400 SourceRange Range) {
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000401 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000402 if (MD)
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000403 addMacroExpansion(MacroNameTok, MD->getMacroInfo(),
404 MacroNameTok.getLocation());
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000405}
406
Stephen Hines651f13c2014-04-23 16:59:28 -0700407void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) {
408 SkippedRanges.push_back(Range);
409}
410
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000411void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD,
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000412 SourceRange Range,
413 const MacroArgs *Args) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000414 addMacroExpansion(Id, MD->getMacroInfo(), Range);
Argyrios Kyrtzidis5f5250b2012-12-08 02:21:17 +0000415}
416
Craig Silverstein2aa92672010-11-19 21:33:15 +0000417void PreprocessingRecord::MacroDefined(const Token &Id,
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000418 const MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000419 const MacroInfo *MI = MD->getMacroInfo();
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000420 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
421 MacroDefinition *Def
Argyrios Kyrtzidisde4e0a82011-09-20 22:14:48 +0000422 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +0000423 addPreprocessedEntity(Def);
424 MacroDefinitions[MI] = Def;
Douglas Gregorb9e1b752010-03-19 17:12:43 +0000425}
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000426
Craig Silverstein2aa92672010-11-19 21:33:15 +0000427void PreprocessingRecord::MacroUndefined(const Token &Id,
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000428 const MacroDirective *MD) {
Argyrios Kyrtzidis36845472013-01-16 16:52:44 +0000429 // Note: MI may be null (when #undef'ining an undefined macro).
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000430 if (MD)
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000431 MacroDefinitions.erase(MD->getMacroInfo());
Douglas Gregor1b058e82010-03-19 21:58:23 +0000432}
433
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000434void PreprocessingRecord::InclusionDirective(
435 SourceLocation HashLoc,
436 const clang::Token &IncludeTok,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000437 StringRef FileName,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000438 bool IsAngled,
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000439 CharSourceRange FilenameRange,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000440 const FileEntry *File,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000441 StringRef SearchPath,
Argyrios Kyrtzidisf8afcff2012-09-29 01:06:10 +0000442 StringRef RelativePath,
443 const Module *Imported) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000444 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
445
446 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
447 case tok::pp_include:
448 Kind = InclusionDirective::Include;
449 break;
450
451 case tok::pp_import:
452 Kind = InclusionDirective::Import;
453 break;
454
455 case tok::pp_include_next:
456 Kind = InclusionDirective::IncludeNext;
457 break;
458
459 case tok::pp___include_macros:
460 Kind = InclusionDirective::IncludeMacros;
461 break;
462
463 default:
464 llvm_unreachable("Unknown include directive kind");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000465 }
Argyrios Kyrtzidisda313592012-09-27 01:42:07 +0000466
467 SourceLocation EndLoc;
468 if (!IsAngled) {
469 EndLoc = FilenameRange.getBegin();
470 } else {
471 EndLoc = FilenameRange.getEnd();
472 if (FilenameRange.isCharRange())
473 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
474 // a token range.
475 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000476 clang::InclusionDirective *ID
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +0000477 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
478 (bool)Imported,
Douglas Gregor4ab829c2010-11-01 15:03:47 +0000479 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +0000480 addPreprocessedEntity(ID);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000481}
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000482
483size_t PreprocessingRecord::getTotalMemory() const {
484 return BumpAlloc.getTotalMemory()
Ted Kremeneke3c20a82011-07-27 18:41:20 +0000485 + llvm::capacity_in_bytes(MacroDefinitions)
486 + llvm::capacity_in_bytes(PreprocessedEntities)
487 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek91d1bd62011-07-26 21:17:24 +0000488}