Index: clang-tidy/abseil/DurationComparisonCheck.cpp =================================================================== --- clang-tidy/abseil/DurationComparisonCheck.cpp +++ clang-tidy/abseil/DurationComparisonCheck.cpp @@ -19,6 +19,26 @@ namespace tidy { namespace abseil { +/// Return `true` if `expr` is a either: not a macro at all; or an argument to +/// one. In the latter case, we should still transform it. +static bool IsValidMacro(const MatchFinder::MatchResult &Result, + const Expr *expr) { + if (!expr->getBeginLoc().isMacroID()) + return true; + + SourceLocation loc = expr->getBeginLoc(); + // We want to get closer towards the initial macro typed into the source only + // if the location is being expanded as a macro argument. + while (Result.SourceManager->isMacroArgExpansion(loc)) { + // We are calling getImmediateMacroCallerLoc, but note it is essentially + // equivalent to calling getImmediateSpellingLoc in this context according + // to Clang implementation. We are not calling getImmediateSpellingLoc + // because Clang comment says it "should not generally be used by clients." + loc = Result.SourceManager->getImmediateMacroCallerLoc(loc); + } + return !loc.isMacroID(); +} + void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) { auto Matcher = binaryOperator(anyOf(hasOperatorName(">"), hasOperatorName(">="), @@ -35,10 +55,6 @@ void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) { const auto *Binop = Result.Nodes.getNodeAs("binop"); - // Don't try to replace things inside of macro definitions. - if (Binop->getExprLoc().isMacroID()) - return; - llvm::Optional Scale = getScaleForInverse( Result.Nodes.getNodeAs("function_decl")->getName()); if (!Scale) @@ -48,19 +64,19 @@ // want to handle the case of rewriting both sides. This is much simpler if // we unconditionally try and rewrite both, and let the rewriter determine // if nothing needs to be done. - llvm::Optional LhsReplacement = + if (!IsValidMacro(Result, Binop->getLHS()) || + !IsValidMacro(Result, Binop->getRHS())) + return; + std::string LhsReplacement = rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS()); - llvm::Optional RhsReplacement = + std::string RhsReplacement = rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS()); - if (!(LhsReplacement && RhsReplacement)) - return; - diag(Binop->getBeginLoc(), "perform comparison in the duration domain") << FixItHint::CreateReplacement(Binop->getSourceRange(), - (llvm::Twine(*LhsReplacement) + " " + + (llvm::Twine(LhsReplacement) + " " + Binop->getOpcodeStr() + " " + - *RhsReplacement) + RhsReplacement) .str()); } Index: clang-tidy/abseil/DurationRewriter.h =================================================================== --- clang-tidy/abseil/DurationRewriter.h +++ clang-tidy/abseil/DurationRewriter.h @@ -65,7 +65,7 @@ /// Assuming `Node` has type `double` or `int` representing a time interval of /// `Scale`, return the expression to make it a suitable `Duration`. -llvm::Optional rewriteExprFromNumberToDuration( +std::string rewriteExprFromNumberToDuration( const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node); Index: clang-tidy/abseil/DurationRewriter.cpp =================================================================== --- clang-tidy/abseil/DurationRewriter.cpp +++ clang-tidy/abseil/DurationRewriter.cpp @@ -183,14 +183,11 @@ return ScaleIter->second; } -llvm::Optional rewriteExprFromNumberToDuration( +std::string rewriteExprFromNumberToDuration( const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node) { const Expr &RootNode = *Node->IgnoreParenImpCasts(); - if (RootNode.getBeginLoc().isMacroID()) - return llvm::None; - // First check to see if we can undo a complimentary function call. if (llvm::Optional MaybeRewrite = rewriteInverseDurationCall(Result, Scale, RootNode)) Index: clang-tidy/abseil/DurationSubtractionCheck.cpp =================================================================== --- clang-tidy/abseil/DurationSubtractionCheck.cpp +++ clang-tidy/abseil/DurationSubtractionCheck.cpp @@ -42,10 +42,8 @@ if (!Scale) return; - llvm::Optional RhsReplacement = + std::string RhsReplacement = rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS()); - if (!RhsReplacement) - return; const Expr *LhsArg = Result.Nodes.getNodeAs("lhs_arg"); @@ -54,7 +52,7 @@ Binop->getSourceRange(), (llvm::Twine("absl::") + FuncDecl->getName() + "(" + tooling::fixit::getText(*LhsArg, *Result.Context) + " - " + - *RhsReplacement + ")") + RhsReplacement + ")") .str()); } Index: test/clang-tidy/abseil-duration-comparison.cpp =================================================================== --- test/clang-tidy/abseil-duration-comparison.cpp +++ test/clang-tidy/abseil-duration-comparison.cpp @@ -127,6 +127,13 @@ // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] // CHECK-FIXES: absl::Milliseconds((y + 5) * 10) > d1; + // We should still transform the expression inside this macro invocation +#define VALUE_IF(v, e) v ? (e) : 0 + int a = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1)); + // CHECK-MESSAGES: [[@LINE-1]]:23: warning: perform comparison in the duration domain [abseil-duration-comparison] + // CHECK-FIXES: VALUE_IF(1, absl::Seconds(5) > d1); +#undef VALUE_IF + // These should not match b = 6 < 4;