Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/PPExpressions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
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 Preprocessor::EvaluateDirectiveExpression method,
10
// which parses and evaluates integer constant expressions for #if directives.
11
//
12
//===----------------------------------------------------------------------===//
13
//
14
// FIXME: implement testing for #assert's.
15
//
16
//===----------------------------------------------------------------------===//
17
18
#include "clang/Basic/IdentifierTable.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Basic/SourceManager.h"
21
#include "clang/Basic/TargetInfo.h"
22
#include "clang/Basic/TokenKinds.h"
23
#include "clang/Lex/CodeCompletionHandler.h"
24
#include "clang/Lex/LexDiagnostic.h"
25
#include "clang/Lex/LiteralSupport.h"
26
#include "clang/Lex/MacroInfo.h"
27
#include "clang/Lex/PPCallbacks.h"
28
#include "clang/Lex/Preprocessor.h"
29
#include "clang/Lex/Token.h"
30
#include "llvm/ADT/APSInt.h"
31
#include "llvm/ADT/STLExtras.h"
32
#include "llvm/ADT/SmallString.h"
33
#include "llvm/ADT/StringExtras.h"
34
#include "llvm/ADT/StringRef.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include "llvm/Support/SaveAndRestore.h"
37
#include <cassert>
38
39
using namespace clang;
40
41
namespace {
42
43
/// PPValue - Represents the value of a subexpression of a preprocessor
44
/// conditional and the source range covered by it.
45
class PPValue {
46
  SourceRange Range;
47
  IdentifierInfo *II = nullptr;
48
49
public:
50
  llvm::APSInt Val;
51
52
  // Default ctor - Construct an 'invalid' PPValue.
53
4.83M
  PPValue(unsigned BitWidth) : Val(BitWidth) {}
54
55
  // If this value was produced by directly evaluating an identifier, produce
56
  // that identifier.
57
11
  IdentifierInfo *getIdentifier() const { return II; }
58
9.31M
  void setIdentifier(IdentifierInfo *II) { this->II = II; }
59
60
4.08M
  unsigned getBitWidth() const { return Val.getBitWidth(); }
61
1.88M
  bool isUnsigned() const { return Val.isUnsigned(); }
62
63
4.83M
  SourceRange getRange() const { return Range; }
64
65
2.97M
  void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
66
231k
  void setRange(SourceLocation B, SourceLocation E) {
67
231k
    Range.setBegin(B); Range.setEnd(E);
68
231k
  }
69
2.74M
  void setBegin(SourceLocation L) { Range.setBegin(L); }
70
5.73M
  void setEnd(SourceLocation L) { Range.setEnd(L); }
71
};
72
73
} // end anonymous namespace
74
75
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
76
                                     Token &PeekTok, bool ValueLive,
77
                                     bool &IncludedUndefinedIds,
78
                                     Preprocessor &PP);
