1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Preprocessor::EvaluateDirectiveExpression method,
10 // which parses and evaluates integer constant expressions for #if directives.
12 //===----------------------------------------------------------------------===//
14 // FIXME: implement testing for #assert's.
16 //===----------------------------------------------------------------------===//
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/StringExtras.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/SaveAndRestore.h"
38 using namespace clang
;
42 /// PPValue - Represents the value of a subexpression of a preprocessor
43 /// conditional and the source range covered by it.
46 IdentifierInfo
*II
= nullptr;
51 // Default ctor - Construct an 'invalid' PPValue.
52 PPValue(unsigned BitWidth
) : Val(BitWidth
) {}
54 // If this value was produced by directly evaluating an identifier, produce
56 IdentifierInfo
*getIdentifier() const { return II
; }
57 void setIdentifier(IdentifierInfo
*II
) { this->II
= II
; }
59 unsigned getBitWidth() const { return Val
.getBitWidth(); }
60 bool isUnsigned() const { return Val
.isUnsigned(); }
62 SourceRange
getRange() const { return Range
; }
64 void setRange(SourceLocation L
) { Range
.setBegin(L
); Range
.setEnd(L
); }
65 void setRange(SourceLocation B
, SourceLocation E
) {
66 Range
.setBegin(B
); Range
.setEnd(E
);
68 void setBegin(SourceLocation L
) { Range
.setBegin(L
); }
69 void setEnd(SourceLocation L
) { Range
.setEnd(L
); }
72 } // end anonymous namespace
74 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
75 Token
&PeekTok
, bool ValueLive
,
76 bool &IncludedUndefinedIds
,
79 /// DefinedTracker - This struct is used while parsing expressions to keep track
80 /// of whether !defined(X) has been seen.
82 /// With this simple scheme, we handle the basic forms:
83 /// !defined(X) and !defined X
84 /// but we also trivially handle (silly) stuff like:
85 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
86 struct DefinedTracker
{
87 /// Each time a Value is evaluated, it returns information about whether the
88 /// parsed value is of the form defined(X), !defined(X) or is something else.
90 DefinedMacro
, // defined(X)
91 NotDefinedMacro
, // !defined(X)
92 Unknown
// Something else.
94 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
95 /// indicates the macro that was checked.
96 IdentifierInfo
*TheMacro
;
97 bool IncludedUndefinedIds
= false;
100 /// EvaluateDefined - Process a 'defined(sym)' expression.
101 static bool EvaluateDefined(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
102 bool ValueLive
, Preprocessor
&PP
) {
103 SourceLocation
beginLoc(PeekTok
.getLocation());
104 Result
.setBegin(beginLoc
);
106 // Get the next token, don't expand it.
107 PP
.LexUnexpandedNonComment(PeekTok
);
109 // Two options, it can either be a pp-identifier or a (.
110 SourceLocation LParenLoc
;
111 if (PeekTok
.is(tok::l_paren
)) {
112 // Found a paren, remember we saw it and skip it.
113 LParenLoc
= PeekTok
.getLocation();
114 PP
.LexUnexpandedNonComment(PeekTok
);
117 if (PeekTok
.is(tok::code_completion
)) {
118 if (PP
.getCodeCompletionHandler())
119 PP
.getCodeCompletionHandler()->CodeCompleteMacroName(false);
120 PP
.setCodeCompletionReached();
121 PP
.LexUnexpandedNonComment(PeekTok
);
124 // If we don't have a pp-identifier now, this is an error.
125 if (PP
.CheckMacroName(PeekTok
, MU_Other
))
128 // Otherwise, we got an identifier, is it defined to something?
129 IdentifierInfo
*II
= PeekTok
.getIdentifierInfo();
130 MacroDefinition Macro
= PP
.getMacroDefinition(II
);
131 Result
.Val
= !!Macro
;
132 Result
.Val
.setIsUnsigned(false); // Result is signed intmax_t.
133 DT
.IncludedUndefinedIds
= !Macro
;
135 PP
.emitMacroExpansionWarnings(
137 (II
->getName() == "INFINITY" || II
->getName() == "NAN") ? true : false);
139 // If there is a macro, mark it used.
140 if (Result
.Val
!= 0 && ValueLive
)
141 PP
.markMacroAsUsed(Macro
.getMacroInfo());
143 // Save macro token for callback.
144 Token
macroToken(PeekTok
);
146 // If we are in parens, ensure we have a trailing ).
147 if (LParenLoc
.isValid()) {
148 // Consume identifier.
149 Result
.setEnd(PeekTok
.getLocation());
150 PP
.LexUnexpandedNonComment(PeekTok
);
152 if (PeekTok
.isNot(tok::r_paren
)) {
153 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expected_after
)
154 << "'defined'" << tok::r_paren
;
155 PP
.Diag(LParenLoc
, diag::note_matching
) << tok::l_paren
;
159 PP
.LexNonComment(PeekTok
);
160 Result
.setEnd(PeekTok
.getLocation());
162 // Consume identifier.
163 Result
.setEnd(PeekTok
.getLocation());
164 PP
.LexNonComment(PeekTok
);
168 // Prior to evaluation, macro invocations in the list of preprocessing
169 // tokens that will become the controlling constant expression are replaced
170 // (except for those macro names modified by the 'defined' unary operator),
171 // just as in normal text. If the token 'defined' is generated as a result
172 // of this replacement process or use of the 'defined' unary operator does
173 // not match one of the two specified forms prior to macro replacement, the
174 // behavior is undefined.
175 // This isn't an idle threat, consider this program:
177 // #define BAR defined(FOO)
183 // clang and gcc will pick the #if branch while Visual Studio will take the
184 // #else branch. Emit a warning about this undefined behavior.
185 if (beginLoc
.isMacroID()) {
186 bool IsFunctionTypeMacro
=
187 PP
.getSourceManager()
188 .getSLocEntry(PP
.getSourceManager().getFileID(beginLoc
))
190 .isFunctionMacroExpansion();
191 // For object-type macros, it's easy to replace
192 // #define FOO defined(BAR)
199 // and doing so makes sense since compilers handle this differently in
200 // practice (see example further up). But for function-type macros,
201 // there is no good way to write
202 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
203 // in a different way, and compilers seem to agree on how to behave here.
204 // So warn by default on object-type macros, but only warn in -pedantic
205 // mode on function-type macros.
206 if (IsFunctionTypeMacro
)
207 PP
.Diag(beginLoc
, diag::warn_defined_in_function_type_macro
);
209 PP
.Diag(beginLoc
, diag::warn_defined_in_object_type_macro
);
212 // Invoke the 'defined' callback.
213 if (PPCallbacks
*Callbacks
= PP
.getPPCallbacks()) {
214 Callbacks
->Defined(macroToken
, Macro
,
215 SourceRange(beginLoc
, PeekTok
.getLocation()));
218 // Success, remember that we saw defined(X).
219 DT
.State
= DefinedTracker::DefinedMacro
;
224 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
225 /// return the computed value in Result. Return true if there was an error
226 /// parsing. This function also returns information about the form of the
227 /// expression in DT. See above for information on what DT means.
229 /// If ValueLive is false, then this value is being evaluated in a context where
230 /// the result is not used. As such, avoid diagnostics that relate to
232 static bool EvaluateValue(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
233 bool ValueLive
, Preprocessor
&PP
) {
234 DT
.State
= DefinedTracker::Unknown
;
236 Result
.setIdentifier(nullptr);
238 if (PeekTok
.is(tok::code_completion
)) {
239 if (PP
.getCodeCompletionHandler())
240 PP
.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
241 PP
.setCodeCompletionReached();
242 PP
.LexNonComment(PeekTok
);
245 switch (PeekTok
.getKind()) {
247 // If this token's spelling is a pp-identifier, check to see if it is
248 // 'defined' or if it is a macro. Note that we check here because many
249 // keywords are pp-identifiers, so we can't check the kind.
250 if (IdentifierInfo
*II
= PeekTok
.getIdentifierInfo()) {
251 // Handle "defined X" and "defined(X)".
252 if (II
->isStr("defined"))
253 return EvaluateDefined(Result
, PeekTok
, DT
, ValueLive
, PP
);
255 if (!II
->isCPlusPlusOperatorKeyword()) {
256 // If this identifier isn't 'defined' or one of the special
257 // preprocessor keywords and it wasn't macro expanded, it turns
260 PP
.Diag(PeekTok
, diag::warn_pp_undef_identifier
) << II
;
262 const DiagnosticsEngine
&DiagEngine
= PP
.getDiagnostics();
263 // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
264 if (DiagEngine
.isIgnored(diag::warn_pp_undef_identifier
,
265 PeekTok
.getLocation())) {
266 const std::vector
<std::string
> UndefPrefixes
=
267 DiagEngine
.getDiagnosticOptions().UndefPrefixes
;
268 const StringRef IdentifierName
= II
->getName();
269 if (llvm::any_of(UndefPrefixes
,
270 [&IdentifierName
](const std::string
&Prefix
) {
271 return IdentifierName
.starts_with(Prefix
);
273 PP
.Diag(PeekTok
, diag::warn_pp_undef_prefix
)
274 << AddFlagValue
{llvm::join(UndefPrefixes
, ",")} << II
;
278 Result
.Val
.setIsUnsigned(false); // "0" is signed intmax_t 0.
279 Result
.setIdentifier(II
);
280 Result
.setRange(PeekTok
.getLocation());
281 DT
.IncludedUndefinedIds
= true;
282 PP
.LexNonComment(PeekTok
);
286 PP
.Diag(PeekTok
, diag::err_pp_expr_bad_token_start_expr
);
290 // If there is no expression, report and exit.
291 PP
.Diag(PeekTok
, diag::err_pp_expected_value_in_expr
);
293 case tok::numeric_constant
: {
294 SmallString
<64> IntegerBuffer
;
295 bool NumberInvalid
= false;
296 StringRef Spelling
= PP
.getSpelling(PeekTok
, IntegerBuffer
,
299 return true; // a diagnostic was already reported
301 NumericLiteralParser
Literal(Spelling
, PeekTok
.getLocation(),
302 PP
.getSourceManager(), PP
.getLangOpts(),
303 PP
.getTargetInfo(), PP
.getDiagnostics());
304 if (Literal
.hadError
)
305 return true; // a diagnostic was already reported.
307 if (Literal
.isFloatingLiteral() || Literal
.isImaginary
) {
308 PP
.Diag(PeekTok
, diag::err_pp_illegal_floating_literal
);
311 assert(Literal
.isIntegerLiteral() && "Unknown ppnumber");
313 // Complain about, and drop, any ud-suffix.
314 if (Literal
.hasUDSuffix())
315 PP
.Diag(PeekTok
, diag::err_pp_invalid_udl
) << /*integer*/1;
317 // 'long long' is a C99 or C++11 feature.
318 if (!PP
.getLangOpts().C99
&& Literal
.isLongLong
) {
319 if (PP
.getLangOpts().CPlusPlus
)
321 PP
.getLangOpts().CPlusPlus11
?
322 diag::warn_cxx98_compat_longlong
: diag::ext_cxx11_longlong
);
324 PP
.Diag(PeekTok
, diag::ext_c99_longlong
);
327 // 'z/uz' literals are a C++23 feature.
329 PP
.Diag(PeekTok
, PP
.getLangOpts().CPlusPlus
330 ? PP
.getLangOpts().CPlusPlus23
331 ? diag::warn_cxx20_compat_size_t_suffix
332 : diag::ext_cxx23_size_t_suffix
333 : diag::err_cxx23_size_t_suffix
);
335 // 'wb/uwb' literals are a C23 feature.
336 // '__wb/__uwb' are a C++ extension.
337 if (Literal
.isBitInt
)
338 PP
.Diag(PeekTok
, PP
.getLangOpts().CPlusPlus
? diag::ext_cxx_bitint_suffix
339 : PP
.getLangOpts().C23
340 ? diag::warn_c23_compat_bitint_suffix
341 : diag::ext_c23_bitint_suffix
);
343 // Parse the integer literal into Result.
344 if (Literal
.GetIntegerValue(Result
.Val
)) {
345 // Overflow parsing integer literal.
347 PP
.Diag(PeekTok
, diag::err_integer_literal_too_large
)
349 Result
.Val
.setIsUnsigned(true);
351 // Set the signedness of the result to match whether there was a U suffix
353 Result
.Val
.setIsUnsigned(Literal
.isUnsigned
);
355 // Detect overflow based on whether the value is signed. If signed
356 // and if the value is too large, emit a warning "integer constant is so
357 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
359 if (!Literal
.isUnsigned
&& Result
.Val
.isNegative()) {
360 // Octal, hexadecimal, and binary literals are implicitly unsigned if
361 // the value does not fit into a signed integer type.
362 if (ValueLive
&& Literal
.getRadix() == 10)
363 PP
.Diag(PeekTok
, diag::ext_integer_literal_too_large_for_signed
);
364 Result
.Val
.setIsUnsigned(true);
368 // Consume the token.
369 Result
.setRange(PeekTok
.getLocation());
370 PP
.LexNonComment(PeekTok
);
373 case tok::char_constant
: // 'x'
374 case tok::wide_char_constant
: // L'x'
375 case tok::utf8_char_constant
: // u8'x'
376 case tok::utf16_char_constant
: // u'x'
377 case tok::utf32_char_constant
: { // U'x'
378 // Complain about, and drop, any ud-suffix.
379 if (PeekTok
.hasUDSuffix())
380 PP
.Diag(PeekTok
, diag::err_pp_invalid_udl
) << /*character*/0;
382 SmallString
<32> CharBuffer
;
383 bool CharInvalid
= false;
384 StringRef ThisTok
= PP
.getSpelling(PeekTok
, CharBuffer
, &CharInvalid
);
388 CharLiteralParser
Literal(ThisTok
.begin(), ThisTok
.end(),
389 PeekTok
.getLocation(), PP
, PeekTok
.getKind());
390 if (Literal
.hadError())
391 return true; // A diagnostic was already emitted.
393 // Character literals are always int or wchar_t, expand to intmax_t.
394 const TargetInfo
&TI
= PP
.getTargetInfo();
396 if (Literal
.isMultiChar())
397 NumBits
= TI
.getIntWidth();
398 else if (Literal
.isWide())
399 NumBits
= TI
.getWCharWidth();
400 else if (Literal
.isUTF16())
401 NumBits
= TI
.getChar16Width();
402 else if (Literal
.isUTF32())
403 NumBits
= TI
.getChar32Width();
404 else // char or char8_t
405 NumBits
= TI
.getCharWidth();
408 llvm::APSInt
Val(NumBits
);
410 Val
= Literal
.getValue();
411 // Set the signedness. UTF-16 and UTF-32 are always unsigned
412 // UTF-8 is unsigned if -fchar8_t is specified.
413 if (Literal
.isWide())
414 Val
.setIsUnsigned(!TargetInfo::isTypeSigned(TI
.getWCharType()));
415 else if (Literal
.isUTF16() || Literal
.isUTF32())
416 Val
.setIsUnsigned(true);
417 else if (Literal
.isUTF8()) {
418 if (PP
.getLangOpts().CPlusPlus
)
420 PP
.getLangOpts().Char8
? true : !PP
.getLangOpts().CharIsSigned
);
422 Val
.setIsUnsigned(true);
424 Val
.setIsUnsigned(!PP
.getLangOpts().CharIsSigned
);
426 if (Result
.Val
.getBitWidth() > Val
.getBitWidth()) {
427 Result
.Val
= Val
.extend(Result
.Val
.getBitWidth());
429 assert(Result
.Val
.getBitWidth() == Val
.getBitWidth() &&
430 "intmax_t smaller than char/wchar_t?");
434 // Consume the token.
435 Result
.setRange(PeekTok
.getLocation());
436 PP
.LexNonComment(PeekTok
);
440 SourceLocation Start
= PeekTok
.getLocation();
441 PP
.LexNonComment(PeekTok
); // Eat the (.
442 // Parse the value and if there are any binary operators involved, parse
444 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
446 // If this is a silly value like (X), which doesn't need parens, check for
448 if (PeekTok
.is(tok::r_paren
)) {
449 // Just use DT unmodified as our result.
451 // Otherwise, we have something like (x+y), and we consumed '(x'.
452 if (EvaluateDirectiveSubExpr(Result
, 1, PeekTok
, ValueLive
,
453 DT
.IncludedUndefinedIds
, PP
))
456 if (PeekTok
.isNot(tok::r_paren
)) {
457 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expected_rparen
)
458 << Result
.getRange();
459 PP
.Diag(Start
, diag::note_matching
) << tok::l_paren
;
462 DT
.State
= DefinedTracker::Unknown
;
464 Result
.setRange(Start
, PeekTok
.getLocation());
465 Result
.setIdentifier(nullptr);
466 PP
.LexNonComment(PeekTok
); // Eat the ).
470 SourceLocation Start
= PeekTok
.getLocation();
471 // Unary plus doesn't modify the value.
472 PP
.LexNonComment(PeekTok
);
473 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
474 Result
.setBegin(Start
);
475 Result
.setIdentifier(nullptr);
479 SourceLocation Loc
= PeekTok
.getLocation();
480 PP
.LexNonComment(PeekTok
);
481 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
482 Result
.setBegin(Loc
);
483 Result
.setIdentifier(nullptr);
485 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
486 Result
.Val
= -Result
.Val
;
488 // -MININT is the only thing that overflows. Unsigned never overflows.
489 bool Overflow
= !Result
.isUnsigned() && Result
.Val
.isMinSignedValue();
491 // If this operator is live and overflowed, report the issue.
492 if (Overflow
&& ValueLive
)
493 PP
.Diag(Loc
, diag::warn_pp_expr_overflow
) << Result
.getRange();
495 DT
.State
= DefinedTracker::Unknown
;
500 SourceLocation Start
= PeekTok
.getLocation();
501 PP
.LexNonComment(PeekTok
);
502 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
503 Result
.setBegin(Start
);
504 Result
.setIdentifier(nullptr);
506 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
507 Result
.Val
= ~Result
.Val
;
508 DT
.State
= DefinedTracker::Unknown
;
513 SourceLocation Start
= PeekTok
.getLocation();
514 PP
.LexNonComment(PeekTok
);
515 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
516 Result
.setBegin(Start
);
517 Result
.Val
= !Result
.Val
;
518 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
519 Result
.Val
.setIsUnsigned(false);
520 Result
.setIdentifier(nullptr);
522 if (DT
.State
== DefinedTracker::DefinedMacro
)
523 DT
.State
= DefinedTracker::NotDefinedMacro
;
524 else if (DT
.State
== DefinedTracker::NotDefinedMacro
)
525 DT
.State
= DefinedTracker::DefinedMacro
;
530 Result
.Val
= PeekTok
.getKind() == tok::kw_true
;
531 Result
.Val
.setIsUnsigned(false); // "0" is signed intmax_t 0.
532 Result
.setIdentifier(PeekTok
.getIdentifierInfo());
533 Result
.setRange(PeekTok
.getLocation());
534 PP
.LexNonComment(PeekTok
);
537 // FIXME: Handle #assert
541 /// getPrecedence - Return the precedence of the specified binary operator
542 /// token. This returns:
543 /// ~0 - Invalid token.
544 /// 14 -> 3 - various operators.
546 static unsigned getPrecedence(tok::TokenKind Kind
) {
551 case tok::star
: return 14;
553 case tok::minus
: return 13;
555 case tok::greatergreater
: return 12;
558 case tok::greaterequal
:
559 case tok::greater
: return 11;
560 case tok::exclaimequal
:
561 case tok::equalequal
: return 10;
562 case tok::amp
: return 9;
563 case tok::caret
: return 8;
564 case tok::pipe
: return 7;
565 case tok::ampamp
: return 6;
566 case tok::pipepipe
: return 5;
567 case tok::question
: return 4;
568 case tok::comma
: return 3;
569 case tok::colon
: return 2;
570 case tok::r_paren
: return 0;// Lowest priority, end of expr.
571 case tok::eod
: return 0;// Lowest priority, end of directive.
575 static void diagnoseUnexpectedOperator(Preprocessor
&PP
, PPValue
&LHS
,
577 if (Tok
.is(tok::l_paren
) && LHS
.getIdentifier())
578 PP
.Diag(LHS
.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen
)
579 << LHS
.getIdentifier();
581 PP
.Diag(Tok
.getLocation(), diag::err_pp_expr_bad_token_binop
)
585 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
586 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
588 /// If ValueLive is false, then this value is being evaluated in a context where
589 /// the result is not used. As such, avoid diagnostics that relate to
590 /// evaluation, such as division by zero warnings.
591 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
592 Token
&PeekTok
, bool ValueLive
,
593 bool &IncludedUndefinedIds
,
595 unsigned PeekPrec
= getPrecedence(PeekTok
.getKind());
596 // If this token isn't valid, report the error.
597 if (PeekPrec
== ~0U) {
598 diagnoseUnexpectedOperator(PP
, LHS
, PeekTok
);
603 // If this token has a lower precedence than we are allowed to parse, return
604 // it so that higher levels of the recursion can parse it.
605 if (PeekPrec
< MinPrec
)
608 tok::TokenKind Operator
= PeekTok
.getKind();
610 // If this is a short-circuiting operator, see if the RHS of the operator is
611 // dead. Note that this cannot just clobber ValueLive. Consider
612 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
613 // this example, the RHS of the && being dead does not make the rest of the
616 if (Operator
== tok::ampamp
&& LHS
.Val
== 0)
617 RHSIsLive
= false; // RHS of "0 && x" is dead.
618 else if (Operator
== tok::pipepipe
&& LHS
.Val
!= 0)
619 RHSIsLive
= false; // RHS of "1 || x" is dead.
620 else if (Operator
== tok::question
&& LHS
.Val
== 0)
621 RHSIsLive
= false; // RHS (x) of "0 ? x : y" is dead.
623 RHSIsLive
= ValueLive
;
625 // Consume the operator, remembering the operator's location for reporting.
626 SourceLocation OpLoc
= PeekTok
.getLocation();
627 PP
.LexNonComment(PeekTok
);
629 PPValue
RHS(LHS
.getBitWidth());
630 // Parse the RHS of the operator.
632 if (EvaluateValue(RHS
, PeekTok
, DT
, RHSIsLive
, PP
)) return true;
633 IncludedUndefinedIds
= DT
.IncludedUndefinedIds
;
635 // Remember the precedence of this operator and get the precedence of the
636 // operator immediately to the right of the RHS.
637 unsigned ThisPrec
= PeekPrec
;
638 PeekPrec
= getPrecedence(PeekTok
.getKind());
640 // If this token isn't valid, report the error.
641 if (PeekPrec
== ~0U) {
642 diagnoseUnexpectedOperator(PP
, RHS
, PeekTok
);
646 // Decide whether to include the next binop in this subexpression. For
647 // example, when parsing x+y*z and looking at '*', we want to recursively
648 // handle y*z as a single subexpression. We do this because the precedence
649 // of * is higher than that of +. The only strange case we have to handle
650 // here is for the ?: operator, where the precedence is actually lower than
651 // the LHS of the '?'. The grammar rule is:
653 // conditional-expression ::=
654 // logical-OR-expression ? expression : conditional-expression
655 // where 'expression' is actually comma-expression.
657 if (Operator
== tok::question
)
658 // The RHS of "?" should be maximally consumed as an expression.
659 RHSPrec
= getPrecedence(tok::comma
);
660 else // All others should munch while higher precedence.
661 RHSPrec
= ThisPrec
+1;
663 if (PeekPrec
>= RHSPrec
) {
664 if (EvaluateDirectiveSubExpr(RHS
, RHSPrec
, PeekTok
, RHSIsLive
,
665 IncludedUndefinedIds
, PP
))
667 PeekPrec
= getPrecedence(PeekTok
.getKind());
669 assert(PeekPrec
<= ThisPrec
&& "Recursion didn't work!");
671 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
672 // either operand is unsigned.
673 llvm::APSInt
Res(LHS
.getBitWidth());
675 case tok::question
: // No UAC for x and y in "x ? y : z".
676 case tok::lessless
: // Shift amount doesn't UAC with shift value.
677 case tok::greatergreater
: // Shift amount doesn't UAC with shift value.
678 case tok::comma
: // Comma operands are not subject to UACs.
679 case tok::pipepipe
: // Logical || does not do UACs.
680 case tok::ampamp
: // Logical && does not do UACs.
683 Res
.setIsUnsigned(LHS
.isUnsigned() || RHS
.isUnsigned());
684 // If this just promoted something from signed to unsigned, and if the
685 // value was negative, warn about it.
686 if (ValueLive
&& Res
.isUnsigned()) {
687 if (!LHS
.isUnsigned() && LHS
.Val
.isNegative())
688 PP
.Diag(OpLoc
, diag::warn_pp_convert_to_positive
) << 0
689 << toString(LHS
.Val
, 10, true) + " to " +
690 toString(LHS
.Val
, 10, false)
691 << LHS
.getRange() << RHS
.getRange();
692 if (!RHS
.isUnsigned() && RHS
.Val
.isNegative())
693 PP
.Diag(OpLoc
, diag::warn_pp_convert_to_positive
) << 1
694 << toString(RHS
.Val
, 10, true) + " to " +
695 toString(RHS
.Val
, 10, false)
696 << LHS
.getRange() << RHS
.getRange();
698 LHS
.Val
.setIsUnsigned(Res
.isUnsigned());
699 RHS
.Val
.setIsUnsigned(Res
.isUnsigned());
702 bool Overflow
= false;
704 default: llvm_unreachable("Unknown operator token!");
707 Res
= LHS
.Val
% RHS
.Val
;
708 else if (ValueLive
) {
709 PP
.Diag(OpLoc
, diag::err_pp_remainder_by_zero
)
710 << LHS
.getRange() << RHS
.getRange();
716 if (LHS
.Val
.isSigned())
717 Res
= llvm::APSInt(LHS
.Val
.sdiv_ov(RHS
.Val
, Overflow
), false);
719 Res
= LHS
.Val
/ RHS
.Val
;
720 } else if (ValueLive
) {
721 PP
.Diag(OpLoc
, diag::err_pp_division_by_zero
)
722 << LHS
.getRange() << RHS
.getRange();
729 Res
= llvm::APSInt(LHS
.Val
.smul_ov(RHS
.Val
, Overflow
), false);
731 Res
= LHS
.Val
* RHS
.Val
;
733 case tok::lessless
: {
734 // Determine whether overflow is about to happen.
735 if (LHS
.isUnsigned())
736 Res
= LHS
.Val
.ushl_ov(RHS
.Val
, Overflow
);
738 Res
= llvm::APSInt(LHS
.Val
.sshl_ov(RHS
.Val
, Overflow
), false);
741 case tok::greatergreater
: {
742 // Determine whether overflow is about to happen.
743 unsigned ShAmt
= static_cast<unsigned>(RHS
.Val
.getLimitedValue());
744 if (ShAmt
>= LHS
.getBitWidth()) {
746 ShAmt
= LHS
.getBitWidth()-1;
748 Res
= LHS
.Val
>> ShAmt
;
752 if (LHS
.isUnsigned())
753 Res
= LHS
.Val
+ RHS
.Val
;
755 Res
= llvm::APSInt(LHS
.Val
.sadd_ov(RHS
.Val
, Overflow
), false);
758 if (LHS
.isUnsigned())
759 Res
= LHS
.Val
- RHS
.Val
;
761 Res
= llvm::APSInt(LHS
.Val
.ssub_ov(RHS
.Val
, Overflow
), false);
764 Res
= LHS
.Val
<= RHS
.Val
;
765 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
768 Res
= LHS
.Val
< RHS
.Val
;
769 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
771 case tok::greaterequal
:
772 Res
= LHS
.Val
>= RHS
.Val
;
773 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
776 Res
= LHS
.Val
> RHS
.Val
;
777 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
779 case tok::exclaimequal
:
780 Res
= LHS
.Val
!= RHS
.Val
;
781 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
783 case tok::equalequal
:
784 Res
= LHS
.Val
== RHS
.Val
;
785 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
788 Res
= LHS
.Val
& RHS
.Val
;
791 Res
= LHS
.Val
^ RHS
.Val
;
794 Res
= LHS
.Val
| RHS
.Val
;
797 Res
= (LHS
.Val
!= 0 && RHS
.Val
!= 0);
798 Res
.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
801 Res
= (LHS
.Val
!= 0 || RHS
.Val
!= 0);
802 Res
.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
805 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
806 // if not being evaluated.
807 if (!PP
.getLangOpts().C99
|| ValueLive
)
808 PP
.Diag(OpLoc
, diag::ext_pp_comma_expr
)
809 << LHS
.getRange() << RHS
.getRange();
810 Res
= RHS
.Val
; // LHS = LHS,RHS -> RHS.
812 case tok::question
: {
813 // Parse the : part of the expression.
814 if (PeekTok
.isNot(tok::colon
)) {
815 PP
.Diag(PeekTok
.getLocation(), diag::err_expected
)
816 << tok::colon
<< LHS
.getRange() << RHS
.getRange();
817 PP
.Diag(OpLoc
, diag::note_matching
) << tok::question
;
821 PP
.LexNonComment(PeekTok
);
823 // Evaluate the value after the :.
824 bool AfterColonLive
= ValueLive
&& LHS
.Val
== 0;
825 PPValue
AfterColonVal(LHS
.getBitWidth());
827 if (EvaluateValue(AfterColonVal
, PeekTok
, DT
, AfterColonLive
, PP
))
830 // Parse anything after the : with the same precedence as ?. We allow
831 // things of equal precedence because ?: is right associative.
832 if (EvaluateDirectiveSubExpr(AfterColonVal
, ThisPrec
,
833 PeekTok
, AfterColonLive
,
834 IncludedUndefinedIds
, PP
))
837 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
838 Res
= LHS
.Val
!= 0 ? RHS
.Val
: AfterColonVal
.Val
;
839 RHS
.setEnd(AfterColonVal
.getRange().getEnd());
841 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
842 // either operand is unsigned.
843 Res
.setIsUnsigned(RHS
.isUnsigned() || AfterColonVal
.isUnsigned());
845 // Figure out the precedence of the token after the : part.
846 PeekPrec
= getPrecedence(PeekTok
.getKind());
850 // Don't allow :'s to float around without being part of ?: exprs.
851 PP
.Diag(OpLoc
, diag::err_pp_colon_without_question
)
852 << LHS
.getRange() << RHS
.getRange();
856 // If this operator is live and overflowed, report the issue.
857 if (Overflow
&& ValueLive
)
858 PP
.Diag(OpLoc
, diag::warn_pp_expr_overflow
)
859 << LHS
.getRange() << RHS
.getRange();
861 // Put the result back into 'LHS' for our next iteration.
863 LHS
.setEnd(RHS
.getRange().getEnd());
864 RHS
.setIdentifier(nullptr);
868 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
869 /// may occur after a #if or #elif directive. If the expression is equivalent
870 /// to "!defined(X)" return X in IfNDefMacro.
871 Preprocessor::DirectiveEvalResult
872 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo
*&IfNDefMacro
,
873 Token
&Tok
, bool &EvaluatedDefined
,
875 SaveAndRestore
PPDir(ParsingIfOrElifDirective
, true);
876 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
877 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
878 // in which case a directive is undefined behavior. We want macros to be able
879 // to recursively expand in order to get more gcc-list behavior, so we force
880 // DisableMacroExpansion to false and restore it when we're done parsing the
882 bool DisableMacroExpansionAtStartOfDirective
= DisableMacroExpansion
;
883 DisableMacroExpansion
= false;
885 // Peek ahead one token.
888 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
889 unsigned BitWidth
= getTargetInfo().getIntMaxTWidth();
891 PPValue
ResVal(BitWidth
);
893 SourceLocation ExprStartLoc
= SourceMgr
.getExpansionLoc(Tok
.getLocation());
894 if (EvaluateValue(ResVal
, Tok
, DT
, true, *this)) {
895 // Parse error, skip the rest of the macro line.
896 SourceRange ConditionRange
= ExprStartLoc
;
897 if (Tok
.isNot(tok::eod
))
898 ConditionRange
= DiscardUntilEndOfDirective(Tok
);
900 // Restore 'DisableMacroExpansion'.
901 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
903 // We cannot trust the source range from the value because there was a
904 // parse error. Track the range manually -- the end of the directive is the
905 // end of the condition range.
906 return {std::nullopt
,
908 DT
.IncludedUndefinedIds
,
909 {ExprStartLoc
, ConditionRange
.getEnd()}};
912 EvaluatedDefined
= DT
.State
!= DefinedTracker::Unknown
;
914 // If we are at the end of the expression after just parsing a value, there
915 // must be no (unparenthesized) binary operators involved, so we can exit
917 if (Tok
.is(tok::eod
)) {
918 // If the expression we parsed was of the form !defined(macro), return the
919 // macro in IfNDefMacro.
920 if (DT
.State
== DefinedTracker::NotDefinedMacro
)
921 IfNDefMacro
= DT
.TheMacro
;
923 // Restore 'DisableMacroExpansion'.
924 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
925 bool IsNonZero
= ResVal
.Val
!= 0;
926 SourceRange ValRange
= ResVal
.getRange();
927 return {std::move(ResVal
.Val
), IsNonZero
, DT
.IncludedUndefinedIds
,
931 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
932 // operator and the stuff after it.
933 if (EvaluateDirectiveSubExpr(ResVal
, getPrecedence(tok::question
),
934 Tok
, true, DT
.IncludedUndefinedIds
, *this)) {
935 // Parse error, skip the rest of the macro line.
936 if (Tok
.isNot(tok::eod
))
937 DiscardUntilEndOfDirective(Tok
);
939 // Restore 'DisableMacroExpansion'.
940 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
941 SourceRange ValRange
= ResVal
.getRange();
942 return {std::nullopt
, false, DT
.IncludedUndefinedIds
, ValRange
};
946 // If we aren't at the tok::eod token, something bad happened, like an extra
948 if (Tok
.isNot(tok::eod
)) {
949 Diag(Tok
, diag::err_pp_expected_eol
);
950 DiscardUntilEndOfDirective(Tok
);
954 EvaluatedDefined
= EvaluatedDefined
|| DT
.State
!= DefinedTracker::Unknown
;
956 // Restore 'DisableMacroExpansion'.
957 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
958 bool IsNonZero
= ResVal
.Val
!= 0;
959 SourceRange ValRange
= ResVal
.getRange();
960 return {std::move(ResVal
.Val
), IsNonZero
, DT
.IncludedUndefinedIds
, ValRange
};
963 Preprocessor::DirectiveEvalResult
964 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo
*&IfNDefMacro
,
967 bool EvaluatedDefined
;
968 return EvaluateDirectiveExpression(IfNDefMacro
, Tok
, EvaluatedDefined
,