有什么好的 C++可以处理这种情况的重构工具

发布于 2024-10-12 03:08:36 字数 1360 浏览 2 评论 0原文

我有一个大型 C++ 代码库,其中包含几个我计划重写的错误日志函数,定义如下;

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

我计划将它们重写为单个函数

void LogError(ErrNo No,...);

ErrNo 在这种情况下将是一个枚举,用于从外部文件查找其余的错误详细信息。虽然我正在使用并且喜欢 Visual Assist,但它似乎不适合这种事情。我认为执行此重构的最简单方法是编写一个小程序,该程序使用搜索输出的结果来查找此函数的所有出现,例如

    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);

,然后解析和修改源代码。还有其他工具可以为我简化这项任务吗?如果查看相关问题,表明没有太多内容那里。我不介意花少量钱购买一个相当易于使用的工具,但没有时间或预算做更多的事情。

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 use tool, but don't have the time or budget for anything more than this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-10-19 03:08:36

如果您使用的是 Unix,则使用 sed 编辑所有源代码可能会处理大部分更改。您必须手动完成一些更改。我过去曾使用过这种技术。

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.

向日葵 2024-10-19 03:08:36

四处寻找能够满足我需求的轻量级的东西却一片空白,而学习 SED 虽然值得,但对于那些不能完全解决我的问题的东西来说,这将是相当大量的工作。我最终编写了自己的工具来对代码库的单独副本进行所需的重构,直到我很高兴它完全满足了我的需要。这涉及从视觉辅助查找所有引用选项获取输出,并使用它来重构代码库。我会发布代码,但就目前情况而言,它非常糟糕,并且在不同的代码库下很可能会失败。一般问题可以更好地表述为这样

  • 对于 C++ 代码库,查找每次出现的带有参数 a,b,...n 的函数 fn
  • 从源文件中删除出现的 fn
  • 将参数提取为文本变量
  • 添加 a更多变量,例如实例编号、源文件名等...
  • 在删除 fn 的位置,编写一个可以包含可用变量的格式化字符串
  • 将类似的格式化字符串附加到一个或多个外部文件(例如资源文件等)。 .)

我猜想对于已经解析源代码的人来说,上述功能很容易实现,并将将此问题的链接传递给 Whole Tomato 作为增强建议。

编辑:对于任何感兴趣的人,有一些关于的后续内容VA 论坛在这里

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文