Saturday, 6 August 2011

Clang now builds Postgres without additional warnings

I'm happy to report that as of this evening, Clang builds PostgreSQL without any warnings, apart from a single remaining warning that also occurs when building with GCC, which is actually a bug in GNU Flex that the Flex developers don't seem to want to fix. On GCC 4.6, the warning looks like this:

In file included from gram.y:12962:0:
scan.c: In function ‘yy_try_NUL_trans’:
scan.c:16246:23: warning: unused variable ‘yyg’ [-Wunused-variable]

With Clang, however, it looks like this:

scan.c:16246:23: warning: unused variable 'yyg' [-Wunused-variable]
    struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; /* This var may be unused depending upon options. */
Note that the "^" is directly underneath the offending variable "yyg" on the terminal emulator that generated this warning.

Note also that Clang usefully gives the context of the warning, and as a result a comment is displayed that suggests that the warning is spurious.

The Clang developers finally committed a fix to remove spurious warnings that occured when building Postgres as a result of it being statically detected that there are assignments past what appears to be the end of a single element array at the end of a struct. That doesn't happen now, although only under circumstances exactly consistent with the use of a popular idiom that is seen quite a bit in the Postgres code.

In working towards removing all Clang warnings, we detected a bug; we were assigning an enum constant from one enum to a variable that was actually another type of enum, which represented a potentially dangerous misuse of an abstraction that the Postgres code uses to represent nodes. This all occurred within a nested macro. Without Clang, it probably would have taken a long time for the problem to be noticed.


  1. Can clang build GCC by the way? And glibc?

  2. @shevy: It can build gcc.

    It cannot build glibc just yet, e.g. because of

  3. Is by any chance the GCC built with CLANG any faster? or output binary is smaller?

  4. From your link, it appears that a fix for that has been proposed. Does anybody know if glibc compiles after that fix is applied?

  5. @Babak Farrokhi: No. GCC bootstraps itself, using an intermediary compiler, so there would be no speed difference, except possibly in building that intermediary compiler.