Running pgindent on non-core code or development code

From PostgreSQL wiki
Jump to navigationJump to search

Note: Copied from http://adpgtech.blogspot.com/2015/05/running-pgindent-on-non-core-code-or.html

Running pgindent is not nearly as hard as some people seem to think it is. The hardest part of getting a workable set of typedefs to use. That's why the buildfarm now constructs these lists automatically for each live branch.

But that doesn't help if you're working on non-core code. Here's what I did to get a working typedefs list for the Redis FDW code:

   objdump -W redis_fdw.so |\
    egrep -A3 DW_TAG_typedef |\
    perl -e ' while (<>) { chomp; @flds = split;next unless (1 < @flds);\
        next if $flds[0]  ne "DW_AT_name" && $flds[1] ne "DW_AT_name";\
        next if $flds[-1] =~ /^DW_FORM_str/;\
        print $flds[-1],"\n"; }'  |\
    sort | uniq > redis_fdw.typedefs


This is a slight adaptation of what the buildfarm code does on Linux to get a typedefs list.

After that, indenting the code was a matter of just doing this:

   pgindent --typedefs=redis_fdw.typedefs redis_fdw.c


What if you're developing a piece of core code and you'd like to run pgindent on it, but you've introduced some new typedefs, so pgindent mucks up the indentation by adding extraneous spaces. You have a couple of options. Let's assume that what you're working on is backend code. Then you could run the above extraction on the built backend - it doesn't have to be installed, just run it against src/backend/postgres. Then use that to run pgindent against each of the files you're working on. You don't have to run it separately for each file - you can name as many files to indent as you like on the command line.

If you do that, look at the results carefully. It's possible that the absence of some platform-dependent typedef has mucked up your file. So a safer procedure is to grab the latest typedefs list from the buildfarm server and combine it with the typedefs list you just constructed, like this:

    wget -q -O - "http://www.pgbuildfarm.org/cgi-bin/typedefs.pl?branch=HEAD" |\
     cat - mytypedefs | sort | uniq > mynewtypedefs

and then use that file to pgindent your code.

None of this is as easy as it might be. But none of it is very hard either.

Hint:

If you only have a handful of new typedefs, you can pass them on the command line to pgindent, like this:

   pgindent --typedefs=mytypedefs --list-of-typedefs="typedef1 typedef2" myfile1.c myfile2.c