[PATCH] D27210: [clang-tidy] misc-string-compare. Adding a new check to clang-tidy

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 1 09:00:16 PST 2016


On 1 December 2016 at 16:42, Mads Ravn <madsravn at gmail.com> wrote:
> I have now implemented your suggestions - all but the fixit one. If I have
> added bindings for str1 and str2 in ast matcher, how would I go about
> creating a replacement for the entire implicitCastExpr or binaryOperator? I
> can't find any example in the code for clang-tidy to suggest how I would
> build a new node for the AST. Or I am looking at it from a wrong direction?

You create insertions, removals and replacements that affect the source code.
The AST is not changed.

Fix-it hints are documented here:
http://clang.llvm.org/docs/InternalsManual.html#fix-it-hints

Source locations and ranges are documented here:
http://clang.llvm.org/docs/InternalsManual.html#sourcelocation

To turn str1.compare(str2) into str1 == str2 you need to replace
".compare(" with " == " and remove ")".

You can get the SourceLocation of the . by calling getOperatorLoc() on
the MemberExpr.
You can tell if you have a . or an -> by calling isArrow() on the MemberExpr.
You can get the SourceLocation of the ) by calling getRParenLoc() on
the CXXMemberCallExpr.

-- 
Malcolm Parsons


More information about the cfe-commits mailing list