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"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-10-15 03:03:49 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/CodeCompletionHandler.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
2007-04-05 13:24:00 +08:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2011-09-23 13:35:21 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-03-19 07:22:28 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include <cassert>
|
|
|
|
|
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;
|
2016-04-16 08:07:09 +08:00
|
|
|
IdentifierInfo *II;
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2008-05-05 14:45:50 +08:00
|
|
|
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
|
|
|
|
2016-04-16 08:07:09 +08:00
|
|
|
// If this value was produced by directly evaluating an identifier, produce
|
|
|
|
// that identifier.
|
|
|
|
IdentifierInfo *getIdentifier() const { return II; }
|
|
|
|
void setIdentifier(IdentifierInfo *II) { this->II = II; }
|
|
|
|
|
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
|
|
|
|
2015-10-04 12:53:55 +08:00
|
|
|
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); }
|
|
|
|
};
|
|
|
|
|
2016-09-08 05:53:17 +08:00
|
|
|
} // end anonymous namespace
|
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) {
|
2013-07-20 02:50:04 +08:00
|
|
|
SourceLocation beginLoc(PeekTok.getLocation());
|
|
|
|
Result.setBegin(beginLoc);
|
2009-10-30 21:49:06 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2014-05-21 14:13:51 +08:00
|
|
|
|
2009-10-30 21:49:06 +08:00
|
|
|
// If we don't have a pp-identifier now, this is an error.
|
2014-10-25 01:31:32 +08:00
|
|
|
if (PP.CheckMacroName(PeekTok, MU_Other))
|
2009-10-30 21:49:06 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, we got an identifier, is it defined to something?
|
2014-05-21 14:13:51 +08:00
|
|
|
IdentifierInfo *II = PeekTok.getIdentifierInfo();
|
2015-05-04 10:25:31 +08:00
|
|
|
MacroDefinition Macro = PP.getMacroDefinition(II);
|
2015-04-30 07:20:19 +08:00
|
|
|
Result.Val = !!Macro;
|
2015-05-04 10:25:31 +08:00
|
|
|
Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
|
2009-10-30 21:49:06 +08:00
|
|
|
|
|
|
|
// If there is a macro, mark it used.
|
2015-04-30 07:20:19 +08:00
|
|
|
if (Result.Val != 0 && ValueLive)
|
|
|
|
PP.markMacroAsUsed(Macro.getMacroInfo());
|
2009-10-30 21:49:06 +08:00
|
|
|
|
2013-07-20 02:50:04 +08:00
|
|
|
// Save macro token for callback.
|
|
|
|
Token macroToken(PeekTok);
|
2011-10-14 08:49:43 +08:00
|
|
|
|
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)) {
|
2013-12-30 09:59:29 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
|
|
|
|
<< "'defined'" << tok::r_paren;
|
2013-12-24 17:48:30 +08:00
|
|
|
PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
|
2009-10-30 21:49:06 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-19 23:15:31 +08:00
|
|
|
// [cpp.cond]p4:
|
|
|
|
// Prior to evaluation, macro invocations in the list of preprocessing
|
|
|
|
// tokens that will become the controlling constant expression are replaced
|
|
|
|
// (except for those macro names modified by the 'defined' unary operator),
|
|
|
|
// just as in normal text. If the token 'defined' is generated as a result
|
|
|
|
// of this replacement process or use of the 'defined' unary operator does
|
|
|
|
// not match one of the two specified forms prior to macro replacement, the
|
|
|
|
// behavior is undefined.
|
|
|
|
// This isn't an idle threat, consider this program:
|
|
|
|
// #define FOO
|
|
|
|
// #define BAR defined(FOO)
|
|
|
|
// #if BAR
|
|
|
|
// ...
|
|
|
|
// #else
|
|
|
|
// ...
|
|
|
|
// #endif
|
|
|
|
// clang and gcc will pick the #if branch while Visual Studio will take the
|
|
|
|
// #else branch. Emit a warning about this undefined behavior.
|
|
|
|
if (beginLoc.isMacroID()) {
|
|
|
|
bool IsFunctionTypeMacro =
|
|
|
|
PP.getSourceManager()
|
|
|
|
.getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
|
|
|
|
.getExpansion()
|
|
|
|
.isFunctionMacroExpansion();
|
|
|
|
// For object-type macros, it's easy to replace
|
|
|
|
// #define FOO defined(BAR)
|
|
|
|
// with
|
|
|
|
// #if defined(BAR)
|
|
|
|
// #define FOO 1
|
|
|
|
// #else
|
|
|
|
// #define FOO 0
|
|
|
|
// #endif
|
|
|
|
// and doing so makes sense since compilers handle this differently in
|
|
|
|
// practice (see example further up). But for function-type macros,
|
|
|
|
// there is no good way to write
|
|
|
|
// # define FOO(x) (defined(M_ ## x) && M_ ## x)
|
|
|
|
// in a different way, and compilers seem to agree on how to behave here.
|
|
|
|
// So warn by default on object-type macros, but only warn in -pedantic
|
|
|
|
// mode on function-type macros.
|
|
|
|
if (IsFunctionTypeMacro)
|
|
|
|
PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
|
|
|
|
else
|
|
|
|
PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
|
|
|
|
}
|
|
|
|
|
2013-07-20 02:50:04 +08:00
|
|
|
// Invoke the 'defined' callback.
|
|
|
|
if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
|
2015-05-04 11:15:40 +08:00
|
|
|
Callbacks->Defined(macroToken, Macro,
|
2013-07-20 02:50:04 +08:00
|
|
|
SourceRange(beginLoc, PeekTok.getLocation()));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
|
|
|
|
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"))
|
2016-01-19 23:15:31 +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".
|
2012-09-20 10:38:38 +08:00
|
|
|
if (ValueLive &&
|
|
|
|
II->getTokenID() != tok::kw_true &&
|
|
|
|
II->getTokenID() != tok::kw_false)
|
2009-12-14 12:26:45 +08:00
|
|
|
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.
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(II);
|
2009-12-14 12:26:45 +08:00
|
|
|
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
|
|
|
|
|
2012-09-24 17:53:54 +08:00
|
|
|
NumericLiteralParser Literal(Spelling, 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
|
|
|
|
2012-03-08 16:45:32 +08:00
|
|
|
// Complain about, and drop, any ud-suffix.
|
|
|
|
if (Literal.hasUDSuffix())
|
|
|
|
PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
|
|
|
|
|
2012-09-25 02:19:21 +08:00
|
|
|
// 'long long' is a C99 or C++11 feature.
|
|
|
|
if (!PP.getLangOpts().C99 && Literal.isLongLong) {
|
|
|
|
if (PP.getLangOpts().CPlusPlus)
|
|
|
|
PP.Diag(PeekTok,
|
2013-01-02 19:42:31 +08:00
|
|
|
PP.getLangOpts().CPlusPlus11 ?
|
2012-09-25 02:19:21 +08:00
|
|
|
diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
|
|
|
|
else
|
|
|
|
PP.Diag(PeekTok, diag::ext_c99_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.
|
2014-07-22 22:08:09 +08:00
|
|
|
if (ValueLive)
|
2014-07-24 22:51:23 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_integer_literal_too_large)
|
|
|
|
<< /* Unsigned */ 1;
|
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()) {
|
2014-03-15 05:21:24 +08:00
|
|
|
// Octal, hexadecimal, and binary literals are implicitly unsigned if
|
|
|
|
// the value does not fit into a signed integer type.
|
2013-07-23 08:25:18 +08:00
|
|
|
if (ValueLive && Literal.getRadix() == 10)
|
2014-07-24 22:51:23 +08:00
|
|
|
PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
|
2008-05-05 14:45:50 +08:00
|
|
|
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'
|
2013-02-01 03:03:16 +08:00
|
|
|
case tok::wide_char_constant: // L'x'
|
2014-11-08 14:08:42 +08:00
|
|
|
case tok::utf8_char_constant: // u8'x'
|
2011-07-27 13:40:30 +08:00
|
|
|
case tok::utf16_char_constant: // u'x'
|
2013-02-01 03:03:16 +08:00
|
|
|
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())
|
2012-03-08 16:45:32 +08:00
|
|
|
PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
|
2012-03-06 11:21:47 +08:00
|
|
|
|
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
|
2015-02-24 21:34:20 +08:00
|
|
|
if (Literal.isWide())
|
|
|
|
Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
|
|
|
|
else if (!Literal.isUTF16() && !Literal.isUTF32())
|
2012-03-11 15:00:24 +08:00
|
|
|
Val.setIsUnsigned(!PP.getLangOpts().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();
|
2013-12-24 17:48:30 +08:00
|
|
|
PP.Diag(Start, diag::note_matching) << tok::l_paren;
|
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());
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
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);
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
2008-05-05 14:45:50 +08:00
|
|
|
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);
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
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);
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
2008-05-05 14:45:50 +08:00
|
|
|
|
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);
|
2016-04-16 08:07:09 +08:00
|
|
|
Result.setIdentifier(nullptr);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 08:07:09 +08:00
|
|
|
static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
|
|
|
|
Token &Tok) {
|
|
|
|
if (Tok.is(tok::l_paren) && LHS.getIdentifier())
|
|
|
|
PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
|
|
|
|
<< LHS.getIdentifier();
|
|
|
|
else
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
|
|
|
|
<< LHS.getRange();
|
|
|
|
}
|
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) {
|
2016-04-16 08:07:09 +08:00
|
|
|
diagnoseUnexpectedOperator(PP, LHS, PeekTok);
|
2006-06-18 13:43:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-09-08 05:53:17 +08:00
|
|
|
while (true) {
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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) {
|
2016-04-16 08:07:09 +08:00
|
|
|
diagnoseUnexpectedOperator(PP, RHS, PeekTok);
|
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())
|
2015-11-14 10:09:55 +08:00
|
|
|
PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
|
2008-11-19 05:48:13 +08:00
|
|
|
<< 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())
|
2015-11-14 10:09:55 +08:00
|
|
|
PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
|
2008-11-19 05:48:13 +08:00
|
|
|
<< 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.
|
2014-10-14 06:18:22 +08:00
|
|
|
if (LHS.isUnsigned())
|
|
|
|
Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
|
|
|
|
else
|
|
|
|
Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, 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());
|
2016-02-19 06:34:54 +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.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!PP.getLangOpts().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)) {
|
2013-12-31 07:29:50 +08:00
|
|
|
PP.Diag(PeekTok.getLocation(), diag::err_expected)
|
|
|
|
<< tok::colon << LHS.getRange() << RHS.getRange();
|
2013-12-24 17:48:30 +08:00
|
|
|
PP.Diag(OpLoc, diag::note_matching) << tok::question;
|
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());
|
2016-04-16 08:07:09 +08:00
|
|
|
RHS.setIdentifier(nullptr);
|
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.
|
2015-05-03 03:29:29 +08:00
|
|
|
bool Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
|
2013-03-19 07:22:28 +08:00
|
|
|
SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
|
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
|
|
|
}
|