[clang-tidy] Move the macro helper function to a common location; NFC

This is useful for multiple checks.

llvm-svn: 351348
This commit is contained in:
Hyrum Wright 2019-01-16 14:49:32 +00:00
parent 148569f7a6
commit 56de30fd4b
3 changed files with 24 additions and 22 deletions

View File

@ -19,26 +19,6 @@ namespace clang {
namespace tidy {
namespace abseil {
/// Return `true` if `E` 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 *E) {
if (!E->getBeginLoc().isMacroID())
return true;
SourceLocation Loc = E->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(">="),
@ -64,8 +44,8 @@ void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) {
// 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.
if (!IsValidMacro(Result, Binop->getLHS()) ||
!IsValidMacro(Result, Binop->getRHS()))
if (!isNotInMacro(Result, Binop->getLHS()) ||
!isNotInMacro(Result, Binop->getRHS()))
return;
std::string LhsReplacement =
rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());

View File

@ -216,6 +216,23 @@ std::string rewriteExprFromNumberToDuration(
.str();
}
bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
if (!E->getBeginLoc().isMacroID())
return true;
SourceLocation Loc = E->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();
}
} // namespace abseil
} // namespace tidy
} // namespace clang

View File

@ -75,6 +75,11 @@ std::string rewriteExprFromNumberToDuration(
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
const Expr *Node);
/// Return `true` if `E` is a either: not a macro at all; or an argument to
/// one. In the both cases, we often want to do the transformation.
bool isNotInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
const Expr *E);
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
DurationConversionFunction) {
using namespace clang::ast_matchers;