Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/MacroArgs.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- MacroArgs.cpp - Formal argument info for Macros ------------------===//
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 implements the MacroArgs interface.
10
//
11
//===----------------------------------------------------------------------===//
12
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"
19
#include <algorithm>
20
21
using namespace clang;
22
23
/// MacroArgs ctor function - This destroys the vector passed in.
24
MacroArgs *MacroArgs::create(const MacroInfo *MI,
25
                             ArrayRef<Token> UnexpArgTokens,
26
28.6M
                             bool VarargsElided, Preprocessor &PP) {
27
28.6M
  assert(MI->isFunctionLike() &&
28
28.6M
         "Can't have args for an object-like macro!");
29
28.6M
  MacroArgs **ResultEnt = nullptr;
30
28.6M
  unsigned ClosestMatch = ~0U;
31
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
90.6M
  for (MacroArgs **Entry = &PP.MacroArgCache; *Entry;
35
90.4M
       
Entry = &(*Entry)->ArgCache62.0M
) {
36
90.4M
    if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
37
90.4M
        
(*Entry)->NumUnexpArgTokens < ClosestMatch58.6M
) {
38
47.4M
      ResultEnt = Entry;
39
40
      // If we have an exact match, use it.
41
47.4M
      if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
42
28.4M
        break;
43
      // Otherwise, use the best fit.
44
19.0M
      ClosestMatch = (*Entry)->NumUnexpArgTokens;
45
19.0M
    }
46
90.4M
  }
47
28.6M
  MacroArgs *Result;
48
28.6M
  if (!ResultEnt) {
49
    // Allocate memory for a MacroArgs object with the lexer tokens at the end,
50
    // and construct the MacroArgs object.
51
83.0k
    Result = new (
52
83.0k
        llvm::safe_malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
53
83.0k
        MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
54
28.5M
  } else {
55
28.5M
    Result = *ResultEnt;
56
    // Unlink this node from the preprocessors singly linked list.
57
28.5M
    *ResultEnt = Result->ArgCache;
58
28.5M
    Result->NumUnexpArgTokens = UnexpArgTokens.size();
59
28.5M
    Result->VarargsElided = VarargsElided;
60
28.5M
    Result->NumMacroArgs = MI->getNumParams();
61
28.5M
  }
62
63
  // Copy the actual unexpanded tokens to immediately after the result ptr.
64
28.6M
  if (!UnexpArgTokens.empty()) {
65
28.6M
    static_assert(std::is_trivial_v<Token>,
66
28.6M
                  "assume trivial copyability if copying into the "
67
28.6M
                  "uninitialized array (as opposed to reusing a cached "
68
28.6M
                  "MacroArgs)");
69
28.6M
    std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
70
28.6M
              Result->getTrailingObjects<Token>());
71
28.6M
  }
72
73
28.6M
  return Result;
74
28.6M
}
75
76
/// destroy - Destroy and deallocate the memory for this object.
77
///
78
28.6M
void MacroArgs::destroy(Preprocessor &PP) {
79
  // Don't clear PreExpArgTokens, just clear the entries.  Clearing the entries
80
  // would deallocate the element vectors.
81
170M
  for (unsigned i = 0, e = PreExpArgTokens.size(); i != e; 
++i142M
)
82
142M
    PreExpArgTokens[i].clear();
83
84
  // Add this to the preprocessor's free list.
85
28.6M
  ArgCache = PP.MacroArgCache;
86
28.6M
  PP.MacroArgCache = this;
87
28.6M
}
88
89
/// deallocate - This should only be called by the Preprocessor when managing
90
/// its freelist.
91
39.4k
MacroArgs *MacroArgs::deallocate() {
92
39.4k
  MacroArgs *Next = ArgCache;
93
94
  // Run the dtor to deallocate the vectors.
95
39.4k
  this->~MacroArgs();
96
  // Release the memory for the object.
97
39.4k
  static_assert(std::is_trivially_destructible_v<Token>,
98
39.4k
                "assume trivially destructible and forego destructors");
99
39.4k
  free(this);
100
101
39.4k
  return Next;
102
39.4k
}
103
104
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
107
/// argument.
108
46.9M
unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
109
46.9M
  unsigned NumArgTokens = 0;
