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/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"
39 using namespace clang
;
43 /// PPValue - Represents the value of a subexpression of a preprocessor
44 /// conditional and the source range covered by it.
52 // Default ctor - Construct an 'invalid' PPValue.
53 PPValue(unsigned BitWidth
) : Val(BitWidth
) {}
55 // If this value was produced by directly evaluating an identifier, produce
57 IdentifierInfo
*getIdentifier() const { return II
; }
58 void setIdentifier(IdentifierInfo
*II
) { this->II
= II
; }
60 unsigned getBitWidth() const { return Val
.getBitWidth(); }
61 bool isUnsigned() const { return Val
.isUnsigned(); }
63 SourceRange
getRange() const { return Range
; }
65 void setRange(SourceLocation L
) { Range
.setBegin(L
); Range
.setEnd(L
); }
66 void setRange(SourceLocation B
, SourceLocation E
) {
67 Range
.setBegin(B
); Range
.setEnd(E
);
69 void setBegin(SourceLocation L
) { Range
.setBegin(L
); }
70 void setEnd(SourceLocation L
) { Range
.setEnd(L
); }
73 } // end anonymous namespace
75 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
76 Token
&PeekTok
, bool ValueLive
,
77 bool &IncludedUndefinedIds
,
80 /// DefinedTracker - This struct is used while parsing expressions to keep track
81 /// of whether !defined(X) has been seen.
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.
91 DefinedMacro
, // defined(X)
92 NotDefinedMacro
, // !defined(X)
93 Unknown
// Something else.
95 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
96 /// indicates the macro that was checked.
97 IdentifierInfo
*TheMacro
;
98 bool IncludedUndefinedIds
= false;
101 /// EvaluateDefined - Process a 'defined(sym)' expression.
102 static bool EvaluateDefined(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
103 bool ValueLive
, Preprocessor
&PP
) {
104 SourceLocation
beginLoc(PeekTok
.getLocation());
105 Result
.setBegin(beginLoc
);
107 // Get the next token, don't expand it.
108 PP
.LexUnexpandedNonComment(PeekTok
);
110 // Two options, it can either be a pp-identifier or a (.
111 SourceLocation LParenLoc
;
112 if (PeekTok
.is(tok::l_paren
)) {
113 // Found a paren, remember we saw it and skip it.
114 LParenLoc
= PeekTok
.getLocation();
115 PP
.LexUnexpandedNonComment(PeekTok
);
118 if (PeekTok
.is(tok::code_completion
)) {
119 if (PP
.getCodeCompletionHandler())
120 PP
.getCodeCompletionHandler()->CodeCompleteMacroName(false);
121 PP
.setCodeCompletionReached();
122 PP
.LexUnexpandedNonComment(PeekTok
);
125 // If we don't have a pp-identifier now, this is an error.
126 if (PP
.CheckMacroName(PeekTok
, MU_Other
))
129 // Otherwise, we got an identifier, is it defined to something?
130 IdentifierInfo
*II
= PeekTok
.getIdentifierInfo();
131 MacroDefinition Macro
= PP
.getMacroDefinition(II
);
132 Result
.Val
= !!Macro
;
133 Result
.Val
.setIsUnsigned(false); // Result is signed intmax_t.
134 DT
.IncludedUndefinedIds
= !Macro
;
136 PP
.emitMacroExpansionWarnings(PeekTok
);
138 // If there is a macro, mark it used.
139 if (Result
.Val
!= 0 && ValueLive
)
140 PP
.markMacroAsUsed(Macro
.getMacroInfo());
142 // Save macro token for callback.
143 Token
macroToken(PeekTok
);
145 // If we are in parens, ensure we have a trailing ).
146 if (LParenLoc
.isValid()) {
147 // Consume identifier.
148 Result
.setEnd(PeekTok
.getLocation());
149 PP
.LexUnexpandedNonComment(PeekTok
);
151 if (PeekTok
.isNot(tok::r_paren
)) {
152 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expected_after
)
153 << "'defined'" << tok::r_paren
;
154 PP
.Diag(LParenLoc
, diag::note_matching
) << tok::l_paren
;
158 PP
.LexNonComment(PeekTok
);
159 Result
.setEnd(PeekTok
.getLocation());
161 // Consume identifier.
162 Result
.setEnd(PeekTok
.getLocation());
163 PP
.LexNonComment(PeekTok
);
167 // Prior to evaluation, macro invocations in the list of preprocessing
168 // tokens that will become the controlling constant expression are replaced
169 // (except for those macro names modified by the 'defined' unary operator),
170 // just as in normal text. If the token 'defined' is generated as a result
171 // of this replacement process or use of the 'defined' unary operator does
172 // not match one of the two specified forms prior to macro replacement, the
173 // behavior is undefined.
174 // This isn't an idle threat, consider this program:
176 // #define BAR defined(FOO)
182 // clang and gcc will pick the #if branch while Visual Studio will take the
183 // #else branch. Emit a warning about this undefined behavior.
184 if (beginLoc
.isMacroID()) {
185 bool IsFunctionTypeMacro
=
186 PP
.getSourceManager()
187 .getSLocEntry(PP
.getSourceManager().getFileID(beginLoc
))
189 .isFunctionMacroExpansion();
190 // For object-type macros, it's easy to replace
191 // #define FOO defined(BAR)
198 // and doing so makes sense since compilers handle this differently in
199 // practice (see example further up). But for function-type macros,
200 // there is no good way to write
201 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
202 // in a different way, and compilers seem to agree on how to behave here.
203 // So warn by default on object-type macros, but only warn in -pedantic
204 // mode on function-type macros.
205 if (IsFunctionTypeMacro
)
206 PP
.Diag(beginLoc
, diag::warn_defined_in_function_type_macro
);
208 PP
.Diag(beginLoc
, diag::warn_defined_in_object_type_macro
);
211 // Invoke the 'defined' callback.
212 if (PPCallbacks
*Callbacks
= PP
.getPPCallbacks()) {
213 Callbacks
->Defined(macroToken
, Macro
,
214 SourceRange(beginLoc
, PeekTok
.getLocation()));
217 // Success, remember that we saw defined(X).
218 DT
.State
= DefinedTracker::DefinedMacro
;
223 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
224 /// return the computed value in Result. Return true if there was an error
225 /// parsing. This function also returns information about the form of the
226 /// expression in DT. See above for information on what DT means.
228 /// If ValueLive is false, then this value is being evaluated in a context where
229 /// the result is not used. As such, avoid diagnostics that relate to
231 static bool EvaluateValue(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
232 bool ValueLive
, Preprocessor
&PP
) {
233 DT
.State
= DefinedTracker::Unknown
;
235 Result
.setIdentifier(nullptr);
237 if (PeekTok
.is(tok::code_completion
)) {
238 if (PP
.getCodeCompletionHandler())
239 PP
.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
240 PP
.setCodeCompletionReached();
241 PP
.LexNonComment(PeekTok
);
244 switch (PeekTok
.getKind()) {
246 // If this token's spelling is a pp-identifier, check to see if it is
247 // 'defined' or if it is a macro. Note that we check here because many
248 // keywords are pp-identifiers, so we can't check the kind.
249 if (IdentifierInfo
*II
= PeekTok
.getIdentifierInfo()) {
250 // Handle "defined X" and "defined(X)".
251 if (II
->isStr("defined"))
252 return EvaluateDefined(Result
, PeekTok
, DT
, ValueLive
, PP
);
254 if (!II
->isCPlusPlusOperatorKeyword()) {
255 // If this identifier isn't 'defined' or one of the special
256 // preprocessor keywords and it wasn't macro expanded, it turns
259 PP
.Diag(PeekTok
, diag::warn_pp_undef_identifier
) << II
;
261 const DiagnosticsEngine
&DiagEngine
= PP
.getDiagnostics();
262 // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
263 if (DiagEngine
.isIgnored(diag::warn_pp_undef_identifier
,
264 PeekTok
.getLocation())) {
265 const std::vector
<std::string
> UndefPrefixes
=
266 DiagEngine
.getDiagnosticOptions().UndefPrefixes
;
267 const StringRef IdentifierName
= II
->getName();
268 if (llvm::any_of(UndefPrefixes
,
269 [&IdentifierName
](const std::string
&Prefix
) {
270 return IdentifierName
.startswith(Prefix
);
272 PP
.Diag(PeekTok
, diag::warn_pp_undef_prefix
)
273 << AddFlagValue
{llvm::join(UndefPrefixes
, ",")} << II
;
277 Result
.Val
.setIsUnsigned(false); // "0" is signed intmax_t 0.
278 Result
.setIdentifier(II
);
279 Result
.setRange(PeekTok
.getLocation());
280 DT
.IncludedUndefinedIds
= true;
281 PP
.LexNonComment(PeekTok
);
285 PP
.Diag(PeekTok
, diag::err_pp_expr_bad_token_start_expr
);
289 // If there is no expression, report and exit.
290 PP
.Diag(PeekTok
, diag::err_pp_expected_value_in_expr
);
292 case tok::numeric_constant
: {
293 SmallString
<64> IntegerBuffer
;
294 bool NumberInvalid
= false;
295 StringRef Spelling
= PP
.getSpelling(PeekTok
, IntegerBuffer
,
298 return true; // a diagnostic was already reported
300 NumericLiteralParser
Literal(Spelling
, PeekTok
.getLocation(),
301 PP
.getSourceManager(), PP
.getLangOpts(),
302 PP
.getTargetInfo(), PP
.getDiagnostics());
303 if (Literal
.hadError
)
304 return true; // a diagnostic was already reported.
306 if (Literal
.isFloatingLiteral() || Literal
.isImaginary
) {
307 PP
.Diag(PeekTok
, diag::err_pp_illegal_floating_literal
);
310 assert(Literal
.isIntegerLiteral() && "Unknown ppnumber");
312 // Complain about, and drop, any ud-suffix.
313 if (Literal
.hasUDSuffix())
314 PP
.Diag(PeekTok
, diag::err_pp_invalid_udl
) << /*integer*/1;
316 // 'long long' is a C99 or C++11 feature.
317 if (!PP
.getLangOpts().C99
&& Literal
.isLongLong
) {
318 if (PP
.getLangOpts().CPlusPlus
)
320 PP
.getLangOpts().CPlusPlus11
?
321 diag::warn_cxx98_compat_longlong
: diag::ext_cxx11_longlong
);
323 PP
.Diag(PeekTok
, diag::ext_c99_longlong
);
326 // 'z/uz' literals are a C++2b feature.
328 PP
.Diag(PeekTok
, PP
.getLangOpts().CPlusPlus
329 ? PP
.getLangOpts().CPlusPlus2b
330 ? diag::warn_cxx20_compat_size_t_suffix
331 : diag::ext_cxx2b_size_t_suffix
332 : diag::err_cxx2b_size_t_suffix
);
334 // 'wb/uwb' literals are a C2x feature. We explicitly do not support the
335 // suffix in C++ as an extension because a library-based UDL that resolves
336 // to a library type may be more appropriate there.
337 if (Literal
.isBitInt
)
338 PP
.Diag(PeekTok
, PP
.getLangOpts().C2x
339 ? diag::warn_c2x_compat_bitint_suffix
340 : diag::ext_c2x_bitint_suffix
);
342 // Parse the integer literal into Result.
343 if (Literal
.GetIntegerValue(Result
.Val
)) {
344 // Overflow parsing integer literal.
346 PP
.Diag(PeekTok
, diag::err_integer_literal_too_large
)
348 Result
.Val
.setIsUnsigned(true);
350 // Set the signedness of the result to match whether there was a U suffix
352 Result
.Val
.setIsUnsigned(Literal
.isUnsigned
);
354 // Detect overflow based on whether the value is signed. If signed
355 // and if the value is too large, emit a warning "integer constant is so
356 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
358 if (!Literal
.isUnsigned
&& Result
.Val
.isNegative()) {
359 // Octal, hexadecimal, and binary literals are implicitly unsigned if
360 // the value does not fit into a signed integer type.
361 if (ValueLive
&& Literal
.getRadix() == 10)
362 PP
.Diag(PeekTok
, diag::ext_integer_literal_too_large_for_signed
);
363 Result
.Val
.setIsUnsigned(true);
367 // Consume the token.
368 Result
.setRange(PeekTok
.getLocation());
369 PP
.LexNonComment(PeekTok
);
372 case tok::char_constant
: // 'x'
373 case tok::wide_char_constant
: // L'x'
374 case tok::utf8_char_constant
: // u8'x'
375 case tok::utf16_char_constant
: // u'x'
376 case tok::utf32_char_constant
: { // U'x'
377 // Complain about, and drop, any ud-suffix.
378 if (PeekTok
.hasUDSuffix())
379 PP
.Diag(PeekTok
, diag::err_pp_invalid_udl
) << /*character*/0;
381 SmallString
<32> CharBuffer
;
382 bool CharInvalid
= false;
383 StringRef ThisTok
= PP
.getSpelling(PeekTok
, CharBuffer
, &CharInvalid
);
387 CharLiteralParser
Literal(ThisTok
.begin(), ThisTok
.end(),
388 PeekTok
.getLocation(), PP
, PeekTok
.getKind());
389 if (Literal
.hadError())
390 return true; // A diagnostic was already emitted.
392 // Character literals are always int or wchar_t, expand to intmax_t.
393 const TargetInfo
&TI
= PP
.getTargetInfo();
395 if (Literal
.isMultiChar())
396 NumBits
= TI
.getIntWidth();
397 else if (Literal
.isWide())
398 NumBits
= TI
.getWCharWidth();
399 else if (Literal
.isUTF16())
400 NumBits
= TI
.getChar16Width();
401 else if (Literal
.isUTF32())
402 NumBits
= TI
.getChar32Width();
403 else // char or char8_t
404 NumBits
= TI
.getCharWidth();
407 llvm::APSInt
Val(NumBits
);
409 Val
= Literal
.getValue();
410 // Set the signedness. UTF-16 and UTF-32 are always unsigned
411 // UTF-8 is unsigned if -fchar8_t is specified.
412 if (Literal
.isWide())
413 Val
.setIsUnsigned(!TargetInfo::isTypeSigned(TI
.getWCharType()));
414 else if (Literal
.isUTF16() || Literal
.isUTF32())
415 Val
.setIsUnsigned(true);
416 else if (Literal
.isUTF8()) {
417 if (PP
.getLangOpts().CPlusPlus
)
419 PP
.getLangOpts().Char8
? true : !PP
.getLangOpts().CharIsSigned
);
421 Val
.setIsUnsigned(true);
423 Val
.setIsUnsigned(!PP
.getLangOpts().CharIsSigned
);
425 if (Result
.Val
.getBitWidth() > Val
.getBitWidth()) {
426 Result
.Val
= Val
.extend(Result
.Val
.getBitWidth());
428 assert(Result
.Val
.getBitWidth() == Val
.getBitWidth() &&
429 "intmax_t smaller than char/wchar_t?");
433 // Consume the token.
434 Result
.setRange(PeekTok
.getLocation());
435 PP
.LexNonComment(PeekTok
);
439 SourceLocation Start
= PeekTok
.getLocation();
440 PP
.LexNonComment(PeekTok
); // Eat the (.
441 // Parse the value and if there are any binary operators involved, parse
443 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
445 // If this is a silly value like (X), which doesn't need parens, check for
447 if (PeekTok
.is(tok::r_paren
)) {
448 // Just use DT unmodified as our result.
450 // Otherwise, we have something like (x+y), and we consumed '(x'.
451 if (EvaluateDirectiveSubExpr(Result
, 1, PeekTok
, ValueLive
,
452 DT
.IncludedUndefinedIds
, PP
))
455 if (PeekTok
.isNot(tok::r_paren
)) {
456 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expected_rparen
)
457 << Result
.getRange();
458 PP
.Diag(Start
, diag::note_matching
) << tok::l_paren
;
461 DT
.State
= DefinedTracker::Unknown
;
463 Result
.setRange(Start
, PeekTok
.getLocation());
464 Result
.setIdentifier(nullptr);
465 PP
.LexNonComment(PeekTok
); // Eat the ).
469 SourceLocation Start
= PeekTok
.getLocation();
470 // Unary plus doesn't modify the value.
471 PP
.LexNonComment(PeekTok
);
472 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
473 Result
.setBegin(Start
);
474 Result
.setIdentifier(nullptr);
478 SourceLocation Loc
= PeekTok
.getLocation();
479 PP
.LexNonComment(PeekTok
);
480 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
481 Result
.setBegin(Loc
);
482 Result
.setIdentifier(nullptr);
484 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
485 Result
.Val
= -Result
.Val
;
487 // -MININT is the only thing that overflows. Unsigned never overflows.
488 bool Overflow
= !Result
.isUnsigned() && Result
.Val
.isMinSignedValue();
490 // If this operator is live and overflowed, report the issue.
491 if (Overflow
&& ValueLive
)
492 PP
.Diag(Loc
, diag::warn_pp_expr_overflow
) << Result
.getRange();
494 DT
.State
= DefinedTracker::Unknown
;
499 SourceLocation Start
= PeekTok
.getLocation();
500 PP
.LexNonComment(PeekTok
);
501 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
502 Result
.setBegin(Start
);
503 Result
.setIdentifier(nullptr);
505 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
506 Result
.Val
= ~Result
.Val
;
507 DT
.State
= DefinedTracker::Unknown
;
512 SourceLocation Start
= PeekTok
.getLocation();
513 PP
.LexNonComment(PeekTok
);
514 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
515 Result
.setBegin(Start
);
516 Result
.Val
= !Result
.Val
;
517 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
518 Result
.Val
.setIsUnsigned(false);
519 Result
.setIdentifier(nullptr);
521 if (DT
.State
== DefinedTracker::DefinedMacro
)
522 DT
.State
= DefinedTracker::NotDefinedMacro
;
523 else if (DT
.State
== DefinedTracker::NotDefinedMacro
)
524 DT
.State
= DefinedTracker::DefinedMacro
;
529 Result
.Val
= PeekTok
.getKind() == tok::kw_true
;
530 Result
.Val
.setIsUnsigned(false); // "0" is signed intmax_t 0.
531 Result
.setIdentifier(PeekTok
.getIdentifierInfo());
532 Result
.setRange(PeekTok
.getLocation());
533 PP
.LexNonComment(PeekTok
);
536 // FIXME: Handle #assert
540 /// getPrecedence - Return the precedence of the specified binary operator
541 /// token. This returns:
542 /// ~0 - Invalid token.
543 /// 14 -> 3 - various operators.
545 static unsigned getPrecedence(tok::TokenKind Kind
) {
550 case tok::star
: return 14;
552 case tok::minus
: return 13;
554 case tok::greatergreater
: return 12;
557 case tok::greaterequal
:
558 case tok::greater
: return 11;
559 case tok::exclaimequal
:
560 case tok::equalequal
: return 10;
561 case tok::amp
: return 9;
562 case tok::caret
: return 8;
563 case tok::pipe
: return 7;
564 case tok::ampamp
: return 6;
565 case tok::pipepipe
: return 5;
566 case tok::question
: return 4;
567 case tok::comma
: return 3;
568 case tok::colon
: return 2;
569 case tok::r_paren
: return 0;// Lowest priority, end of expr.
570 case tok::eod
: return 0;// Lowest priority, end of directive.
574 static void diagnoseUnexpectedOperator(Preprocessor
&PP
, PPValue
&LHS
,
576 if (Tok
.is(tok::l_paren
) && LHS
.getIdentifier())
577 PP
.Diag(LHS
.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen
)
578 << LHS
.getIdentifier();
580 PP
.Diag(Tok
.getLocation(), diag::err_pp_expr_bad_token_binop
)
584 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
585 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
587 /// If ValueLive is false, then this value is being evaluated in a context where
588 /// the result is not used. As such, avoid diagnostics that relate to
589 /// evaluation, such as division by zero warnings.
590 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
591 Token
&PeekTok
, bool ValueLive
,
592 bool &IncludedUndefinedIds
,
594 unsigned PeekPrec
= getPrecedence(PeekTok
.getKind());
595 // If this token isn't valid, report the error.
596 if (PeekPrec
== ~0U) {
597 diagnoseUnexpectedOperator(PP
, LHS
, PeekTok
);
602 // If this token has a lower precedence than we are allowed to parse, return
603 // it so that higher levels of the recursion can parse it.
604 if (PeekPrec
< MinPrec
)
607 tok::TokenKind Operator
= PeekTok
.getKind();
609 // If this is a short-circuiting operator, see if the RHS of the operator is
610 // dead. Note that this cannot just clobber ValueLive. Consider
611 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
612 // this example, the RHS of the && being dead does not make the rest of the
615 if (Operator
== tok::ampamp
&& LHS
.Val
== 0)
616 RHSIsLive
= false; // RHS of "0 && x" is dead.
617 else if (Operator
== tok::pipepipe
&& LHS
.Val
!= 0)
618 RHSIsLive
= false; // RHS of "1 || x" is dead.
619 else if (Operator
== tok::question
&& LHS
.Val
== 0)
620 RHSIsLive
= false; // RHS (x) of "0 ? x : y" is dead.
622 RHSIsLive
= ValueLive
;
624 // Consume the operator, remembering the operator's location for reporting.
625 SourceLocation OpLoc
= PeekTok
.getLocation();
626 PP
.LexNonComment(PeekTok
);
628 PPValue
RHS(LHS
.getBitWidth());
629 // Parse the RHS of the operator.
631 if (EvaluateValue(RHS
, PeekTok
, DT
, RHSIsLive
, PP
)) return true;
632 IncludedUndefinedIds
= DT
.IncludedUndefinedIds
;
634 // Remember the precedence of this operator and get the precedence of the
635 // operator immediately to the right of the RHS.
636 unsigned ThisPrec
= PeekPrec
;
637 PeekPrec
= getPrecedence(PeekTok
.getKind());
639 // If this token isn't valid, report the error.
640 if (PeekPrec
== ~0U) {
641 diagnoseUnexpectedOperator(PP
, RHS
, PeekTok
);
645 // Decide whether to include the next binop in this subexpression. For
646 // example, when parsing x+y*z and looking at '*', we want to recursively
647 // handle y*z as a single subexpression. We do this because the precedence
648 // of * is higher than that of +. The only strange case we have to handle
649 // here is for the ?: operator, where the precedence is actually lower than
650 // the LHS of the '?'. The grammar rule is:
652 // conditional-expression ::=
653 // logical-OR-expression ? expression : conditional-expression
654 // where 'expression' is actually comma-expression.
656 if (Operator
== tok::question
)
657 // The RHS of "?" should be maximally consumed as an expression.
658 RHSPrec
= getPrecedence(tok::comma
);
659 else // All others should munch while higher precedence.
660 RHSPrec
= ThisPrec
+1;
662 if (PeekPrec
>= RHSPrec
) {
663 if (EvaluateDirectiveSubExpr(RHS
, RHSPrec
, PeekTok
, RHSIsLive
,
664 IncludedUndefinedIds
, PP
))
666 PeekPrec
= getPrecedence(PeekTok
.getKind());
668 assert(PeekPrec
<= ThisPrec
&& "Recursion didn't work!");
670 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
671 // either operand is unsigned.
672 llvm::APSInt
Res(LHS
.getBitWidth());
674 case tok::question
: // No UAC for x and y in "x ? y : z".
675 case tok::lessless
: // Shift amount doesn't UAC with shift value.
676 case tok::greatergreater
: // Shift amount doesn't UAC with shift value.
677 case tok::comma
: // Comma operands are not subject to UACs.
678 case tok::pipepipe
: // Logical || does not do UACs.
679 case tok::ampamp
: // Logical && does not do UACs.
682 Res
.setIsUnsigned(LHS
.isUnsigned() || RHS
.isUnsigned());
683 // If this just promoted something from signed to unsigned, and if the
684 // value was negative, warn about it.
685 if (ValueLive
&& Res
.isUnsigned()) {
686 if (!LHS
.isUnsigned() && LHS
.Val
.isNegative())
687 PP
.Diag(OpLoc
, diag::warn_pp_convert_to_positive
) << 0
688 << toString(LHS
.Val
, 10, true) + " to " +
689 toString(LHS
.Val
, 10, false)
690 << LHS
.getRange() << RHS
.getRange();
691 if (!RHS
.isUnsigned() && RHS
.Val
.isNegative())
692 PP
.Diag(OpLoc
, diag::warn_pp_convert_to_positive
) << 1
693 << toString(RHS
.Val
, 10, true) + " to " +
694 toString(RHS
.Val
, 10, false)
695 << LHS
.getRange() << RHS
.getRange();
697 LHS
.Val
.setIsUnsigned(Res
.isUnsigned());
698 RHS
.Val
.setIsUnsigned(Res
.isUnsigned());
701 bool Overflow
= false;
703 default: llvm_unreachable("Unknown operator token!");
706 Res
= LHS
.Val
% RHS
.Val
;
707 else if (ValueLive
) {
708 PP
.Diag(OpLoc
, diag::err_pp_remainder_by_zero
)
709 << LHS
.getRange() << RHS
.getRange();
715 if (LHS
.Val
.isSigned())
716 Res
= llvm::APSInt(LHS
.Val
.sdiv_ov(RHS
.Val
, Overflow
), false);
718 Res
= LHS
.Val
/ RHS
.Val
;
719 } else if (ValueLive
) {
720 PP
.Diag(OpLoc
, diag::err_pp_division_by_zero
)
721 << LHS
.getRange() << RHS
.getRange();
728 Res
= llvm::APSInt(LHS
.Val
.smul_ov(RHS
.Val
, Overflow
), false);
730 Res
= LHS
.Val
* RHS
.Val
;
732 case tok::lessless
: {
733 // Determine whether overflow is about to happen.
734 if (LHS
.isUnsigned())
735 Res
= LHS
.Val
.ushl_ov(RHS
.Val
, Overflow
);
737 Res
= llvm::APSInt(LHS
.Val
.sshl_ov(RHS
.Val
, Overflow
), false);
740 case tok::greatergreater
: {
741 // Determine whether overflow is about to happen.
742 unsigned ShAmt
= static_cast<unsigned>(RHS
.Val
.getLimitedValue());
743 if (ShAmt
>= LHS
.getBitWidth()) {
745 ShAmt
= LHS
.getBitWidth()-1;
747 Res
= LHS
.Val
>> ShAmt
;
751 if (LHS
.isUnsigned())
752 Res
= LHS
.Val
+ RHS
.Val
;
754 Res
= llvm::APSInt(LHS
.Val
.sadd_ov(RHS
.Val
, Overflow
), false);
757 if (LHS
.isUnsigned())
758 Res
= LHS
.Val
- RHS
.Val
;
760 Res
= llvm::APSInt(LHS
.Val
.ssub_ov(RHS
.Val
, Overflow
), false);
763 Res
= LHS
.Val
<= RHS
.Val
;
764 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
767 Res
= LHS
.Val
< RHS
.Val
;
768 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
770 case tok::greaterequal
:
771 Res
= LHS
.Val
>= RHS
.Val
;
772 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
775 Res
= LHS
.Val
> RHS
.Val
;
776 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
778 case tok::exclaimequal
:
779 Res
= LHS
.Val
!= RHS
.Val
;
780 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
782 case tok::equalequal
:
783 Res
= LHS
.Val
== RHS
.Val
;
784 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
787 Res
= LHS
.Val
& RHS
.Val
;
790 Res
= LHS
.Val
^ RHS
.Val
;
793 Res
= LHS
.Val
| RHS
.Val
;
796 Res
= (LHS
.Val
!= 0 && RHS
.Val
!= 0);
797 Res
.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
800 Res
= (LHS
.Val
!= 0 || RHS
.Val
!= 0);
801 Res
.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
804 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
805 // if not being evaluated.
806 if (!PP
.getLangOpts().C99
|| ValueLive
)
807 PP
.Diag(OpLoc
, diag::ext_pp_comma_expr
)
808 << LHS
.getRange() << RHS
.getRange();
809 Res
= RHS
.Val
; // LHS = LHS,RHS -> RHS.
811 case tok::question
: {
812 // Parse the : part of the expression.
813 if (PeekTok
.isNot(tok::colon
)) {
814 PP
.Diag(PeekTok
.getLocation(), diag::err_expected
)
815 << tok::colon
<< LHS
.getRange() << RHS
.getRange();
816 PP
.Diag(OpLoc
, diag::note_matching
) << tok::question
;
820 PP
.LexNonComment(PeekTok
);
822 // Evaluate the value after the :.
823 bool AfterColonLive
= ValueLive
&& LHS
.Val
== 0;
824 PPValue
AfterColonVal(LHS
.getBitWidth());
826 if (EvaluateValue(AfterColonVal
, PeekTok
, DT
, AfterColonLive
, PP
))
829 // Parse anything after the : with the same precedence as ?. We allow
830 // things of equal precedence because ?: is right associative.
831 if (EvaluateDirectiveSubExpr(AfterColonVal
, ThisPrec
,
832 PeekTok
, AfterColonLive
,
833 IncludedUndefinedIds
, PP
))
836 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
837 Res
= LHS
.Val
!= 0 ? RHS
.Val
: AfterColonVal
.Val
;
838 RHS
.setEnd(AfterColonVal
.getRange().getEnd());
840 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
841 // either operand is unsigned.
842 Res
.setIsUnsigned(RHS
.isUnsigned() || AfterColonVal
.isUnsigned());
844 // Figure out the precedence of the token after the : part.
845 PeekPrec
= getPrecedence(PeekTok
.getKind());
849 // Don't allow :'s to float around without being part of ?: exprs.
850 PP
.Diag(OpLoc
, diag::err_pp_colon_without_question
)
851 << LHS
.getRange() << RHS
.getRange();
855 // If this operator is live and overflowed, report the issue.
856 if (Overflow
&& ValueLive
)
857 PP
.Diag(OpLoc
, diag::warn_pp_expr_overflow
)
858 << LHS
.getRange() << RHS
.getRange();
860 // Put the result back into 'LHS' for our next iteration.
862 LHS
.setEnd(RHS
.getRange().getEnd());
863 RHS
.setIdentifier(nullptr);
867 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
868 /// may occur after a #if or #elif directive. If the expression is equivalent
869 /// to "!defined(X)" return X in IfNDefMacro.
870 Preprocessor::DirectiveEvalResult
871 Preprocessor::EvaluateDirectiveExpression(IdentifierInfo
*&IfNDefMacro
) {
872 SaveAndRestore
PPDir(ParsingIfOrElifDirective
, true);
873 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
874 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
875 // in which case a directive is undefined behavior. We want macros to be able
876 // to recursively expand in order to get more gcc-list behavior, so we force
877 // DisableMacroExpansion to false and restore it when we're done parsing the
879 bool DisableMacroExpansionAtStartOfDirective
= DisableMacroExpansion
;
880 DisableMacroExpansion
= false;
882 // Peek ahead one token.
886 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
887 unsigned BitWidth
= getTargetInfo().getIntMaxTWidth();
889 PPValue
ResVal(BitWidth
);
891 SourceLocation ExprStartLoc
= SourceMgr
.getExpansionLoc(Tok
.getLocation());
892 if (EvaluateValue(ResVal
, Tok
, DT
, true, *this)) {
893 // Parse error, skip the rest of the macro line.
894 SourceRange ConditionRange
= ExprStartLoc
;
895 if (Tok
.isNot(tok::eod
))
896 ConditionRange
= DiscardUntilEndOfDirective();
898 // Restore 'DisableMacroExpansion'.
899 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
901 // We cannot trust the source range from the value because there was a
902 // parse error. Track the range manually -- the end of the directive is the
903 // end of the condition range.
905 DT
.IncludedUndefinedIds
,
906 {ExprStartLoc
, ConditionRange
.getEnd()}};
909 // If we are at the end of the expression after just parsing a value, there
910 // must be no (unparenthesized) binary operators involved, so we can exit
912 if (Tok
.is(tok::eod
)) {
913 // If the expression we parsed was of the form !defined(macro), return the
914 // macro in IfNDefMacro.
915 if (DT
.State
== DefinedTracker::NotDefinedMacro
)
916 IfNDefMacro
= DT
.TheMacro
;
918 // Restore 'DisableMacroExpansion'.
919 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
920 return {ResVal
.Val
!= 0, DT
.IncludedUndefinedIds
, ResVal
.getRange()};
923 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
924 // operator and the stuff after it.
925 if (EvaluateDirectiveSubExpr(ResVal
, getPrecedence(tok::question
),
926 Tok
, true, DT
.IncludedUndefinedIds
, *this)) {
927 // Parse error, skip the rest of the macro line.
928 if (Tok
.isNot(tok::eod
))
929 DiscardUntilEndOfDirective();
931 // Restore 'DisableMacroExpansion'.
932 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
933 return {false, DT
.IncludedUndefinedIds
, ResVal
.getRange()};
936 // If we aren't at the tok::eod token, something bad happened, like an extra
938 if (Tok
.isNot(tok::eod
)) {
939 Diag(Tok
, diag::err_pp_expected_eol
);
940 DiscardUntilEndOfDirective();
943 // Restore 'DisableMacroExpansion'.
944 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
945 return {ResVal
.Val
!= 0, DT
.IncludedUndefinedIds
, ResVal
.getRange()};