79
80
/// DefinedTracker - This struct is used while parsing expressions to keep track
81
/// of whether !defined(X) has been seen.
82
///
83
/// With this simple scheme, we handle the basic forms:
84
///    !defined(X)   and !defined X
85
/// but we also trivially handle (silly) stuff like:
86
///    !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
87
struct DefinedTracker {
88
  /// Each time a Value is evaluated, it returns information about whether the
89
  /// parsed value is of the form defined(X), !defined(X) or is something else.
90
  enum TrackerState {
91
    DefinedMacro,        // defined(X)
92
    NotDefinedMacro,     // !defined(X)
93
    Unknown              // Something else.
94
  } State;
95
  /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
96
  /// indicates the macro that was checked.
97
  IdentifierInfo *TheMacro;
98
  bool IncludedUndefinedIds = false;
99
};
100
101
/// EvaluateDefined - Process a 'defined(sym)' expression.
102
static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
103
1.85M
                            bool ValueLive, Preprocessor &PP) {
104
1.85M
  SourceLocation beginLoc(PeekTok.getLocation());
105
1.85M
  Result.setBegin(beginLoc);
106
107
  // Get the next token, don't expand it.
108
1.85M
  PP.LexUnexpandedNonComment(PeekTok);
109
110
  // Two options, it can either be a pp-identifier or a (.
111
1.85M
  SourceLocation LParenLoc;
112
1.85M
  if (PeekTok.is(tok::l_paren)) {
113
    // Found a paren, remember we saw it and skip it.
114
1.83M
    LParenLoc = PeekTok.getLocation();
115
1.83M
    PP.LexUnexpandedNonComment(PeekTok);
116
1.83M
  }
117
118
1.85M
  if (PeekTok.is(tok::code_completion)) {
119
2
    if (PP.getCodeCompletionHandler())
120
2
      PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
121
2
    PP.setCodeCompletionReached();
122
2
    PP.LexUnexpandedNonComment(PeekTok);
123
2
  }
124
125
  // If we don't have a pp-identifier now, this is an error.
126
1.85M
  if (PP.CheckMacroName(PeekTok, MU_Other))
127
4
    return true;
128
129
  // Otherwise, we got an identifier, is it defined to something?
130
1.85M
  IdentifierInfo *II = PeekTok.getIdentifierInfo();
131
1.85M
  MacroDefinition Macro = PP.getMacroDefinition(II);
132
1.85M
  Result.Val = !!Macro;
133
1.85M
  Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
134
1.85M
  DT.IncludedUndefinedIds = !Macro;
135
136
1.85M
  PP.emitMacroExpansionWarnings(PeekTok);
137
138
  // If there is a macro, mark it used.
139
1.85M
  if (Result.Val != 0 && 
ValueLive448k
)
140
408k
    PP.markMacroAsUsed(Macro.getMacroInfo());
141
142
  // Save macro token for callback.
143
1.85M
  Token macroToken(PeekTok);
144
145
  // If we are in parens, ensure we have a trailing ).
146
1.85M
  if (LParenLoc.isValid()) {
147
    // Consume identifier.
148
1.83M
    Result.setEnd(PeekTok.getLocation());
149
1.83M
    PP.LexUnexpandedNonComment(PeekTok);
150
151
1.83M
    if (PeekTok.isNot(tok::r_paren)) {
152
1
      PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
153
1
          << "'defined'" << tok::r_paren;
154
1
      PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
155
1
      return true;
156
1
    }
157
    // Consume the ).
158
1.83M
    PP.LexNonComment(PeekTok);
159
1.83M
    Result.setEnd(PeekTok.getLocation());
160
1.83M
  } else {
161
    // Consume identifier.
162
21.7k
    Result.setEnd(PeekTok.getLocation());
163
21.7k
    PP.LexNonComment(PeekTok);
164
21.7k
  }
165
166
  // [cpp.cond]p4:
167
  //   Prior to evaluation, macro invocations in the list of preprocessing
168
  //   tokens that will become the controlling constant expression are replaced
169
  //   (except for those macro names modified by the 'defined' unary operator),
170
  //   just as in normal text. If the token 'defined' is generated as a result
171
  //   of this replacement process or use of the 'defined' unary operator does
172
  //   not match one of the two specified forms prior to macro replacement, the
173
  //   behavior is undefined.
174
  // This isn't an idle threat, consider this program:
175
  //   #define FOO
176
  //   #define BAR defined(FOO)
177
  //   #if BAR
178
  //   ...
179
  //   #else
180
  //   ...
181
  //   #endif
182
  // clang and gcc will pick the #if branch while Visual Studio will take the
183
  // #else branch.  Emit a warning about this undefined behavior.
184
1.85M
  if (beginLoc.isMacroID()) {
185
3.02k
    bool IsFunctionTypeMacro =
186
3.02k
        PP.getSourceManager()
187
3.02k
            .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
188
3.02k
            .getExpansion()
189
3.02k
            .isFunctionMacroExpansion();
190
    // For object-type macros, it's easy to replace
191
    //   #define FOO defined(BAR)
192
    // with
193
    //   #if defined(BAR)
194
    //   #define FOO 1
195
    //   #else
196
    //   #define FOO 0
197
    //   #endif
198
    // and doing so makes sense since compilers handle this differently in
199
    // practice (see example further up).  But for function-type macros,
200
    // there is no good way to write
201
    //   # define FOO(x) (defined(M_ ## x) && M_ ## x)
202
    // in a different way, and compilers seem to agree on how to behave here.
203
    // So warn by default on object-type macros, but only warn in -pedantic
204
    // mode on function-type macros.
205
3.02k
    if (IsFunctionTypeMacro)
206
970
      PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
207
2.05k
    else
208
2.05k
      PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
209
3.02k
  }
210
211
  // Invoke the 'defined' callback.
212
1.85M
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
213
1.85M
    Callbacks->Defined(macroToken, Macro,
214
1.85M
                       SourceRange(beginLoc, PeekTok.getLocation()));
215
1.85M
  }
216
217
  // Success, remember that we saw defined(X).
218
1.85M
  DT.State = DefinedTracker::DefinedMacro;
219
1.85M
  DT.TheMacro = II;
220
1.85M
  return false;
