Coverage Report

Created: 2020-09-19 12:23

/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.17M
  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.5M
  void setIdentifier(IdentifierInfo *II) { this->II = II; }
59
60
4.91M
  unsigned getBitWidth() const { return Val.getBitWidth(); }
61
2.07M
  bool isUnsigned() const { return Val.isUnsigned(); }
62
63
6.17M
  SourceRange getRange() const { return Range; }
64
65
3.99M
  void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
66
331k
  void setRange(SourceLocation B, SourceLocation E) {
67
331k
    Range.setBegin(B); Range.setEnd(E);
68
331k
  }
69
3.12M
  void setBegin(SourceLocation L) { Range.setBegin(L); }
70
6.80M
  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.18M
                            bool ValueLive, Preprocessor &PP) {
104
2.18M
  SourceLocation beginLoc(PeekTok.getLocation());
105
2.18M
  Result.setBegin(beginLoc);
106
2.18M
107
  // Get the next token, don't expand it.
108
2.18M
  PP.LexUnexpandedNonComment(PeekTok);
109
2.18M
110
  // Two options, it can either be a pp-identifier or a (.
111
2.18M
  SourceLocation LParenLoc;
112
2.18M
  if (PeekTok.is(tok::l_paren)) {
113
    // Found a paren, remember we saw it and skip it.
114
2.16M
    LParenLoc = PeekTok.getLocation();
115
2.16M
    PP.LexUnexpandedNonComment(PeekTok);
116
2.16M
  }
117
2.18M
118
2.18M
  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
2.18M
125
  // If we don't have a pp-identifier now, this is an error.
126
2.18M
  if (PP.CheckMacroName(PeekTok, MU_Other))
127
4
    return true;
128
2.18M
129
  // Otherwise, we got an identifier, is it defined to something?
130
2.18M
  IdentifierInfo *II = PeekTok.getIdentifierInfo();
131
2.18M
  MacroDefinition Macro = PP.getMacroDefinition(II);
132
2.18M
  Result.Val = !!Macro;
133
2.18M
  Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
134
2.18M
  DT.IncludedUndefinedIds = !Macro;
135
2.18M
136
  // If there is a macro, mark it used.
137
2.18M
  if (Result.Val != 0 && 
ValueLive647k
)
138
528k
    PP.markMacroAsUsed(Macro.getMacroInfo());
139
2.18M
140
  // Save macro token for callback.
141
2.18M
  Token macroToken(PeekTok);
142
2.18M
143
  // If we are in parens, ensure we have a trailing ).
144
2.18M
  if (LParenLoc.isValid()) {
145
    // Consume identifier.
146
2.16M
    Result.setEnd(PeekTok.getLocation());
147
2.16M
    PP.LexUnexpandedNonComment(PeekTok);
148
2.16M
149
2.16M
    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.16M
    PP.LexNonComment(PeekTok);
157
2.16M
    Result.setEnd(PeekTok.getLocation());
158
20.5k
  } else {
159
    // Consume identifier.
160
20.5k
    Result.setEnd(PeekTok.getLocation());
161
20.5k
    PP.LexNonComment(PeekTok);
162
20.5k
  }
163
2.18M
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.18M
  if (beginLoc.isMacroID()) {
183
4.08k
    bool IsFunctionTypeMacro =
184
4.08k
        PP.getSourceManager()
185
4.08k
            .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
186
4.08k
            .getExpansion()
187
4.08k
            .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.08k
    if (IsFunctionTypeMacro)
204
819
      PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
205
3.26k
    else
206
3.26k
      PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
207
4.08k
  }
208
2.18M
209
  // Invoke the 'defined' callback.
210
2.18M
  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
211
2.18M
    Callbacks->Defined(macroToken, Macro,
212
2.18M
                       SourceRange(beginLoc, PeekTok.getLocation()));
213
2.18M
  }
214
2.18M
215
  // Success, remember that we saw defined(X).
216
2.18M
  DT.State = DefinedTracker::DefinedMacro;
