Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a diagnostic warning about parsing ambiguity in a conjunction #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,10 @@ let CategoryName = "Reflection Issue" in {
def err_cannot_reflect_entity : Error<"cannot reflect entity">;
def err_expansion_statements_disabled : Error<
"'template for' statements are not enabled; use '-fexpansion-statements'">;

def warn_parsing_ambiguity_in_refl_expression_with_ampamp_token : Warning <
"the compound condition may be misinterpreted due to '%0 &&' type parsing logic. did you mean '... (^%0) && ...'?"
>;
}

let CategoryName = "Generics Issue" in {
Expand Down
24 changes: 18 additions & 6 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6750,12 +6750,24 @@ void Parser::ParseDeclaratorInternal(Declarator &D,
// Is a reference
DeclSpec DS(AttrFactory);

// Complain about rvalue references in C++03, but then go on and build
// the declarator.
if (Kind == tok::ampamp)
Diag(Loc, getLangOpts().CPlusPlus11 ?
diag::warn_cxx98_compat_rvalue_reference :
diag::ext_rvalue_reference);
// Complain about:
// - rvalue references in C++03
// - `^T &&` parsing ambiguity of compound expression with reflection in C++2x
// but then go on and build the declarator.
if (Kind == tok::ampamp) {
if (D.getContext() == DeclaratorContext::ReflectOperator) {
// parser already consumed '^' token before setting this context
Diag(Loc,
diag::warn_parsing_ambiguity_in_refl_expression_with_ampamp_token)
<< (D.hasName()
? Actions.GetNameForDeclarator(D).getName().getAsString()
: "T");
}

Diag(Loc, getLangOpts().CPlusPlus11
? diag::warn_cxx98_compat_rvalue_reference
: diag::ext_rvalue_reference);
}

// GNU-style and C++11 attributes are allowed here, as is restrict.
ParseTypeQualifierListOpt(DS);
Expand Down
32 changes: 32 additions & 0 deletions clang/test/Parser/cxx2x-ambig-reflect-expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// Copyright 2024 Bloomberg Finance L.P.
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// RUN: %clang_cc1 -std=c++26 -freflection -verify %s


enum MyEnum { x, y, e = -1, f, z = 99 };

void func(MyEnum && x) { // ok
MyEnum value{};
MyEnum& ref = value;

constexpr auto reflValue = ^value;

constexpr bool test_comparison_0 = reflValue != (^MyEnum) && true; // ok
constexpr bool test_comparison_1 = (reflValue != (^MyEnum) && true); // ok

constexpr bool test_comparison_2 = ^MyEnum != (^MyEnum) && true; // ok
constexpr bool test_comparison_3 = (^MyEnum != (^MyEnum) && true); // ok

constexpr bool test_comparison_4 = reflValue != ^MyEnum && true;
// expected-warning@28 {{the compound condition may be misinterpreted due to 'T &&' type parsing logic. did you mean '... (^T) && ...'?}}
// expected-error@28 {{expected ';' at end of declaration}}
}

Loading