221
1.85M
}
222
223
/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
224
/// return the computed value in Result.  Return true if there was an error
225
/// parsing.  This function also returns information about the form of the
226
/// expression in DT.  See above for information on what DT means.
227
///
228
/// If ValueLive is false, then this value is being evaluated in a context where
229
/// the result is not used.  As such, avoid diagnostics that relate to
230
/// evaluation.
231
static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
232
5.95M
                          bool ValueLive, Preprocessor &PP) {
233
5.95M
  DT.State = DefinedTracker::Unknown;
234
235
5.95M
  Result.setIdentifier(nullptr);
236
237
5.95M
  if (PeekTok.is(tok::code_completion)) {
238
8
    if (PP.getCodeCompletionHandler())
239
8
      PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
240
8
    PP.setCodeCompletionReached();
241
8
    PP.LexNonComment(PeekTok);
242
8
  }
243
244
5.95M
  switch (PeekTok.getKind()) {
245
2.06M
  default:
246
    // If this token's spelling is a pp-identifier, check to see if it is
247
    // 'defined' or if it is a macro.  Note that we check here because many
248
    // keywords are pp-identifiers, so we can't check the kind.
249
2.06M
    if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
250
      // Handle "defined X" and "defined(X)".
251
2.06M
      if (II->isStr("defined"))
252
1.85M
        return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
253
254
207k
      if (!II->isCPlusPlusOperatorKeyword()) {
255
        // If this identifier isn't 'defined' or one of the special
256
        // preprocessor keywords and it wasn't macro expanded, it turns
257
        // into a simple 0
258
207k
        if (ValueLive) {
259
29.0k
          PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
260
261
29.0k
          const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
262
          // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
263
29.0k
          if (DiagEngine.isIgnored(diag::warn_pp_undef_identifier,
264
29.0k
                                   PeekTok.getLocation())) {
265
28.9k
            const std::vector<std::string> UndefPrefixes =
266
28.9k
                DiagEngine.getDiagnosticOptions().UndefPrefixes;
267
28.9k
            const StringRef IdentifierName = II->getName();
268
28.9k
            if (llvm::any_of(UndefPrefixes,
269
28.9k
                             [&IdentifierName](const std::string &Prefix) {
270
25.7k
                               return IdentifierName.startswith(Prefix);
271
25.7k
                             }))
272
2.58k
              PP.Diag(PeekTok, diag::warn_pp_undef_prefix)
273
2.58k
                  << AddFlagValue{llvm::join(UndefPrefixes, ",")} << II;
274
28.9k
          }
275
29.0k
        }
276
207k
        Result.Val = 0;
277
207k
        Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
278
207k
        Result.setIdentifier(II);
279
207k
        Result.setRange(PeekTok.getLocation());
280
207k
        DT.IncludedUndefinedIds = true;
281
207k
        PP.LexNonComment(PeekTok);
282
207k
        return false;
283
207k
      }
284
207k
    }
285
34
    PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
286
34
    return true;
287
19
  case tok::eod:
288
19
  case tok::r_paren:
289
    // If there is no expression, report and exit.
290
19
    PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
291
19
    return true;
292
2.77M
  case tok::numeric_constant: {
293
2.77M
    SmallString<64> IntegerBuffer;
294
2.77M
    bool NumberInvalid = false;
295
2.77M
    StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
296
2.77M
                                              &NumberInvalid);
297
2.77M
    if (NumberInvalid)
298
0
      return true; // a diagnostic was already reported
299
300
2.77M
    NumericLiteralParser Literal(Spelling, PeekTok.getLocation(),
301
2.77M
                                 PP.getSourceManager(), PP.getLangOpts(),
302
2.77M
                                 PP.getTargetInfo(), PP.getDiagnostics());
303
2.77M
    if (Literal.hadError)
304
1
      return true; // a diagnostic was already reported.
305
306
2.77M
    if (Literal.isFloatingLiteral() || Literal.isImaginary) {
307
0
      PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
308
0
      return true;
309
0
    }
310
2.77M
    assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
311
312
    // Complain about, and drop, any ud-suffix.
313
2.77M
    if (Literal.hasUDSuffix())
314
1
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
315
316
    // 'long long' is a C99 or C++11 feature.
317
2.77M
    if (!PP.getLangOpts().C99 && 
Literal.isLongLong1.84M
) {
318
27
      if (PP.getLangOpts().CPlusPlus)
319
26
        PP.Diag(PeekTok,
320
26
             PP.getLangOpts().CPlusPlus11 ?
321
25
             diag::warn_cxx98_compat_longlong : 
diag::ext_cxx11_longlong1
);
322
1
      else
323
1
        PP.Diag(PeekTok, diag::ext_c99_longlong);
324
27
    }
325
326
    // 'z/uz' literals are a C++23 feature.
327
2.77M
    if (Literal.isSizeT)
328
4
      PP.Diag(PeekTok, PP.getLangOpts().CPlusPlus
329
4
                           ? PP.getLangOpts().CPlusPlus23
330
4
                                 ? diag::warn_cxx20_compat_size_t_suffix
331
4
                                 : 
diag::ext_cxx23_size_t_suffix0
332
4
                           : 
diag::err_cxx23_size_t_suffix0
);
333
334
    // 'wb/uwb' literals are a C23 feature. We explicitly do not support the
335
    // suffix in C++ as an extension because a library-based UDL that resolves
336
    // to a library type may be more appropriate there.
337
2.77M
    if (Literal.isBitInt)
338
12
      PP.Diag(PeekTok, PP.getLangOpts().C23
339
12
                           ? 
diag::warn_c23_compat_bitint_suffix11
340
12
                           : 
diag::ext_c23_bitint_suffix1
);
341
342
    // Parse the integer literal into Result.
343
2.77M
    if (Literal.GetIntegerValue(Result.Val)) {
344
      // Overflow parsing integer literal.
345
4
      if (ValueLive)
346
4
        PP.Diag(PeekTok, diag::err_integer_literal_too_large)
347
4
            << /* Unsigned */ 1;
348
4
      Result.Val.setIsUnsigned(true);
349
2.77M
    } else {
350
      // Set the signedness of the result to match whether there was a U suffix
351
      // or not.
352
2.77M
      Result.Val.setIsUnsigned(Literal.isUnsigned);
353
354
      // Detect overflow based on whether the value is signed.  If signed
355
      // and if the value is too large, emit a warning "integer constant is so
356
      // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
357
      // is 64-bits.
358
2.77M
      if (!Literal.isUnsigned && 
Result.Val.isNegative()2.76M
) {
359
        // Octal, hexadecimal, and binary literals are implicitly unsigned if
360
        // the value does not fit into a signed integer type.
361
19
        if (ValueLive && 
Literal.getRadix() == 1013
)
362
2
          PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
363
19
        Result.Val.setIsUnsigned(true);
364
19
      }
365
2.77M
    }
366
367
    // Consume the token.
368
2.77M
    Result.setRange(PeekTok.getLocation());
369
2.77M
    PP.LexNonComment(PeekTok);
370
2.77M
    return false;
371
2.77M
  }
