Clang warning: plain '_Complex' requires a type specifier; assuming '_Complex double' (ext_plain_complex)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: plain '_Complex' requires a type specifier; assuming '_Complex double'
Type Warning
Category Semantic Issue
Internal Id ext_plain_complex
Active by Default Yes
Internal Message plain '_Complex' requires a type specifier; assuming '_Complex double'
Regular Expression (?:warning|error|fatal error)\: plain '_Complex' requires a type specifier; assuming '_Complex double'(?: \[(?:\-Werror)?[^\]]*\])?
First Commit 2009-03-04 b1c4d5507fad The basic representation of diagnostics information in tablegen format, plus (uncommented and incomp...

Description

The warning is issued by the Clang compiler when a '_Complex' type declaration is used without an explicit type specifier. In the C language, '_Complex' is a keyword used to define complex number types. However, the C standard requires that a complex type must be specified with a base type such as 'float', 'double', or 'long double'. When a program declares a '_Complex' variable without accompanying it with a specific type, Clang assumes '_Complex double' as the default. This assumption is made because 'double' is the most common type used with complex numbers, offering a balance between precision and computational efficiency. The warning serves as a notification to the programmer that the type for the '_Complex' variable was not explicitly mentioned, and that the compiler has defaulted to '_Complex double'. It encourages the addition of a type specifier to ensure the code behaves as intended and to improve code clarity.  
AI Generated

Example

In the following example, a variable named x is declared using the _Complex keyword without an accompanying type specifier. Typically, _Complex should be followed by a concrete type, such as float, double, or long double, to specify the base type of the complex number. Since no specific type is provided in this declaration, the Clang compiler generates a warning. The warning indicates that the absence of a type specifier renders _Complex ambiguous, and therefore, the compiler assumes _Complex double as the default. This default denotes a complex number with double as its base type. The warning serves to inform that an explicit type should be specified to avoid any assumptions by the compiler and to ensure the code behaves as intended.  
AI Generated


Flags -xc

[Try out in Compiler Explorer]

Source
#include <complex.h>

int main() {
  _Complex x; // Missing type specifier
  return 0;
}
Compiler Output
<source>:4:3: warning: plain '_Complex' requires a type specifier; assuming '_Complex double'


Clang Internals (17.0.6)

Git Commit Message

The basic representation of diagnostics information in tablegen format, plus (uncommented and incomplete) test conversions of the existing def files to this format.

llvm-svn: 66064

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/DeclSpec.cpp (line 1306)

/// Finish - This does final analysis of the declspec, rejecting things like
/// "_Imaginary" (lacking an FP type). After calling this method, DeclSpec is
/// guaranteed to be self-consistent, even if an error occurred.
void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
  // ...
  // TODO: if the implementation does not implement _Complex or _Imaginary,
  // disallow their use.  Need information about the backend.
  if (TypeSpecComplex != TSC_unspecified) {
    if (TypeSpecType == TST_unspecified) {
      S.Diag(TSCLoc, diag::ext_plain_complex) << FixItHint::CreateInsertion(S.getLocForEndOfToken(getTypeSpecComplexLoc()), " double");

Triggered in Clang Tests

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

clang/test/FixIt/fixit-recompile.c

  • clang/test/FixIt/fixit-recompile.c:4:1: error: plain '_Complex' requires a type specifier; assuming '_Complex double' [-Werror]