Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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.25M
  PPValue(unsigned BitWidth) : Val(BitWidth) {}
52
53
  // If this value was produced by directly evaluating an identifier, produce
54
  // that identifier.
55
7
  IdentifierInfo *getIdentifier() const { return II; }
56
10.7M
  void setIdentifier(IdentifierInfo *II) { this->II = II; }
57
58
4.67M
  unsigned getBitWidth() const { return Val.getBitWidth(); }
59
1.79M
  bool isUnsigned() const { return Val.isUnsigned(); }
60
61
6.25M
  SourceRange getRange() const { return Range; }
62
63
4.32M
  void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
64
258k
  void setRange(SourceLocation B, SourceLocation E) {
65
258k
    Range.setBegin(B); Range.setEnd(E);
66
258k
  }
67
2.64M
  void setBegin(SourceLocation L) { Range.setBegin(L); }
68
6.17M
  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.93M
                            bool ValueLive, Preprocessor &PP) {
102
1.93M
  SourceLocation beginLoc(PeekTok.getLocation());
103
1.93M
  Result.setBegin(beginLoc);
104
1.93M
105
1.93M
  // Get the next token, don't expand it.
106
1.93M
  PP.LexUnexpandedNonComment(PeekTok);
107
1.93M
108
1.93M
  // Two options, it can either be a pp-identifier or a (.
109
1.93M
  SourceLocation LParenLoc;
110
1.93M
  if (PeekTok.is(tok::l_paren)) {
111
1.90M
    // Found a paren, remember we saw it and skip it.
112
1.90M
    LParenLoc = PeekTok.getLocation();
113
1.90M
    PP.LexUnexpandedNonComment(PeekTok);
114
1.90M
  }
115
1.93M
116
1.93M
  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.93M
123
1.93M
  // If we don't have a pp-identifier now, this is an error.
124
1.93M
  if (PP.CheckMacroName(PeekTok, MU_Other))
125
4
    return true;
126
1.93M
127
1.93M
  // Otherwise, we got an identifier, is it defined to something?
128
1.93M
  IdentifierInfo *II = PeekTok.getIdentifierInfo();
129
1.93M
  MacroDefinition Macro = PP.getMacroDefinition(II);
130
1.93M
  Result.Val = !!Macro;
131
1.93M
  Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
132
1.93M
  DT.IncludedUndefinedIds = !Macro;
133
1.93M
134
1.93M
  // If there is a macro, mark it used.
135
1.93M
  if (Result.Val != 0 && 
ValueLive593k
)
136
468k
    PP.markMacroAsUsed(Macro.getMacroInfo());
137
1.93M
138
1.93M
  // Save macro token for callback.
139
1.93M
  Token macroToken(PeekTok);
140
1.93M
141
1.93M
  // If we are in parens, ensure we have a trailing ).
142
1.93M
  if (LParenLoc.isValid()) {
143
1.90M
    // Consume identifier.
144
1.90M
    Result.setEnd(PeekTok.getLocation());
145
1.90M
    PP.LexUnexpandedNonComment(PeekTok);
146
1.90M
147
1.90M
    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.90M
    // Consume the ).
154
1.90M
    PP.LexNonComment(PeekTok);
155
1.90M
    Result.setEnd(PeekTok.getLocation());
156
1.90M
  } else {
157
27.4k
    // Consume identifier.
158
27.4k
    Result.setEnd(PeekTok.getLocation());
159
27.4k
    PP.LexNonComment(PeekTok);
160
27.4k
  }
161
1.93M
162
1.93M
  // [cpp.cond]p4:
163
1.93M
  //   Prior to evaluation, macro invocations in the list of preprocessing
164
1.93M
  //   tokens that will become the controlling constant expression are replaced
165
1.93M
  //   (except for those macro names modified by the 'defined' unary operator),
166
1.93M
  //   just as in normal text. If the token 'defined' is generated as a result
167
1.93M
  //   of this replacement process or use of the 'defined' unary operator does
168
1.93M
  //   not match one of the two specified forms prior to macro replacement, the
169
1.93M
  //   behavior is undefined.
170
1.93M
  // This isn't an idle threat, consider this program:
171
1.93M
  //   #define FOO
172
1.93M
  //   #define BAR defined(FOO)
173
1.93M
  //   #if BAR
174
1.93M
  //   ...
175
1.93M
  //   #else
176
1.93M
  //   ...
177
1.93M
  //   #endif
178
1.93M
  // clang and gcc will pick the #if branch while Visual Studio will take the
179
1.93M
  // #else branch.  Emit a warning about this undefined behavior.
180
1.93M
  
if (1.93M
beginLoc.isMacroID()1.93M
) {
181
2.85k
    bool IsFunctionTypeMacro =
182
2.85k
        PP.getSourceManager()
183
2.85k
            .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
184
2.85k
            .getExpansion()
185
2.85k
            .isFunctionMacroExpansion();
186
2.85k
    // For object-type macros, it's easy to replace
187
2.85k
    //   #define FOO defined(BAR)
188
2.85k
    // with
189
2.85k
    //   #if defined(BAR)
190
2.85k
    //   #define FOO 1
191
2.85k
    //   #else
192
2.85k
    //   #define FOO 0
193
2.85k
    //   #endif
194
2.85k
    // and doing so makes sense since compilers handle this differently in
195
2.85k
    // practice (see example further up).  But for function-type macros,
196
2.85k
    // there is no good way to write
197
2.85k
    //   # define FOO(x) (defined(M_ ## x) && M_ ## x)
198
2.85k
    // in a different way, and compilers seem to agree on how to behave here.
199
2.85k
    // So warn by default on object-type macros, but only warn in -pedantic
200
2.85k
    // mode on function-type macros.
201
2.85k
    if (IsFunctionTypeMacro)
202
673
      PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
203
2.18k
    else
204
2.18k
      PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
205
2.85k
  }
206
1.93M
207
1.93M
  // Invoke the 'defined' callback.
208
1.93M
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
209
1.93M
    Callbacks->Defined(macroToken, Macro,
210
1.93M
                       SourceRange(beginLoc, PeekTok.getLocation()));
211
1.93M
  }