217
2.18M
  DT.TheMacro = II;
218
2.18M
  return false;
219
2.18M
}
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.45M
                          bool ValueLive, Preprocessor &PP) {
231
7.45M
  DT.State = DefinedTracker::Unknown;
232
7.45M
233
7.45M
  Result.setIdentifier(nullptr);
234
7.45M
235
7.45M
  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
7.45M
242
7.45M
  switch (PeekTok.getKind()) {
243
2.49M
  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.49M
    if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
248
      // Handle "defined X" and "defined(X)".
249
2.49M
      if (II->isStr("defined"))
250
2.18M
        return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
251
313k
252
313k
      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
313k
        if (ValueLive) {
257
68.8k
          PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
258
68.8k
259
68.8k
          const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
260
          // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
261
68.8k
          if (DiagEngine.isIgnored(diag::warn_pp_undef_identifier,
262
68.8k
                                   PeekTok.getLocation())) {
263
68.8k
            const std::vector<std::string> UndefPrefixes =
264
68.8k
                DiagEngine.getDiagnosticOptions().UndefPrefixes;
265
68.8k
            const StringRef IdentifierName = II->getName();
266
68.8k
            if (llvm::any_of(UndefPrefixes,
267
64.9k
                             [&IdentifierName](const std::string &Prefix) {
268
64.9k
                               return IdentifierName.startswith(Prefix);
269
64.9k
                             }))
270
4.52k
              PP.Diag(PeekTok, diag::warn_pp_undef_prefix)
271
4.52k
                  << AddFlagValue{llvm::join(UndefPrefixes, ",")} << II;
272
68.8k
          }
273
68.8k
        }
274
313k
        Result.Val = 0;
275
313k
        Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
276
313k
        Result.setIdentifier(II);
277
313k
        Result.setRange(PeekTok.getLocation());
278
313k
        DT.IncludedUndefinedIds = true;
279
313k
        PP.LexNonComment(PeekTok);
280
313k
        return false;
281
313k
      }
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.68M
  case tok::numeric_constant: {
291
3.68M
    SmallString<64> IntegerBuffer;
292
3.68M
    bool NumberInvalid = false;
293
3.68M
    StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
294
3.68M
                                              &NumberInvalid);
295
3.68M
    if (NumberInvalid)
296
0
      return true; // a diagnostic was already reported
297
3.68M
298
3.68M
    NumericLiteralParser Literal(Spelling, PeekTok.getLocation(),
299
3.68M
                                 PP.getSourceManager(), PP.getLangOpts(),
300
3.68M
                                 PP.getTargetInfo(), PP.getDiagnostics());
301
3.68M
    if (Literal.hadError)
302
0
      return true; // a diagnostic was already reported.
303
3.68M
304
3.68M
    if (Literal.isFloatingLiteral() || Literal.isImaginary) {
305
0
      PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
306
0
      return true;
307
0
    }
308
3.68M
    assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
309
3.68M
310
    // Complain about, and drop, any ud-suffix.
311
3.68M
    if (Literal.hasUDSuffix())
312
1
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
313
3.68M
314
    // 'long long' is a C99 or C++11 feature.
315
3.68M
    if (!PP.getLangOpts().C99 && 
Literal.isLongLong2.00M
) {
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
3.68M
324
    // Parse the integer literal into Result.
325
3.68M
    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.68M
    } else {
332
      // Set the signedness of the result to match whether there was a U suffix
333
      // or not.
334
3.68M
      Result.Val.setIsUnsigned(Literal.isUnsigned);
335
3.68M
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.68M
      if (!Literal.isUnsigned && 
Result.Val.isNegative()3.67M
) {
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.68M
    }
348
3.68M
349
    // Consume the token.
350
3.68M
    Result.setRange(PeekTok.getLocation());
351
3.68M
    PP.LexNonComment(PeekTok);
352
3.68M
    return false;
353
3.68M
  }
354
13
  case tok::char_constant:          // 'x'
355
13
  case tok::wide_char_constant:     // L'x'
356
13
  case tok::utf8_char_constant:     // u8'x'
357
13
  case tok::utf16_char_constant:    // u'x'
358
13
  case tok::utf32_char_constant: {  // U'x'
359
    // Complain about, and drop, any ud-suffix.
360
13
    if (PeekTok.hasUDSuffix())
361
2
      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
362
13
363
13
    SmallString<32> CharBuffer;
364
13
    bool CharInvalid = false;
365
13
    StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
366
13
    if (CharInvalid)
367
0
      return true;
368
13
369
13
    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
370
13
                              PeekTok.getLocation(), PP, PeekTok.getKind());
371
13
    if (Literal.hadError())
372
0
      return true;  // A diagnostic was already emitted.
373
13
374
    // Character literals are always int or wchar_t, expand to intmax_t.
375
13
    const TargetInfo &TI = PP.getTargetInfo();
376
13
    unsigned NumBits;
377
13
    if (Literal.isMultiChar())
378
3
      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
13
388
    // Set the width.
389
13
    llvm::APSInt Val(NumBits);
390
    // Set the value.
391
13
    Val = Literal.getValue();
392
    // Set the signedness. UTF-16 and UTF-32 are always unsigned
393
13
    if (Literal.isWide())
394
4
      Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
395
9
    else if (!Literal.isUTF16() && 
!Literal.isUTF32()8
)
396
8
      Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
397
13
398
13
    if (Result.Val.getBitWidth() > Val.getBitWidth()) {
399
13
      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
13
406
    // Consume the token.
407
13
    Result.setRange(PeekTok.getLocation());
408
13
    PP.LexNonComment(PeekTok);
409
13
    return false;
410
13
  }
411
331k
  case tok::l_paren: {
412
331k
    SourceLocation Start = PeekTok.getLocation();
413
331k
    PP.LexNonComment(PeekTok);  // Eat the (.
414
    // Parse the value and if there are any binary operators involved, parse
415
    // them.
416
331k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
417
331k
418
    // If this is a silly value like (X), which doesn't need parens, check for
419
    // !(defined X).
420
331k
    if (PeekTok.is(tok::r_paren)) {
421
      // Just use DT unmodified as our result.
422
287k
    } else {
423
      // Otherwise, we have something like (x+y), and we consumed '(x'.
424
287k
      if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
425
287k
                                   DT.IncludedUndefinedIds, PP))
426
0
        return true;
427
287k
428
287k
      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
287k
      DT.State = DefinedTracker::Unknown;
435
287k
    }
