Is there a random C++ program generator?

2019-03-09 18:46发布

问题:

I am looking for a tool that is able to randomly generate C++ programs. I have been using the Csmith C program generator for testing a tool I am developing, but now I would like to go a step further and test with randomly generated C++ code. Note that I am not interested in existing C++ test suites (I already use some).

Any help will be greatly appreciated. Thanks in advance!

回答1:

Here's another suggestion for a similar program (with source included), which is being used for compiler development. You can probably refer to it and develop something similar. link

Random C/C++ Program Generator

  • Create a set of random types to be used throughout the program
  • Create the main func.
  • Generate a random block of statements with maximum control & expression nesting depths.
  • If new functions were defined in #3, then loop back to fill in it's body.
  • When a maximum number of functions is reached, stop generating new functions and finish off the bodies of the remaining funcs.
  • Output generated program.

GOALS:

  • Locate basic errors in compiler front-ends (crashes, etc)
  • Ferret out correctness errors in optimization passes.
  • Support the design of tools to search for improved optimization paths (partial execution, etc)


回答2:

My manydl.c program demonstrates (on Linux) that a program could do a lot (at least many hundred thousands on a desktop) of dlopen-s. It works by generating some more or less random C code (in a lot of files), then compiling them and dlopen-ing them, but I designed the generator so that generated functions terminate quickly.

You probably could adapt it quite easily (e.g. to generate some C++ code).

BTW, I feel that generating more or less random C++ code is rather simple. You just need to generate some random AST, then emit it. Generating some "useful" C++ code is probably more tricky.

BTW, my MELT program could also interest you: it is a domain specific language to customize GCC, and it works by generating quite big C++ files.

PS. Is your C or C++ compiler some free software? I'll be delighted to have a look inside it....