212
1.93M
213
1.93M
  // Success, remember that we saw defined(X).
214
1.93M
  DT.State = DefinedTracker::DefinedMacro;
215
1.93M
  DT.TheMacro = II;
216
1.93M
  return false;
217
1.93M
}
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.22M
                          bool ValueLive, Preprocessor &PP) {
229
7.22M
  DT.State = DefinedTracker::Unknown;
230
7.22M
231
7.22M
  Result.setIdentifier(nullptr);
232
7.22M
233
7.22M
  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.22M
240
7.22M
  switch (PeekTok.getKind()) {
241
7.22M
  default:
242
2.18M
    // If this token's spelling is a pp-identifier, check to see if it is
243
2.18M
    // 'defined' or if it is a macro.  Note that we check here because many
244
2.18M
    // keywords are pp-identifiers, so we can't check the kind.
245
2.18M
    if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
246
2.18M
      // Handle "defined X" and "defined(X)".
247
2.18M
      if (II->isStr("defined"))
248
1.93M
        return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
249
253k
250
253k
      if (!II->isCPlusPlusOperatorKeyword()) {
251
253k
        // If this identifier isn't 'defined' or one of the special
252
253k
        // preprocessor keywords and it wasn't macro expanded, it turns
253
253k
        // into a simple 0
254
253k
        if (ValueLive)
255
52.8k
          PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
256
253k
        Result.Val = 0;
257
253k
        Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
258
253k
        Result.setIdentifier(II);
259
253k
        Result.setRange(PeekTok.getLocation());
260
253k
        DT.IncludedUndefinedIds = true;
261
253k
        PP.LexNonComment(PeekTok);
262
253k
        return false;
263
253k
      }
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.06M
  case tok::numeric_constant: {
273
4.06M
    SmallString<64> IntegerBuffer;
274
4.06M
    bool NumberInvalid = false;
275
4.06M
    StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
276
4.06M
                                              &NumberInvalid);
277
4.06M
    if (NumberInvalid)
278
0
      return true; // a diagnostic was already reported
279
4.06M
280
4.06M
    NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
281
4.06M
    if (Literal.hadError)
282
0
      return true; // a diagnostic was already reported.
283
4.06M
284
4.06M
    if (Literal.isFloatingLiteral() || Literal.isImaginary) {
285
0
      PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
286
0
      return true;
287
0
    }
288
4.06M
    assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
289
4.06M
290
4.06M
    // Complain about, and drop, any ud-suffix.
291
4.06M
    if (Literal.hasUDSuffix())
292
1
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
293
4.06M
294
4.06M
    // 'long long' is a C99 or C++11 feature.
295
4.06M
    if (!PP.getLangOpts().C99 && 
Literal.isLongLong1.93M
) {
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.06M
304
4.06M
    // Parse the integer literal into Result.
305
4.06M
    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.06M
    } else {
312
4.06M
      // Set the signedness of the result to match whether there was a U suffix
313
4.06M
      // or not.
314
4.06M
      Result.Val.setIsUnsigned(Literal.isUnsigned);
315
4.06M
316
4.06M
      // Detect overflow based on whether the value is signed.  If signed
317
4.06M
      // and if the value is too large, emit a warning "integer constant is so
318
4.06M
      // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
319
4.06M
      // is 64-bits.
320
4.06M
      if (!Literal.isUnsigned && 
Result.Val.isNegative()4.06M
) {
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.06M
    }
328
4.06M
329
4.06M
    // Consume the token.
330
4.06M
    Result.setRange(PeekTok.getLocation());
331
4.06M
    PP.LexNonComment(PeekTok);
332
4.06M
    return false;
333
4.06M
  }
334
4.06M
  case tok::char_constant:          // 'x'
335
211
  case tok::wide_char_constant:     // L'x'
336
211
  case tok::utf8_char_constant:     // u8'x'
337
211
  case tok::utf16_char_constant:    // u'x'
338
211
  case tok::utf32_char_constant: {  // U'x'
339
211
    // Complain about, and drop, any ud-suffix.
340
211
    if (PeekTok.hasUDSuffix())
341
2
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
342
211
343
211
    SmallString<32> CharBuffer;
344
211
    bool CharInvalid = false;
345
211
    StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
346
211
    if (CharInvalid)
347
0
      return true;
348
211
349
211
    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
350
211
                              PeekTok.getLocation(), PP, PeekTok.getKind());
351
211
    if (Literal.hadError())
352
0
      return true;  // A diagnostic was already emitted.
353
211
354
211
    // Character literals are always int or wchar_t, expand to intmax_t.
355
211
    const TargetInfo &TI = PP.getTargetInfo();
356
211
    unsigned NumBits;
357
211
    if (Literal.isMultiChar())
358
3
      NumBits = TI.getIntWidth();
359
208
    else if (Literal.isWide())
360
4
      NumBits = TI.getWCharWidth();
361
204
    else if (Literal.isUTF16())
362
1
      NumBits = TI.getChar16Width();
363
203
    else if (Literal.isUTF32())
364
0
      NumBits = TI.getChar32Width();
365
203
    else // char or char8_t
366
203
      NumBits = TI.getCharWidth();
367
211
368
211
    // Set the width.
369
211
    llvm::APSInt Val(NumBits);
370
211
    // Set the value.
371
211
    Val = Literal.getValue();
372
211
    // Set the signedness. UTF-16 and UTF-32 are always unsigned
373
211
    if (Literal.isWide())
374
4
      Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
375
207
    else if (!Literal.isUTF16() && 
!Literal.isUTF32()206
)
376
206
      Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
377
211
378
211
    if (Result.Val.getBitWidth() > Val.getBitWidth()) {
379
211
      Result.Val = Val.extend(Result.Val.getBitWidth());
380
211
    } 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
211
386
211
    // Consume the token.
387
211
    Result.setRange(PeekTok.getLocation());
388
211
    PP.LexNonComment(PeekTok);
389
211
    return false;
390
211
  }
391
258k
  case tok::l_paren: {
392
258k
    SourceLocation Start = PeekTok.getLocation();
393
258k
    PP.LexNonComment(PeekTok);  // Eat the (.
394
258k
    // Parse the value and if there are any binary operators involved, parse
395
258k
    // them.
396
258k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
397
258k
398
258k
    // If this is a silly value like (X), which doesn't need parens, check for
399
258k
    // !(defined X).
400
258k
    if (PeekTok.is(tok::r_paren)) {
401
17.9k
      // Just use DT unmodified as our result.
402
240k
    } else {
403
240k
      // Otherwise, we have something like (x+y), and we consumed '(x'.
404
240k
      if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
405
240k
                                   DT.IncludedUndefinedIds, PP))
406
0
        return true;
407
240k
408
240k
      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
240k
      DT.State = DefinedTracker::Unknown;
415
240k
    }
