Clang warning: tentative array definition assumed to have one element (warn_tentative_incomplete_array)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: tentative array definition assumed to have one element
Type Warning
Category Semantic Issue
Internal Id warn_tentative_incomplete_array
Active by Default Yes
Internal Message tentative array definition assumed to have one element
Regular Expression (?:warning|error|fatal error)\: tentative array definition assumed to have one element(?: \[(?:\-Werror)?[^\]]*\])?
First Commit 2009-04-15 c7ba5333782f Add warning when a tentative array definition is assumed to have one element.

Description

The warning is issued by the Clang compiler when it encounters a declaration of an array without a specified size, known as a tentative definition. In C, arrays declared at file scope or as static without an explicit size and without an initializer are allowed, and are referred to as tentative definitions. The Clang compiler, in the absence of a complete definition later in the translation unit, assumes such arrays to have a single element. This behavior is aligned with the C standard, which stipulates that a tentative definition without a size, not followed by a definition with a size, is treated as if it were declared with a size of one.

This diagnostic is useful for identifying places where the size of an array is not explicitly defined, which can lead to unexpected behavior in programs. By default, this diagnostic is enabled and emitted as a warning, encouraging developers to specify the size of their arrays or to provide an initializer ensuring a specific size is deduced. The rationale behind issuing a warning rather than an error is to allow compatibility with legacy code that might rely on this feature of the C language, while still guiding developers towards more precise and safer code practices.

int example[]; // This declaration triggers the warning.

Such declarations may be intentional but can lead to bugs if the program's logic assumes the array contains more elements. Clarifying the array size or providing an initializer can mitigate potential errors and improve the readability and maintainability of the code.

 
AI Generated

Example

In the following example, an array named a is declared without specifying its size. This leads to the Clang compiler emitting a warning, indicating that it assumes the array to have only one element. This behavior is a result of the array being a tentative definition, which in C, allows for arrays to be declared without an explicit size at file scope or as static. The warning serves to inform the developer of the assumption made by the compiler, potentially preventing unexpected behavior in programs where the actual size of the array matters.  
AI Generated


Flags -xc

[Try out in Compiler Explorer]

Source
int a[]; // Unspecified array size
int main() { return 0; }
Compiler Output
<source>:1:5: warning: tentative array definition assumed to have one element


Clang Internals (17.0.6)

Git Commit Message

Add warning when a tentative array definition is assumed to have one element.
 - Also, fixed one to actually be one (instead of zero). :)

llvm-svn: 69226

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/Sema.cpp (line 1307)

/// ActOnEndOfTranslationUnit - This is called at the very end of the
/// translation unit when EOF is reached and all but the top-level scope is
/// popped.
void Sema::ActOnEndOfTranslationUnit() {
  // ...
  for (TentativeDefinitionsType::iterator T = TentativeDefinitions.begin(ExternalSource.get()), TEnd = TentativeDefinitions.end(); T != TEnd; ++T) {
    // ...
    if (const IncompleteArrayType *ArrayT = Context.getAsIncompleteArrayType(VD->getType())) {
      // ...
      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);

Triggered in Clang Tests

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

clang/test/Sema/extern-redecl.c

  • clang/test/Sema/extern-redecl.c:4:12: warning: tentative array definition assumed to have one element
  • clang/test/Sema/extern-redecl.c:23:12: warning: tentative array definition assumed to have one element