372
12
  case tok::char_constant:          // 'x'
373
28
  case tok::wide_char_constant:     // L'x'
374
33
  case tok::utf8_char_constant:     // u8'x'
375
34
  case tok::utf16_char_constant:    // u'x'
376
34
  case tok::utf32_char_constant: {  // U'x'
377
    // Complain about, and drop, any ud-suffix.
378
34
    if (PeekTok.hasUDSuffix())
379
2
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
380
381
34
    SmallString<32> CharBuffer;
382
34
    bool CharInvalid = false;
383
34
    StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
384
34
    if (CharInvalid)
385
0
      return true;
386
387
34
    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
388
34
                              PeekTok.getLocation(), PP, PeekTok.getKind());
389
34
    if (Literal.hadError())
390
0
      return true;  // A diagnostic was already emitted.
391
392
    // Character literals are always int or wchar_t, expand to intmax_t.
393
34
    const TargetInfo &TI = PP.getTargetInfo();
394
34
    unsigned NumBits;
395
34
    if (Literal.isMultiChar())
396
2
      NumBits = TI.getIntWidth();
397
32
    else if (Literal.isWide())
398
16
      NumBits = TI.getWCharWidth();
399
16
    else if (Literal.isUTF16())
400
1
      NumBits = TI.getChar16Width();
401
15
    else if (Literal.isUTF32())
402
0
      NumBits = TI.getChar32Width();
403
15
    else // char or char8_t
404
15
      NumBits = TI.getCharWidth();
405
406
    // Set the width.
407
34
    llvm::APSInt Val(NumBits);
408
    // Set the value.
409
34
    Val = Literal.getValue();
410
    // Set the signedness. UTF-16 and UTF-32 are always unsigned
411
    // UTF-8 is unsigned if -fchar8_t is specified.
412
34
    if (Literal.isWide())
413
16
      Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
414
18
    else if (Literal.isUTF16() || 
Literal.isUTF32()17
)
415
1
      Val.setIsUnsigned(true);
416
17
    else if (Literal.isUTF8()) {
417
5
      if (PP.getLangOpts().CPlusPlus)
418
4
        Val.setIsUnsigned(
419
4
            PP.getLangOpts().Char8 ? 
true2
:
!PP.getLangOpts().CharIsSigned2
);
420
1
      else
421
1
        Val.setIsUnsigned(true);
422
5
    } else
423
12
      Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
424
425
34
    if (Result.Val.getBitWidth() > Val.getBitWidth()) {
426
34
      Result.Val = Val.extend(Result.Val.getBitWidth());
427
34
    } else {
428
0
      assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
429
0
             "intmax_t smaller than char/wchar_t?");
430
0
      Result.Val = Val;
431
0
    }
432
433
    // Consume the token.
434
34
    Result.setRange(PeekTok.getLocation());
435
34
    PP.LexNonComment(PeekTok);
436
34
    return false;
437
34
  }
438
231k
  case tok::l_paren: {
439
231k
    SourceLocation Start = PeekTok.getLocation();
440
231k
    PP.LexNonComment(PeekTok);  // Eat the (.
441
    // Parse the value and if there are any binary operators involved, parse
442
    // them.
443
231k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
444
445
    // If this is a silly value like (X), which doesn't need parens, check for
446
    // !(defined X).
447
231k
    if (PeekTok.is(tok::r_paren)) {
448
      // Just use DT unmodified as our result.
449
226k
    } else {
450
      // Otherwise, we have something like (x+y), and we consumed '(x'.
451
226k
      if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
452
226k
                                   DT.IncludedUndefinedIds, PP))
453
0
        return true;
454
455
226k
      if (PeekTok.isNot(tok::r_paren)) {
456
0
        PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
457
0
          << Result.getRange();
458
0
        PP.Diag(Start, diag::note_matching) << tok::l_paren;
459
0
        return true;
460
0
      }
461
226k
      DT.State = DefinedTracker::Unknown;
462
226k
    }
463
231k
    Result.setRange(Start, PeekTok.getLocation());
464
231k
    Result.setIdentifier(nullptr);
465
231k
    PP.LexNonComment(PeekTok);  // Eat the ).
466
231k
    return false;
467
231k
  }
468
0
  case tok::plus: {
469
0
    SourceLocation Start = PeekTok.getLocation();
470
    // Unary plus doesn't modify the value.
471
0
    PP.LexNonComment(PeekTok);
472
0
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
473
0
    Result.setBegin(Start);
474
0
    Result.setIdentifier(nullptr);
475
0
    return false;
476
0
  }
