Clang error: 'A' declared as array of functions of type B (err_illegal_decl_array_of_functions)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: 'A' declared as array of functions of type B
Type Error
Category Semantic Issue
Internal Id err_illegal_decl_array_of_functions
Internal Message '%0' declared as array of functions of type %1
Regular Expression (?:error|fatal error)\: '(.*?)' declared as array of functions of type (.*?)
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when an attempt is made to declare an array where the element type is a function. In C and C++, functions cannot be elements of an array, though it is possible to have an array of function pointers. This error typically indicates a misunderstanding of how functions and arrays can be utilized together in these languages. Specifically, the error message is triggered when the syntax suggests that an array is being declared with elements that are of a function type, which is not allowed by the language specifications. The correct approach to achieve similar functionality would be to use an array of pointers to functions instead. This distinction is crucial for writing valid C or C++ code as it relates to the organization of data in memory and how functions are invoked.  
AI Generated

Example

In the following example, an attempt is made to declare an array named a with ten elements, where each element is specified to be of a function type that returns an int and takes no arguments. This declaration misunderstands the syntax and conventions of the C++ programming language, as it is not possible to create arrays of functions. The correct approach is to use arrays of function pointers instead. This distinction is crucial for the proper definition and invocation of functions within an array structure. The generated compiler error message highlights the semantic issue and indicates that the element type of the array is incorrectly specified as a function type.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
int main() { int a[10](); } // a is incorrectly declared as an array of functions returning int
Compiler Output
<source>:1:19: error: 'a' declared as array of functions of type 'int ()'


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

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/SemaType.cpp (line 2530)

/// Build an array type.
///
/// \param T The type of each element in the array.
///
/// \param ASM C99 array size modifier (e.g., '*', 'static').
///
/// \param ArraySize Expression describing the size of the array.
///
/// \param Brackets The range from the opening '[' to the closing ']'.
///
/// \param Entity The name of the entity that involves the array
/// type, if known.
///
/// \returns A suitable array type, if there are no errors. Otherwise,
/// returns a NULL type.
QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, Expr *ArraySize, unsigned Quals, SourceRange Brackets, DeclarationName Entity) {
  // ...
  if (T->isFunctionType()) {
    Diag(Loc, diag::err_illegal_decl_array_of_functions) << getPrintableNameForEntity(Entity) << T;

Triggered in Clang Tests

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

clang/test/Sema/static-array.c

  • clang/test/Sema/static-array.c:60:14: error: 'x' declared as array of functions of type 'int (void)'