416
258k
    Result.setRange(Start, PeekTok.getLocation());
417
258k
    Result.setIdentifier(nullptr);
418
258k
    PP.LexNonComment(PeekTok);  // Eat the ).
419
258k
    return false;
420
258k
  }
421
258k
  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
282
  case tok::minus: {
431
282
    SourceLocation Loc = PeekTok.getLocation();
432
282
    PP.LexNonComment(PeekTok);
433
282
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
434
282
    Result.setBegin(Loc);
435
282
    Result.setIdentifier(nullptr);
436
282
437
282
    // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
438
282
    Result.Val = -Result.Val;
439
282
440
282
    // -MININT is the only thing that overflows.  Unsigned never overflows.
441
282
    bool Overflow = !Result.isUnsigned() && 
Result.Val.isMinSignedValue()280
;
442
282
443
282
    // If this operator is live and overflowed, report the issue.
444
282
    if (Overflow && 
ValueLive0
)
445
0
      PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
446
282
447
282
    DT.State = DefinedTracker::Unknown;
448
282
    return false;
449
282
  }
450
282
451
282
  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
713k
  case tok::exclaim: {
465
713k
    SourceLocation Start = PeekTok.getLocation();
466
713k
    PP.LexNonComment(PeekTok);
467
713k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
468
713k
    Result.setBegin(Start);
469
713k
    Result.Val = !Result.Val;
470
713k
    // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
471
713k
    Result.Val.setIsUnsigned(false);
472
713k
    Result.setIdentifier(nullptr);
473
713k
474
713k
    if (DT.State == DefinedTracker::DefinedMacro)
475
577k
      DT.State = DefinedTracker::NotDefinedMacro;
476
136k
    else if (DT.State == DefinedTracker::NotDefinedMacro)
477
0
      DT.State = DefinedTracker::DefinedMacro;
478
713k
    return false;
479
713k
  }
