-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Handle null base class initialization #167023
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,6 +234,89 @@ RValue CIRGenFunction::emitCXXMemberOrOperatorCall( | |
| return emitCall(fnInfo, callee, returnValue, args, nullptr, loc); | ||
| } | ||
|
|
||
| static void emitNullBaseClassInitialization(CIRGenFunction &cgf, | ||
| Address destPtr, | ||
| const CXXRecordDecl *base) { | ||
| if (base->isEmpty()) | ||
| return; | ||
|
|
||
| cgf.cgm.errorNYI(base->getSourceRange(), | ||
| "emitNullBaseClassInitialization: not empty"); | ||
| } | ||
|
|
||
| void CIRGenFunction::emitCXXConstructExpr(const CXXConstructExpr *e, | ||
| AggValueSlot dest) { | ||
| assert(!dest.isIgnored() && "Must have a destination!"); | ||
| const CXXConstructorDecl *cd = e->getConstructor(); | ||
|
|
||
| // If we require zero initialization before (or instead of) calling the | ||
| // constructor, as can be the case with a non-user-provided default | ||
| // constructor, emit the zero initialization now, unless destination is | ||
| // already zeroed. | ||
| if (e->requiresZeroInitialization() && !dest.isZeroed()) { | ||
| switch (e->getConstructionKind()) { | ||
| case CXXConstructionKind::Delegating: | ||
| case CXXConstructionKind::Complete: | ||
| emitNullInitialization(getLoc(e->getSourceRange()), dest.getAddress(), | ||
| e->getType()); | ||
| break; | ||
| case CXXConstructionKind::VirtualBase: | ||
| case CXXConstructionKind::NonVirtualBase: | ||
| emitNullBaseClassInitialization(*this, dest.getAddress(), | ||
| cd->getParent()); | ||
|
Comment on lines
+265
to
+266
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only changed code in this function. |
||
| break; | ||
| } | ||
| } | ||
|
|
||
| // If this is a call to a trivial default constructor, do nothing. | ||
| if (cd->isTrivial() && cd->isDefaultConstructor()) | ||
| return; | ||
|
|
||
| // Elide the constructor if we're constructing from a temporary | ||
| if (getLangOpts().ElideConstructors && e->isElidable()) { | ||
| // FIXME: This only handles the simplest case, where the source object is | ||
| // passed directly as the first argument to the constructor. This | ||
| // should also handle stepping through implicit casts and conversion | ||
| // sequences which involve two steps, with a conversion operator | ||
| // follwed by a converting constructor. | ||
| const Expr *srcObj = e->getArg(0); | ||
| assert(srcObj->isTemporaryObject(getContext(), cd->getParent())); | ||
| assert( | ||
| getContext().hasSameUnqualifiedType(e->getType(), srcObj->getType())); | ||
| emitAggExpr(srcObj, dest); | ||
| return; | ||
| } | ||
|
|
||
| if (const ArrayType *arrayType = getContext().getAsArrayType(e->getType())) { | ||
| assert(!cir::MissingFeatures::sanitizers()); | ||
| emitCXXAggrConstructorCall(cd, arrayType, dest.getAddress(), e, false); | ||
| } else { | ||
|
|
||
| clang::CXXCtorType type = Ctor_Complete; | ||
| bool forVirtualBase = false; | ||
| bool delegating = false; | ||
|
|
||
| switch (e->getConstructionKind()) { | ||
| case CXXConstructionKind::Complete: | ||
| type = Ctor_Complete; | ||
| break; | ||
| case CXXConstructionKind::Delegating: | ||
| // We should be emitting a constructor; GlobalDecl will assert this | ||
| type = curGD.getCtorType(); | ||
| delegating = true; | ||
| break; | ||
| case CXXConstructionKind::VirtualBase: | ||
| forVirtualBase = true; | ||
| [[fallthrough]]; | ||
| case CXXConstructionKind::NonVirtualBase: | ||
| type = Ctor_Base; | ||
| break; | ||
| } | ||
|
|
||
| emitCXXConstructorCall(cd, type, forVirtualBase, delegating, dest, e); | ||
| } | ||
| } | ||
|
|
||
| static CharUnits calculateCookiePadding(CIRGenFunction &cgf, | ||
| const CXXNewExpr *e) { | ||
| if (!e->isArray()) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir | ||
| // RUN: FileCheck --input-file=%t.cir --check-prefix=CIR %s | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll | ||
| // RUN: FileCheck --input-file=%t-cir.ll --check-prefix=LLVM %s | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll | ||
| // RUN: FileCheck --input-file=%t.ll --check-prefix=OGCG %s | ||
|
|
||
| struct A { | ||
| A() = default; | ||
| A(int); // This constructor triggers the null base class initialization. | ||
| }; | ||
|
|
||
| struct B : A { | ||
| }; | ||
|
|
||
| void test_empty_base_null_init() { | ||
| B{}; | ||
| } | ||
|
|
||
| // CIR: cir.func {{.*}} @_Z25test_empty_base_null_initv() | ||
| // CIR-NEXT: %[[B_ADDR:.*]] = cir.alloca !rec_B, !cir.ptr<!rec_B>, ["agg.tmp.ensured"] | ||
| // CIR-NEXT: %[[A_ADDR:.*]] = cir.base_class_addr %[[B_ADDR]] : !cir.ptr<!rec_B> nonnull [0] -> !cir.ptr<!rec_A> | ||
|
|
||
| // LLVM: define{{.*}} @_Z25test_empty_base_null_initv() | ||
| // LLVM-NEXT: %[[B:.*]] = alloca %struct.B | ||
| // LLVM-NEXT: ret void | ||
|
|
||
| // OGCG: define{{.*}} @_Z25test_empty_base_null_initv() | ||
| // OGCG-NEXT: entry: | ||
| // OGCG-NEXT: %[[B:.*]] = alloca %struct.B | ||
| // OGCG-NEXT: ret void |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only part of this function changed in this PR. The function is being moved to CIRGenExprCXX.cpp.