本文介绍了更改对不推荐使用的方法的引用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的代码库中有一些不推荐使用的方法,并且我知道该如何替换它们,有什么方法可以自动执行此操作吗?我正在使用Visual Studio 2015 Update 3,但可以使用其他文本编辑器...

I have some methods that are deprecated in my codebase and I know how I'm supposed to replace them, is there any way to do this automatically? I'm using Visual studio 2015 update 3 but I'm open to using other text editors...

代码看起来像这样:

// Deprecated method
myFunction(char* firstParam, char* secondParam = NULL);

// New method, same name, different params
myFunction(char* firstParam, bool flag, char* secondParam = NULL);

我只想要一些可以用对第二个函数的引用替换所有对第一个函数的引用。
即:

I just want something that could replace all references to the first function with a reference to the second one.i.e:

myFunction( "hello", "world");
// Replace with
myFunction( "hello", true, "world");

myFunction("hello");
// Replace with
myFunction("hello", true);

myFunction("hello", isTrue); // isTrue is a bool here
// Do not replace with anything

myFunction("hello", world); //world is a char* here
// Replace with
myFunction("hello", true, world);

我愿意使用Visual Studio或其他文本编辑器来解决方案。我不手动进行操作的原因是因为代码库太大。

I'm open to solutions using visual studio or even other text editors. The reason I'm not doing it manually is because codebase is too large.

推荐答案

您可以使用我们的,使用。

You can do this with our DMS Software Reengineering Toolkit, using DMS's source-to-source transformations.

DMS将(C ++ 17 / VisualStudio2015)源代码解析为AST,应用源到源转换来修改树,然后将AST的结果打印出来以重新生成(修改)源代码。这样就可以可靠地自动在大型代码库中进行代码更改。

DMS parses (C++17/VisualStudio2015) source code to an AST, applies the source-to-source transforms which modify the tree, and the the result AST is prettyprinted to regenerate (modified) source code. This allows one to automate code changes across large code bases in a reliable way.

OP示例的DMS重写规则如下:

DMS rewrite rules for OP's examples would look like this:

rule add_true_to_hello_world()
    :functioncall->functioncall
    = "myFunction( \"hello\", \"world\");"
    -> "myFunction( \"hello\", true, \"world\");";

rule add_true_to_call_with_literal_string(s: STRING)
    : functioncall->functioncall
    = "myFunction(\s)"
    -> "myFunction(\s, true);"

rule add_true_when_char_star(i:IDENTIFIER,s:STRING, a:argument):
     :functioncall->functioncall
     = "\i(\s,\a);"
     -> "\i(\s, true, \a)"
     if IsCharStart(a);

ruleset replace_deprecated_calls =
    { add_true_to_hello_world,
      add_true_to_call_with_literal_string,
      add_true_when_char_star
    }

Breif解释:规则采用以下形式

Breif explanation: rules have the form

rule name(metavariable): syntaxclass->syntaxclass
  lefthandside -> righthandside  if condition ;

规则有名称,以便人们和规则集可以方便地引用它们;有时,可能会有成千上万条规则来执行非常复杂的转换。规则的参数指定规则内允许使用哪种类型的元变量((v)。 functioncall-> functioncall符号表示我们正在将函数调用转换为函数调用,而不是其他形式。 C ++文本周围的引号是引号,用于将D ++规则文本中的C ++文本分隔开,导致我们需要使用\来转义实际的C ++文字字符串引号。[是的,我们可能设计了DMS来制作

Rules have names so people and rulesets can refer to them conveniently; sometimes one might have thousands of rules to carry out very complex transforms. A rule has parameters specifying what kind of metavariables (written \v) are allowed inside the rule. The "functioncall->functioncall" notation means we are transforming function calls into function calls and not something else. Quote marks around C++ text are metaquotes delimiting C++ text from DMS rule text, causing us to need to escape the actual C++ literal string quote marks with \". [Yeah we might have designed DMS to make that case non-escaped; can't always be clever enough].

一个规则集只是将规则分组到一起,它们都可以作为一个组来应用。 DMS调用以应用规则集。

A ruleset just groups rules to they can all be applied as a group. Not shown is the trivial DMS call to apply the ruleset.

您可以在上面的链接中详细了解规则语法。

You can read more about rule syntax at the link above.

I他的示例实现了与OP所表达的不同的规则,他的示例仅显示了一个函数更改为语句(在示例中请注意;),但是他在文本中写道,他希望替换所有函数调用。关于函数调用而不是语句的更改OP的第一条规则我的编码与他在示例中所显示的完全相同;仅当函数调用具有字面意义上的那些参数字符串literas时,该规则才有效。文字字符串,而不仅仅是世界。我推广的第三条规则允许使用任意函数名称,并按他的指示添加了类型检查。

I implemented his rules differently than OP expressed. His examples show only a function call as a statement (note the ";" in his examples) getting changed, but he writes in the text that he wants to replace all function calls. Thus these rules are about changes to function calls, not statements. OP's first rule I coded exactly as he showed in his example; it will only work if the function call has literally those argument string literas. His second rule I generalized to allow an arbitrary literal string rather than just "world". The third rule I generalized to allow an arbitrary function name and added a type check as he indicated.

请注意,模式匹配实际上发生在语法树而非原始文本上。 DMS不会被注释中的函数调用或具有不同的whitespacing / formats欺骗。

Note the pattern matches actually occur over syntax trees and not raw text. DMS won't be fooled by function calls inside comments or with different whitespacing/formatting.

这篇关于更改对不推荐使用的方法的引用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:32