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