www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Clang of LLVM 2.9

reply bearophile <bearophileHUGS lycos.com> writes:
LLVM 2.9 is out (since some days), and for the first time Clang is available
for Windows. Beside normal warnings has a "--analyze" option that performs more
static tests on the code. Being a young feature it's surely not as powerful as
a lint (as split, that's free, splint.org ), but it's better than just a C
compiler.

I have tried Clang on some little pieces of wrong C code, and later on larger C
programs. It outputs quite colorful error messages (here you see no colors). It
compiles C++ code too, with Clang++.

---------------------------

int main() {
    int x;
    return 0;
}


...>clang -Wall temp.c -o temp
temp.c:2:9: warning: unused variable 'x' [-Wunused-variable]
    int x;
        ^
1 warning generated.

---------------------------

int main() {
    int x;
    x = 10;
    return 0;
}


...>clang -Wall -Wextra --analyze temp.c -o temp
temp.c:3:5: warning: Value stored to 'x' is never read
    x = 10;
    ^   ~~
1 warning generated.

---------------------------

#include <stdlib.h>

int main(int argc, char **argv) {
  int *p = malloc(10 * sizeof(int));
  return 0;
}


...>clang --analyze temp.c -o temp
temp.c:4:8: warning: Value stored to 'p' during its initialization is never read
  int *p = malloc(10 * sizeof(int));
       ^   ~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

---------------------------

#include <stdlib.h>

int main(int argc, char **argv) {
  int *q;
  q[5] = 1;
  return 0;
}


...>clang --analyze temp.c -o temp
temp.c:5:3: warning: Dereference of undefined pointer value
  q[5] = 1;
  ^
1 warning generated.

---------------------------

#include <stdlib.h>

int main(int argc, char **argv) {
  int *q = NULL;
  q[5] = 1;
  return 0;
}


...>clang --analyze temp.c -o temp
temp.c:5:3: warning: Array access (from variable 'q') results in a null pointer
dereference
  q[5] = 1;
  ^
1 warning generated.

---------------------------

#include <stdio.h>

void f(int a) {
  int b;
  //if (a > 0) b = 1;
  if (a > 5) printf("%d", b);
}
int main() {
  return 0;
}


...>clang --analyze temp.c -o temp
temp.c:6:14: warning: Function call argument is an uninitialized value
  if (a > 5) printf("%d", b);
             ^            ~
1 warning generated.

---------------------------

#include <stdio.h>

void f(int a) {
  int b;
  if (a > 0) b = 1;
  if (a > 5) printf("%d", b);
}
int main() {
  return 0;
}


No errors.

---------------------------

#include <stdio.h>

int sqr(int x, int y) {
  return x * x;
}
int main() {
  return 0;
}


...>clang -Wall -Wextra temp.c -o temp
temp.c:3:20: warning: unused parameter 'y' [-Wunused-parameter]
int sqr(int x, int y) {
                   ^
1 warning generated.

---------------------------

typedef unsigned int UINT;
int main(int argc, char **argv) {
  UINT x = 10;
  if (x == argc) return 1;
  return 0;
}


...>clang -Wall -Wextra temp.c -o temp
temp.c:4:9: warning: comparison of integers of different signs: 'UINT' (aka
'unsigned int') and 'int'
      [-Wsign-compare]
  if (x == argc) return 1;
      ~ ^  ~~~~

---------------------------

Bye,
bearophile
Apr 25 2011
parent reply Daniel Gibson <metalcaedes gmail.com> writes:
Am 26.04.2011 03:59, schrieb bearophile:
 LLVM 2.9 is out (since some days), and for the first time Clang is available
for Windows. 

 It compiles C++ code too, with Clang++. 
Does it support exceptions on Windows? AFAIK that wasn't supported in older LLVM versions (and thus in LDC).
Apr 25 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Daniel Gibson:

 Does it support exceptions on Windows?
 AFAIK that wasn't supported in older LLVM versions (and thus in LDC).
I have just tried it with this (fixing the output): https://alioth.debian.org/scm/viewvc.php/*checkout*/shootout/bench/except/except.gcc?revision=1.1.1.1&root=shootout At runtime the program stops quickly with a message like: terminate called after throwing an instance of '...' So I think it throws exceptions but doesn't catch them :-( Bye, bearophile
Apr 25 2011