MacroInfo.h revision f953276b6165dc7e8f4679cce4d0e7c649cd4232
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 Get the first definition in the chain.
173  MacroInfo *getFirstDefinition() {
174    MacroInfo *MI = this;
175    while (MI->PreviousDefinition)
176      MI = MI->PreviousDefinition;
177    return MI;
178  }
179
180  /// \brief Find macro definition active in the specified source location. If
181  /// this macro was not defined there, return NULL.
182  const MacroInfo *findDefinitionAtLoc(SourceLocation L,
183                                       SourceManager &SM) const;
184
185  /// \brief Get length in characters of the macro definition.
186  unsigned getDefinitionLength(SourceManager &SM) const {
187    if (IsDefinitionLengthCached)
188      return DefinitionLength;
189    return getDefinitionLengthSlow(SM);
190  }
191
192  /// isIdenticalTo - Return true if the specified macro definition is equal to
193  /// this macro in spelling, arguments, and whitespace.  This is used to emit
194  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
195  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
196
197  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
198  ///
199  void setIsBuiltinMacro(bool Val = true) {
200    IsBuiltinMacro = Val;
201  }
202
203  /// setIsUsed - Set the value of the IsUsed flag.
204  ///
205  void setIsUsed(bool Val) {
206    IsUsed = Val;
207  }
208
209  /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
210  /// IsAllowRedefinitionsWithoutWarning flag.
211  void setIsAllowRedefinitionsWithoutWarning(bool Val) {
212    IsAllowRedefinitionsWithoutWarning = Val;
213  }
214
215  /// \brief Set the value of the IsWarnIfUnused flag.
216  void setIsWarnIfUnused(bool val) {
217    IsWarnIfUnused = val;
218  }
219
220  /// setArgumentList - Set the specified list of identifiers as the argument
221  /// list for this macro.
222  void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
223                       llvm::BumpPtrAllocator &PPAllocator) {
224    assert(ArgumentList == 0 && NumArguments == 0 &&
225           "Argument list already set!");
226    if (NumArgs == 0) return;
227
228    NumArguments = NumArgs;
229    ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
230    for (unsigned i = 0; i != NumArgs; ++i)
231      ArgumentList[i] = List[i];
232  }
233
234  /// Arguments - The list of arguments for a function-like macro.  This can be
235  /// empty, for, e.g. "#define X()".
236  typedef IdentifierInfo* const *arg_iterator;
237  bool arg_empty() const { return NumArguments == 0; }
238  arg_iterator arg_begin() const { return ArgumentList; }
239  arg_iterator arg_end() const { return ArgumentList+NumArguments; }
240  unsigned getNumArgs() const { return NumArguments; }
241
242  /// getArgumentNum - Return the argument number of the specified identifier,
243  /// or -1 if the identifier is not a formal argument identifier.
244  int getArgumentNum(IdentifierInfo *Arg) const {
245    for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
246      if (*I == Arg) return I-arg_begin();
247    return -1;
248  }
249
250  /// Function/Object-likeness.  Keep track of whether this macro has formal
251  /// parameters.
252  void setIsFunctionLike() { IsFunctionLike = true; }
253  bool isFunctionLike() const { return IsFunctionLike; }
254  bool isObjectLike() const { return !IsFunctionLike; }
255
256  /// Varargs querying methods.  This can only be set for function-like macros.
257  void setIsC99Varargs() { IsC99Varargs = true; }
258  void setIsGNUVarargs() { IsGNUVarargs = true; }
259  bool isC99Varargs() const { return IsC99Varargs; }
260  bool isGNUVarargs() const { return IsGNUVarargs; }
261  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
262
263  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
264  /// __LINE__, which requires processing before expansion.
265  bool isBuiltinMacro() const { return IsBuiltinMacro; }
266
267  bool hasCommaPasting() const { return HasCommaPasting; }
268  void setHasCommaPasting() { HasCommaPasting = true; }
269
270  /// isFromAST - Return true if this macro was loaded from an AST file.
271  bool isFromAST() const { return IsFromAST; }
272
273  /// setIsFromAST - Set whether this macro was loaded from an AST file.
274  void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
275
276  /// \brief Determine whether this macro has changed since it was loaded from
277  /// an AST file.
278  bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
279
280  /// \brief Note whether this macro has changed after it was loaded from an
281  /// AST file.
282  void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
283
284  /// isUsed - Return false if this macro is defined in the main file and has
285  /// not yet been used.
286  bool isUsed() const { return IsUsed; }
287
288  /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
289  /// redefined without warning.
290  bool isAllowRedefinitionsWithoutWarning() const {
291    return IsAllowRedefinitionsWithoutWarning;
292  }
293
294  /// \brief Return true if we should emit a warning if the macro is unused.
295  bool isWarnIfUnused() const {
296    return IsWarnIfUnused;
297  }
298
299  /// getNumTokens - Return the number of tokens that this macro expands to.
300  ///
301  unsigned getNumTokens() const {
302    return ReplacementTokens.size();
303  }
304
305  const Token &getReplacementToken(unsigned Tok) const {
306    assert(Tok < ReplacementTokens.size() && "Invalid token #");
307    return ReplacementTokens[Tok];
308  }
309
310  typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
311  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
312  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
313  bool tokens_empty() const { return ReplacementTokens.empty(); }
314
315  /// AddTokenToBody - Add the specified token to the replacement text for the
316  /// macro.
317  void AddTokenToBody(const Token &Tok) {
318    assert(!IsDefinitionLengthCached &&
319          "Changing replacement tokens after definition length got calculated");
320    ReplacementTokens.push_back(Tok);
321  }
322
323  /// isEnabled - Return true if this macro is enabled: in other words, that we
324  /// are not currently in an expansion of this macro.
325  bool isEnabled() const { return !IsDisabled; }
326
327  void EnableMacro() {
328    assert(IsDisabled && "Cannot enable an already-enabled macro!");
329    IsDisabled = false;
330  }
331
332  void DisableMacro() {
333    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
334    IsDisabled = true;
335  }
336
337  /// \brief Set the export location for this macro.
338  void setVisibility(bool Public, SourceLocation Loc) {
339    VisibilityLocation = Loc;
340    IsPublic = Public;
341  }
342
343  /// \brief Determine whether this macro is part of the public API of its
344  /// module.
345  bool isPublic() const { return IsPublic; }
346
347  /// \brief Determine the location where this macro was explicitly made
348  /// public or private within its module.
349  SourceLocation getVisibilityLocation() { return VisibilityLocation; }
350
351  /// \brief Determine whether this macro is currently defined (and has not
352  /// been #undef'd) or has been hidden.
353  bool isDefined() const { return UndefLocation.isInvalid() && !IsHidden; }
354
355  /// \brief Determine whether this macro definition is hidden.
356  bool isHidden() const { return IsHidden; }
357
358  /// \brief Set whether this macro definition is hidden.
359  void setHidden(bool Val) { IsHidden = Val; }
360
361  /// \brief Determine whether this macro definition is ambiguous with
362  /// other macro definitions.
363  bool isAmbiguous() const { return IsAmbiguous; }
364
365  /// \brief Set whether this macro definition is ambiguous.
366  void setAmbiguous(bool Val) { IsAmbiguous = Val; }
367
368private:
369  unsigned getDefinitionLengthSlow(SourceManager &SM) const;
370};
371
372}  // end namespace clang
373
374#endif
375