436
331k
    Result.setRange(Start, PeekTok.getLocation());
437
331k
    Result.setIdentifier(nullptr);
438
331k
    PP.LexNonComment(PeekTok);  // Eat the ).
439
331k
    return false;
440
331k
  }
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
110
457
    // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
458
110
    Result.Val = -Result.Val;
459
110
460
    // -MININT is the only thing that overflows.  Unsigned never overflows.
461
110
    bool Overflow = !Result.isUnsigned() && 
Result.Val.isMinSignedValue()108
;
462
110
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
110
467
110
    DT.State = DefinedTracker::Unknown;
468
110
    return false;
469
110
  }
470
110
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
7
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
7
484
946k
  case tok::exclaim: {
485
946k
    SourceLocation Start = PeekTok.getLocation();
486
946k
    PP.LexNonComment(PeekTok);
487
946k
    if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) 
return true0
;
488
946k
    Result.setBegin(Start);
489
946k
    Result.Val = !Result.Val;
490
    // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
491
946k
    Result.Val.setIsUnsigned(false);
492
946k
    Result.setIdentifier(nullptr);
493
946k
494
946k
    if (DT.State == DefinedTracker::DefinedMacro)
495
707k
      DT.State = DefinedTracker::NotDefinedMacro;
496
239k
    else if (DT.State == DefinedTracker::NotDefinedMacro)
497
0
      DT.State = DefinedTracker::DefinedMacro;
