Edit

Share via


Compiler warnings (level 1) C5301 and C5302

'#pragma omp for': 'loop-index' increases while loop condition uses 'comparison'; non-terminating loop?

'#pragma omp for': 'loop-index' decreases while loop condition uses 'comparison'; non-terminating loop?

Remarks

Along with improved support for OpenMP 3.1, we've added two diagnostics, C5301 and C5302, to improve the developer experience. These diagnostics check that the loop conditions for omp parallel for are correct, based on whether the loop index variable is increasing or decreasing. These checks work for both integral and pointer indices.

These compiler warnings are new in Visual Studio 2022 version 17.4.

Example

The sample code shows a diagnostic for a for loop that decrements the index, but it uses a <= comparison that tests whether the index is less than a value higher than the starting value.

// C5302.c
// compile using /openmp

#include <stdio.h>

int main()
{
    int a[100], i;
    int k = 1;
    #pragma omp parallel for
    for (i = 0; i <= 100; i--)
        a[i] = i*i;
}

/*
Compiler warning message:

.\C5302.c(11,19): warning C5302: '#pragma omp for': 'i' decreases while loop condition uses '<='; non-terminating loop?
    for (i = 0; i <= 100; i--)
                  ^
*/

To resolve this issue, change the test condition or the direction of the index change to one that terminates without causing overflow, underflow, or other undefined behavior.