开发者

Any good C++ refactoring tools that could handle this scenario

I have a large C++ code base that contains a couple of functions for error logging that I'm planning to rewrite, defined as follows;

void LogError(ErrorLevel elvl,LPCTSTR Format,...);  // Literal version
void LogError(ErrorLevel elvl,UINT ResourceID,...); // Resource version

I'm planning to rewrite these as a single function

void LogError(ErrNo No,...);

ErrNo in this case is will be an enum, used to look up the rest of the error details from an external file. While I'm using and love Visual Assist, it doesn't appear to be up to this kind of thing. I'm thinking the easiest way to carry out this refactor is to write a small program that uses the results of a search output to find all the occurences of this function, e.g.

    c:\cpp\common\Atlas\Comps\LSADJUST.cpp
        LSAFormNormalEquations (174):    LogError(elvl_Error,IDS_WINWRN0058,i+1,TravObs.setup_no,TravObs.round_no
        LSAFormNormalEquations (180):    LogError(elvl_Error,IDS_WINWRN0059,i+1,TravObs.setup_no,TravObs.round_no
        LSAFormNormalEquations (186):    LogError(elvl_Error,IDS_WINWRN0060,i+1,TravObs.setup_no,TravObs.round_no
    c:\cpp\common\Atlas\Comps\LSADJUSTZ.CPP
        LSAFormNormalEquationsZ (45):    LogError(elvl_Note,_T("Adjusting heights by least squares"));
    c:\cpp\Win32\Atlas\Section\OptmizeSectionVolumes.cpp
        OnSectionOptimizeVolumes (239):    LogError(elvl_Note,"Shifted section at chainage %0.1lf by %0.3lf",Graph.c1,Offset);

and then parse and modify the source. Are there any other tools that could simplify this task for me? If looked at a related question which suggets there isn't much out there. I don't mind spending a small amount for a reasonably easy to 开发者_如何学Pythonuse tool, but don't have the time or budget for anything more than this.


If you were using Unix, using sed to edit all your source-code might handle most of the changes. You would have to complete some of the changes by hand. I have used this technique in the past.


Searching around for something light weight that met my needs drew a blank, and learning SED while worthwhile would have been a fair amount of work for something that did not quite solve my problem. I ended up writing my own tool to carry out the refactor needed on a seperate copy of the code base until I was happy that it was doing exactly what I needed. This involved taking the output from Visual Assists find all references option, and use it to refactor the code base. I'd post the code, but as it stands is pretty awful and would be liable to fail under a different code base. The general problem can be better stated as something like this

  • For a C++ code base, find every occurence of function fn taking parameters a,b,...n
  • Remove the occurence of fn from the source file
  • Extract the parameters as text variables
  • Add a few more variables such as instance number, source file name, etc...
  • At the point where fn was removed, write a formatted string that can include available variables
  • Append similar formatted strings to one or more external files (e.g. resource files etc...)

I'm guessing the above functionality would be easy enough to implement for someone who is already parsing the source, and will pass a link to this question to Whole Tomato as an enhancement suggestion.

Edit: For anyone interested, there's some follow up on the VA forum here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