MacroInfo.h revision f46f68b5587b6933a92260220567ea7c36924a80
1//===--- MacroInfo.h - Information about #defined identifiers ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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/LexerToken.h"
18#include "llvm/ADT/SmallVector.h"
19#include <vector>
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
34  /// Arguments - The list of arguments for a function-like macro.  This can be
35  /// empty, for, e.g. "#define X()".  In a C99-style variadic macro, this
36  /// includes the __VA_ARGS__ identifier on the list.
37  std::vector<IdentifierInfo*> Arguments;
38
39  /// ReplacementTokens - This is the list of tokens that the macro is defined
40  /// to.
41  llvm::SmallVector<LexerToken, 8> ReplacementTokens;
42
43  /// IsFunctionLike - True if this macro is a function-like macro, false if it
44  /// is an object-like macro.
45  bool IsFunctionLike : 1;
46
47  /// IsC99Varargs - True if this macro is of the form "#define X(...)" or
48  /// "#define X(Y,Z,...)".  The __VA_ARGS__ token should be replaced with the
49  /// contents of "..." in an invocation.
50  bool IsC99Varargs : 1;
51
52  /// IsGNUVarargs -  True if this macro is of the form "#define X(a...)".  The
53  /// "a" identifier in th replacement list will be replaced with all arguments
54  /// of the macro starting with the specified one.
55  bool IsGNUVarargs : 1;
56
57  /// IsBuiltinMacro - True if this is a builtin macro, such as __LINE__, and if
58  /// it has not yet been redefined or undefined.
59  bool IsBuiltinMacro : 1;
60
61  /// IsTargetSpecific - True if this is a target-specific macro defined with
62  /// #define_target.
63  bool IsTargetSpecific : 1;
64private:
65  //===--------------------------------------------------------------------===//
66  // State that changes as the macro is used.
67
68  /// IsDisabled - True if we have started an expansion of this macro already.
69  /// This disbles recursive expansion, which would be quite bad for things like
70  /// #define A A.
71  bool IsDisabled : 1;
72
73  /// IsUsed - True if this macro is either defined in the main file and has
74  /// been used, or if it is not defined in the main file.  This is used to
75  /// emit -Wunused-macros diagnostics.
76  bool IsUsed : 1;
77public:
78  MacroInfo(SourceLocation DefLoc);
79
80  /// getDefinitionLoc - Return the location that the macro was defined at.
81  ///
82  SourceLocation getDefinitionLoc() const { return Location; }
83
84  /// isIdenticalTo - Return true if the specified macro definition is equal to
85  /// this macro in spelling, arguments, and whitespace.  This is used to emit
86  /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
87  bool isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const;
88
89  /// setIsBuiltinMacro - Set or clear the isBuiltinMacro flag.
90  ///
91  void setIsBuiltinMacro(bool Val = true) {
92    IsBuiltinMacro = Val;
93  }
94
95  /// setIsTargetSpecific - Set or clear the IsTargetSpecific flag.
96  ///
97  void setIsTargetSpecific(bool Val = true) {
98    IsTargetSpecific = Val;
99  }
100  bool isTargetSpecific() const { return IsTargetSpecific; }
101
102  /// setIsUsed - Set the value of the IsUsed flag.
103  ///
104  void setIsUsed(bool Val) {
105    IsUsed = Val;
106  }
107
108  /// addArgument - Add an argument to the list of formal arguments for this
109  /// function-like macro.
110  void addArgument(IdentifierInfo *Arg) {
111    Arguments.push_back(Arg);
112  }
113
114  /// getArgumentNum - Return the argument number of the specified identifier,
115  /// or -1 if the identifier is not a formal argument identifier.
116  int getArgumentNum(IdentifierInfo *Arg) {
117    for (unsigned i = 0, e = Arguments.size(); i != e; ++i)
118      if (Arguments[i] == Arg) return i;
119    return -1;
120  }
121
122  /// Arguments - The list of arguments for a function-like macro.  This can be
123  /// empty, for, e.g. "#define X()".
124  typedef std::vector<IdentifierInfo*>::const_iterator arg_iterator;
125  arg_iterator arg_begin() const { return Arguments.begin(); }
126  arg_iterator arg_end() const { return Arguments.end(); }
127  unsigned getNumArgs() const { return Arguments.size(); }
128
129  /// Function/Object-likeness.  Keep track of whether this macro has formal
130  /// parameters.
131  void setIsFunctionLike() { IsFunctionLike = true; }
132  bool isFunctionLike() const { return IsFunctionLike; }
133  bool isObjectLike() const { return !IsFunctionLike; }
134
135  /// Varargs querying methods.  This can only be set for function-like macros.
136  void setIsC99Varargs() { IsC99Varargs = true; }
137  void setIsGNUVarargs() { IsGNUVarargs = true; }
138  bool isC99Varargs() const { return IsC99Varargs; }
139  bool isGNUVarargs() const { return IsGNUVarargs; }
140  bool isVariadic() const { return IsC99Varargs | IsGNUVarargs; }
141
142  /// isBuiltinMacro - Return true if this macro is a builtin macro, such as
143  /// __LINE__, which requires processing before expansion.
144  bool isBuiltinMacro() const { return IsBuiltinMacro; }
145
146  /// isUsed - Return false if this macro is defined in the main file and has
147  /// not yet been used.
148  bool isUsed() const { return IsUsed; }
149
150  /// getNumTokens - Return the number of tokens that this macro expands to.
151  ///
152  unsigned getNumTokens() const {
153    return ReplacementTokens.size();
154  }
155
156  const LexerToken &getReplacementToken(unsigned Tok) const {
157    assert(Tok < ReplacementTokens.size() && "Invalid token #");
158    return ReplacementTokens[Tok];
159  }
160
161  typedef llvm::SmallVector<LexerToken, 8>::const_iterator tokens_iterator;
162  tokens_iterator tokens_begin() const { return ReplacementTokens.begin(); }
163  tokens_iterator tokens_end() const { return ReplacementTokens.end(); }
164
165  /// AddTokenToBody - Add the specified token to the replacement text for the
166  /// macro.
167  void AddTokenToBody(const LexerToken &Tok) {
168    ReplacementTokens.push_back(Tok);
169  }
170
171  /// isEnabled - Return true if this macro is enabled: in other words, that we
172  /// are not currently in an expansion of this macro.
173  bool isEnabled() const { return !IsDisabled; }
174
175  void EnableMacro() {
176    assert(IsDisabled && "Cannot enable an already-enabled macro!");
177    IsDisabled = false;
178  }
179
180  void DisableMacro() {
181    assert(!IsDisabled && "Cannot disable an already-disabled macro!");
182    IsDisabled = true;
183  }
184};
185
186}  // end namespace clang
187
188#endif
189