From b2b9a189d9c5ceec53f58fcf22f9f571789eb536 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Sat, 8 Feb 2025 20:35:27 +0100 Subject: [PATCH] Compute types of unary plus operators Signed-off-by: Roberto Raggi --- src/parser/cxx/literals.cc | 4 ++++ src/parser/cxx/parser.cc | 16 ++++++++++++++++ tests/unit_tests/sema/unary_plus_01.cc | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/unit_tests/sema/unary_plus_01.cc diff --git a/src/parser/cxx/literals.cc b/src/parser/cxx/literals.cc index 11a40196..36d591fc 100644 --- a/src/parser/cxx/literals.cc +++ b/src/parser/cxx/literals.cc @@ -729,6 +729,10 @@ auto FloatLiteral::Components::from(std::string_view text, const auto firstChar = literalText.data(); components.value = strtod(firstChar, nullptr); + components.isFloat = components.suffix == FloatingPointSuffix::kF; + components.isLongDouble = components.suffix == FloatingPointSuffix::kL; + components.isDouble = !components.isFloat && !components.isLongDouble; + return components; } diff --git a/src/parser/cxx/parser.cc b/src/parser/cxx/parser.cc index d4533c5a..7c0689ac 100644 --- a/src/parser/cxx/parser.cc +++ b/src/parser/cxx/parser.cc @@ -2930,6 +2930,22 @@ auto Parser::parse_unop_expression(ExpressionAST*& yyast, break; } + case cxx::TokenKind::T_PLUS: { + ExpressionAST* expr = ast->expression; + ensure_prvalue(expr); + auto ty = control_->remove_cvref(expr->type); + if (control_->is_arithmetic_or_unscoped_enum(ty) || + control_->is_pointer(ty)) { + if (control_->is_integral_or_unscoped_enum(ty)) { + (void)integral_promotion(expr); + } + ast->expression = expr; + ast->type = expr->type; + ast->valueCategory = ValueCategory::kPrValue; + } + break; + } + default: break; } // switch diff --git a/tests/unit_tests/sema/unary_plus_01.cc b/tests/unit_tests/sema/unary_plus_01.cc new file mode 100644 index 00000000..99a63924 --- /dev/null +++ b/tests/unit_tests/sema/unary_plus_01.cc @@ -0,0 +1,25 @@ +// RUN: %cxx -verify -fcheck %s + +auto main() -> int { + static_assert(__is_same(decltype(+'a'), int)); + static_assert(__is_same(decltype(+1), int)); + static_assert(__is_same(decltype(+1.0f), float)); + static_assert(__is_same(decltype(+1.0), double)); + + short x{}; + static_assert(__is_same(decltype(+x), int)); + + short& y = x; + static_assert(__is_same(decltype(+y), int)); + + int a[2]; + static_assert(__is_same(decltype(+a), int*)); + + void (*f)() = nullptr; + static_assert(__is_same(decltype(+f), void (*)())); + + const void* p = nullptr; + static_assert(__is_same(decltype(+p), const void*)); + + return 0; +} \ No newline at end of file