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.
|
2006-06-18 13:43:12 +08:00
|
|
|
// FIXME: Track signed/unsigned correctly.
|
|
|
|
// FIXME: Track and report integer overflow correctly.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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-04 14:46:55 +08:00
|
|
|
#include "llvm/ADT/APInt.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-04 14:46:55 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(APInt &LHS, unsigned MinPrec,
|
2006-07-05 02:11:39 +08:00
|
|
|
LexerToken &PeekTok, 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-04 14:46:55 +08:00
|
|
|
static bool EvaluateValue(APInt &Result, LexerToken &PeekTok,
|
|
|
|
DefinedTracker &DT, 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;
|
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;
|
2006-07-03 13:42:18 +08:00
|
|
|
|
|
|
|
// If there is a macro, mark it used.
|
2007-04-04 14:46:55 +08:00
|
|
|
if (Result != 0) {
|
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);
|
|
|
|
}
|
2006-10-15 03:54:15 +08:00
|
|
|
} else {
|
|
|
|
// 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-04 14:54:19 +08:00
|
|
|
|
2007-04-04 14:46:55 +08:00
|
|
|
// FIXME: Handle 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. 12345678901234567890.
|
|
|
|
if (Literal.GetIntegerValue(Result))
|
|
|
|
PP.Diag(PeekTok, diag::warn_integer_too_large);
|
|
|
|
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.
|
2006-07-05 02:32:03 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, 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 {
|
|
|
|
if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, PP)) return true;
|
|
|
|
|
|
|
|
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);
|
2006-07-05 02:32:03 +08:00
|
|
|
return EvaluateValue(Result, PeekTok, DT, PP);
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::minus:
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-07-05 02:32:03 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
|
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);
|
2006-07-05 02:32:03 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
|
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);
|
2006-07-05 02:32:03 +08:00
|
|
|
if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
|
2006-06-18 13:43:12 +08:00
|
|
|
Result = !Result;
|
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-04 14:46:55 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(APInt &LHS, unsigned MinPrec,
|
2006-07-05 02:11:39 +08:00
|
|
|
LexerToken &PeekTok, 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();
|
|
|
|
|
|
|
|
// 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-04 14:46:55 +08:00
|
|
|
APInt RHS(LHS.getBitWidth(), 0);
|
2006-06-18 13:43:12 +08:00
|
|
|
// Parse the RHS of the operator.
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
|
|
|
if (EvaluateValue(RHS, PeekTok, DT, 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)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, PP))
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
PeekPrec = getPrecedence(PeekTok.getKind());
|
|
|
|
}
|
|
|
|
assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
|
|
|
|
|
|
|
|
switch (Operator) {
|
|
|
|
default: assert(0 && "Unknown operator token!");
|
|
|
|
case tok::percent:
|
|
|
|
if (RHS == 0) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-04 14:46:55 +08:00
|
|
|
// FIXME: sign.
|
|
|
|
LHS = LHS.urem(RHS);
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case tok::slash:
|
|
|
|
if (RHS == 0) {
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::err_pp_division_by_zero);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-04 14:46:55 +08:00
|
|
|
// FIXME: sign.
|
|
|
|
LHS = LHS.udiv(RHS);
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
case tok::star : LHS *= RHS; break;
|
2007-04-04 14:46:55 +08:00
|
|
|
case tok::lessless:
|
|
|
|
// FIXME: shift amt overflow?
|
|
|
|
// FIXME: Don't use getZExtValue.
|
|
|
|
LHS = LHS << RHS.getZExtValue();
|
|
|
|
break;
|
|
|
|
case tok::greatergreater:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
// FIXME: Don't use getZExtValue.
|
|
|
|
LHS = LHS.ashr(RHS.getZExtValue());
|
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::plus : LHS += RHS; break;
|
|
|
|
case tok::minus: LHS -= RHS; break;
|
2007-04-04 14:46:55 +08:00
|
|
|
case tok::lessequal:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
LHS = LHS.sle(RHS);
|
|
|
|
break;
|
|
|
|
case tok::less:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
LHS = LHS.slt(RHS);
|
|
|
|
break;
|
|
|
|
case tok::greaterequal:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
LHS = LHS.sge(RHS);
|
|
|
|
break;
|
|
|
|
case tok::greater:
|
|
|
|
// FIXME: signed vs unsigned
|
|
|
|
LHS = LHS.sgt(RHS);
|
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
case tok::exclaimequal: LHS = LHS != RHS; break;
|
|
|
|
case tok::equalequal: LHS = LHS == RHS; break;
|
|
|
|
case tok::amp: LHS &= RHS; break;
|
|
|
|
case tok::caret: LHS ^= RHS; break;
|
|
|
|
case tok::pipe: LHS |= RHS; break;
|
2007-04-04 14:46:55 +08:00
|
|
|
case tok::ampamp: LHS = LHS != 0 && RHS != 0; break;
|
|
|
|
case tok::pipepipe: LHS = LHS != 0 || RHS != 0; 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-04 14:46:55 +08:00
|
|
|
APInt AfterColonVal(LHS.getBitWidth(), 0);
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
|
|
|
if (EvaluateValue(AfterColonVal, PeekTok, DT, 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,
|
2006-07-05 02:11:39 +08:00
|
|
|
PeekTok, 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;
|
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());
|
|
|
|
APInt ResVal(BitWidth, 0);
|
2006-07-05 02:32:03 +08:00
|
|
|
DefinedTracker DT;
|
|
|
|
if (EvaluateValue(ResVal, Tok, DT, *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.
|
|
|
|
if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, *this)) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|