xaizek / for-postfix (License: GPLv2+) (since 2018-12-07)
Clang-based standalone tool that detects for-loops with postfix operators.
Commit f2bd84a633c0a7dfbda7f33e3c9596191f4f8bc1

Add matcher for overloaded operators
Author: xaizek
Author date (UTC): 2014-03-24 19:19
Committer name: xaizek
Committer date (UTC): 2014-03-24 19:19
Parent(s): 5204d56b9f0c584364d4fec343960e04f718d34f
Signing key:
Tree: db60fe4a8edcedfc4559e7987fb33da14f72d0cd
File Lines added Lines deleted
for-postfix.cpp 26 2
File for-postfix.cpp changed (mode: 100644) (index 0681922..e024d91)
... ... static StatementMatcher builtinMatcher =
23 23 ) )
24 24 ); );
25 25
26 static StatementMatcher opMatcher =
27 forStmt( // for ([init]; [condition]; [increment])
28 hasIncrement( // "increment" part of for-loop
29 operatorCallExpr( // call of overloaded operator
30 argumentCountIs(2) // that requires two arguments
31 ).bind("op") // bind matched unary op to "op" name
32 )
33 );
34
26 35 class MatchHelper : public MatchFinder::MatchCallback class MatchHelper : public MatchFinder::MatchCallback
27 36 { {
28 37 public: public:
 
... ... public:
31 40 using namespace clang; using namespace clang;
32 41
33 42 typedef UnaryOperator UnOp; typedef UnaryOperator UnOp;
43 typedef CXXOperatorCallExpr Call;
34 44
35 45 if (const UnOp *op = result.Nodes.getNodeAs<UnOp>("op")) { if (const UnOp *op = result.Nodes.getNodeAs<UnOp>("op")) {
36 46 if (op->isIncrementDecrementOp() && op->isPostfix()) { if (op->isIncrementDecrementOp() && op->isPostfix()) {
37 op->dump();
38 std::cout << '\n';
47 printOut(result, op);
48 }
49 } else if (const Call *op = result.Nodes.getNodeAs<Call>("op")) {
50 const OverloadedOperatorKind opKind = op->getOperator();
51
52 if (opKind == OO_PlusPlus || opKind == OO_MinusMinus) {
53 printOut(result, op);
39 54 } }
40 55 } }
41 56 } }
57
58 private:
59 void printOut(const MatchFinder::MatchResult &result,
60 const clang::Expr *expr) const
61 {
62 expr->dump();
63 std::cout << '\n';
64 }
42 65 }; };
43 66
44 67 int int
 
... ... main(int argc, const char *argv[])
52 75
53 76 MatchFinder finder; MatchFinder finder;
54 77 finder.addMatcher(builtinMatcher, &helper); finder.addMatcher(builtinMatcher, &helper);
78 finder.addMatcher(opMatcher, &helper);
55 79
56 80 return tool.run(newFrontendActionFactory(&finder)); return tool.run(newFrontendActionFactory(&finder));
57 81 } }
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/for-postfix

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/for-postfix

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master