Search code examples
gcccompiler-errorscompiler-construction

How does the compiler guess the correct name when one mistypes it?


At times, gcc yields the following error message:

error: 'class X' has no member named 'Y'; did you mean 'Z'?

I have seen gcc correctly guess Z when Y contains some simple typo, e.g. wrong lower/upper case, but also when there are some missing/extra character(s) in the name.

I was curious to know

  • how does gcc correctly guess Z starting from Y?
  • if it applies a fixed set of rules, what kind of jamming is it able to handle and what falls beyond its grasp?

I would welcome answers relating other compilers too, if they perform something ostensibly different or interesting.


Solution

  • Well after a quick search it seems that GCC has an internal code to handle spellcheck which includes an implementation of Levenstein distance.

    see https://gcc.gnu.org/ml/gcc-patches/2015-09/msg01090.html and https://gcc.gnu.org/ml/gcc-patches/2015-11/msg00046.html