blob: af439dbfa5842ef43fa4028a67e909fbb6a9e159 [file] [log] [blame]
Eugene Zelenkocb96ac62017-12-08 22:39:26 +00001//===- PreprocessingRecord.cpp - Record of Preprocessing ------------------===//
Douglas Gregor065f8d12010-03-18 17:52:52 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000014
Douglas Gregor065f8d12010-03-18 17:52:52 +000015#include "clang/Lex/PreprocessingRecord.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000016#include "clang/Basic/IdentifierTable.h"
17#include "clang/Basic/LLVM.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/TokenKinds.h"
Douglas Gregor065f8d12010-03-18 17:52:52 +000021#include "clang/Lex/MacroInfo.h"
22#include "clang/Lex/Token.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/Optional.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/iterator_range.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000027#include "llvm/Support/Capacity.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000028#include "llvm/Support/Casting.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000030#include <algorithm>
31#include <cassert>
32#include <cstddef>
33#include <cstring>
34#include <iterator>
35#include <utility>
36#include <vector>
Douglas Gregor065f8d12010-03-18 17:52:52 +000037
38using namespace clang;
39
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000040ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() =
41 default;
Douglas Gregoraae92242010-03-19 21:51:54 +000042
Douglas Gregorf09b6c92010-11-01 15:03:47 +000043InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
Benjamin Kramerc3f89252016-10-20 14:27:22 +000044 InclusionKind Kind, StringRef FileName,
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +000045 bool InQuotes, bool ImportedModule,
Benjamin Kramerc3f89252016-10-20 14:27:22 +000046 const FileEntry *File, SourceRange Range)
47 : PreprocessingDirective(InclusionDirectiveKind, Range), InQuotes(InQuotes),
48 Kind(Kind), ImportedModule(ImportedModule), File(File) {
49 char *Memory = (char *)PPRec.Allocate(FileName.size() + 1, alignof(char));
Douglas Gregorf09b6c92010-11-01 15:03:47 +000050 memcpy(Memory, FileName.data(), FileName.size());
51 Memory[FileName.size()] = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000052 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000053}
54
Eugene Zelenkocb96ac62017-12-08 22:39:26 +000055PreprocessingRecord::PreprocessingRecord(SourceManager &SM) : SourceMgr(SM) {}
Douglas Gregoraae92242010-03-19 21:51:54 +000056
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000057/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
James Dennett303d8d42012-06-22 05:37:13 +000058/// that source range \p Range encompasses.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000059llvm::iterator_range<PreprocessingRecord::iterator>
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000060PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
61 if (Range.isInvalid())
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000062 return llvm::make_range(iterator(), iterator());
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000063
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000064 if (CachedRangeQuery.Range == Range) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000065 return llvm::make_range(iterator(this, CachedRangeQuery.Result.first),
66 iterator(this, CachedRangeQuery.Result.second));
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000067 }
68
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000069 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000070
71 CachedRangeQuery.Range = Range;
72 CachedRangeQuery.Result = Res;
Benjamin Kramerb4ef6682015-02-06 17:25:10 +000073
74 return llvm::make_range(iterator(this, Res.first),
75 iterator(this, Res.second));
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000076}
77
78static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
79 SourceManager &SM) {
Yaron Keren8b563662015-10-03 10:46:20 +000080 assert(FID.isValid());
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000081 if (!PPE)
82 return false;
83
84 SourceLocation Loc = PPE->getSourceRange().getBegin();
85 if (Loc.isInvalid())
86 return false;
David Blaikie7a3cbb22015-03-09 02:02:07 +000087
88 return SM.isInFileID(SM.getFileLoc(Loc), FID);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000089}
90
91/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
92/// points to is coming from the file \arg FID.
93///
94/// Can be used to avoid implicit deserializations of preallocated
95/// preprocessed entities if we only care about entities of a specific file
James Dennett303d8d42012-06-22 05:37:13 +000096/// and not from files \#included in the range given at
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000097/// \see getPreprocessedEntitiesInRange.
98bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
99 if (FID.isInvalid())
100 return false;
101
Benjamin Kramer15b97172015-03-15 15:27:19 +0000102 int Pos = std::distance(iterator(this, 0), PPEI);
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000103 if (Pos < 0) {
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +0000104 if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
105 assert(0 && "Out-of bounds loaded preprocessed entity");
106 return false;
107 }
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000108 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000109 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000110 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
111 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
112
113 // See if the external source can see if the entity is in the file without
114 // deserializing it.
David Blaikie05785d12013-02-20 22:23:23 +0000115 Optional<bool> IsInFile =
116 ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000117 if (IsInFile.hasValue())
118 return IsInFile.getValue();
119
120 // The external source did not provide a definite answer, go and deserialize
121 // the entity to check it.
122 return isPreprocessedEntityIfInFileID(
123 getLoadedPreprocessedEntity(LoadedIndex),
124 FID, SourceMgr);
125 }
126
Argyrios Kyrtzidis962b2212013-02-12 21:41:23 +0000127 if (unsigned(Pos) >= PreprocessedEntities.size()) {
128 assert(0 && "Out-of bounds local preprocessed entity");
129 return false;
130 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000131 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000132 FID, SourceMgr);
133}
134
135/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
136/// that source range \arg R encompasses.
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000137std::pair<int, int>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000138PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
139 assert(Range.isValid());
140 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
141
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000142 std::pair<unsigned, unsigned>
143 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000144
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000145 // Check if range spans local entities.
146 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000147 return std::make_pair(Local.first, Local.second);
148
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000149 std::pair<unsigned, unsigned>
150 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000151
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000152 // Check if range spans local entities.
153 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000154 return std::make_pair(Local.first, Local.second);
155
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000156 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000157
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000158 // Check if range spans loaded entities.
159 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000160 return std::make_pair(int(Loaded.first)-TotalLoaded,
161 int(Loaded.second)-TotalLoaded);
162
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000163 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000164 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000165}
166
167std::pair<unsigned, unsigned>
168PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
169 SourceRange Range) const {
170 if (Range.isInvalid())
171 return std::make_pair(0,0);
172 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
173
174 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
175 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
176 return std::make_pair(Begin, End);
177}
178
179namespace {
180
181template <SourceLocation (SourceRange::*getRangeLoc)() const>
182struct PPEntityComp {
183 const SourceManager &SM;
184
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000185 explicit PPEntityComp(const SourceManager &SM) : SM(SM) {}
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000186
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000187 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
188 SourceLocation LHS = getLoc(L);
189 SourceLocation RHS = getLoc(R);
190 return SM.isBeforeInTranslationUnit(LHS, RHS);
191 }
192
193 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000194 SourceLocation LHS = getLoc(L);
195 return SM.isBeforeInTranslationUnit(LHS, RHS);
196 }
197
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000198 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000199 SourceLocation RHS = getLoc(R);
200 return SM.isBeforeInTranslationUnit(LHS, RHS);
201 }
202
203 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000204 SourceRange Range = PPE->getSourceRange();
205 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000206 }
207};
208
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000209} // namespace
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000210
211unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
212 SourceLocation Loc) const {
213 if (SourceMgr.isLoadedSourceLocation(Loc))
214 return 0;
215
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000216 size_t Count = PreprocessedEntities.size();
217 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000218 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000219 First = PreprocessedEntities.begin();
220 std::vector<PreprocessedEntity *>::const_iterator I;
221
222 // Do a binary search manually instead of using std::lower_bound because
223 // The end locations of entities may be unordered (when a macro expansion
224 // is inside another macro argument), but for this case it is not important
225 // whether we get the first macro expansion or its containing macro.
226 while (Count > 0) {
227 Half = Count/2;
228 I = First;
229 std::advance(I, Half);
230 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
231 Loc)){
232 First = I;
233 ++First;
234 Count = Count - Half - 1;
235 } else
236 Count = Half;
237 }
238
239 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000240}
241
242unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
243 SourceLocation Loc) const {
244 if (SourceMgr.isLoadedSourceLocation(Loc))
245 return 0;
246
247 std::vector<PreprocessedEntity *>::const_iterator
248 I = std::upper_bound(PreprocessedEntities.begin(),
249 PreprocessedEntities.end(),
250 Loc,
251 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
252 return I - PreprocessedEntities.begin();
253}
254
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000255PreprocessingRecord::PPEntityID
256PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000257 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000258 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000259
Richard Smith66a81862015-05-04 02:25:31 +0000260 if (isa<MacroDefinitionRecord>(Entity)) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000261 assert((PreprocessedEntities.empty() ||
Richard Smith66a81862015-05-04 02:25:31 +0000262 !SourceMgr.isBeforeInTranslationUnit(
263 BeginLoc,
264 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000265 "a macro definition was encountered out-of-order");
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000266 PreprocessedEntities.push_back(Entity);
267 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
268 }
269
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000270 // Check normal case, this entity begin location is after the previous one.
271 if (PreprocessedEntities.empty() ||
272 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
273 PreprocessedEntities.back()->getSourceRange().getBegin())) {
274 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000275 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000276 }
277
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000278 // The entity's location is not after the previous one; this can happen with
279 // include directives that form the filename using macros, e.g:
Argyrios Kyrtzidiseb994f42013-01-09 23:22:20 +0000280 // "#include MACRO(STUFF)"
281 // or with macro expansions inside macro arguments where the arguments are
282 // not expanded in the same order as listed, e.g:
283 // \code
284 // #define M1 1
285 // #define M2 2
286 // #define FM(x,y) y x
287 // FM(M1, M2)
288 // \endcode
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000289
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000290 using pp_iter = std::vector<PreprocessedEntity *>::iterator;
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000291
292 // Usually there are few macro expansions when defining the filename, do a
293 // linear search for a few entities.
294 unsigned count = 0;
295 for (pp_iter RI = PreprocessedEntities.end(),
296 Begin = PreprocessedEntities.begin();
297 RI != Begin && count < 4; --RI, ++count) {
298 pp_iter I = RI;
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000299 --I;
300 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
301 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000302 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
303 return getPPEntityID(insertI - PreprocessedEntities.begin(),
304 /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000305 }
306 }
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000307
308 // Linear search unsuccessful. Do a binary search.
309 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
310 PreprocessedEntities.end(),
311 BeginLoc,
312 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
313 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
314 return getPPEntityID(insertI - PreprocessedEntities.begin(),
315 /*isLoaded=*/false);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000316}
317
Douglas Gregoraae92242010-03-19 21:51:54 +0000318void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000319 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000320 assert(!ExternalSource &&
321 "Preprocessing record already has an external source");
322 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000323}
324
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000325unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
326 unsigned Result = LoadedPreprocessedEntities.size();
327 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
328 + NumEntities);
329 return Result;
330}
331
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000332void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
Richard Smith66a81862015-05-04 02:25:31 +0000333 MacroDefinitionRecord *Def) {
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000334 MacroDefinitions[Macro] = Def;
Douglas Gregoraae92242010-03-19 21:51:54 +0000335}
336
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000337/// \brief Retrieve the preprocessed entity at the given ID.
338PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000339 if (PPID.ID < 0) {
340 unsigned Index = -PPID.ID - 1;
341 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000342 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000343 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000344 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000345
346 if (PPID.ID == 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000347 return nullptr;
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000348 unsigned Index = PPID.ID - 1;
349 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000350 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000351 return PreprocessedEntities[Index];
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000352}
353
354/// \brief Retrieve the loaded preprocessed entity at the given index.
355PreprocessedEntity *
356PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
357 assert(Index < LoadedPreprocessedEntities.size() &&
358 "Out-of bounds loaded preprocessed entity");
359 assert(ExternalSource && "No external source to load from");
360 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
361 if (!Entity) {
362 Entity = ExternalSource->ReadPreprocessedEntity(Index);
363 if (!Entity) // Failed to load.
364 Entity = new (*this)
365 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
366 }
367 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000368}
369
Richard Smith66a81862015-05-04 02:25:31 +0000370MacroDefinitionRecord *
371PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
372 llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *>::iterator Pos =
373 MacroDefinitions.find(MI);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000374 if (Pos == MacroDefinitions.end())
Craig Topperd2d442c2014-05-17 23:10:59 +0000375 return nullptr;
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000376
377 return Pos->second;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000378}
379
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000380void PreprocessingRecord::addMacroExpansion(const Token &Id,
381 const MacroInfo *MI,
382 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000383 // We don't record nested macro expansions.
384 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000385 return;
386
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000387 if (MI->isBuiltinMacro())
Richard Smith66a81862015-05-04 02:25:31 +0000388 addPreprocessedEntity(new (*this)
389 MacroExpansion(Id.getIdentifierInfo(), Range));
390 else if (MacroDefinitionRecord *Def = findMacroDefinition(MI))
391 addPreprocessedEntity(new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000392}
393
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000394void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000395 const MacroDefinition &MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000396 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000397 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000398 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000399 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000400}
401
402void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000403 const MacroDefinition &MD) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000404 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000405 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000406 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000407 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000408}
409
410void PreprocessingRecord::Defined(const Token &MacroNameTok,
Richard Smith36bd40d2015-05-04 03:15:40 +0000411 const MacroDefinition &MD,
John Thompsoncda95fe2013-07-19 18:50:04 +0000412 SourceRange Range) {
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000413 // This is not actually a macro expansion but record it as a macro reference.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000414 if (MD)
Richard Smith36bd40d2015-05-04 03:15:40 +0000415 addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000416 MacroNameTok.getLocation());
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000417}
418
Vedant Kumar3919a502017-09-11 20:47:42 +0000419void PreprocessingRecord::SourceRangeSkipped(SourceRange Range,
420 SourceLocation EndifLoc) {
421 SkippedRanges.emplace_back(Range.getBegin(), EndifLoc);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +0000422}
423
Richard Smith36bd40d2015-05-04 03:15:40 +0000424void PreprocessingRecord::MacroExpands(const Token &Id,
425 const MacroDefinition &MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000426 SourceRange Range,
427 const MacroArgs *Args) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000428 addMacroExpansion(Id, MD.getMacroInfo(), Range);
Argyrios Kyrtzidisf77b0f82012-12-08 02:21:17 +0000429}
430
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000431void PreprocessingRecord::MacroDefined(const Token &Id,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000432 const MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000433 const MacroInfo *MI = MD->getMacroInfo();
Douglas Gregor7dc87222010-03-19 17:12:43 +0000434 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
Richard Smith66a81862015-05-04 02:25:31 +0000435 MacroDefinitionRecord *Def =
436 new (*this) MacroDefinitionRecord(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +0000437 addPreprocessedEntity(Def);
438 MacroDefinitions[MI] = Def;
Douglas Gregor7dc87222010-03-19 17:12:43 +0000439}
Douglas Gregoraae92242010-03-19 21:51:54 +0000440
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000441void PreprocessingRecord::MacroUndefined(const Token &Id,
Vedant Kumar349a6242017-04-26 21:05:44 +0000442 const MacroDefinition &MD,
443 const MacroDirective *Undef) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000444 MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); });
Douglas Gregor8aaca672010-03-19 21:58:23 +0000445}
446
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000447void PreprocessingRecord::InclusionDirective(
448 SourceLocation HashLoc,
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000449 const Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000450 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000451 bool IsAngled,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000452 CharSourceRange FilenameRange,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000453 const FileEntry *File,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000454 StringRef SearchPath,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000455 StringRef RelativePath,
456 const Module *Imported) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000457 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
458
459 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
460 case tok::pp_include:
461 Kind = InclusionDirective::Include;
462 break;
463
464 case tok::pp_import:
465 Kind = InclusionDirective::Import;
466 break;
467
468 case tok::pp_include_next:
469 Kind = InclusionDirective::IncludeNext;
470 break;
471
472 case tok::pp___include_macros:
473 Kind = InclusionDirective::IncludeMacros;
474 break;
475
476 default:
477 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000478 }
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000479
480 SourceLocation EndLoc;
481 if (!IsAngled) {
482 EndLoc = FilenameRange.getBegin();
483 } else {
484 EndLoc = FilenameRange.getEnd();
485 if (FilenameRange.isCharRange())
486 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
487 // a token range.
488 }
Eugene Zelenkocb96ac62017-12-08 22:39:26 +0000489 clang::InclusionDirective *ID =
490 new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
491 (bool)Imported, File,
492 SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000493 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000494}
Ted Kremenek182543a2011-07-26 21:17:24 +0000495
496size_t PreprocessingRecord::getTotalMemory() const {
497 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000498 + llvm::capacity_in_bytes(MacroDefinitions)
499 + llvm::capacity_in_bytes(PreprocessedEntities)
500 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000501}