diff options
author | Sam McCall <sam.mccall@gmail.com> | 2018-11-15 15:06:11 +0000 |
---|---|---|
committer | Sam McCall <sam.mccall@gmail.com> | 2018-11-15 15:06:11 +0000 |
commit | 3be64bcb74b0f500da4eba67d2c914f4ca5f3e44 (patch) | |
tree | 8cd7af4e39aa53ecd896bfd02c2be1a7400a303f /clang-tidy/readability | |
parent | 2109f226fdb68de136fcb3c0a27c237e74f11d19 (diff) |
[clang-tidy] Update checks to play nicely with limited traversal scope added in r346847
Summary: (See D54204 for original review)
Reviewers: hokein
Subscribers: xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D54579
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@346961 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/readability')
-rw-r--r-- | clang-tidy/readability/SimplifyBooleanExprCheck.cpp | 16 | ||||
-rw-r--r-- | clang-tidy/readability/SimplifyBooleanExprCheck.h | 1 |
2 files changed, 14 insertions, 3 deletions
diff --git a/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 9420c6c3..3cb26532 100644 --- a/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -507,8 +507,16 @@ void SimplifyBooleanExprCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { ChainedConditionalAssignment); } +// This is a silly hack to let us run a RecursiveASTVisitor on the Context. +// We want to match exactly one node in the AST, doesn't matter which. +AST_MATCHER_P(Decl, matchOnce, bool *, Matched) { + if (*Matched) + return false; + return *Matched = true; +} + void SimplifyBooleanExprCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher(translationUnitDecl().bind("top"), this); + Finder->addMatcher(matchOnce(&MatchedOnce), this); matchBoolCondition(Finder, true, ConditionThenStmtId); matchBoolCondition(Finder, false, ConditionElseStmtId); @@ -556,8 +564,10 @@ void SimplifyBooleanExprCheck::check(const MatchFinder::MatchResult &Result) { else if (const auto *Compound = Result.Nodes.getNodeAs<CompoundStmt>(CompoundNotBoolId)) replaceCompoundReturnWithCondition(Result, Compound, true); - else if (const auto TU = Result.Nodes.getNodeAs<Decl>("top")) - Visitor(this, Result).TraverseDecl(const_cast<Decl*>(TU)); + else { // MatchOnce matcher + assert(MatchedOnce); + Visitor(this, Result).TraverseAST(*Result.Context); + } } void SimplifyBooleanExprCheck::issueDiag( diff --git a/clang-tidy/readability/SimplifyBooleanExprCheck.h b/clang-tidy/readability/SimplifyBooleanExprCheck.h index af47453f..14ac82de 100644 --- a/clang-tidy/readability/SimplifyBooleanExprCheck.h +++ b/clang-tidy/readability/SimplifyBooleanExprCheck.h @@ -79,6 +79,7 @@ private: SourceLocation Loc, StringRef Description, SourceRange ReplacementRange, StringRef Replacement); + bool MatchedOnce = false; const bool ChainedConditionalReturn; const bool ChainedConditionalAssignment; }; |