MacroArgs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the MacroArgs interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_LEX_MACROARGS_H
  18. #define LLVM_CLANG_LEX_MACROARGS_H
  19. #include "clang/Basic/LLVM.h"
  20. #include "clang/Lex/Token.h"
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/Support/TrailingObjects.h"
  23. #include <vector>
  24. namespace clang {
  25. class MacroInfo;
  26. class Preprocessor;
  27. class SourceLocation;
  28. /// MacroArgs - An instance of this class captures information about
  29. /// the formal arguments specified to a function-like macro invocation.
  30. class MacroArgs final
  31. : private llvm::TrailingObjects<MacroArgs, Token> {
  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. /// VarargsElided - True if this is a C99 style varargs macro invocation and
  39. /// there was no argument specified for the "..." argument. If the argument
  40. /// was specified (even empty) or this isn't a C99 style varargs function, or
  41. /// if in strict mode and the C99 varargs macro had only a ... argument, this
  42. /// is false.
  43. bool VarargsElided;
  44. /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty
  45. /// if not yet computed. This includes the EOF marker at the end of the
  46. /// stream.
  47. std::vector<std::vector<Token> > PreExpArgTokens;
  48. /// ArgCache - This is a linked list of MacroArgs objects that the
  49. /// Preprocessor owns which we use to avoid thrashing malloc/free.
  50. MacroArgs *ArgCache;
  51. /// MacroArgs - The number of arguments the invoked macro expects.
  52. unsigned NumMacroArgs;
  53. MacroArgs(unsigned NumToks, bool varargsElided, unsigned MacroArgs)
  54. : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided),
  55. ArgCache(nullptr), NumMacroArgs(MacroArgs) {}
  56. ~MacroArgs() = default;
  57. public:
  58. /// MacroArgs ctor function - Create a new MacroArgs object with the specified
  59. /// macro and argument info.
  60. static MacroArgs *create(const MacroInfo *MI,
  61. ArrayRef<Token> UnexpArgTokens,
  62. bool VarargsElided, Preprocessor &PP);
  63. /// destroy - Destroy and deallocate the memory for this object.
  64. ///
  65. void destroy(Preprocessor &PP);
  66. /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
  67. /// by pre-expansion, return false. Otherwise, conservatively return true.
  68. bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const;
  69. /// getUnexpArgument - Return a pointer to the first token of the unexpanded
  70. /// token list for the specified formal.
  71. ///
  72. const Token *getUnexpArgument(unsigned Arg) const;
  73. /// getArgLength - Given a pointer to an expanded or unexpanded argument,
  74. /// return the number of tokens, not counting the EOF, that make up the
  75. /// argument.
  76. static unsigned getArgLength(const Token *ArgPtr);
  77. /// getPreExpArgument - Return the pre-expanded form of the specified
  78. /// argument.
  79. const std::vector<Token> &
  80. getPreExpArgument(unsigned Arg, Preprocessor &PP);
  81. /// getNumMacroArguments - Return the number of arguments the invoked macro
  82. /// expects.
  83. unsigned getNumMacroArguments() const { return NumMacroArgs; }
  84. /// isVarargsElidedUse - Return true if this is a C99 style varargs macro
  85. /// invocation and there was no argument specified for the "..." argument. If
  86. /// the argument was specified (even empty) or this isn't a C99 style varargs
  87. /// function, or if in strict mode and the C99 varargs macro had only a ...
  88. /// argument, this returns false.
  89. bool isVarargsElidedUse() const { return VarargsElided; }
  90. /// Returns true if the macro was defined with a variadic (ellipsis) parameter
  91. /// AND was invoked with at least one token supplied as a variadic argument
  92. /// (after pre-expansion).
  93. ///
  94. /// \code
  95. /// #define F(a) a
  96. /// #define V(a, ...) __VA_OPT__(a)
  97. /// F() <-- returns false on this invocation.
  98. /// V(,a) <-- returns true on this invocation.
  99. /// V(,) <-- returns false on this invocation.
  100. /// V(,F()) <-- returns false on this invocation.
  101. /// \endcode
  102. ///
  103. bool invokedWithVariadicArgument(const MacroInfo *const MI, Preprocessor &PP);
  104. /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
  105. /// tokens into the literal string token that should be produced by the C #
  106. /// preprocessor operator. If Charify is true, then it should be turned into
  107. /// a character literal for the Microsoft charize (#@) extension.
  108. ///
  109. static Token StringifyArgument(const Token *ArgToks,
  110. Preprocessor &PP, bool Charify,
  111. SourceLocation ExpansionLocStart,
  112. SourceLocation ExpansionLocEnd);
  113. /// deallocate - This should only be called by the Preprocessor when managing
  114. /// its freelist.
  115. MacroArgs *deallocate();
  116. };
  117. } // end namespace clang
  118. #endif
  119. #ifdef __GNUC__
  120. #pragma GCC diagnostic pop
  121. #endif