Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -6125,6 +6125,9 @@ def err_atomic_op_needs_non_const_atomic : Error< "address argument to atomic operation must be a pointer to non-const _Atomic " "type (%0 invalid)">; +def err_atomic_op_needs_non_const_pointer : Error< + "address argument to atomic operation must be a pointer to non-const " + "type %0 (%1 invalid)">; def err_atomic_op_needs_trivial_copy : Error< "address argument to atomic operation must be a pointer to a " "trivially-copyable type (%0 invalid)">; Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp +++ lib/Sema/SemaChecking.cpp @@ -1405,6 +1405,16 @@ } } +// If the ArgPtr is a pointer type return the qualifiers of the pointee type. +// Otherwise return an empty set of qualifiers. +static Qualifiers getPointeeQualifiers(Sema& S, Expr *ArgPtr) { + ArgPtr = S.DefaultFunctionArrayLvalueConversion(ArgPtr).get(); + QualType Ty = ArgPtr->getType(); + const PointerType *PointerTy = Ty->getAs(); + return PointerTy ? PointerTy->getPointeeType().getQualifiers() + : Qualifiers(); +} + ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, AtomicExpr::AtomicOp Op) { CallExpr *TheCall = cast(TheCallResult.get()); @@ -1449,6 +1459,15 @@ Op == AtomicExpr::AO__atomic_exchange_n || Op == AtomicExpr::AO__atomic_compare_exchange_n; bool IsAddSub = false; + // Allow extra const and volatile qualifiers on the pointee type of the + // second operand of these functions. + bool AllowConstCP = Op == AtomicExpr::AO__atomic_store || + Op == AtomicExpr::AO__atomic_exchange; + bool AllowVolatileCP = Op == AtomicExpr::AO__atomic_store || + Op == AtomicExpr::AO__atomic_load || + Op == AtomicExpr::AO__atomic_exchange || + Op == AtomicExpr::AO__atomic_compare_exchange || + Op == AtomicExpr::AO__atomic_compare_exchange_n; switch (Op) { case AtomicExpr::AO__c11_atomic_init: @@ -1548,6 +1567,13 @@ return ExprError(); } ValType = AtomTy->getAs()->getValueType(); + } else if (Form != Load && Op != AtomicExpr::AO__atomic_load) { + if (ValType.isConstQualified()) { + Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_pointer) + << ValType.getUnqualifiedType() << Ptr->getType() + << Ptr->getSourceRange(); + return ExprError(); + } } // For an arithmetic operation, the implied arithmetic must be well-formed. @@ -1585,9 +1611,6 @@ return ExprError(); } - // FIXME: For any builtin other than a load, the ValType must not be - // const-qualified. - switch (ValType.getObjCLifetime()) { case Qualifiers::OCL_None: case Qualifiers::OCL_ExplicitNone: @@ -1635,17 +1658,40 @@ assert(Form != Load); if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) Ty = ValType; - else if (Form == Copy || Form == Xchg) - Ty = ByValType; else if (Form == Arithmetic) Ty = Context.getPointerDiffType(); - else - Ty = Context.getPointerType(ValType.getUnqualifiedType()); + // Handle the Copy and Xchg atomics that don't take a pointer as the + // second argument. + else if ((Form == Copy && (IsN || IsC11)) || Form == Xchg) + Ty = ValType; + else { + // If the second type is a pointer propagate the cv-qualifiers from + // the pointee type to the deduced type wherever we are allowed to. + Qualifiers SecondParamQuals = getPointeeQualifiers(*this, TheCall->getArg(1)); + Ty = ValType.getUnqualifiedType(); + if (SecondParamQuals.hasVolatile() && AllowVolatileCP) + Ty.addVolatile(); + if (SecondParamQuals.hasConst() && AllowConstCP) + Ty.addConst(); + Ty = Context.getPointerType(Ty); + } break; case 2: - // The third argument to compare_exchange / GNU exchange is a - // (pointer to a) desired value. - Ty = ByValType; + // Handle the Xchg and CmpXchg builtins that take a pointer + // as their third argument. + if (Op == AtomicExpr::AO__atomic_exchange || + Op == AtomicExpr::AO__atomic_compare_exchange) { + Qualifiers ThirdParamQuals = getPointeeQualifiers(*this, TheCall->getArg(2)); + Ty = ValType.getUnqualifiedType(); + if (ThirdParamQuals.hasVolatile()) + Ty.addVolatile(); + if (Op == AtomicExpr::AO__atomic_compare_exchange + && ThirdParamQuals.hasConst()) + Ty.addConst(); + Ty = Context.getPointerType(Ty); + } + else + Ty = ByValType; break; case 3: // The fourth argument to GNU compare_exchange is a 'weak' flag. Index: test/Sema/atomic-ops.c =================================================================== --- test/Sema/atomic-ops.c +++ test/Sema/atomic-ops.c @@ -85,41 +85,72 @@ _Static_assert(!__atomic_always_lock_free(8, &i32), ""); _Static_assert(__atomic_always_lock_free(8, &i64), ""); -void f(_Atomic(int) *i, _Atomic(int*) *p, _Atomic(float) *d, - int *I, int **P, float *D, struct S *s1, struct S *s2) { +void f(_Atomic(int) *i, const _Atomic(int) *ci, volatile _Atomic(int) *vi, + const volatile _Atomic(int) *cvi, _Atomic(int*) *p, _Atomic(float) *d, + int *I, const int *CI, volatile int *VI, const volatile int* CVI, + int **P, float *D, struct S *s1, struct S *s2) { __c11_atomic_init(I, 5); // expected-error {{pointer to _Atomic}} + __c11_atomic_init(ci, 5); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const _Atomic(int) *' invalid)}} + __c11_atomic_init(vi, 5); + __c11_atomic_init(cvi, 5); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const volatile _Atomic(int) *' invalid)}} __c11_atomic_load(0); // expected-error {{too few arguments to function}} __c11_atomic_load(0,0,0); // expected-error {{too many arguments to function}} __c11_atomic_store(0,0,0); // expected-error {{address argument to atomic builtin must be a pointer}} __c11_atomic_store((int*)0,0,0); // expected-error {{address argument to atomic operation must be a pointer to _Atomic}} + __c11_atomic_store(i, 0, memory_order_relaxed); + __c11_atomic_store(vi, 0, memory_order_relaxed); + __c11_atomic_store(ci, 0, memory_order_relaxed); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const _Atomic(int) *' invalid)}} + __c11_atomic_store(cvi, 0, memory_order_relaxed); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const volatile _Atomic(int) *' invalid)}} __c11_atomic_load(i, memory_order_seq_cst); __c11_atomic_load(p, memory_order_seq_cst); __c11_atomic_load(d, memory_order_seq_cst); + __c11_atomic_load(vi, memory_order_seq_cst); + __c11_atomic_load(ci, memory_order_seq_cst); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const _Atomic(int) *' invalid)}} + __c11_atomic_load(cvi, memory_order_seq_cst); // expected-error {{address argument to atomic operation must be a pointer to non-const _Atomic type ('const volatile _Atomic(int) *' invalid)}} int load_n_1 = __atomic_load_n(I, memory_order_relaxed); int *load_n_2 = __atomic_load_n(P, memory_order_relaxed); float load_n_3 = __atomic_load_n(D, memory_order_relaxed); // expected-error {{must be a pointer to integer or pointer}} __atomic_load_n(s1, memory_order_relaxed); // expected-error {{must be a pointer to integer or pointer}} + load_n_1 = __atomic_load_n(VI, memory_order_relaxed); + load_n_1 = __atomic_load_n(CI, memory_order_relaxed); + load_n_1 = __atomic_load_n(CVI, memory_order_relaxed); __atomic_load(i, I, memory_order_relaxed); // expected-error {{must be a pointer to a trivially-copyable type}} + __atomic_load(CI, I, memory_order_relaxed); + __atomic_load(VI, I, memory_order_relaxed); + __atomic_load(CVI, I, memory_order_relaxed); + __atomic_load(VI, VI, memory_order_relaxed); + __atomic_load(CVI, VI, memory_order_relaxed); + __atomic_load(I, i, memory_order_relaxed); // expected-warning {{passing '_Atomic(int) *' to parameter of type 'int *'}} __atomic_load(I, *P, memory_order_relaxed); __atomic_load(I, *P, memory_order_relaxed, 42); // expected-error {{too many arguments}} (int)__atomic_load(I, I, memory_order_seq_cst); // expected-error {{operand of type 'void'}} __atomic_load(s1, s2, memory_order_acquire); - + (void)__atomic_load(CI, CI, memory_order_relaxed); // expected-warning {{passing 'const int *' to parameter of type 'int *' discards qualifiers}} + (void)__atomic_load(CVI, VI, memory_order_relaxed); __c11_atomic_store(i, 1, memory_order_seq_cst); __c11_atomic_store(p, 1, memory_order_seq_cst); // expected-warning {{incompatible integer to pointer conversion}} (int)__c11_atomic_store(d, 1, memory_order_seq_cst); // expected-error {{operand of type 'void'}} __atomic_store_n(I, 4, memory_order_release); __atomic_store_n(I, 4.0, memory_order_release); + __atomic_store_n(VI, 4, memory_order_release); + __atomic_store_n(CI, 4, memory_order_release); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const int *' invalid)}} + __atomic_store_n(CVI, 4, memory_order_release); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const volatile int *' invalid)}} __atomic_store_n(I, P, memory_order_release); // expected-warning {{parameter of type 'int'}} __atomic_store_n(i, 1, memory_order_release); // expected-error {{must be a pointer to integer or pointer}} __atomic_store_n(s1, *s2, memory_order_release); // expected-error {{must be a pointer to integer or pointer}} + __atomic_store_n(I, I, memory_order_release); // expected-warning {{incompatible pointer to integer conversion passing 'int *' to parameter of type 'int'; dereference with *}} __atomic_store(I, *P, memory_order_release); + __atomic_store(VI, CVI, memory_order_release); + __atomic_store(VI, I, memory_order_release); + __atomic_store(VI, VI, memory_order_release); + __atomic_store(CI, I, memory_order_release); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const int *' invalid)}} + __atomic_store(CVI, I, memory_order_release); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const volatile int *' invalid)}} __atomic_store(s1, s2, memory_order_release); __atomic_store(i, I, memory_order_release); // expected-error {{trivially-copyable}} @@ -131,6 +162,11 @@ __atomic_exchange(s1, s2, s2, memory_order_seq_cst); __atomic_exchange(s1, I, P, memory_order_seq_cst); // expected-warning 2{{parameter of type 'struct S *'}} (int)__atomic_exchange(s1, s2, s2, memory_order_seq_cst); // expected-error {{operand of type 'void'}} + __atomic_exchange(I, I, I, memory_order_seq_cst); + __atomic_exchange(VI, I, I, memory_order_seq_cst); + __atomic_exchange(VI, CI, VI, memory_order_seq_cst); + __atomic_exchange(CI, I, I, memory_order_seq_cst); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const int *' invalid)}} + __atomic_exchange(I, I, CI, memory_order_seq_cst); // expected-warning {{passing 'const int *' to parameter of type 'int *' discards qualifiers}} __c11_atomic_fetch_add(i, 1, memory_order_seq_cst); __c11_atomic_fetch_add(p, 1, memory_order_seq_cst); @@ -159,10 +195,20 @@ _Bool cmpexch_4 = __atomic_compare_exchange_n(I, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); _Bool cmpexch_5 = __atomic_compare_exchange_n(I, P, 5, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{; dereference with *}} _Bool cmpexch_6 = __atomic_compare_exchange_n(I, I, P, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'int **' to parameter of type 'int'}} + (void)__atomic_compare_exchange_n(VI, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange_n(I, VI, 5, 1, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange_n(VI, VI, 5, 1, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange_n(CI, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const int *' invalid)}} + (void)__atomic_compare_exchange_n(I, CI, 5, 1, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'const int *' to parameter of type 'int *' discards qualifiers}} _Bool cmpexch_7 = __atomic_compare_exchange(I, I, 5, 1, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'int' to parameter of type 'int *'}} _Bool cmpexch_8 = __atomic_compare_exchange(I, P, I, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{; dereference with *}} _Bool cmpexch_9 = __atomic_compare_exchange(I, I, I, 0, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange(VI, I, I, 0, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange(I, VI, CVI, 0, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange(VI, VI, CVI, 0, memory_order_seq_cst, memory_order_seq_cst); + (void)__atomic_compare_exchange(CI, I, I, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-error {{address argument to atomic operation must be a pointer to non-const type 'int' ('const int *' invalid)}} + (void)__atomic_compare_exchange(I, CI, I, 0, memory_order_seq_cst, memory_order_seq_cst); // expected-warning {{passing 'const int *' to parameter of type 'int *' discards qualifiers}} const volatile int flag_k = 0; volatile int flag = 0;