1 //===--- MacroArgs.cpp - Formal argument info for Macros ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the MacroArgs interface.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Lex/MacroArgs.h"
14 #include "clang/Lex/LexDiagnostic.h"
15 #include "clang/Lex/MacroInfo.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/SaveAndRestore.h"
21 using namespace clang
;
23 /// MacroArgs ctor function - This destroys the vector passed in.
24 MacroArgs
*MacroArgs::create(const MacroInfo
*MI
,
25 ArrayRef
<Token
> UnexpArgTokens
,
26 bool VarargsElided
, Preprocessor
&PP
) {
27 assert(MI
->isFunctionLike() &&
28 "Can't have args for an object-like macro!");
29 MacroArgs
**ResultEnt
= nullptr;
30 unsigned ClosestMatch
= ~0U;
32 // See if we have an entry with a big enough argument list to reuse on the
33 // free list. If so, reuse it.
34 for (MacroArgs
**Entry
= &PP
.MacroArgCache
; *Entry
;
35 Entry
= &(*Entry
)->ArgCache
) {
36 if ((*Entry
)->NumUnexpArgTokens
>= UnexpArgTokens
.size() &&
37 (*Entry
)->NumUnexpArgTokens
< ClosestMatch
) {
40 // If we have an exact match, use it.
41 if ((*Entry
)->NumUnexpArgTokens
== UnexpArgTokens
.size())
43 // Otherwise, use the best fit.
44 ClosestMatch
= (*Entry
)->NumUnexpArgTokens
;
49 // Allocate memory for a MacroArgs object with the lexer tokens at the end,
50 // and construct the MacroArgs object.
52 llvm::safe_malloc(totalSizeToAlloc
<Token
>(UnexpArgTokens
.size())))
53 MacroArgs(UnexpArgTokens
.size(), VarargsElided
, MI
->getNumParams());
56 // Unlink this node from the preprocessors singly linked list.
57 *ResultEnt
= Result
->ArgCache
;
58 Result
->NumUnexpArgTokens
= UnexpArgTokens
.size();
59 Result
->VarargsElided
= VarargsElided
;
60 Result
->NumMacroArgs
= MI
->getNumParams();
63 // Copy the actual unexpanded tokens to immediately after the result ptr.
64 if (!UnexpArgTokens
.empty()) {
65 static_assert(std::is_trivial_v
<Token
>,
66 "assume trivial copyability if copying into the "
67 "uninitialized array (as opposed to reusing a cached "
69 std::copy(UnexpArgTokens
.begin(), UnexpArgTokens
.end(),
70 Result
->getTrailingObjects
<Token
>());
76 /// destroy - Destroy and deallocate the memory for this object.
78 void MacroArgs::destroy(Preprocessor
&PP
) {
79 // Don't clear PreExpArgTokens, just clear the entries. Clearing the entries
80 // would deallocate the element vectors.
81 for (unsigned i
= 0, e
= PreExpArgTokens
.size(); i
!= e
; ++i
)
82 PreExpArgTokens
[i
].clear();
84 // Add this to the preprocessor's free list.
85 ArgCache
= PP
.MacroArgCache
;
86 PP
.MacroArgCache
= this;
89 /// deallocate - This should only be called by the Preprocessor when managing
91 MacroArgs
*MacroArgs::deallocate() {
92 MacroArgs
*Next
= ArgCache
;
94 // Run the dtor to deallocate the vectors.
96 // Release the memory for the object.
97 static_assert(std::is_trivially_destructible_v
<Token
>,
98 "assume trivially destructible and forego destructors");
105 /// getArgLength - Given a pointer to an expanded or unexpanded argument,
106 /// return the number of tokens, not counting the EOF, that make up the
108 unsigned MacroArgs::getArgLength(const Token
*ArgPtr
) {
109 unsigned NumArgTokens
= 0;
110 for (; ArgPtr
->isNot(tok::eof
); ++ArgPtr
)
116 /// getUnexpArgument - Return the unexpanded tokens for the specified formal.
118 const Token
*MacroArgs::getUnexpArgument(unsigned Arg
) const {
120 assert(Arg
< getNumMacroArguments() && "Invalid arg #");
121 // The unexpanded argument tokens start immediately after the MacroArgs object
123 const Token
*Start
= getTrailingObjects
<Token
>();
124 const Token
*Result
= Start
;
127 for (; Arg
; ++Result
) {
128 assert(Result
< Start
+NumUnexpArgTokens
&& "Invalid arg #");
129 if (Result
->is(tok::eof
))
132 assert(Result
< Start
+NumUnexpArgTokens
&& "Invalid arg #");
136 bool MacroArgs::invokedWithVariadicArgument(const MacroInfo
*const MI
,
138 if (!MI
->isVariadic())
140 const int VariadicArgIndex
= getNumMacroArguments() - 1;
141 return getPreExpArgument(VariadicArgIndex
, PP
).front().isNot(tok::eof
);
144 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
145 /// by pre-expansion, return false. Otherwise, conservatively return true.
146 bool MacroArgs::ArgNeedsPreexpansion(const Token
*ArgTok
,
147 Preprocessor
&PP
) const {
148 // If there are no identifiers in the argument list, or if the identifiers are
149 // known to not be macros, pre-expansion won't modify it.
150 for (; ArgTok
->isNot(tok::eof
); ++ArgTok
)
151 if (IdentifierInfo
*II
= ArgTok
->getIdentifierInfo())
152 if (II
->hasMacroDefinition())
153 // Return true even though the macro could be a function-like macro
154 // without a following '(' token, or could be disabled, or not visible.
159 /// getPreExpArgument - Return the pre-expanded form of the specified
161 const std::vector
<Token
> &MacroArgs::getPreExpArgument(unsigned Arg
,
163 assert(Arg
< getNumMacroArguments() && "Invalid argument number!");
165 // If we have already computed this, return it.
166 if (PreExpArgTokens
.size() < getNumMacroArguments())
167 PreExpArgTokens
.resize(getNumMacroArguments());
169 std::vector
<Token
> &Result
= PreExpArgTokens
[Arg
];
170 if (!Result
.empty()) return Result
;
172 SaveAndRestore
PreExpandingMacroArgs(PP
.InMacroArgPreExpansion
, true);
174 const Token
*AT
= getUnexpArgument(Arg
);
175 unsigned NumToks
= getArgLength(AT
)+1; // Include the EOF.
177 // Otherwise, we have to pre-expand this argument, populating Result. To do
178 // this, we set up a fake TokenLexer to lex from the unexpanded argument
179 // list. With this installed, we lex expanded tokens until we hit the EOF
180 // token at the end of the unexp list.
181 PP
.EnterTokenStream(AT
, NumToks
, false /*disable expand*/,
182 false /*owns tokens*/, false /*is reinject*/);
184 // Lex all of the macro-expanded tokens into Result.
186 Result
.push_back(Token());
187 Token
&Tok
= Result
.back();
189 } while (Result
.back().isNot(tok::eof
));
191 // Pop the token stream off the top of the stack. We know that the internal
192 // pointer inside of it is to the "end" of the token stream, but the stack
193 // will not otherwise be popped until the next token is lexed. The problem is
194 // that the token may be lexed sometime after the vector of tokens itself is
195 // destroyed, which would be badness.
196 if (PP
.InCachingLexMode())
197 PP
.ExitCachingLexMode();
198 PP
.RemoveTopOfLexerStack();
203 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
204 /// tokens into the literal string token that should be produced by the C #
205 /// preprocessor operator. If Charify is true, then it should be turned into
206 /// a character literal for the Microsoft charize (#@) extension.
208 Token
MacroArgs::StringifyArgument(const Token
*ArgToks
,
209 Preprocessor
&PP
, bool Charify
,
210 SourceLocation ExpansionLocStart
,
211 SourceLocation ExpansionLocEnd
) {
214 Tok
.setKind(Charify
? tok::char_constant
: tok::string_literal
);
216 const Token
*ArgTokStart
= ArgToks
;
218 // Stringify all the tokens.
219 SmallString
<128> Result
;
223 for (; ArgToks
->isNot(tok::eof
); ++ArgToks
) {
224 const Token
&Tok
= *ArgToks
;
225 if (!isFirst
&& (Tok
.hasLeadingSpace() || Tok
.isAtStartOfLine()))
229 // If this is a string or character constant, escape the token as specified
231 if (tok::isStringLiteral(Tok
.getKind()) || // "foo", u8R"x(foo)x"_bar, etc.
232 Tok
.is(tok::char_constant
) || // 'x'
233 Tok
.is(tok::wide_char_constant
) || // L'x'.
234 Tok
.is(tok::utf8_char_constant
) || // u8'x'.
235 Tok
.is(tok::utf16_char_constant
) || // u'x'.
236 Tok
.is(tok::utf32_char_constant
)) { // U'x'.
237 bool Invalid
= false;
238 std::string TokStr
= PP
.getSpelling(Tok
, &Invalid
);
240 std::string Str
= Lexer::Stringify(TokStr
);
241 Result
.append(Str
.begin(), Str
.end());
243 } else if (Tok
.is(tok::code_completion
)) {
244 PP
.CodeCompleteNaturalLanguage();
246 // Otherwise, just append the token. Do some gymnastics to get the token
247 // in place and avoid copies where possible.
248 unsigned CurStrLen
= Result
.size();
249 Result
.resize(CurStrLen
+Tok
.getLength());
250 const char *BufPtr
= Result
.data() + CurStrLen
;
251 bool Invalid
= false;
252 unsigned ActualTokLen
= PP
.getSpelling(Tok
, BufPtr
, &Invalid
);
255 // If getSpelling returned a pointer to an already uniqued version of
256 // the string instead of filling in BufPtr, memcpy it onto our string.
257 if (ActualTokLen
&& BufPtr
!= &Result
[CurStrLen
])
258 memcpy(&Result
[CurStrLen
], BufPtr
, ActualTokLen
);
260 // If the token was dirty, the spelling may be shorter than the token.
261 if (ActualTokLen
!= Tok
.getLength())
262 Result
.resize(CurStrLen
+ActualTokLen
);
267 // If the last character of the string is a \, and if it isn't escaped, this
268 // is an invalid string literal, diagnose it as specified in C99.
269 if (Result
.back() == '\\') {
270 // Count the number of consecutive \ characters. If even, then they are
271 // just escaped backslashes, otherwise it's an error.
272 unsigned FirstNonSlash
= Result
.size()-2;
273 // Guaranteed to find the starting " if nothing else.
274 while (Result
[FirstNonSlash
] == '\\')
276 if ((Result
.size()-1-FirstNonSlash
) & 1) {
277 // Diagnose errors for things like: #define F(X) #X / F(\)
278 PP
.Diag(ArgToks
[-1], diag::pp_invalid_string_literal
);
279 Result
.pop_back(); // remove one of the \'s.
284 // If this is the charify operation and the result is not a legal character
285 // constant, diagnose it.
287 // First step, turn double quotes into single quotes:
289 Result
[Result
.size()-1] = '\'';
291 // Check for bogus character.
293 if (Result
.size() == 3)
294 isBad
= Result
[1] == '\''; // ''' is not legal. '\' already fixed above.
296 isBad
= (Result
.size() != 4 || Result
[1] != '\\'); // Not '\x'
299 PP
.Diag(ArgTokStart
[0], diag::err_invalid_character_to_charify
);
300 Result
= "' '"; // Use something arbitrary, but legal.
304 PP
.CreateString(Result
, Tok
,
305 ExpansionLocStart
, ExpansionLocEnd
);