1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H |
15 | #define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H |
16 | |
17 | #include "clang/Basic/IdentifierTable.h" |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "clang/Basic/SourceLocation.h" |
20 | #include "clang/Lex/PPCallbacks.h" |
21 | #include "llvm/ADT/DenseMap.h" |
22 | #include "llvm/ADT/None.h" |
23 | #include "llvm/ADT/Optional.h" |
24 | #include "llvm/ADT/PointerUnion.h" |
25 | #include "llvm/ADT/StringRef.h" |
26 | #include "llvm/ADT/iterator.h" |
27 | #include "llvm/ADT/iterator_range.h" |
28 | #include "llvm/Support/Allocator.h" |
29 | #include "llvm/Support/Compiler.h" |
30 | #include <cassert> |
31 | #include <cstddef> |
32 | #include <iterator> |
33 | #include <utility> |
34 | #include <vector> |
35 | |
36 | namespace clang { |
37 | |
38 | class PreprocessingRecord; |
39 | |
40 | } |
41 | |
42 | |
43 | void *operator new(size_t bytes, clang::PreprocessingRecord &PR, |
44 | unsigned alignment = 8) noexcept; |
45 | |
46 | |
47 | void operator delete(void *ptr, clang::PreprocessingRecord &PR, |
48 | unsigned) noexcept; |
49 | |
50 | namespace clang { |
51 | |
52 | class FileEntry; |
53 | class IdentifierInfo; |
54 | class MacroInfo; |
55 | class SourceManager; |
56 | class Token; |
57 | |
58 | |
59 | |
60 | class PreprocessedEntity { |
61 | public: |
62 | |
63 | enum EntityKind { |
64 | |
65 | InvalidKind, |
66 | |
67 | |
68 | MacroExpansionKind, |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | MacroDefinitionKind, |
75 | |
76 | |
77 | |
78 | InclusionDirectiveKind, |
79 | |
80 | |
81 | |
82 | FirstPreprocessingDirective = MacroDefinitionKind, |
83 | LastPreprocessingDirective = InclusionDirectiveKind |
84 | }; |
85 | |
86 | private: |
87 | |
88 | EntityKind Kind; |
89 | |
90 | |
91 | SourceRange Range; |
92 | |
93 | protected: |
94 | friend class PreprocessingRecord; |
95 | |
96 | PreprocessedEntity(EntityKind Kind, SourceRange Range) |
97 | : Kind(Kind), Range(Range) {} |
98 | |
99 | public: |
100 | |
101 | EntityKind getKind() const { return Kind; } |
102 | |
103 | |
104 | |
105 | SourceRange getSourceRange() const LLVM_READONLY { return Range; } |
106 | |
107 | |
108 | |
109 | bool isInvalid() const { return Kind == InvalidKind; } |
110 | |
111 | |
112 | |
113 | void *operator new(size_t bytes, PreprocessingRecord &PR, |
114 | unsigned alignment = 8) noexcept { |
115 | return ::operator new(bytes, PR, alignment); |
116 | } |
117 | |
118 | void *operator new(size_t bytes, void *mem) noexcept { return mem; } |
119 | |
120 | void operator delete(void *ptr, PreprocessingRecord &PR, |
121 | unsigned alignment) noexcept { |
122 | return ::operator delete(ptr, PR, alignment); |
123 | } |
124 | |
125 | void operator delete(void *, std::size_t) noexcept {} |
126 | void operator delete(void *, void *) noexcept {} |
127 | |
128 | private: |
129 | |
130 | void *operator new(size_t bytes) noexcept; |
131 | void operator delete(void *data) noexcept; |
132 | }; |
133 | |
134 | |
135 | class PreprocessingDirective : public PreprocessedEntity { |
136 | public: |
137 | PreprocessingDirective(EntityKind Kind, SourceRange Range) |
138 | : PreprocessedEntity(Kind, Range) {} |
139 | |
140 | |
141 | static bool classof(const PreprocessedEntity *PD) { |
142 | return PD->getKind() >= FirstPreprocessingDirective && |
143 | PD->getKind() <= LastPreprocessingDirective; |
144 | } |
145 | }; |
146 | |
147 | |
148 | class MacroDefinitionRecord : public PreprocessingDirective { |
149 | |
150 | const IdentifierInfo *Name; |
151 | |
152 | public: |
153 | explicit MacroDefinitionRecord(const IdentifierInfo *Name, |
154 | SourceRange Range) |
155 | : PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) {} |
156 | |
157 | |
158 | const IdentifierInfo *getName() const { return Name; } |
159 | |
160 | |
161 | SourceLocation getLocation() const { return getSourceRange().getBegin(); } |
162 | |
163 | |
164 | static bool classof(const PreprocessedEntity *PE) { |
165 | return PE->getKind() == MacroDefinitionKind; |
166 | } |
167 | }; |
168 | |
169 | |
170 | class MacroExpansion : public PreprocessedEntity { |
171 | |
172 | |
173 | llvm::PointerUnion<IdentifierInfo *, MacroDefinitionRecord *> NameOrDef; |
174 | |
175 | public: |
176 | MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range) |
177 | : PreprocessedEntity(MacroExpansionKind, Range), |
178 | NameOrDef(BuiltinName) {} |
179 | |
180 | MacroExpansion(MacroDefinitionRecord *Definition, SourceRange Range) |
181 | : PreprocessedEntity(MacroExpansionKind, Range), NameOrDef(Definition) { |
182 | } |
183 | |
184 | |
185 | bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); } |
186 | |
187 | |
188 | const IdentifierInfo *getName() const { |
189 | if (MacroDefinitionRecord *Def = getDefinition()) |
190 | return Def->getName(); |
191 | return NameOrDef.get<IdentifierInfo *>(); |
192 | } |
193 | |
194 | |
195 | |
196 | MacroDefinitionRecord *getDefinition() const { |
197 | return NameOrDef.dyn_cast<MacroDefinitionRecord *>(); |
198 | } |
199 | |
200 | |
201 | static bool classof(const PreprocessedEntity *PE) { |
202 | return PE->getKind() == MacroExpansionKind; |
203 | } |
204 | }; |
205 | |
206 | |
207 | |
208 | class InclusionDirective : public PreprocessingDirective { |
209 | public: |
210 | |
211 | |
212 | enum InclusionKind { |
213 | |
214 | Include, |
215 | |
216 | |
217 | Import, |
218 | |
219 | |
220 | IncludeNext, |
221 | |
222 | |
223 | IncludeMacros |
224 | }; |
225 | |
226 | private: |
227 | |
228 | |
229 | StringRef FileName; |
230 | |
231 | |
232 | |
233 | unsigned InQuotes : 1; |
234 | |
235 | |
236 | |
237 | |
238 | unsigned Kind : 2; |
239 | |
240 | |
241 | |
242 | unsigned ImportedModule : 1; |
243 | |
244 | |
245 | const FileEntry *File; |
246 | |
247 | public: |
248 | InclusionDirective(PreprocessingRecord &PPRec, |
249 | InclusionKind Kind, StringRef FileName, |
250 | bool InQuotes, bool ImportedModule, |
251 | const FileEntry *File, SourceRange Range); |
252 | |
253 | |
254 | InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); } |
255 | |
256 | |
257 | StringRef getFileName() const { return FileName; } |
258 | |
259 | |
260 | |
261 | bool wasInQuotes() const { return InQuotes; } |
262 | |
263 | |
264 | |
265 | bool importedModule() const { return ImportedModule; } |
266 | |
267 | |
268 | |
269 | const FileEntry *getFile() const { return File; } |
270 | |
271 | |
272 | static bool classof(const PreprocessedEntity *PE) { |
273 | return PE->getKind() == InclusionDirectiveKind; |
274 | } |
275 | }; |
276 | |
277 | |
278 | |
279 | class ExternalPreprocessingRecordSource { |
280 | public: |
281 | virtual ~ExternalPreprocessingRecordSource(); |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0; |
288 | |
289 | |
290 | |
291 | virtual std::pair<unsigned, unsigned> |
292 | findPreprocessedEntitiesInRange(SourceRange Range) = 0; |
293 | |
294 | |
295 | |
296 | virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index, |
297 | FileID FID) { |
298 | return None; |
299 | } |
300 | |
301 | |
302 | virtual SourceRange ReadSkippedRange(unsigned Index) = 0; |
303 | }; |
304 | |
305 | |
306 | |
307 | |
308 | class PreprocessingRecord : public PPCallbacks { |
309 | SourceManager &SourceMgr; |
310 | |
311 | |
312 | llvm::BumpPtrAllocator BumpAlloc; |
313 | |
314 | |
315 | |
316 | std::vector<PreprocessedEntity *> PreprocessedEntities; |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | std::vector<PreprocessedEntity *> LoadedPreprocessedEntities; |
324 | |
325 | |
326 | std::vector<SourceRange> SkippedRanges; |
327 | |
328 | bool SkippedRangesAllLoaded = true; |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | class PPEntityID { |
340 | friend class PreprocessingRecord; |
341 | |
342 | int ID = 0; |
343 | |
344 | explicit PPEntityID(int ID) : ID(ID) {} |
345 | |
346 | public: |
347 | PPEntityID() = default; |
348 | }; |
349 | |
350 | static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) { |
351 | return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1); |
352 | } |
353 | |
354 | |
355 | llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *> MacroDefinitions; |
356 | |
357 | |
358 | ExternalPreprocessingRecordSource *ExternalSource = nullptr; |
359 | |
360 | |
361 | PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID); |
362 | |
363 | |
364 | PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index); |
365 | |
366 | |
367 | |
368 | unsigned getNumLoadedPreprocessedEntities() const { |
369 | return LoadedPreprocessedEntities.size(); |
370 | } |
371 | |
372 | |
373 | |
374 | std::pair<unsigned, unsigned> |
375 | findLocalPreprocessedEntitiesInRange(SourceRange Range) const; |
376 | unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const; |
377 | unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const; |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | unsigned allocateLoadedEntities(unsigned NumEntities); |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | unsigned allocateSkippedRanges(unsigned NumRanges); |
391 | |
392 | |
393 | void ensureSkippedRangesLoaded(); |
394 | |
395 | |
396 | void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinitionRecord *Def); |
397 | |
398 | public: |
399 | |
400 | explicit PreprocessingRecord(SourceManager &SM); |
401 | |
402 | |
403 | void *Allocate(unsigned Size, unsigned Align = 8) { |
404 | return BumpAlloc.Allocate(Size, Align); |
405 | } |
406 | |
407 | |
408 | void Deallocate(void *Ptr) {} |
409 | |
410 | size_t getTotalMemory() const; |
411 | |
412 | SourceManager &getSourceManager() const { return SourceMgr; } |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | |
430 | class iterator : public llvm::iterator_adaptor_base< |
431 | iterator, int, std::random_access_iterator_tag, |
432 | PreprocessedEntity *, int, PreprocessedEntity *, |
433 | PreprocessedEntity *> { |
434 | friend class PreprocessingRecord; |
435 | |
436 | PreprocessingRecord *Self; |
437 | |
438 | iterator(PreprocessingRecord *Self, int Position) |
439 | : iterator::iterator_adaptor_base(Position), Self(Self) {} |
440 | |
441 | public: |
442 | iterator() : iterator(nullptr, 0) {} |
443 | |
444 | PreprocessedEntity *operator*() const { |
445 | bool isLoaded = this->I < 0; |
446 | unsigned Index = isLoaded ? |
447 | Self->LoadedPreprocessedEntities.size() + this->I : this->I; |
448 | PPEntityID ID = Self->getPPEntityID(Index, isLoaded); |
449 | return Self->getPreprocessedEntity(ID); |
450 | } |
451 | PreprocessedEntity *operator->() const { return **this; } |
452 | }; |
453 | |
454 | |
455 | iterator begin() { |
456 | return iterator(this, -(int)LoadedPreprocessedEntities.size()); |
457 | } |
458 | |
459 | |
460 | iterator end() { |
461 | return iterator(this, PreprocessedEntities.size()); |
462 | } |
463 | |
464 | |
465 | iterator local_begin() { |
466 | return iterator(this, 0); |
467 | } |
468 | |
469 | |
470 | iterator local_end() { |
471 | return iterator(this, PreprocessedEntities.size()); |
472 | } |
473 | |
474 | |
475 | |
476 | llvm::iterator_range<iterator> getIteratorsForLoadedRange(unsigned start, |
477 | unsigned count) { |
478 | unsigned end = start + count; |
479 | assert(end <= LoadedPreprocessedEntities.size()); |
480 | return llvm::make_range( |
481 | iterator(this, int(start) - LoadedPreprocessedEntities.size()), |
482 | iterator(this, int(end) - LoadedPreprocessedEntities.size())); |
483 | } |
484 | |
485 | |
486 | |
487 | |
488 | |
489 | llvm::iterator_range<iterator> |
490 | getPreprocessedEntitiesInRange(SourceRange R); |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | |
497 | |
498 | |
499 | bool isEntityInFileID(iterator PPEI, FileID FID); |
500 | |
501 | |
502 | PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity); |
503 | |
504 | |
505 | void SetExternalSource(ExternalPreprocessingRecordSource &Source); |
506 | |
507 | |
508 | ExternalPreprocessingRecordSource *getExternalSource() const { |
509 | return ExternalSource; |
510 | } |
511 | |
512 | |
513 | |
514 | MacroDefinitionRecord *findMacroDefinition(const MacroInfo *MI); |
515 | |
516 | |
517 | const std::vector<SourceRange> &getSkippedRanges() { |
518 | ensureSkippedRangesLoaded(); |
519 | return SkippedRanges; |
520 | } |
521 | |
522 | private: |
523 | friend class ASTReader; |
524 | friend class ASTWriter; |
525 | |
526 | void MacroExpands(const Token &Id, const MacroDefinition &MD, |
527 | SourceRange Range, const MacroArgs *Args) override; |
528 | void MacroDefined(const Token &Id, const MacroDirective *MD) override; |
529 | void MacroUndefined(const Token &Id, const MacroDefinition &MD, |
530 | const MacroDirective *Undef) override; |
531 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
532 | StringRef FileName, bool IsAngled, |
533 | CharSourceRange FilenameRange, |
534 | const FileEntry *File, StringRef SearchPath, |
535 | StringRef RelativePath, const Module *Imported, |
536 | SrcMgr::CharacteristicKind FileType) override; |
537 | void Ifdef(SourceLocation Loc, const Token &MacroNameTok, |
538 | const MacroDefinition &MD) override; |
539 | void Ifndef(SourceLocation Loc, const Token &MacroNameTok, |
540 | const MacroDefinition &MD) override; |
541 | |
542 | |
543 | void Defined(const Token &MacroNameTok, const MacroDefinition &MD, |
544 | SourceRange Range) override; |
545 | |
546 | void SourceRangeSkipped(SourceRange Range, |
547 | SourceLocation EndifLoc) override; |
548 | |
549 | void addMacroExpansion(const Token &Id, const MacroInfo *MI, |
550 | SourceRange Range); |
551 | |
552 | |
553 | |
554 | struct { |
555 | SourceRange Range; |
556 | std::pair<int, int> Result; |
557 | } CachedRangeQuery; |
558 | |
559 | std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R); |
560 | }; |
561 | |
562 | } |
563 | |
564 | inline void *operator new(size_t bytes, clang::PreprocessingRecord &PR, |
565 | unsigned alignment) noexcept { |
566 | return PR.Allocate(bytes, alignment); |
567 | } |
568 | |
569 | inline void operator delete(void *ptr, clang::PreprocessingRecord &PR, |
570 | unsigned) noexcept { |
571 | PR.Deallocate(ptr); |
572 | } |
573 | |
574 | #endif |
575 | |