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

Match and dump increment operator
Author: xaizek
Author date (UTC): 2014-03-23 21:55
Committer name: xaizek
Committer date (UTC): 2014-03-23 21:55
Parent(s): 86d601ce9e7334a2295f0194d87124628e0cb38f
Signing key:
Tree: 86f9f22c7aa9e948bbceef934fdc3fcb1969d74f
File Lines added Lines deleted
for-postfix.cpp 30 0
File for-postfix.cpp changed (mode: 100644) (index e3496df..2eb68ca)
1 #include <iostream>
2
1 3 #include <llvm/Support/CommandLine.h> #include <llvm/Support/CommandLine.h>
2 4
3 5 #include <clang/ASTMatchers/ASTMatchFinder.h> #include <clang/ASTMatchers/ASTMatchFinder.h>
 
... ... static llvm::cl::OptionCategory toolCategory("for-postfix options");
12 14
13 15 static llvm::cl::extrahelp commonHelp(CommonOptionsParser::HelpMessage); static llvm::cl::extrahelp commonHelp(CommonOptionsParser::HelpMessage);
14 16
17 static StatementMatcher incMatcher =
18 forStmt( // for ([init]; [condition]; [increment])
19 hasIncrement( // "increment" part of for-loop
20 unaryOperator( // any unary op, e.g. *, &, --
21 hasOperatorName("++") // exact unary op: ++
22 ).bind("op") // bind matched unary op to "op" name
23 )
24 );
25
26 class MatchHelper : public MatchFinder::MatchCallback
27 {
28 public:
29 virtual void run(const MatchFinder::MatchResult &result)
30 {
31 using namespace clang;
32
33 typedef UnaryOperator UnOp;
34
35 if (const UnOp *op = result.Nodes.getNodeAs<UnOp>("op")) {
36 op->dump();
37 std::cout << '\n';
38 }
39 }
40 };
41
15 42 int int
16 43 main(int argc, const char *argv[]) main(int argc, const char *argv[])
17 44 { {
 
... ... main(int argc, const char *argv[])
19 46 ClangTool tool(optionsParser.getCompilations(), ClangTool tool(optionsParser.getCompilations(),
20 47 optionsParser.getSourcePathList()); optionsParser.getSourcePathList());
21 48
49 MatchHelper helper;
50
22 51 MatchFinder finder; MatchFinder finder;
52 finder.addMatcher(incMatcher, &helper);
23 53
24 54 return tool.run(newFrontendActionFactory(&finder)); return tool.run(newFrontendActionFactory(&finder));
25 55 } }
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