Clang of LLVM 2.9
bearophile
bearophileHUGS at lycos.com
Mon Apr 25 18:59:28 PDT 2011
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
More information about the Digitalmars-d
mailing list