Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:57

0001 //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the MacroArgs interface.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_LEX_MACROARGS_H
0014 #define LLVM_CLANG_LEX_MACROARGS_H
0015 
0016 #include "clang/Basic/LLVM.h"
0017 #include "clang/Lex/Token.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/Support/TrailingObjects.h"
0020 #include <vector>
0021 
0022 namespace clang {
0023   class MacroInfo;
0024   class Preprocessor;
0025   class SourceLocation;
0026 
0027 /// MacroArgs - An instance of this class captures information about
0028 /// the formal arguments specified to a function-like macro invocation.
0029 class MacroArgs final
0030     : private llvm::TrailingObjects<MacroArgs, Token> {
0031 
0032   friend TrailingObjects;
0033   /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
0034   /// arguments.  All of the actual argument tokens are allocated immediately
0035   /// after the MacroArgs object in memory.  This is all of the arguments
0036   /// concatenated together, with 'EOF' markers at the end of each argument.
0037   unsigned NumUnexpArgTokens;
0038 
0039   /// VarargsElided - True if this is a C99 style varargs macro invocation and
0040   /// there was no argument specified for the "..." argument.  If the argument
0041   /// was specified (even empty) or this isn't a C99 style varargs function, or
0042   /// if in strict mode and the C99 varargs macro had only a ... argument, this
0043   /// is false.
0044   bool VarargsElided;
0045 
0046   /// PreExpArgTokens - Pre-expanded tokens for arguments that need them.  Empty
0047   /// if not yet computed.  This includes the EOF marker at the end of the
0048   /// stream.
0049   std::vector<std::vector<Token> > PreExpArgTokens;
0050 
0051   /// ArgCache - This is a linked list of MacroArgs objects that the
0052   /// Preprocessor owns which we use to avoid thrashing malloc/free.
0053   MacroArgs *ArgCache;
0054 
0055   /// MacroArgs - The number of arguments the invoked macro expects.
0056   unsigned NumMacroArgs;
0057 
0058   MacroArgs(unsigned NumToks, bool varargsElided, unsigned MacroArgs)
0059       : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided),
0060         ArgCache(nullptr), NumMacroArgs(MacroArgs) {}
0061   ~MacroArgs() = default;
0062 
0063 public:
0064   /// MacroArgs ctor function - Create a new MacroArgs object with the specified
0065   /// macro and argument info.
0066   static MacroArgs *create(const MacroInfo *MI,
0067                            ArrayRef<Token> UnexpArgTokens,
0068                            bool VarargsElided, Preprocessor &PP);
0069 
0070   /// destroy - Destroy and deallocate the memory for this object.
0071   ///
0072   void destroy(Preprocessor &PP);
0073 
0074   /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
0075   /// by pre-expansion, return false.  Otherwise, conservatively return true.
0076   bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
0077 
0078   /// getUnexpArgument - Return a pointer to the first token of the unexpanded
0079   /// token list for the specified formal.
0080   ///
0081   const Token *getUnexpArgument(unsigned Arg) const;
0082 
0083   /// getArgLength - Given a pointer to an expanded or unexpanded argument,
0084   /// return the number of tokens, not counting the EOF, that make up the
0085   /// argument.
0086   static unsigned getArgLength(const Token *ArgPtr);
0087 
0088   /// getPreExpArgument - Return the pre-expanded form of the specified
0089   /// argument.
0090   const std::vector<Token> &
0091     getPreExpArgument(unsigned Arg, Preprocessor &PP);
0092 
0093   /// getNumMacroArguments - Return the number of arguments the invoked macro
0094   /// expects.
0095   unsigned getNumMacroArguments() const { return NumMacroArgs; }
0096 
0097   /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
0098   /// invocation and there was no argument specified for the "..." argument.  If
0099   /// the argument was specified (even empty) or this isn't a C99 style varargs
0100   /// function, or if in strict mode and the C99 varargs macro had only a ...
0101   /// argument, this returns false.
0102   bool isVarargsElidedUse() const { return VarargsElided; }
0103 
0104   /// Returns true if the macro was defined with a variadic (ellipsis) parameter
0105   /// AND was invoked with at least one token supplied as a variadic argument
0106   /// (after pre-expansion).
0107   ///
0108   /// \code
0109   ///   #define F(a)  a
0110   ///   #define V(a, ...) __VA_OPT__(a)
0111   ///   F()     <-- returns false on this invocation.
0112   ///   V(,a)   <-- returns true on this invocation.
0113   ///   V(,)    <-- returns false on this invocation.
0114   ///   V(,F()) <-- returns false on this invocation.
0115   /// \endcode
0116   ///
0117   bool invokedWithVariadicArgument(const MacroInfo *const MI, Preprocessor &PP);
0118 
0119   /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
0120   /// tokens into the literal string token that should be produced by the C #
0121   /// preprocessor operator.  If Charify is true, then it should be turned into
0122   /// a character literal for the Microsoft charize (#@) extension.
0123   ///
0124   static Token StringifyArgument(const Token *ArgToks,
0125                                  Preprocessor &PP, bool Charify,
0126                                  SourceLocation ExpansionLocStart,
0127                                  SourceLocation ExpansionLocEnd);
0128 
0129 
0130   /// deallocate - This should only be called by the Preprocessor when managing
0131   /// its freelist.
0132   MacroArgs *deallocate();
0133 };
0134 
0135 }  // end namespace clang
0136 
0137 #endif