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