1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Preprocessor::EvaluateDirectiveExpression method,
11 // which parses and evaluates integer constant expressions for #if directives.
13 //===----------------------------------------------------------------------===//
15 // FIXME: implement testing for #assert's.
17 //===----------------------------------------------------------------------===//
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/MacroInfo.h"
21 #include "clang/Lex/LiteralSupport.h"
22 #include "clang/Lex/CodeCompletionHandler.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Lex/LexDiagnostic.h"
25 #include "llvm/ADT/APSInt.h"
26 using namespace clang
;
30 /// PPValue - Represents the value of a subexpression of a preprocessor
31 /// conditional and the source range covered by it.
37 // Default ctor - Construct an 'invalid' PPValue.
38 PPValue(unsigned BitWidth
) : Val(BitWidth
) {}
40 unsigned getBitWidth() const { return Val
.getBitWidth(); }
41 bool isUnsigned() const { return Val
.isUnsigned(); }
43 const SourceRange
&getRange() const { return Range
; }
45 void setRange(SourceLocation L
) { Range
.setBegin(L
); Range
.setEnd(L
); }
46 void setRange(SourceLocation B
, SourceLocation E
) {
47 Range
.setBegin(B
); Range
.setEnd(E
);
49 void setBegin(SourceLocation L
) { Range
.setBegin(L
); }
50 void setEnd(SourceLocation L
) { Range
.setEnd(L
); }
55 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
56 Token
&PeekTok
, bool ValueLive
,
59 /// DefinedTracker - This struct is used while parsing expressions to keep track
60 /// of whether !defined(X) has been seen.
62 /// With this simple scheme, we handle the basic forms:
63 /// !defined(X) and !defined X
64 /// but we also trivially handle (silly) stuff like:
65 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
66 struct DefinedTracker
{
67 /// Each time a Value is evaluated, it returns information about whether the
68 /// parsed value is of the form defined(X), !defined(X) or is something else.
70 DefinedMacro
, // defined(X)
71 NotDefinedMacro
, // !defined(X)
72 Unknown
// Something else.
74 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
75 /// indicates the macro that was checked.
76 IdentifierInfo
*TheMacro
;
79 /// EvaluateDefined - Process a 'defined(sym)' expression.
80 static bool EvaluateDefined(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
81 bool ValueLive
, Preprocessor
&PP
) {
83 Result
.setBegin(PeekTok
.getLocation());
85 // Get the next token, don't expand it.
86 PP
.LexUnexpandedToken(PeekTok
);
88 // Two options, it can either be a pp-identifier or a (.
89 SourceLocation LParenLoc
;
90 if (PeekTok
.is(tok::l_paren
)) {
91 // Found a paren, remember we saw it and skip it.
92 LParenLoc
= PeekTok
.getLocation();
93 PP
.LexUnexpandedToken(PeekTok
);
96 if (PeekTok
.is(tok::code_completion
)) {
97 if (PP
.getCodeCompletionHandler())
98 PP
.getCodeCompletionHandler()->CodeCompleteMacroName(false);
99 PP
.LexUnexpandedToken(PeekTok
);
102 // If we don't have a pp-identifier now, this is an error.
103 if ((II
= PeekTok
.getIdentifierInfo()) == 0) {
104 PP
.Diag(PeekTok
, diag::err_pp_defined_requires_identifier
);
108 // Otherwise, we got an identifier, is it defined to something?
109 Result
.Val
= II
->hasMacroDefinition();
110 Result
.Val
.setIsUnsigned(false); // Result is signed intmax_t.
112 // If there is a macro, mark it used.
113 if (Result
.Val
!= 0 && ValueLive
) {
114 MacroInfo
*Macro
= PP
.getMacroInfo(II
);
115 PP
.markMacroAsUsed(Macro
);
118 // Consume identifier.
119 Result
.setEnd(PeekTok
.getLocation());
120 PP
.LexUnexpandedToken(PeekTok
);
122 // If we are in parens, ensure we have a trailing ).
123 if (LParenLoc
.isValid()) {
124 if (PeekTok
.isNot(tok::r_paren
)) {
125 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_missing_rparen
) << "defined";
126 PP
.Diag(LParenLoc
, diag::note_matching
) << "(";
130 Result
.setEnd(PeekTok
.getLocation());
131 PP
.LexNonComment(PeekTok
);
134 // Success, remember that we saw defined(X).
135 DT
.State
= DefinedTracker::DefinedMacro
;
140 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
141 /// return the computed value in Result. Return true if there was an error
142 /// parsing. This function also returns information about the form of the
143 /// expression in DT. See above for information on what DT means.
145 /// If ValueLive is false, then this value is being evaluated in a context where
146 /// the result is not used. As such, avoid diagnostics that relate to
148 static bool EvaluateValue(PPValue
&Result
, Token
&PeekTok
, DefinedTracker
&DT
,
149 bool ValueLive
, Preprocessor
&PP
) {
150 DT
.State
= DefinedTracker::Unknown
;
152 if (PeekTok
.is(tok::code_completion
)) {
153 if (PP
.getCodeCompletionHandler())
154 PP
.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
155 PP
.LexUnexpandedToken(PeekTok
);
158 // If this token's spelling is a pp-identifier, check to see if it is
159 // 'defined' or if it is a macro. Note that we check here because many
160 // keywords are pp-identifiers, so we can't check the kind.
161 if (IdentifierInfo
*II
= PeekTok
.getIdentifierInfo()) {
162 // Handle "defined X" and "defined(X)".
163 if (II
->isStr("defined"))
164 return(EvaluateDefined(Result
, PeekTok
, DT
, ValueLive
, PP
));
166 // If this identifier isn't 'defined' or one of the special
167 // preprocessor keywords and it wasn't macro expanded, it turns
168 // into a simple 0, unless it is the C++ keyword "true", in which case it
171 PP
.Diag(PeekTok
, diag::warn_pp_undef_identifier
) << II
;
172 Result
.Val
= II
->getTokenID() == tok::kw_true
;
173 Result
.Val
.setIsUnsigned(false); // "0" is signed intmax_t 0.
174 Result
.setRange(PeekTok
.getLocation());
175 PP
.LexNonComment(PeekTok
);
179 switch (PeekTok
.getKind()) {
180 default: // Non-value token.
181 PP
.Diag(PeekTok
, diag::err_pp_expr_bad_token_start_expr
);
185 // If there is no expression, report and exit.
186 PP
.Diag(PeekTok
, diag::err_pp_expected_value_in_expr
);
188 case tok::numeric_constant
: {
189 llvm::SmallString
<64> IntegerBuffer
;
190 bool NumberInvalid
= false;
191 llvm::StringRef Spelling
= PP
.getSpelling(PeekTok
, IntegerBuffer
,
194 return true; // a diagnostic was already reported
196 NumericLiteralParser
Literal(Spelling
.begin(), Spelling
.end(),
197 PeekTok
.getLocation(), PP
);
198 if (Literal
.hadError
)
199 return true; // a diagnostic was already reported.
201 if (Literal
.isFloatingLiteral() || Literal
.isImaginary
) {
202 PP
.Diag(PeekTok
, diag::err_pp_illegal_floating_literal
);
205 assert(Literal
.isIntegerLiteral() && "Unknown ppnumber");
207 // long long is a C99 feature.
208 if (!PP
.getLangOptions().C99
&& !PP
.getLangOptions().CPlusPlus0x
209 && Literal
.isLongLong
)
210 PP
.Diag(PeekTok
, diag::ext_longlong
);
212 // Parse the integer literal into Result.
213 if (Literal
.GetIntegerValue(Result
.Val
)) {
214 // Overflow parsing integer literal.
215 if (ValueLive
) PP
.Diag(PeekTok
, diag::warn_integer_too_large
);
216 Result
.Val
.setIsUnsigned(true);
218 // Set the signedness of the result to match whether there was a U suffix
220 Result
.Val
.setIsUnsigned(Literal
.isUnsigned
);
222 // Detect overflow based on whether the value is signed. If signed
223 // and if the value is too large, emit a warning "integer constant is so
224 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
226 if (!Literal
.isUnsigned
&& Result
.Val
.isNegative()) {
227 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
228 if (ValueLive
&& Literal
.getRadix() != 16)
229 PP
.Diag(PeekTok
, diag::warn_integer_too_large_for_signed
);
230 Result
.Val
.setIsUnsigned(true);
234 // Consume the token.
235 Result
.setRange(PeekTok
.getLocation());
236 PP
.LexNonComment(PeekTok
);
239 case tok::char_constant
: { // 'x'
240 llvm::SmallString
<32> CharBuffer
;
241 bool CharInvalid
= false;
242 llvm::StringRef ThisTok
= PP
.getSpelling(PeekTok
, CharBuffer
, &CharInvalid
);
246 CharLiteralParser
Literal(ThisTok
.begin(), ThisTok
.end(),
247 PeekTok
.getLocation(), PP
);
248 if (Literal
.hadError())
249 return true; // A diagnostic was already emitted.
251 // Character literals are always int or wchar_t, expand to intmax_t.
252 const TargetInfo
&TI
= PP
.getTargetInfo();
254 if (Literal
.isMultiChar())
255 NumBits
= TI
.getIntWidth();
256 else if (Literal
.isWide())
257 NumBits
= TI
.getWCharWidth();
259 NumBits
= TI
.getCharWidth();
262 llvm::APSInt
Val(NumBits
);
264 Val
= Literal
.getValue();
265 // Set the signedness.
266 Val
.setIsUnsigned(!PP
.getLangOptions().CharIsSigned
);
268 if (Result
.Val
.getBitWidth() > Val
.getBitWidth()) {
269 Result
.Val
= Val
.extend(Result
.Val
.getBitWidth());
271 assert(Result
.Val
.getBitWidth() == Val
.getBitWidth() &&
272 "intmax_t smaller than char/wchar_t?");
276 // Consume the token.
277 Result
.setRange(PeekTok
.getLocation());
278 PP
.LexNonComment(PeekTok
);
282 SourceLocation Start
= PeekTok
.getLocation();
283 PP
.LexNonComment(PeekTok
); // Eat the (.
284 // Parse the value and if there are any binary operators involved, parse
286 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
288 // If this is a silly value like (X), which doesn't need parens, check for
290 if (PeekTok
.is(tok::r_paren
)) {
291 // Just use DT unmodified as our result.
293 // Otherwise, we have something like (x+y), and we consumed '(x'.
294 if (EvaluateDirectiveSubExpr(Result
, 1, PeekTok
, ValueLive
, PP
))
297 if (PeekTok
.isNot(tok::r_paren
)) {
298 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expected_rparen
)
299 << Result
.getRange();
300 PP
.Diag(Start
, diag::note_matching
) << "(";
303 DT
.State
= DefinedTracker::Unknown
;
305 Result
.setRange(Start
, PeekTok
.getLocation());
306 PP
.LexNonComment(PeekTok
); // Eat the ).
310 SourceLocation Start
= PeekTok
.getLocation();
311 // Unary plus doesn't modify the value.
312 PP
.LexNonComment(PeekTok
);
313 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
314 Result
.setBegin(Start
);
318 SourceLocation Loc
= PeekTok
.getLocation();
319 PP
.LexNonComment(PeekTok
);
320 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
321 Result
.setBegin(Loc
);
323 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
324 Result
.Val
= -Result
.Val
;
326 // -MININT is the only thing that overflows. Unsigned never overflows.
327 bool Overflow
= !Result
.isUnsigned() && Result
.Val
.isMinSignedValue();
329 // If this operator is live and overflowed, report the issue.
330 if (Overflow
&& ValueLive
)
331 PP
.Diag(Loc
, diag::warn_pp_expr_overflow
) << Result
.getRange();
333 DT
.State
= DefinedTracker::Unknown
;
338 SourceLocation Start
= PeekTok
.getLocation();
339 PP
.LexNonComment(PeekTok
);
340 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
341 Result
.setBegin(Start
);
343 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
344 Result
.Val
= ~Result
.Val
;
345 DT
.State
= DefinedTracker::Unknown
;
350 SourceLocation Start
= PeekTok
.getLocation();
351 PP
.LexNonComment(PeekTok
);
352 if (EvaluateValue(Result
, PeekTok
, DT
, ValueLive
, PP
)) return true;
353 Result
.setBegin(Start
);
354 Result
.Val
= !Result
.Val
;
355 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
356 Result
.Val
.setIsUnsigned(false);
358 if (DT
.State
== DefinedTracker::DefinedMacro
)
359 DT
.State
= DefinedTracker::NotDefinedMacro
;
360 else if (DT
.State
== DefinedTracker::NotDefinedMacro
)
361 DT
.State
= DefinedTracker::DefinedMacro
;
365 // FIXME: Handle #assert
371 /// getPrecedence - Return the precedence of the specified binary operator
372 /// token. This returns:
373 /// ~0 - Invalid token.
374 /// 14 -> 3 - various operators.
376 static unsigned getPrecedence(tok::TokenKind Kind
) {
381 case tok::star
: return 14;
383 case tok::minus
: return 13;
385 case tok::greatergreater
: return 12;
388 case tok::greaterequal
:
389 case tok::greater
: return 11;
390 case tok::exclaimequal
:
391 case tok::equalequal
: return 10;
392 case tok::amp
: return 9;
393 case tok::caret
: return 8;
394 case tok::pipe
: return 7;
395 case tok::ampamp
: return 6;
396 case tok::pipepipe
: return 5;
397 case tok::question
: return 4;
398 case tok::comma
: return 3;
399 case tok::colon
: return 2;
400 case tok::r_paren
: return 0; // Lowest priority, end of expr.
401 case tok::eom
: return 0; // Lowest priority, end of macro.
406 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
407 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
409 /// If ValueLive is false, then this value is being evaluated in a context where
410 /// the result is not used. As such, avoid diagnostics that relate to
411 /// evaluation, such as division by zero warnings.
412 static bool EvaluateDirectiveSubExpr(PPValue
&LHS
, unsigned MinPrec
,
413 Token
&PeekTok
, bool ValueLive
,
415 unsigned PeekPrec
= getPrecedence(PeekTok
.getKind());
416 // If this token isn't valid, report the error.
417 if (PeekPrec
== ~0U) {
418 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expr_bad_token_binop
)
424 // If this token has a lower precedence than we are allowed to parse, return
425 // it so that higher levels of the recursion can parse it.
426 if (PeekPrec
< MinPrec
)
429 tok::TokenKind Operator
= PeekTok
.getKind();
431 // If this is a short-circuiting operator, see if the RHS of the operator is
432 // dead. Note that this cannot just clobber ValueLive. Consider
433 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
434 // this example, the RHS of the && being dead does not make the rest of the
437 if (Operator
== tok::ampamp
&& LHS
.Val
== 0)
438 RHSIsLive
= false; // RHS of "0 && x" is dead.
439 else if (Operator
== tok::pipepipe
&& LHS
.Val
!= 0)
440 RHSIsLive
= false; // RHS of "1 || x" is dead.
441 else if (Operator
== tok::question
&& LHS
.Val
== 0)
442 RHSIsLive
= false; // RHS (x) of "0 ? x : y" is dead.
444 RHSIsLive
= ValueLive
;
446 // Consume the operator, remembering the operator's location for reporting.
447 SourceLocation OpLoc
= PeekTok
.getLocation();
448 PP
.LexNonComment(PeekTok
);
450 PPValue
RHS(LHS
.getBitWidth());
451 // Parse the RHS of the operator.
453 if (EvaluateValue(RHS
, PeekTok
, DT
, RHSIsLive
, PP
)) return true;
455 // Remember the precedence of this operator and get the precedence of the
456 // operator immediately to the right of the RHS.
457 unsigned ThisPrec
= PeekPrec
;
458 PeekPrec
= getPrecedence(PeekTok
.getKind());
460 // If this token isn't valid, report the error.
461 if (PeekPrec
== ~0U) {
462 PP
.Diag(PeekTok
.getLocation(), diag::err_pp_expr_bad_token_binop
)
467 // Decide whether to include the next binop in this subexpression. For
468 // example, when parsing x+y*z and looking at '*', we want to recursively
469 // handle y*z as a single subexpression. We do this because the precedence
470 // of * is higher than that of +. The only strange case we have to handle
471 // here is for the ?: operator, where the precedence is actually lower than
472 // the LHS of the '?'. The grammar rule is:
474 // conditional-expression ::=
475 // logical-OR-expression ? expression : conditional-expression
476 // where 'expression' is actually comma-expression.
478 if (Operator
== tok::question
)
479 // The RHS of "?" should be maximally consumed as an expression.
480 RHSPrec
= getPrecedence(tok::comma
);
481 else // All others should munch while higher precedence.
482 RHSPrec
= ThisPrec
+1;
484 if (PeekPrec
>= RHSPrec
) {
485 if (EvaluateDirectiveSubExpr(RHS
, RHSPrec
, PeekTok
, RHSIsLive
, PP
))
487 PeekPrec
= getPrecedence(PeekTok
.getKind());
489 assert(PeekPrec
<= ThisPrec
&& "Recursion didn't work!");
491 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
492 // either operand is unsigned.
493 llvm::APSInt
Res(LHS
.getBitWidth());
495 case tok::question
: // No UAC for x and y in "x ? y : z".
496 case tok::lessless
: // Shift amount doesn't UAC with shift value.
497 case tok::greatergreater
: // Shift amount doesn't UAC with shift value.
498 case tok::comma
: // Comma operands are not subject to UACs.
499 case tok::pipepipe
: // Logical || does not do UACs.
500 case tok::ampamp
: // Logical && does not do UACs.
503 Res
.setIsUnsigned(LHS
.isUnsigned()|RHS
.isUnsigned());
504 // If this just promoted something from signed to unsigned, and if the
505 // value was negative, warn about it.
506 if (ValueLive
&& Res
.isUnsigned()) {
507 if (!LHS
.isUnsigned() && LHS
.Val
.isNegative())
508 PP
.Diag(OpLoc
, diag::warn_pp_convert_lhs_to_positive
)
509 << LHS
.Val
.toString(10, true) + " to " +
510 LHS
.Val
.toString(10, false)
511 << LHS
.getRange() << RHS
.getRange();
512 if (!RHS
.isUnsigned() && RHS
.Val
.isNegative())
513 PP
.Diag(OpLoc
, diag::warn_pp_convert_rhs_to_positive
)
514 << RHS
.Val
.toString(10, true) + " to " +
515 RHS
.Val
.toString(10, false)
516 << LHS
.getRange() << RHS
.getRange();
518 LHS
.Val
.setIsUnsigned(Res
.isUnsigned());
519 RHS
.Val
.setIsUnsigned(Res
.isUnsigned());
522 bool Overflow
= false;
524 default: assert(0 && "Unknown operator token!");
527 Res
= LHS
.Val
% RHS
.Val
;
528 else if (ValueLive
) {
529 PP
.Diag(OpLoc
, diag::err_pp_remainder_by_zero
)
530 << LHS
.getRange() << RHS
.getRange();
536 if (LHS
.Val
.isSigned())
537 Res
= llvm::APSInt(LHS
.Val
.sdiv_ov(RHS
.Val
, Overflow
), false);
539 Res
= LHS
.Val
/ RHS
.Val
;
540 } else if (ValueLive
) {
541 PP
.Diag(OpLoc
, diag::err_pp_division_by_zero
)
542 << LHS
.getRange() << RHS
.getRange();
549 Res
= llvm::APSInt(LHS
.Val
.smul_ov(RHS
.Val
, Overflow
), false);
551 Res
= LHS
.Val
* RHS
.Val
;
553 case tok::lessless
: {
554 // Determine whether overflow is about to happen.
555 unsigned ShAmt
= static_cast<unsigned>(RHS
.Val
.getLimitedValue());
556 if (LHS
.isUnsigned()) {
557 Overflow
= ShAmt
>= LHS
.Val
.getBitWidth();
559 ShAmt
= LHS
.Val
.getBitWidth()-1;
560 Res
= LHS
.Val
<< ShAmt
;
562 Res
= llvm::APSInt(LHS
.Val
.sshl_ov(ShAmt
, Overflow
), false);
566 case tok::greatergreater
: {
567 // Determine whether overflow is about to happen.
568 unsigned ShAmt
= static_cast<unsigned>(RHS
.Val
.getLimitedValue());
569 if (ShAmt
>= LHS
.getBitWidth())
570 Overflow
= true, ShAmt
= LHS
.getBitWidth()-1;
571 Res
= LHS
.Val
>> ShAmt
;
575 if (LHS
.isUnsigned())
576 Res
= LHS
.Val
+ RHS
.Val
;
578 Res
= llvm::APSInt(LHS
.Val
.sadd_ov(RHS
.Val
, Overflow
), false);
581 if (LHS
.isUnsigned())
582 Res
= LHS
.Val
- RHS
.Val
;
584 Res
= llvm::APSInt(LHS
.Val
.ssub_ov(RHS
.Val
, Overflow
), false);
587 Res
= LHS
.Val
<= RHS
.Val
;
588 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
591 Res
= LHS
.Val
< RHS
.Val
;
592 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
594 case tok::greaterequal
:
595 Res
= LHS
.Val
>= RHS
.Val
;
596 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
599 Res
= LHS
.Val
> RHS
.Val
;
600 Res
.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
602 case tok::exclaimequal
:
603 Res
= LHS
.Val
!= RHS
.Val
;
604 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
606 case tok::equalequal
:
607 Res
= LHS
.Val
== RHS
.Val
;
608 Res
.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
611 Res
= LHS
.Val
& RHS
.Val
;
614 Res
= LHS
.Val
^ RHS
.Val
;
617 Res
= LHS
.Val
| RHS
.Val
;
620 Res
= (LHS
.Val
!= 0 && RHS
.Val
!= 0);
621 Res
.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
624 Res
= (LHS
.Val
!= 0 || RHS
.Val
!= 0);
625 Res
.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
628 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
629 // if not being evaluated.
630 if (!PP
.getLangOptions().C99
|| ValueLive
)
631 PP
.Diag(OpLoc
, diag::ext_pp_comma_expr
)
632 << LHS
.getRange() << RHS
.getRange();
633 Res
= RHS
.Val
; // LHS = LHS,RHS -> RHS.
635 case tok::question
: {
636 // Parse the : part of the expression.
637 if (PeekTok
.isNot(tok::colon
)) {
638 PP
.Diag(PeekTok
.getLocation(), diag::err_expected_colon
)
639 << LHS
.getRange(), RHS
.getRange();
640 PP
.Diag(OpLoc
, diag::note_matching
) << "?";
644 PP
.LexNonComment(PeekTok
);
646 // Evaluate the value after the :.
647 bool AfterColonLive
= ValueLive
&& LHS
.Val
== 0;
648 PPValue
AfterColonVal(LHS
.getBitWidth());
650 if (EvaluateValue(AfterColonVal
, PeekTok
, DT
, AfterColonLive
, PP
))
653 // Parse anything after the : with the same precedence as ?. We allow
654 // things of equal precedence because ?: is right associative.
655 if (EvaluateDirectiveSubExpr(AfterColonVal
, ThisPrec
,
656 PeekTok
, AfterColonLive
, PP
))
659 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
660 Res
= LHS
.Val
!= 0 ? RHS
.Val
: AfterColonVal
.Val
;
661 RHS
.setEnd(AfterColonVal
.getRange().getEnd());
663 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
664 // either operand is unsigned.
665 Res
.setIsUnsigned(RHS
.isUnsigned() | AfterColonVal
.isUnsigned());
667 // Figure out the precedence of the token after the : part.
668 PeekPrec
= getPrecedence(PeekTok
.getKind());
672 // Don't allow :'s to float around without being part of ?: exprs.
673 PP
.Diag(OpLoc
, diag::err_pp_colon_without_question
)
674 << LHS
.getRange() << RHS
.getRange();
678 // If this operator is live and overflowed, report the issue.
679 if (Overflow
&& ValueLive
)
680 PP
.Diag(OpLoc
, diag::warn_pp_expr_overflow
)
681 << LHS
.getRange() << RHS
.getRange();
683 // Put the result back into 'LHS' for our next iteration.
685 LHS
.setEnd(RHS
.getRange().getEnd());
691 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
692 /// may occur after a #if or #elif directive. If the expression is equivalent
693 /// to "!defined(X)" return X in IfNDefMacro.
695 EvaluateDirectiveExpression(IdentifierInfo
*&IfNDefMacro
) {
696 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
697 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
698 // in which case a directive is undefined behavior. We want macros to be able
699 // to recursively expand in order to get more gcc-list behavior, so we force
700 // DisableMacroExpansion to false and restore it when we're done parsing the
702 bool DisableMacroExpansionAtStartOfDirective
= DisableMacroExpansion
;
703 DisableMacroExpansion
= false;
705 // Peek ahead one token.
709 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
710 unsigned BitWidth
= getTargetInfo().getIntMaxTWidth();
712 PPValue
ResVal(BitWidth
);
714 if (EvaluateValue(ResVal
, Tok
, DT
, true, *this)) {
715 // Parse error, skip the rest of the macro line.
716 if (Tok
.isNot(tok::eom
))
717 DiscardUntilEndOfDirective();
719 // Restore 'DisableMacroExpansion'.
720 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
724 // If we are at the end of the expression after just parsing a value, there
725 // must be no (unparenthesized) binary operators involved, so we can exit
727 if (Tok
.is(tok::eom
)) {
728 // If the expression we parsed was of the form !defined(macro), return the
729 // macro in IfNDefMacro.
730 if (DT
.State
== DefinedTracker::NotDefinedMacro
)
731 IfNDefMacro
= DT
.TheMacro
;
733 // Restore 'DisableMacroExpansion'.
734 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
735 return ResVal
.Val
!= 0;
738 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
739 // operator and the stuff after it.
740 if (EvaluateDirectiveSubExpr(ResVal
, getPrecedence(tok::question
),
742 // Parse error, skip the rest of the macro line.
743 if (Tok
.isNot(tok::eom
))
744 DiscardUntilEndOfDirective();
746 // Restore 'DisableMacroExpansion'.
747 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
751 // If we aren't at the tok::eom token, something bad happened, like an extra
753 if (Tok
.isNot(tok::eom
)) {
754 Diag(Tok
, diag::err_pp_expected_eol
);
755 DiscardUntilEndOfDirective();
758 // Restore 'DisableMacroExpansion'.
759 DisableMacroExpansion
= DisableMacroExpansionAtStartOfDirective
;
760 return ResVal
.Val
!= 0;