An Emacs Function for LLVM Assembly

I finally decided to use the power of emacs to automate a task that I perform at least a dozen times per day: converting a snippet of C source code into LLVM IR assembly. The command to do this is simple:

clang -emit-llvm -c -o - file.c | opt -S

The only annoying part about using the command was finding a spare terminal and finding the input file on the file system (and then remembering where I put that terminal when I wanted to refer to it later). Enter emacs. It took a while to navigate the emacs documentation to figure out how to write what I wanted and learn the relevant terminology. The function I came up with pipes the current buffer (or region, if it is active) to clang and opt, puts the output in a new buffer, activates the llvm major mode, and switches to the new buffer.

This has a few nice benefits over just throwing LLVM IR assembly dumps into random terminals:

I feel like this style of function is generally useful and I should probably make a few more to automate my life a bit. This function still needs a few improvements:

Back to reading the emacs documentation.