diff options
author | Hyrum Wright <hwright@google.com> | 2019-01-16 14:49:32 +0000 |
---|---|---|
committer | Hyrum Wright <hwright@google.com> | 2019-01-16 14:49:32 +0000 |
commit | 56de30fd4b87fa44e31b396cb8a3c700b38b3d3d (patch) | |
tree | 49f51260476382a88dd7d5b85a356319a594ec48 /clang-tools-extra | |
parent | [llvm-nm] Allow --size-sort to print symbols with only Symbol size (diff) | |
download | llvm-project-56de30fd4b87fa44e31b396cb8a3c700b38b3d3d.tar.gz llvm-project-56de30fd4b87fa44e31b396cb8a3c700b38b3d3d.tar.bz2 llvm-project-56de30fd4b87fa44e31b396cb8a3c700b38b3d3d.zip |
[clang-tidy] Move the macro helper function to a common location; NFC
This is useful for multiple checks.
llvm-svn: 351348
Diffstat (limited to 'clang-tools-extra')
3 files changed, 24 insertions, 22 deletions
diff --git a/clang-tools-extra/clang-tidy/abseil/DurationComparisonCheck.cpp b/clang-tools-extra/clang-tidy/abseil/DurationComparisonCheck.cpp index 963da3f39914..adf0f6d87ddb 100644 --- a/clang-tools-extra/clang-tidy/abseil/DurationComparisonCheck.cpp +++ b/clang-tools-extra/clang-tidy/abseil/DurationComparisonCheck.cpp @@ -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()); diff --git a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp index ed648897a3aa..ca44ab5b1336 100644 --- a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp +++ b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp @@ -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 diff --git a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.h b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.h index d0004d1c09c5..e42041314713 100644 --- a/clang-tools-extra/clang-tidy/abseil/DurationRewriter.h +++ b/clang-tools-extra/clang-tidy/abseil/DurationRewriter.h @@ -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; |