Skip to content

Conversation

tinnamchoi
Copy link
Contributor

This check, introduced in 0bf3140, has not been necessary since 21673c4

// C99 6.5.7p2: Each of the operands shall have integer type.
if (!lex->getType()->hasIntegerRepresentation() ||
!rex->getType()->hasIntegerRepresentation())
return InvalidOperands(Loc, lex, rex);
// C++0x: Don't allow scoped enums. FIXME: Use something better than
// hasIntegerRepresentation() above instead of this.
if (isScopedEnumerationType(lex->getType()) ||
isScopedEnumerationType(rex->getType())) {
return InvalidOperands(Loc, lex, rex);
}

bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
BT->getKind() <= BuiltinType::Int128;
if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
// Incomplete enum types are not treated as integer types.
// FIXME: In C++, enum types are never integer types.
return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
return false;
}
bool Type::hasIntegerRepresentation() const {
if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isIntegerType();
else
return isIntegerType();
}

This check, introduced in 0bf3140, has not been necessary since 21673c4
Copy link

github-actions bot commented Aug 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-clang

Author: Timothy Choi (tinnamchoi)

Changes

This check, introduced in 0bf3140, has not been necessary since 21673c4

// C99 6.5.7p2: Each of the operands shall have integer type.
if (!lex->getType()->hasIntegerRepresentation() ||
!rex->getType()->hasIntegerRepresentation())
return InvalidOperands(Loc, lex, rex);
// C++0x: Don't allow scoped enums. FIXME: Use something better than
// hasIntegerRepresentation() above instead of this.
if (isScopedEnumerationType(lex->getType()) ||
isScopedEnumerationType(rex->getType())) {
return InvalidOperands(Loc, lex, rex);
}

bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
BT->getKind() <= BuiltinType::Int128;
if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
// Incomplete enum types are not treated as integer types.
// FIXME: In C++, enum types are never integer types.
return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
return false;
}
bool Type::hasIntegerRepresentation() const {
if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isIntegerType();
else
return isIntegerType();
}


Full diff: https://github.com/llvm/llvm-project/pull/152865.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (-6)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6793d6da85cb1..c805ef205beba 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11747,12 +11747,6 @@ QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS,
       !RHSType->hasIntegerRepresentation())
     return InvalidOperands(Loc, LHS, RHS);
 
-  // C++0x: Don't allow scoped enums. FIXME: Use something better than
-  // hasIntegerRepresentation() above instead of this.
-  if (isScopedEnumerationType(LHSType) ||
-      isScopedEnumerationType(RHSType)) {
-    return InvalidOperands(Loc, LHS, RHS);
-  }
   DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType);
 
   // "The type of the result is that of the promoted left operand."

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is fine because we don’t treat scoped enums as integer types, but we don’t seem to have any tests for this, so could you add some to make sure that we do reject shifts where one (or both) operands are scoped enums (in C and C++).

@tinnamchoi
Copy link
Contributor Author

What would the tests in C look like? Scoped enums don't exist in C.

Other than that, the tests to be introduced in #152698 should cover this, running locally seems fine.

0 << E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:10-[[@LINE-5]]:10}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"
0 >> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:10-[[@LINE-5]]:10}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"

a <<= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:11-[[@LINE-2]]:11}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:18-[[@LINE-3]]:18}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:11-[[@LINE-5]]:11}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"
a >>= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:11-[[@LINE-2]]:11}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:18-[[@LINE-3]]:18}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:11-[[@LINE-5]]:11}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"

b <<= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
b >>= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}

@Sirraide
Copy link
Member

What would the tests in C look like? Scoped enums don't exist in C.

Yeah, ignore that; I was already tired yesterday and for some reason thought C had them too (it doesn’t; what it does have now is enums with a fixed underlying type, which is probably what I was thinking of)

Other than that, the tests to be introduced in #152698 should cover this, running locally seems fine.

