67 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "DurationFactoryFloatCheck.h"10#include "DurationRewriter.h"11#include "clang/AST/ASTContext.h"12#include "clang/ASTMatchers/ASTMatchFinder.h"13#include "clang/Lex/Lexer.h"14#include <optional>15 16using namespace clang::ast_matchers;17 18namespace clang::tidy::abseil {19 20// Returns `true` if `Range` is inside a macro definition.21static bool insideMacroDefinition(const MatchFinder::MatchResult &Result,22 SourceRange Range) {23 return !clang::Lexer::makeFileCharRange(24 clang::CharSourceRange::getCharRange(Range),25 *Result.SourceManager, Result.Context->getLangOpts())26 .isValid();27}28 29void DurationFactoryFloatCheck::registerMatchers(MatchFinder *Finder) {30 Finder->addMatcher(31 callExpr(callee(functionDecl(durationFactoryFunction())),32 hasArgument(0, anyOf(cxxStaticCastExpr(hasDestinationType(33 realFloatingPointType())),34 cStyleCastExpr(hasDestinationType(35 realFloatingPointType())),36 cxxFunctionalCastExpr(hasDestinationType(37 realFloatingPointType())),38 floatLiteral())))39 .bind("call"),40 this);41}42 43void DurationFactoryFloatCheck::check(const MatchFinder::MatchResult &Result) {44 const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("call");45 46 // Don't try and replace things inside of macro definitions.47 if (insideMacroDefinition(Result, MatchedCall->getSourceRange()))48 return;49 50 const Expr *Arg = MatchedCall->getArg(0)->IgnoreImpCasts();51 // Arguments which are macros are ignored.52 if (Arg->getBeginLoc().isMacroID())53 return;54 55 std::optional<std::string> SimpleArg = stripFloatCast(Result, *Arg);56 if (!SimpleArg)57 SimpleArg = stripFloatLiteralFraction(Result, *Arg);58 59 if (SimpleArg) {60 diag(MatchedCall->getBeginLoc(), "use the integer version of absl::%0")61 << MatchedCall->getDirectCallee()->getName()62 << FixItHint::CreateReplacement(Arg->getSourceRange(), *SimpleArg);63 }64}65 66} // namespace clang::tidy::abseil67