MacroInfo.h revision 4fa4b480270c14dfdcd0dfd4f76938e973082e3b
1//===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MacroInfo interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_MACROINFO_H
15#define LLVM_CLANG_MACROINFO_H
16
17#include "clang/Lex/Token.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Support/Allocator.h"
20#include <cassert>
21
22namespace clang {
23  class Preprocessor;
24
25/// MacroInfo - Each identifier that is \#define'd has an instance of this class
26/// associated with it, used to implement macro expansion.
27class MacroInfo {
28  //===--------------------------------------------------------------------===//
29  // State set when the macro is defined.
30
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,
39  /// or NULL.
40  MacroInfo *PreviousDefinition;
41
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;
47
48  /// \brief The location at which this macro was either explicitly exported
49  /// from its module or marked as private.
50  ///
51  /// If invalid, this macro has not been explicitly given any visibility.
52  SourceLocation VisibilityLocation;
53
54  /// \brief This is the list of tokens that the macro is defined to.
55  SmallVector<Token, 8> ReplacementTokens;
56
57  /// \brief Length in characters of the macro definition.
58  mutable unsigned DefinitionLength;
59  mutable bool IsDefinitionLengthCached : 1;
60
61  /// \brief True if this macro is a function-like macro, false if it
62  /// is an object-like macro.
63  bool IsFunctionLike : 1;
64
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;
69
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;
74
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;
78
79  /// \brief Whether this macro contains the sequence ", ## __VA_ARGS__"
80  bool HasCommaPasting : 1;
81
82  /// \brief True if this macro was loaded from an AST file.
83  bool IsFromAST : 1;
84
85  /// \brief Whether this macro changed after it was loaded from an AST file.
86  bool ChangedAfterLoad : 1;
87
88private:
89  //===--------------------------------------------------------------------===//
90  // State that changes as the macro is used.
91
92  /// IsDisabled - True if we have started an expansion of this macro already.
93  /// This disables recursive expansion, which would be quite bad for things
94  /// like \#define A A.
95  bool IsDisabled : 1;
96
97  /// IsUsed - True if this macro is either defined in the main file and has
98  /// been used, or if it is not defined in the main file.  This is used to
99  /// emit -Wunused-macros diagnostics.
100  bool IsUsed : 1;
101
102  /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
103  /// without emitting a warning.
104  bool IsAllowRedefinitionsWithoutWarning : 1;
105
106  /// \brief Must warn if the macro is unused at the end of translation unit.
107  bool IsWarnIfUnused : 1;
108
109  /// \brief Whether the macro has public (when described in a module).
110  bool IsPublic : 1;
111
112  /// \brief Whether the macro definition is currently "hidden".
113  /// Note that this is transient state that is never serialized to the AST
114  /// file.
115  bool IsHidden : 1;
116
117  /// \brief Whether the definition of this macro is ambiguous, due to
118  /// multiple definitions coming in from multiple modules.
119  bool IsAmbiguous : 1;
120
121   ~MacroInfo() {
122    assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
123  }
124
125public:
126  MacroInfo(SourceLocation DefLoc);
127  MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
128
129  /// FreeArgumentList - Free the argument list of the macro, restoring it to a
130  /// state where it can be reused for other devious purposes.
131  void FreeArgumentList() {
132    ArgumentList = 0;
133    NumArguments = 0;
134  }
135
136  /// Destroy - destroy this MacroInfo object.
137  void Destroy() {
138    FreeArgumentList();
139    this->~MacroInfo();
140  }
141
142  /// getDefinitionLoc - Return the location that the macro was defined at.
143  ///
144  SourceLocation getDefinitionLoc() const { return Location; }
145
146  /// setDefinitionEndLoc - Set the location of the last token in the macro.
147  ///
148  void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
149
150  /// getDefinitionEndLoc - Return the location of the last token in the macro.
151  ///
152  SourceLocation getDefinitionEndLoc() const { return EndLocation; }
153
154  /// \brief Set the location where macro was undefined. Can only be set once.
155  void setUndefLoc(SourceLocation UndefLoc) {
156    assert(UndefLocation.isInvalid() && "UndefLocation is already set!");
157    assert(UndefLoc.isValid() && "Invalid UndefLoc!");
158    UndefLocation = UndefLoc;
159  }
160
161  /// \brief Get the location where macro was undefined.
162  SourceLocation getUndefLoc() const { return UndefLocation; }
163
164  /// \brief Set previous definition of the macro with the same name.
165  void setPreviousDefinition(MacroInfo *PreviousDef) {
166    PreviousDefinition = PreviousDef;
167  }
168
169  /// \brief Get previous definition of the macro with the same name.
170  MacroInfo *getPreviousDefinition() { return PreviousDefinition; }
171
172  /// \brief Find macro definition active in the specified source location. If
173  /// this macro was not defined there, return NULL.
174  const MacroInfo *findDefinitionAtLoc(SourceLocation L,
175                                       SourceManager &SM) const;
176
177  /// \brief Get length in characters of the macro definition.
178  unsigned getDefinitionLength(SourceManager &SM) const {
179    if (IsDefinitionLengthCached)
180      return DefinitionLength;
181    return getDefinitionLengthSlow(SM);
182  }
183
184  /// isIdenticalTo - Return true if the specified macro definition is equal to
185  /// this macro in spelling, arguments, and whitespace.  This is used to emit
186  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
187  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
188
189  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
190  ///
191  void setIsBuiltinMacro(bool Val = true) {
192    IsBuiltinMacro = Val;
193  }
194
195  /// setIsUsed - Set the value of the IsUsed flag.
196  ///
197  void setIsUsed(bool Val) {
198    IsUsed = Val;
199  }
200
201  /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
202  /// IsAllowRedefinitionsWithoutWarning flag.
203  void setIsAllowRedefinitionsWithoutWarning(bool Val) {
204    IsAllowRedefinitionsWithoutWarning = Val;
205  }
206
207  /// \brief Set the value of the IsWarnIfUnused flag.
208  void setIsWarnIfUnused(bool val) {
209    IsWarnIfUnused = val;
210  }
211
212  /// setArgumentList - Set the specified list of identifiers as the argument
213  /// list for this macro.
214  void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
215                       llvm::BumpPtrAllocator &PPAllocator) {
216    assert(ArgumentList == 0 && NumArguments == 0 &&
217           "Argument list already set!");
218    if (NumArgs == 0) return;
219
220    NumArguments = NumArgs;
221    ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
222    for (unsigned i = 0; i != NumArgs; ++i)
223      ArgumentList[i] = List[i];
224  }
225
226  /// Arguments - The list of arguments for a function-like macro.  This can be
227  /// empty, for, e.g. "#define X()".
228  typedef IdentifierInfo* const *arg_iterator;
229  bool arg_empty() const { return NumArguments == 0; }
230  arg_iterator arg_begin() const { return ArgumentList; }
231  arg_iterator arg_end() const { return ArgumentList+NumArguments; }
232  unsigned getNumArgs() const { return NumArguments; }
233
234  /// getArgumentNum - Return the argument number of the specified identifier,
235  /// or -1 if the identifier is not a formal argument identifier.
236  int getArgumentNum(IdentifierInfo *Arg) const {
237    for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
238      if (*I == Arg) return I-arg_begin();
239    return -1;
240  }
241
242  /// Function/Object-likeness.  Keep track of whether this macro has formal
243  /// parameters.
244  void setIsFunctionLike() { IsFunctionLike = true; }
245  bool isFunctionLike() const { return IsFunctionLike; }
246  bool isObjectLike() const { return !IsFunctionLike; }
247
248  /// Varargs querying methods.  This can only be set for function-like macros.
249  void setIsC99Varargs() { IsC99Varargs = true; }
250  void setIsGNUVarargs() { IsGNUVarargs = true; }
251  bool isC99Varargs() const { return IsC99Varargs; }
252  bool isGNUVarargs() const { return IsGNUVarargs; }
253  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
254
255  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
256  /// __LINE__, which requires processing before expansion.
257  bool isBuiltinMacro() const { return IsBuiltinMacro; }
258
259  bool hasCommaPasting() const { return HasCommaPasting; }
260  void setHasCommaPasting() { HasCommaPasting = true; }
261
262  /// isFromAST - Return true if this macro was loaded from an AST file.
263  bool isFromAST() const { return IsFromAST; }
264
265  /// setIsFromAST - Set whether this macro was loaded from an AST file.
266  void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
267
268  /// \brief Determine whether this macro has changed since it was loaded from
269  /// an AST file.
270  bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
271
272  /// \brief Note whether this macro has changed after it was loaded from an
273  /// AST file.
274  void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
275
276  /// isUsed - Return false if this macro is defined in the main file and has
277  /// not yet been used.
278  bool isUsed() const { return IsUsed; }
279
280  /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
281  /// redefined without warning.
282  bool isAllowRedefinitionsWithoutWarning() const {
283    return IsAllowRedefinitionsWithoutWarning;
284  }
285
286  /// \brief Return true if we should emit a warning if the macro is unused.
287  bool isWarnIfUnused() const {
288    return IsWarnIfUnused;
289  }
290
291  /// getNumTokens - Return the number of tokens that this macro expands to.
292  ///
293  unsigned getNumTokens() const {
294    return ReplacementTokens.size();
295  }
296
297  const Token &getReplacementToken(unsigned Tok) const {
298    assert(Tok < ReplacementTokens.size() && "Invalid token #");
299    return ReplacementTokens[Tok];
300  }
301
302  typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
303  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
304  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
305  bool tokens_empty() const { return ReplacementTokens.empty(); }
306
307  /// AddTokenToBody - Add the specified token to the replacement text for the
308  /// macro.
309  void AddTokenToBody(const Token &Tok) {
310    assert(!IsDefinitionLengthCached &&
311          "Changing replacement tokens after definition length got calculated");
312    ReplacementTokens.push_back(Tok);
313  }
314
315  /// isEnabled - Return true if this macro is enabled: in other words, that we
316  /// are not currently in an expansion of this macro.
317  bool isEnabled() const { return !IsDisabled; }
318
319  void EnableMacro() {
320    assert(IsDisabled && "Cannot enable an already-enabled macro!");
321    IsDisabled = false;
322  }
323
324  void DisableMacro() {
325    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
326    IsDisabled = true;
327  }
328
329  /// \brief Set the export location for this macro.
330  void setVisibility(bool Public, SourceLocation Loc) {
331    VisibilityLocation = Loc;
332    IsPublic = Public;
333  }
334
335  /// \brief Determine whether this macro is part of the public API of its
336  /// module.
337  bool isPublic() const { return IsPublic; }
338
339  /// \brief Determine the location where this macro was explicitly made
340  /// public or private within its module.
341  SourceLocation getVisibilityLocation() { return VisibilityLocation; }
342
343  /// \brief Determine whether this macro is currently defined (and has not
344  /// been #undef'd) or has been hidden.
345  bool isDefined() const { return UndefLocation.isInvalid() && !IsHidden; }
346
347  /// \brief Determine whether this macro definition is hidden.
348  bool isHidden() const { return IsHidden; }
349
350  /// \brief Set whether this macro definition is hidden.
351  void setHidden(bool Val) { IsHidden = Val; }
352
353  /// \brief Determine whether this macro definition is ambiguous with
354  /// other macro definitions.
355  bool isAmbiguous() const { return IsAmbiguous; }
356
357  /// \brief Set whether this macro definition is ambiguous.
358  void setAmbiguous(bool Val) { IsAmbiguous = Val; }
359
360private:
361  unsigned getDefinitionLengthSlow(SourceManager &SM) const;
362};
363
364}  // end namespace clang
365
366#endif
367