480
713k
  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.22M
489
7.22M
  // FIXME: Handle #assert
490
7.22M
  }
491
7.22M
}
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.90M
static unsigned getPrecedence(tok::TokenKind Kind) {
499
5.90M
  switch (Kind) {
500
5.90M
  
default: return ~0U13
;
501
5.90M
  case tok::percent:
502
8.94k
  case tok::slash:
503
8.94k
  case tok::star:                 return 14;
504
19.8k
  case tok::plus:
505
19.8k
  case tok::minus:                return 13;
506
19.8k
  case tok::lessless:
507
3.86k
  case tok::greatergreater:       return 12;
508
1.14M
  case tok::lessequal:
509
1.14M
  case tok::less:
510
1.14M
  case tok::greaterequal:
511
1.14M
  case tok::greater:              return 11;
512
1.14M
  case tok::exclaimequal:
513
106k
  case tok::equalequal:           return 10;
514
106k
  
case tok::amp: return 92.00k
;
515
106k
  
case tok::caret: return 810
;
516
106k
  
case tok::pipe: return 713
;
517
828k
  case tok::ampamp:               return 6;
518
639k
  case tok::pipepipe:             return 5;
519
1.25M
  case tok::question:             return 4;
520
106k
  
case tok::comma: return 34.33k
;
521
106k
  
case tok::colon: return 24.34k
;
522
265k
  case tok::r_paren:              return 0;// Lowest priority, end of expr.
523
1.62M
  case tok::eod:                  return 0;// Lowest priority, end of directive.
524
5.90M
  }
525
5.90M
}
526
527
static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
528
13
                                       Token &Tok) {
529
13
  if (Tok.is(tok::l_paren) && 
LHS.getIdentifier()4
)
530
3
    PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
531
3
        << LHS.getIdentifier();
532
10
  else
533
10
    PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
534
10
        << LHS.getRange();
535
13
}
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.90M
                                     Preprocessor &PP) {
547
1.90M
  unsigned PeekPrec = getPrecedence(PeekTok.getKind());
548
1.90M
  // If this token isn't valid, report the error.
549
1.90M
  if (PeekPrec == ~0U) {
550
12
    diagnoseUnexpectedOperator(PP, LHS, PeekTok);
551
12
    return true;
552
12
  }
553
1.90M
554
4.23M
  
while (1.90M
true) {
555
4.23M
    // If this token has a lower precedence than we are allowed to parse, return
556
4.23M
    // it so that higher levels of the recursion can parse it.
557
4.23M
    if (PeekPrec < MinPrec)
558
1.90M
      return false;
559
2.33M
560
2.33M
    tok::TokenKind Operator = PeekTok.getKind();
561
2.33M
562
2.33M
    // If this is a short-circuiting operator, see if the RHS of the operator is
563
2.33M
    // dead.  Note that this cannot just clobber ValueLive.  Consider
564
2.33M
    // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)".  In
565
2.33M
    // this example, the RHS of the && being dead does not make the rest of the
566
2.33M
    // expr dead.
567
2.33M
    bool RHSIsLive;
568
2.33M
    if (Operator == tok::ampamp && 
LHS.Val == 0818k
)
569
415k
      RHSIsLive = false;   // RHS of "0 && x" is dead.
570
1.91M
    else if (Operator == tok::pipepipe && 
LHS.Val != 0632k
)
571
342k
      RHSIsLive = false;   // RHS of "1 || x" is dead.
572
1.57M
    else if (Operator == tok::question && 
LHS.Val == 04.33k
)
573
4.07k
      RHSIsLive = false;   // RHS (x) of "0 ? x : y" is dead.
574
1.57M
    else
575
1.57M
      RHSIsLive = ValueLive;
576
2.33M
577
2.33M
    // Consume the operator, remembering the operator's location for reporting.
578
2.33M
    SourceLocation OpLoc = PeekTok.getLocation();
579
2.33M
    PP.LexNonComment(PeekTok);
580
2.33M
581
2.33M
    PPValue RHS(LHS.getBitWidth());
582
2.33M
    // Parse the RHS of the operator.
583
2.33M
    DefinedTracker DT;
584
2.33M
    if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) 
return true1
;
585
2.33M
    IncludedUndefinedIds = DT.IncludedUndefinedIds;
586
2.33M
587
2.33M
    // Remember the precedence of this operator and get the precedence of the
588
2.33M
    // operator immediately to the right of the RHS.
589
2.33M
    unsigned ThisPrec = PeekPrec;
590
2.33M
    PeekPrec = getPrecedence(PeekTok.getKind());
591
2.33M
592
2.33M
    // If this token isn't valid, report the error.
593
2.33M
    if (PeekPrec == ~0U) {
594
1
      diagnoseUnexpectedOperator(PP, RHS, PeekTok);
595
1
      return true;
596
1
    }
597
2.33M
598
2.33M
    // Decide whether to include the next binop in this subexpression.  For
599
2.33M
    // example, when parsing x+y*z and looking at '*', we want to recursively
600
2.33M
    // handle y*z as a single subexpression.  We do this because the precedence
601
2.33M
    // of * is higher than that of +.  The only strange case we have to handle
602
2.33M
    // here is for the ?: operator, where the precedence is actually lower than
603
2.33M
    // the LHS of the '?'.  The grammar rule is:
604
2.33M
    //
605
2.33M
    // conditional-expression ::=
606
2.33M
    //    logical-OR-expression ? expression : conditional-expression
607
2.33M
    // where 'expression' is actually comma-expression.
608
2.33M
    unsigned RHSPrec;
609
2.33M
    if (Operator == tok::question)
610
4.33k
      // The RHS of "?" should be maximally consumed as an expression.
611
4.33k
      RHSPrec = getPrecedence(tok::comma);
612
2.33M
    else  // All others should munch while higher precedence.
613
2.33M
      RHSPrec = ThisPrec+1;
614
2.33M
615
2.33M
    if (PeekPrec >= RHSPrec) {
616
412k
      if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
617
412k
                                   IncludedUndefinedIds, PP))
