Skip to content

Commit

Permalink
[APSInt][OpenMP] Fix isNegative, etc. for unsigned types
Browse files Browse the repository at this point in the history
Without this patch, APSInt inherits APInt::isNegative, which merely
checks the sign bit without regard to whether the type is actually
signed.  isNonNegative and isStrictlyPositive call isNegative and so
are also affected.

This patch adjusts APSInt to override isNegative, isNonNegative, and
isStrictlyPositive with implementations that consider whether the type
is signed.

A large set of Clang OpenMP tests are affected.  Without this patch,
these tests assume that `true` is not a valid argument for clauses
like `collapse`.  Indeed, `true` fails APInt::isStrictlyPositive but
not APSInt::isStrictlyPositive.  This patch adjusts those tests to
assume `true` should be accepted.

This patch also adds tests revealing various other similar fixes due
to APSInt::isNegative calls in Clang's ExprConstant.cpp and
SemaExpr.cpp: `++` and `--` overflow in `constexpr`, evaluated object
size based on `alloc_size`, `<<` and `>>` shift count validation, and
OpenMP array section validation.

Reviewed By: lebedev.ri, ABataev, hfinkel

Differential Revision: https://reviews.llvm.org/D59712

llvm-svn: 359012
  • Loading branch information
jdenny-ornl committed Apr 23, 2019
1 parent bdb864a commit 3234887
Show file tree
Hide file tree
Showing 64 changed files with 359 additions and 124 deletions.
9 changes: 9 additions & 0 deletions clang/test/CodeGen/alloc-size.c
Expand Up @@ -357,3 +357,12 @@ void test12() {
// CHECK: store i32 -1
gi = OBJECT_SIZE_BUILTIN(my_signed_calloc(-2, 1), 0);
}

void *alloc_uchar(unsigned char) __attribute__((alloc_size(1)));

// CHECK-LABEL: @test13
void test13() {
// If 128 were incorrectly seen as negative, the result would become -1.
// CHECK: store i32 128,
gi = OBJECT_SIZE_BUILTIN(alloc_uchar(128), 0);
}
4 changes: 2 additions & 2 deletions clang/test/OpenMP/distribute_collapse_messages.cpp
Expand Up @@ -42,7 +42,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
// expected-note@+5 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+3 2 {{directive '#pragma omp distribute' cannot contain more than one 'collapse' clause}}
// expected-error@+2 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+1 2 {{expression is not an integral constant expression}}
#pragma omp distribute collapse (foobool(argc)), collapse (true), collapse (-5)
for (int i = ST; i < N; i++) argv[0][i] = argv[0][i] - argv[0][i-ST];
Expand Down Expand Up @@ -85,7 +85,7 @@ int main(int argc, char **argv) {
#endif
// expected-error@+3 {{expression is not an integral constant expression}}
// expected-error@+2 2 {{directive '#pragma omp distribute' cannot contain more than one 'collapse' clause}}
// expected-error@+1 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+1 {{argument to 'collapse' clause must be a strictly positive integer value}}
#pragma omp distribute collapse (foobool(argc)), collapse (true), collapse (-5)
for (int i = 4; i < 12; i++) argv[0][i] = argv[0][i] - argv[0][i-4];
#pragma omp distribute collapse (S1) // expected-error {{'S1' does not refer to a value}}
Expand Down
Expand Up @@ -53,7 +53,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
#pragma omp distribute parallel for collapse ((ST > 0) ? 1 + ST : 2) // expected-note 2 {{as specified in 'collapse' clause}}
for (int i = ST; i < N; i++) argv[0][i] = argv[0][i] - argv[0][i-ST]; // expected-error 2 {{expected 2 for loops after '#pragma omp distribute parallel for', but found only 1}}
// expected-error@+8 2 {{directive '#pragma omp distribute parallel for' cannot contain more than one 'collapse' clause}}
// expected-error@+7 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+7 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+6 2 {{expression is not an integral constant expression}}
#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
Expand Down Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
// expected-note@+6{{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+4 2 {{directive '#pragma omp distribute parallel for' cannot contain more than one 'collapse' clause}}
// expected-error@+3 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+3 {{argument to 'collapse' clause must be a strictly positive integer value}}
#pragma omp target
#pragma omp teams
#pragma omp distribute parallel for collapse (foobool(argc)), collapse (true), collapse (-5)
Expand Down
Expand Up @@ -53,7 +53,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
#pragma omp distribute parallel for simd collapse ((ST > 0) ? 1 + ST : 2) // expected-note 2 {{as specified in 'collapse' clause}}
for (int i = ST; i < N; i++) argv[0][i] = argv[0][i] - argv[0][i-ST]; // expected-error 2 {{expected 2 for loops after '#pragma omp distribute parallel for simd', but found only 1}}
// expected-error@+8 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'collapse' clause}}
// expected-error@+7 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+7 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+6 2 {{expression is not an integral constant expression}}
#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
Expand Down Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
// expected-note@+6{{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+4 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'collapse' clause}}
// expected-error@+3 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+3 {{argument to 'collapse' clause must be a strictly positive integer value}}
#pragma omp target
#pragma omp teams
#pragma omp distribute parallel for simd collapse (foobool(argc)), collapse (true), collapse (-5)
Expand Down
Expand Up @@ -63,11 +63,14 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
argv[0][i] = argv[0][i] - argv[0][i-ST];

#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp distribute parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'safelen' clause}} expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{expression is not an integral constant expression}}
// expected-error@+3 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'safelen' clause}}
// expected-error@+2 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+1 2 {{expression is not an integral constant expression}}
#pragma omp distribute parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = ST; i < N; i++)
argv[0][i] = argv[0][i] - argv[0][i-ST];

Expand Down Expand Up @@ -144,11 +147,14 @@ int main(int argc, char **argv) {
argv[0][i] = argv[0][i] - argv[0][i-4];

#if __cplusplus >= 201103L
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}} expected-error {{expression is not an integral constant expression}}
// expected-error@+3 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+2 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}}
// expected-error@+1 {{expression is not an integral constant expression}}
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = 4; i < 12; i++)
argv[0][i] = argv[0][i] - argv[0][i-4];

