[PATCH] D45944: Disallow pointers to const in __sync_fetch_and_xxx
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 23 04:19:59 PDT 2018
aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, dblaikie.
The following code is currently accepted without a diagnostic when it should be prohibited:
void f(const int *ptr) {
__sync_fetch_and_add(ptr, 1);
}
NB: the above code is diagnosed by GCC and ICC. However, Clang attempts to modify the underlying object, which seems dangerous.
https://reviews.llvm.org/D45944
Files:
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/builtins.c
Index: test/Sema/builtins.c
===================================================================
--- test/Sema/builtins.c
+++ test/Sema/builtins.c
@@ -248,3 +248,7 @@
return buf;
}
+
+void test21(const int *ptr) {
+ __sync_fetch_and_add(ptr, 1); // expected-error{{address argument to atomic builtin cannot be const-qualified ('const int *' invalid)}}
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -3428,6 +3428,12 @@
return ExprError();
}
+ if (ValType.isConstQualified()) {
+ Diag(DRE->getLocStart(), diag::err_atomic_builtin_cannot_be_const)
+ << FirstArg->getType() << FirstArg->getSourceRange();
+ return ExprError();
+ }
+
switch (ValType.getObjCLifetime()) {
case Qualifiers::OCL_None:
case Qualifiers::OCL_ExplicitNone:
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -7079,6 +7079,8 @@
def err_atomic_builtin_must_be_pointer_intptr : Error<
"address argument to atomic builtin must be a pointer to integer or pointer"
" (%0 invalid)">;
+def err_atomic_builtin_cannot_be_const : Error<
+ "address argument to atomic builtin cannot be const-qualified (%0 invalid)">;
def err_atomic_builtin_must_be_pointer_intfltptr : Error<
"address argument to atomic builtin must be a pointer to integer,"
" floating-point or pointer (%0 invalid)">;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45944.143523.patch
Type: text/x-patch
Size: 1615 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180423/60411cb8/attachment.bin>
More information about the cfe-commits
mailing list