Clang – YIKES – a C compiler with user-friendly error messages!

One of the inherent problems with many programming languages is that compilers are often less than friendly when it comes to compiler messages. Not so with the Clang compiler – front end for C, C++, Objective-C and Objective-C++. Developed by Apple, this compiler actually produces messages the novice can understand. Consider the following piece of non-sensical code, containing a number of syntactic errors:

1  #include <stdio.h>
2
3  int main(void)
4  {
5      int anArray[n];
6      int i, n=10;
7      char aString[20];
8
9      scanf("%s", &aString);
10     i = 0;
11     while(true){
12         anArray[i] = 0
13     }
14     return 0;
15 }

When the code is compiled with gcc, we get the following messages:

$ gcc -Wall -std=c99 errors.c
errors.c: In function ‘main’:
errors.c:5: error: ‘n’ undeclared (first use in this function)
errors.c:5: error: (Each undeclared identifier is reported only once
errors.c:5: error: for each function it appears in.)
errors.c:9: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
errors.c:9: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
errors.c:11: error: ‘true’ undeclared (first use in this function)
errors.c:13: error: expected ‘;’ before ‘}’ token
errors.c:6: warning: unused variable ‘n’
errors.c:5: warning: unused variable ‘anArray’

Clearly from these error messages, there are issues with the code. Basically here are the actual problems with the syntax of the code:

  1. On line 5 of the code, n is used to size the integer array anArray, however n is not declared until line 6.
  2. On line 6, n is declared but never used.
  3. On line 9, the scanf() function uses an ampersand, &, before the string variable, which is redundant.
  4. On line 11, the variable true does not exist.
  5. On line 12, the statement is not terminated by a ;.

The inherent problem with gcc, and classically most compilers is that the error messages may not always be worded in the most appropriate way for novice programmers. That and no context is given, requiring the programmer to refer back to the code.

Here are the messages produced using Clang:

cc errors.c
errors.c:5:17: error: use of undeclared identifier 'n'
    int anArray[n];
                ^
errors.c:9:13: warning: format specifies type 'char *' but the argument has type
 'char (*)[20]' [-Wformat]
    scanf("%s", &aString);
           ~^ ~~~~~~~~
errors.c:11:11: error: use of undeclared identifier 'true'
    while(true){
          ^
1 warning and 2 errors generated.

Firstly, you’ll notice that this compiler has colour-coded the error messages. This makes it easier to differentiate errors from warnings. Secondly, it actually reproduces the offending code. This means the programmer does not need to refer back to the code in order to understand the problems. Thirdly, the source of the error on the line is pinpointed using the ^ character. Now not all the errors are shown, and that may be of benefit to the novice, as to not overwhelm them.

Fixing these 2 errors and the warning and re-compiling it results in the final problem being routed out:

errors.c:12:23: error: expected ';' after expression
    anArray[i] = 0
                  ^
                  ;
1 error generated.

A compiler well worth considering.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s