MacroInfo.h revision c69a505cfa318d571ce8a0cd038c8d958585a735
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 __VA_ARGS__ identifier on the list.
39  IdentifierInfo **ArgumentList;
40  unsigned NumArguments;
41
42  /// ReplacementTokens - This is the list of tokens that the macro is defined
43  /// to.
44  llvm::SmallVector<Token, 8> ReplacementTokens;
45
46  /// IsFunctionLike - True if this macro is a function-like macro, false if it
47  /// is an object-like macro.
48  bool IsFunctionLike : 1;
49
50  /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
51  /// "#define X(Y,Z,...)".  The __VA_ARGS__ token should be replaced with the
52  /// contents of "..." in an invocation.
53  bool IsC99Varargs : 1;
54
55  /// IsGNUVarargs -  True if this macro is of the form "#define X(a...)".  The
56  /// "a" identifier in the replacement list will be replaced with all arguments
57  /// of the macro starting with the specified one.
58  bool IsGNUVarargs : 1;
59
60  /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
61  /// it has not yet been redefined or undefined.
62  bool IsBuiltinMacro : 1;
63
64  /// IsFromAST - True if this macro was loaded from an AST file.
65  bool IsFromAST : 1;
66
67private:
68  //===--------------------------------------------------------------------===//
69  // State that changes as the macro is used.
70
71  /// IsDisabled - True if we have started an expansion of this macro already.
72  /// This disbles recursive expansion, which would be quite bad for things like
73  /// #define A A.
74  bool IsDisabled : 1;
75
76  /// IsUsed - True if this macro is either defined in the main file and has
77  /// been used, or if it is not defined in the main file.  This is used to
78  /// emit -Wunused-macros diagnostics.
79  bool IsUsed : 1;
80
81  /// AllowRedefinitionsWithoutWarning - True if this macro can be redefined
82  /// without emitting a warning.
83  bool IsAllowRedefinitionsWithoutWarning : 1;
84
85  /// \brief Must warn if the macro is unused at the end of translation unit.
86  bool IsWarnIfUnused : 1;
87
88   ~MacroInfo() {
89    assert(ArgumentList == 0 && "Didn't call destroy before dtor!");
90  }
91
92public:
93  MacroInfo(SourceLocation DefLoc);
94  MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator);
95
96  /// FreeArgumentList - Free the argument list of the macro, restoring it to a
97  /// state where it can be reused for other devious purposes.
98  void FreeArgumentList() {
99    ArgumentList = 0;
100    NumArguments = 0;
101  }
102
103  /// Destroy - destroy this MacroInfo object.
104  void Destroy() {
105    FreeArgumentList();
106    this->~MacroInfo();
107  }
108
109  /// getDefinitionLoc - Return the location that the macro was defined at.
110  ///
111  SourceLocation getDefinitionLoc() const { return Location; }
112
113  /// setDefinitionEndLoc - Set the location of the last token in the macro.
114  ///
115  void setDefinitionEndLoc(SourceLocation EndLoc) { EndLocation = EndLoc; }
116  /// getDefinitionEndLoc - Return the location of the last token in the macro.
117  ///
118  SourceLocation getDefinitionEndLoc() const { return EndLocation; }
119
120  /// isIdenticalTo - Return true if the specified macro definition is equal to
121  /// this macro in spelling, arguments, and whitespace.  This is used to emit
122  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
123  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
124
125  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
126  ///
127  void setIsBuiltinMacro(bool Val = true) {
128    IsBuiltinMacro = Val;
129  }
130
131  /// setIsUsed - Set the value of the IsUsed flag.
132  ///
133  void setIsUsed(bool Val) {
134    IsUsed = Val;
135  }
136
137  /// setIsAllowRedefinitionsWithoutWarning - Set the value of the
138  /// IsAllowRedefinitionsWithoutWarning flag.
139  void setIsAllowRedefinitionsWithoutWarning(bool Val) {
140    IsAllowRedefinitionsWithoutWarning = Val;
141  }
142
143  /// \brief Set the value of the IsWarnIfUnused flag.
144  void setIsWarnIfUnused(bool val) {
145    IsWarnIfUnused = val;
146  }
147
148  /// setArgumentList - Set the specified list of identifiers as the argument
149  /// list for this macro.
150  void setArgumentList(IdentifierInfo* const *List, unsigned NumArgs,
151                       llvm::BumpPtrAllocator &PPAllocator) {
152    assert(ArgumentList == 0 && NumArguments == 0 &&
153           "Argument list already set!");
154    if (NumArgs == 0) return;
155
156    NumArguments = NumArgs;
157    ArgumentList = PPAllocator.Allocate<IdentifierInfo*>(NumArgs);
158    for (unsigned i = 0; i != NumArgs; ++i)
159      ArgumentList[i] = List[i];
160  }
161
162  /// Arguments - The list of arguments for a function-like macro.  This can be
163  /// empty, for, e.g. "#define X()".
164  typedef IdentifierInfo* const *arg_iterator;
165  bool arg_empty() const { return NumArguments == 0; }
166  arg_iterator arg_begin() const { return ArgumentList; }
167  arg_iterator arg_end() const { return ArgumentList+NumArguments; }
168  unsigned getNumArgs() const { return NumArguments; }
169
170  /// getArgumentNum - Return the argument number of the specified identifier,
171  /// or -1 if the identifier is not a formal argument identifier.
172  int getArgumentNum(IdentifierInfo *Arg) const {
173    for (arg_iterator I = arg_begin(), E = arg_end(); I != E; ++I)
174      if (*I == Arg) return I-arg_begin();
175    return -1;
176  }
177
178  /// Function/Object-likeness.  Keep track of whether this macro has formal
179  /// parameters.
180  void setIsFunctionLike() { IsFunctionLike = true; }
181  bool isFunctionLike() const { return IsFunctionLike; }
182  bool isObjectLike() const { return !IsFunctionLike; }
183
184  /// Varargs querying methods.  This can only be set for function-like macros.
185  void setIsC99Varargs() { IsC99Varargs = true; }
186  void setIsGNUVarargs() { IsGNUVarargs = true; }
187  bool isC99Varargs() const { return IsC99Varargs; }
188  bool isGNUVarargs() const { return IsGNUVarargs; }
189  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
190
191  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
192  /// __LINE__, which requires processing before expansion.
193  bool isBuiltinMacro() const { return IsBuiltinMacro; }
194
195  /// isFromAST - Return true if this macro was loaded from an AST file.
196  bool isFromAST() const { return IsFromAST; }
197
198  /// setIsFromAST - Set whether this macro was loaded from an AST file.
199  void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
200
201  /// isUsed - Return false if this macro is defined in the main file and has
202  /// not yet been used.
203  bool isUsed() const { return IsUsed; }
204
205  /// isAllowRedefinitionsWithoutWarning - Return true if this macro can be
206  /// redefined without warning.
207  bool isAllowRedefinitionsWithoutWarning() const {
208    return IsAllowRedefinitionsWithoutWarning;
209  }
210
211  /// \brief Return true if we should emit a warning if the macro is unused.
212  bool isWarnIfUnused() const {
213    return IsWarnIfUnused;
214  }
215
216  /// getNumTokens - Return the number of tokens that this macro expands to.
217  ///
218  unsigned getNumTokens() const {
219    return ReplacementTokens.size();
220  }
221
222  const Token &getReplacementToken(unsigned Tok) const {
223    assert(Tok < ReplacementTokens.size() && "Invalid token #");
224    return ReplacementTokens[Tok];
225  }
226
227  typedef llvm::SmallVector<Token, 8>::const_iterator tokens_iterator;
228  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
229  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
230  bool tokens_empty() const { return ReplacementTokens.empty(); }
231
232  /// AddTokenToBody - Add the specified token to the replacement text for the
233  /// macro.
234  void AddTokenToBody(const Token &Tok) {
235    ReplacementTokens.push_back(Tok);
236  }
237
238  /// isEnabled - Return true if this macro is enabled: in other words, that we
239  /// are not currently in an expansion of this macro.
240  bool isEnabled() const { return !IsDisabled; }
241
242  void EnableMacro() {
243    assert(IsDisabled && "Cannot enable an already-enabled macro!");
244    IsDisabled = false;
245  }
246
247  void DisableMacro() {
248    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
249    IsDisabled = true;
250  }
251};
252
253}  // end namespace clang
254
255#endif
256