Clang, a better compiler for C and its derivatives.

If there’s a project that caught my interest lately, it’s Clang, a front-end for the LLVM compiler created as a “drop-in” replacement for gcc. It’s a brand new, state of the art, modern, BSD-licensed open-source compiler that is a lot faster, optimizes more aggressively while using far less memory than the competition. For now, only the C(Yes, at last, a C99 compiler) and Objective C support is complete and are considered “production ready”. C++’s support is still only partial but the situation is improving at a very fast pace. See http://clang.llvm.org/cxx_status.html for more information about it.

Having a compiler that produces good and optimized code is always welcome but… Is there anything left in it for the programmer? Yes, clang’s static analyzer provides the programmer with clearer, better diagnostic messages. It is also easier to integrate to IDEs and is already doing wonders in Apple’s XCode 3.2. Let’s see what it can do.

Both gcc’s and cl’s diagnostic are wrong, the semi-colon should be placed at the end of line 8. On the other side, clang found that a semi-colon is missing at the 26th character of line 8 and even points it with ^, a really nice improvement.

Now, all three compilers found the error but the diagnostics of gcc and cl are not really helpful, they only tell us that we cannot add two LPCTSTR but doesn’t give us a clue as to why. CLang’s diagnostic message is about the same but also shows us that LPCTSTRs are simply char pointers in disguise and where is the exact source of the problem.

Third Error: Passing a const char * to a function taking char * could be dangerous

Now, gcc’s diagnostic is just plain vague, the fact is mentions “pointer target type” can give us a small hint that LPCTSTR might be a pointer but it’s not like “const” is the only possible qualifier in C. cl does a bit better telling us that it’s a constness issue but again, clang is clearly the winner by letting us know that LPCTSTR is in fact a char const * and showing us where is the problem.

Probably that some conservative “elitists” are going to say that gcc or cl diagnostics are fine the way they are but the way I see it, there’s always place for improvement and if a tool like clang can increase my productivity while generating fast and optimized code then… why not? I encourage everyone to at least give it a try( for C and Objective C ) and even contributing to it by improving it’s C++ support.