blob: 954b569bb026b497be25b43744aa4d212c13b39b [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
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000022ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
Douglas Gregoraae92242010-03-19 21:51:54 +000023
Douglas Gregorf09b6c92010-11-01 15:03:47 +000024InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
Benjamin Kramerc3f89252016-10-20 14:27:22 +000025 InclusionKind Kind, StringRef FileName,
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +000026 bool InQuotes, bool ImportedModule,
Benjamin Kramerc3f89252016-10-20 14:27:22 +000027 const FileEntry *File, SourceRange Range)
28 : PreprocessingDirective(InclusionDirectiveKind, Range), InQuotes(InQuotes),
29 Kind(Kind), ImportedModule(ImportedModule), File(File) {
30 char *Memory = (char *)PPRec.Allocate(FileName.size() + 1, alignof(char));
Douglas Gregorf09b6c92010-11-01 15:03:47 +000031 memcpy(Memory, FileName.data(), FileName.size());
32 Memory[FileName.size()] = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000033 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000034}
35
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000036PreprocessingRecord::PreprocessingRecord(SourceManager &SM)
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000037 : SourceMgr(SM),
Craig Topperd2d442c2014-05-17 23:10:59 +000038 ExternalSource(nullptr) {
Douglas Gregoraae92242010-03-19 21:51:54 +000039}
40
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000041/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
James Dennett303d8d42012-06-22 05:37:13 +000042/// that source range \p Range encompasses.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000043llvm::iterator_range<PreprocessingRecord::iterator>
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000044PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
45 if (Range.isInvalid())
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000046 return llvm::make_range(iterator(), iterator());
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000047
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000048 if (CachedRangeQuery.Range == Range) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000049 return llvm::make_range(iterator(this, CachedRangeQuery.Result.first),
50 iterator(this, CachedRangeQuery.Result.second));
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000051 }
52
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000053 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000054
55 CachedRangeQuery.Range = Range;
56 CachedRangeQuery.Result = Res;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000057
58 return llvm::make_range(iterator(this, Res.first),
59 iterator(this, Res.second));
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000060}
61
62static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
63 SourceManager &SM) {
Yaron Keren8b563662015-10-03 10:46:20 +000064 assert(FID.isValid());
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000065 if (!PPE)
66 return false;
67
68 SourceLocation Loc = PPE->getSourceRange().getBegin();
69 if (Loc.isInvalid())
70 return false;
David Blaikie7a3cbb22015-03-09 02:02:07 +000071
72 return SM.isInFileID(SM.getFileLoc(Loc), FID);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000073}
74
75/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
76/// points to is coming from the file \arg FID.
77///
78/// Can be used to avoid implicit deserializations of preallocated
79/// preprocessed entities if we only care about entities of a specific file
James Dennett303d8d42012-06-22 05:37:13 +000080/// and not from files \#included in the range given at
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000081/// \see getPreprocessedEntitiesInRange.
82bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
83 if (FID.isInvalid())
84 return false;
85
Benjamin Kramer15b97172015-03-15 15:27:19 +000086 int Pos = std::distance(iterator(this, 0), PPEI);
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000087 if (Pos < 0) {
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +000088 if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
89 assert(0 && "Out-of bounds loaded preprocessed entity");
90 return false;
91 }
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000092 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000093 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000094 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
95 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
96
97 // See if the external source can see if the entity is in the file without
98 // deserializing it.
David Blaikie05785d12013-02-20 22:23:23 +000099 Optional<bool> IsInFile =
100 ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000101 if (IsInFile.hasValue())
102 return IsInFile.getValue();
103
104 // The external source did not provide a definite answer, go and deserialize
105 // the entity to check it.
106 return isPreprocessedEntityIfInFileID(
107 getLoadedPreprocessedEntity(LoadedIndex),
108 FID, SourceMgr);
109 }
110
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +0000111 if (unsigned(Pos) >= PreprocessedEntities.size()) {
112 assert(0 && "Out-of bounds local preprocessed entity");
113 return false;
114 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000115 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000116 FID, SourceMgr);
117}
118
119/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
120/// that source range \arg R encompasses.
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000121std::pair<int, int>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000122PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
123 assert(Range.isValid());
124 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
125
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000126 std::pair<unsigned, unsigned>
127 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000128
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000129 // Check if range spans local entities.
130 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000131 return std::make_pair(Local.first, Local.second);
132
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000133 std::pair<unsigned, unsigned>
134 Loaded = ExternalSource->findPreprocessedEntitiesInRange(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 (Loaded.first == Loaded.second)
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 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000141
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000142 // Check if range spans loaded entities.
143 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000144 return std::make_pair(int(Loaded.first)-TotalLoaded,
145 int(Loaded.second)-TotalLoaded);
146
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000147 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000148 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000149}
150
151std::pair<unsigned, unsigned>
152PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
153 SourceRange Range) const {
154 if (Range.isInvalid())
155 return std::make_pair(0,0);
156 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
157
158 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
159 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
160 return std::make_pair(Begin, End);
161}
162
163namespace {
164
165template <SourceLocation (SourceRange::*getRangeLoc)() const>
166struct PPEntityComp {
167 const SourceManager &SM;
168
169 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
170
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000171 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
172 SourceLocation LHS = getLoc(L);
173 SourceLocation RHS = getLoc(R);
174 return SM.isBeforeInTranslationUnit(LHS, RHS);
175 }
176
177 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000178 SourceLocation LHS = getLoc(L);
179 return SM.isBeforeInTranslationUnit(LHS, RHS);
180 }
181
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000182 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000183 SourceLocation RHS = getLoc(R);
184 return SM.isBeforeInTranslationUnit(LHS, RHS);
185 }
186
187 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000188 SourceRange Range = PPE->getSourceRange();
189 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000190 }
191};
192
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000193}
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000194
195unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
196 SourceLocation Loc) const {
197 if (SourceMgr.isLoadedSourceLocation(Loc))
198 return 0;
199
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000200 size_t Count = PreprocessedEntities.size();
201 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000202 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000203 First = PreprocessedEntities.begin();
204 std::vector<PreprocessedEntity *>::const_iterator I;
205
206 // Do a binary search manually instead of using std::lower_bound because
207 // The end locations of entities may be unordered (when a macro expansion
208 // is inside another macro argument), but for this case it is not important
209 // whether we get the first macro expansion or its containing macro.
210 while (Count > 0) {
211 Half = Count/2;
212 I = First;
213 std::advance(I, Half);
214 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
215 Loc)){
216 First = I;
217 ++First;
218 Count = Count - Half - 1;
219 } else
220 Count = Half;
221 }
222
223 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000224}
225
226unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
227 SourceLocation Loc) const {
228 if (SourceMgr.isLoadedSourceLocation(Loc))
229 return 0;
230
231 std::vector<PreprocessedEntity *>::const_iterator
232 I = std::upper_bound(PreprocessedEntities.begin(),
233 PreprocessedEntities.end(),
234 Loc,
235 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
236 return I - PreprocessedEntities.begin();
237}
238
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000239PreprocessingRecord::PPEntityID
240PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000241 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000242 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000243
Richard Smith66a81862015-05-04 02:25:31 +0000244 if (isa<MacroDefinitionRecord>(Entity)) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000245 assert((PreprocessedEntities.empty() ||
Richard Smith66a81862015-05-04 02:25:31 +0000246 !SourceMgr.isBeforeInTranslationUnit(
247 BeginLoc,
248 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000249 "a macro definition was encountered out-of-order");
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000250 PreprocessedEntities.push_back(Entity);
251 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
252 }
253
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000254 // Check normal case, this entity begin location is after the previous one.
255 if (PreprocessedEntities.empty() ||
256 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
257 PreprocessedEntities.back()->getSourceRange().getBegin())) {
258 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000259 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000260 }
261
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000262 // The entity's location is not after the previous one; this can happen with
263 // include directives that form the filename using macros, e.g:
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000264 // "#include MACRO(STUFF)"
265 // or with macro expansions inside macro arguments where the arguments are
266 // not expanded in the same order as listed, e.g:
267 // \code
268 // #define M1 1
269 // #define M2 2
270 // #define FM(x,y) y x
271 // FM(M1, M2)
272 // \endcode
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000273
274 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
275
276 // Usually there are few macro expansions when defining the filename, do a
277 // linear search for a few entities.
278 unsigned count = 0;
279 for (pp_iter RI = PreprocessedEntities.end(),
280 Begin = PreprocessedEntities.begin();
281 RI != Begin && count < 4; --RI, ++count) {
282 pp_iter I = RI;
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000283 --I;
284 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
285 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000286 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
287 return getPPEntityID(insertI - PreprocessedEntities.begin(),
288 /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000289 }
290 }
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000291
292 // Linear search unsuccessful. Do a binary search.
293 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
294 PreprocessedEntities.end(),
295 BeginLoc,
296 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
297 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
298 return getPPEntityID(insertI - PreprocessedEntities.begin(),
299 /*isLoaded=*/false);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000300}
301
Douglas Gregoraae92242010-03-19 21:51:54 +0000302void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000303 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000304 assert(!ExternalSource &&
305 "Preprocessing record already has an external source");
306 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000307}
308
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000309unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
310 unsigned Result = LoadedPreprocessedEntities.size();
311 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
312 + NumEntities);
313 return Result;
314}
315
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000316void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
Richard Smith66a81862015-05-04 02:25:31 +0000317 MacroDefinitionRecord *Def) {
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000318 MacroDefinitions[Macro] = Def;
Douglas Gregoraae92242010-03-19 21:51:54 +0000319}
320
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000321/// \brief Retrieve the preprocessed entity at the given ID.
322PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000323 if (PPID.ID < 0) {
324 unsigned Index = -PPID.ID - 1;
325 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000326 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000327 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000328 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000329
330 if (PPID.ID == 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000331 return nullptr;
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000332 unsigned Index = PPID.ID - 1;
333 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000334 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000335 return PreprocessedEntities[Index];
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000336}
337
338/// \brief Retrieve the loaded preprocessed entity at the given index.
339PreprocessedEntity *
340PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
341 assert(Index < LoadedPreprocessedEntities.size() &&
342 "Out-of bounds loaded preprocessed entity");
343 assert(ExternalSource && "No external source to load from");
344 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
345 if (!Entity) {
346 Entity = ExternalSource->ReadPreprocessedEntity(Index);
347 if (!Entity) // Failed to load.
348 Entity = new (*this)
349 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
350 }
351 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000352}
353
Richard Smith66a81862015-05-04 02:25:31 +0000354MacroDefinitionRecord *
355PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
356 llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *>::iterator Pos =
357 MacroDefinitions.find(MI);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000358 if (Pos == MacroDefinitions.end())
Craig Topperd2d442c2014-05-17 23:10:59 +0000359 return nullptr;
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000360
361 return Pos->second;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000362}
363
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000364void PreprocessingRecord::addMacroExpansion(const Token &Id,
365 const MacroInfo *MI,
366 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000367 // We don't record nested macro expansions.
368 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000369 return;
370
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000371 if (MI->isBuiltinMacro())
Richard Smith66a81862015-05-04 02:25:31 +0000372 addPreprocessedEntity(new (*this)
373 MacroExpansion(Id.getIdentifierInfo(), Range));
374 else if (MacroDefinitionRecord *Def = findMacroDefinition(MI))
375 addPreprocessedEntity(new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000376}
377
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000378void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000379 const MacroDefinition &MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000380 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000381 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000382 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000383 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000384}
385
386void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000387 const MacroDefinition &MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000388 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000389 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000390 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000391 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000392}
393
394void PreprocessingRecord::Defined(const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000395 const MacroDefinition &MD,
John Thompsoncda95fe2013-07-19 18:50:04 +0000396 SourceRange Range) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000397 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000398 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000399 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000400 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000401}
402
Vedant Kumar3919a502017-09-11 20:47:42 +0000403void PreprocessingRecord::SourceRangeSkipped(SourceRange Range,
404 SourceLocation EndifLoc) {
405 SkippedRanges.emplace_back(Range.getBegin(), EndifLoc);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +0000406}
407
Richard Smith36bd40d2015-05-04 03:15:40 +0000408void PreprocessingRecord::MacroExpands(const Token &Id,
409 const MacroDefinition &MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000410 SourceRange Range,
411 const MacroArgs *Args) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000412 addMacroExpansion(Id, MD.getMacroInfo(), Range);
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000413}
414
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000415void PreprocessingRecord::MacroDefined(const Token &Id,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000416 const MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000417 const MacroInfo *MI = MD->getMacroInfo();
Douglas Gregor7dc87222010-03-19 17:12:43 +0000418 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
Richard Smith66a81862015-05-04 02:25:31 +0000419 MacroDefinitionRecord *Def =
420 new (*this) MacroDefinitionRecord(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000421 addPreprocessedEntity(Def);
422 MacroDefinitions[MI] = Def;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000423}
Douglas Gregoraae92242010-03-19 21:51:54 +0000424
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000425void PreprocessingRecord::MacroUndefined(const Token &Id,
Vedant Kumar349a6242017-04-26 21:05:44 +0000426 const MacroDefinition &MD,
427 const MacroDirective *Undef) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000428 MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); });
Douglas Gregor8aaca672010-03-19 21:58:23 +0000429}
430
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000431void PreprocessingRecord::InclusionDirective(
432 SourceLocation HashLoc,
433 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000434 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000435 bool IsAngled,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000436 CharSourceRange FilenameRange,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000437 const FileEntry *File,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000438 StringRef SearchPath,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000439 StringRef RelativePath,
440 const Module *Imported) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000441 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
442
443 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
444 case tok::pp_include:
445 Kind = InclusionDirective::Include;
446 break;
447
448 case tok::pp_import:
449 Kind = InclusionDirective::Import;
450 break;
451
452 case tok::pp_include_next:
453 Kind = InclusionDirective::IncludeNext;
454 break;
455
456 case tok::pp___include_macros:
457 Kind = InclusionDirective::IncludeMacros;
458 break;
459
460 default:
461 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000462 }
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000463
464 SourceLocation EndLoc;
465 if (!IsAngled) {
466 EndLoc = FilenameRange.getBegin();
467 } else {
468 EndLoc = FilenameRange.getEnd();
469 if (FilenameRange.isCharRange())
470 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
471 // a token range.
472 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000473 clang::InclusionDirective *ID
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +0000474 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
475 (bool)Imported,
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000476 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000477 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000478}
Ted Kremenek182543a2011-07-26 21:17:24 +0000479
480size_t PreprocessingRecord::getTotalMemory() const {
481 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000482 + llvm::capacity_in_bytes(MacroDefinitions)
483 + llvm::capacity_in_bytes(PreprocessedEntities)
484 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000485}