Author: sammccall Date: Thu Nov 15 07:06:11 2018 New Revision: 346961 URL: http://llvm.org/viewvc/llvm-project?rev=346961&view=rev Log: [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 Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertUtils.h clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.cpp clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.h Modified: clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp?rev=346961&r1=346960&r2=346961&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/misc/UnusedParametersCheck.cpp Thu Nov 15 07:06:11 2018 @@ -74,7 +74,7 @@ static FixItHint removeArgument(const Ma class UnusedParametersCheck::IndexerVisitor : public RecursiveASTVisitor<IndexerVisitor> { public: - IndexerVisitor(TranslationUnitDecl *Top) { TraverseDecl(Top); } + IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); } const std::unordered_set<const CallExpr *> & getFnCalls(const FunctionDecl *Fn) { @@ -136,8 +136,7 @@ void UnusedParametersCheck::warnOnUnused auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param; if (!Indexer) { - Indexer = llvm::make_unique<IndexerVisitor>( - Result.Context->getTranslationUnitDecl()); + Indexer = llvm::make_unique<IndexerVisitor>(*Result.Context); } // Comment out parameter name for non-local functions. Modified: clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp?rev=346961&r1=346960&r2=346961&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp Thu Nov 15 07:06:11 2018 @@ -899,7 +899,7 @@ void LoopConvertCheck::check(const Match // variable declared inside the loop outside of it. // FIXME: Determine when the external dependency isn't an expression converted // by another loop. - TUInfo->getParentFinder().gatherAncestors(Context->getTranslationUnitDecl()); + TUInfo->getParentFinder().gatherAncestors(*Context); DependencyFinderASTVisitor DependencyFinder( &TUInfo->getParentFinder().getStmtToParentStmtMap(), &TUInfo->getParentFinder().getDeclToParentStmtMap(), Modified: clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertUtils.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertUtils.h?rev=346961&r1=346960&r2=346961&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertUtils.h (original) +++ clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertUtils.h Thu Nov 15 07:06:11 2018 @@ -56,12 +56,12 @@ class StmtAncestorASTVisitor public: StmtAncestorASTVisitor() { StmtStack.push_back(nullptr); } - /// \brief Run the analysis on the TranslationUnitDecl. + /// \brief Run the analysis on the AST. /// /// In case we're running this analysis multiple times, don't repeat the work. - void gatherAncestors(const clang::TranslationUnitDecl *T) { + void gatherAncestors(ASTContext &Ctx) { if (StmtAncestors.empty()) - TraverseDecl(const_cast<clang::TranslationUnitDecl *>(T)); + TraverseAST(Ctx); } /// Accessor for StmtAncestors. Modified: clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.cpp?rev=346961&r1=346960&r2=346961&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.cpp Thu Nov 15 07:06:11 2018 @@ -507,8 +507,16 @@ void SimplifyBooleanExprCheck::storeOpti 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(con 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( Modified: clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.h?rev=346961&r1=346960&r2=346961&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/readability/SimplifyBooleanExprCheck.h Thu Nov 15 07:06:11 2018 @@ -79,6 +79,7 @@ private: SourceLocation Loc, StringRef Description, SourceRange ReplacementRange, StringRef Replacement); + bool MatchedOnce = false; const bool ChainedConditionalReturn; const bool ChainedConditionalAssignment; }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits