2006-06-18 13:43:12 +08:00
|
|
|
//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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.
|
2007-04-05 13:24:00 +08:00
|
|
|
// FIXME: Detect and report overflow in expression (e.g. (1 << 62)*2)
|
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"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-04-05 13:24:00 +08:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2007-03-13 07:22:38 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
|
2007-04-10 13:26:38 +08:00
|
|
|
LexerToken &PeekTok, bool ValueLive,
|
|
|
|
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.
|
2007-04-05 13:24:00 +08:00
|
|
|
static bool EvaluateValue(APSInt &Result, LexerToken &PeekTok,
|
2007-04-10 13:26:38 +08:00
|
|
|
DefinedTracker &DT, bool ValueLive,
|
|
|
|
Preprocessor &PP) {
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = 0;
|
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
|
|
|
|
// into a simple 0.
|
2006-11-22 06:24:17 +08:00
|
|
|
if (II->getPPKeywordID() != tok::pp_defined) {
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = 0;
|
2007-04-05 13:24:00 +08:00
|
|
|
Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
|
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)".
|
|
|
|
|
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 (.
|
|
|
|
bool InParens = false;
|
|
|
|
if (PeekTok.getKind() == tok::l_paren) {
|
|
|
|
// Found a paren, remember we saw it and skip it.
|
|
|
|
InParens = true;
|
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?
|
|
|
|
Result = II->getMacroInfo() != 0;
|
2007-04-05 13:24:00 +08:00
|
|
|
Result.setIsUnsigned(false); // Result is signed intmax_t.
|
|
|
|
|
2006-07-03 13:42:18 +08:00
|
|
|
// If there is a macro, mark it used.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (Result != 0 && ValueLive) {
|
2006-10-15 03:03:49 +08:00
|
|
|
II->getMacroInfo()->setIsUsed(true);
|
|
|
|
|
|
|
|
// If this is the first use of a target-specific macro, warn about it.
|
|
|
|
if (II->getMacroInfo()->isTargetSpecific()) {
|
|
|
|
// Don't warn on second use.
|
|
|
|
II->getMacroInfo()->setIsTargetSpecific(false);
|
|
|
|
PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
|
|
|
|
diag::port_target_macro_use);
|
|
|
|
}
|
2007-04-10 13:26:38 +08:00
|
|
|
} else if (ValueLive) {
|
2006-10-15 03:54:15 +08:00
|
|
|
// Use of a target-specific macro for some other target? If so, warn.
|
|
|
|
if (II->isOtherTargetMacro()) {
|
|
|
|
II->setIsOtherTargetMacro(false); // Don't warn on second use.
|
|
|
|
PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
|
|
|
|
diag::port_target_macro_use);
|
|
|
|
}
|
2006-10-15 03:03:49 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Consume identifier.
|
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 ).
|
|
|
|
if (InParens) {
|
|
|
|
if (PeekTok.getKind() != tok::r_paren) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_missing_rparen);
|
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
|
|
|
}
|
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.
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
|
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-04-04 14:54:19 +08:00
|
|
|
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
|
|
|
|
|
|
|
if (Literal.isFloatingLiteral()) {
|
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
|
|
|
|
|
|
|
// Parse the integer literal into Result.
|
|
|
|
if (Literal.GetIntegerValue(Result)) {
|
|
|
|
// Overflow parsing integer literal.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
|
2007-04-05 13:24:00 +08:00
|
|
|
Result.setIsUnsigned(true);
|
|
|
|
} else {
|
|
|
|
// Set the signedness of the result to match whether there was a U suffix
|
|
|
|
// or not.
|
|
|
|
Result.setIsUnsigned(Literal.isUnsigned);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!Literal.isUnsigned && Result.isNegative()) {
|
2007-04-10 13:26:38 +08:00
|
|
|
if (ValueLive)PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
|
2007-04-05 13:24:00 +08:00
|
|
|
Result.setIsUnsigned(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the token.
|
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'
|
|
|
|
SmallString<32> CharBuffer;
|
|
|
|
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();
|
|
|
|
unsigned NumBits;
|
|
|
|
if (Literal.isWide())
|
|
|
|
NumBits = TI.getWCharWidth(PeekTok.getLocation());
|
|
|
|
else
|
|
|
|
NumBits = TI.getCharWidth(PeekTok.getLocation());
|
|
|
|
|
|
|
|
// Set the width.
|
|
|
|
APSInt Val(NumBits);
|
|
|
|
// Set the value.
|
|
|
|
Val = Literal.getValue();
|
|
|
|
// Set the signedness.
|
|
|
|
Val.setIsUnsigned(!TI.isCharSigned(PeekTok.getLocation()));
|
|
|
|
|
|
|
|
if (Result.getBitWidth() > Val.getBitWidth()) {
|
|
|
|
if (Val.isSigned())
|
|
|
|
Result = Val.sext(Result.getBitWidth());
|
|
|
|
else
|
|
|
|
Result = Val.zext(Result.getBitWidth());
|
|
|
|
Result.setIsUnsigned(Val.isUnsigned());
|
|
|
|
} else {
|
|
|
|
assert(Result.getBitWidth() == Val.getBitWidth() &&
|
|
|
|
"intmax_t smaller than char/wchar_t?");
|
|
|
|
Result = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the token.
|
|
|
|
PP.LexNonComment(PeekTok);
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::l_paren:
|
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).
|
|
|
|
if (PeekTok.getKind() == tok::r_paren) {
|
|
|
|
// Just use DT unmodified as our result.
|
|
|
|
} else {
|
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
|
|
|
|
|
|
|
if (PeekTok.getKind() != tok::r_paren) {
|
|
|
|
PP.Diag(PeekTok, diag::err_pp_expected_rparen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok); // Eat the ).
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::plus:
|
|
|
|
// Unary plus doesn't modify the value.
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2007-04-10 13:26:38 +08:00
|
|
|
return EvaluateValue(Result, PeekTok, DT, ValueLive, PP);
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::minus:
|
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;
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = -Result;
|
2006-07-05 02:32:03 +08:00
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::tilde:
|
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;
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = ~Result;
|
2006-07-05 02:32:03 +08:00
|
|
|
DT.State = DefinedTracker::Unknown;
|
2006-06-18 13:43:12 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::exclaim:
|
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;
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = !Result;
|
2007-04-05 13:24:00 +08:00
|
|
|
// C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
|
|
|
|
Result.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;
|
|
|
|
|
|
|
|
// FIXME: Handle #assert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// getPrecedence - Return the precedence of the specified binary operator
|
|
|
|
/// token. This returns:
|
|
|
|
/// ~0 - Invalid token.
|
2006-10-27 13:24:37 +08:00
|
|
|
/// 14 - *,/,%
|
|
|
|
/// 13 - -,+
|
|
|
|
/// 12 - <<,>>
|
|
|
|
/// 11 - >=, <=, >, <
|
|
|
|
/// 10 - ==, !=
|
2006-06-18 13:43:12 +08:00
|
|
|
/// 9 - &
|
|
|
|
/// 8 - ^
|
|
|
|
/// 7 - |
|
|
|
|
/// 6 - &&
|
|
|
|
/// 5 - ||
|
|
|
|
/// 4 - ?
|
|
|
|
/// 3 - :
|
|
|
|
/// 0 - eom, )
|
|
|
|
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;
|
|
|
|
case tok::question: return 4;
|
|
|
|
case tok::colon: return 3;
|
|
|
|
case tok::comma: return 2;
|
|
|
|
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
|
|
|
|
/// PeekTok, and whose precedence is PeekPrec.
|
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.
|
2007-04-05 13:24:00 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
|
2007-04-10 13:26:38 +08:00
|
|
|
LexerToken &PeekTok, bool ValueLive,
|
|
|
|
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) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
|
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;
|
|
|
|
if (Operator == tok::ampamp && LHS == 0)
|
|
|
|
RHSIsLive = false; // RHS of "0 && x" is dead.
|
|
|
|
else if (Operator == tok::pipepipe && LHS != 0)
|
|
|
|
RHSIsLive = false; // RHS of "1 || x" is dead.
|
|
|
|
else if (Operator == tok::question && LHS == 0)
|
|
|
|
RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
|
|
|
|
else
|
|
|
|
RHSIsLive = ValueLive;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Consume the operator, saving the operator token for error reporting.
|
|
|
|
LexerToken OpToken = PeekTok;
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
APSInt 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) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isRightAssoc = Operator == tok::question;
|
|
|
|
|
|
|
|
// Get the precedence of the operator to the right of the RHS. If it binds
|
|
|
|
// more tightly with RHS than we do, evaluate it completely first.
|
|
|
|
if (ThisPrec < PeekPrec ||
|
|
|
|
(ThisPrec == PeekPrec && isRightAssoc)) {
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, 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
|
|
|
|
// either operand is unsigned. Don't do this for x and y in "x ? y : z".
|
|
|
|
if (Operator != tok::question) {
|
|
|
|
if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
|
|
|
|
RHS.setIsUnsigned(LHS.isUnsigned());
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: All of these should detect and report overflow??
|
2006-06-18 13:43:12 +08:00
|
|
|
switch (Operator) {
|
|
|
|
default: assert(0 && "Unknown operator token!");
|
|
|
|
case tok::percent:
|
|
|
|
if (RHS == 0) {
|
2007-04-10 13:26:38 +08:00
|
|
|
if (ValueLive) PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS %= RHS;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case tok::slash:
|
|
|
|
if (RHS == 0) {
|
2007-04-10 13:26:38 +08:00
|
|
|
if (ValueLive) PP.Diag(OpToken, diag::err_pp_division_by_zero);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS /= RHS;
|
|
|
|
break;
|
|
|
|
case tok::star:
|
|
|
|
LHS *= RHS;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
2007-04-04 14:46:55 +08:00
|
|
|
case tok::lessless:
|
|
|
|
// FIXME: shift amt overflow?
|
|
|
|
// FIXME: Don't use getZExtValue.
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS <<= RHS.getZExtValue();
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::greatergreater:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
// FIXME: Don't use getZExtValue.
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS >>= RHS.getZExtValue();
|
|
|
|
break;
|
|
|
|
case tok::plus:
|
|
|
|
LHS += RHS;
|
|
|
|
break;
|
|
|
|
case tok::minus:
|
|
|
|
LHS -= RHS;
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::lessequal:
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS = LHS <= RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::less:
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS = LHS < RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::greaterequal:
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS = LHS >= RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::greater:
|
2007-04-05 13:24:00 +08:00
|
|
|
LHS = LHS > RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
|
|
|
break;
|
|
|
|
case tok::exclaimequal:
|
|
|
|
LHS = LHS != RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
|
|
|
break;
|
|
|
|
case tok::equalequal:
|
|
|
|
LHS = LHS == RHS;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::amp: LHS &= RHS; break;
|
|
|
|
case tok::caret: LHS ^= RHS; break;
|
|
|
|
case tok::pipe: LHS |= RHS; break;
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::ampamp:
|
|
|
|
LHS = LHS != 0 && RHS != 0;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
|
|
|
|
break;
|
|
|
|
case tok::pipepipe:
|
|
|
|
LHS = LHS != 0 || RHS != 0;
|
|
|
|
LHS.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
|
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::comma:
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::ext_pp_comma_expr);
|
2006-06-18 13:43:12 +08:00
|
|
|
LHS = RHS; // LHS = LHS,RHS -> RHS.
|
|
|
|
break;
|
|
|
|
case tok::question: {
|
|
|
|
// Parse the : part of the expression.
|
|
|
|
if (PeekTok.getKind() != tok::colon) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::err_pp_question_without_colon);
|
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 :.
|
2007-04-10 13:26:38 +08:00
|
|
|
bool AfterColonLive = ValueLive && LHS == 0;
|
2007-04-05 13:24:00 +08:00
|
|
|
APSInt 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
|
|
|
|
|
|
|
// Parse anything after the : RHS that has a higher precedence than ?.
|
|
|
|
if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
|
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.
|
2007-04-04 14:46:55 +08:00
|
|
|
LHS = LHS != 0 ? RHS : AfterColonVal;
|
2007-04-05 13:24:00 +08:00
|
|
|
|
|
|
|
// Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
|
|
|
|
// either operand is unsigned.
|
|
|
|
LHS.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.
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::err_pp_colon_without_question);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
LexerToken Tok;
|
|
|
|
Lex(Tok);
|
|
|
|
|
2007-04-04 14:46:55 +08:00
|
|
|
// C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
|
|
|
|
unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
|
2007-04-05 13:24:00 +08:00
|
|
|
APSInt 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.
|
|
|
|
if (Tok.getKind() != tok::eom)
|
|
|
|
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.
|
|
|
|
if (Tok.getKind() == 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;
|
|
|
|
|
2006-07-05 02:11:39 +08:00
|
|
|
return ResVal != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
|
|
|
|
// operator and the stuff after it.
|
2007-04-10 13:26:38 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, true, *this)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
// Parse error, skip the rest of the macro line.
|
|
|
|
if (Tok.getKind() != tok::eom)
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we aren't at the tok::eom token, something bad happened, like an extra
|
|
|
|
// ')' token.
|
|
|
|
if (Tok.getKind() != tok::eom) {
|
|
|
|
Diag(Tok, diag::err_pp_expected_eol);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResVal != 0;
|
|
|
|
}
|
|
|
|
|