618
0
        return true;
619
412k
      PeekPrec = getPrecedence(PeekTok.getKind());
620
412k
    }
621
2.33M
    assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
622
2.33M
623
2.33M
    // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
624
2.33M
    // either operand is unsigned.
625
2.33M
    llvm::APSInt Res(LHS.getBitWidth());
626
2.33M
    switch (Operator) {
627
2.33M
    case tok::question:       // No UAC for x and y in "x ? y : z".
628
1.45M
    case tok::lessless:       // Shift amount doesn't UAC with shift value.
629
1.45M
    case tok::greatergreater: // Shift amount doesn't UAC with shift value.
630
1.45M
    case tok::comma:          // Comma operands are not subject to UACs.
631
1.45M
    case tok::pipepipe:       // Logical || does not do UACs.
632
1.45M
    case tok::ampamp:         // Logical && does not do UACs.
633
1.45M
      break;                  // No UAC
634
1.45M
    default:
635
874k
      Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
636
874k
      // If this just promoted something from signed to unsigned, and if the
637
874k
      // value was negative, warn about it.
638
874k
      if (ValueLive && 
Res.isUnsigned()604k
) {
639
5.45k
        if (!LHS.isUnsigned() && 
LHS.Val.isNegative()4.32k
)
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
5.45k
        if (!RHS.isUnsigned() && 
RHS.Val.isNegative()1.06k
)
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
5.45k
      }
650
874k
      LHS.Val.setIsUnsigned(Res.isUnsigned());
651
874k
      RHS.Val.setIsUnsigned(Res.isUnsigned());
652
2.33M
    }
