summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHyrum Wright <hwright@google.com>2019-01-16 14:49:32 +0000
committerHyrum Wright <hwright@google.com>2019-01-16 14:49:32 +0000
commit3dc0be8e8667f0fab94ba9c92756dda6e4b76df2 (patch)
tree13b9bb7f332074591931057afc09d50536b45892
parente7175c58ff090050d3bebf447e48eba41e5e2aef (diff)
[clang-tidy] Move the macro helper function to a common location; NFC
This is useful for multiple checks.
-rw-r--r--clang-tools-extra/clang-tidy/abseil/DurationComparisonCheck.cpp24
-rw-r--r--clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp17
-rw-r--r--clang-tools-extra/clang-tidy/abseil/DurationRewriter.h5
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 963da3f3991..adf0f6d87dd 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 ed648897a3a..ca44ab5b133 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 d0004d1c09c..e4204131471 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;