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 |
} |
} |