Search code examples
haskellghccompiler-warningsgenerated-code

How can I suppress warnings in generated code?


I'm building a lexer and parser with Alex and Happy. The code they generate throws a huge number of warnings with ghc-options: -Wall turned on in my project's .cabal file.

This makes it difficult to catch real warnings. How can I turn off the warnings only inside the generated files? I know it can be done with a pragma:

{#- GHC_OPTIONS -w -#}

But I can't think of an easy way to stick this pragma at the top of every generated file, every time they're rebuilt.

What's the right way to do this?


Solution

  • A typical alex file begins with some stuff at the top - usually a module declaration which will get copied verbatim into the generated file:

    {
    module Main where
    }
    %wrapper "basic"
    ...
    

    So just add the GHC_OPTIONS pragma before the module Main ... line, e.g.:

    {
    {-# GHC_OPTIONS -w #-}
    module Main where
    }
    %wrapper "basic"
    

    and it will be present in your generated file. The same can be done with happy files.