MacroInfo.h revision 63333619fa68c8d1d8219f6d7f2d3c36f4356346
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
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 \c __VA_ARGS__ identifier on the list.
39  IdentifierInfo **ArgumentList;
40  unsigned NumArguments;
41
42  /// \brief The location at which this macro was either explicitly exported
43  /// from its module or marked as private.
44  ///
45  /// If invalid, this macro has not been explicitly given any visibility.
46  SourceLocation VisibilityLocation;
47
48  /// \brief This is the list of tokens that the macro is defined to.
49  SmallVector<Token, 8> ReplacementTokens;
50
51  /// \brief Length in characters of the macro definition.
52  mutable unsigned DefinitionLength;
53  mutable bool IsDefinitionLengthCached : 1;
54
55  /// \brief True if this macro is a function-like macro, false if it
56  /// is an object-like macro.
57  bool IsFunctionLike : 1;
58
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;
63
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;
68
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;
72
73  /// \brief True if this macro was loaded from an AST file.
74  bool IsFromAST : 1;
75
76  /// \brief Whether this macro changed after it was loaded from an AST file.
77  bool ChangedAfterLoad : 1;
78
79private:
80  //===--------------------------------------------------------------------===//
81  // State that changes as the macro is used.
82
83  /// IsDisabled - True if we have started an expansion of this macro already.
84  /// This disables recursive expansion, which would be quite bad for things
85  /// like \#define A A.
86  bool IsDisabled : 1;
87
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.
91  bool IsUsed : 1;
92
93  /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
94  /// without emitting a warning.
95  bool IsAllowRedefinitionsWithoutWarning : 1;
96
97  /// \brief Must warn if the macro is unused at the end of translation unit.
98  bool IsWarnIfUnused : 1;
99
100  /// \brief Whether the macro has public (when described in a module).
101  bool IsPublic : 1;
102
103   ~MacroInfo() {
104    assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
105  }
106
107public:
108  MacroInfo(SourceLocation DefLoc);
109  MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
110
111  /// FreeArgumentList - Free the argument list of the macro, restoring it to a
112  /// state where it can be reused for other devious purposes.
113  void FreeArgumentList() {
114    ArgumentList = 0;
115    NumArguments = 0;
116  }
117
118  /// Destroy - destroy this MacroInfo object.
119  void Destroy() {
120    FreeArgumentList();
121    this->~MacroInfo();
122  }
123
124  /// getDefinitionLoc - Return the location that the macro was defined at.
125  ///
126  SourceLocation getDefinitionLoc() const { return Location; }
127
128  /// setDefinitionEndLoc - Set the location of the last token in the macro.
129  ///
130  void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
131  /// getDefinitionEndLoc - Return the location of the last token in the macro.
132  ///
133  SourceLocation getDefinitionEndLoc() const { return EndLocation; }
134
135  /// \brief Get length in characters of the macro definition.
136  unsigned getDefinitionLength(SourceManager &SM) const {
137    if (IsDefinitionLengthCached)
138      return DefinitionLength;
139    return getDefinitionLengthSlow(SM);
140  }
141
142  /// isIdenticalTo - Return true if the specified macro definition is equal to
143  /// this macro in spelling, arguments, and whitespace.  This is used to emit
144  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
145  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
146
147  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
148  ///
149  void setIsBuiltinMacro(bool Val = true) {
150    IsBuiltinMacro = Val;
151  }
152
153  /// setIsUsed - Set the value of the IsUsed flag.
154  ///
155  void setIsUsed(bool Val) {
156    IsUsed = Val;
157  }
158
159  /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
160  /// IsAllowRedefinitionsWithoutWarning flag.
161  void setIsAllowRedefinitionsWithoutWarning(bool Val) {
162    IsAllowRedefinitionsWithoutWarning = Val;
163  }
164
165  /// \brief Set the value of the IsWarnIfUnused flag.
166  void setIsWarnIfUnused(bool val) {
167    IsWarnIfUnused = val;
168  }
169
170  /// setArgumentList - Set the specified list of identifiers as the argument
171  /// list for this macro.
172  void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
173                       llvm::BumpPtrAllocator &PPAllocator) {
174    assert(ArgumentList == 0 && NumArguments == 0 &&
175           "Argument list already set!");
176    if (NumArgs == 0) return;
177
178    NumArguments = NumArgs;
179    ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
180    for (unsigned i = 0; i != NumArgs; ++i)
181      ArgumentList[i] = List[i];
182  }
183
184  /// Arguments - The list of arguments for a function-like macro.  This can be
185  /// empty, for, e.g. "#define X()".
186  typedef IdentifierInfo* const *arg_iterator;
187  bool arg_empty() const { return NumArguments == 0; }
188  arg_iterator arg_begin() const { return ArgumentList; }
189  arg_iterator arg_end() const { return ArgumentList+NumArguments; }
190  unsigned getNumArgs() const { return NumArguments; }
191
192  /// getArgumentNum - Return the argument number of the specified identifier,
193  /// or -1 if the identifier is not a formal argument identifier.
194  int getArgumentNum(IdentifierInfo *Arg) const {
195    for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
196      if (*I == Arg) return I-arg_begin();
197    return -1;
198  }
199
200  /// Function/Object-likeness.  Keep track of whether this macro has formal
201  /// parameters.
202  void setIsFunctionLike() { IsFunctionLike = true; }
203  bool isFunctionLike() const { return IsFunctionLike; }
204  bool isObjectLike() const { return !IsFunctionLike; }
205
206  /// Varargs querying methods.  This can only be set for function-like macros.
207  void setIsC99Varargs() { IsC99Varargs = true; }
208  void setIsGNUVarargs() { IsGNUVarargs = true; }
209  bool isC99Varargs() const { return IsC99Varargs; }
210  bool isGNUVarargs() const { return IsGNUVarargs; }
211  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
212
213  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
214  /// __LINE__, which requires processing before expansion.
215  bool isBuiltinMacro() const { return IsBuiltinMacro; }
216
217  /// isFromAST - Return true if this macro was loaded from an AST file.
218  bool isFromAST() const { return IsFromAST; }
219
220  /// setIsFromAST - Set whether this macro was loaded from an AST file.
221  void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
222
223  /// \brief Determine whether this macro has changed since it was loaded from
224  /// an AST file.
225  bool hasChangedAfterLoad() const { return ChangedAfterLoad; }
226
227  /// \brief Note whether this macro has changed after it was loaded from an
228  /// AST file.
229  void setChangedAfterLoad(bool CAL = true) { ChangedAfterLoad = CAL; }
230
231  /// isUsed - Return false if this macro is defined in the main file and has
232  /// not yet been used.
233  bool isUsed() const { return IsUsed; }
234
235  /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
236  /// redefined without warning.
237  bool isAllowRedefinitionsWithoutWarning() const {
238    return IsAllowRedefinitionsWithoutWarning;
239  }
240
241  /// \brief Return true if we should emit a warning if the macro is unused.
242  bool isWarnIfUnused() const {
243    return IsWarnIfUnused;
244  }
245
246  /// getNumTokens - Return the number of tokens that this macro expands to.
247  ///
248  unsigned getNumTokens() const {
249    return ReplacementTokens.size();
250  }
251
252  const Token &getReplacementToken(unsigned Tok) const {
253    assert(Tok < ReplacementTokens.size() && "Invalid token #");
254    return ReplacementTokens[Tok];
255  }
256
257  typedef SmallVector<Token, 8>::const_iterator tokens_iterator;
258  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
259  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
260  bool tokens_empty() const { return ReplacementTokens.empty(); }
261
262  /// AddTokenToBody - Add the specified token to the replacement text for the
263  /// macro.
264  void AddTokenToBody(const Token &Tok) {
265    assert(!IsDefinitionLengthCached &&
266          "Changing replacement tokens after definition length got calculated");
267    ReplacementTokens.push_back(Tok);
268  }
269
270  /// isEnabled - Return true if this macro is enabled: in other words, that we
271  /// are not currently in an expansion of this macro.
272  bool isEnabled() const { return !IsDisabled; }
273
274  void EnableMacro() {
275    assert(IsDisabled && "Cannot enable an already-enabled macro!");
276    IsDisabled = false;
277  }
278
279  void DisableMacro() {
280    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
281    IsDisabled = true;
282  }
283
284  /// \brief Set the export location for this macro.
285  void setVisibility(bool Public, SourceLocation Loc) {
286    VisibilityLocation = Loc;
287    IsPublic = Public;
288  }
289
290  /// \brief Determine whether this macro is part of the public API of its
291  /// module.
292  bool isPublic() const { return IsPublic; }
293
294  /// \brief Determine the location where this macro was explicitly made
295  /// public or private within its module.
296  SourceLocation getVisibilityLocation() { return VisibilityLocation; }
297
298private:
299  unsigned getDefinitionLengthSlow(SourceManager &SM) const;
300};
301
302}  // end namespace clang
303
304#endif
305