Skip to content

Commit

Permalink
[clang][Interp] Diagnose volatile reads
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaederr committed Jul 15, 2024
1 parent 9d34b67 commit 9ac2b89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions clang/lib/AST/Interp/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,27 @@ bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
return false;
}

bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
assert(Ptr.isLive());

// FIXME: This check here might be kinda expensive. Maybe it would be better
// to have another field in InlineDescriptor for this?
if (!Ptr.isBlockPointer())
return true;

QualType PtrType = Ptr.getType();
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);
return false;
}

bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
assert(Ptr.isLive());
Expand Down Expand Up @@ -508,6 +529,8 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return false;
if (!CheckMutable(S, OpPC, Ptr))
return false;
if (!CheckVolatile(S, OpPC, Ptr, AK))
return false;
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,3 +1290,9 @@ namespace UnaryOpError {
}
}
#endif

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}}
}

0 comments on commit 9ac2b89

Please sign in to comment.