110
172M
  for (; ArgPtr->isNot(tok::eof); 
++ArgPtr125M
)
111
125M
    ++NumArgTokens;
112
46.9M
  return NumArgTokens;
113
46.9M
}
114
115
116
/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
117
///
118
49.4M
const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
119
120
49.4M
  assert(Arg < getNumMacroArguments() && "Invalid arg #");
121
  // The unexpanded argument tokens start immediately after the MacroArgs object
122
  // in memory.
123
49.4M
  const Token *Start = getTrailingObjects<Token>();
124
49.4M
  const Token *Result = Start;
125
126
  // Scan to find Arg.
127
186M
  for (; Arg; 
++Result137M
) {
128
137M
    assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
129
137M
    if (Result->is(tok::eof))
130
46.9M
      --Arg;
131
137M
  }
132
49.4M
  assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
133
49.4M
  return Result;
134
49.4M
}
135
136
bool MacroArgs::invokedWithVariadicArgument(const MacroInfo *const MI,
137
98
                                            Preprocessor &PP) {
138
98
  if (!MI->isVariadic())
139
0
    return false;
140
98
  const int VariadicArgIndex = getNumMacroArguments() - 1;
141
98
  return getPreExpArgument(VariadicArgIndex, PP).front().isNot(tok::eof);
142
98
}
143
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
31.4M
                                     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
114M
  for (; ArgTok->isNot(tok::eof); 
++ArgTok83.0M
)
151
89.2M
    if (IdentifierInfo *II = ArgTok->getIdentifierInfo())
152
27.4M
      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.
155
6.17M
        return true;
156
25.3M
  return false;
