Clang error: const-qualified variable cannot be A (err_omp_const_variable)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: const-qualified variable cannot be A
Type Error
Category OpenMP Issue
Internal Id err_omp_const_variable
Internal Message const-qualified variable cannot be %0
Regular Expression (?:error|fatal error)\: const\-qualified variable cannot be (.*?)
First Commit 2014-04-22 8dba66412b0a [OPENMP] parsing 'linear' clause (for directive 'omp simd')

Description

The error is issued by the Clang compiler when it encounters a const-qualified variable in a context where the language standard does not allow such a variable to be used. This situation typically arises in parallel programming constructs, such as those provided by OpenMP, where variables are shared across different threads of execution. The error indicates that the const-qualified variable cannot participate in the specific OpenMP clause as described by the error message, due to restrictions imposed by both the const qualifier and the OpenMP specifications. The const qualifier implies that the variable's value is not meant to be altered after its initialization, meaning that any attempt to modify it, or specify it in a context where modification is implied or required, would be incorrect. This restriction is pivotal in maintaining data integrity across the concurrent execution paths in multi-threaded environments.  
AI Generated

Example

In the following example, a scenario is encountered where a const-qualified variable is used within an OpenMP parallel construct, specifically within a parallel for directive. The variable x is defined outside the parallel block as a constant, implying that its value should remain unchanged throughout the program's execution. The code attempts to declare this constant variable as private to each thread in the parallel block. However, this contradicts the OpenMP and C++ language standards because a const-qualified variable, by definition, cannot be altered, and declaring it as private in a parallel context implies a per-thread variable that can be independently modified. This results in the Clang compiler emitting an error indicating this misuse of a const-qualified variable within the specified context, thereby enforcing the const-correctness and the semantics of the OpenMP standard, which are crucial for the predictable execution of parallel code.  
AI Generated


Flags -fopenmp -xc

[Try out in Compiler Explorer]

Source
#include <omp.h>

int main() {
  const int x = 10; // x is const
  #pragma omp parallel for private(x) // error: x cannot be private in OpenMP
  for(int i = 0; i < 10; i++) {}
  return 0;
}
Compiler Output
<source>:5:36: error: const-qualified variable cannot be private
<source>:4:13: note: 'x' defined here


Clang Internals (17.0.6)

Git Commit Message

[OPENMP] parsing 'linear' clause (for directive 'omp simd')

Differential Revision: http://reviews.llvm.org/D3272

llvm-svn: 206891

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaOpenMP.cpp (line 1712)

static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D, QualType Type, OpenMPClauseKind CKind, SourceLocation ELoc, bool AcceptIfMutable = true, bool ListItemNotVar = false) {
  // ...
  if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
    unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item : IsClassType ? diag::err_omp_const_not_mutable_variable : diag::err_omp_const_variable;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/OpenMP/target_teams_private_messages.cpp

  • clang/test/OpenMP/target_teams_private_messages.cpp:88:44: error: const-qualified variable cannot be private
  • clang/test/OpenMP/target_teams_private_messages.cpp:88:47: error: const-qualified variable cannot be private
  • clang/test/OpenMP/target_teams_private_messages.cpp:96:34: error: const-qualified variable cannot be private