1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_LEX_MACROINFO_H |
15 | #define LLVM_CLANG_LEX_MACROINFO_H |
16 | |
17 | #include "clang/Lex/Token.h" |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "clang/Basic/SourceLocation.h" |
20 | #include "llvm/ADT/ArrayRef.h" |
21 | #include "llvm/ADT/FoldingSet.h" |
22 | #include "llvm/ADT/PointerIntPair.h" |
23 | #include "llvm/ADT/SmallVector.h" |
24 | #include "llvm/Support/Allocator.h" |
25 | #include <algorithm> |
26 | #include <cassert> |
27 | |
28 | namespace clang { |
29 | |
30 | class DefMacroDirective; |
31 | class IdentifierInfo; |
32 | class Module; |
33 | class Preprocessor; |
34 | class SourceManager; |
35 | |
36 | |
37 | |
38 | |
39 | class MacroInfo { |
40 | |
41 | |
42 | |
43 | |
44 | SourceLocation Location; |
45 | |
46 | |
47 | SourceLocation EndLocation; |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | IdentifierInfo **ParameterList = nullptr; |
56 | |
57 | |
58 | unsigned NumParameters = 0; |
59 | |
60 | |
61 | SmallVector<Token, 8> ReplacementTokens; |
62 | |
63 | |
64 | mutable unsigned DefinitionLength; |
65 | mutable bool IsDefinitionLengthCached : 1; |
66 | |
67 | |
68 | bool IsFunctionLike : 1; |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | bool IsC99Varargs : 1; |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | bool IsGNUVarargs : 1; |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | bool IsBuiltinMacro : 1; |
89 | |
90 | |
91 | bool HasCommaPasting : 1; |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | bool IsDisabled : 1; |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | bool IsUsed : 1; |
107 | |
108 | |
109 | bool IsAllowRedefinitionsWithoutWarning : 1; |
110 | |
111 | |
112 | bool IsWarnIfUnused : 1; |
113 | |
114 | |
115 | bool : 1; |
116 | |
117 | |
118 | MacroInfo(SourceLocation DefLoc); |
119 | ~MacroInfo() = default; |
120 | |
121 | public: |
122 | |
123 | SourceLocation getDefinitionLoc() const { return Location; } |
124 | |
125 | |
126 | void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; } |
127 | |
128 | |
129 | SourceLocation getDefinitionEndLoc() const { return EndLocation; } |
130 | |
131 | |
132 | unsigned getDefinitionLength(const SourceManager &SM) const { |
133 | if (IsDefinitionLengthCached) |
134 | return DefinitionLength; |
135 | return getDefinitionLengthSlow(SM); |
136 | } |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, |
146 | bool Syntactically) const; |
147 | |
148 | |
149 | void setIsBuiltinMacro(bool Val = true) { IsBuiltinMacro = Val; } |
150 | |
151 | |
152 | void setIsUsed(bool Val) { IsUsed = Val; } |
153 | |
154 | |
155 | void setIsAllowRedefinitionsWithoutWarning(bool Val) { |
156 | IsAllowRedefinitionsWithoutWarning = Val; |
157 | } |
158 | |
159 | |
160 | void setIsWarnIfUnused(bool val) { IsWarnIfUnused = val; } |
161 | |
162 | |
163 | |
164 | void setParameterList(ArrayRef<IdentifierInfo *> List, |
165 | llvm::BumpPtrAllocator &PPAllocator) { |
166 | (0) . __assert_fail ("ParameterList == nullptr && NumParameters == 0 && \"Parameter list already set!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 167, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(ParameterList == nullptr && NumParameters == 0 && |
167 | (0) . __assert_fail ("ParameterList == nullptr && NumParameters == 0 && \"Parameter list already set!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 167, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Parameter list already set!"); |
168 | if (List.empty()) |
169 | return; |
170 | |
171 | NumParameters = List.size(); |
172 | ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size()); |
173 | std::copy(List.begin(), List.end(), ParameterList); |
174 | } |
175 | |
176 | |
177 | |
178 | using param_iterator = IdentifierInfo *const *; |
179 | bool param_empty() const { return NumParameters == 0; } |
180 | param_iterator param_begin() const { return ParameterList; } |
181 | param_iterator param_end() const { return ParameterList + NumParameters; } |
182 | unsigned getNumParams() const { return NumParameters; } |
183 | ArrayRef<const IdentifierInfo *> params() const { |
184 | return ArrayRef<const IdentifierInfo *>(ParameterList, NumParameters); |
185 | } |
186 | |
187 | |
188 | |
189 | int getParameterNum(const IdentifierInfo *Arg) const { |
190 | for (param_iterator I = param_begin(), E = param_end(); I != E; ++I) |
191 | if (*I == Arg) |
192 | return I - param_begin(); |
193 | return -1; |
194 | } |
195 | |
196 | |
197 | |
198 | void setIsFunctionLike() { IsFunctionLike = true; } |
199 | bool isFunctionLike() const { return IsFunctionLike; } |
200 | bool isObjectLike() const { return !IsFunctionLike; } |
201 | |
202 | |
203 | void setIsC99Varargs() { IsC99Varargs = true; } |
204 | void setIsGNUVarargs() { IsGNUVarargs = true; } |
205 | bool isC99Varargs() const { return IsC99Varargs; } |
206 | bool isGNUVarargs() const { return IsGNUVarargs; } |
207 | bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; } |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | bool isBuiltinMacro() const { return IsBuiltinMacro; } |
216 | |
217 | bool hasCommaPasting() const { return HasCommaPasting; } |
218 | void setHasCommaPasting() { HasCommaPasting = true; } |
219 | |
220 | |
221 | |
222 | bool isUsed() const { return IsUsed; } |
223 | |
224 | |
225 | bool isAllowRedefinitionsWithoutWarning() const { |
226 | return IsAllowRedefinitionsWithoutWarning; |
227 | } |
228 | |
229 | |
230 | bool isWarnIfUnused() const { return IsWarnIfUnused; } |
231 | |
232 | |
233 | unsigned getNumTokens() const { return ReplacementTokens.size(); } |
234 | |
235 | const Token &getReplacementToken(unsigned Tok) const { |
236 | (0) . __assert_fail ("Tok < ReplacementTokens.size() && \"Invalid token #\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 236, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Tok < ReplacementTokens.size() && "Invalid token #"); |
237 | return ReplacementTokens[Tok]; |
238 | } |
239 | |
240 | using tokens_iterator = SmallVectorImpl<Token>::const_iterator; |
241 | |
242 | tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); } |
243 | tokens_iterator tokens_end() const { return ReplacementTokens.end(); } |
244 | bool tokens_empty() const { return ReplacementTokens.empty(); } |
245 | ArrayRef<Token> tokens() const { return ReplacementTokens; } |
246 | |
247 | |
248 | void AddTokenToBody(const Token &Tok) { |
249 | (0) . __assert_fail ("!IsDefinitionLengthCached && \"Changing replacement tokens after definition length got calculated\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 251, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert( |
250 | (0) . __assert_fail ("!IsDefinitionLengthCached && \"Changing replacement tokens after definition length got calculated\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 251, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> !IsDefinitionLengthCached && |
251 | (0) . __assert_fail ("!IsDefinitionLengthCached && \"Changing replacement tokens after definition length got calculated\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 251, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Changing replacement tokens after definition length got calculated"); |
252 | ReplacementTokens.push_back(Tok); |
253 | } |
254 | |
255 | |
256 | |
257 | |
258 | bool isEnabled() const { return !IsDisabled; } |
259 | |
260 | void EnableMacro() { |
261 | (0) . __assert_fail ("IsDisabled && \"Cannot enable an already-enabled macro!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 261, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(IsDisabled && "Cannot enable an already-enabled macro!"); |
262 | IsDisabled = false; |
263 | } |
264 | |
265 | void DisableMacro() { |
266 | (0) . __assert_fail ("!IsDisabled && \"Cannot disable an already-disabled macro!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 266, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!IsDisabled && "Cannot disable an already-disabled macro!"); |
267 | IsDisabled = true; |
268 | } |
269 | |
270 | |
271 | bool () const { return UsedForHeaderGuard; } |
272 | |
273 | void (bool Val) { UsedForHeaderGuard = Val; } |
274 | |
275 | void dump() const; |
276 | |
277 | private: |
278 | friend class Preprocessor; |
279 | |
280 | unsigned getDefinitionLengthSlow(const SourceManager &SM) const; |
281 | }; |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | class MacroDirective { |
291 | public: |
292 | enum Kind { |
293 | MD_Define, |
294 | MD_Undefine, |
295 | MD_Visibility |
296 | }; |
297 | |
298 | protected: |
299 | |
300 | MacroDirective *Previous = nullptr; |
301 | |
302 | SourceLocation Loc; |
303 | |
304 | |
305 | unsigned MDKind : 2; |
306 | |
307 | |
308 | unsigned IsFromPCH : 1; |
309 | |
310 | |
311 | |
312 | |
313 | |
314 | unsigned IsPublic : 1; |
315 | |
316 | MacroDirective(Kind K, SourceLocation Loc) |
317 | : Loc(Loc), MDKind(K), IsFromPCH(false), IsPublic(true) {} |
318 | |
319 | public: |
320 | Kind getKind() const { return Kind(MDKind); } |
321 | |
322 | SourceLocation getLocation() const { return Loc; } |
323 | |
324 | |
325 | void setPrevious(MacroDirective *Prev) { Previous = Prev; } |
326 | |
327 | |
328 | const MacroDirective *getPrevious() const { return Previous; } |
329 | |
330 | |
331 | MacroDirective *getPrevious() { return Previous; } |
332 | |
333 | |
334 | bool isFromPCH() const { return IsFromPCH; } |
335 | |
336 | void setIsFromPCH() { IsFromPCH = true; } |
337 | |
338 | class DefInfo { |
339 | DefMacroDirective *DefDirective = nullptr; |
340 | SourceLocation UndefLoc; |
341 | bool IsPublic = true; |
342 | |
343 | public: |
344 | DefInfo() = default; |
345 | DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, |
346 | bool isPublic) |
347 | : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {} |
348 | |
349 | const DefMacroDirective *getDirective() const { return DefDirective; } |
350 | DefMacroDirective *getDirective() { return DefDirective; } |
351 | |
352 | inline SourceLocation getLocation() const; |
353 | inline MacroInfo *getMacroInfo(); |
354 | |
355 | const MacroInfo *getMacroInfo() const { |
356 | return const_cast<DefInfo *>(this)->getMacroInfo(); |
357 | } |
358 | |
359 | SourceLocation getUndefLocation() const { return UndefLoc; } |
360 | bool isUndefined() const { return UndefLoc.isValid(); } |
361 | |
362 | bool isPublic() const { return IsPublic; } |
363 | |
364 | bool isValid() const { return DefDirective != nullptr; } |
365 | bool isInvalid() const { return !isValid(); } |
366 | |
367 | explicit operator bool() const { return isValid(); } |
368 | |
369 | inline DefInfo getPreviousDefinition(); |
370 | |
371 | const DefInfo getPreviousDefinition() const { |
372 | return const_cast<DefInfo *>(this)->getPreviousDefinition(); |
373 | } |
374 | }; |
375 | |
376 | |
377 | |
378 | |
379 | DefInfo getDefinition(); |
380 | const DefInfo getDefinition() const { |
381 | return const_cast<MacroDirective *>(this)->getDefinition(); |
382 | } |
383 | |
384 | bool isDefined() const { |
385 | if (const DefInfo Def = getDefinition()) |
386 | return !Def.isUndefined(); |
387 | return false; |
388 | } |
389 | |
390 | const MacroInfo *getMacroInfo() const { |
391 | return getDefinition().getMacroInfo(); |
392 | } |
393 | MacroInfo *getMacroInfo() { return getDefinition().getMacroInfo(); } |
394 | |
395 | |
396 | |
397 | const DefInfo findDirectiveAtLoc(SourceLocation L, |
398 | const SourceManager &SM) const; |
399 | |
400 | void dump() const; |
401 | |
402 | static bool classof(const MacroDirective *) { return true; } |
403 | }; |
404 | |
405 | |
406 | class DefMacroDirective : public MacroDirective { |
407 | MacroInfo *Info; |
408 | |
409 | public: |
410 | DefMacroDirective(MacroInfo *MI, SourceLocation Loc) |
411 | : MacroDirective(MD_Define, Loc), Info(MI) { |
412 | (0) . __assert_fail ("MI && \"MacroInfo is null\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 412, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(MI && "MacroInfo is null"); |
413 | } |
414 | explicit DefMacroDirective(MacroInfo *MI) |
415 | : DefMacroDirective(MI, MI->getDefinitionLoc()) {} |
416 | |
417 | |
418 | const MacroInfo *getInfo() const { return Info; } |
419 | MacroInfo *getInfo() { return Info; } |
420 | |
421 | static bool classof(const MacroDirective *MD) { |
422 | return MD->getKind() == MD_Define; |
423 | } |
424 | |
425 | static bool classof(const DefMacroDirective *) { return true; } |
426 | }; |
427 | |
428 | |
429 | class UndefMacroDirective : public MacroDirective { |
430 | public: |
431 | explicit UndefMacroDirective(SourceLocation UndefLoc) |
432 | : MacroDirective(MD_Undefine, UndefLoc) { |
433 | (0) . __assert_fail ("UndefLoc.isValid() && \"Invalid UndefLoc!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Lex/MacroInfo.h", 433, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(UndefLoc.isValid() && "Invalid UndefLoc!"); |
434 | } |
435 | |
436 | static bool classof(const MacroDirective *MD) { |
437 | return MD->getKind() == MD_Undefine; |
438 | } |
439 | |
440 | static bool classof(const UndefMacroDirective *) { return true; } |
441 | }; |
442 | |
443 | |
444 | class VisibilityMacroDirective : public MacroDirective { |
445 | public: |
446 | explicit VisibilityMacroDirective(SourceLocation Loc, bool Public) |
447 | : MacroDirective(MD_Visibility, Loc) { |
448 | IsPublic = Public; |
449 | } |
450 | |
451 | |
452 | |
453 | bool isPublic() const { return IsPublic; } |
454 | |
455 | static bool classof(const MacroDirective *MD) { |
456 | return MD->getKind() == MD_Visibility; |
457 | } |
458 | |
459 | static bool classof(const VisibilityMacroDirective *) { return true; } |
460 | }; |
461 | |
462 | inline SourceLocation MacroDirective::DefInfo::getLocation() const { |
463 | if (isInvalid()) |
464 | return {}; |
465 | return DefDirective->getLocation(); |
466 | } |
467 | |
468 | inline MacroInfo *MacroDirective::DefInfo::getMacroInfo() { |
469 | if (isInvalid()) |
470 | return nullptr; |
471 | return DefDirective->getInfo(); |
472 | } |
473 | |
474 | inline MacroDirective::DefInfo |
475 | MacroDirective::DefInfo::getPreviousDefinition() { |
476 | if (isInvalid() || DefDirective->getPrevious() == nullptr) |
477 | return {}; |
478 | return DefDirective->getPrevious()->getDefinition(); |
479 | } |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | class ModuleMacro : public llvm::FoldingSetNode { |
489 | friend class Preprocessor; |
490 | |
491 | |
492 | IdentifierInfo *II; |
493 | |
494 | |
495 | MacroInfo *Macro; |
496 | |
497 | |
498 | Module *OwningModule; |
499 | |
500 | |
501 | unsigned NumOverriddenBy = 0; |
502 | |
503 | |
504 | unsigned NumOverrides; |
505 | |
506 | ModuleMacro(Module *OwningModule, IdentifierInfo *II, MacroInfo *Macro, |
507 | ArrayRef<ModuleMacro *> Overrides) |
508 | : II(II), Macro(Macro), OwningModule(OwningModule), |
509 | NumOverrides(Overrides.size()) { |
510 | std::copy(Overrides.begin(), Overrides.end(), |
511 | reinterpret_cast<ModuleMacro **>(this + 1)); |
512 | } |
513 | |
514 | public: |
515 | static ModuleMacro *create(Preprocessor &PP, Module *OwningModule, |
516 | IdentifierInfo *II, MacroInfo *Macro, |
517 | ArrayRef<ModuleMacro *> Overrides); |
518 | |
519 | void Profile(llvm::FoldingSetNodeID &ID) const { |
520 | return Profile(ID, OwningModule, II); |
521 | } |
522 | |
523 | static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule, |
524 | IdentifierInfo *II) { |
525 | ID.AddPointer(OwningModule); |
526 | ID.AddPointer(II); |
527 | } |
528 | |
529 | |
530 | IdentifierInfo *getName() const { return II; } |
531 | |
532 | |
533 | Module *getOwningModule() const { return OwningModule; } |
534 | |
535 | |
536 | |
537 | MacroInfo *getMacroInfo() const { return Macro; } |
538 | |
539 | |
540 | |
541 | using overrides_iterator = ModuleMacro *const *; |
542 | |
543 | overrides_iterator overrides_begin() const { |
544 | return reinterpret_cast<overrides_iterator>(this + 1); |
545 | } |
546 | |
547 | overrides_iterator overrides_end() const { |
548 | return overrides_begin() + NumOverrides; |
549 | } |
550 | |
551 | ArrayRef<ModuleMacro *> overrides() const { |
552 | return llvm::makeArrayRef(overrides_begin(), overrides_end()); |
553 | } |
554 | |
555 | |
556 | |
557 | unsigned getNumOverridingMacros() const { return NumOverriddenBy; } |
558 | }; |
559 | |
560 | |
561 | |
562 | |
563 | |
564 | class MacroDefinition { |
565 | llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous; |
566 | ArrayRef<ModuleMacro *> ModuleMacros; |
567 | |
568 | public: |
569 | MacroDefinition() = default; |
570 | MacroDefinition(DefMacroDirective *MD, ArrayRef<ModuleMacro *> MMs, |
571 | bool IsAmbiguous) |
572 | : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {} |
573 | |
574 | |
575 | explicit operator bool() const { |
576 | return getLocalDirective() || !ModuleMacros.empty(); |
577 | } |
578 | |
579 | |
580 | MacroInfo *getMacroInfo() const { |
581 | if (!ModuleMacros.empty()) |
582 | return ModuleMacros.back()->getMacroInfo(); |
583 | if (auto *MD = getLocalDirective()) |
584 | return MD->getMacroInfo(); |
585 | return nullptr; |
586 | } |
587 | |
588 | |
589 | bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); } |
590 | |
591 | |
592 | |
593 | DefMacroDirective *getLocalDirective() const { |
594 | return LatestLocalAndAmbiguous.getPointer(); |
595 | } |
596 | |
597 | |
598 | ArrayRef<ModuleMacro *> getModuleMacros() const { return ModuleMacros; } |
599 | |
600 | template <typename Fn> void forAllDefinitions(Fn F) const { |
601 | if (auto *MD = getLocalDirective()) |
602 | F(MD->getMacroInfo()); |
603 | for (auto *MM : getModuleMacros()) |
604 | F(MM->getMacroInfo()); |
605 | } |
606 | }; |
607 | |
608 | } |
609 | |
610 | #endif |
611 | |