Coverage Report

Created: 2021-01-19 06:58

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