Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm working since 6 years with a huge legacy software.
This SW contains thousands of old style c functions with many parameters.

Here an example with few parameters:

old_function (x,y,z)
int x;
int y;
int z;
{
  // some code
  return (0);
}


When I started at the company, I changed the code by removing the code
between the round brackets and replacing ; by ,:

old_function (int x, int y, int z)
{
  // some code
  return (0);
}



What I dind't know at that time is that the parameters can be swapped like this:

old_function (x,y,z)
int y;
int x;
int z;
{
  // some code
  return (0);
}


Here my way of correction would swap x and y, which is fatal.

I found this out later, because the program showed undefined behaviour caused
by one of my changes.

But I already changed hundreds of functions with up to 20 parameters before.

So what I need now is a parser, which parses all code without my modificiations, finds the old style function definitions and reports
swapped parameters.

Does any one know if such a tool exists? Or some c or C++ code which I can use
as a base and modify according to my needs?

Thanks and br
Christian

What I have tried:

I started doing it by hand. :-(
Posted
Comments
0x01AA 4-Feb-24 13:28pm    
Why you refactor that (I'm assuming 'old style' still works)?
k5054 4-Feb-24 18:28pm    
K&R style function definitions have been removed in C23. But the OP is probably not being forced into C23.

Your best bet is to scrap your revised version, and revert to the original. Then if you must refactor the definitions write code to do that from the original, working version instead of trying to manually check each and every function.

I'm not aware of any existing software that would correct your current version, so you would have to write it - and that's twice the work of reverting to a known good version!
 
Share this answer
 
Comments
CPallini 5-Feb-24 2:10am    
5.
There used to be a utility distributed with GCC called protoize that could assist with converting K&R function definitions to ANSI. However, it seems to have been discontinued around gcc 4, which goes back to circa 2005. There's some information about issues here : Protoize Caveats[^] If there's no show stoppers for you, you might be able to extract protoize from the source tree, and try compiling it. There's also cproto : CPROTO – Generate function prototypes[^] While the project seems to be defunct, you might be able to trace down the source code for it. As far as I know, both cproto and protoize have issues, so some care is needed when working with either.
 
Share this answer
 
Comments
CPallini 5-Feb-24 2:10am    
5.
This seems an interesting problem, and I thought why not ask ChatGPT and see how it responds to this problem. Surprisingly it has generated a code (first in Python, later in Java) that works (I just tested your two base codes by the way).

Maybe it can give you an idea of how to begin with.

Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParameterComparator {

    public static boolean compareParameterNames(String code1, String code2) {
        String pattern = "\\b(?:int|char|float|double|long|short)\\s+([a-zA-Z_][a-zA-Z0-9_]*)\\b";
        Pattern paramPattern = Pattern.compile(pattern);

        Matcher matcher1 = paramPattern.matcher(code1);
        Matcher matcher2 = paramPattern.matcher(code2);

        while (matcher1.find() && matcher2.find()) {
            String param1 = matcher1.group(1);
            String param2 = matcher2.group(1);

            if (!param1.equals(param2)) {
                return false; // Parameter names are different
            }
        }

        return true; // Parameter names are the same
    }

    public static void main(String[] args) {
        String code1 = "old_function (x,y,z)\n"+
"int x;\n"+
"int y;\n"+
"int z;\n"+
"{\n"+
"  // some code\n"+
"  return (0);\n"+
"})\n";
        String code2 = "old_function (int y, int x, int z)\n"+
"{\n"+
"  // some code\n"+
"  return (0);\n"+
"}\n";

        boolean result = compareParameterNames(code1, code2);
        System.out.println("Parameters swapped: " + !result);
    }
}



Do I need to tell how to ask ChatGPT to modify the code in such a way that first, it needs two files (source and target) to extract functions and then apply comparison. No, I don't think so!

I would still think that for a large-scale project, above approach may not work and you should still go with OG Advice but then why not do a little bit of experiment (probably a day) and learn something new!

Thanks,
Asif
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900