ghc-srcspan-plugin-0.1.0.0: Generic GHC Plugin for annotating Haskell code with source location data.

Safe HaskellNone
LanguageHaskell2010

GHC.Plugins.SrcSpan

Description

This module provides a generic Core-to-Core pass for annotating Haskell expressions with the original source locations. You can use it to build a GHC Plugin tailored to your own library by providing a predicate to select interesting expressions for annotation and a function to annotate the expressions.

Example usage:

module MyPlugin (plugin) where

import GhcPlugins
import GHC.Plugins.SrcSpan

plugin :: Plugin
plugin = defaultPlugin { installCoreToDos = install }

install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
install opts todos = do
  reinitializeGlobals
  return $ mypass : todos
  where
  mypass = CoreDoPluginPass "Add Locations" $ mkPass isInteresting annotate False
  isInteresting expr = ...
  annotate expr = ...

You will need to coax GHC into adding the source information to the Core via Ticks. Currently there are three ways to do this:

  1. Load your module in ghci.
  2. Compile your module with -prof -fprof-auto-calls. (You can use other profiling options, but that will result in poorer Tick granularity)
  3. Compile your module with -fhpc. Note that this will result in the hpc runtime being linked into your program, which is a bit inconvenient. The plugin will prevent this if you pass True instead of False to mkPass, but be warned, this will likely break any FFI code your module uses.

Synopsis

Documentation

mkPass Source

Arguments

:: (CoreExpr -> CoreM Bool)

Should we annotate this CoreExpr?

-> (SrcSpan -> CoreExpr -> CoreM CoreExpr)

Annotate the CoreExpr with the SrcSpan.

-> Bool

Should we remove the hpc hooks from the resulting binary?

-> ModGuts 
-> CoreM ModGuts 

Given a way of identifying "interesting" CoreExprs and annotating them with SrcSpans, construct a Core-to-Core pass that traverses all of the CoreBinds and annotates the interesting ones.