Clang 15 will be available with Red Hat Enterprise Linux (RHEL) 8.8 and RHEL 9.2. It comes with some new and upgraded warnings that will help modernize your C/C++ code base. Starting with version 15, Clang is beginning to emit more warnings and errors by default for code constructs that have been deprecated in newer versions of C. This includes implicit function definitions and functions without prototypes.

-Wint-conversion

In Clang 15, the -Wint-conversion warning has been upgraded to an error. This warning catches when an integer is being implicitly converted to a pointer. One common mistake that this warning will catch is implicit function definitions, where code tries to call a function that has not been declared. In C89/C90, undeclared functions are assumed to return int, so if you try to assign the value of an undeclared function to a pointer, Clang will now emit this error:

$ cat int-conversion.c
int foo () {
  char *s = missing_decl();
}
$ clang -c int-conversion.c
int-conversion.c:2:13: warning: call to undeclared function 'missing_decl'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  char *s = missing_decl();
            ^
int-conversion.c:2:9: error: incompatible integer to pointer conversion initializing 'char *' with an expression of type 'int' [-Wint-conversion]
  char *s = missing_decl();
        ^   ~~~~~~~~~~~~~~
1 warning and 1 error generated.

-Wdeprecated-non-prototype

-Wdeprecated-non-prototype is a new warning that will catch cases where a deprecated prototype would have a different behavior in C2x.

cat deprecated-non-prototype.c 
static void f();

static void f(char *s) { }
[root@intel-xeon-ee-iotg-04 ~]# clang -c deprecated-non-prototype.c 
deprecated-non-prototype.c:1:13: warning: a function declaration without a prototype is deprecated in all versions of C and is treated as a zero-parameter prototype in C2x, conflicting with a subsequent definition [-Wdeprecated-non-prototype]
static void f();
            ^
deprecated-non-prototype.c:3:13: note: conflicting prototype is here
static void f(char *s) { }
            ^
1 warning generated.

Definition without a prototype

Clang 15 will now emit an error if you forget the prototype in a function definition that has a declaration with a prototype.

cat prototype.c
void f(int); void f() {}

clang -c prototype.c 
prototype.c:1:19: error: conflicting types for 'f'
void f(int); void f() {} 
                  ^
prototype.c:1:6: note: previous declaration is here
void f(int); void f() {} 
     ^
1 error generated.

-Wunused-but-set-variable improvements

The -Wunused-but-set-variable warning, which checks for unused variables in the code, has been updated to be able to detect variables for unary operators like ++.

$ cat unused-but-set.c
void do_something(void);

void f(void) {

 int count, i;
 count = 0;
 for (i = 0; i < 5; i++) {
   do_something();
   count++;
  }
}

$ clang -c unused-but-set.c -Wunused-but-set-variable
unused-but-set.c:5:6: warning: variable 'count' set but not used [-Wunused-but-set-variable]
 int count, i;
     ^
1 warning generated.

Learn more

While Clang 15 won’t be available in RHEL until the forthcoming minor releases are out, you can get a preview of the latest Clang 15 builds from CentOS Stream. Even if you are using Gnu Compiler Collection (GCC), or an older version of Clang as the compiler for your application, you can still do a one-off build with Clang 15 to see these new warnings and errors to catch any existing mistakes in your code. Clang 16 and future versions of GCC will become even more strict about deprecated C constructs, so it’s a good idea to start finding these problems as soon as you can.


執筆者紹介