498
946k
    return false;
499
946k
  }
500
4
  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
7.45M
509
  // FIXME: Handle #assert
510
7.45M
  }
511
7.45M
}
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.25M
static unsigned getPrecedence(tok::TokenKind Kind) {
519
6.25M
  switch (Kind) {
520
15
  default: return ~0U;
521
16.3k
  case tok::percent:
522
16.3k
  case tok::slash:
523
16.3k
  case tok::star:                 return 14;
524
31.4k
  case tok::plus:
525
31.4k
  case tok::minus:                return 13;
526
11
  case tok::lessless:
527
11
  case tok::greatergreater:       return 12;
528
1.28M
  case tok::lessequal:
529
1.28M
  case tok::less:
530
1.28M
  case tok::greaterequal:
531
1.28M
  case tok::greater:              return 11;
532
86.8k
  case tok::exclaimequal:
533
86.8k
  case tok::equalequal:           return 10;
534
572
  case tok::amp:                  return 9;
535
10
  case tok::caret:                return 8;
536
10
  case tok::pipe:                 return 7;
537
842k
  case tok::ampamp:               return 6;
538
623k
  case tok::pipepipe:             return 5;
539
1.34M
  case tok::question:             return 4;
540
2.21k
  case tok::comma:                return 3;
541
2.21k
  case tok::colon:                return 2;
542
328k
  case tok::r_paren:              return 0;// Lowest priority, end of expr.
543
1.69M
  case tok::eod:                  return 0;// Lowest priority, end of directive.
544
6.25M
  }
545
6.25M
}
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.04M
                                     Preprocessor &PP) {
567
2.04M
  unsigned PeekPrec = getPrecedence(PeekTok.getKind());
568
  // If this token isn't valid, report the error.
569
2.04M
  if (PeekPrec == ~0U) {
570
14
    diagnoseUnexpectedOperator(PP, LHS, PeekTok);
571
14
    return true;
572
14
  }
573
2.04M
574
4.49M
  
while (2.04M
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.49M
    if (PeekPrec < MinPrec)
578
2.04M
      return false;
579
2.45M
580
2.45M
    tok::TokenKind Operator = PeekTok.getKind();
581
2.45M
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.45M
    bool RHSIsLive;
588
2.45M
    if (Operator == tok::ampamp && 
LHS.Val == 0830k
)
589
407k
      RHSIsLive = false;   // RHS of "0 && x" is dead.
590
2.05M
    else if (Operator == tok::pipepipe && 
LHS.Val != 0612k
)
591
355k
      RHSIsLive = false;   // RHS of "1 || x" is dead.
592
1.69M
    else if (Operator == tok::question && 
LHS.Val == 02.20k
)
593
1.90k
      RHSIsLive = false;   // RHS (x) of "0 ? x : y" is dead.
594
1.69M
    else
595
1.69M
      RHSIsLive = ValueLive;
596
2.45M
597
    // Consume the operator, remembering the operator's location for reporting.
598
2.45M
    SourceLocation OpLoc = PeekTok.getLocation();
599
2.45M
    PP.LexNonComment(PeekTok);
600
2.45M
601
2.45M
    PPValue RHS(LHS.getBitWidth());
602
    // Parse the RHS of the operator.
603
2.45M
    DefinedTracker DT;
604
2.45M
    if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) 
return true1
;
605
2.45M
    IncludedUndefinedIds = DT.IncludedUndefinedIds;
606
2.45M
607
    // Remember the precedence of this operator and get the precedence of the
608
    // operator immediately to the right of the RHS.
609
2.45M
    unsigned ThisPrec = PeekPrec;
610
2.45M
    PeekPrec = getPrecedence(PeekTok.getKind());
611
2.45M
612
    // If this token isn't valid, report the error.
613
2.45M
    if (PeekPrec == ~0U) {
614
1
      diagnoseUnexpectedOperator(PP, RHS, PeekTok);
615
1
      return true;
616
1
    }
617
2.45M
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.45M
    unsigned RHSPrec;
629
2.45M
    if (Operator == tok::question)
630
      // The RHS of "?" should be maximally consumed as an expression.
631
2.20k
      RHSPrec = getPrecedence(tok::comma);
632
2.45M
    else  // All others should munch while higher precedence.
633
2.45M
      RHSPrec = ThisPrec+1;
634
2.45M
635
2.45M
    if (PeekPrec >= RHSPrec) {
636
410k
      if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
637
410k
                                   IncludedUndefinedIds, PP))
