clang  9.0.0svn
MacroArgs.h
Go to the documentation of this file.
1 //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the MacroArgs interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LEX_MACROARGS_H
14 #define LLVM_CLANG_LEX_MACROARGS_H
15 
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Lex/Token.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/Support/TrailingObjects.h"
20 #include <vector>
21 
22 namespace clang {
23  class MacroInfo;
24  class Preprocessor;
25  class SourceLocation;
26 
27 /// MacroArgs - An instance of this class captures information about
28 /// the formal arguments specified to a function-like macro invocation.
29 class MacroArgs final
30  : private llvm::TrailingObjects<MacroArgs, Token> {
31 
32  friend TrailingObjects;
33  /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
34  /// arguments. All of the actual argument tokens are allocated immediately
35  /// after the MacroArgs object in memory. This is all of the arguments
36  /// concatenated together, with 'EOF' markers at the end of each argument.
37  unsigned NumUnexpArgTokens;
38 
39  /// VarargsElided - True if this is a C99 style varargs macro invocation and
40  /// there was no argument specified for the "..." argument. If the argument
41  /// was specified (even empty) or this isn't a C99 style varargs function, or
42  /// if in strict mode and the C99 varargs macro had only a ... argument, this
43  /// is false.
44  bool VarargsElided;
45 
46  /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty
47  /// if not yet computed. This includes the EOF marker at the end of the
48  /// stream.
49  std::vector<std::vector<Token> > PreExpArgTokens;
50 
51  /// StringifiedArgs - This contains arguments in 'stringified' form. If the
52  /// stringified form of an argument has not yet been computed, this is empty.
53  std::vector<Token> StringifiedArgs;
54 
55  /// ArgCache - This is a linked list of MacroArgs objects that the
56  /// Preprocessor owns which we use to avoid thrashing malloc/free.
57  MacroArgs *ArgCache;
58 
59  /// MacroArgs - The number of arguments the invoked macro expects.
60  unsigned NumMacroArgs;
61 
62  MacroArgs(unsigned NumToks, bool varargsElided, unsigned MacroArgs)
63  : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided),
64  ArgCache(nullptr), NumMacroArgs(MacroArgs) {}
65  ~MacroArgs() = default;
66 
67 public:
68  /// MacroArgs ctor function - Create a new MacroArgs object with the specified
69  /// macro and argument info.
70  static MacroArgs *create(const MacroInfo *MI,
71  ArrayRef<Token> UnexpArgTokens,
72  bool VarargsElided, Preprocessor &PP);
73 
74  /// destroy - Destroy and deallocate the memory for this object.
75  ///
76  void destroy(Preprocessor &PP);
77 
78  /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
79  /// by pre-expansion, return false. Otherwise, conservatively return true.
80  bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
81 
82  /// getUnexpArgument - Return a pointer to the first token of the unexpanded
83  /// token list for the specified formal.
84  ///
85  const Token *getUnexpArgument(unsigned Arg) const;
86 
87  /// getArgLength - Given a pointer to an expanded or unexpanded argument,
88  /// return the number of tokens, not counting the EOF, that make up the
89  /// argument.
90  static unsigned getArgLength(const Token *ArgPtr);
91 
92  /// getPreExpArgument - Return the pre-expanded form of the specified
93  /// argument.
94  const std::vector<Token> &
95  getPreExpArgument(unsigned Arg, Preprocessor &PP);
96 
97  /// getStringifiedArgument - Compute, cache, and return the specified argument
98  /// that has been 'stringified' as required by the # operator.
99  const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP,
100  SourceLocation ExpansionLocStart,
101  SourceLocation ExpansionLocEnd);
102 
103  /// getNumMacroArguments - Return the number of arguments the invoked macro
104  /// expects.
105  unsigned getNumMacroArguments() const { return NumMacroArgs; }
106 
107  /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
108  /// invocation and there was no argument specified for the "..." argument. If
109  /// the argument was specified (even empty) or this isn't a C99 style varargs
110  /// function, or if in strict mode and the C99 varargs macro had only a ...
111  /// argument, this returns false.
112  bool isVarargsElidedUse() const { return VarargsElided; }
113 
114  /// Returns true if the macro was defined with a variadic (ellipsis) parameter
115  /// AND was invoked with at least one token supplied as a variadic argument.
116  ///
117  /// \code
118  /// #define F(a) a
119  /// #define V(a, ...) __VA_OPT__(a)
120  /// F() <-- returns false on this invocation.
121  /// V(,a) <-- returns true on this invocation.
122  /// V(,) <-- returns false on this invocation.
123  /// \endcode
124  ///
125 
126  bool invokedWithVariadicArgument(const MacroInfo *const MI) const;
127 
128  /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
129  /// tokens into the literal string token that should be produced by the C #
130  /// preprocessor operator. If Charify is true, then it should be turned into
131  /// a character literal for the Microsoft charize (#@) extension.
132  ///
133  static Token StringifyArgument(const Token *ArgToks,
134  Preprocessor &PP, bool Charify,
135  SourceLocation ExpansionLocStart,
136  SourceLocation ExpansionLocEnd);
137 
138 
139  /// deallocate - This should only be called by the Preprocessor when managing
140  /// its freelist.
141  MacroArgs *deallocate();
142 };
143 
144 } // end namespace clang
145 
146 #endif
bool invokedWithVariadicArgument(const MacroInfo *const MI) const
Returns true if the macro was defined with a variadic (ellipsis) parameter AND was invoked with at le...
Definition: MacroArgs.cpp:142
bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const
ArgNeedsPreexpansion - If we can prove that the argument won&#39;t be affected by pre-expansion, return false.
Definition: MacroArgs.cpp:151
bool isVarargsElidedUse() const
isVarargsElidedUse - Return true if this is a C99 style varargs macro invocation and there was no arg...
Definition: MacroArgs.h:112
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void destroy(Preprocessor &PP)
destroy - Destroy and deallocate the memory for this object.
Definition: MacroArgs.cpp:78
const Token & getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
getStringifiedArgument - Compute, cache, and return the specified argument that has been &#39;stringified...
Definition: MacroArgs.cpp:316
static Token StringifyArgument(const Token *ArgToks, Preprocessor &PP, bool Charify, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of tokens into the literal string...
Definition: MacroArgs.cpp:213
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:29
MacroArgs * deallocate()
deallocate - This should only be called by the Preprocessor when managing its freelist.
Definition: MacroArgs.cpp:93
Encodes a location in the source.
Dataflow Directional Tag Classes.
const std::vector< Token > & getPreExpArgument(unsigned Arg, Preprocessor &PP)
getPreExpArgument - Return the pre-expanded form of the specified argument.
Definition: MacroArgs.cpp:166
static unsigned getArgLength(const Token *ArgPtr)
getArgLength - Given a pointer to an expanded or unexpanded argument, return the number of tokens...
Definition: MacroArgs.cpp:110
unsigned getNumMacroArguments() const
getNumMacroArguments - Return the number of arguments the invoked macro expects.
Definition: MacroArgs.h:105
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
friend TrailingObjects
Definition: OpenMPClause.h:98
static MacroArgs * create(const MacroInfo *MI, ArrayRef< Token > UnexpArgTokens, bool VarargsElided, Preprocessor &PP)
MacroArgs ctor function - Create a new MacroArgs object with the specified macro and argument info...
Definition: MacroArgs.cpp:24
const Token * getUnexpArgument(unsigned Arg) const
getUnexpArgument - Return a pointer to the first token of the unexpanded token list for the specified...
Definition: MacroArgs.cpp:120
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:123