477
170
  case tok::minus: {
478
170
    SourceLocation Loc = PeekTok.getLocation();
479
170
    PP.LexNonComment(PeekTok);
480
170
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
481
170
    Result.setBegin(Loc);
482
170
    Result.setIdentifier(nullptr);
483
484
    // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
485
170
    Result.Val = -Result.Val;
486
487
    // -MININT is the only thing that overflows.  Unsigned never overflows.
488
170
    bool Overflow = !Result.isUnsigned() && 
Result.Val.isMinSignedValue()166
;
489
490
    // If this operator is live and overflowed, report the issue.
491
170
    if (Overflow && 
ValueLive0
)
492
0
      PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
493
494
170
    DT.State = DefinedTracker::Unknown;
495
170
    return false;
496
170
  }
497
498
7
  case tok::tilde: {
499
7
    SourceLocation Start = PeekTok.getLocation();
500
7
    PP.LexNonComment(PeekTok);
501
7
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
502
7
    Result.setBegin(Start);
503
7
    Result.setIdentifier(nullptr);
504
505
    // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
506
7
    Result.Val = ~Result.Val;
507
7
    DT.State = DefinedTracker::Unknown;
508
7
    return false;
509
7
  }
510
511
884k
  case tok::exclaim: {
512
884k
    SourceLocation Start = PeekTok.getLocation();
513
884k
    PP.LexNonComment(PeekTok);
514
884k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
515
884k
    Result.setBegin(Start);
516
884k
    Result.Val = !Result.Val;
517
    // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
518
884k
    Result.Val.setIsUnsigned(false);
519
884k
    Result.setIdentifier(nullptr);
520
521
884k
    if (DT.State == DefinedTracker::DefinedMacro)
522
762k
      DT.State = DefinedTracker::NotDefinedMacro;
523
122k
    else if (DT.State == DefinedTracker::NotDefinedMacro)
524
0
      DT.State = DefinedTracker::DefinedMacro;
525
884k
    return false;
526
884k
  }
527
3
  case tok::kw_true:
528
6
  case tok::kw_false:
529
6
    Result.Val = PeekTok.getKind() == tok::kw_true;
530
6
    Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
531
6
    Result.setIdentifier(PeekTok.getIdentifierInfo());
532
6
    Result.setRange(PeekTok.getLocation());
533
6
    PP.LexNonComment(PeekTok);
534
6
    return false;
535
536
  // FIXME: Handle #assert
537
5.95M
  }
538
5.95M
}
539
540
/// getPrecedence - Return the precedence of the specified binary operator
541
/// token.  This returns:
542
///   ~0 - Invalid token.
543
///   14 -> 3 - various operators.
544
///    0 - 'eod' or ')'
545
5.26M
static unsigned getPrecedence(tok::TokenKind Kind) {
546
5.26M
  switch (Kind) {
547
15
  default: return ~0U;
548
0
  case tok::percent:
549
19
  case tok::slash:
550
2.32k
  case tok::star:                 return 14;
551
2.31k
  case tok::plus:
552
11.0k
  case tok::minus:                return 13;
553
16.8k
  case tok::lessless:
554
16.9k
  case tok::greatergreater:       return 12;
555
86.7k
  case tok::lessequal:
556
162k
  case tok::less:
557
1.13M
  case tok::greaterequal:
558
1.16M
  case tok::greater:              return 11;
559
15.2k
  case tok::exclaimequal:
560
81.9k
  case tok::equalequal:           return 10;
561
628
  case tok::amp:                  return 9;
562
10
  case tok::caret:                return 8;
563
10
  case tok::pipe:                 return 7;
564
609k
  case tok::ampamp:               return 6;
565
501k
  case tok::pipepipe:             return 5;
566
1.15M
  case tok::question:             return 4;
567
1.78k
  case tok::comma:                return 3;
568
1.78k
  case tok::colon:                return 2;
569
269k
  case tok::r_paren:              return 0;// Lowest priority, end of expr.
570
1.44M
  case tok::eod:                  return 0;// Lowest priority, end of directive.
571
5.26M
  }
572
5.26M
}
573
574
static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
575
15
                                       Token &Tok) {
576
15
  if (Tok.is(tok::l_paren) && 
LHS.getIdentifier()6
)
577
5
    PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
578
5
        << LHS.getIdentifier();
579
10
  else
580
10
    PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
581
10
        << LHS.getRange();
582
15
}
583
584
/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
585
/// PeekTok, and whose precedence is PeekPrec.  This returns the result in LHS.
586
///
587
/// If ValueLive is false, then this value is being evaluated in a context where
588
/// the result is not used.  As such, avoid diagnostics that relate to
589
/// evaluation, such as division by zero warnings.
590
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
591
                                     Token &PeekTok, bool ValueLive,
592
                                     bool &IncludedUndefinedIds,
