Skip to content

[Clang][Interp] Fix the location of uninitialized base warning #100761

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 8 commits into from
Jul 31, 2024
Merged
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
19 changes: 10 additions & 9 deletions clang/lib/AST/Interp/EvaluationResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "InterpState.h"
#include "Record.h"
#include "clang/AST/ExprCXX.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include <iterator>

namespace clang {
namespace interp {
Expand Down Expand Up @@ -122,19 +124,18 @@ static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
}

// Check Fields in all bases
for (const Record::Base &B : R->bases()) {
for (auto [I, B] : llvm::enumerate(R->bases())) {
Pointer P = BasePtr.atField(B.Offset);
if (!P.isInitialized()) {
const Descriptor *Desc = BasePtr.getDeclDesc();
if (Desc->asDecl())
S.FFDiag(BasePtr.getDeclDesc()->asDecl()->getLocation(),
diag::note_constexpr_uninitialized_base)
if (const auto *CD = dyn_cast_if_present<CXXRecordDecl>(R->getDecl())) {
const auto &BS = *std::next(CD->bases_begin(), I);
S.FFDiag(BS.getBaseTypeLoc(), diag::note_constexpr_uninitialized_base)
<< B.Desc->getType() << BS.getSourceRange();
} else {
S.FFDiag(Desc->getLocation(), diag::note_constexpr_uninitialized_base)
<< B.Desc->getType();
else
S.FFDiag(BasePtr.getDeclDesc()->asExpr()->getExprLoc(),
diag::note_constexpr_uninitialized_base)
<< B.Desc->getType();

}
return false;
}
Result &= CheckFieldsInitialized(S, Loc, P, B.R);
Expand Down
20 changes: 7 additions & 13 deletions clang/test/AST/Interp/constexpr-subobj-initialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,36 @@
/// Differences:
/// 1) The type of the uninitialized base class is printed WITH the namespace,
/// i.e. 'baseclass_uninit::DelBase' instead of just 'DelBase'.
/// 2) The location is not the base specifier declaration, but the call site
/// of the constructor.


namespace baseclass_uninit {
struct DelBase {
constexpr DelBase() = delete; // expected-note {{'DelBase' has been explicitly marked deleted here}}
};

struct Foo : DelBase {
struct Foo : DelBase { // expected-note 2{{constructor of base class 'baseclass_uninit::DelBase' is not called}}
constexpr Foo() {}; // expected-error {{call to deleted constructor of 'DelBase'}}
};
constexpr Foo f; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{constructor of base class 'baseclass_uninit::DelBase' is not called}}
constexpr Foo f; // expected-error {{must be initialized by a constant expression}}

struct Bar : Foo {
constexpr Bar() {};
};
constexpr Bar bar; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{constructor of base class 'baseclass_uninit::DelBase' is not called}}
constexpr Bar bar; // expected-error {{must be initialized by a constant expression}}

struct Base {};
struct A : Base {
struct A : Base { // expected-note {{constructor of base class 'baseclass_uninit::Base' is not called}}
constexpr A() : value() {} // expected-error {{member initializer 'value' does not name a non-static data member or base class}}
};

constexpr A a; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{constructor of base class 'baseclass_uninit::Base' is not called}}
constexpr A a; // expected-error {{must be initialized by a constant expression}}


struct B : Base {
struct B : Base { // expected-note {{constructor of base class 'baseclass_uninit::Base' is not called}}
constexpr B() : {} // expected-error {{expected class member or base class name}}
};

constexpr B b; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{constructor of base class 'baseclass_uninit::Base' is not called}}
constexpr B b; // expected-error {{must be initialized by a constant expression}}
} // namespace baseclass_uninit


Expand Down
1 change: 1 addition & 0 deletions clang/test/C/C23/n3018.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -std=c23 -verify -triple x86_64 -pedantic -Wno-conversion -Wno-constant-conversion %s
// RUN: %clang_cc1 -std=c23 -verify -triple x86_64 -pedantic -Wno-conversion -Wno-constant-conversion -fexperimental-new-constant-interpreter %s

/* WG14 N3018: Full
* The constexpr specifier for object definitions
Expand Down
1 change: 1 addition & 0 deletions clang/test/CodeGen/pr3518.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 %s -fexperimental-new-constant-interpreter -emit-llvm -o - | FileCheck %s
// PR 3518
// Some of the objects were coming out as uninitialized (external) before 3518
// was fixed. Internal names are different between llvm-gcc and clang so they
Expand Down
2 changes: 2 additions & 0 deletions clang/test/Preprocessor/embed_weird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// RUN: printf "\0" > %t/null_byte.bin
// RUN: %clang_cc1 %s -fsyntax-only --embed-dir=%t -verify=expected,cxx -Wno-c23-extensions
// RUN: %clang_cc1 -x c -std=c23 %s -fsyntax-only --embed-dir=%t -verify=expected,c
// RUN: %clang_cc1 %s -fsyntax-only -fexperimental-new-constant-interpreter --embed-dir=%t -verify=expected,cxx -Wno-c23-extensions
// RUN: %clang_cc1 -x c -std=c23 %s -fsyntax-only -fexperimental-new-constant-interpreter --embed-dir=%t -verify=expected,c
#embed <media/empty>
;

Expand Down
7 changes: 4 additions & 3 deletions clang/test/SemaCXX/constexpr-subobj-initialization.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s

namespace baseclass_uninit {
struct DelBase {
constexpr DelBase() = delete; // expected-note {{'DelBase' has been explicitly marked deleted here}}
};

struct Foo : DelBase { // expected-note 2{{constructor of base class 'DelBase' is not called}}
struct Foo : DelBase { // expected-note-re 2{{constructor of base class '{{.*}}DelBase' is not called}}
constexpr Foo() {}; // expected-error {{call to deleted constructor of 'DelBase'}}
};
constexpr Foo f; // expected-error {{must be initialized by a constant expression}}
Expand All @@ -15,13 +16,13 @@ struct Bar : Foo {
constexpr Bar bar; // expected-error {{must be initialized by a constant expression}}

struct Base {};
struct A : Base { // expected-note {{constructor of base class 'Base' is not called}}
struct A : Base { // expected-note-re {{constructor of base class '{{.*}}Base' is not called}}
constexpr A() : value() {} // expected-error {{member initializer 'value' does not name a non-static data member or base class}}
};

constexpr A a; // expected-error {{must be initialized by a constant expression}}

struct B : Base { // expected-note {{constructor of base class 'Base' is not called}}
struct B : Base { // expected-note-re {{constructor of base class '{{.*}}Base' is not called}}
constexpr B() : {} // expected-error {{expected class member or base class name}}
};

Expand Down
Loading