157
31.4M
}
158
159
/// getPreExpArgument - Return the pre-expanded form of the specified
160
/// argument.
161
const std::vector<Token> &MacroArgs::getPreExpArgument(unsigned Arg,
162
6.18M
                                                       Preprocessor &PP) {
163
6.18M
  assert(Arg < getNumMacroArguments() && "Invalid argument number!");
164
165
  // If we have already computed this, return it.
166
6.18M
  if (PreExpArgTokens.size() < getNumMacroArguments())
167
43.0k
    PreExpArgTokens.resize(getNumMacroArguments());
168
169
6.18M
  std::vector<Token> &Result = PreExpArgTokens[Arg];
170
6.18M
  if (!Result.empty()) 
return Result110k
;
171
172
6.06M
  SaveAndRestore PreExpandingMacroArgs(PP.InMacroArgPreExpansion, true);
173
174
6.06M
  const Token *AT = getUnexpArgument(Arg);
175
6.06M
  unsigned NumToks = getArgLength(AT)+1;  // Include the EOF.
176
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
6.06M
  PP.EnterTokenStream(AT, NumToks, false /*disable expand*/,
182
6.06M
                      false /*owns tokens*/, false /*is reinject*/);
183
184
  // Lex all of the macro-expanded tokens into Result.
185
15.4M
  do {
186
15.4M
    Result.push_back(Token());
187
15.4M
    Token &Tok = Result.back();
188
15.4M
    PP.Lex(Tok);
189
15.4M
  } while (Result.back().isNot(tok::eof));
190
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
6.06M
  if (PP.InCachingLexMode())
197
0
    PP.ExitCachingLexMode();
198
6.06M
  PP.RemoveTopOfLexerStack();
199
6.06M
  return Result;
200
6.18M
}
201
202
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.
207
///
208
Token MacroArgs::StringifyArgument(const Token *ArgToks,
209
                                   Preprocessor &PP, bool Charify,
210
                                   SourceLocation ExpansionLocStart,
211
1.86M
                                   SourceLocation ExpansionLocEnd) {
212
1.86M
  Token Tok;
213
1.86M
  Tok.startToken();
214
1.86M
  Tok.setKind(Charify ? 
tok::char_constant1
:
tok::string_literal1.86M
);
215
216
1.86M
  const Token *ArgTokStart = ArgToks;
217
218
  // Stringify all the tokens.
219
1.86M
  SmallString<128> Result;
220
1.86M
  Result += "\"";
221
222
1.86M
  bool isFirst = true;
223
3.78M
  for (; ArgToks->isNot(tok::eof); 
++ArgToks1.92M
) {
224
1.92M
    const Token &Tok = *ArgToks;
225
1.92M
    if (!isFirst && 
(56.5k
Tok.hasLeadingSpace()56.5k
||
Tok.isAtStartOfLine()35.2k
))
226
21.2k
      Result += ' ';
227
1.92M
    isFirst = false;
228
229
    // If this is a string or character constant, escape the token as specified
230
    // by 6.10.3.2p2.
231
1.92M
    if (tok::isStringLiteral(Tok.getKind()) || // "foo", u8R"x(foo)x"_bar, etc.
232
1.92M
        
Tok.is(tok::char_constant)1.91M
|| // 'x'
233
1.92M
        
Tok.is(tok::wide_char_constant)1.91M
|| // L'x'.
234
1.92M
        
Tok.is(tok::utf8_char_constant)1.91M
|| // u8'x'.
235
1.92M
        
Tok.is(tok::utf16_char_constant)1.91M
|| // u'x'.
236
1.92M
        
Tok.is(tok::utf32_char_constant)1.91M
) { // U'x'.
237
2.07k
      bool Invalid = false;
238
2.07k
      std::string TokStr = PP.getSpelling(Tok, &Invalid);
239
2.07k
      if (!Invalid) {
240
2.07k
        std::string Str = Lexer::Stringify(TokStr);
241
2.07k
        Result.append(Str.begin(), Str.end());
242
2.07k
      }
243
1.91M
    } else if (Tok.is(tok::code_completion)) {
244
2
      PP.CodeCompleteNaturalLanguage();
245
1.91M
    } else {
246
      // Otherwise, just append the token.  Do some gymnastics to get the token
247
      // in place and avoid copies where possible.
248
1.91M
      unsigned CurStrLen = Result.size();
249
1.91M
      Result.resize(CurStrLen+Tok.getLength());
250
1.91M
      const char *BufPtr = Result.data() + CurStrLen;
251
1.91M
      bool Invalid = false;
252
1.91M
      unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr, &Invalid);
253
254
1.91M
      if (!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
1.91M
        if (ActualTokLen && 
BufPtr != &Result[CurStrLen]1.91M
)
258
1.91M
          memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
259
260
        // If the token was dirty, the spelling may be shorter than the token.
261
1.91M
        if (ActualTokLen != Tok.getLength())
262
0
          Result.resize(CurStrLen+ActualTokLen);
263
1.91M
      }
264
1.91M
    }
265
1.92M
  }
266
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
1.86M
  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
0
    unsigned FirstNonSlash = Result.size()-2;
273
    // Guaranteed to find the starting " if nothing else.
274
0
    while (Result[FirstNonSlash] == '\\')
275
0
      --FirstNonSlash;
276
0
    if ((Result.size()-1-FirstNonSlash) & 1) {
277
      // Diagnose errors for things like: #define F(X) #X   /   F(\)
278
0
      PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
279
0
      Result.pop_back();  // remove one of the \'s.
280
0
    }
281
0
  }
282
1.86M
  Result += '"';
283
284
  // If this is the charify operation and the result is not a legal character
285
  // constant, diagnose it.
286
1.86M
  if (Charify) {
287
    // First step, turn double quotes into single quotes:
288
1
    Result[0] = '\'';
289
1
    Result[Result.size()-1] = '\'';
290
291
    // Check for bogus character.
292
1
    bool isBad = false;
293
1
    if (Result.size() == 3)
294
1
      isBad = Result[1] == '\'';   // ''' is not legal. '\' already fixed above.
295
0
    else
296
0
      isBad = (Result.size() != 4 || Result[1] != '\\');  // Not '\x'
297
298
1
    if (isBad) {
299
0
      PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
300
0
      Result = "' '";  // Use something arbitrary, but legal.
301
0
    }
302
1
  }
303
304
1.86M
  PP.CreateString(Result, Tok,
305
1.86M
                  ExpansionLocStart, ExpansionLocEnd);
306
1.86M
  return Tok;
307
1.86M
}