593
1.72M
                                     Preprocessor &PP) {
594
1.72M
  unsigned PeekPrec = getPrecedence(PeekTok.getKind());
595
  // If this token isn't valid, report the error.
596
1.72M
  if (PeekPrec == ~0U) {
597
14
    diagnoseUnexpectedOperator(PP, LHS, PeekTok);
598
14
    return true;
599
14
  }
600
601
3.76M
  
while (1.72M
true) {
602
    // If this token has a lower precedence than we are allowed to parse, return
603
    // it so that higher levels of the recursion can parse it.
604
3.76M
    if (PeekPrec < MinPrec)
605
1.72M
      return false;
606
607
2.04M
    tok::TokenKind Operator = PeekTok.getKind();
608
609
    // If this is a short-circuiting operator, see if the RHS of the operator is
610
    // dead.  Note that this cannot just clobber ValueLive.  Consider
611
    // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)".  In
612
    // this example, the RHS of the && being dead does not make the rest of the
613
    // expr dead.
614
2.04M
    bool RHSIsLive;
615
2.04M
    if (Operator == tok::ampamp && 
LHS.Val == 0601k
)
616
274k
      RHSIsLive = false;   // RHS of "0 && x" is dead.
617
1.76M
    else if (Operator == tok::pipepipe && 
LHS.Val != 0498k
)
618
303k
      RHSIsLive = false;   // RHS of "1 || x" is dead.
619
1.46M
    else if (Operator == tok::question && 
LHS.Val == 01.77k
)
620
1.40k
      RHSIsLive = false;   // RHS (x) of "0 ? x : y" is dead.
621
1.46M
    else
622
1.46M
      RHSIsLive = ValueLive;
623
624
    // Consume the operator, remembering the operator's location for reporting.
625
2.04M
    SourceLocation OpLoc = PeekTok.getLocation();
626
2.04M
    PP.LexNonComment(PeekTok);
627
628
2.04M
    PPValue RHS(LHS.getBitWidth());
629
    // Parse the RHS of the operator.
630
2.04M
    DefinedTracker DT;
631
2.04M
    if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) 
return true1
;
632
2.04M
    IncludedUndefinedIds = DT.IncludedUndefinedIds;
633
634
    // Remember the precedence of this operator and get the precedence of the
635
    // operator immediately to the right of the RHS.
636
2.04M
    unsigned ThisPrec = PeekPrec;
637
2.04M
    PeekPrec = getPrecedence(PeekTok.getKind());
638
639
    // If this token isn't valid, report the error.
640
2.04M
    if (PeekPrec == ~0U) {
641
1
      diagnoseUnexpectedOperator(PP, RHS, PeekTok);
642
1
      return true;
643
1
    }
644
645
    // Decide whether to include the next binop in this subexpression.  For
646
    // example, when parsing x+y*z and looking at '*', we want to recursively
647
    // handle y*z as a single subexpression.  We do this because the precedence
648
    // of * is higher than that of +.  The only strange case we have to handle
649
    // here is for the ?: operator, where the precedence is actually lower than
650
    // the LHS of the '?'.  The grammar rule is:
651
    //
652
    // conditional-expression ::=
653
    //    logical-OR-expression ? expression : conditional-expression
654
    // where 'expression' is actually comma-expression.
655
2.04M
    unsigned RHSPrec;
656
2.04M
    if (Operator == tok::question)
657
      // The RHS of "?" should be maximally consumed as an expression.
658
1.77k
      RHSPrec = getPrecedence(tok::comma);
659
2.03M
    else  // All others should munch while higher precedence.
660
2.03M
      RHSPrec = ThisPrec+1;
661
662
2.04M
    if (PeekPrec >= RHSPrec) {
663
341k
      if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
664
341k
                                   IncludedUndefinedIds, PP))
665
0
        return true;
666
341k
      PeekPrec = getPrecedence(PeekTok.getKind());
667
341k
    }
668
2.04M
    assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
669
670
    // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
671
    // either operand is unsigned.
672
2.04M
    llvm::APSInt Res(LHS.getBitWidth());
673
2.04M
    switch (Operator) {
674
1.77k
    case tok::question:       // No UAC for x and y in "x ? y : z".
675
18.6k
    case tok::lessless:       // Shift amount doesn't UAC with shift value.
676
18.6k
    case tok::greatergreater: // Shift amount doesn't UAC with shift value.
677
18.6k
    case tok::comma:          // Comma operands are not subject to UACs.
678
517k
    case tok::pipepipe:       // Logical || does not do UACs.
679
1.11M
    case tok::ampamp:         // Logical && does not do UACs.
680
1.11M
      break;                  // No UAC
681
921k
    default:
682
921k
      Res.setIsUnsigned(LHS.isUnsigned() || 
RHS.isUnsigned()918k
);
683
      // If this just promoted something from signed to unsigned, and if the
684
      // value was negative, warn about it.
685
921k
      if (ValueLive && 
Res.isUnsigned()712k
) {
686
4.77k
        if (!LHS.isUnsigned() && 
LHS.Val.isNegative()2.16k
)
687
2
          PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
688
2
            << toString(LHS.Val, 10, true) + " to " +
689
2
               toString(LHS.Val, 10, false)
690
2
            << LHS.getRange() << RHS.getRange();
691
4.77k
        if (!RHS.isUnsigned() && 
RHS.Val.isNegative()2.55k
)
692
5
          PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
693
5
            << toString(RHS.Val, 10, true) + " to " +
694
5
               toString(RHS.Val, 10, false)
695
5
            << LHS.getRange() << RHS.getRange();
696
4.77k
      }
697
921k
      LHS.Val.setIsUnsigned(Res.isUnsigned());
698
921k
      RHS.Val.setIsUnsigned(Res.isUnsigned());
699
2.04M
    }