653
2.33M
654
2.33M
    bool Overflow = false;
655
2.33M
    switch (Operator) {
656
2.33M
    
default: 0
llvm_unreachable0
("Unknown operator token!");
657
2.33M
    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
12
    case tok::slash:
667
12
      if (RHS.Val != 0) {
668
2
        if (LHS.Val.isSigned())
669
1
          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
8
      break;
678
8
679
8.92k
    case tok::star:
680
8.92k
      if (Res.isSigned())
681
8.89k
        Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
682
25
      else
683
25
        Res = LHS.Val * RHS.Val;
684
8.92k
      break;
685
3.78k
    case tok::lessless: {
686
3.78k
      // Determine whether overflow is about to happen.
687
3.78k
      if (LHS.isUnsigned())
688
3
        Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
689
3.78k
      else
690
3.78k
        Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
691
3.78k
      break;
692
8
    }
693
78
    case tok::greatergreater: {
694
78
      // Determine whether overflow is about to happen.
695
78
      unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
696
78
      if (ShAmt >= LHS.getBitWidth()) {
697
0
        Overflow = true;
698
0
        ShAmt = LHS.getBitWidth()-1;
699
0
      }
700
78
      Res = LHS.Val >> ShAmt;
701
78
      break;
702
8
    }
703
9.08k
    case tok::plus:
704
9.08k
      if (LHS.isUnsigned())
705
26
        Res = LHS.Val + RHS.Val;
706
9.06k
      else
707
9.06k
        Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
708
9.08k
      break;
709
10.5k
    case tok::minus:
710
10.5k
      if (LHS.isUnsigned())
711
13
        Res = LHS.Val - RHS.Val;
712
10.5k
      else
713
10.5k
        Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
714
10.5k
      break;
715
16.7k
    case tok::lessequal:
716
16.7k
      Res = LHS.Val <= RHS.Val;
717
16.7k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
718
16.7k
      break;
719
57.2k
    case tok::less:
720
57.2k
      Res = LHS.Val < RHS.Val;
721
57.2k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
722
57.2k
      break;
723
519k
    case tok::greaterequal:
724
519k
      Res = LHS.Val >= RHS.Val;
725
519k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
726
519k
      break;
727
164k
    case tok::greater:
728
164k
      Res = LHS.Val > RHS.Val;
729
164k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
730
164k
      break;
731
2.33k
    case tok::exclaimequal:
732
2.33k
      Res = LHS.Val != RHS.Val;
733
2.33k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
734
2.33k
      break;
735
83.1k
    case tok::equalequal:
736
83.1k
      Res = LHS.Val == RHS.Val;
737
83.1k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
738
83.1k
      break;
739
2.00k
    case tok::amp:
740
2.00k
      Res = LHS.Val & RHS.Val;
741
2.00k
      break;
742
10
    case tok::caret:
743
10
      Res = LHS.Val ^ RHS.Val;
744
10
      break;
745
13
    case tok::pipe:
746
13
      Res = LHS.Val | RHS.Val;
747
13
      break;
748
818k
    case tok::ampamp:
749
818k
      Res = (LHS.Val != 0 && 
RHS.Val != 0403k
);
750
818k
      Res.setIsUnsigned(false);  // C99 6.5.13p3, result is always int (signed)
751
818k
      break;
752
632k
    case tok::pipepipe:
753
632k
      Res = (LHS.Val != 0 || 
RHS.Val != 0290k
);
754
632k
      Res.setIsUnsigned(false);  // C99 6.5.14p3, result is always int (signed)
755
632k
      break;
756
8
    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
4.33k
    case tok::question: {
765
4.33k
      // Parse the : part of the expression.
766
4.33k
      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
4.33k
      // Consume the :.
773
4.33k
      PP.LexNonComment(PeekTok);
774
4.33k
775
4.33k
      // Evaluate the value after the :.
776
4.33k
      bool AfterColonLive = ValueLive && 
LHS.Val == 04.25k
;
777
4.33k
      PPValue AfterColonVal(LHS.getBitWidth());
778
4.33k
      DefinedTracker DT;
779
4.33k
      if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
780
0
        return true;
781
4.33k
782
4.33k
      // Parse anything after the : with the same precedence as ?.  We allow
783
4.33k
      // things of equal precedence because ?: is right associative.
784
4.33k
      if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
785
4.33k
                                   PeekTok, AfterColonLive,
786
4.33k
                                   IncludedUndefinedIds, PP))
787
1
        return true;
788
4.33k
789
4.33k
      // Now that we have the condition, the LHS and the RHS of the :, evaluate.
790
4.33k
      Res = LHS.Val != 0 ? 
RHS.Val259
:
AfterColonVal.Val4.07k
;
791
4.33k
      RHS.setEnd(AfterColonVal.getRange().getEnd());
792
4.33k
793
4.33k
      // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
794
4.33k
      // either operand is unsigned.
795
4.33k
      Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
796
4.33k
797
4.33k
      // Figure out the precedence of the token after the : part.
798
4.33k
      PeekPrec = getPrecedence(PeekTok.getKind());
799
4.33k
      break;
800
4.33k
    }
