Being remarkable: Finding pleasure in small things
Being remarkable doesn’t always mean you have to develop something large. Sometimes remarkable is small, as this Clang example seen today on Hacker News demonstrates very nicely.
Clang is an open-source compiler front-end for C, C++ and Objective C. The project builds on the LLVM compiler back-end with the goal of replacing the GCC tool chain. Their worldview accepts that programmers can and do make mistakes. Amazing feats of Clang Error Recovery shows how they’ve woven this worldview into the compiler.
What caught my eye was how Clang recovers from unknown tokens. Instead of unhelpful error messages (like GCC), the Clang team chose to do something remarkable: they added a spell-checker to guess what you mean:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
$ cat t.c #include "inttypes.h" int64 x; $ clang t.c t.c:2:1: error: unknown type name 'int64'; did you mean 'int64_t'? int64 x; ^~~~~ int64_t $ gcc t.c t.c:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'x' |
Another example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ cat t.c #include "sys/stat.h" int foo(int x, struct stat *P) { return P->st_blocksize*2; } $ clang t.c t.c:4:13: error: no member named 'st_blocksize' in 'struct stat'; did you mean 'st_blksize'? return P->st_blocksize*2; ^~~~~~~~~~~~ st_blksize $ gcc t.c t.c: In function 'foo': t.c:4: error: 'struct stat' has no member named 'st_blocksize' |
Clang uses the assumed token for the rest of the parse, avoiding the follow-on errors you typically get from compilers. See the post on the LLVM blog for more examples of how Clang recovers from unknown tokens and other common programming errors.
Clang’s spell-checker is simple, but remarkable. I don’t recall seeing this in any compiler, interpreter, or generator I’ve used. We could’ve implemented this in the tools I’ve worked on; shame we never thought of it. Props to Clang!
I’m spreading this story as I hope you’ll find it useful and might inspire you to add spell-checking somewhere unexpected in your product. Your software will be more remarkable and you’ll have a great story that might spread.
