clang: include/clang/Lex/MacroInfo.h Source File (original) (raw)
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
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
26#include
27
29
30class DefMacroDirective;
31class IdentifierInfo;
33class Preprocessor;
34class SourceManager;
35
36
37
38
40
41
42
43
45
46
48
49
50
51
52
53
54
56
57
58 const Token *ReplacementTokens = nullptr;
59
60
61 unsigned NumParameters = 0;
62
63
64 unsigned NumReplacementTokens = 0;
65
66
67 mutable unsigned DefinitionLength;
68 mutable bool IsDefinitionLengthCached : 1;
69
70
71 bool IsFunctionLike : 1;
72
73
74
75
76
77
78 bool IsC99Varargs : 1;
79
80
81
82
83
84 bool IsGNUVarargs : 1;
85
86
87
88
89
90
91 bool IsBuiltinMacro : 1;
92
93
94 bool HasCommaPasting : 1;
95
96
97
98
99
100
101
102
103 bool IsDisabled : 1;
104
105
106
107
108
109 bool IsUsed : 1;
110
111
112 bool IsAllowRedefinitionsWithoutWarning : 1;
113
114
115 bool IsWarnIfUnused : 1;
116
117
118 bool UsedForHeaderGuard : 1;
119
120
122
123public:
124
126
127
129
130
132
133
135 if (IsDefinitionLengthCached)
136 return DefinitionLength;
137 return getDefinitionLengthSlow(SM);
138 }
139
140
141
142
143
144
145
146
148 bool Syntactically) const;
149
150
152
153
155
156
158 IsAllowRedefinitionsWithoutWarning = Val;
159 }
160
161
163
164
165
167 llvm::BumpPtrAllocator &PPAllocator) {
168 assert(ParameterList == nullptr && NumParameters == 0 &&
169 "Parameter list already set!");
170 if (List.empty())
171 return;
172
173 NumParameters = List.size();
174 ParameterList = PPAllocator.Allocate<IdentifierInfo *>(List.size());
175 std::copy(List.begin(), List.end(), ParameterList);
176 }
177
178
179
181 bool param_empty() const { return NumParameters == 0; }
187 }
188
189
190
193 if (*I == Arg)
195 return -1;
196 }
197
198
199
203
204
209 bool isVariadic() const { return IsC99Varargs || IsGNUVarargs; }
210
211
212
213
214
215
216
218
221
222
223
224 bool isUsed() const { return IsUsed; }
225
226
228 return IsAllowRedefinitionsWithoutWarning;
229 }
230
231
233
234
235 unsigned getNumTokens() const { return NumReplacementTokens; }
236
238 assert(Tok < NumReplacementTokens && "Invalid token #");
239 return ReplacementTokens[Tok];
240 }
241
243
246 return ReplacementTokens + NumReplacementTokens;
247 }
248 bool tokens_empty() const { return NumReplacementTokens == 0; }
250 return llvm::ArrayRef(ReplacementTokens, NumReplacementTokens);
251 }
252
254 allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator) {
255 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
256 "Token list already allocated!");
257 NumReplacementTokens = NumTokens;
258 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(NumTokens);
259 ReplacementTokens = NewReplacementTokens;
261 }
262
264 assert(
265 !IsDefinitionLengthCached &&
266 "Changing replacement tokens after definition length got calculated");
267 assert(ReplacementTokens == nullptr && NumReplacementTokens == 0 &&
268 "Token list already set!");
269 if (Tokens.empty())
270 return;
271
272 NumReplacementTokens = Tokens.size();
273 Token *NewReplacementTokens = PPAllocator.Allocate<Token>(Tokens.size());
274 std::copy(Tokens.begin(), Tokens.end(), NewReplacementTokens);
275 ReplacementTokens = NewReplacementTokens;
276 }
277
278
279
280
281 bool isEnabled() const { return !IsDisabled; }
282
284 assert(IsDisabled && "Cannot enable an already-enabled macro!");
285 IsDisabled = false;
286 }
287
289 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
290 IsDisabled = true;
291 }
292
293
295
297
298 void dump() const;
299
300private:
302
303 unsigned getDefinitionLengthSlow(const SourceManager &SM) const;
304};
305
306
307
308
309
310
311
312
314public:
320
321protected:
322
324
326
327
328 LLVM_PREFERRED_TYPE(Kind)
330
331
332 LLVM_PREFERRED_TYPE(bool)
334
335
336
337
338
339 LLVM_PREFERRED_TYPE(bool)
341
344
345public:
347
349
350
352
353
355
356
358
359
361
363
367 bool IsPublic = true;
368
369 public:
373 : DefDirective(DefDirective), UndefLoc(UndefLoc), IsPublic(isPublic) {}
374
377
380
383 }
384
387
388 bool isPublic() const { return IsPublic; }
389
390 bool isValid() const { return DefDirective != nullptr; }
392
394
396
399 }
400 };
401
402
403
404
408 }
409
412 return !Def.isUndefined();
413 return false;
414 }
415
418 }
420
421
422
425
426 void dump() const;
427
429};
430
431
434
435public:
438 assert(MI && "MacroInfo is null");
439 }
442
443
446
449 }
450
452};
453
454
456public:
459 assert(UndefLoc.isValid() && "Invalid UndefLoc!");
460 }
461
464 }
465
467};
468
469
471public:
475 }
476
477
478
480
483 }
484
486};
487
490 return {};
492}
493
496 return nullptr;
497 return DefDirective->getInfo();
498}
499
502 if (isInvalid() || DefDirective->getPrevious() == nullptr)
503 return {};
504 return DefDirective->getPrevious()->getDefinition();
505}
506
507
508
509
510
511
512
513
516
517
519
520
522
523
524 Module *OwningModule;
525
526
527 unsigned NumOverriddenBy = 0;
528
529
530 unsigned NumOverrides;
531
534 : II(II), Macro(Macro), OwningModule(OwningModule),
535 NumOverrides(Overrides.size()) {
536 std::copy(Overrides.begin(), Overrides.end(),
537 reinterpret_cast<ModuleMacro **>(this + 1));
538 }
539
540public:
544
545 void Profile(llvm::FoldingSetNodeID &ID) const {
546 return Profile(ID, OwningModule, II);
547 }
548
549 static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule,
551 ID.AddPointer(OwningModule);
552 ID.AddPointer(II);
553 }
554
555
557
558
560
561
562
564
565
566
568
571 }
572
574 return overrides_begin() + NumOverrides;
575 }
576
578 return llvm::ArrayRef(overrides_begin(), overrides_end());
579 }
580
581
582
584};
585
586
587
588
589
591 llvm::PointerIntPair<DefMacroDirective *, 1, bool> LatestLocalAndAmbiguous;
593
594public:
597 bool IsAmbiguous)
598 : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {}
599
600
601 explicit operator bool() const {
602 return getLocalDirective() || !ModuleMacros.empty();
603 }
604
605
607 if (!ModuleMacros.empty())
608 return ModuleMacros.back()->getMacroInfo();
609 if (auto *MD = getLocalDirective())
610 return MD->getMacroInfo();
611 return nullptr;
612 }
613
614
615 bool isAmbiguous() const { return LatestLocalAndAmbiguous.getInt(); }
616
617
618
620 return LatestLocalAndAmbiguous.getPointer();
621 }
622
623
625
627 if (auto *MD = getLocalDirective())
628 F(MD->getMacroInfo());
629 for (auto *MM : getModuleMacros())
630 F(MM->getMacroInfo());
631 }
632};
633
634}
635
636#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
static bool isInvalid(LocType Loc, bool *Invalid)
A directive for a defined macro or a macro imported from a module.
static bool classof(const MacroDirective *MD)
static bool classof(const DefMacroDirective *)
const MacroInfo * getInfo() const
The data for the macro definition.
DefMacroDirective(MacroInfo *MI)
DefMacroDirective(MacroInfo *MI, SourceLocation Loc)
One of these records is kept for each identifier that is lexed.
A description of the current definition of a macro.
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
DefMacroDirective * getLocalDirective() const
Get the latest non-imported, non-#undef'd macro definition for this macro.
ArrayRef< ModuleMacro * > getModuleMacros() const
Get the active module macros for this macro.
bool isAmbiguous() const
true if the definition is ambiguous, false otherwise.
MacroDefinition(DefMacroDirective *MD, ArrayRef< ModuleMacro * > MMs, bool IsAmbiguous)
MacroDefinition()=default
void forAllDefinitions(Fn F) const
const DefInfo getPreviousDefinition() const
DefMacroDirective * getDirective()
DefInfo(DefMacroDirective *DefDirective, SourceLocation UndefLoc, bool isPublic)
DefInfo getPreviousDefinition()
MacroInfo * getMacroInfo()
const MacroInfo * getMacroInfo() const
SourceLocation getUndefLocation() const
const DefMacroDirective * getDirective() const
SourceLocation getLocation() const
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
MacroDirective * Previous
Previous macro directive for the same identifier, or nullptr.
const MacroDirective * getPrevious() const
Get previous definition of the macro with the same name.
const MacroInfo * getMacroInfo() const
const DefInfo findDirectiveAtLoc(SourceLocation L, const SourceManager &SM) const
Find macro definition active in the specified source location.
unsigned IsPublic
Whether the macro has public visibility (when described in a module).
void setPrevious(MacroDirective *Prev)
Set previous definition of the macro with the same name.
unsigned IsFromPCH
True if the macro directive was loaded from a PCH file.
SourceLocation getLocation() const
static bool classof(const MacroDirective *)
unsigned MDKind
MacroDirective kind.
MacroInfo * getMacroInfo()
const DefInfo getDefinition() const
bool isFromPCH() const
Return true if the macro directive was loaded from a PCH file.
MacroDirective * getPrevious()
Get previous definition of the macro with the same name.
DefInfo getDefinition()
Traverses the macro directives history and returns the next macro definition directive along with inf...
Encapsulates the data about a macro definition (e.g.
void setIsAllowRedefinitionsWithoutWarning(bool Val)
Set the value of the IsAllowRedefinitionsWithoutWarning flag.
bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP, bool Syntactically) const
Return true if the specified macro definition is equal to this macro in spelling, arguments,...
bool isUsed() const
Return false if this macro is defined in the main file and has not yet been used.
bool isC99Varargs() const
bool isFunctionLike() const
const_tokens_iterator tokens_begin() const
bool isAllowRedefinitionsWithoutWarning() const
Return true if this macro can be redefined without warning.
SourceLocation getDefinitionEndLoc() const
Return the location of the last token in the macro.
void setUsedForHeaderGuard(bool Val)
void setHasCommaPasting()
param_iterator param_begin() const
const_tokens_iterator tokens_end() const
ArrayRef< const IdentifierInfo * > params() const
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
unsigned getNumParams() const
const Token & getReplacementToken(unsigned Tok) const
void setDefinitionEndLoc(SourceLocation EndLoc)
Set the location of the last token in the macro.
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
void setTokens(ArrayRef< Token > Tokens, llvm::BumpPtrAllocator &PPAllocator)
IdentifierInfo *const * param_iterator
Parameters - The list of parameters for a function-like macro.
void setParameterList(ArrayRef< IdentifierInfo * > List, llvm::BumpPtrAllocator &PPAllocator)
Set the specified list of identifiers as the parameter list for this macro.
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
bool tokens_empty() const
unsigned getDefinitionLength(const SourceManager &SM) const
Get length in characters of the macro definition.
llvm::MutableArrayRef< Token > allocateTokens(unsigned NumTokens, llvm::BumpPtrAllocator &PPAllocator)
bool hasCommaPasting() const
void setIsFunctionLike()
Function/Object-likeness.
bool isObjectLike() const
param_iterator param_end() const
bool isUsedForHeaderGuard() const
Determine whether this macro was used for a header guard.
ArrayRef< Token > tokens() const
void setIsWarnIfUnused(bool val)
Set the value of the IsWarnIfUnused flag.
int getParameterNum(const IdentifierInfo *Arg) const
Return the parameter number of the specified identifier, or -1 if the identifier is not a formal para...
bool isWarnIfUnused() const
Return true if we should emit a warning if the macro is unused.
bool isGNUVarargs() const
void setIsC99Varargs()
Varargs querying methods. This can only be set for function-like macros.
bool isEnabled() const
Return true if this macro is enabled.
void setIsUsed(bool Val)
Set the value of the IsUsed flag.
void setIsBuiltinMacro(bool Val=true)
Set or clear the isBuiltinMacro flag.
Represents a macro directive exported by a module.
const IdentifierInfo * getName() const
Get the name of the macro.
MacroInfo * getMacroInfo() const
Get definition for this exported #define, or nullptr if this represents a #undef.
overrides_iterator overrides_begin() const
ArrayRef< ModuleMacro * > overrides() const
static void Profile(llvm::FoldingSetNodeID &ID, Module *OwningModule, const IdentifierInfo *II)
unsigned getNumOverridingMacros() const
Get the number of macros that override this one.
void Profile(llvm::FoldingSetNodeID &ID) const
Module * getOwningModule() const
Get the ID of the module that exports this macro.
ModuleMacro *const * overrides_iterator
Iterators over the overridden module IDs.
overrides_iterator overrides_end() const
Describes a module or submodule.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
A directive for an undefined macro.
static bool classof(const UndefMacroDirective *)
static bool classof(const MacroDirective *MD)
UndefMacroDirective(SourceLocation UndefLoc)
A directive for setting the module visibility of a macro.
VisibilityMacroDirective(SourceLocation Loc, bool Public)
static bool classof(const MacroDirective *MD)
static bool classof(const VisibilityMacroDirective *)
bool isPublic() const
Determine whether this macro is part of the public API of its module.
The JSON file list parser is used to communicate input to InstallAPI.
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
@ Other
Other implicit parameter.