801
4.33k
    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.33M
    }
807
2.33M
808
2.33M
    // If this operator is live and overflowed, report the issue.
809
2.33M
    if (Overflow && 
ValueLive3
)
810
3
      PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
811
3
        << LHS.getRange() << RHS.getRange();
812
2.33M
813
2.33M
    // Put the result back into 'LHS' for our next iteration.
814
2.33M
    LHS.Val = Res;
815
2.33M
    LHS.setEnd(RHS.getRange().getEnd());
816
2.33M
    RHS.setIdentifier(nullptr);
817
2.33M
  }
818
1.90M
}
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.91M
Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
825
3.91M
  SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
826
3.91M
  // Save the current state of 'DisableMacroExpansion' and reset it to false. If
827
3.91M
  // 'DisableMacroExpansion' is true, then we must be in a macro argument list
828
3.91M
  // in which case a directive is undefined behavior.  We want macros to be able
829
3.91M
  // to recursively expand in order to get more gcc-list behavior, so we force
830
3.91M
  // DisableMacroExpansion to false and restore it when we're done parsing the
831
3.91M
  // expression.
832
3.91M
  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
833
3.91M
  DisableMacroExpansion = false;
834
3.91M
835
3.91M
  // Peek ahead one token.
836
3.91M
  Token Tok;
