12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Tooling/FixIt.h" 26 if (!E->getBeginLoc().isMacroID())
29 SourceLocation
Loc = E->getBeginLoc();
32 while (Result.SourceManager->isMacroArgExpansion(Loc)) {
37 Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
39 return !Loc.isMacroID();
42 void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) {
44 binaryOperator(anyOf(hasOperatorName(
">"), hasOperatorName(
">="),
45 hasOperatorName(
"=="), hasOperatorName(
"<="),
46 hasOperatorName(
"<")),
47 hasEitherOperand(ignoringImpCasts(callExpr(
48 callee(functionDecl(DurationConversionFunction())
49 .bind(
"function_decl"))))))
52 Finder->addMatcher(Matcher,
this);
55 void DurationComparisonCheck::check(
const MatchFinder::MatchResult &
Result) {
56 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>(
"binop");
59 Result.Nodes.getNodeAs<FunctionDecl>(
"function_decl")->getName());
70 std::string LhsReplacement =
72 std::string RhsReplacement =
75 diag(Binop->getBeginLoc(),
"perform comparison in the duration domain")
76 << FixItHint::CreateReplacement(Binop->getSourceRange(),
77 (llvm::Twine(LhsReplacement) +
" " +
78 Binop->getOpcodeStr() +
" " +
SourceLocation Loc
'#' location in the include directive
std::string rewriteExprFromNumberToDuration(const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
Assuming Node has type double or int representing a time interval of Scale, return the expression to ...
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
llvm::Optional< DurationScale > getScaleForInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool IsValidMacro(const MatchFinder::MatchResult &Result, const Expr *E)
Return true if E is a either: not a macro at all; or an argument to one.