blob: 5f1353c86e1d8240e603bdf4b5c71110920b9d1c [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"
Douglas Gregor796d76a2010-10-20 22:00:55 +000017#include "llvm/Support/ErrorHandling.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000018#include "llvm/Support/Capacity.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) {
95 assert(unsigned(-Pos-1) < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000096 "Out-of bounds loaded preprocessed entity");
97 assert(ExternalSource && "No external source to load from");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +000098 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000099 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
100 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
101
102 // See if the external source can see if the entity is in the file without
103 // deserializing it.
104 llvm::Optional<bool>
105 IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
106 if (IsInFile.hasValue())
107 return IsInFile.getValue();
108
109 // The external source did not provide a definite answer, go and deserialize
110 // the entity to check it.
111 return isPreprocessedEntityIfInFileID(
112 getLoadedPreprocessedEntity(LoadedIndex),
113 FID, SourceMgr);
114 }
115
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000116 assert(unsigned(Pos) < PreprocessedEntities.size() &&
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000117 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000118 return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000119 FID, SourceMgr);
120}
121
122/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
123/// that source range \arg R encompasses.
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000124std::pair<int, int>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000125PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
126 assert(Range.isValid());
127 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
128
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000129 std::pair<unsigned, unsigned>
130 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000131
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000132 // Check if range spans local entities.
133 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000134 return std::make_pair(Local.first, Local.second);
135
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000136 std::pair<unsigned, unsigned>
137 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000138
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000139 // Check if range spans local entities.
140 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000141 return std::make_pair(Local.first, Local.second);
142
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000143 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000144
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000145 // Check if range spans loaded entities.
146 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000147 return std::make_pair(int(Loaded.first)-TotalLoaded,
148 int(Loaded.second)-TotalLoaded);
149
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000150 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000151 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000152}
153
154std::pair<unsigned, unsigned>
155PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
156 SourceRange Range) const {
157 if (Range.isInvalid())
158 return std::make_pair(0,0);
159 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
160
161 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
162 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
163 return std::make_pair(Begin, End);
164}
165
166namespace {
167
168template <SourceLocation (SourceRange::*getRangeLoc)() const>
169struct PPEntityComp {
170 const SourceManager &SM;
171
172 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
173
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000174 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
175 SourceLocation LHS = getLoc(L);
176 SourceLocation RHS = getLoc(R);
177 return SM.isBeforeInTranslationUnit(LHS, RHS);
178 }
179
180 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000181 SourceLocation LHS = getLoc(L);
182 return SM.isBeforeInTranslationUnit(LHS, RHS);
183 }
184
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000185 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000186 SourceLocation RHS = getLoc(R);
187 return SM.isBeforeInTranslationUnit(LHS, RHS);
188 }
189
190 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000191 SourceRange Range = PPE->getSourceRange();
192 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000193 }
194};
195
196}
197
198unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
199 SourceLocation Loc) const {
200 if (SourceMgr.isLoadedSourceLocation(Loc))
201 return 0;
202
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000203 size_t Count = PreprocessedEntities.size();
204 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000205 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000206 First = PreprocessedEntities.begin();
207 std::vector<PreprocessedEntity *>::const_iterator I;
208
209 // Do a binary search manually instead of using std::lower_bound because
210 // The end locations of entities may be unordered (when a macro expansion
211 // is inside another macro argument), but for this case it is not important
212 // whether we get the first macro expansion or its containing macro.
213 while (Count > 0) {
214 Half = Count/2;
215 I = First;
216 std::advance(I, Half);
217 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
218 Loc)){
219 First = I;
220 ++First;
221 Count = Count - Half - 1;
222 } else
223 Count = Half;
224 }
225
226 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000227}
228
229unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
230 SourceLocation Loc) const {
231 if (SourceMgr.isLoadedSourceLocation(Loc))
232 return 0;
233
234 std::vector<PreprocessedEntity *>::const_iterator
235 I = std::upper_bound(PreprocessedEntities.begin(),
236 PreprocessedEntities.end(),
237 Loc,
238 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
239 return I - PreprocessedEntities.begin();
240}
241
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000242PreprocessingRecord::PPEntityID
243PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000244 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000245 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000246
247 if (!isa<class InclusionDirective>(Entity)) {
248 assert((PreprocessedEntities.empty() ||
249 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
250 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
251 "a macro directive was encountered out-of-order");
252 PreprocessedEntities.push_back(Entity);
253 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
254 }
255
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000256 // Check normal case, this entity begin location is after the previous one.
257 if (PreprocessedEntities.empty() ||
258 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
259 PreprocessedEntities.back()->getSourceRange().getBegin())) {
260 PreprocessedEntities.push_back(Entity);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000261 return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000262 }
263
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000264 // The entity's location is not after the previous one; this can happen with
265 // include directives that form the filename using macros, e.g:
266 // "#include MACRO(STUFF)".
267
268 typedef std::vector<PreprocessedEntity *>::iterator pp_iter;
269
270 // Usually there are few macro expansions when defining the filename, do a
271 // linear search for a few entities.
272 unsigned count = 0;
273 for (pp_iter RI = PreprocessedEntities.end(),
274 Begin = PreprocessedEntities.begin();
275 RI != Begin && count < 4; --RI, ++count) {
276 pp_iter I = RI;
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000277 --I;
278 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
279 (*I)->getSourceRange().getBegin())) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000280 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
281 return getPPEntityID(insertI - PreprocessedEntities.begin(),
282 /*isLoaded=*/false);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000283 }
284 }
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000285
286 // Linear search unsuccessful. Do a binary search.
287 pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
288 PreprocessedEntities.end(),
289 BeginLoc,
290 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
291 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
292 return getPPEntityID(insertI - PreprocessedEntities.begin(),
293 /*isLoaded=*/false);
Douglas Gregor065f8d12010-03-18 17:52:52 +0000294}
295
Douglas Gregoraae92242010-03-19 21:51:54 +0000296void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000297 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000298 assert(!ExternalSource &&
299 "Preprocessing record already has an external source");
300 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000301}
302
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000303unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
304 unsigned Result = LoadedPreprocessedEntities.size();
305 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
306 + NumEntities);
307 return Result;
308}
309
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000310void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
311 PPEntityID PPID) {
312 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000313}
314
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000315/// \brief Retrieve the preprocessed entity at the given ID.
316PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000317 if (PPID.ID < 0) {
318 unsigned Index = -PPID.ID - 1;
319 assert(Index < LoadedPreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000320 "Out-of bounds loaded preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000321 return getLoadedPreprocessedEntity(Index);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000322 }
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000323
324 if (PPID.ID == 0)
325 return 0;
326 unsigned Index = PPID.ID - 1;
327 assert(Index < PreprocessedEntities.size() &&
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000328 "Out-of bounds local preprocessed entity");
Argyrios Kyrtzidisabc721a2012-10-05 00:22:28 +0000329 return PreprocessedEntities[Index];
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000330}
331
332/// \brief Retrieve the loaded preprocessed entity at the given index.
333PreprocessedEntity *
334PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
335 assert(Index < LoadedPreprocessedEntities.size() &&
336 "Out-of bounds loaded preprocessed entity");
337 assert(ExternalSource && "No external source to load from");
338 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
339 if (!Entity) {
340 Entity = ExternalSource->ReadPreprocessedEntity(Index);
341 if (!Entity) // Failed to load.
342 Entity = new (*this)
343 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
344 }
345 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000346}
347
Douglas Gregor8aaca672010-03-19 21:58:23 +0000348MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000349 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000350 = MacroDefinitions.find(MI);
351 if (Pos == MacroDefinitions.end())
352 return 0;
353
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000354 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
355 if (Entity->isInvalid())
356 return 0;
357 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000358}
359
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000360void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
361 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000362 // We don't record nested macro expansions.
363 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000364 return;
365
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000366 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000367 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000368 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
369 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000370 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000371 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000372}
373
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000374void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000375 const MacroInfo *MI) {
376 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
377 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000378 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000379 MacroDefinitions[MI] = addPreprocessedEntity(Def);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000380}
Douglas Gregoraae92242010-03-19 21:51:54 +0000381
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000382void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000383 const MacroInfo *MI) {
Benjamin Kramere894e092012-03-24 18:22:12 +0000384 MacroDefinitions.erase(MI);
Douglas Gregor8aaca672010-03-19 21:58:23 +0000385}
386
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000387void PreprocessingRecord::InclusionDirective(
388 SourceLocation HashLoc,
389 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000390 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000391 bool IsAngled,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000392 CharSourceRange FilenameRange,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000393 const FileEntry *File,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000394 StringRef SearchPath,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000395 StringRef RelativePath,
396 const Module *Imported) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000397 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
398
399 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
400 case tok::pp_include:
401 Kind = InclusionDirective::Include;
402 break;
403
404 case tok::pp_import:
405 Kind = InclusionDirective::Import;
406 break;
407
408 case tok::pp_include_next:
409 Kind = InclusionDirective::IncludeNext;
410 break;
411
412 case tok::pp___include_macros:
413 Kind = InclusionDirective::IncludeMacros;
414 break;
415
416 default:
417 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000418 }
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000419
420 SourceLocation EndLoc;
421 if (!IsAngled) {
422 EndLoc = FilenameRange.getBegin();
423 } else {
424 EndLoc = FilenameRange.getEnd();
425 if (FilenameRange.isCharRange())
426 EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
427 // a token range.
428 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000429 clang::InclusionDirective *ID
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +0000430 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
431 (bool)Imported,
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000432 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000433 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000434}
Ted Kremenek182543a2011-07-26 21:17:24 +0000435
436size_t PreprocessingRecord::getTotalMemory() const {
437 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000438 + llvm::capacity_in_bytes(MacroDefinitions)
439 + llvm::capacity_in_bytes(PreprocessedEntities)
440 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000441}