uninitialized variable warning, etc

given following code:
~/clang-test-> cat test.c

#include <stdio.h>

int main (void) {
  int a = 0, b;

  a = b + a;
  printf ("a = %s", a);
  return 0;
}

/clang-test-> gcc -Wall -Wno-switch -Wdisabled-optimization -Wpointer-arith -Wredundant-decls -Wno-pointer-sign -Wcast-qual -Wwrite-strings -O3 -fno-math-errno -std=c99 test.c
test.c: In function ‘main’:
test.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
test.c:7: warning: ‘b’ is used uninitialized in this function

~/clang-test-> ccc -Wall -Wno-switch -Wdisabled-optimization -Wpointer-arith -Wredundant-decls -Wno-pointer-sign -Wcast-qual -Wwrite-strings -O3 -fno-math-errno -std=c99 test.c

~/clang-test-> gcc -Wall -std=c99 test.c test.c: In function ‘main’:
test.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

~/clang-test-> gcc -Wall -O3 test.c
test.c: In function ‘main’:
test.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
test.c:7: warning: ‘b’ is used uninitialized in this function

~/clang-test-> ccc -Wall -O3 -std=c99 test.c
~/clang-test->

Also clang warns on the following but gcc doesn't??
libavcodec/dsputil.c:177:8: warning: use of unary operator that may be intended as compound assignment (-=)
    end=-1;
       ^~
libavcodec/dsputil.c:3966:10: warning: use of unary operator that may be intended as compound assignment (-=)
    for(i=-len, j=len-1; i<0; i++, j--) {
         ^~

Hi,

clang does not yet implement the uninitialized values warning outside
of the static analysis engine.

test.c: In function ‘main’:
test.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2

There's a TODO in the code to add this check.

has type ‘int’
test.c:7: warning: ‘b’ is used uninitialized in this function

clang doesn't have a warning like this as part of the default
compilation process, at least for the moment. There is a separate
analysis module which prints warnings like this, though; see
http://clang.llvm.org/StaticAnalysis.html.

libavcodec/dsputil.c:3966:10: warning: use of unary operator that may
be intended as compound assignment (-=)

This warning triggers on a lot of stuff... we should probably disable
it by default.

-Eli

Daniel,

I think this is a matter of mapping -Wuninitialized to running the same logic as -warn-unit-values. That logic is just a simple flow-sensitive check.

Thanks Daniel,

May be there should note on how a programmer would do the compile
and analysis, So they won't get confused. I mean do I compile and then
analyze.

anyway I ran the analyze command
on test.c and it only gave one warning. may be ccc is not passing -analyze command
properly.

~/clang-test-> cat test.c
#include <stdio.h>
#include <stdlib.h>

int main (void) {
   int a = 0;
   int b;

   a = b + a;
   printf ("a = %d", a);

   exit(0);
}

~/clang-test-> ccc --analyze test.c
test.c:9:3: warning: Pass-by-value argument in function call is undefined.
   printf ("a = %d", a);
   ^ ~
1 diagnostic generated.

If I comment out the printf statement then I also only get one warning. So something is wrong.
~/clang-test-> ccc --analyze test.c
test.c:8:3: warning: Value stored to 'a' is never read
   a = b + a;
   ^ ~~~~~
1 diagnostic generated.

P.S. I would expect -Wall to cover all warning but I guess I am asking too much.

test.c: In function ‘main’:
test.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2

There's a TODO in the code to add this check.

has type ‘int’
test.c:7: warning: ‘b’ is used uninitialized in this function

clang doesn't have a warning like this as part of the default
compilation process, at least for the moment. There is a separate
analysis module which prints warnings like this, though; see
http://clang.llvm.org/StaticAnalysis.html.

Right, note that GCC only produces "dataflow warnings" when optimizations are enabled though.

libavcodec/dsputil.c:3966:10: warning: use of unary operator that may
be intended as compound assignment (-=)

This warning triggers on a lot of stuff... we should probably disable
it by default.

What sorts of cases does it trigger on that are false positives?

-Chris

Here are some examples taken from ffmpeg-export-2008-12-18

         s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x

             for(change=-1; change<=1; change+=2){

         next_dir=-1;

         for(mv=-(16<<f_code); mv<(16<<f_code); mv++){

     c=d=e=-1;

             pred_table[0]=-2;

                 lpc[i][j]=-m[(pass-1)&1].coeff[i][j];

Here are some examples taken from ffmpeg-export-2008-12-18

These are all exactly the sorts of things that the warning was designed to catch. Are spaces really that horrible to use? :slight_smile:

-Chris

Trying to dictate a coding style using compiler warnings isn't
productive. I like the suggestion that we should warn on "=- " (with
a trailing space), but not not just "=-"; the false positive rate with
the trailing space should be almost zero, and the people using a
coding style that puts spaces around operators keep the benefit of the
warning.

-Eli

Here are some examples taken from ffmpeg-export-2008-12-18

These are all exactly the sorts of things that the warning was designed to
catch. Are spaces really that horrible to use? :slight_smile:

Trying to dictate a coding style using compiler warnings isn't
productive.

I completely disagree. Warnings are *all about* (optionally, when enabled) forcing coding standards on people. Warnings are inherently about code that is valid but dubious.

I like the suggestion that we should warn on "=- " (with
a trailing space), but not not just "=-"; the false positive rate with
the trailing space should be almost zero, and the people using a
coding style that puts spaces around operators keep the benefit of the
warning.

That is a great suggestion, implemented!
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20090302/013618.html

-Chris

fyi,

this example fails your new test.

libavcodec/mpeg12enc.c:756:19: warning: use of unary operator that may be intended as compound assignment (-=)
             for(mv=-MAX_MV; mv<=MAX_MV; mv++){
                   ^~

Please include a self-contained .c file.

-Chris

here you go.

unary.c (110 Bytes)

fixed in r66416, thanks!

-Chris