2006-06-18 13:43:12 +08:00
|
|
|
//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-07-05 02:03:19 +08:00
|
|
|
// This file implements the Preprocessor::EvaluateDirectiveExpression method,
|
|
|
|
// which parses and evaluates integer constant expressions for #if directives.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-04-04 14:54:19 +08:00
|
|
|
// FIXME: implement testing for #assert's.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2006-07-03 13:42:18 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2007-03-13 07:22:38 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
2006-10-15 03:03:49 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2007-04-05 13:24:00 +08:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
/// PPValue - Represents the value of a subexpression of a preprocessor
|
|
|
|
/// conditional and the source range covered by it.
|
|
|
|
class PPValue {
|
|
|
|
SourceRange Range;
|
|
|
|
public:
|
|
|
|
llvm::APSInt Val;
|
|
|
|
|
|
|
|
// Default ctor - Construct an 'invalid' PPValue.
|
|
|
|
PPValue(unsigned BitWidth) : Val(BitWidth) {}
|
|
|
|
|
|
|
|
unsigned getBitWidth() const { return Val.getBitWidth(); }
|
|
|
|
bool isUnsigned() const { return Val.isUnsigned(); }
|
|
|
|
|
|
|
|
const SourceRange &getRange() const { return Range; }
|
|
|
|
|
|
|
|
void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
|
|
|
|
void setRange(SourceLocation B, SourceLocation E) {
|
|
|
|
Range.setBegin(B); Range.setEnd(E);
|
|
|
|
}
|
|
|
|
void setBegin(SourceLocation L) { Range.setBegin(L); }
|
|
|
|
void setEnd(SourceLocation L) { Range.setEnd(L); }
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
|
2007-07-21 00:59:19 +08:00
|
|
|
Token &PeekTok, bool ValueLive,
|
2007-04-10 13:26:38 +08:00
|
|
|
Preprocessor &PP);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-05 02:32:03 +08:00
|
|
|
/// DefinedTracker - This struct is used while parsing expressions to keep track
|
|
|
|
/// of whether !defined(X) has been seen.
|
|
|
|
///
|
|
|
|
/// With this simple scheme, we handle the basic forms:
|
|
|
|
/// !defined(X) and !defined X
|
|
|
|
/// but we also trivially handle (silly) stuff like:
|
|
|
|
/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
|
|
|
|
struct DefinedTracker {
|
|
|
|
/// Each time a Value is evaluated, it returns information about whether the
|
|
|
|
/// parsed value is of the form defined(X), !defined(X) or is something else.
|
|
|
|
enum TrackerState {
|
|
|
|
DefinedMacro, // defined(X)
|
|
|
|
NotDefinedMacro, // !defined(X)
|
|
|
|
Unknown // Something else.
|
|
|
|
} State;
|
|
|
|
/// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
|
|
|
|
/// indicates the macro that was checked.
|
|
|
|
IdentifierInfo *TheMacro;
|
|
|
|
};
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
|
|
|
|
/// return the computed value in Result. Return true if there was an error
|
2006-07-05 02:32:03 +08:00
|
|
|
/// parsing. This function also returns information about the form of the
|
|
|
|
/// expression in DT. See above for information on what DT means.
|
2007-04-10 13:26:38 +08:00
|
|
|
///
|
|
|
|
/// If ValueLive is false, then this value is being evaluated in a context where
|
|
|
|
/// the result is not used. As such, avoid diagnostics that relate to
|
|
|
|
/// evaluation.
|
2008-05-05 14:45:50 +08:00
|
|
|
static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
|
|
|
|
bool ValueLive, Preprocessor &PP) {
|
2006-07-05 02:32:03 +08:00
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this token's spelling is a pp-identifier, check to see if it is
|
|
|
|
// 'defined' or if it is a macro. Note that we check here because many
|
|
|
|
// keywords are pp-identifiers, so we can't check the kind.
|
2006-07-05 02:32:03 +08:00
|
|
|
if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// If this identifier isn't 'defined' and it wasn't macro expanded, it turns
|
2007-04-10 14:16:30 +08:00
|
|
|
// into a simple 0, unless it is the C++ keyword "true", in which case it
|
|
|
|
// turns into "1".
|
2006-11-22 06:24:17 +08:00
|
|
|
if (II->getPPKeywordID() != tok::pp_defined) {
|
2009-01-19 05:18:58 +08:00
|
|
|
if (ValueLive)
|
|
|
|
PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val = II->getTokenID() == tok::kw_true;
|
|
|
|
Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
|
|
|
|
Result.setRange(PeekTok.getLocation());
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 14:48:37 +08:00
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle "defined X" and "defined(X)".
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setBegin(PeekTok.getLocation());
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-05 02:11:39 +08:00
|
|
|
// Get the next token, don't expand it.
|
|
|
|
PP.LexUnexpandedToken(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Two options, it can either be a pp-identifier or a (.
|
2008-05-05 14:45:50 +08:00
|
|
|
SourceLocation LParenLoc;
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.is(tok::l_paren)) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// Found a paren, remember we saw it and skip it.
|
2008-05-05 14:45:50 +08:00
|
|
|
LParenLoc = PeekTok.getLocation();
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.LexUnexpandedToken(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a pp-identifier now, this is an error.
|
|
|
|
if ((II = PeekTok.getIdentifierInfo()) == 0) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we got an identifier, is it defined to something?
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val = II->hasMacroDefinition();
|
|
|
|
Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
|
2007-04-05 13:24:00 +08:00
|
|
|
|
2006-07-03 13:42:18 +08:00
|
|
|
// If there is a macro, mark it used.
|
2008-05-05 14:45:50 +08:00
|
|
|
if (Result.Val != 0 && ValueLive) {
|
2007-10-07 16:44:20 +08:00
|
|
|
MacroInfo *Macro = PP.getMacroInfo(II);
|
2007-10-07 16:04:56 +08:00
|
|
|
Macro->setIsUsed(true);
|
2006-10-15 03:03:49 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Consume identifier.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setEnd(PeekTok.getLocation());
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If we are in parens, ensure we have a trailing ).
|
2008-05-05 14:45:50 +08:00
|
|
|
if (LParenLoc.isValid()) {
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.isNot(tok::r_paren)) {
|
2008-05-05 14:45:50 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen);
|
2008-11-24 07:17:07 +08:00
|
|
|
PP.Diag(LParenLoc, diag::note_matching) << "(";
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Consume the ).
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setEnd(PeekTok.getLocation());
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-07-05 02:32:03 +08:00
|
|
|
|
|
|
|
// Success, remember that we saw defined(X).
|
|
|
|
DT.State = DefinedTracker::DefinedMacro;
|
|
|
|
DT.TheMacro = II;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PeekTok.getKind()) {
|
|
|
|
default: // Non-value token.
|
2008-04-14 04:38:43 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
case tok::eom:
|
|
|
|
case tok::r_paren:
|
|
|
|
// If there is no expression, report and exit.
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
case tok::numeric_constant: {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallString<64> IntegerBuffer;
|
2007-03-13 07:22:38 +08:00
|
|
|
IntegerBuffer.resize(PeekTok.getLength());
|
|
|
|
const char *ThisTokBegin = &IntegerBuffer[0];
|
|
|
|
unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
|
|
|
|
NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
|
|
|
|
PeekTok.getLocation(), PP);
|
2007-04-04 14:54:19 +08:00
|
|
|
if (Literal.hadError)
|
2007-03-14 04:29:44 +08:00
|
|
|
return true; // a diagnostic was already reported.
|
2007-04-04 14:46:55 +08:00
|
|
|
|
2007-08-26 11:29:23 +08:00
|
|
|
if (Literal.isFloatingLiteral() || Literal.isImaginary) {
|
2007-03-13 07:22:38 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
|
2007-03-14 04:29:44 +08:00
|
|
|
return true;
|
2007-03-13 07:22:38 +08:00
|
|
|
}
|
2007-04-04 14:46:55 +08:00
|
|
|
assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
|
2007-04-05 13:24:00 +08:00
|
|
|
|
2007-08-30 06:00:19 +08:00
|
|
|
// long long is a C99 feature.
|
|
|
|
if (!PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus0x
|
2007-08-30 06:13:52 +08:00
|
|
|
&& Literal.isLongLong)
|
2007-08-30 06:00:19 +08:00
|
|
|
PP.Diag(PeekTok, diag::ext_longlong);
|
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
// Parse the integer literal into Result.
|
2008-05-05 14:45:50 +08:00
|
|
|
if (Literal.GetIntegerValue(Result.Val)) {
|
2007-04-05 13:24:00 +08:00
|
|
|
// Overflow parsing integer literal.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val.setIsUnsigned(true);
|
2007-04-05 13:24:00 +08:00
|
|
|
} else {
|
|
|
|
// Set the signedness of the result to match whether there was a U suffix
|
|
|
|
// or not.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val.setIsUnsigned(Literal.isUnsigned);
|
2007-04-05 13:24:00 +08:00
|
|
|
|
|
|
|
// Detect overflow based on whether the value is signed. If signed
|
|
|
|
// and if the value is too large, emit a warning "integer constant is so
|
|
|
|
// large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
|
|
|
|
// is 64-bits.
|
2008-05-05 14:45:50 +08:00
|
|
|
if (!Literal.isUnsigned && Result.Val.isNegative()) {
|
2008-07-03 11:47:30 +08:00
|
|
|
// Don't warn for a hex literal: 0x8000..0 shouldn't warn.
|
|
|
|
if (ValueLive && Literal.getRadix() != 16)
|
2008-05-05 14:45:50 +08:00
|
|
|
PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
|
|
|
|
Result.Val.setIsUnsigned(true);
|
2007-04-05 13:24:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the token.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setRange(PeekTok.getLocation());
|
2007-04-04 14:46:55 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-04-05 14:58:56 +08:00
|
|
|
case tok::char_constant: { // 'x'
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallString<32> CharBuffer;
|
2007-04-05 14:58:56 +08:00
|
|
|
CharBuffer.resize(PeekTok.getLength());
|
|
|
|
const char *ThisTokBegin = &CharBuffer[0];
|
|
|
|
unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
|
|
|
|
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
|
|
|
|
PeekTok.getLocation(), PP);
|
|
|
|
if (Literal.hadError())
|
|
|
|
return true; // A diagnostic was already emitted.
|
|
|
|
|
|
|
|
// Character literals are always int or wchar_t, expand to intmax_t.
|
|
|
|
TargetInfo &TI = PP.getTargetInfo();
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned NumBits = TI.getCharWidth(Literal.isWide());
|
2007-04-05 14:58:56 +08:00
|
|
|
|
|
|
|
// Set the width.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::APSInt Val(NumBits);
|
2007-04-05 14:58:56 +08:00
|
|
|
// Set the value.
|
|
|
|
Val = Literal.getValue();
|
|
|
|
// Set the signedness.
|
2008-03-06 02:54:05 +08:00
|
|
|
Val.setIsUnsigned(!TI.isCharSigned());
|
2007-04-05 14:58:56 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
if (Result.Val.getBitWidth() > Val.getBitWidth()) {
|
|
|
|
Result.Val = Val.extend(Result.Val.getBitWidth());
|
2007-04-05 14:58:56 +08:00
|
|
|
} else {
|
2008-05-05 14:45:50 +08:00
|
|
|
assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
|
2007-04-05 14:58:56 +08:00
|
|
|
"intmax_t smaller than char/wchar_t?");
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val = Val;
|
2007-04-05 14:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the token.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setRange(PeekTok.getLocation());
|
2007-04-05 14:58:56 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-05 14:45:50 +08:00
|
|
|
case tok::l_paren: {
|
|
|
|
SourceLocation Start = PeekTok.getLocation();
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok); // Eat the (.
|
2006-06-18 14:48:37 +08:00
|
|
|
// Parse the value and if there are any binary operators involved, parse
|
|
|
|
// them.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-07-05 02:32:03 +08:00
|
|
|
// If this is a silly value like (X), which doesn't need parens, check for
|
|
|
|
// !(defined X).
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.is(tok::r_paren)) {
|
2006-07-05 02:32:03 +08:00
|
|
|
// Just use DT unmodified as our result.
|
|
|
|
} else {
|
2008-05-05 14:45:50 +08:00
|
|
|
// Otherwise, we have something like (x+y), and we consumed '(x'.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
|
|
|
|
return true;
|
2006-07-05 02:32:03 +08:00
|
|
|
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.isNot(tok::r_paren)) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
|
|
|
|
<< Result.getRange();
|
2008-11-24 07:17:07 +08:00
|
|
|
PP.Diag(Start, diag::note_matching) << "(";
|
2006-07-05 02:32:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setRange(Start, PeekTok.getLocation());
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok); // Eat the ).
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
2008-05-05 14:45:50 +08:00
|
|
|
}
|
|
|
|
case tok::plus: {
|
|
|
|
SourceLocation Start = PeekTok.getLocation();
|
2006-06-18 13:43:12 +08:00
|
|
|
// Unary plus doesn't modify the value.
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2008-05-05 14:45:50 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
|
|
|
|
Result.setBegin(Start);
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-11 11:42:36 +08:00
|
|
|
case tok::minus: {
|
|
|
|
SourceLocation Loc = PeekTok.getLocation();
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setBegin(Loc);
|
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val = -Result.Val;
|
2007-04-11 11:42:36 +08:00
|
|
|
|
2008-07-03 11:47:30 +08:00
|
|
|
// -MININT is the only thing that overflows. Unsigned never overflows.
|
|
|
|
bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
|
2007-04-11 11:42:36 +08:00
|
|
|
|
|
|
|
// If this operator is live and overflowed, report the issue.
|
|
|
|
if (Overflow && ValueLive)
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
|
2007-04-11 11:42:36 +08:00
|
|
|
|
2006-07-05 02:32:03 +08:00
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
2007-04-11 11:42:36 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
case tok::tilde: {
|
|
|
|
SourceLocation Start = PeekTok.getLocation();
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setBegin(Start);
|
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val = ~Result.Val;
|
2006-07-05 02:32:03 +08:00
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
2008-05-05 14:45:50 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
case tok::exclaim: {
|
|
|
|
SourceLocation Start = PeekTok.getLocation();
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.setBegin(Start);
|
|
|
|
Result.Val = !Result.Val;
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
|
2008-05-05 14:45:50 +08:00
|
|
|
Result.Val.setIsUnsigned(false);
|
2006-07-05 02:32:03 +08:00
|
|
|
|
|
|
|
if (DT.State == DefinedTracker::DefinedMacro)
|
|
|
|
DT.State = DefinedTracker::NotDefinedMacro;
|
|
|
|
else if (DT.State == DefinedTracker::NotDefinedMacro)
|
|
|
|
DT.State = DefinedTracker::DefinedMacro;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
2008-05-05 14:45:50 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// FIXME: Handle #assert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// getPrecedence - Return the precedence of the specified binary operator
|
|
|
|
/// token. This returns:
|
|
|
|
/// ~0 - Invalid token.
|
2008-05-05 12:10:51 +08:00
|
|
|
/// 14 -> 3 - various operators.
|
|
|
|
/// 0 - 'eom' or ')'
|
2006-06-18 13:43:12 +08:00
|
|
|
static unsigned getPrecedence(tok::TokenKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
default: return ~0U;
|
|
|
|
case tok::percent:
|
|
|
|
case tok::slash:
|
2006-10-27 13:24:37 +08:00
|
|
|
case tok::star: return 14;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::plus:
|
2006-10-27 13:24:37 +08:00
|
|
|
case tok::minus: return 13;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::lessless:
|
2006-10-27 13:24:37 +08:00
|
|
|
case tok::greatergreater: return 12;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::lessequal:
|
|
|
|
case tok::less:
|
|
|
|
case tok::greaterequal:
|
2006-10-27 13:24:37 +08:00
|
|
|
case tok::greater: return 11;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::exclaimequal:
|
2006-10-27 13:24:37 +08:00
|
|
|
case tok::equalequal: return 10;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::amp: return 9;
|
|
|
|
case tok::caret: return 8;
|
|
|
|
case tok::pipe: return 7;
|
|
|
|
case tok::ampamp: return 6;
|
|
|
|
case tok::pipepipe: return 5;
|
2008-05-06 04:07:41 +08:00
|
|
|
case tok::question: return 4;
|
|
|
|
case tok::comma: return 3;
|
2008-05-05 02:36:18 +08:00
|
|
|
case tok::colon: return 2;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::r_paren: return 0; // Lowest priority, end of expr.
|
|
|
|
case tok::eom: return 0; // Lowest priority, end of macro.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
|
2008-05-05 14:45:50 +08:00
|
|
|
/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
|
2007-04-10 13:26:38 +08:00
|
|
|
///
|
|
|
|
/// If ValueLive is false, then this value is being evaluated in a context where
|
|
|
|
/// the result is not used. As such, avoid diagnostics that relate to
|
2008-05-05 14:45:50 +08:00
|
|
|
/// evaluation, such as division by zero warnings.
|
|
|
|
static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
|
2007-07-21 00:59:19 +08:00
|
|
|
Token &PeekTok, bool ValueLive,
|
2007-04-10 13:26:38 +08:00
|
|
|
Preprocessor &PP) {
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned PeekPrec = getPrecedence(PeekTok.getKind());
|
|
|
|
// If this token isn't valid, report the error.
|
|
|
|
if (PeekPrec == ~0U) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
|
|
|
|
<< LHS.getRange();
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// If this token has a lower precedence than we are allowed to parse, return
|
|
|
|
// it so that higher levels of the recursion can parse it.
|
|
|
|
if (PeekPrec < MinPrec)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok::TokenKind Operator = PeekTok.getKind();
|
2007-04-10 13:26:38 +08:00
|
|
|
|
|
|
|
// If this is a short-circuiting operator, see if the RHS of the operator is
|
|
|
|
// dead. Note that this cannot just clobber ValueLive. Consider
|
|
|
|
// "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
|
|
|
|
// this example, the RHS of the && being dead does not make the rest of the
|
|
|
|
// expr dead.
|
|
|
|
bool RHSIsLive;
|
2008-05-05 14:45:50 +08:00
|
|
|
if (Operator == tok::ampamp && LHS.Val == 0)
|
2007-04-10 13:26:38 +08:00
|
|
|
RHSIsLive = false; // RHS of "0 && x" is dead.
|
2008-05-05 14:45:50 +08:00
|
|
|
else if (Operator == tok::pipepipe && LHS.Val != 0)
|
2007-04-10 13:26:38 +08:00
|
|
|
RHSIsLive = false; // RHS of "1 || x" is dead.
|
2008-05-05 14:45:50 +08:00
|
|
|
else if (Operator == tok::question && LHS.Val == 0)
|
2007-04-10 13:26:38 +08:00
|
|
|
RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
|
|
|
|
else
|
|
|
|
RHSIsLive = ValueLive;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
// Consume the operator, remembering the operator's location for reporting.
|
|
|
|
SourceLocation OpLoc = PeekTok.getLocation();
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
PPValue RHS(LHS.getBitWidth());
|
2006-06-18 13:43:12 +08:00
|
|
|
// Parse the RHS of the operator.
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Remember the precedence of this operator and get the precedence of the
|
|
|
|
// operator immediately to the right of the RHS.
|
|
|
|
unsigned ThisPrec = PeekPrec;
|
|
|
|
PeekPrec = getPrecedence(PeekTok.getKind());
|
|
|
|
|
|
|
|
// If this token isn't valid, report the error.
|
|
|
|
if (PeekPrec == ~0U) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
|
|
|
|
<< RHS.getRange();
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-06 04:07:41 +08:00
|
|
|
// Decide whether to include the next binop in this subexpression. For
|
|
|
|
// example, when parsing x+y*z and looking at '*', we want to recursively
|
2008-05-06 04:09:27 +08:00
|
|
|
// handle y*z as a single subexpression. We do this because the precedence
|
|
|
|
// of * is higher than that of +. The only strange case we have to handle
|
|
|
|
// here is for the ?: operator, where the precedence is actually lower than
|
|
|
|
// the LHS of the '?'. The grammar rule is:
|
2008-05-06 04:07:41 +08:00
|
|
|
//
|
|
|
|
// conditional-expression ::=
|
|
|
|
// logical-OR-expression ? expression : conditional-expression
|
|
|
|
// where 'expression' is actually comma-expression.
|
|
|
|
unsigned RHSPrec;
|
|
|
|
if (Operator == tok::question)
|
|
|
|
// The RHS of "?" should be maximally consumed as an expression.
|
|
|
|
RHSPrec = getPrecedence(tok::comma);
|
|
|
|
else // All others should munch while higher precedence.
|
|
|
|
RHSPrec = ThisPrec+1;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-06 04:07:41 +08:00
|
|
|
if (PeekPrec >= RHSPrec) {
|
|
|
|
if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
PeekPrec = getPrecedence(PeekTok.getKind());
|
|
|
|
}
|
|
|
|
assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
|
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
// Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
|
2008-05-05 07:46:17 +08:00
|
|
|
// either operand is unsigned.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::APSInt Res(LHS.getBitWidth());
|
2008-05-05 07:46:17 +08:00
|
|
|
switch (Operator) {
|
|
|
|
case tok::question: // No UAC for x and y in "x ? y : z".
|
|
|
|
case tok::lessless: // Shift amount doesn't UAC with shift value.
|
|
|
|
case tok::greatergreater: // Shift amount doesn't UAC with shift value.
|
|
|
|
case tok::comma: // Comma operands are not subject to UACs.
|
|
|
|
case tok::pipepipe: // Logical || does not do UACs.
|
|
|
|
case tok::ampamp: // Logical && does not do UACs.
|
|
|
|
break; // No UAC
|
|
|
|
default:
|
2007-04-11 12:14:45 +08:00
|
|
|
Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
|
|
|
|
// If this just promoted something from signed to unsigned, and if the
|
|
|
|
// value was negative, warn about it.
|
|
|
|
if (ValueLive && Res.isUnsigned()) {
|
2008-05-05 14:45:50 +08:00
|
|
|
if (!LHS.isUnsigned() && LHS.Val.isNegative())
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
|
|
|
|
<< LHS.Val.toString(10, true) + " to " +
|
|
|
|
LHS.Val.toString(10, false)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2008-05-05 14:45:50 +08:00
|
|
|
if (!RHS.isUnsigned() && RHS.Val.isNegative())
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
|
|
|
|
<< RHS.Val.toString(10, true) + " to " +
|
|
|
|
RHS.Val.toString(10, false)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2007-04-11 12:14:45 +08:00
|
|
|
}
|
2008-05-05 14:45:50 +08:00
|
|
|
LHS.Val.setIsUnsigned(Res.isUnsigned());
|
|
|
|
RHS.Val.setIsUnsigned(Res.isUnsigned());
|
2007-04-05 13:24:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: All of these should detect and report overflow??
|
2007-04-10 14:54:33 +08:00
|
|
|
bool Overflow = false;
|
2006-06-18 13:43:12 +08:00
|
|
|
switch (Operator) {
|
|
|
|
default: assert(0 && "Unknown operator token!");
|
|
|
|
case tok::percent:
|
2008-05-05 14:45:50 +08:00
|
|
|
if (RHS.Val != 0)
|
|
|
|
Res = LHS.Val % RHS.Val;
|
|
|
|
else if (ValueLive) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2008-05-05 14:45:50 +08:00
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::slash:
|
2008-05-05 14:45:50 +08:00
|
|
|
if (RHS.Val != 0) {
|
|
|
|
Res = LHS.Val / RHS.Val;
|
|
|
|
if (LHS.Val.isSigned()) // MININT/-1 --> overflow.
|
|
|
|
Overflow = LHS.Val.isMinSignedValue() && RHS.Val.isAllOnesValue();
|
|
|
|
} else if (ValueLive) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::err_pp_division_by_zero)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2008-05-05 14:45:50 +08:00
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
2008-05-04 15:15:21 +08:00
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::star:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val * RHS.Val;
|
|
|
|
if (LHS.Val != 0 && RHS.Val != 0)
|
|
|
|
Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
2007-04-10 14:54:33 +08:00
|
|
|
case tok::lessless: {
|
|
|
|
// Determine whether overflow is about to happen.
|
2008-05-05 14:45:50 +08:00
|
|
|
unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
|
|
|
|
if (ShAmt >= LHS.Val.getBitWidth())
|
|
|
|
Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
|
2007-04-10 14:54:33 +08:00
|
|
|
else if (LHS.isUnsigned())
|
2008-05-05 14:45:50 +08:00
|
|
|
Overflow = ShAmt > LHS.Val.countLeadingZeros();
|
|
|
|
else if (LHS.Val.isNonNegative()) // Don't allow sign change.
|
|
|
|
Overflow = ShAmt >= LHS.Val.countLeadingZeros();
|
2007-04-10 14:54:33 +08:00
|
|
|
else
|
2008-05-05 14:45:50 +08:00
|
|
|
Overflow = ShAmt >= LHS.Val.countLeadingOnes();
|
2007-04-10 14:54:33 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val << ShAmt;
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
2007-04-10 14:54:33 +08:00
|
|
|
}
|
|
|
|
case tok::greatergreater: {
|
|
|
|
// Determine whether overflow is about to happen.
|
2008-05-05 14:45:50 +08:00
|
|
|
unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
|
2007-04-10 14:54:33 +08:00
|
|
|
if (ShAmt >= LHS.getBitWidth())
|
|
|
|
Overflow = true, ShAmt = LHS.getBitWidth()-1;
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val >> ShAmt;
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
2007-04-10 14:54:33 +08:00
|
|
|
}
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::plus:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val + RHS.Val;
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
2008-05-05 14:45:50 +08:00
|
|
|
Overflow = Res.ult(LHS.Val);
|
|
|
|
else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
|
|
|
|
Res.isNonNegative() != LHS.Val.isNonNegative())
|
2007-04-11 11:34:29 +08:00
|
|
|
Overflow = true; // Overflow for signed addition.
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::minus:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val - RHS.Val;
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
2008-05-05 14:45:50 +08:00
|
|
|
Overflow = Res.ugt(LHS.Val);
|
|
|
|
else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
|
|
|
|
Res.isNonNegative() != LHS.Val.isNonNegative())
|
2007-04-11 11:34:29 +08:00
|
|
|
Overflow = true; // Overflow for signed subtraction.
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::lessequal:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val <= RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::less:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val < RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::greaterequal:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val >= RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::greater:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val > RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::exclaimequal:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val != RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::equalequal:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val == RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
|
|
|
break;
|
|
|
|
case tok::amp:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val & RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
break;
|
|
|
|
case tok::caret:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val ^ RHS.Val;
|
2007-04-10 15:07:11 +08:00
|
|
|
break;
|
|
|
|
case tok::pipe:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val | RHS.Val;
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::ampamp:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = (LHS.Val != 0 && RHS.Val != 0);
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::pipepipe:
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = (LHS.Val != 0 || RHS.Val != 0);
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::comma:
|
2008-05-05 02:36:18 +08:00
|
|
|
// Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
|
|
|
|
// if not being evaluated.
|
|
|
|
if (!PP.getLangOptions().C99 || ValueLive)
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::ext_pp_comma_expr)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = RHS.Val; // LHS = LHS,RHS -> RHS.
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case tok::question: {
|
|
|
|
// Parse the : part of the expression.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.isNot(tok::colon)) {
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
|
|
|
|
<< LHS.getRange(), RHS.getRange();
|
2008-11-24 07:17:07 +08:00
|
|
|
PP.Diag(OpLoc, diag::note_matching) << "?";
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Consume the :.
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Evaluate the value after the :.
|
2008-05-05 14:45:50 +08:00
|
|
|
bool AfterColonLive = ValueLive && LHS.Val == 0;
|
|
|
|
PPValue AfterColonVal(LHS.getBitWidth());
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
|
|
|
|
return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-05-06 04:09:27 +08:00
|
|
|
// Parse anything after the : with the same precedence as ?. We allow
|
|
|
|
// things of equal precedence because ?: is right associative.
|
2008-05-06 04:07:41 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
|
2007-04-10 13:26:38 +08:00
|
|
|
PeekTok, AfterColonLive, PP))
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Now that we have the condition, the LHS and the RHS of the :, evaluate.
|
2008-05-05 14:45:50 +08:00
|
|
|
Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
|
|
|
|
RHS.setEnd(AfterColonVal.getRange().getEnd());
|
2007-04-05 13:24:00 +08:00
|
|
|
|
|
|
|
// Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
|
|
|
|
// either operand is unsigned.
|
2007-04-10 15:07:11 +08:00
|
|
|
Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Figure out the precedence of the token after the : part.
|
|
|
|
PeekPrec = getPrecedence(PeekTok.getKind());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case tok::colon:
|
|
|
|
// Don't allow :'s to float around without being part of ?: exprs.
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::err_pp_colon_without_question)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-10 14:54:33 +08:00
|
|
|
|
|
|
|
// If this operator is live and overflowed, report the issue.
|
|
|
|
if (Overflow && ValueLive)
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
|
|
|
|
<< LHS.getRange() << RHS.getRange();
|
2007-04-10 15:07:11 +08:00
|
|
|
|
|
|
|
// Put the result back into 'LHS' for our next iteration.
|
2008-05-05 14:45:50 +08:00
|
|
|
LHS.Val = Res;
|
|
|
|
LHS.setEnd(RHS.getRange().getEnd());
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2006-07-05 02:11:39 +08:00
|
|
|
|
|
|
|
/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
|
|
|
|
/// may occur after a #if or #elif directive. If the expression is equivalent
|
|
|
|
/// to "!defined(X)" return X in IfNDefMacro.
|
|
|
|
bool Preprocessor::
|
|
|
|
EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
|
|
|
|
// Peek ahead one token.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2006-07-05 02:11:39 +08:00
|
|
|
Lex(Tok);
|
|
|
|
|
2007-04-04 14:46:55 +08:00
|
|
|
// C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
|
2007-12-13 06:39:36 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
PPValue ResVal(BitWidth);
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
// Parse error, skip the rest of the macro line.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (Tok.isNot(tok::eom))
|
2006-07-05 02:11:39 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are at the end of the expression after just parsing a value, there
|
|
|
|
// must be no (unparenthesized) binary operators involved, so we can exit
|
|
|
|
// directly.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (Tok.is(tok::eom)) {
|
2006-07-05 02:32:03 +08:00
|
|
|
// If the expression we parsed was of the form !defined(macro), return the
|
|
|
|
// macro in IfNDefMacro.
|
|
|
|
if (DT.State == DefinedTracker::NotDefinedMacro)
|
|
|
|
IfNDefMacro = DT.TheMacro;
|
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
return ResVal.Val != 0;
|
2006-07-05 02:11:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
|
|
|
|
// operator and the stuff after it.
|
2008-05-06 04:07:41 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
|
|
|
|
Tok, true, *this)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
// Parse error, skip the rest of the macro line.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (Tok.isNot(tok::eom))
|
2006-07-05 02:11:39 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we aren't at the tok::eom token, something bad happened, like an extra
|
|
|
|
// ')' token.
|
2007-10-10 02:02:16 +08:00
|
|
|
if (Tok.isNot(tok::eom)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
Diag(Tok, diag::err_pp_expected_eol);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
return ResVal.Val != 0;
|
2006-07-05 02:11:39 +08:00
|
|
|
}
|
|
|
|
|