Expand Down
Expand Up @@ -66,11 +66,14 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
argv[0][i] = argv[0][i] - argv[0][i-ST];

#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp distribute parallel for simd simdlen (foobool(argc)), simdlen (true), simdlen (-5) // expected-error 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'simdlen' clause}} expected-error 2 {{argument to 'simdlen' clause must be a strictly positive integer value}} expected-error 2 {{expression is not an integral constant expression}}
// expected-error@+3 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'simdlen' clause}}
// expected-error@+2 {{argument to 'simdlen' clause must be a strictly positive integer value}}
// expected-error@+1 2 {{expression is not an integral constant expression}}
#pragma omp distribute parallel for simd simdlen (foobool(argc)), simdlen (true), simdlen (-5)
for (int i = ST; i < N; i++)
argv[0][i] = argv[0][i] - argv[0][i-ST];

Expand Down Expand Up @@ -148,11 +151,14 @@ int main(int argc, char **argv) {


#if __cplusplus >= 201103L
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp distribute parallel for simd simdlen (foobool(argc)), simdlen (true), simdlen (-5) // expected-error {{expression is not an integral constant expression}} expected-error 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'simdlen' clause}} expected-error 2 {{argument to 'simdlen' clause must be a strictly positive integer value}}
// expected-error@+3 {{expression is not an integral constant expression}}
// expected-error@+2 2 {{directive '#pragma omp distribute parallel for simd' cannot contain more than one 'simdlen' clause}}
// expected-error@+1 {{argument to 'simdlen' clause must be a strictly positive integer value}}
#pragma omp distribute parallel for simd simdlen (foobool(argc)), simdlen (true), simdlen (-5)
for (int i = 4; i < 12; i++)
argv[0][i] = argv[0][i] - argv[0][i-4];

Expand Down
4 changes: 2 additions & 2 deletions clang/test/OpenMP/distribute_simd_collapse_messages.cpp
Expand Up @@ -53,7 +53,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
#pragma omp distribute simd collapse ((ST > 0) ? 1 + ST : 2) // expected-note 2 {{as specified in 'collapse' clause}}
for (int i = ST; i < N; i++) argv[0][i] = argv[0][i] - argv[0][i-ST]; // expected-error 2 {{expected 2 for loops after '#pragma omp distribute simd', but found only 1}}
// expected-error@+8 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'collapse' clause}}
// expected-error@+7 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+7 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+6 2 {{expression is not an integral constant expression}}
#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
Expand Down Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
// expected-note@+6{{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+4 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'collapse' clause}}
// expected-error@+3 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+3 {{argument to 'collapse' clause must be a strictly positive integer value}}
#pragma omp target
#pragma omp teams
#pragma omp distribute simd collapse (foobool(argc)), collapse (true), collapse (-5)
Expand Down
14 changes: 10 additions & 4 deletions clang/test/OpenMP/distribute_simd_safelen_messages.cpp
Expand Up @@ -63,11 +63,14 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
argv[0][i] = argv[0][i] - argv[0][i-ST];

#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp distribute simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'safelen' clause}} expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{expression is not an integral constant expression}}
// expected-error@+3 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'safelen' clause}}
// expected-error@+2 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+1 2 {{expression is not an integral constant expression}}
#pragma omp distribute simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = ST; i < N; i++)
argv[0][i] = argv[0][i] - argv[0][i-ST];

Expand Down Expand Up @@ -144,11 +147,14 @@ int main(int argc, char **argv) {
argv[0][i] = argv[0][i] - argv[0][i-4];

#if __cplusplus >= 201103L
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}} expected-error {{expression is not an integral constant expression}}
// expected-error@+3 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+2 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}}
// expected-error@+1 {{expression is not an integral constant expression}}
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = 4; i < 12; i++)
argv[0][i] = argv[0][i] - argv[0][i-4];

