1 //===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Preprocessor::EvaluateDirectiveExpression method,
11 // which parses and evaluates integer constant expressions for #if directives.
12 //
13 //===----------------------------------------------------------------------===//
14 //
15 // FIXME: implement testing for #assert's.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Lex/CodeCompletionHandler.h"
22 #include "clang/Lex/LexDiagnostic.h"
23 #include "clang/Lex/LiteralSupport.h"
24 #include "clang/Lex/MacroInfo.h"
25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SaveAndRestore.h"
28 using namespace clang;
29
30 namespace {
31
32 /// PPValue - Represents the value of a subexpression of a preprocessor
33 /// conditional and the source range covered by it.
34 class PPValue {
35 SourceRange Range;
36 public:
37 llvm::APSInt Val;
38
39 // Default ctor - Construct an 'invalid' PPValue.
PPValue(unsigned BitWidth)40 PPValue(unsigned BitWidth) : Val(BitWidth) {}
41
getBitWidth() const42 unsigned getBitWidth() const { return Val.getBitWidth(); }
isUnsigned() const43 bool isUnsigned() const { return Val.isUnsigned(); }
44
getRange() const45 SourceRange getRange() const { return Range; }
46
setRange(SourceLocation L)47 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
setRange(SourceLocation B,SourceLocation E)48 void setRange(SourceLocation B, SourceLocation E) {
49 Range.setBegin(B); Range.setEnd(E);
50 }
setBegin(SourceLocation L)51 void setBegin(SourceLocation L) { Range.setBegin(L); }
setEnd(SourceLocation L)52 void setEnd(SourceLocation L) { Range.setEnd(L); }
53 };
54
55 }
56
57 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
58 Token &PeekTok, bool ValueLive,
59 Preprocessor &PP);
60
61 /// DefinedTracker - This struct is used while parsing expressions to keep track
62 /// of whether !defined(X) has been seen.
63 ///
64 /// With this simple scheme, we handle the basic forms:
65 /// !defined(X) and !defined X
66 /// but we also trivially handle (silly) stuff like:
67 /// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
68 struct DefinedTracker {
69 /// Each time a Value is evaluated, it returns information about whether the
70 /// parsed value is of the form defined(X), !defined(X) or is something else.
71 enum TrackerState {
72 DefinedMacro, // defined(X)
73 NotDefinedMacro, // !defined(X)
74 Unknown // Something else.
75 } State;
76 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
77 /// indicates the macro that was checked.
78 IdentifierInfo *TheMacro;
79 };
80
81 /// EvaluateDefined - Process a 'defined(sym)' expression.
EvaluateDefined(PPValue & Result,Token & PeekTok,DefinedTracker & DT,bool ValueLive,Preprocessor & PP)82 static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
84 SourceLocation beginLoc(PeekTok.getLocation());
85 Result.setBegin(beginLoc);
86
87 // Get the next token, don't expand it.
88 PP.LexUnexpandedNonComment(PeekTok);
89
90 // Two options, it can either be a pp-identifier or a (.
91 SourceLocation LParenLoc;
92 if (PeekTok.is(tok::l_paren)) {
93 // Found a paren, remember we saw it and skip it.
94 LParenLoc = PeekTok.getLocation();
95 PP.LexUnexpandedNonComment(PeekTok);
96 }
97
98 if (PeekTok.is(tok::code_completion)) {
99 if (PP.getCodeCompletionHandler())
100 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
101 PP.setCodeCompletionReached();
102 PP.LexUnexpandedNonComment(PeekTok);
103 }
104
105 // If we don't have a pp-identifier now, this is an error.
106 if (PP.CheckMacroName(PeekTok, MU_Other))
107 return true;
108
109 // Otherwise, we got an identifier, is it defined to something?
110 IdentifierInfo *II = PeekTok.getIdentifierInfo();
111 MacroDefinition Macro = PP.getMacroDefinition(II);
112 Result.Val = !!Macro;
113 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
114
115 // If there is a macro, mark it used.
116 if (Result.Val != 0 && ValueLive)
117 PP.markMacroAsUsed(Macro.getMacroInfo());
118
119 // Save macro token for callback.
120 Token macroToken(PeekTok);
121
122 // If we are in parens, ensure we have a trailing ).
123 if (LParenLoc.isValid()) {
124 // Consume identifier.
125 Result.setEnd(PeekTok.getLocation());
126 PP.LexUnexpandedNonComment(PeekTok);
127
128 if (PeekTok.isNot(tok::r_paren)) {
129 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
130 << "'defined'" << tok::r_paren;
131 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
132 return true;
133 }
134 // Consume the ).
135 Result.setEnd(PeekTok.getLocation());
136 PP.LexNonComment(PeekTok);
137 } else {
138 // Consume identifier.
139 Result.setEnd(PeekTok.getLocation());
140 PP.LexNonComment(PeekTok);
141 }
142
143 // Invoke the 'defined' callback.
144 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
145 Callbacks->Defined(macroToken, Macro,
146 SourceRange(beginLoc, PeekTok.getLocation()));
147 }
148
149 // Success, remember that we saw defined(X).
150 DT.State = DefinedTracker::DefinedMacro;
151 DT.TheMacro = II;
152 return false;
153 }
154
155 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
156 /// return the computed value in Result. Return true if there was an error
157 /// parsing. This function also returns information about the form of the
158 /// expression in DT. See above for information on what DT means.
159 ///
160 /// If ValueLive is false, then this value is being evaluated in a context where
161 /// the result is not used. As such, avoid diagnostics that relate to
162 /// evaluation.
EvaluateValue(PPValue & Result,Token & PeekTok,DefinedTracker & DT,bool ValueLive,Preprocessor & PP)163 static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
164 bool ValueLive, Preprocessor &PP) {
165 DT.State = DefinedTracker::Unknown;
166
167 if (PeekTok.is(tok::code_completion)) {
168 if (PP.getCodeCompletionHandler())
169 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
170 PP.setCodeCompletionReached();
171 PP.LexNonComment(PeekTok);
172 }
173
174 // If this token's spelling is a pp-identifier, check to see if it is
175 // 'defined' or if it is a macro. Note that we check here because many
176 // keywords are pp-identifiers, so we can't check the kind.
177 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
178 // Handle "defined X" and "defined(X)".
179 if (II->isStr("defined"))
180 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
181
182 // If this identifier isn't 'defined' or one of the special
183 // preprocessor keywords and it wasn't macro expanded, it turns
184 // into a simple 0, unless it is the C++ keyword "true", in which case it
185 // turns into "1".
186 if (ValueLive &&
187 II->getTokenID() != tok::kw_true &&
188 II->getTokenID() != tok::kw_false)
189 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
190 Result.Val = II->getTokenID() == tok::kw_true;
191 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
192 Result.setRange(PeekTok.getLocation());
193 PP.LexNonComment(PeekTok);
194 return false;
195 }
196
197 switch (PeekTok.getKind()) {
198 default: // Non-value token.
199 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
200 return true;
201 case tok::eod:
202 case tok::r_paren:
203 // If there is no expression, report and exit.
204 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
205 return true;
206 case tok::numeric_constant: {
207 SmallString<64> IntegerBuffer;
208 bool NumberInvalid = false;
209 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
210 &NumberInvalid);
211 if (NumberInvalid)
212 return true; // a diagnostic was already reported
213
214 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
215 if (Literal.hadError)
216 return true; // a diagnostic was already reported.
217
218 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
219 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
220 return true;
221 }
222 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
223
224 // Complain about, and drop, any ud-suffix.
225 if (Literal.hasUDSuffix())
226 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
227
228 // 'long long' is a C99 or C++11 feature.
229 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
230 if (PP.getLangOpts().CPlusPlus)
231 PP.Diag(PeekTok,
232 PP.getLangOpts().CPlusPlus11 ?
233 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
234 else
235 PP.Diag(PeekTok, diag::ext_c99_longlong);
236 }
237
238 // Parse the integer literal into Result.
239 if (Literal.GetIntegerValue(Result.Val)) {
240 // Overflow parsing integer literal.
241 if (ValueLive)
242 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
243 << /* Unsigned */ 1;
244 Result.Val.setIsUnsigned(true);
245 } else {
246 // Set the signedness of the result to match whether there was a U suffix
247 // or not.
248 Result.Val.setIsUnsigned(Literal.isUnsigned);
249
250 // Detect overflow based on whether the value is signed. If signed
251 // and if the value is too large, emit a warning "integer constant is so
252 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
253 // is 64-bits.
254 if (!Literal.isUnsigned && Result.Val.isNegative()) {
255 // Octal, hexadecimal, and binary literals are implicitly unsigned if
256 // the value does not fit into a signed integer type.
257 if (ValueLive && Literal.getRadix() == 10)
258 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
259 Result.Val.setIsUnsigned(true);
260 }
261 }
262
263 // Consume the token.
264 Result.setRange(PeekTok.getLocation());
265 PP.LexNonComment(PeekTok);
266 return false;
267 }
268 case tok::char_constant: // 'x'
269 case tok::wide_char_constant: // L'x'
270 case tok::utf8_char_constant: // u8'x'
271 case tok::utf16_char_constant: // u'x'
272 case tok::utf32_char_constant: { // U'x'
273 // Complain about, and drop, any ud-suffix.
274 if (PeekTok.hasUDSuffix())
275 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
276
277 SmallString<32> CharBuffer;
278 bool CharInvalid = false;
279 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
280 if (CharInvalid)
281 return true;
282
283 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
284 PeekTok.getLocation(), PP, PeekTok.getKind());
285 if (Literal.hadError())
286 return true; // A diagnostic was already emitted.
287
288 // Character literals are always int or wchar_t, expand to intmax_t.
289 const TargetInfo &TI = PP.getTargetInfo();
290 unsigned NumBits;
291 if (Literal.isMultiChar())
292 NumBits = TI.getIntWidth();
293 else if (Literal.isWide())
294 NumBits = TI.getWCharWidth();
295 else if (Literal.isUTF16())
296 NumBits = TI.getChar16Width();
297 else if (Literal.isUTF32())
298 NumBits = TI.getChar32Width();
299 else
300 NumBits = TI.getCharWidth();
301
302 // Set the width.
303 llvm::APSInt Val(NumBits);
304 // Set the value.
305 Val = Literal.getValue();
306 // Set the signedness. UTF-16 and UTF-32 are always unsigned
307 if (Literal.isWide())
308 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
309 else if (!Literal.isUTF16() && !Literal.isUTF32())
310 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
311
312 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
313 Result.Val = Val.extend(Result.Val.getBitWidth());
314 } else {
315 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
316 "intmax_t smaller than char/wchar_t?");
317 Result.Val = Val;
318 }
319
320 // Consume the token.
321 Result.setRange(PeekTok.getLocation());
322 PP.LexNonComment(PeekTok);
323 return false;
324 }
325 case tok::l_paren: {
326 SourceLocation Start = PeekTok.getLocation();
327 PP.LexNonComment(PeekTok); // Eat the (.
328 // Parse the value and if there are any binary operators involved, parse
329 // them.
330 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
331
332 // If this is a silly value like (X), which doesn't need parens, check for
333 // !(defined X).
334 if (PeekTok.is(tok::r_paren)) {
335 // Just use DT unmodified as our result.
336 } else {
337 // Otherwise, we have something like (x+y), and we consumed '(x'.
338 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
339 return true;
340
341 if (PeekTok.isNot(tok::r_paren)) {
342 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
343 << Result.getRange();
344 PP.Diag(Start, diag::note_matching) << tok::l_paren;
345 return true;
346 }
347 DT.State = DefinedTracker::Unknown;
348 }
349 Result.setRange(Start, PeekTok.getLocation());
350 PP.LexNonComment(PeekTok); // Eat the ).
351 return false;
352 }
353 case tok::plus: {
354 SourceLocation Start = PeekTok.getLocation();
355 // Unary plus doesn't modify the value.
356 PP.LexNonComment(PeekTok);
357 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
358 Result.setBegin(Start);
359 return false;
360 }
361 case tok::minus: {
362 SourceLocation Loc = PeekTok.getLocation();
363 PP.LexNonComment(PeekTok);
364 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
365 Result.setBegin(Loc);
366
367 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
368 Result.Val = -Result.Val;
369
370 // -MININT is the only thing that overflows. Unsigned never overflows.
371 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
372
373 // If this operator is live and overflowed, report the issue.
374 if (Overflow && ValueLive)
375 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
376
377 DT.State = DefinedTracker::Unknown;
378 return false;
379 }
380
381 case tok::tilde: {
382 SourceLocation Start = PeekTok.getLocation();
383 PP.LexNonComment(PeekTok);
384 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
385 Result.setBegin(Start);
386
387 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
388 Result.Val = ~Result.Val;
389 DT.State = DefinedTracker::Unknown;
390 return false;
391 }
392
393 case tok::exclaim: {
394 SourceLocation Start = PeekTok.getLocation();
395 PP.LexNonComment(PeekTok);
396 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
397 Result.setBegin(Start);
398 Result.Val = !Result.Val;
399 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
400 Result.Val.setIsUnsigned(false);
401
402 if (DT.State == DefinedTracker::DefinedMacro)
403 DT.State = DefinedTracker::NotDefinedMacro;
404 else if (DT.State == DefinedTracker::NotDefinedMacro)
405 DT.State = DefinedTracker::DefinedMacro;
406 return false;
407 }
408
409 // FIXME: Handle #assert
410 }
411 }
412
413
414
415 /// getPrecedence - Return the precedence of the specified binary operator
416 /// token. This returns:
417 /// ~0 - Invalid token.
418 /// 14 -> 3 - various operators.
419 /// 0 - 'eod' or ')'
getPrecedence(tok::TokenKind Kind)420 static unsigned getPrecedence(tok::TokenKind Kind) {
421 switch (Kind) {
422 default: return ~0U;
423 case tok::percent:
424 case tok::slash:
425 case tok::star: return 14;
426 case tok::plus:
427 case tok::minus: return 13;
428 case tok::lessless:
429 case tok::greatergreater: return 12;
430 case tok::lessequal:
431 case tok::less:
432 case tok::greaterequal:
433 case tok::greater: return 11;
434 case tok::exclaimequal:
435 case tok::equalequal: return 10;
436 case tok::amp: return 9;
437 case tok::caret: return 8;
438 case tok::pipe: return 7;
439 case tok::ampamp: return 6;
440 case tok::pipepipe: return 5;
441 case tok::question: return 4;
442 case tok::comma: return 3;
443 case tok::colon: return 2;
444 case tok::r_paren: return 0;// Lowest priority, end of expr.
445 case tok::eod: return 0;// Lowest priority, end of directive.
446 }
447 }
448
449
450 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
451 /// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
452 ///
453 /// If ValueLive is false, then this value is being evaluated in a context where
454 /// the result is not used. As such, avoid diagnostics that relate to
455 /// evaluation, such as division by zero warnings.
EvaluateDirectiveSubExpr(PPValue & LHS,unsigned MinPrec,Token & PeekTok,bool ValueLive,Preprocessor & PP)456 static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
457 Token &PeekTok, bool ValueLive,
458 Preprocessor &PP) {
459 unsigned 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)
463 << LHS.getRange();
464 return true;
465 }
466
467 while (1) {
468 // If this token has a lower precedence than we are allowed to parse, return
469 // it so that higher levels of the recursion can parse it.
470 if (PeekPrec < MinPrec)
471 return false;
472
473 tok::TokenKind Operator = PeekTok.getKind();
474
475 // If this is a short-circuiting operator, see if the RHS of the operator is
476 // dead. Note that this cannot just clobber ValueLive. Consider
477 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
478 // this example, the RHS of the && being dead does not make the rest of the
479 // expr dead.
480 bool RHSIsLive;
481 if (Operator == tok::ampamp && LHS.Val == 0)
482 RHSIsLive = false; // RHS of "0 && x" is dead.
483 else if (Operator == tok::pipepipe && LHS.Val != 0)
484 RHSIsLive = false; // RHS of "1 || x" is dead.
485 else if (Operator == tok::question && LHS.Val == 0)
486 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
487 else
488 RHSIsLive = ValueLive;
489
490 // Consume the operator, remembering the operator's location for reporting.
491 SourceLocation OpLoc = PeekTok.getLocation();
492 PP.LexNonComment(PeekTok);
493
494 PPValue RHS(LHS.getBitWidth());
495 // Parse the RHS of the operator.
496 DefinedTracker DT;
497 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
498
499 // Remember the precedence of this operator and get the precedence of the
500 // operator immediately to the right of the RHS.
501 unsigned ThisPrec = PeekPrec;
502 PeekPrec = getPrecedence(PeekTok.getKind());
503
504 // If this token isn't valid, report the error.
505 if (PeekPrec == ~0U) {
506 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
507 << RHS.getRange();
508 return true;
509 }
510
511 // Decide whether to include the next binop in this subexpression. For
512 // example, when parsing x+y*z and looking at '*', we want to recursively
513 // handle y*z as a single subexpression. We do this because the precedence
514 // of * is higher than that of +. The only strange case we have to handle
515 // here is for the ?: operator, where the precedence is actually lower than
516 // the LHS of the '?'. The grammar rule is:
517 //
518 // conditional-expression ::=
519 // logical-OR-expression ? expression : conditional-expression
520 // where 'expression' is actually comma-expression.
521 unsigned RHSPrec;
522 if (Operator == tok::question)
523 // The RHS of "?" should be maximally consumed as an expression.
524 RHSPrec = getPrecedence(tok::comma);
525 else // All others should munch while higher precedence.
526 RHSPrec = ThisPrec+1;
527
528 if (PeekPrec >= RHSPrec) {
529 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
530 return true;
531 PeekPrec = getPrecedence(PeekTok.getKind());
532 }
533 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
534
535 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
536 // either operand is unsigned.
537 llvm::APSInt Res(LHS.getBitWidth());
538 switch (Operator) {
539 case tok::question: // No UAC for x and y in "x ? y : z".
540 case tok::lessless: // Shift amount doesn't UAC with shift value.
541 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
542 case tok::comma: // Comma operands are not subject to UACs.
543 case tok::pipepipe: // Logical || does not do UACs.
544 case tok::ampamp: // Logical && does not do UACs.
545 break; // No UAC
546 default:
547 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
548 // If this just promoted something from signed to unsigned, and if the
549 // value was negative, warn about it.
550 if (ValueLive && Res.isUnsigned()) {
551 if (!LHS.isUnsigned() && LHS.Val.isNegative())
552 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
553 << LHS.Val.toString(10, true) + " to " +
554 LHS.Val.toString(10, false)
555 << LHS.getRange() << RHS.getRange();
556 if (!RHS.isUnsigned() && RHS.Val.isNegative())
557 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
558 << RHS.Val.toString(10, true) + " to " +
559 RHS.Val.toString(10, false)
560 << LHS.getRange() << RHS.getRange();
561 }
562 LHS.Val.setIsUnsigned(Res.isUnsigned());
563 RHS.Val.setIsUnsigned(Res.isUnsigned());
564 }
565
566 bool Overflow = false;
567 switch (Operator) {
568 default: llvm_unreachable("Unknown operator token!");
569 case tok::percent:
570 if (RHS.Val != 0)
571 Res = LHS.Val % RHS.Val;
572 else if (ValueLive) {
573 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
574 << LHS.getRange() << RHS.getRange();
575 return true;
576 }
577 break;
578 case tok::slash:
579 if (RHS.Val != 0) {
580 if (LHS.Val.isSigned())
581 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
582 else
583 Res = LHS.Val / RHS.Val;
584 } else if (ValueLive) {
585 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
586 << LHS.getRange() << RHS.getRange();
587 return true;
588 }
589 break;
590
591 case tok::star:
592 if (Res.isSigned())
593 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
594 else
595 Res = LHS.Val * RHS.Val;
596 break;
597 case tok::lessless: {
598 // Determine whether overflow is about to happen.
599 if (LHS.isUnsigned())
600 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
601 else
602 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
603 break;
604 }
605 case tok::greatergreater: {
606 // Determine whether overflow is about to happen.
607 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
608 if (ShAmt >= LHS.getBitWidth())
609 Overflow = true, ShAmt = LHS.getBitWidth()-1;
610 Res = LHS.Val >> ShAmt;
611 break;
612 }
613 case tok::plus:
614 if (LHS.isUnsigned())
615 Res = LHS.Val + RHS.Val;
616 else
617 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
618 break;
619 case tok::minus:
620 if (LHS.isUnsigned())
621 Res = LHS.Val - RHS.Val;
622 else
623 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
624 break;
625 case tok::lessequal:
626 Res = LHS.Val <= RHS.Val;
627 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
628 break;
629 case tok::less:
630 Res = LHS.Val < RHS.Val;
631 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
632 break;
633 case tok::greaterequal:
634 Res = LHS.Val >= RHS.Val;
635 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
636 break;
637 case tok::greater:
638 Res = LHS.Val > RHS.Val;
639 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
640 break;
641 case tok::exclaimequal:
642 Res = LHS.Val != RHS.Val;
643 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
644 break;
645 case tok::equalequal:
646 Res = LHS.Val == RHS.Val;
647 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
648 break;
649 case tok::amp:
650 Res = LHS.Val & RHS.Val;
651 break;
652 case tok::caret:
653 Res = LHS.Val ^ RHS.Val;
654 break;
655 case tok::pipe:
656 Res = LHS.Val | RHS.Val;
657 break;
658 case tok::ampamp:
659 Res = (LHS.Val != 0 && RHS.Val != 0);
660 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
661 break;
662 case tok::pipepipe:
663 Res = (LHS.Val != 0 || RHS.Val != 0);
664 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
665 break;
666 case tok::comma:
667 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
668 // if not being evaluated.
669 if (!PP.getLangOpts().C99 || ValueLive)
670 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
671 << LHS.getRange() << RHS.getRange();
672 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
673 break;
674 case tok::question: {
675 // Parse the : part of the expression.
676 if (PeekTok.isNot(tok::colon)) {
677 PP.Diag(PeekTok.getLocation(), diag::err_expected)
678 << tok::colon << LHS.getRange() << RHS.getRange();
679 PP.Diag(OpLoc, diag::note_matching) << tok::question;
680 return true;
681 }
682 // Consume the :.
683 PP.LexNonComment(PeekTok);
684
685 // Evaluate the value after the :.
686 bool AfterColonLive = ValueLive && LHS.Val == 0;
687 PPValue AfterColonVal(LHS.getBitWidth());
688 DefinedTracker DT;
689 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
690 return true;
691
692 // Parse anything after the : with the same precedence as ?. We allow
693 // things of equal precedence because ?: is right associative.
694 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
695 PeekTok, AfterColonLive, PP))
696 return true;
697
698 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
699 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
700 RHS.setEnd(AfterColonVal.getRange().getEnd());
701
702 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
703 // either operand is unsigned.
704 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
705
706 // Figure out the precedence of the token after the : part.
707 PeekPrec = getPrecedence(PeekTok.getKind());
708 break;
709 }
710 case tok::colon:
711 // Don't allow :'s to float around without being part of ?: exprs.
712 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
713 << LHS.getRange() << RHS.getRange();
714 return true;
715 }
716
717 // If this operator is live and overflowed, report the issue.
718 if (Overflow && ValueLive)
719 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
720 << LHS.getRange() << RHS.getRange();
721
722 // Put the result back into 'LHS' for our next iteration.
723 LHS.Val = Res;
724 LHS.setEnd(RHS.getRange().getEnd());
725 }
726 }
727
728 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
729 /// may occur after a #if or #elif directive. If the expression is equivalent
730 /// to "!defined(X)" return X in IfNDefMacro.
EvaluateDirectiveExpression(IdentifierInfo * & IfNDefMacro)731 bool Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
732 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
733 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
734 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
735 // in which case a directive is undefined behavior. We want macros to be able
736 // to recursively expand in order to get more gcc-list behavior, so we force
737 // DisableMacroExpansion to false and restore it when we're done parsing the
738 // expression.
739 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
740 DisableMacroExpansion = false;
741
742 // Peek ahead one token.
743 Token Tok;
744 LexNonComment(Tok);
745
746 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
747 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
748
749 PPValue ResVal(BitWidth);
750 DefinedTracker DT;
751 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
752 // Parse error, skip the rest of the macro line.
753 if (Tok.isNot(tok::eod))
754 DiscardUntilEndOfDirective();
755
756 // Restore 'DisableMacroExpansion'.
757 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
758 return false;
759 }
760
761 // If we are at the end of the expression after just parsing a value, there
762 // must be no (unparenthesized) binary operators involved, so we can exit
763 // directly.
764 if (Tok.is(tok::eod)) {
765 // If the expression we parsed was of the form !defined(macro), return the
766 // macro in IfNDefMacro.
767 if (DT.State == DefinedTracker::NotDefinedMacro)
768 IfNDefMacro = DT.TheMacro;
769
770 // Restore 'DisableMacroExpansion'.
771 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
772 return ResVal.Val != 0;
773 }
774
775 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
776 // operator and the stuff after it.
777 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
778 Tok, true, *this)) {
779 // Parse error, skip the rest of the macro line.
780 if (Tok.isNot(tok::eod))
781 DiscardUntilEndOfDirective();
782
783 // Restore 'DisableMacroExpansion'.
784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
785 return false;
786 }
787
788 // If we aren't at the tok::eod token, something bad happened, like an extra
789 // ')' token.
790 if (Tok.isNot(tok::eod)) {
791 Diag(Tok, diag::err_pp_expected_eol);
792 DiscardUntilEndOfDirective();
793 }
794
795 // Restore 'DisableMacroExpansion'.
796 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
797 return ResVal.Val != 0;
798 }
799