700
701
2.04M
    bool Overflow = false;
702
2.04M
    switch (Operator) {
703
0
    default: llvm_unreachable("Unknown operator token!");
704
0
    case tok::percent:
705
0
      if (RHS.Val != 0)
706
0
        Res = LHS.Val % RHS.Val;
707
0
      else if (ValueLive) {
708
0
        PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
709
0
          << LHS.getRange() << RHS.getRange();
710
0
        return true;
711
0
      }
712
0
      break;
713
14
    case tok::slash:
714
14
      if (RHS.Val != 0) {
715
1
        if (LHS.Val.isSigned())
716
0
          Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
717
1
        else
718
1
          Res = LHS.Val / RHS.Val;
719
13
      } else if (ValueLive) {
720
7
        PP.Diag(OpLoc, diag::err_pp_division_by_zero)
721
7
          << LHS.getRange() << RHS.getRange();
722
7
        return true;
723
7
      }
724
7
      break;
725
726
2.30k
    case tok::star:
727
2.30k
      if (Res.isSigned())
728
2.27k
        Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
729
24
      else
730
24
        Res = LHS.Val * RHS.Val;
731
2.30k
      break;
732
16.8k
    case tok::lessless: {
733
      // Determine whether overflow is about to happen.
734
16.8k
      if (LHS.isUnsigned())
735
0
        Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
736
16.8k
      else
737
16.8k
        Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
738
16.8k
      break;
739
14
    }
740
10
    case tok::greatergreater: {
741
      // Determine whether overflow is about to happen.
742
10
      unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
743
10
      if (ShAmt >= LHS.getBitWidth()) {
744
0
        Overflow = true;
745
0
        ShAmt = LHS.getBitWidth()-1;
746
0
      }
747
10
      Res = LHS.Val >> ShAmt;
748
10
      break;
749
14
    }
750
2.31k
    case tok::plus:
751
2.31k
      if (LHS.isUnsigned())
752
29
        Res = LHS.Val + RHS.Val;
753
2.28k
      else
754
2.28k
        Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
755
2.31k
      break;
756
8.72k
    case tok::minus:
757
8.72k
      if (LHS.isUnsigned())
758
14
        Res = LHS.Val - RHS.Val;
759
8.71k
      else
760
8.71k
        Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
761
8.72k
      break;
762
69.1k
    case tok::lessequal:
763
69.1k
      Res = LHS.Val <= RHS.Val;
764
69.1k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
765
69.1k
      break;
766
48.1k
    case tok::less:
767
48.1k
      Res = LHS.Val < RHS.Val;
768
48.1k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
769
48.1k
      break;
770
705k
    case tok::greaterequal:
771
705k
      Res = LHS.Val >= RHS.Val;
772
705k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
773
705k
      break;
774
20.7k
    case tok::greater:
775
20.7k
      Res = LHS.Val > RHS.Val;
776
20.7k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
777
20.7k
      break;
778
9.10k
    case tok::exclaimequal:
779
9.10k
      Res = LHS.Val != RHS.Val;
780
9.10k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
781
9.10k
      break;
782
54.4k
    case tok::equalequal:
783
54.4k
      Res = LHS.Val == RHS.Val;
784
54.4k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
785
54.4k
      break;
786
628
    case tok::amp:
787
628
      Res = LHS.Val & RHS.Val;
788
628
      break;
789
10
    case tok::caret:
790
10
      Res = LHS.Val ^ RHS.Val;
791
10
      break;
792
10
    case tok::pipe:
793
10
      Res = LHS.Val | RHS.Val;
794
10
      break;
795
601k
    case tok::ampamp:
796
601k
      Res = (LHS.Val != 0 && 
RHS.Val != 0327k
);
797
601k
      Res.setIsUnsigned(false);  // C99 6.5.13p3, result is always int (signed)
798
601k
      break;
799
498k
    case tok::pipepipe:
800
498k
      Res = (LHS.Val != 0 || 
RHS.Val != 0195k
);
801
498k
      Res.setIsUnsigned(false);  // C99 6.5.14p3, result is always int (signed)
802
498k
      break;
803
2
    case tok::comma:
804
      // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
805
      // if not being evaluated.
806
2
      if (!PP.getLangOpts().C99 || 
ValueLive1
)
807
1
        PP.Diag(OpLoc, diag::ext_pp_comma_expr)
808
1
          << LHS.getRange() << RHS.getRange();
809
2
      Res = RHS.Val; // LHS = LHS,RHS -> RHS.
810
2
      break;
811
1.77k
    case tok::question: {
812
      // Parse the : part of the expression.
813
1.77k
      if (PeekTok.isNot(tok::colon)) {
814
0
        PP.Diag(PeekTok.getLocation(), diag::err_expected)
815
0
            << tok::colon << LHS.getRange() << RHS.getRange();
816
0
        PP.Diag(OpLoc, diag::note_matching) << tok::question;
817
0
        return true;
818
0
      }
819
      // Consume the :.
820
1.77k
      PP.LexNonComment(PeekTok);
821
822
      // Evaluate the value after the :.
823
1.77k
      bool AfterColonLive = ValueLive && 
LHS.Val == 01.70k
;
824
1.77k
      PPValue AfterColonVal(LHS.getBitWidth());
825
1.77k
      DefinedTracker DT;
826
1.77k
      if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
827
0
        return true;
828
829
      // Parse anything after the : with the same precedence as ?.  We allow
830
      // things of equal precedence because ?: is right associative.
831
1.77k
      if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
832
1.77k
                                   PeekTok, AfterColonLive,
833
1.77k
                                   IncludedUndefinedIds, PP))
