|
73 | 73 | #include "llvm/Support/ErrorHandling.h" |
74 | 74 | #include "llvm/Support/Path.h" |
75 | 75 | #include "llvm/Support/raw_ostream.h" |
| 76 | +#include <algorithm> |
76 | 77 | #include <cmath> |
77 | 78 | #include <optional> |
78 | 79 | #include <string> |
@@ -782,57 +783,54 @@ const syntax::Token *findNearbyIdentifier(const SpelledWord &Word, |
782 | 783 | } |
783 | 784 |
|
784 | 785 | auto locateASTQuery(ParsedAST &AST, SearchASTArgs const &Query) |
785 | | - -> std::vector<ast_matchers::BoundNodes> { |
| 786 | + -> llvm::Expected<std::vector<ast_matchers::BoundNodes>> { |
786 | 787 | using namespace ast_matchers; |
787 | 788 | using namespace ast_matchers::dynamic; |
788 | 789 | using ast_matchers::dynamic::Parser; |
789 | 790 |
|
790 | 791 | Diagnostics Diag; |
791 | 792 | auto MatcherSource = llvm::StringRef(Query.searchQuery).ltrim(); |
792 | | - auto OrigMatcherSource = MatcherSource; |
793 | 793 |
|
794 | 794 | std::optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression( |
795 | 795 | MatcherSource, |
796 | 796 | nullptr /* is this sema instance usefull, to reduce overhead?*/, |
797 | 797 | nullptr /*we currently don't support let*/, &Diag); |
798 | 798 | if (!Matcher) { |
799 | | - elog("Not a valid top-level matcher.\n"); |
800 | | - return {/* TODO */}; |
| 799 | + return error("Not a valid top-level matcher: {}.", Diag.toString()); |
801 | 800 | } |
802 | | - auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() - |
803 | | - MatcherSource.size()); |
804 | | - auto *Q = new query::MatchQuery(ActualSource, *Matcher); |
805 | | - Q->RemainingContent = MatcherSource; |
806 | 801 |
|
807 | | - // Q->run(AST);: |
808 | | - //== |
809 | 802 |
|
810 | 803 | struct CollectBoundNodes : MatchFinder::MatchCallback { |
811 | | - std::vector<BoundNodes> &Bindings; |
812 | | - CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {} |
| 804 | + std::vector<BoundNodes> *Bindings; |
| 805 | + CollectBoundNodes(std::vector<BoundNodes> &Bindings) |
| 806 | + : Bindings(&Bindings) {} |
813 | 807 | void run(const MatchFinder::MatchResult &Result) override { |
814 | | - Bindings.push_back(Result.Nodes); |
| 808 | + Bindings->push_back(Result.Nodes); |
815 | 809 | } |
816 | 810 | }; |
817 | 811 |
|
818 | | - MatchFinder Finder; |
819 | | - std::vector<BoundNodes> Matches; |
820 | 812 | DynTypedMatcher MaybeBoundMatcher = *Matcher; |
821 | 813 | if (Query.BindRoot) { |
822 | 814 | std::optional<DynTypedMatcher> M = Matcher->tryBind("root"); |
823 | 815 | if (M) |
824 | 816 | MaybeBoundMatcher = *M; |
825 | 817 | } |
| 818 | + std::vector<BoundNodes> Matches; |
826 | 819 | CollectBoundNodes Collect(Matches); |
| 820 | + |
| 821 | + MatchFinder::MatchFinderOptions Opt; |
| 822 | + Opt.IgnoreSystemHeaders = true; |
| 823 | + MatchFinder Finder{Opt}; |
827 | 824 | if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) { |
828 | | - log("Not a valid top-level matcher.\n"); |
829 | | - return {/* TODO */}; |
| 825 | + return error("Can't add matcher."); |
830 | 826 | } |
831 | 827 |
|
832 | 828 | ASTContext &Ctx = AST.getASTContext(); |
| 829 | + |
| 830 | + auto OldTK = Ctx.getParentMapContext().getTraversalKind(); |
833 | 831 | Ctx.getParentMapContext().setTraversalKind(Query.Tk); |
834 | 832 | Finder.matchAST(Ctx); |
835 | | - |
| 833 | + Ctx.getParentMapContext().setTraversalKind(OldTK); |
836 | 834 | return Matches; |
837 | 835 | } |
838 | 836 |
|
|
0 commit comments