1 //===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MacroInfo interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_MACROINFO_H
15 #define LLVM_CLANG_MACROINFO_H
17 #include "clang/Lex/Token.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/Allocator.h"
25 /// MacroInfo - Each identifier that is \#define'd has an instance of this class
26 /// associated with it, used to implement macro expansion.
28 //===--------------------------------------------------------------------===//
29 // State set when the macro is defined.
31 /// Location - This is the place the macro is defined.
32 SourceLocation Location;
33 /// EndLocation - The location of the last token in the macro.
34 SourceLocation EndLocation;
35 /// \brief The location where the macro was #undef'd, or an invalid location
36 /// for macros that haven't been undefined.
37 SourceLocation UndefLocation;
38 /// \brief Previous definition, the identifier of this macro was defined to,
40 MacroInfo *PreviousDefinition;
42 /// Arguments - The list of arguments for a function-like macro. This can be
43 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this
44 /// includes the \c __VA_ARGS__ identifier on the list.
45 IdentifierInfo **ArgumentList;
46 unsigned NumArguments;
48 /// \brief The location at which this macro was either explicitly exported
49 /// from its module or marked as private.
51 /// If invalid, this macro has not been explicitly given any visibility.
52 SourceLocation VisibilityLocation;
54 /// \brief This is the list of tokens that the macro is defined to.
55 SmallVector<Token, 8> ReplacementTokens;
57 /// \brief Length in characters of the macro definition.
58 mutable unsigned DefinitionLength;
59 mutable bool IsDefinitionLengthCached : 1;
61 /// \brief True if this macro is a function-like macro, false if it
62 /// is an object-like macro.
63 bool IsFunctionLike : 1;
65 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
66 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the
67 /// contents of "..." in an invocation.
68 bool IsC99Varargs : 1;
70 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The
71 /// "a" identifier in the replacement list will be replaced with all arguments
72 /// of the macro starting with the specified one.
73 bool IsGNUVarargs : 1;
75 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
76 /// it has not yet been redefined or undefined.
77 bool IsBuiltinMacro : 1;
79 /// \brief True if this macro was loaded from an AST file.
82 /// \brief Whether this macro changed after it was loaded from an AST file.
83 bool ChangedAfterLoad : 1;
86 //===--------------------------------------------------------------------===//
87 // State that changes as the macro is used.
89 /// IsDisabled - True if we have started an expansion of this macro already.
90 /// This disables recursive expansion, which would be quite bad for things
91 /// like \#define A A.
94 /// IsUsed - True if this macro is either defined in the main file and has
95 /// been used, or if it is not defined in the main file. This is used to
96 /// emit -Wunused-macros diagnostics.
99 /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
100 /// without emitting a warning.
101 bool IsAllowRedefinitionsWithoutWarning : 1;
103 /// \brief Must warn if the macro is unused at the end of translation unit.
104 bool IsWarnIfUnused : 1;
106 /// \brief Whether the macro has public (when described in a module).
109 /// \brief Whether the macro definition is currently "hidden".
110 /// Note that this is transient state that is never serialized to the AST
114 /// \brief Whether the definition of this macro is ambiguous, due to
115 /// multiple definitions coming in from multiple modules.
116 bool IsAmbiguous : 1;
119 assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
123 MacroInfo(SourceLocation DefLoc);
124 MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
126 /// FreeArgumentList - Free the argument list of the macro, restoring it to a
127 /// state where it can be reused for other devious purposes.
128 void FreeArgumentList() {
133 /// Destroy - destroy this MacroInfo object.
139 /// getDefinitionLoc - Return the location that the macro was defined at.
141 SourceLocation getDefinitionLoc() const { return Location; }
143 /// setDefinitionEndLoc - Set the location of the last token in the macro.
145 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
147 /// getDefinitionEndLoc - Return the location of the last token in the macro.
149 SourceLocation getDefinitionEndLoc() const { return EndLocation; }
151 /// \brief Set the location where macro was undefined. Can only be set once.
152 void setUndefLoc(SourceLocation UndefLoc) {
153 assert(UndefLocation.isInvalid() && "UndefLocation is already set!");
154 assert(UndefLoc.isValid() && "Invalid UndefLoc!");
155 UndefLocation = UndefLoc;
158 /// \brief Get the location where macro was undefined.
159 SourceLocation getUndefLoc() const { return UndefLocation; }
161 /// \brief Set previous definition of the macro with the same name.
162 void setPreviousDefinition(MacroInfo *PreviousDef) {
163 PreviousDefinition = PreviousDef;
166 /// \brief Get previous definition of the macro with the same name.
167 MacroInfo *getPreviousDefinition() { return PreviousDefinition; }
169 /// \brief Find macro definition active in the specified source location. If
170 /// this macro was not defined there, return NULL.
171 const MacroInfo *findDefinitionAtLoc(SourceLocation L,
172 SourceManager &SM) const;
174 /// \brief Get length in characters of the macro definition.
175 unsigned getDefinitionLength(SourceManager &SM) const {
176 if (IsDefinitionLengthCached)
177 return DefinitionLength;
178 return getDefinitionLengthSlow(SM);
181 /// isIdenticalTo - Return true if the specified macro definition is equal to
182 /// this macro in spelling, arguments, and whitespace. This is used to emit
183 /// duplicate definition warnings. This implements the rules in C99 6.10.3.
184 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
186 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
188 void setIsBuiltinMacro(bool Val = true) {
189 IsBuiltinMacro = Val;
192 /// setIsUsed - Set the value of the IsUsed flag.
194 void setIsUsed(bool Val) {
198 /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
199 /// IsAllowRedefinitionsWithoutWarning flag.
200 void setIsAllowRedefinitionsWithoutWarning(bool Val) {
201 IsAllowRedefinitionsWithoutWarning = Val;
204 /// \brief Set the value of the IsWarnIfUnused flag.
205 void setIsWarnIfUnused(bool val) {
206 IsWarnIfUnused = val;
209 /// setArgumentList - Set the specified list of identifiers as the argument
210 /// list for this macro.
211 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
212 llvm::BumpPtrAllocator &PPAllocator) {
213 assert(ArgumentList == 0 && NumArguments == 0 &&
214 "Argument list already set!");
215 if (NumArgs == 0) return;
217 NumArguments = NumArgs;
218 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
219 for (unsigned i = 0; i != NumArgs; ++i)
220 ArgumentList[i] = List[i];
223 /// Arguments - The list of arguments for a function-like macro. This can be
224 /// empty, for, e.g. "#define X()".
225 typedef IdentifierInfo* const *arg_iterator;
226 bool arg_empty() const { return NumArguments == 0; }
227 arg_iterator arg_begin() const { return ArgumentList; }
228 arg_iterator arg_end() const { return ArgumentList+NumArguments; }
229 unsigned getNumArgs() const { return NumArguments; }
231 /// getArgumentNum - Return the argument number of the specified identifier,
232 /// or -1 if the identifier is not a formal argument identifier.
233 int getArgumentNum(IdentifierInfo *Arg) const {
234 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
235 if (*I == Arg) return I-arg_begin();
239 /// Function/Object-likeness. Keep track of whether this macro has formal
241 void setIsFunctionLike() { IsFunctionLike = true; }
242 bool isFunctionLike() const { return IsFunctionLike; }
243 bool isObjectLike() const { return !IsFunctionLike; }
245 /// Varargs querying methods. This can only be set for function-like macros.
246 void setIsC99Varargs() { IsC99Varargs = true; }
247 void setIsGNUVarargs() { IsGNUVarargs = true; }
248 bool isC99Varargs() const { return IsC99Varargs; }
249 bool isGNUVarargs() const { return IsGNUVarargs; }
250 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
252 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
253 /// __LINE__, which requires processing before expansion.
254 bool isBuiltinMacro() const { return IsBuiltinMacro; }
256 /// isFromAST - Return true if this macro was loaded from an AST file.
257 bool isFromAST() const { return IsFromAST; }
259 /// setIsFromAST - Set whether this macro was loaded from an AST file.
260 void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
262 /// \brief Determine whether this macro has changed since it was loaded from
264 bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
266 /// \brief Note whether this macro has changed after it was loaded from an
268 void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
270 /// isUsed - Return false if this macro is defined in the main file and has
271 /// not yet been used.
272 bool isUsed() const { return IsUsed; }
274 /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
275 /// redefined without warning.
276 bool isAllowRedefinitionsWithoutWarning() const {
277 return IsAllowRedefinitionsWithoutWarning;
280 /// \brief Return true if we should emit a warning if the macro is unused.
281 bool isWarnIfUnused() const {
282 return IsWarnIfUnused;
285 /// getNumTokens - Return the number of tokens that this macro expands to.
287 unsigned getNumTokens() const {
288 return ReplacementTokens.size();
291 const Token &getReplacementToken(unsigned Tok) const {
292 assert(Tok < ReplacementTokens.size() && "Invalid token #");
293 return ReplacementTokens[Tok];
296 typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
297 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
298 tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
299 bool tokens_empty() const { return ReplacementTokens.empty(); }
301 /// AddTokenToBody - Add the specified token to the replacement text for the
303 void AddTokenToBody(const Token &Tok) {
304 assert(!IsDefinitionLengthCached &&
305 "Changing replacement tokens after definition length got calculated");
306 ReplacementTokens.push_back(Tok);
309 /// isEnabled - Return true if this macro is enabled: in other words, that we
310 /// are not currently in an expansion of this macro.
311 bool isEnabled() const { return !IsDisabled; }
314 assert(IsDisabled && "Cannot enable an already-enabled macro!");
318 void DisableMacro() {
319 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
323 /// \brief Set the export location for this macro.
324 void setVisibility(bool Public, SourceLocation Loc) {
325 VisibilityLocation = Loc;
329 /// \brief Determine whether this macro is part of the public API of its
331 bool isPublic() const { return IsPublic; }
333 /// \brief Determine the location where this macro was explicitly made
334 /// public or private within its module.
335 SourceLocation getVisibilityLocation() { return VisibilityLocation; }
337 /// \brief Determine whether this macro is currently defined (and has not
338 /// been #undef'd) or has been hidden.
339 bool isDefined() const { return UndefLocation.isInvalid() && !IsHidden; }
341 /// \brief Determine whether this macro definition is hidden.
342 bool isHidden() const { return IsHidden; }
344 /// \brief Set whether this macro definition is hidden.
345 void setHidden(bool Val) { IsHidden = Val; }
347 /// \brief Determine whether this macro definition is ambiguous with
348 /// other macro definitions.
349 bool isAmbiguous() const { return IsAmbiguous; }
351 /// \brief Set whether this macro definition is ambiguous.
352 void setAmbiguous(bool Val) { IsAmbiguous = Val; }
355 unsigned getDefinitionLengthSlow(SourceManager &SM) const;
358 } // end namespace clang