diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -420,6 +420,9 @@ (`#62711 `_). - Fix crash on attempt to initialize union with flexible array member. (`#61746 `_). +- Print diagnostic warning about precedence when integer expression is used + without parentheses in an conditional operator expression. + (`#61943 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -9314,6 +9314,8 @@ return OP->getOpcode() == UO_LNot; if (E->getType()->isPointerType()) return true; + if (E->getType()->isIntegerType()) + return true; // FIXME: What about overloaded operator calls returning "unspecified boolean // type"s (commonly pointer-to-members)? diff --git a/clang/test/Analysis/uninit-vals.c b/clang/test/Analysis/uninit-vals.c --- a/clang/test/Analysis/uninit-vals.c +++ b/clang/test/Analysis/uninit-vals.c @@ -32,7 +32,7 @@ int f2_b(void) { int x; // expected-note{{'x' declared without an initial value}} - return ((1+x)+2+((x))) + 1 ? 1 : 2; // expected-warning{{The right operand of '+' is a garbage value}} + return (((1+x)+2+((x))) + 1) ? 1 : 2; // expected-warning{{The right operand of '+' is a garbage value}} // expected-note@-1{{The right operand of '+' is a garbage value}} } diff --git a/clang/test/Sema/integer-overflow.c b/clang/test/Sema/integer-overflow.c --- a/clang/test/Sema/integer-overflow.c +++ b/clang/test/Sema/integer-overflow.c @@ -34,7 +34,7 @@ uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL); // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} - overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0; + overflow = (4608 * 1024 * 1024) ? 4608 * 1024 * 1024 : 0; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow = 0 ? 0 : 4608 * 1024 * 1024; diff --git a/clang/test/Sema/parentheses.c b/clang/test/Sema/parentheses.c --- a/clang/test/Sema/parentheses.c +++ b/clang/test/Sema/parentheses.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s @@ -84,10 +84,10 @@ // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")" - (void)(x % 2 ? 1 : 2); // no warning + (void)(x % 2 ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '%'}} expected-note 2{{place parenthese}} (void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} - (void)(p + x ? 1 : 2); // no warning + (void)(p + x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} diff --git a/clang/test/Sema/parentheses.cpp b/clang/test/Sema/parentheses.cpp --- a/clang/test/Sema/parentheses.cpp +++ b/clang/test/Sema/parentheses.cpp @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s -// RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s +// RUN: %clang_cc1 -Wparentheses -Woverloaded-shift-op-parentheses -fsyntax-only -verify %s +// RUN: %clang_cc1 -Wparentheses -Woverloaded-shift-op-parentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s bool someConditionFunc(); @@ -38,7 +38,7 @@ Stream &operator>>(const char*); }; -void f(Stream& s, bool b) { +void f(Stream& s, bool b, int x) { (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \ // expected-note {{place parentheses around the '<<' expression to silence this warning}} \ // expected-note {{place parentheses around the '?:' expression to evaluate it first}} @@ -62,6 +62,30 @@ // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")" // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"(" // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")" + + void(s << "Test" << x ? "foo" : "bar"); //expected-warning {{operator '?:' has lower precedence than '<<'}} \ + // expected-note {{place parentheses around the '<<' expression to silence this warning}} \ + // expected-note {{place parentheses around the '?:' expression to evaluate it first}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:8-[[@LINE-3]]:8}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:24-[[@LINE-4]]:24}:")" + // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:23-[[@LINE-5]]:23}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:40-[[@LINE-6]]:40}:")" + + void(s << x == 1 ? "foo": "bar"); //expected-warning {{overloaded operator << has higher precedence than comparison operator}} \ + //expected-note {{place parentheses around the '<<' expression to silence this warning}} \ + //expected-note {{place parentheses around comparison expression to evaluate it first}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:8-[[@LINE-3]]:8}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:14-[[@LINE-4]]:14}:")" + // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:19-[[@LINE-6]]:19}:")" + + void(s << static_cast(x) ? "foo" : "bar"); //expected-warning {{operator '?:' has lower precedence than '<<'}} \ + //expected-note {{place parentheses around the '<<' expression to silence this warning}} \ + //expected-note {{place parentheses around the '?:' expression to evaluate it first}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:8-[[@LINE-3]]:8}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")" + // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:49-[[@LINE-6]]:49}:")" } struct S { diff --git a/clang/test/SemaCXX/array-bounds.cpp b/clang/test/SemaCXX/array-bounds.cpp --- a/clang/test/SemaCXX/array-bounds.cpp +++ b/clang/test/SemaCXX/array-bounds.cpp @@ -87,7 +87,7 @@ } #define SIZE 10 -#define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1 +#define ARR_IN_MACRO(flag, arr, idx) (flag ? arr[idx] : 1) int test_no_warn_macro_unreachable() { int arr[SIZE]; // expected-note {{array 'arr' declared here}} diff --git a/clang/test/SemaCXX/integer-overflow.cpp b/clang/test/SemaCXX/integer-overflow.cpp --- a/clang/test/SemaCXX/integer-overflow.cpp +++ b/clang/test/SemaCXX/integer-overflow.cpp @@ -41,7 +41,7 @@ uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL); // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} - overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0; + overflow = (4608 * 1024 * 1024) ? 4608 * 1024 * 1024 : 0; // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} overflow = 0 ? 0 : 4608 * 1024 * 1024; diff --git a/libcxx/include/__chrono/duration.h b/libcxx/include/__chrono/duration.h --- a/libcxx/include/__chrono/duration.h +++ b/libcxx/include/__chrono/duration.h @@ -194,7 +194,7 @@ return __lower; if (__lower_diff > __upper_diff) return __upper; - return __lower.count() & 1 ? __upper : __lower; + return (__lower.count() & 1) ? __upper : __lower; } #endif diff --git a/libcxx/include/strstream b/libcxx/include/strstream --- a/libcxx/include/strstream +++ b/libcxx/include/strstream @@ -300,7 +300,7 @@ _LIBCPP_INLINE_VISIBILITY ostrstream(char* __s, int __n, ios_base::openmode __mode = ios_base::out) : ostream(&__sb_), - __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0)) + __sb_(__s, __n, __s + ((__mode & ios::app) ? _VSTD::strlen(__s) : 0)) {} #ifndef _LIBCPP_CXX03_LANG @@ -360,7 +360,7 @@ _LIBCPP_INLINE_VISIBILITY strstream(char* __s, int __n, ios_base::openmode __mode = ios_base::in | ios_base::out) : iostream(&__sb_), - __sb_(__s, __n, __s + (__mode & ios::app ? _VSTD::strlen(__s) : 0)) + __sb_(__s, __n, __s + ((__mode & ios::app) ? _VSTD::strlen(__s) : 0)) {} #ifndef _LIBCPP_CXX03_LANG diff --git a/libcxx/test/libcxx/algorithms/nth_element_stability.pass.cpp b/libcxx/test/libcxx/algorithms/nth_element_stability.pass.cpp --- a/libcxx/test/libcxx/algorithms/nth_element_stability.pass.cpp +++ b/libcxx/test/libcxx/algorithms/nth_element_stability.pass.cpp @@ -32,7 +32,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? i : kSize / 2 + i); + v[i].value = ((i % 2) ? i : kSize / 2 + i); } std::__nth_element(v.begin(), v.begin() + kSize / 2, v.end(), std::less()); return v; @@ -43,7 +43,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? i : kSize / 2 + i); + v[i].value = ((i % 2) ? i : kSize / 2 + i); } auto deterministic_v = deterministic(); std::nth_element(v.begin(), v.begin() + kSize / 2, v.end()); @@ -61,7 +61,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? i : kSize / 2 + i); + v[i].value = ((i % 2) ? i : kSize / 2 + i); } auto snapshot_v = v; auto snapshot_custom_v = v; diff --git a/libcxx/test/libcxx/algorithms/partial_sort_stability.pass.cpp b/libcxx/test/libcxx/algorithms/partial_sort_stability.pass.cpp --- a/libcxx/test/libcxx/algorithms/partial_sort_stability.pass.cpp +++ b/libcxx/test/libcxx/algorithms/partial_sort_stability.pass.cpp @@ -32,7 +32,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? 1 : kSize / 2 + i); + v[i].value = ((i % 2) ? 1 : kSize / 2 + i); } auto comp = std::less(); std::__partial_sort_impl(v.begin(), v.begin() + kSize / 2, v.end(), comp); @@ -44,7 +44,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? 1 : kSize / 2 + i); + v[i].value = ((i % 2) ? 1 : kSize / 2 + i); } auto deterministic_v = deterministic(); std::partial_sort(v.begin(), v.begin() + kSize / 2, v.end()); @@ -62,7 +62,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = (i % 2 ? 1 : kSize / 2 + i); + v[i].value = ((i % 2) ? 1 : kSize / 2 + i); } auto snapshot_v = v; auto snapshot_custom_v = v; diff --git a/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp b/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp --- a/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp +++ b/libcxx/test/libcxx/algorithms/sort_stability.pass.cpp @@ -32,7 +32,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = kSize / 2 - i * (i % 2 ? -1 : 1); + v[i].value = kSize / 2 - i * ((i % 2) ? -1 : 1); } std::less comp; std::__sort_dispatch(v.begin(), v.end(), comp); @@ -44,7 +44,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = kSize / 2 - i * (i % 2 ? -1 : 1); + v[i].value = kSize / 2 - i * ((i % 2) ? -1 : 1); } auto deterministic_v = deterministic(); std::sort(v.begin(), v.end()); @@ -62,7 +62,7 @@ std::vector v; v.resize(kSize); for (int i = 0; i < kSize; ++i) { - v[i].value = kSize / 2 - i * (i % 2 ? -1 : 1); + v[i].value = kSize / 2 - i * ((i % 2) ? -1 : 1); } auto snapshot_v = v; auto snapshot_custom_v = v; diff --git a/libcxxabi/src/cxa_personality.cpp b/libcxxabi/src/cxa_personality.cpp --- a/libcxxabi/src/cxa_personality.cpp +++ b/libcxxabi/src/cxa_personality.cpp @@ -718,9 +718,7 @@ if (actionEntry == 0) { // Found a cleanup - results.reason = actions & _UA_SEARCH_PHASE - ? _URC_CONTINUE_UNWIND - : _URC_HANDLER_FOUND; + results.reason = (actions & _UA_SEARCH_PHASE) ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND; return; } // Convert 1-based byte offset into @@ -832,9 +830,8 @@ // End of action list. If this is phase 2 and we have found // a cleanup (ttypeIndex=0), return _URC_HANDLER_FOUND; // otherwise return _URC_CONTINUE_UNWIND. - results.reason = hasCleanup && actions & _UA_CLEANUP_PHASE - ? _URC_HANDLER_FOUND - : _URC_CONTINUE_UNWIND; + results.reason = + (hasCleanup && (actions & _UA_CLEANUP_PHASE)) ? _URC_HANDLER_FOUND : _URC_CONTINUE_UNWIND; return; } // Go to next action @@ -1243,10 +1240,9 @@ { const __shim_type_info* excpType = static_cast(new_exception_header->exceptionType); - adjustedPtr = - __getExceptionClass(&new_exception_header->unwindHeader) == kOurDependentExceptionClass ? - ((__cxa_dependent_exception*)new_exception_header)->primaryException : - new_exception_header + 1; + adjustedPtr = (__getExceptionClass(&new_exception_header->unwindHeader) == kOurDependentExceptionClass) + ? ((__cxa_dependent_exception*)new_exception_header)->primaryException + : new_exception_header + 1; if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding, excpType, adjustedPtr, unwind_exception, base))