1

I am coding a tool to parse C++ code. I don't want to end up coding half of a compiler. Even though I am aware of Clang, that is too much for my purposes.

I was hand-parsing, but have come up against one too many coding styles.

I seek a tool which will parse C++ code, with only one important factor :

It must be able to put the entire function declaration on a single line.

So, things like

unsigned
int
myclass::
myfunc
(int x;
int y)
{
}

and all of its many variations, should become

unsigned int myclass::myfunc(int x; int y)
{
}

That’s all that I need for this particular project - the ability to find each function in a file and get it's return type.

Nice if it's Linux, but I can live with Windows.

GUI would be nice, with preview being a bonus.

Batch mode would be nice, including the ability to recursively process a given directory.

But none of those nice are mandatory, only the single line for the function declaration requirement is.

  • 1
    Partially. Replace all \n by [space], CTRL+A, CTRL+K then CTRL+F to format, then replace all { by \n{ and replace all } by \n} this can be scripted to go through all files. – Franck Feb 26 '15 at 14:11
  • 1
    Community Edition also have a Web version running of a webpage so compatible with virtually any operating systems – Franck Feb 26 '15 at 14:14
  • 1
    here the main web page. Right bottom corner you can signup for 5 free basic license. all code saved with Git so there is built in source control – Franck Feb 27 '15 at 12:44
  • 2
    "I seek a tool which will parse C++ code, with only one important factor : It must be able to put the entire function declaration on a single line." This is not a parser. Maybe you are looking for a C++ parser that you can use to build a prettyprinter. But that's not what your title says. Then you say, "you need to find each function and get its return type" So what it is you want? If you actually want the return type, you'll need preprocessing and symbol tables too. This SR request is very badly written. – Ira Baxter Mar 22 '15 at 9:04
  • 1
    @Mawg: ... So what do you mean, "you want the return type"? You want just the string of characters used to declare the type, or something deeper? If the function header is "int foo(...)" then the return type is obviously "int". What is the return type for "T foo(...)"; is it "T", or the typedef behind T? How about "A:B:C:T foo(...)"? What if the return type is template invocation? A macro call? Involves a prepreprocessor conditional? – Ira Baxter Aug 8 '15 at 6:58
1

http://astyle.sourceforge.net/

aStyle - no competition. I've been using this for a while now as I tend to write functions in notepad++ on my way to work and then beautify it with this before inclusion in projects - it really does a great job in my opinion.

  • does it support my single requirement? If so, how? – Mawg Feb 27 '15 at 7:52
  • 1
    Six months and no evidence this supports OP's requirement. Downvoting as "not a high quality answer". – Ira Baxter Aug 8 '15 at 6:49
1

GNU Indent can handle this:

indent b.c

Input:

unsigned
int
myfunc
(int x;
int y)
{
}

Output:

unsigned int myfunc (int x; int y)
{
}
0

I don't want to end up coding half of a compiler

It's not that complicated for the feature you're requesting. Here is a Perl script to start you off.

The arrays keywords_nospace and keywords_space contain the words/characters at the end of the lines you want to combine. The first is for combining without spaces, and the second appends spaces.

use strict;
use warnings;

open(my $in, "<", $ARGV[0]) or die "Can't open file: $!";
open(my $out, ">", "restructured_$ARGV[0]");

my $word;
my @keywords_nospace = ("::","myfunc");
my @keywords_space = ("unsigned","int",";");

while (<$in>) {
   for $word (@keywords_nospace) {chomp if (/\Q$word\E$/i);} # removes trailing spaces and newline
   for $word (@keywords_space)
     {
       if (/\Q$word\E$/i)
     {
       chomp; # Removes trailing spaces and newline
       $_ = "$_ "; # Add a space at the end of the line
     }
     }
  print $out $_;
}

You can add as many keywords as you like. Save in a .pl file, and run in the command line using:

perl script_name.pl input.cpp

This should create a new file in the directory called restructured_input.cpp. I've tested it on your example function and got the desired output.

  • 2
    Just because this "works" on his specific example doesn't make this general. You can't "reformat" an arbitrary piece of C++ code without having a full C++ parser. Macros, preprocessor conditionals, types, template specifications (esp. ">>"), lambda declarations, nesting, you name it are all going to complicate what is on the "function header line" far beyond what this script (or any script that doesn't essentially parse) can do. – Ira Baxter Aug 8 '15 at 6:48
  • @IraBaxter It's not intended to be general. I understand that actual programs are more complex than that, but the OP asked for a very specific requirement and provided a very simple example. The answer matches those, and it only serves the purpose to show how it can be done for such simple cases. If the OP needs more requirements, it's up to him to specify them in the question as per the rules of this site. – Tymric Aug 8 '15 at 14:36
  • He said I seek a tool which will parse C++ code. I think it reasonable to assume he wanted to process arbitrary C++ code, not just the example. – Ira Baxter Aug 8 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.