I'm trying to do a quick rewrite of our codebase to replace all instances of new T(...)
with New<T>(...)
(a custom function), so I can do some local measurements.
I think clang-tidy
is the way to go here, so I've made a small clang tidy script, based off of this tutorial I found. The script is able to run, but not pass tests, since I do not match things correctly.
However, I'm running into problems getting the type out of the matched new expression.
What I have so far is:
class ReplaceNewAndDeleteCheck : public utils::TransformerClangTidyCheck {
public:
ReplaceNewAndDeleteCheck(StringRef Name, ClangTidyContext *Context)
: TransformerClangTidyCheck(MakeRules(), Name, Context) {}
};
Where MakeRules
is defined as follows:
RewriteRuleWith<std::string> MakeRules() {
std::string s = "new";
return makeRule(
cxxNewExpr().bind(s),
changeTo(cat("New<", node(s), ">")),
cat("")
);
}
The script is able to run, but does not give the correct result. For example, with int* x = new int(10);
, what I want is int* x = New<int>(10);
, but what I get is int* x = New<new int(10)>;
.
I know MakeRules
is wrong, since I'm binding the entire new expression, not just the on the entire but I can't figure out how to either get the type and arguments out of the new
expression, or to bind to those instead.