#!/usr/bin/env bash

# goescape determines the variables which are escaping to the heap for a given
# Go source file. Courtesy of Nathan VanBenschoten.

set -eu

if [ $# -eq 0 ]
then
        echo "usage: goescape <filename>"
        exit 1
fi

FILEPATH="$1"
DIR=$(dirname "$FILEPATH")
FILE=$(basename "$FILEPATH")

pushd "$DIR" > /dev/null
touch "$FILE"

go build -gcflags='-m' 2>&1 | \
        grep 'to heap' | \
        grep "./$FILE"

popd > /dev/null