638
0
        return true;
639
410k
      PeekPrec = getPrecedence(PeekTok.getKind());
640
410k
    }
641
2.45M
    assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
642
2.45M
643
    // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
644
    // either operand is unsigned.
645
2.45M
    llvm::APSInt Res(LHS.getBitWidth());
646
2.45M
    switch (Operator) {
647
1.44M
    case tok::question:       // No UAC for x and y in "x ? y : z".
648
1.44M
    case tok::lessless:       // Shift amount doesn't UAC with shift value.
649
1.44M
    case tok::greatergreater: // Shift amount doesn't UAC with shift value.
650
1.44M
    case tok::comma:          // Comma operands are not subject to UACs.
651
1.44M
    case tok::pipepipe:       // Logical || does not do UACs.
652
1.44M
    case tok::ampamp:         // Logical && does not do UACs.
653
1.44M
      break;                  // No UAC
654
1.01M
    default:
655
1.01M
      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.01M
      if (ValueLive && 
Res.isUnsigned()718k
) {
659
6.07k
        if (!LHS.isUnsigned() && 
LHS.Val.isNegative()4.17k
)
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.07k
        if (!RHS.isUnsigned() && 
RHS.Val.isNegative()1.88k
)
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.07k
      }
670
1.01M
      LHS.Val.setIsUnsigned(Res.isUnsigned());
671
1.01M
      RHS.Val.setIsUnsigned(Res.isUnsigned());
672
2.45M
    }
673
2.45M
674
2.45M
    bool Overflow = false;
675
2.45M
    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
7
699
16.2k
    case tok::star:
700
16.2k
      if (Res.isSigned())
701
16.2k
        Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
702
1
      else
703
1
        Res = LHS.Val * RHS.Val;
704
16.2k
      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
16.2k
    case tok::plus:
724
16.2k
      if (LHS.isUnsigned())
725
2
        Res = LHS.Val + RHS.Val;
726
16.2k
      else
727
16.2k
        Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
728
16.2k
      break;
729
15.1k
    case tok::minus:
730
15.1k
      if (LHS.isUnsigned())
731
13
        Res = LHS.Val - RHS.Val;
732
15.1k
      else
733
15.1k
        Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
734
15.1k
      break;
735
38.2k
    case tok::lessequal:
736
38.2k
      Res = LHS.Val <= RHS.Val;
737
38.2k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
738
38.2k
      break;
739
84.3k
    case tok::less:
740
84.3k
      Res = LHS.Val < RHS.Val;
741
84.3k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
742
84.3k
      break;
743
547k
    case tok::greaterequal:
744
547k
      Res = LHS.Val >= RHS.Val;
745
547k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
746
547k
      break;
747
226k
    case tok::greater:
748
226k
      Res = LHS.Val > RHS.Val;
749
226k
      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
750
226k
      break;
751
2.34k
    case tok::exclaimequal:
752
2.34k
      Res = LHS.Val != RHS.Val;
753
2.34k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
754
2.34k
      break;
755
65.7k
    case tok::equalequal:
756
65.7k
      Res = LHS.Val == RHS.Val;
757
65.7k
      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
758
65.7k
      break;
759
572
    case tok::amp:
760
572
      Res = LHS.Val & RHS.Val;
761
572
      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
830k
    case tok::ampamp:
769
830k
      Res = (LHS.Val != 0 && 
RHS.Val != 0422k
);
770
830k
      Res.setIsUnsigned(false);  // C99 6.5.13p3, result is always int (signed)
