Skip to content

Commit 9ac2b89

Browse files
committed
[clang][Interp] Diagnose volatile reads
1 parent 9d34b67 commit 9ac2b89

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

clang/lib/AST/Interp/Interp.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,27 @@ bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
444444
return false;
445445
}
446446

447+
bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
448+
AccessKinds AK) {
449+
assert(Ptr.isLive());
450+
451+
// FIXME: This check here might be kinda expensive. Maybe it would be better
452+
// to have another field in InlineDescriptor for this?
453+
if (!Ptr.isBlockPointer())
454+
return true;
455+
456+
QualType PtrType = Ptr.getType();
457+
if (!PtrType.isVolatileQualified())
458+
return true;
459+
460+
const SourceInfo &Loc = S.Current->getSource(OpPC);
461+
if (S.getLangOpts().CPlusPlus)
462+
S.FFDiag(Loc, diag::note_constexpr_access_volatile_type) << AK << PtrType;
463+
else
464+
S.FFDiag(Loc);
465+
return false;
466+
}
467+
447468
bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
448469
AccessKinds AK) {
449470
assert(Ptr.isLive());
@@ -508,6 +529,8 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
508529
return false;
509530
if (!CheckMutable(S, OpPC, Ptr))
510531
return false;
532+
if (!CheckVolatile(S, OpPC, Ptr, AK))
533+
return false;
511534
return true;
512535
}
513536

clang/test/AST/Interp/literals.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,3 +1290,9 @@ namespace UnaryOpError {
12901290
}
12911291
}
12921292
#endif
1293+
1294+
namespace VolatileReads {
1295+
const volatile int b = 1;
1296+
static_assert(b, ""); // both-error {{not an integral constant expression}} \
1297+
// both-note {{read of volatile-qualified type 'const volatile int' is not allowed in a constant expression}}
1298+
}

0 commit comments

Comments
 (0)