blob: 2816609d5f8ff5ec9d13a20c73fe13f597d9a00d [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())
52 return std::make_pair(iterator(this, 0), iterator(this, 0));
53 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
54
55 std::pair<unsigned, unsigned>
56 Local = findLocalPreprocessedEntitiesInRange(Range);
57
58 // Check if range spans local entities.
59 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
60 return std::make_pair(iterator(this, Local.first),
61 iterator(this, Local.second));
62
63 std::pair<unsigned, unsigned>
64 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
65
66 // Check if range spans local entities.
67 if (Loaded.first == Loaded.second)
68 return std::make_pair(iterator(this, Local.first),
69 iterator(this, Local.second));
70
71 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
72
73 // Check if range spans loaded entities.
74 if (Local.first == Local.second)
75 return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded),
76 iterator(this, int(Loaded.second)-TotalLoaded));
77
78 // Range spands loaded and local entities.
79 return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded),
80 iterator(this, Local.second));
81}
82
83std::pair<unsigned, unsigned>
84PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
85 SourceRange Range) const {
86 if (Range.isInvalid())
87 return std::make_pair(0,0);
88 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
89
90 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
91 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
92 return std::make_pair(Begin, End);
93}
94
95namespace {
96
97template <SourceLocation (SourceRange::*getRangeLoc)() const>
98struct PPEntityComp {
99 const SourceManager &SM;
100
101 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
102
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000103 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
104 SourceLocation LHS = getLoc(L);
105 SourceLocation RHS = getLoc(R);
106 return SM.isBeforeInTranslationUnit(LHS, RHS);
107 }
108
109 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000110 SourceLocation LHS = getLoc(L);
111 return SM.isBeforeInTranslationUnit(LHS, RHS);
112 }
113
Benjamin Kramer2e9d9cf2011-09-21 16:58:20 +0000114 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000115 SourceLocation RHS = getLoc(R);
116 return SM.isBeforeInTranslationUnit(LHS, RHS);
117 }
118
119 SourceLocation getLoc(PreprocessedEntity *PPE) const {
Argyrios Kyrtzidisa35c4442011-09-19 22:02:08 +0000120 SourceRange Range = PPE->getSourceRange();
121 return (Range.*getRangeLoc)();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000122 }
123};
124
125}
126
127unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
128 SourceLocation Loc) const {
129 if (SourceMgr.isLoadedSourceLocation(Loc))
130 return 0;
131
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000132 size_t Count = PreprocessedEntities.size();
133 size_t Half;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000134 std::vector<PreprocessedEntity *>::const_iterator
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +0000135 First = PreprocessedEntities.begin();
136 std::vector<PreprocessedEntity *>::const_iterator I;
137
138 // Do a binary search manually instead of using std::lower_bound because
139 // The end locations of entities may be unordered (when a macro expansion
140 // is inside another macro argument), but for this case it is not important
141 // whether we get the first macro expansion or its containing macro.
142 while (Count > 0) {
143 Half = Count/2;
144 I = First;
145 std::advance(I, Half);
146 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
147 Loc)){
148 First = I;
149 ++First;
150 Count = Count - Half - 1;
151 } else
152 Count = Half;
153 }
154
155 return First - PreprocessedEntities.begin();
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000156}
157
158unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
159 SourceLocation Loc) const {
160 if (SourceMgr.isLoadedSourceLocation(Loc))
161 return 0;
162
163 std::vector<PreprocessedEntity *>::const_iterator
164 I = std::upper_bound(PreprocessedEntities.begin(),
165 PreprocessedEntities.end(),
166 Loc,
167 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
168 return I - PreprocessedEntities.begin();
169}
170
Douglas Gregor065f8d12010-03-18 17:52:52 +0000171void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis45e8cf52011-09-20 23:27:33 +0000172 assert(Entity);
Argyrios Kyrtzidisf37d0a62011-10-12 17:36:33 +0000173 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
174
175 // Check normal case, this entity begin location is after the previous one.
176 if (PreprocessedEntities.empty() ||
177 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
178 PreprocessedEntities.back()->getSourceRange().getBegin())) {
179 PreprocessedEntities.push_back(Entity);
180 return;
181 }
182
183 // The entity's location is not after the previous one; this can happen rarely
184 // e.g. with "#include MACRO".
185 // Iterate the entities vector in reverse until we find the right place to
186 // insert the new entity.
187 for (std::vector<PreprocessedEntity *>::iterator
188 RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
189 RI != Begin; --RI) {
190 std::vector<PreprocessedEntity *>::iterator I = RI;
191 --I;
192 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
193 (*I)->getSourceRange().getBegin())) {
194 PreprocessedEntities.insert(RI, Entity);
195 return;
196 }
197 }
Douglas Gregor065f8d12010-03-18 17:52:52 +0000198}
199
Douglas Gregoraae92242010-03-19 21:51:54 +0000200void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000201 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000202 assert(!ExternalSource &&
203 "Preprocessing record already has an external source");
204 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000205}
206
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000207unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
208 unsigned Result = LoadedPreprocessedEntities.size();
209 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
210 + NumEntities);
211 return Result;
212}
213
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000214void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
215 PPEntityID PPID) {
216 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000217}
218
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000219/// \brief Retrieve the preprocessed entity at the given ID.
220PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
221 if (PPID < 0) {
222 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
223 "Out-of bounds loaded preprocessed entity");
224 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
225 }
226 assert(unsigned(PPID) < PreprocessedEntities.size() &&
227 "Out-of bounds local preprocessed entity");
228 return PreprocessedEntities[PPID];
229}
230
231/// \brief Retrieve the loaded preprocessed entity at the given index.
232PreprocessedEntity *
233PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
234 assert(Index < LoadedPreprocessedEntities.size() &&
235 "Out-of bounds loaded preprocessed entity");
236 assert(ExternalSource && "No external source to load from");
237 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
238 if (!Entity) {
239 Entity = ExternalSource->ReadPreprocessedEntity(Index);
240 if (!Entity) // Failed to load.
241 Entity = new (*this)
242 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
243 }
244 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000245}
246
Douglas Gregor8aaca672010-03-19 21:58:23 +0000247MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000248 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000249 = MacroDefinitions.find(MI);
250 if (Pos == MacroDefinitions.end())
251 return 0;
252
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000253 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
254 if (Entity->isInvalid())
255 return 0;
256 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000257}
258
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000259void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
260 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000261 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000262 return;
263
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000264 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000265 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000266 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
267 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000268 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000269 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000270}
271
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000272void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000273 const MacroInfo *MI) {
274 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
275 MacroDefinition *Def
Argyrios Kyrtzidis0d48fb82011-09-20 22:14:48 +0000276 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000277 addPreprocessedEntity(Def);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000278 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
279 /*isLoaded=*/false);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000280}
Douglas Gregoraae92242010-03-19 21:51:54 +0000281
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000282void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000283 const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000284 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor8aaca672010-03-19 21:58:23 +0000285 = MacroDefinitions.find(MI);
286 if (Pos != MacroDefinitions.end())
287 MacroDefinitions.erase(Pos);
288}
289
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000290void PreprocessingRecord::InclusionDirective(
291 SourceLocation HashLoc,
292 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000293 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000294 bool IsAngled,
295 const FileEntry *File,
296 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 StringRef SearchPath,
298 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000299 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
300
301 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
302 case tok::pp_include:
303 Kind = InclusionDirective::Include;
304 break;
305
306 case tok::pp_import:
307 Kind = InclusionDirective::Import;
308 break;
309
310 case tok::pp_include_next:
311 Kind = InclusionDirective::IncludeNext;
312 break;
313
314 case tok::pp___include_macros:
315 Kind = InclusionDirective::IncludeMacros;
316 break;
317
318 default:
319 llvm_unreachable("Unknown include directive kind");
320 return;
321 }
322
323 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000324 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
325 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000326 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000327}
Ted Kremenek182543a2011-07-26 21:17:24 +0000328
329size_t PreprocessingRecord::getTotalMemory() const {
330 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000331 + llvm::capacity_in_bytes(MacroDefinitions)
332 + llvm::capacity_in_bytes(PreprocessedEntities)
333 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000334}