771
830k
      break;
772
612k
    case tok::pipepipe:
773
612k
      Res = (LHS.Val != 0 || 
RHS.Val != 0257k
);
774
612k
      Res.setIsUnsigned(false);  // C99 6.5.14p3, result is always int (signed)
775
612k
      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.20k
    case tok::question: {
785
      // Parse the : part of the expression.
786
2.20k
      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.20k
      PP.LexNonComment(PeekTok);
794
2.20k
795
      // Evaluate the value after the :.
796
2.20k
      bool AfterColonLive = ValueLive && 
LHS.Val == 02.12k
;
797
2.20k
      PPValue AfterColonVal(LHS.getBitWidth());
798
2.20k
      DefinedTracker DT;
799
2.20k
      if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
800
0
        return true;
801
2.20k
802
      // Parse anything after the : with the same precedence as ?.  We allow
803
      // things of equal precedence because ?: is right associative.
804
2.20k
      if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
805
2.20k
                                   PeekTok, AfterColonLive,
806
2.20k
                                   IncludedUndefinedIds, PP))
807
1
        return true;
808
2.20k
809
      // Now that we have the condition, the LHS and the RHS of the :, evaluate.
810
2.20k
      Res = LHS.Val != 0 ? 
RHS.Val304
:
AfterColonVal.Val1.90k
;
811
2.20k
      RHS.setEnd(AfterColonVal.getRange().getEnd());
812
2.20k
813
      // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
814
      // either operand is unsigned.
815
2.20k
      Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
816
2.20k
817
      // Figure out the precedence of the token after the : part.
818
2.20k
      PeekPrec = getPrecedence(PeekTok.getKind());
819
2.20k
      break;
820
2.20k
    }
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.45M
    }
827
2.45M
828
    // If this operator is live and overflowed, report the issue.
829
2.45M
    if (Overflow && 
ValueLive3
)
830
3
      PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
831
3
        << LHS.getRange() << RHS.getRange();
832
2.45M
833
    // Put the result back into 'LHS' for our next iteration.
834
2.45M
    LHS.Val = Res;
835
2.45M
    LHS.setEnd(RHS.getRange().getEnd());
836
2.45M
    RHS.setIdentifier(nullptr);
837
2.45M
  }
838
2.04M
}
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.71M
Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
845
3.71M
  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.71M
  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
853
3.71M
  DisableMacroExpansion = false;
854
3.71M
855
  // Peek ahead one token.
856
3.71M
  Token Tok;
857
3.71M
  LexNonComment(Tok);
858
3.71M
859
  // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
860
3.71M
  unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
861
3.71M
862
3.71M
  PPValue ResVal(BitWidth);
863
3.71M
  DefinedTracker DT;
864
3.71M
  SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
865
3.71M
  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
33
871
    // Restore 'DisableMacroExpansion'.
872
33
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
873
33
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
3.71M
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.71M
  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.37M
    if (DT.State == DefinedTracker::NotDefinedMacro)
889
231k
      IfNDefMacro = DT.TheMacro;
890
2.37M
891
    // Restore 'DisableMacroExpansion'.
892
2.37M
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
893
2.37M
    return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
894
2.37M
  }
895
1.34M
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.34M
  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
20
904
    // Restore 'DisableMacroExpansion'.
905
20
    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
906
20
    return {false, DT.IncludedUndefinedIds, ResVal.getRange()};
907
20
  }
908
1.34M
909
  // If we aren't at the tok::eod token, something bad happened, like an extra
910
  // ')' token.
911
1.34M
  if (Tok.isNot(tok::eod)) {
912
2
    Diag(Tok, diag::err_pp_expected_eol);
913
2
    DiscardUntilEndOfDirective();
914
2
  }
915
1.34M
916
  // Restore 'DisableMacroExpansion'.
917
1.34M
  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
918
1.34M
  return {ResVal.Val != 0, DT.IncludedUndefinedIds, ResVal.getRange()};
919
1.34M
}