834
1
        return true;
835
836
      // Now that we have the condition, the LHS and the RHS of the :, evaluate.
837
1.77k
      Res = LHS.Val != 0 ? 
RHS.Val370
:
AfterColonVal.Val1.40k
;
838
1.77k
      RHS.setEnd(AfterColonVal.getRange().getEnd());
839
840
      // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
841
      // either operand is unsigned.
842
1.77k
      Res.setIsUnsigned(RHS.isUnsigned() || AfterColonVal.isUnsigned());
843
844
      // Figure out the precedence of the token after the : part.
845
1.77k
      PeekPrec = getPrecedence(PeekTok.getKind());
846
1.77k
      break;
847
1.77k
    }
848
0
    case tok::colon:
849
      // Don't allow :'s to float around without being part of ?: exprs.
850
0
      PP.Diag(OpLoc, diag::err_pp_colon_without_question)
851
0
        << LHS.getRange() << RHS.getRange();
852
0
      return true;
853
2.04M
    }
854
855
    // If this operator is live and overflowed, report the issue.
856
2.04M
    if (Overflow && 
ValueLive5
)
857
5
      PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
858
5
        << LHS.getRange() << RHS.getRange();
859
860
    // Put the result back into 'LHS' for our next iteration.
861
2.04M
    LHS.Val = Res;
862
2.04M
    LHS.setEnd(RHS.getRange().getEnd());
863
2.04M
    RHS.setIdentifier(nullptr);
864
2.04M
  }
865
1.72M
}
866
867
/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
868
/// may occur after a #if or #elif directive.  If the expression is equivalent
869
/// to "!defined(X)" return X in IfNDefMacro.
870
Preprocessor::DirectiveEvalResult
871
2.79M
Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
872
2.79M
  SaveAndRestore PPDir(ParsingIfOrElifDirective, true);
873
  // Save the current state of 'DisableMacroExpansion' and reset it to false. If
874
  // 'DisableMacroExpansion' is true, then we must be in a macro argument list
875
  // in which case a directive is undefined behavior.  We want macros to be able
876
  // to recursively expand in order to get more gcc-list behavior, so we force
877
  // DisableMacroExpansion to false and restore it when we're done parsing the
878
  // expression.
879
2.79M
  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
880
2.79M
  DisableMacroExpansion = false;
881
882
  // Peek ahead one token.
883
2.79M
  Token Tok;
884
2.79M
  LexNonComment(Tok);
885
886
  // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
887
2.79M
  unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
888
889
2.79M
  PPValue ResVal(BitWidth);
890
2.79M
  DefinedTracker DT;
891
2.79M
  SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
892
2.79M
  if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
893
    // Parse error, skip the rest of the macro line.
894
58
    SourceRange ConditionRange = ExprStartLoc;
895
58
    if (Tok.isNot(tok::eod))
896
36
      ConditionRange = DiscardUntilEndOfDirective();
897
898
    // Restore 'DisableMacroExpansion'.
899
58
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
900
901
    // We cannot trust the source range from the value because there was a
902
    // parse error. Track the range manually -- the end of the directive is the
903
    // end of the condition range.
904
58
    return {false,
905
58
            DT.IncludedUndefinedIds,
906
58
            {ExprStartLoc, ConditionRange.getEnd()}};
907
58
  }
908
909
  // If we are at the end of the expression after just parsing a value, there
910
  // must be no (unparenthesized) binary operators involved, so we can exit
911
  // directly.
912
2.79M
  if (Tok.is(tok::eod)) {
913
    // If the expression we parsed was of the form !defined(macro), return the
914
    // macro in IfNDefMacro.
915
1.63M
    if (DT.State == DefinedTracker::NotDefinedMacro)
916
440k
      IfNDefMacro = DT.TheMacro;
917
918
    // Restore 'DisableMacroExpansion'.
919
1.63M
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
920
1.63M
    return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
921
1.63M
  }
922
923
  // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
924
  // operator and the stuff after it.
925
1.15M
  if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
926
1.15M
                               Tok, true, DT.IncludedUndefinedIds, *this)) {
927
    // Parse error, skip the rest of the macro line.
928
23
    if (Tok.isNot(tok::eod))
929
16
      DiscardUntilEndOfDirective();
930
931
    // Restore 'DisableMacroExpansion'.
932
23
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
933
23
    return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
934
23
  }
935
936
  // If we aren't at the tok::eod token, something bad happened, like an extra
937
  // ')' token.
938
1.15M
  if (Tok.isNot(tok::eod)) {
939
2
    Diag(Tok, diag::err_pp_expected_eol);
940
2
    DiscardUntilEndOfDirective();
941
2
  }
942
943
  // Restore 'DisableMacroExpansion'.
944
1.15M
  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
945
1.15M
  return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
946
1.15M
}