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