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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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 clang;
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(llvm::APSInt &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.
|
2007-07-21 00:59:19 +08:00
|
|
|
static bool EvaluateValue(llvm::APSInt &Result, Token &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
|
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) {
|
2007-04-10 14:16:30 +08:00
|
|
|
Result = II->getTokenID() == tok::kw_true;
|
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;
|
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.
|
|
|
|
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?
|
2007-10-07 16:04:56 +08:00
|
|
|
Result = II->hasMacroDefinition();
|
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) {
|
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
|
|
|
|
|
|
|
// If this is the first use of a target-specific macro, warn about it.
|
2007-10-07 16:04:56 +08:00
|
|
|
if (Macro->isTargetSpecific()) {
|
2006-10-15 03:03:49 +08:00
|
|
|
// Don't warn on second use.
|
2007-10-07 16:04:56 +08:00
|
|
|
Macro->setIsTargetSpecific(false);
|
2006-10-15 03:03:49 +08:00
|
|
|
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) {
|
2007-10-10 02:02:16 +08:00
|
|
|
if (PeekTok.isNot(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-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.
|
|
|
|
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'
|
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();
|
|
|
|
unsigned NumBits;
|
|
|
|
if (Literal.isWide())
|
|
|
|
NumBits = TI.getWCharWidth(PeekTok.getLocation());
|
|
|
|
else
|
|
|
|
NumBits = TI.getCharWidth(PeekTok.getLocation());
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
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).
|
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 {
|
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)) {
|
2006-07-05 02:32:03 +08:00
|
|
|
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);
|
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;
|
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;
|
2007-04-11 11:42:36 +08:00
|
|
|
|
|
|
|
bool Overflow = false;
|
|
|
|
if (Result.isUnsigned())
|
|
|
|
Overflow = !Result.isPositive();
|
|
|
|
else if (Result.isMinSignedValue())
|
|
|
|
Overflow = true; // -MININT is the only thing that overflows.
|
|
|
|
|
|
|
|
// If this operator is live and overflowed, report the issue.
|
|
|
|
if (Overflow && ValueLive)
|
|
|
|
PP.Diag(Loc, diag::warn_pp_expr_overflow);
|
|
|
|
|
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
|
|
|
|
|
|
|
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-06-16 07:05:46 +08:00
|
|
|
static bool EvaluateDirectiveSubExpr(llvm::APSInt &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) {
|
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.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token OpToken = PeekTok;
|
2006-10-27 13:43:50 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::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".
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::APSInt Res(LHS.getBitWidth());
|
2007-04-05 13:24:00 +08:00
|
|
|
if (Operator != tok::question) {
|
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()) {
|
|
|
|
if (!LHS.isUnsigned() && LHS.isNegative())
|
|
|
|
PP.Diag(OpToken, diag::warn_pp_convert_lhs_to_positive,
|
2007-08-23 13:22:10 +08:00
|
|
|
LHS.toStringSigned() + " to " + LHS.toStringUnsigned());
|
2007-04-11 12:14:45 +08:00
|
|
|
if (!RHS.isUnsigned() && RHS.isNegative())
|
|
|
|
PP.Diag(OpToken, diag::warn_pp_convert_rhs_to_positive,
|
2007-08-23 13:22:10 +08:00
|
|
|
RHS.toStringSigned() + " to " + RHS.toStringUnsigned());
|
2007-04-11 12:14:45 +08:00
|
|
|
}
|
|
|
|
LHS.setIsUnsigned(Res.isUnsigned());
|
|
|
|
RHS.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:
|
|
|
|
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-10 15:07:11 +08:00
|
|
|
Res = 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-10 15:07:11 +08:00
|
|
|
Res = LHS / RHS;
|
2007-04-11 12:04:29 +08:00
|
|
|
if (LHS.isSigned())
|
|
|
|
Overflow = LHS.isMinSignedValue() && RHS.isAllOnesValue(); // MININT/-1
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::star:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS * RHS;
|
2007-04-11 12:04:29 +08:00
|
|
|
if (LHS != 0 && RHS != 0)
|
|
|
|
Overflow = Res/RHS != LHS || Res/LHS != RHS;
|
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.
|
2007-09-04 10:45:27 +08:00
|
|
|
unsigned ShAmt = static_cast<unsigned>(RHS.getLimitedValue());
|
2007-04-10 14:54:33 +08:00
|
|
|
if (ShAmt >= LHS.getBitWidth())
|
|
|
|
Overflow = true, ShAmt = LHS.getBitWidth()-1;
|
|
|
|
else if (LHS.isUnsigned())
|
|
|
|
Overflow = ShAmt > LHS.countLeadingZeros();
|
|
|
|
else if (LHS.isPositive())
|
|
|
|
Overflow = ShAmt >= LHS.countLeadingZeros(); // Don't allow sign change.
|
|
|
|
else
|
|
|
|
Overflow = ShAmt >= LHS.countLeadingOnes();
|
|
|
|
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS << 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.
|
2007-09-04 10:45:27 +08:00
|
|
|
unsigned ShAmt = static_cast<unsigned>(RHS.getLimitedValue());
|
2007-04-10 14:54:33 +08:00
|
|
|
if (ShAmt >= LHS.getBitWidth())
|
|
|
|
Overflow = true, ShAmt = LHS.getBitWidth()-1;
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS >> 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:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS + RHS;
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
|
|
|
Overflow = Res.ult(LHS);
|
|
|
|
else if (LHS.isPositive() == RHS.isPositive() &&
|
|
|
|
Res.isPositive() != LHS.isPositive())
|
|
|
|
Overflow = true; // Overflow for signed addition.
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::minus:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS - RHS;
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
|
|
|
Overflow = Res.ugt(LHS);
|
|
|
|
else if (LHS.isPositive() != RHS.isPositive() &&
|
|
|
|
Res.isPositive() != LHS.isPositive())
|
|
|
|
Overflow = true; // Overflow for signed subtraction.
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
|
|
|
case tok::lessequal:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS <= RHS;
|
|
|
|
Res.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-10 15:07:11 +08:00
|
|
|
Res = LHS < RHS;
|
|
|
|
Res.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-10 15:07:11 +08:00
|
|
|
Res = LHS >= RHS;
|
|
|
|
Res.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-10 15:07:11 +08:00
|
|
|
Res = LHS > RHS;
|
|
|
|
Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::exclaimequal:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS != RHS;
|
|
|
|
Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::equalequal:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = LHS == RHS;
|
|
|
|
Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
|
|
|
|
break;
|
|
|
|
case tok::amp:
|
|
|
|
Res = LHS & RHS;
|
|
|
|
break;
|
|
|
|
case tok::caret:
|
|
|
|
Res = LHS ^ RHS;
|
|
|
|
break;
|
|
|
|
case tok::pipe:
|
|
|
|
Res = LHS | RHS;
|
2007-04-04 14:46:55 +08:00
|
|
|
break;
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::ampamp:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = (LHS != 0 && RHS != 0);
|
|
|
|
Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::pipepipe:
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = (LHS != 0 || RHS != 0);
|
|
|
|
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:
|
2006-07-05 02:11:39 +08:00
|
|
|
PP.Diag(OpToken, diag::ext_pp_comma_expr);
|
2007-04-10 15:07:11 +08:00
|
|
|
Res = RHS; // 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)) {
|
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-06-16 07:05:46 +08:00
|
|
|
llvm::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-10 15:07:11 +08:00
|
|
|
Res = 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.
|
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.
|
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;
|
|
|
|
}
|
2007-04-10 14:54:33 +08:00
|
|
|
|
|
|
|
// If this operator is live and overflowed, report the issue.
|
|
|
|
if (Overflow && ValueLive)
|
|
|
|
PP.Diag(OpToken, diag::warn_pp_expr_overflow);
|
2007-04-10 15:07:11 +08:00
|
|
|
|
|
|
|
// Put the result back into 'LHS' for our next iteration.
|
|
|
|
LHS = Res;
|
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.
|
|
|
|
unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::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.
|
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;
|
|
|
|
|
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.
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResVal != 0;
|
|
|
|
}
|
|
|
|
|