I mean, it might honestly make more sense then to just integrate this change into that pr seeing as this one is pretty small and you’re basically already refactoring how checking around scoped enums works in the other one.

@tinnamchoi
Copy link
Contributor Author

I prefer keeping my PRs atomic. All of my changes in that PR are necessary towards the primary goal of that PR, whereas this change wouldn't be. If I allowed myself to make refactors whenever I'm working on a feature it would introduce quite a bit of noise, which seems like a bad habit to have.

@Sirraide
Copy link
Member

I prefer keeping my PRs atomic. All of my changes in that PR are necessary towards the primary goal of that PR, whereas this change wouldn't be. If I allowed myself to make refactors whenever I'm working on a feature it would introduce quite a bit of noise, which seems like a bad habit to have.

I mean, altenratively we could just wait until the other pr is merged and then merge this one afterwards; I just don’t like the idea of making a non-obvious change if there are no tests at all for it (unless we already have test that I missed but I couldn’t find any).

@tinnamchoi
Copy link
Contributor Author

Agreed, let's wait for that then.

@tinnamchoi
Copy link
Contributor Author

@Sirraide The other PR got merged, this should be good upon green CI rerun

@Sirraide Sirraide enabled auto-merge (squash) August 23, 2025 13:07
@Sirraide
Copy link
Member

CI failure seems to be due to something in LLDB and it looks like other prs are suffering from the same issue.

@Sirraide Sirraide disabled auto-merge August 23, 2025 13:30
@Sirraide Sirraide merged commit 66eaa80 into llvm:main Aug 23, 2025
8 of 9 checks passed
Copy link

@tinnamchoi Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 23, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building clang at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/23112

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-shell :: Subprocess/clone-follow-child-softbp.test (1689 of 2313)
PASS: lldb-shell :: Settings/TestStopCommandSourceOnError.test (1690 of 2313)
PASS: lldb-shell :: Subprocess/clone-follow-child.test (1691 of 2313)
PASS: lldb-shell :: Settings/TestFrameFormatName.test (1692 of 2313)
PASS: lldb-shell :: Subprocess/clone-follow-parent-softbp.test (1693 of 2313)
PASS: lldb-shell :: Subprocess/clone-follow-parent.test (1694 of 2313)
PASS: lldb-shell :: Subprocess/fork-follow-child-softbp.test (1695 of 2313)
PASS: lldb-shell :: Subprocess/fork-follow-child.test (1696 of 2313)
PASS: lldb-shell :: Subprocess/fork-follow-parent-softbp.test (1697 of 2313)
UNRESOLVED: lldb-api :: functionalities/statusline/TestStatusline.py (1698 of 2313)
******************** TEST 'lldb-api :: functionalities/statusline/TestStatusline.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --cmake-build-type Release /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/functionalities/statusline -p TestStatusline.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision 66eaa80c39ae0f8aa6cd58024ac6562037038b5f)
  clang revision 66eaa80c39ae0f8aa6cd58024ac6562037038b5f
  llvm revision 66eaa80c39ae0f8aa6cd58024ac6562037038b5f
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test (TestStatusline.TestStatusline)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_deadlock (TestStatusline.TestStatusline)
lldb-server exiting...
FAIL: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_modulelist_deadlock (TestStatusline.TestStatusline)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_no_color (TestStatusline.TestStatusline)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_no_target (TestStatusline.TestStatusline)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_resize (TestStatusline.TestStatusline)
======================================================================
ERROR: test_modulelist_deadlock (TestStatusline.TestStatusline)
   Regression test for a deadlock that occurs when the status line is enabled before connecting
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/functionalities/statusline/TestStatusline.py", line 199, in test_modulelist_deadlock
    self.expect(
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/packages/Python/lldbsuite/test/lldbpexpect.py", line 94, in expect
    self.child.expect_exact(s)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/spawnbase.py", line 432, in expect_exact
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 181, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 144, in timeout
    raise exc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants