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;
36 /// Arguments - The list of arguments for a function-like macro. This can be
37 /// empty, for, e.g. "#define X()". In a C99-style variadic macro, this
38 /// includes the __VA_ARGS__ identifier on the list.
39 IdentifierInfo **ArgumentList;
40 unsigned NumArguments;
42 /// \brief The location at which this macro was exported from its module.
44 /// If invalid, this macro has not been explicitly exported.
45 SourceLocation ExportLocation;
47 /// ReplacementTokens - This is the list of tokens that the macro is defined
49 SmallVector<Token, 8> ReplacementTokens;
51 /// \brief Length in characters of the macro definition.
52 mutable unsigned DefinitionLength;
53 mutable bool IsDefinitionLengthCached : 1;
55 /// IsFunctionLike - True if this macro is a function-like macro, false if it
56 /// is an object-like macro.
57 bool IsFunctionLike : 1;
59 /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
60 /// "#define X(Y,Z,...)". The __VA_ARGS__ token should be replaced with the
61 /// contents of "..." in an invocation.
62 bool IsC99Varargs : 1;
64 /// IsGNUVarargs - True if this macro is of the form "#define X(a...)". The
65 /// "a" identifier in the replacement list will be replaced with all arguments
66 /// of the macro starting with the specified one.
67 bool IsGNUVarargs : 1;
69 /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
70 /// it has not yet been redefined or undefined.
71 bool IsBuiltinMacro : 1;
73 /// IsFromAST - True if this macro was loaded from an AST file.
76 /// \brief Whether this macro changed after it was loaded from an AST file.
77 bool ChangedAfterLoad : 1;
80 //===--------------------------------------------------------------------===//
81 // State that changes as the macro is used.
83 /// IsDisabled - True if we have started an expansion of this macro already.
84 /// This disbles recursive expansion, which would be quite bad for things like
88 /// IsUsed - True if this macro is either defined in the main file and has
89 /// been used, or if it is not defined in the main file. This is used to
90 /// emit -Wunused-macros diagnostics.
93 /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
94 /// without emitting a warning.
95 bool IsAllowRedefinitionsWithoutWarning : 1;
97 /// \brief Must warn if the macro is unused at the end of translation unit.
98 bool IsWarnIfUnused : 1;
101 assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
105 MacroInfo(SourceLocation DefLoc);
106 MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
108 /// FreeArgumentList - Free the argument list of the macro, restoring it to a
109 /// state where it can be reused for other devious purposes.
110 void FreeArgumentList() {
115 /// Destroy - destroy this MacroInfo object.
121 /// getDefinitionLoc - Return the location that the macro was defined at.
123 SourceLocation getDefinitionLoc() const { return Location; }
125 /// setDefinitionEndLoc - Set the location of the last token in the macro.
127 void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
128 /// getDefinitionEndLoc - Return the location of the last token in the macro.
130 SourceLocation getDefinitionEndLoc() const { return EndLocation; }
132 /// \brief Get length in characters of the macro definition.
133 unsigned getDefinitionLength(SourceManager &SM) const {
134 if (IsDefinitionLengthCached)
135 return DefinitionLength;
136 return getDefinitionLengthSlow(SM);
139 /// isIdenticalTo - Return true if the specified macro definition is equal to
140 /// this macro in spelling, arguments, and whitespace. This is used to emit
141 /// duplicate definition warnings. This implements the rules in C99 6.10.3.
142 bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
144 /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
146 void setIsBuiltinMacro(bool Val = true) {
147 IsBuiltinMacro = Val;
150 /// setIsUsed - Set the value of the IsUsed flag.
152 void setIsUsed(bool Val) {
156 /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
157 /// IsAllowRedefinitionsWithoutWarning flag.
158 void setIsAllowRedefinitionsWithoutWarning(bool Val) {
159 IsAllowRedefinitionsWithoutWarning = Val;
162 /// \brief Set the value of the IsWarnIfUnused flag.
163 void setIsWarnIfUnused(bool val) {
164 IsWarnIfUnused = val;
167 /// setArgumentList - Set the specified list of identifiers as the argument
168 /// list for this macro.
169 void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
170 llvm::BumpPtrAllocator &PPAllocator) {
171 assert(ArgumentList == 0 && NumArguments == 0 &&
172 "Argument list already set!");
173 if (NumArgs == 0) return;
175 NumArguments = NumArgs;
176 ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
177 for (unsigned i = 0; i != NumArgs; ++i)
178 ArgumentList[i] = List[i];
181 /// Arguments - The list of arguments for a function-like macro. This can be
182 /// empty, for, e.g. "#define X()".
183 typedef IdentifierInfo* const *arg_iterator;
184 bool arg_empty() const { return NumArguments == 0; }
185 arg_iterator arg_begin() const { return ArgumentList; }
186 arg_iterator arg_end() const { return ArgumentList+NumArguments; }
187 unsigned getNumArgs() const { return NumArguments; }
189 /// getArgumentNum - Return the argument number of the specified identifier,
190 /// or -1 if the identifier is not a formal argument identifier.
191 int getArgumentNum(IdentifierInfo *Arg) const {
192 for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
193 if (*I == Arg) return I-arg_begin();
197 /// Function/Object-likeness. Keep track of whether this macro has formal
199 void setIsFunctionLike() { IsFunctionLike = true; }
200 bool isFunctionLike() const { return IsFunctionLike; }
201 bool isObjectLike() const { return !IsFunctionLike; }
203 /// Varargs querying methods. This can only be set for function-like macros.
204 void setIsC99Varargs() { IsC99Varargs = true; }
205 void setIsGNUVarargs() { IsGNUVarargs = true; }
206 bool isC99Varargs() const { return IsC99Varargs; }
207 bool isGNUVarargs() const { return IsGNUVarargs; }
208 bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
210 /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
211 /// __LINE__, which requires processing before expansion.
212 bool isBuiltinMacro() const { return IsBuiltinMacro; }
214 /// isFromAST - Return true if this macro was loaded from an AST file.
215 bool isFromAST() const { return IsFromAST; }
217 /// setIsFromAST - Set whether this macro was loaded from an AST file.
218 void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
220 /// \brief Determine whether this macro has changed since it was loaded from
222 bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
224 /// \brief Note whether this macro has changed after it was loaded from an
226 void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
228 /// isUsed - Return false if this macro is defined in the main file and has
229 /// not yet been used.
230 bool isUsed() const { return IsUsed; }
232 /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
233 /// redefined without warning.
234 bool isAllowRedefinitionsWithoutWarning() const {
235 return IsAllowRedefinitionsWithoutWarning;
238 /// \brief Return true if we should emit a warning if the macro is unused.
239 bool isWarnIfUnused() const {
240 return IsWarnIfUnused;
243 /// getNumTokens - Return the number of tokens that this macro expands to.
245 unsigned getNumTokens() const {
246 return ReplacementTokens.size();
249 const Token &getReplacementToken(unsigned Tok) const {
250 assert(Tok < ReplacementTokens.size() && "Invalid token #");
251 return ReplacementTokens[Tok];
254 typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
255 tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
256 tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
257 bool tokens_empty() const { return ReplacementTokens.empty(); }
259 /// AddTokenToBody - Add the specified token to the replacement text for the
261 void AddTokenToBody(const Token &Tok) {
262 assert(!IsDefinitionLengthCached &&
263 "Changing replacement tokens after definition length got calculated");
264 ReplacementTokens.push_back(Tok);
267 /// isEnabled - Return true if this macro is enabled: in other words, that we
268 /// are not currently in an expansion of this macro.
269 bool isEnabled() const { return !IsDisabled; }
272 assert(IsDisabled && "Cannot enable an already-enabled macro!");
276 void DisableMacro() {
277 assert(!IsDisabled && "Cannot disable an already-disabled macro!");
281 /// \brief Set the export location for this macro.
282 void setExportLocation(SourceLocation ExportLoc) {
283 ExportLocation = ExportLoc;
286 /// \brief Determine whether this macro was explicitly exported from its
288 bool isExported() const { return ExportLocation.isValid(); }
290 /// \brief Determine the location where this macro was explicitly exported
292 SourceLocation getExportLocation() { return ExportLocation; }
295 unsigned getDefinitionLengthSlow(SourceManager &SM) const;
298 } // end namespace clang