ned14 / pcpp

A C99 preprocessor written in pure Python
Other
220 stars 41 forks source link

How to preserver whitespace? #10

Closed GrantEdwards closed 6 years ago

GrantEdwards commented 6 years ago

Is there any way to get pcpp to preserve whitespace?

It appears to collapse all whitespace within each line. This unfortunately renders it useless for my application. I need to take source code, run it through pcpp, to expand a few selected macros, evaluate some #if #endif sections and end up with something that is till readable.

GrantEdwards commented 6 years ago

It looks like I've figured out where to hobble the Preprocessor write() method so it will preserve whitespace. What's not obvious is how to make in configurable from the API or from the command line pcpp tool...

GrantEdwards commented 6 years ago

Just figured that out too. Will be submitting a pull request...

GrantEdwards commented 6 years ago

Pull request #12 adds an option to preserve whitespace in output lines.

ned14 commented 6 years ago

Does not --line-directive (nothing after it) do what you want?

GrantEdwards commented 6 years ago

No. For me, --line-directive (nothing after it) throws an exception:

$ cat foo.c

struct 
{
  int a;
  int b;
  int c;
} s[] =
  {
    {     1,  1234,     0},
    {432643,    34,    12},
    {    19,     4,  2345},
  };

// The commas should all be aligned vertically in the data table
// above.

$ pcpp --passthru-comments --line-directive foo.c

Traceback (most recent call last):
  File "/home/grante/.local/bin/pcpp", line 11, in <module>
    load_entry_point('pcpp==1.1.0', 'console_scripts', 'pcpp')()
  File "/home/grante/.local/lib64/python2.7/site-packages/pcpp-1.1.0-py2.7.egg/pcpp/cmd.py", line 195, in main
    p = CmdPreprocessor(sys.argv)
  File "/home/grante/.local/lib64/python2.7/site-packages/pcpp-1.1.0-py2.7.egg/pcpp/cmd.py", line 50, in __init__
    args = argp.parse_known_args(argv[1:])
  File "/usr/lib64/python2.7/argparse.py", line 1733, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib64/python2.7/argparse.py", line 1942, in _parse_known_args
    stop_index = consume_positionals(start_index)
  File "/usr/lib64/python2.7/argparse.py", line 1898, in consume_positionals
    take_action(action, args)
  File "/usr/lib64/python2.7/argparse.py", line 1807, in take_action
    action(self, namespace, argument_values, option_string)
  File "/home/grante/.local/lib64/python2.7/site-packages/pcpp-1.1.0-py2.7.egg/pcpp/cmd.py", line 20, in __call__
    items += [argparse.FileType('rt')(value) for value in values]
  File "/usr/lib64/python2.7/argparse.py", line 1139, in __call__
    return open(string, self._mode, self._bufsize)
TypeError: coercing to Unicode: need string or buffer, file found

Setting line-directive to the empty string also does not work:

$ pcpp --passthru-comments --line-directive= foo.c

struct
{
  int a;
  int b;
  int c;
} s[] =
  {
    { 1, 1234, 0},
    {432643, 34, 12},
    { 19, 4, 2345},
  };

// The commas should all be aligned vertically in the data table
// above.
GrantEdwards commented 6 years ago

The test above was done with a snapshot of pcpp-master taken about 20 minutes ago.

ned14 commented 6 years ago

Would you have a sample of the problematic preprocessing for me to test with?

GrantEdwards commented 6 years ago

Here's the foo.c file from the example shown above:

foo.c.zip

GrantEdwards commented 6 years ago

Here's how my --preserve-witespace option currently works. Unfortunately, it's now inserting additional blank lines in the output. :/

$ cat foo.c
struct 
{
  int a;
  int b;
  int c;
} s[] =
  {
    {     1,  1234,     0},
    {432643,    34,    12},
    {    19,     4,  2345},
  };

// The commas should all be aligned vertically in the data table
// above.
$ 
$ 
$ 
$ pcpp --preserve-whitespace --passthru-comments foo.c
struct
{
  int a;
  int b;
  int c;
} s[] =
  {
    {     1,  1234,     0},
    {432643,    34,    12},
    {    19,     4,  2345},
  };

// The commas should all be aligned vertically in the data table
// above.
ned14 commented 6 years ago

Oh I hadn't realised that you meant macro unexpanded output. My apologies. I'll look into it.

ned14 commented 6 years ago

Try the above commit and let me know how you get on.

ned14 commented 6 years ago

Closing due to lack of response.

GrantEdwards commented 5 years ago

Brilliant!

It looks like it's doing exactly what I wanted.

Thanks much. Sorry it took so long for me to respond. The project where I was using pcpp got put on hold.