Coverage Report

Created: 2020-02-25 14:32

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