blob: 770a69d19bb62ec9e8c5b45c8483cd8e67553886 [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,
Douglas Gregorf09b6c92010-11-01 15:03:47 +000028 bool InQuotes, const FileEntry *File,
29 SourceRange Range)
30 : PreprocessingDirective(InclusionDirectiveKind, Range),
31 InQuotes(InQuotes), Kind(Kind), File(File)
32{
33 char *Memory
34 = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
35 memcpy(Memory, FileName.data(), FileName.size());
36 Memory[FileName.size()] = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000037 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000038}
39
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000040PreprocessingRecord::PreprocessingRecord(SourceManager &SM,
41 bool IncludeNestedMacroExpansions)
42 : SourceMgr(SM), IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +000043 ExternalSource(0)
Douglas Gregoraae92242010-03-19 21:51:54 +000044{
45}
46
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000047/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
48/// that source range \arg R encompasses.
49std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
50PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
51 if (Range.isInvalid())
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000052 return std::make_pair(iterator(), iterator());
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000053
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000054 if (CachedRangeQuery.Range == Range) {
55 return std::make_pair(iterator(this, CachedRangeQuery.Result.first),
56 iterator(this, CachedRangeQuery.Result.second));
57 }
58
59 std::pair<PPEntityID, PPEntityID>
60 Res = getPreprocessedEntitiesInRangeSlow(Range);
61
62 CachedRangeQuery.Range = Range;
63 CachedRangeQuery.Result = Res;
64
65 return std::make_pair(iterator(this, Res.first), iterator(this, Res.second));
66}
67
68static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
69 SourceManager &SM) {
70 assert(!FID.isInvalid());
71 if (!PPE)
72 return false;
73
74 SourceLocation Loc = PPE->getSourceRange().getBegin();
75 if (Loc.isInvalid())
76 return false;
77
78 if (SM.isInFileID(SM.getFileLoc(Loc), FID))
79 return true;
80 else
81 return false;
82}
83
84/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
85/// points to is coming from the file \arg FID.
86///
87/// Can be used to avoid implicit deserializations of preallocated
88/// preprocessed entities if we only care about entities of a specific file
89/// and not from files #included in the range given at
90/// \see getPreprocessedEntitiesInRange.
91bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
92 if (FID.isInvalid())
93 return false;
94
95 PPEntityID PPID = PPEI.Position;
96 if (PPID < 0) {
97 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
98 "Out-of bounds loaded preprocessed entity");
99 assert(ExternalSource && "No external source to load from");
100 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+PPID;
101 if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
102 return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
103
104 // See if the external source can see if the entity is in the file without
105 // deserializing it.
106 llvm::Optional<bool>
107 IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
108 if (IsInFile.hasValue())
109 return IsInFile.getValue();
110
111 // The external source did not provide a definite answer, go and deserialize
112 // the entity to check it.
113 return isPreprocessedEntityIfInFileID(
114 getLoadedPreprocessedEntity(LoadedIndex),
115 FID, SourceMgr);
116 }
117
118 assert(unsigned(PPID) < PreprocessedEntities.size() &&
119 "Out-of bounds local preprocessed entity");
120 return isPreprocessedEntityIfInFileID(PreprocessedEntities[PPID],
121 FID, SourceMgr);
122}
123
124/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
125/// that source range \arg R encompasses.
126std::pair<PreprocessingRecord::PPEntityID, PreprocessingRecord::PPEntityID>
127PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
128 assert(Range.isValid());
129 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
130
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000131 std::pair<unsigned, unsigned>
132 Local = findLocalPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000133
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000134 // Check if range spans local entities.
135 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000136 return std::make_pair(Local.first, Local.second);
137
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000138 std::pair<unsigned, unsigned>
139 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000140
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000141 // Check if range spans local entities.
142 if (Loaded.first == Loaded.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000143 return std::make_pair(Local.first, Local.second);
144
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000145 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000146
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000147 // Check if range spans loaded entities.
148 if (Local.first == Local.second)
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000149 return std::make_pair(int(Loaded.first)-TotalLoaded,
150 int(Loaded.second)-TotalLoaded);
151
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000152 // Range spands loaded and local entities.
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +0000153 return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
Argyrios Kyrtzidis64f63812011-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 Kramer2e9d9cf2011-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 Kyrtzidis64f63812011-09-19 20:40:25 +0000183 SourceLocation LHS = getLoc(L);
184 return SM.isBeforeInTranslationUnit(LHS, RHS);
185 }
186
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000187 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-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 Kyrtzidisa35c4442011-09-19 22:02:08 +0000193 SourceRange Range = PPE->getSourceRange();
194 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-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 Kyrtzidise523e382011-09-22 21:17:02 +0000205 size_t Count = PreprocessedEntities.size();
206 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000207 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-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 Kyrtzidis64f63812011-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
Douglas Gregor065f8d12010-03-18 17:52:52 +0000244void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000245 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000246 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
247
248 // Check normal case, this entity begin location is after the previous one.
249 if (PreprocessedEntities.empty() ||
250 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
251 PreprocessedEntities.back()->getSourceRange().getBegin())) {
252 PreprocessedEntities.push_back(Entity);
253 return;
254 }
255
256 // The entity's location is not after the previous one; this can happen rarely
257 // e.g. with "#include MACRO".
258 // Iterate the entities vector in reverse until we find the right place to
259 // insert the new entity.
260 for (std::vector<PreprocessedEntity *>::iterator
261 RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
262 RI != Begin; --RI) {
263 std::vector<PreprocessedEntity *>::iterator I = RI;
264 --I;
265 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
266 (*I)->getSourceRange().getBegin())) {
267 PreprocessedEntities.insert(RI, Entity);
268 return;
269 }
270 }
Douglas Gregor065f8d12010-03-18 17:52:52 +0000271}
272
Douglas Gregoraae92242010-03-19 21:51:54 +0000273void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000274 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000275 assert(!ExternalSource &&
276 "Preprocessing record already has an external source");
277 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000278}
279
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000280unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
281 unsigned Result = LoadedPreprocessedEntities.size();
282 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
283 + NumEntities);
284 return Result;
285}
286
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000287void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
288 PPEntityID PPID) {
289 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000290}
291
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000292/// \brief Retrieve the preprocessed entity at the given ID.
293PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
294 if (PPID < 0) {
295 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
296 "Out-of bounds loaded preprocessed entity");
297 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
298 }
299 assert(unsigned(PPID) < PreprocessedEntities.size() &&
300 "Out-of bounds local preprocessed entity");
301 return PreprocessedEntities[PPID];
302}
303
304/// \brief Retrieve the loaded preprocessed entity at the given index.
305PreprocessedEntity *
306PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
307 assert(Index < LoadedPreprocessedEntities.size() &&
308 "Out-of bounds loaded preprocessed entity");
309 assert(ExternalSource && "No external source to load from");
310 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
311 if (!Entity) {
312 Entity = ExternalSource->ReadPreprocessedEntity(Index);
313 if (!Entity) // Failed to load.
314 Entity = new (*this)
315 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
316 }
317 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000318}
319
Douglas Gregor8aaca672010-03-19 21:58:23 +0000320MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000321 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000322 = MacroDefinitions.find(MI);
323 if (Pos == MacroDefinitions.end())
324 return 0;
325
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000326 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
327 if (Entity->isInvalid())
328 return 0;
329 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000330}
331
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000332void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
333 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000334 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000335 return;
336
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000337 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000338 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000339 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
340 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000341 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000342 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000343}
344
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000345void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000346 const MacroInfo *MI) {
347 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
348 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000349 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000350 addPreprocessedEntity(Def);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000351 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
352 /*isLoaded=*/false);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000353}
Douglas Gregoraae92242010-03-19 21:51:54 +0000354
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000355void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000356 const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000357 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor8aaca672010-03-19 21:58:23 +0000358 = MacroDefinitions.find(MI);
359 if (Pos != MacroDefinitions.end())
360 MacroDefinitions.erase(Pos);
361}
362
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000363void PreprocessingRecord::InclusionDirective(
364 SourceLocation HashLoc,
365 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000366 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000367 bool IsAngled,
368 const FileEntry *File,
369 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000370 StringRef SearchPath,
371 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000372 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
373
374 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
375 case tok::pp_include:
376 Kind = InclusionDirective::Include;
377 break;
378
379 case tok::pp_import:
380 Kind = InclusionDirective::Import;
381 break;
382
383 case tok::pp_include_next:
384 Kind = InclusionDirective::IncludeNext;
385 break;
386
387 case tok::pp___include_macros:
388 Kind = InclusionDirective::IncludeMacros;
389 break;
390
391 default:
392 llvm_unreachable("Unknown include directive kind");
393 return;
394 }
395
396 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000397 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
398 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000399 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000400}
Ted Kremenek182543a2011-07-26 21:17:24 +0000401
402size_t PreprocessingRecord::getTotalMemory() const {
403 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000404 + llvm::capacity_in_bytes(MacroDefinitions)
405 + llvm::capacity_in_bytes(PreprocessedEntities)
406 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000407}