Expand Down
14 changes: 10 additions & 4 deletions clang/test/OpenMP/distribute_simd_simdlen_messages.cpp
Expand Up @@ -63,11 +63,14 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
argv[0][i] = argv[0][i] - argv[0][i-ST];

#if __cplusplus >= 201103L
// expected-note@+4 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp distribute simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'safelen' clause}} expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{expression is not an integral constant expression}}
// expected-error@+3 2 {{directive '#pragma omp distribute simd' cannot contain more than one 'safelen' clause}}
// expected-error@+2 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+1 2 {{expression is not an integral constant expression}}
#pragma omp distribute simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = ST; i < N; i++)
argv[0][i] = argv[0][i] - argv[0][i-ST];

Expand Down Expand Up @@ -144,11 +147,14 @@ int main(int argc, char **argv) {
argv[0][i] = argv[0][i] - argv[0][i-4];

#if __cplusplus >= 201103L
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
// expected-note@+7 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
#pragma omp target
#pragma omp teams
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5) // expected-error 2 {{argument to 'safelen' clause must be a strictly positive integer value}} expected-error 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}} expected-error {{expression is not an integral constant expression}}
// expected-error@+3 {{argument to 'safelen' clause must be a strictly positive integer value}}
// expected-error@+2 2 {{directive '#pragma omp parallel for simd' cannot contain more than one 'safelen' clause}}
// expected-error@+1 {{expression is not an integral constant expression}}
#pragma omp parallel for simd safelen (foobool(argc)), safelen (true), safelen (-5)
for (int i = 4; i < 12; i++)
argv[0][i] = argv[0][i] - argv[0][i-4];

Expand Down
4 changes: 2 additions & 2 deletions clang/test/OpenMP/for_collapse_messages.cpp
Expand Up @@ -39,7 +39,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}}
#pragma omp for collapse ((ST > 0) ? 1 + ST : 2) // expected-note 2 {{as specified in 'collapse' clause}}
for (int i = ST; i < N; i++) argv[0][i] = argv[0][i] - argv[0][i-ST]; // expected-error 2 {{expected 2 for loops after '#pragma omp for', but found only 1}}
// expected-error@+6 2 {{directive '#pragma omp for' cannot contain more than one 'collapse' clause}}
// expected-error@+5 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+5 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+4 2 {{expression is not an integral constant expression}}
#if __cplusplus >= 201103L
// expected-note@+2 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
Expand Down Expand Up @@ -86,7 +86,7 @@ int main(int argc, char **argv) {
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+2 2 {{directive '#pragma omp for' cannot contain more than one 'collapse' clause}}
// expected-error@+1 2 {{argument to 'collapse' clause must be a strictly positive integer value}}
// expected-error@+1 {{argument to 'collapse' clause must be a strictly positive integer value}}
#pragma omp for collapse (foobool(argc)), collapse (true), collapse (-5)
for (int i = 4; i < 12; i++) argv[0][i] = argv[0][i] - argv[0][i-4];
#pragma omp for collapse (S1) // expected-error {{'S1' does not refer to a value}}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/OpenMP/for_ordered_clause.cpp
Expand Up @@ -47,7 +47,7 @@ T tmain(T argc, S **argv) { //expected-note 2 {{declared here}
for (int i = ST; i < N; i++)
argv[0][i] = argv[0][i] - argv[0][i - ST]; // expected-error 2 {{expected 2 for loops after '#pragma omp for', but found only 1}}
// expected-error@+6 2 {{directive '#pragma omp for' cannot contain more than one 'ordered' clause}}
// expected-error@+5 2 {{argument to 'ordered' clause must be a strictly positive integer value}}
// expected-error@+5 {{argument to 'ordered' clause must be a strictly positive integer value}}
// expected-error@+4 2 {{expression is not an integral constant expression}}
#if __cplusplus >= 201103L
// expected-note@+2 2 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
Expand Down Expand Up @@ -113,7 +113,7 @@ int main(int argc, char **argv) {
// expected-note@+4 {{non-constexpr function 'foobool' cannot be used in a constant expression}}
#endif
// expected-error@+2 2 {{directive '#pragma omp for' cannot contain more than one 'ordered' clause}}
// expected-error@+1 2 {{argument to 'ordered' clause must be a strictly positive integer value}}
// expected-error@+1 {{argument to 'ordered' clause must be a strictly positive integer value}}
#pragma omp for ordered(foobool(argc)), ordered(true), ordered(-5)
for (int i = 4; i < 12; i++)
argv[0][i] = argv[0][i] - argv[0][i - 4];
Expand Down

0 comments on commit 3234887

Please sign in to comment.