837
3.91M
  LexNonComment(Tok);
838
3.91M
839
3.91M
  // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
840
3.91M
  unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
841
3.91M
842
3.91M
  PPValue ResVal(BitWidth);
843
3.91M
  DefinedTracker DT;
844
3.91M
  SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
845
3.91M
  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.91M
862
3.91M
  // If we are at the end of the expression after just parsing a value, there
863
3.91M
  // must be no (unparenthesized) binary operators involved, so we can exit
864
3.91M
  // directly.
865
3.91M
  if (Tok.is(tok::eod)) {
866
2.66M
    // If the expression we parsed was of the form !defined(macro), return the
867
2.66M
    // macro in IfNDefMacro.
868
2.66M
    if (DT.State == DefinedTracker::NotDefinedMacro)
869
97.3k
      IfNDefMacro = DT.TheMacro;
870
2.66M
871
2.66M
    // Restore 'DisableMacroExpansion'.
872
2.66M
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
873
2.66M
    return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
874
2.66M
  }
875
1.24M
876
1.24M
  // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
877
1.24M
  // operator and the stuff after it.
878
1.24M
  if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
879
1.24M
                               Tok, true, DT.IncludedUndefinedIds, *this)) {
880
18
    // Parse error, skip the rest of the macro line.
881
18
    if (Tok.isNot(tok::eod))
882
14
      DiscardUntilEndOfDirective();
883
18
884
18
    // Restore 'DisableMacroExpansion'.
885
18
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
886
18
    return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
887
18
  }
888
1.24M
889
1.24M
  // If we aren't at the tok::eod token, something bad happened, like an extra
890
1.24M
  // ')' token.
891
1.24M
  if (Tok.isNot(tok::eod)) {
892
2
    Diag(Tok, diag::err_pp_expected_eol);
893
2
    DiscardUntilEndOfDirective();
894
2
  }
895
1.24M
896
1.24M
  // Restore 'DisableMacroExpansion'.
897
1.24M
  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
898
1.24M
  return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
899
1.24M
}