11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
21 void DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
23 binaryOperator(hasOperatorName(
"+"),
24 hasEitherOperand(expr(ignoringParenImpCasts(
25 callExpr(callee(functionDecl(TimeConversionFunction())
26 .bind(
"function_decl")))
32 void DurationAdditionCheck::check(
const MatchFinder::MatchResult &Result) {
33 const BinaryOperator *Binop =
34 Result.Nodes.getNodeAs<clang::BinaryOperator>(
"binop");
35 const CallExpr *Call = Result.Nodes.getNodeAs<clang::CallExpr>(
"call");
38 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
42 Result.Nodes.getNodeAs<clang::FunctionDecl>(
"function_decl")->getName());
49 if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {
50 Hint = FixItHint::CreateReplacement(
51 Binop->getSourceRange(),
52 (llvm::Twine(TimeFactory) +
"(" +
53 tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
" + " +
57 assert(Call == Binop->getRHS()->IgnoreParenImpCasts() &&
58 "Call should be found on the RHS");
59 Hint = FixItHint::CreateReplacement(
60 Binop->getSourceRange(),
61 (llvm::Twine(TimeFactory) +
"(" +
63 " + " + tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
68 diag(Binop->getBeginLoc(),
"perform addition in the duration domain") << Hint;