Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Clang] Fix handling of reference types in tryEvaluateBuiltinObjectSize #138247

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

Merged
merged 2 commits into from
May 2, 2025

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented May 2, 2025

The order of operation was slightly incorrect, as we were checking for incomplete types before handling reference types.

Fixes #129397

The order of operation was slightly incorrect, as we were checking
for incomplete types *before* handling reference types.

Fixes llvm#129397
@cor3ntin cor3ntin requested review from AaronBallman and erichkeane and removed request for AaronBallman May 2, 2025 10:02
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

The order of operation was slightly incorrect, as we were checking for incomplete types before handling reference types.

Fixes #129397


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3-1)
  • (modified) clang/lib/AST/ExprConstant.cpp (+5-3)
  • (modified) clang/test/SemaCXX/builtin-object-size-cxx14.cpp (+12)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 95e0574562a2d..b2b44c29bdf24 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -623,6 +623,8 @@ Bug Fixes to C++ Support
 - Clang now emits a warning when class template argument deduction for alias templates is used in C++17. (#GH133806)
 - Fix a crash when checking the template template parameters of a dependent lambda appearing in an alias declaration.
   (#GH136432), (#GH137014), (#GH138018)
+- Fixed an assertion when trying to constant-fold various builtins when the argument
+  refered to a reference to an incomplete type. (#GH129397)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -769,7 +771,7 @@ clang-format
 
 libclang
 --------
-- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different 
+- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different
   in-memory files to be considered as equal.
 - Added ``clang_visitCXXMethods``, which allows visiting the methods
   of a class.
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f2e49b9ea669e..b79d8c197fe7d 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12772,11 +12772,13 @@ static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
   bool DetermineForCompleteObject = refersToCompleteObject(LVal);
 
   auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
-    if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType())
+    if (Ty.isNull())
       return false;
 
-    if (Ty->isReferenceType())
-      Ty = Ty.getNonReferenceType();
+    Ty = Ty.getNonReferenceType();
+
+    if (Ty->isIncompleteType() || Ty->isFunctionType())
+      return false;
 
     return HandleSizeof(Info, ExprLoc, Ty, Result);
   };
diff --git a/clang/test/SemaCXX/builtin-object-size-cxx14.cpp b/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
index b7c6f6be01f54..fdd3cb7af088f 100644
--- a/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
+++ b/clang/test/SemaCXX/builtin-object-size-cxx14.cpp
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx14 -std=c++14 %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2b %s
+
 
 typedef __SIZE_TYPE__ size_t;
 
@@ -119,3 +121,13 @@ constexpr int bos_new() { // cxx14-error {{constant expression}}
   void *p = new int; // cxx14-note {{until C++20}}
   return __builtin_object_size(p, 0);
 }
+
+
+namespace GH129397 {
+
+struct incomplete;
+void test(incomplete &ref) {
+  __builtin_object_size(&ref, 1);
+}
+
+}

@cor3ntin cor3ntin merged commit cb068dc into llvm:main May 2, 2025
12 checks passed
@zhscn
Copy link

zhscn commented May 3, 2025

Could this PR be backported to the release/20.x branch which also has the same issue?

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
@zhscn
Copy link

zhscn commented May 12, 2025

ping @cor3ntin @erichkeane

@erichkeane
Copy link
Collaborator

Could this PR be backported to the release/20.x branch which also has the same issue?

I don't believe so, typically we save backports for regressions since the previous version. Although, I tried messing with the example, and I don't see a difference in behavior between the 20.1 and the trunk branch?

@zhscn
Copy link

zhscn commented May 12, 2025

Could this PR be backported to the release/20.x branch which also has the same issue?

I don't believe so, typically we save backports for regressions since the previous version. Although, I tried messing with the example, and I don't see a difference in behavior between the 20.1 and the trunk branch?

Please check out https://godbolt.org/z/shTezPvx4

@erichkeane
Copy link
Collaborator

Could this PR be backported to the release/20.x branch which also has the same issue?

I don't believe so, typically we save backports for regressions since the previous version. Although, I tried messing with the example, and I don't see a difference in behavior between the 20.1 and the trunk branch?

Please check out https://godbolt.org/z/shTezPvx4

Ah, huh... it DOES look like this i doesn't fail in 19, right? @cor3ntin : WDYT?

@zhscn
Copy link

zhscn commented May 12, 2025

Yeah, clang 19 works well.

@cor3ntin
Copy link
Contributor Author

/cherry-pick cb068dc

@llvmbot
Copy link
Member

llvmbot commented May 12, 2025

Failed to cherry-pick: cb068dc

https://github.com/llvm/llvm-project/actions/runs/14974508844

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

cor3ntin added a commit to cor3ntin/llvm-project that referenced this pull request May 12, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
@cor3ntin
Copy link
Contributor Author

#139579

swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request May 13, 2025
…ze (llvm#138247)

The order of operation was slightly incorrect, as we were checking for
incomplete types *before* handling reference types.

Fixes llvm#129397

---------

Co-authored-by: Erich Keane <[email protected]>
@tstellar tstellar moved this from Needs Triage to Done in LLVM Release Status May 13, 2025
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 release:cherry-pick-failed
Projects
Development

Successfully merging this pull request may close these issues.

ICE with clang-21 HEAD
4 participants