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"
|
2010-08-25 04:21:13 +08:00
|
|
|
#include "clang/Lex/CodeCompletionHandler.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"
|
2011-09-23 13:35:21 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-07-27 05:25:24 +08:00
|
|
|
namespace {
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
// Default ctor - Construct an 'invalid' PPValue.
|
|
|
|
PPValue(unsigned BitWidth) : Val(BitWidth) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
unsigned getBitWidth() const { return Val.getBitWidth(); }
|
|
|
|
bool isUnsigned() const { return Val.isUnsigned(); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
const SourceRange &getRange() const { return Range; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
|
|
|
|
void setRange(SourceLocation B, SourceLocation E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Range.setBegin(B); Range.setEnd(E);
|
2008-05-05 14:45:50 +08:00
|
|
|
}
|
|
|
|
void setBegin(SourceLocation L) { Range.setBegin(L); }
|
|
|
|
void setEnd(SourceLocation L) { Range.setEnd(L); }
|
|
|
|
};
|
|
|
|
|
2010-07-27 05:25:24 +08:00
|
|
|
}
|
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
/// EvaluateDefined - Process a 'defined(sym)' expression.
|
2009-12-14 13:00:18 +08:00
|
|
|
static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
|
|
|
|
bool ValueLive, Preprocessor &PP) {
|
2009-10-30 21:49:06 +08:00
|
|
|
IdentifierInfo *II;
|
|
|
|
Result.setBegin(PeekTok.getLocation());
|
|
|
|
|
|
|
|
// Get the next token, don't expand it.
|
2011-08-03 08:04:13 +08:00
|
|
|
PP.LexUnexpandedNonComment(PeekTok);
|
2009-10-30 21:49:06 +08:00
|
|
|
|
|
|
|
// Two options, it can either be a pp-identifier or a (.
|
|
|
|
SourceLocation LParenLoc;
|
|
|
|
if (PeekTok.is(tok::l_paren)) {
|
|
|
|
// Found a paren, remember we saw it and skip it.
|
|
|
|
LParenLoc = PeekTok.getLocation();
|
2011-08-03 08:04:13 +08:00
|
|
|
PP.LexUnexpandedNonComment(PeekTok);
|
2009-10-30 21:49:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-25 04:21:13 +08:00
|
|
|
if (PeekTok.is(tok::code_completion)) {
|
|
|
|
if (PP.getCodeCompletionHandler())
|
|
|
|
PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
|
2011-09-04 11:32:15 +08:00
|
|
|
PP.setCodeCompletionReached();
|
2011-08-03 08:04:13 +08:00
|
|
|
PP.LexUnexpandedNonComment(PeekTok);
|
2010-08-25 04:21:13 +08:00
|
|
|
}
|
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
// If we don't have a pp-identifier now, this is an error.
|
|
|
|
if ((II = PeekTok.getIdentifierInfo()) == 0) {
|
|
|
|
PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we got an identifier, is it defined to something?
|
|
|
|
Result.Val = II->hasMacroDefinition();
|
|
|
|
Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
|
|
|
|
|
|
|
|
// If there is a macro, mark it used.
|
|
|
|
if (Result.Val != 0 && ValueLive) {
|
|
|
|
MacroInfo *Macro = PP.getMacroInfo(II);
|
2010-12-16 02:44:22 +08:00
|
|
|
PP.markMacroAsUsed(Macro);
|
2009-10-30 21:49:06 +08:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:49:43 +08:00
|
|
|
// Invoke the 'defined' callback.
|
|
|
|
if (PPCallbacks *Callbacks = PP.getPPCallbacks())
|
|
|
|
Callbacks->Defined(PeekTok);
|
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
// If we are in parens, ensure we have a trailing ).
|
|
|
|
if (LParenLoc.isValid()) {
|
2011-08-03 08:04:13 +08:00
|
|
|
// Consume identifier.
|
|
|
|
Result.setEnd(PeekTok.getLocation());
|
|
|
|
PP.LexUnexpandedNonComment(PeekTok);
|
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
if (PeekTok.isNot(tok::r_paren)) {
|
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
|
|
|
|
PP.Diag(LParenLoc, diag::note_matching) << "(";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Consume the ).
|
|
|
|
Result.setEnd(PeekTok.getLocation());
|
|
|
|
PP.LexNonComment(PeekTok);
|
2011-08-03 08:04:13 +08:00
|
|
|
} else {
|
|
|
|
// Consume identifier.
|
|
|
|
Result.setEnd(PeekTok.getLocation());
|
|
|
|
PP.LexNonComment(PeekTok);
|
2009-10-30 21:49:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Success, remember that we saw defined(X).
|
|
|
|
DT.State = DefinedTracker::DefinedMacro;
|
|
|
|
DT.TheMacro = II;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 06:20:20 +08:00
|
|
|
if (PeekTok.is(tok::code_completion)) {
|
|
|
|
if (PP.getCodeCompletionHandler())
|
|
|
|
PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
|
2011-09-04 11:32:15 +08:00
|
|
|
PP.setCodeCompletionReached();
|
2011-08-03 08:04:13 +08:00
|
|
|
PP.LexNonComment(PeekTok);
|
2010-08-25 06:20:20 +08:00
|
|
|
}
|
|
|
|
|
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()) {
|
2009-12-14 12:26:45 +08:00
|
|
|
// Handle "defined X" and "defined(X)".
|
|
|
|
if (II->isStr("defined"))
|
2009-10-30 21:49:06 +08:00
|
|
|
return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
|
2009-12-14 12:26:45 +08:00
|
|
|
|
|
|
|
// If this identifier isn't 'defined' or one of the special
|
|
|
|
// preprocessor keywords and it wasn't macro expanded, it turns
|
|
|
|
// into a simple 0, unless it is the C++ keyword "true", in which case it
|
|
|
|
// turns into "1".
|
|
|
|
if (ValueLive)
|
|
|
|
PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
|
|
|
|
Result.Val = II->getTokenID() == tok::kw_true;
|
|
|
|
Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
|
|
|
|
Result.setRange(PeekTok.getLocation());
|
|
|
|
PP.LexNonComment(PeekTok);
|
|
|
|
return false;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::eod:
|
2006-06-18 13:43:12 +08:00
|
|
|
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: {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> IntegerBuffer;
|
2010-03-16 13:20:39 +08:00
|
|
|
bool NumberInvalid = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
|
2010-03-16 13:20:39 +08:00
|
|
|
&NumberInvalid);
|
|
|
|
if (NumberInvalid)
|
|
|
|
return true; // a diagnostic was already reported
|
|
|
|
|
2010-02-28 00:29:36 +08:00
|
|
|
NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
|
2007-03-13 07:22:38 +08:00
|
|
|
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.
|
2009-09-09 23:08:12 +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.
|
2011-10-15 09:18:56 +08:00
|
|
|
if (!PP.getLangOptions().C99 && Literal.isLongLong)
|
|
|
|
PP.Diag(PeekTok, PP.getLangOptions().CPlusPlus0x ?
|
|
|
|
diag::warn_cxx98_compat_longlong : diag::ext_longlong);
|
2007-08-30 06:00:19 +08:00
|
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
2011-07-27 13:40:30 +08:00
|
|
|
case tok::char_constant: // 'x'
|
|
|
|
case tok::wide_char_constant: { // L'x'
|
|
|
|
case tok::utf16_char_constant: // u'x'
|
|
|
|
case tok::utf32_char_constant: // U'x'
|
2012-03-06 11:21:47 +08:00
|
|
|
// Complain about, and drop, any ud-suffix.
|
|
|
|
if (PeekTok.hasUDSuffix())
|
|
|
|
PP.Diag(PeekTok, diag::err_pp_invalid_char_udl);
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<32> CharBuffer;
|
2010-03-16 13:20:39 +08:00
|
|
|
bool CharInvalid = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
|
2010-03-16 13:20:39 +08:00
|
|
|
if (CharInvalid)
|
|
|
|
return true;
|
2010-02-27 21:44:12 +08:00
|
|
|
|
|
|
|
CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
|
2011-07-27 13:40:30 +08:00
|
|
|
PeekTok.getLocation(), PP, PeekTok.getKind());
|
2007-04-05 14:58:56 +08:00
|
|
|
if (Literal.hadError())
|
|
|
|
return true; // A diagnostic was already emitted.
|
|
|
|
|
|
|
|
// Character literals are always int or wchar_t, expand to intmax_t.
|
2009-11-13 13:51:54 +08:00
|
|
|
const TargetInfo &TI = PP.getTargetInfo();
|
2009-06-01 13:25:02 +08:00
|
|
|
unsigned NumBits;
|
|
|
|
if (Literal.isMultiChar())
|
|
|
|
NumBits = TI.getIntWidth();
|
2009-07-14 14:30:34 +08:00
|
|
|
else if (Literal.isWide())
|
|
|
|
NumBits = TI.getWCharWidth();
|
2011-07-27 13:40:30 +08:00
|
|
|
else if (Literal.isUTF16())
|
|
|
|
NumBits = TI.getChar16Width();
|
|
|
|
else if (Literal.isUTF32())
|
|
|
|
NumBits = TI.getChar32Width();
|
2009-06-01 13:25:02 +08:00
|
|
|
else
|
2009-07-14 14:30:34 +08:00
|
|
|
NumBits = TI.getCharWidth();
|
2009-06-01 13:25:02 +08:00
|
|
|
|
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();
|
2011-07-27 13:40:30 +08:00
|
|
|
// Set the signedness. UTF-16 and UTF-32 are always unsigned
|
|
|
|
if (!Literal.isUTF16() && !Literal.isUTF32())
|
|
|
|
Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
|
2009-09-09 23:08:12 +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;
|
2009-09-09 23:08:12 +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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
2009-09-09 23:08:12 +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();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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();
|
2009-09-09 23:08:12 +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
|
|
|
}
|
2009-09-09 23:08: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
|
|
|
}
|
2009-09-09 23:08: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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +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.
|
2011-02-28 10:37:51 +08:00
|
|
|
/// 0 - 'eod' 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;
|
2011-02-28 10:37:51 +08:00
|
|
|
case tok::r_paren: return 0;// Lowest priority, end of expr.
|
|
|
|
case tok::eod: return 0;// Lowest priority, end of directive.
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
tok::TokenKind Operator = PeekTok.getKind();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-10 13:26:38 +08:00
|
|
|
// If this is a short-circuiting operator, see if the RHS of the operator is
|
2009-09-09 23:08:12 +08:00
|
|
|
// dead. Note that this cannot just clobber ValueLive. Consider
|
2007-04-10 13:26:38 +08:00
|
|
|
// "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;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
2009-09-09 23:08: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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
// Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
|
2009-09-09 23:08:12 +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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-10 14:54:33 +08:00
|
|
|
bool Overflow = false;
|
2006-06-18 13:43:12 +08:00
|
|
|
switch (Operator) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("Unknown operator token!");
|
2006-06-18 13:43:12 +08:00
|
|
|
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) {
|
2010-10-14 07:46:56 +08:00
|
|
|
if (LHS.Val.isSigned())
|
|
|
|
Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
|
|
|
|
else
|
|
|
|
Res = LHS.Val / RHS.Val;
|
2008-05-05 14:45:50 +08:00
|
|
|
} 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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 13:24:00 +08:00
|
|
|
case tok::star:
|
2010-10-14 07:46:56 +08:00
|
|
|
if (Res.isSigned())
|
|
|
|
Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
|
|
|
|
else
|
|
|
|
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());
|
2010-10-14 07:46:56 +08:00
|
|
|
if (LHS.isUnsigned()) {
|
|
|
|
Overflow = ShAmt >= LHS.Val.getBitWidth();
|
|
|
|
if (Overflow)
|
|
|
|
ShAmt = LHS.Val.getBitWidth()-1;
|
|
|
|
Res = LHS.Val << ShAmt;
|
|
|
|
} else {
|
|
|
|
Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
|
|
|
|
}
|
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:
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
2010-10-14 07:46:56 +08:00
|
|
|
Res = LHS.Val + RHS.Val;
|
|
|
|
else
|
|
|
|
Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
|
2007-04-05 13:24:00 +08:00
|
|
|
break;
|
|
|
|
case tok::minus:
|
2007-04-11 11:34:29 +08:00
|
|
|
if (LHS.isUnsigned())
|
2010-10-14 07:46:56 +08:00
|
|
|
Res = LHS.Val - RHS.Val;
|
|
|
|
else
|
|
|
|
Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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) {
|
2009-12-14 13:00:18 +08:00
|
|
|
// Save the current state of 'DisableMacroExpansion' and reset it to false. If
|
|
|
|
// 'DisableMacroExpansion' is true, then we must be in a macro argument list
|
|
|
|
// in which case a directive is undefined behavior. We want macros to be able
|
|
|
|
// to recursively expand in order to get more gcc-list behavior, so we force
|
|
|
|
// DisableMacroExpansion to false and restore it when we're done parsing the
|
|
|
|
// expression.
|
|
|
|
bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
|
|
|
|
DisableMacroExpansion = false;
|
|
|
|
|
2006-07-05 02:11:39 +08:00
|
|
|
// Peek ahead one token.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2011-08-03 08:04:13 +08:00
|
|
|
LexNonComment(Tok);
|
2010-08-25 04:21:13 +08:00
|
|
|
|
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();
|
2009-09-09 23:08:12 +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.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.isNot(tok::eod))
|
2006-07-05 02:11:39 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2009-12-14 13:00:18 +08:00
|
|
|
|
|
|
|
// Restore 'DisableMacroExpansion'.
|
|
|
|
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
|
2006-07-05 02:11:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-05 02:11:39 +08:00
|
|
|
// 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.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.is(tok::eod)) {
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-14 13:00:18 +08:00
|
|
|
// Restore 'DisableMacroExpansion'.
|
|
|
|
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
|
2008-05-05 14:45:50 +08:00
|
|
|
return ResVal.Val != 0;
|
2006-07-05 02:11:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.isNot(tok::eod))
|
2006-07-05 02:11:39 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
2009-12-14 13:00:18 +08:00
|
|
|
|
|
|
|
// Restore 'DisableMacroExpansion'.
|
|
|
|
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
|
2006-07-05 02:11:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// If we aren't at the tok::eod token, something bad happened, like an extra
|
2006-07-05 02:11:39 +08:00
|
|
|
// ')' token.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.isNot(tok::eod)) {
|
2006-07-05 02:11:39 +08:00
|
|
|
Diag(Tok, diag::err_pp_expected_eol);
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-14 13:00:18 +08:00
|
|
|
// Restore 'DisableMacroExpansion'.
|
|
|
|
DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
|
2008-05-05 14:45:50 +08:00
|
|
|
return ResVal.Val != 0;
|
2006-07-05 02:11:39 +08:00
|
|
|
}
|