Skip to content

[clang][bytecode] Refine diagnostics for volatile reads #136857

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 1 commit into from
Apr 23, 2025
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
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {

switch (CE->getCastKind()) {
case CK_LValueToRValue: {
if (SubExpr->getType().isVolatileQualified())
return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, CE);

std::optional<PrimType> SubExprT = classify(SubExpr->getType());
// Prepare storage for the result.
if (!Initializing && !SubExprT) {
Expand Down
29 changes: 24 additions & 5 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,30 @@ static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
if (!PtrType.isVolatileQualified())
return true;

const SourceInfo &Loc = S.Current->getSource(OpPC);
if (S.getLangOpts().CPlusPlus)
S.FFDiag(Loc, diag::note_constexpr_access_volatile_type) << AK << PtrType;
else
S.FFDiag(Loc);
if (!S.getLangOpts().CPlusPlus)
return Invalid(S, OpPC);

const NamedDecl *ND = nullptr;
int DiagKind;
SourceLocation Loc;
if (const auto *F = Ptr.getField()) {
DiagKind = 2;
Loc = F->getLocation();
ND = F;
} else if (auto *VD = Ptr.getFieldDesc()->asValueDecl()) {
DiagKind = 1;
Loc = VD->getLocation();
ND = VD;
} else {
DiagKind = 0;
if (const auto *E = Ptr.getFieldDesc()->asExpr())
Loc = E->getExprLoc();
}

S.FFDiag(S.Current->getLocation(OpPC),
diag::note_constexpr_access_volatile_obj, 1)
<< AK << DiagKind << ND;
S.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind;
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2885,12 +2885,22 @@ inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
bool Fatal) {
const SourceLocation &Loc = S.Current->getLocation(OpPC);

// FIXME: Support diagnosing other invalid cast kinds.
if (Kind == CastKind::Reinterpret) {
S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
<< static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
return !Fatal;
} else if (Kind == CastKind::Volatile) {
// FIXME: Technically not a cast.
const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
if (S.getLangOpts().CPlusPlus)
S.FFDiag(E, diag::note_constexpr_access_volatile_type)
<< AK_Read << E->getSubExpr()->getType();
else
S.FFDiag(E);

return false;
}

return false;
}

Expand Down
7 changes: 4 additions & 3 deletions clang/lib/AST/ByteCode/PrimType.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ inline constexpr bool isPtrType(PrimType T) {

enum class CastKind : uint8_t {
Reinterpret,
Atomic,
Volatile,
};

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
interp::CastKind CK) {
switch (CK) {
case interp::CastKind::Reinterpret:
OS << "reinterpret_cast";
break;
case interp::CastKind::Atomic:
OS << "atomic";
case interp::CastKind::Volatile:
OS << "volatile";
break;
}
return OS;
Expand Down
16 changes: 16 additions & 0 deletions clang/test/AST/ByteCode/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,22 @@ namespace VolatileReads {
const volatile int b = 1;
static_assert(b, ""); // both-error {{not an integral constant expression}} \
// both-note {{read of volatile-qualified type 'const volatile int' is not allowed in a constant expression}}


constexpr int a = 12;
constexpr volatile int c = (volatile int&)a; // both-error {{must be initialized by a constant expression}} \
// both-note {{read of volatile-qualified type 'volatile int'}}

volatile constexpr int n1 = 0; // both-note {{here}}
volatile const int n2 = 0; // both-note {{here}}
constexpr int m1 = n1; // both-error {{constant expression}} \
// both-note {{read of volatile-qualified type 'const volatile int'}}
constexpr int m2 = n2; // both-error {{constant expression}} \
// both-note {{read of volatile-qualified type 'const volatile int'}}
constexpr int m1b = const_cast<const int&>(n1); // both-error {{constant expression}} \
// both-note {{read of volatile object 'n1'}}
constexpr int m2b = const_cast<const int&>(n2); // both-error {{constant expression}} \
// both-note {{read of volatile object 'n2'}}
}
#if __cplusplus >= 201703L
namespace {
Expand Down
Loading