blob: c87088d2decf844e63ae96d5fc8a8faeca262795 [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 Kyrtzidis335c5a42012-02-25 02:41:16 +000040PreprocessingRecord::PreprocessingRecord(SourceManager &SM)
41 : SourceMgr(SM), ExternalSource(0)
Douglas Gregoraae92242010-03-19 21:51:54 +000042{
43}
44
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000045/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
46/// that source range \arg R encompasses.
47std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
48PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
49 if (Range.isInvalid())
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000050 return std::make_pair(iterator(), iterator());
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000051
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +000052 if (CachedRangeQuery.Range == Range) {
53 return std::make_pair(iterator(this, CachedRangeQuery.Result.first),
54 iterator(this, CachedRangeQuery.Result.second));
55 }
56
57 std::pair<PPEntityID, PPEntityID>
58 Res = getPreprocessedEntitiesInRangeSlow(Range);
59
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
87/// and not from files #included in the range given at
88/// \see getPreprocessedEntitiesInRange.
89bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
90 if (FID.isInvalid())
91 return false;
92
93 PPEntityID PPID = PPEI.Position;
94 if (PPID < 0) {
95 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
96 "Out-of bounds loaded preprocessed entity");
97 assert(ExternalSource && "No external source to load from");
98 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+PPID;
99 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
116 assert(unsigned(PPID) < PreprocessedEntities.size() &&
117 "Out-of bounds local preprocessed entity");
118 return isPreprocessedEntityIfInFileID(PreprocessedEntities[PPID],
119 FID, SourceMgr);
120}
121
122/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
123/// that source range \arg R encompasses.
124std::pair<PreprocessingRecord::PPEntityID, PreprocessingRecord::PPEntityID>
125PreprocessingRecord::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
Douglas Gregor065f8d12010-03-18 17:52:52 +0000242void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000243 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000244 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
245
246 // Check normal case, this entity begin location is after the previous one.
247 if (PreprocessedEntities.empty() ||
248 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
249 PreprocessedEntities.back()->getSourceRange().getBegin())) {
250 PreprocessedEntities.push_back(Entity);
251 return;
252 }
253
254 // The entity's location is not after the previous one; this can happen rarely
255 // e.g. with "#include MACRO".
256 // Iterate the entities vector in reverse until we find the right place to
257 // insert the new entity.
258 for (std::vector<PreprocessedEntity *>::iterator
259 RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
260 RI != Begin; --RI) {
261 std::vector<PreprocessedEntity *>::iterator I = RI;
262 --I;
263 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
264 (*I)->getSourceRange().getBegin())) {
265 PreprocessedEntities.insert(RI, Entity);
266 return;
267 }
268 }
Douglas Gregor065f8d12010-03-18 17:52:52 +0000269}
270
Douglas Gregoraae92242010-03-19 21:51:54 +0000271void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000272 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000273 assert(!ExternalSource &&
274 "Preprocessing record already has an external source");
275 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000276}
277
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000278unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
279 unsigned Result = LoadedPreprocessedEntities.size();
280 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
281 + NumEntities);
282 return Result;
283}
284
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000285void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
286 PPEntityID PPID) {
287 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000288}
289
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000290/// \brief Retrieve the preprocessed entity at the given ID.
291PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
292 if (PPID < 0) {
293 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
294 "Out-of bounds loaded preprocessed entity");
295 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
296 }
297 assert(unsigned(PPID) < PreprocessedEntities.size() &&
298 "Out-of bounds local preprocessed entity");
299 return PreprocessedEntities[PPID];
300}
301
302/// \brief Retrieve the loaded preprocessed entity at the given index.
303PreprocessedEntity *
304PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
305 assert(Index < LoadedPreprocessedEntities.size() &&
306 "Out-of bounds loaded preprocessed entity");
307 assert(ExternalSource && "No external source to load from");
308 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
309 if (!Entity) {
310 Entity = ExternalSource->ReadPreprocessedEntity(Index);
311 if (!Entity) // Failed to load.
312 Entity = new (*this)
313 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
314 }
315 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000316}
317
Douglas Gregor8aaca672010-03-19 21:58:23 +0000318MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000319 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000320 = MacroDefinitions.find(MI);
321 if (Pos == MacroDefinitions.end())
322 return 0;
323
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000324 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
325 if (Entity->isInvalid())
326 return 0;
327 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000328}
329
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000330void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
331 SourceRange Range) {
Argyrios Kyrtzidis335c5a42012-02-25 02:41:16 +0000332 // We don't record nested macro expansions.
333 if (Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000334 return;
335
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000336 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000337 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000338 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
339 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000340 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000341 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000342}
343
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000344void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000345 const MacroInfo *MI) {
346 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
347 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000348 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000349 addPreprocessedEntity(Def);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000350 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
351 /*isLoaded=*/false);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000352}
Douglas Gregoraae92242010-03-19 21:51:54 +0000353
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000354void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000355 const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000356 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor8aaca672010-03-19 21:58:23 +0000357 = MacroDefinitions.find(MI);
358 if (Pos != MacroDefinitions.end())
359 MacroDefinitions.erase(Pos);
360}
361
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000362void PreprocessingRecord::InclusionDirective(
363 SourceLocation HashLoc,
364 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000366 bool IsAngled,
367 const FileEntry *File,
368 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000369 StringRef SearchPath,
370 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000371 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
372
373 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
374 case tok::pp_include:
375 Kind = InclusionDirective::Include;
376 break;
377
378 case tok::pp_import:
379 Kind = InclusionDirective::Import;
380 break;
381
382 case tok::pp_include_next:
383 Kind = InclusionDirective::IncludeNext;
384 break;
385
386 case tok::pp___include_macros:
387 Kind = InclusionDirective::IncludeMacros;
388 break;
389
390 default:
391 llvm_unreachable("Unknown include directive kind");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000392 }
393
394 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000395 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
396 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000397 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000398}
Ted Kremenek182543a2011-07-26 21:17:24 +0000399
400size_t PreprocessingRecord::getTotalMemory() const {
401 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000402 + llvm::capacity_in_bytes(MacroDefinitions)
403 + llvm::capacity_in_bytes(PreprocessedEntities)
404 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000405}