Monday, April 30, 2012

Refactoring in C using cscout

Today i had to do some basic refactoring in a C project for my internship. As i'm using emacs i tried doing it with query-replace-regexp but i soon found out that i can easily mess up the code.

The first instinct was to google for an emacs plugin that does basic refactoring. There seems to be one called xrefactory but its website is down.

One of the top google results on "c refactoring" was cscout. I thought that it is related with another project that i like, namely cscope, so i gave it a try.

I was surprised that it didn't had a makefile, so i had to manually do some installing steps, but it wasn't too complicated.

This tool does more than refactoring, but in order to use it you must first create a script that describes the steps needed for parsing your workspace

You can do this in two ways:
  • If you build your project using a Makefile you can use their replacement of make, called "csmake" on you Makefile. It has the same effect as make but will also output the script you need. However i couldn't get this working.
  • You can describe your workspace, project and files in some configuration file which you compile into a script using another tool called "cswc". I used this method. Note: You need to specify all your include directories, including the the standard library ones "/usr/include". You should also add here "include" directory in the cscout distribution. Here is a basic example of such a file.
  • workspace w {
        ro_prefix "/usr/include"

        project p {
            ipath "/home/titus/src/cscout-2.8/include"
            ipath "/usr/include"
           
            cd "/home/titus/tests/"
            file hello.c
        }
    }


  • After you have this file, let's call it "workspace.ws" you compile it with "cswc < workspace.ws > workspace.cs" and you call cscout on this "cscout workspace.cs"
  • You write directly the cs file.
After you run cscout on a script file "cscout workspace.cs" it will start as web-server on localhost. The default port is 8081 but you can change it. So you see your project in your browser at "http://localhost:8081". You stop the server from the web-interface

For refactoring you go to Files -> Writeable identifiers -> (pick you identifier) -> Subsitute.
After you do this, the identifier will not be replaced immediately but after you save and exit cscout.

No comments:

Post a Comment