Skip to content

Commit ce1f6de

Browse files
committed
WIP: (squash) Improved error handling
Cleanup Danger, this commit will be sqashed, do not rely on it
1 parent 6ed1d54 commit ce1f6de

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ std::optional<int64_t> decodeVersion(llvm::StringRef Encoded) {
7676
const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
7777
const llvm::StringLiteral ApplyTweakCommand = "clangd.applyTweak";
7878
const llvm::StringLiteral ApplyRenameCommand = "clangd.applyRename";
79-
constexpr llvm::StringLiteral SearchASTCommand = "clangd.searchAST";
79+
constexpr llvm::StringLiteral SearchASTMethod = "textDocument/searchAST";
8080

8181
CodeAction toCodeAction(const ClangdServer::CodeActionResult::Rename &R,
8282
const URIForFile &File) {
@@ -853,14 +853,13 @@ void ClangdLSPServer::onCommandApplyRename(const RenameParams &R,
853853
});
854854
}
855855

856-
void ClangdLSPServer::onCommandSearchAST(const SearchASTArgs &Args,
857-
Callback<llvm::json::Value> Reply) {
856+
void ClangdLSPServer::onMethodSearchAST(const SearchASTArgs &Args,
857+
Callback<llvm::json::Value> Reply) {
858858
Server->findAST(Args, [Reply = std::move(Reply)](
859859
llvm::Expected<std::vector<std::vector<ASTNode>>>
860860
BoundNodes) mutable {
861861
if (!BoundNodes)
862862
return Reply(BoundNodes.takeError());
863-
auto v = llvm::json::Value(*BoundNodes);
864863
return Reply(*BoundNodes);
865864
});
866865
}
@@ -1741,7 +1740,7 @@ void ClangdLSPServer::bindMethods(LSPBinder &Bind,
17411740
Bind.command(ApplyFixCommand, this, &ClangdLSPServer::onCommandApplyEdit);
17421741
Bind.command(ApplyTweakCommand, this, &ClangdLSPServer::onCommandApplyTweak);
17431742
Bind.command(ApplyRenameCommand, this, &ClangdLSPServer::onCommandApplyRename);
1744-
Bind.command(SearchASTCommand, this, &ClangdLSPServer::onCommandSearchAST);
1743+
Bind.method(SearchASTMethod, this, &ClangdLSPServer::onMethodSearchAST);
17451744

17461745
ApplyWorkspaceEdit = Bind.outgoingMethod("workspace/applyEdit");
17471746
PublishDiagnostics = Bind.outgoingNotification("textDocument/publishDiagnostics");

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks,
186186
void onCommandApplyEdit(const WorkspaceEdit &, Callback<llvm::json::Value>);
187187
void onCommandApplyTweak(const TweakArgs &, Callback<llvm::json::Value>);
188188
void onCommandApplyRename(const RenameParams &, Callback<llvm::json::Value>);
189-
void onCommandSearchAST(const SearchASTArgs &, Callback<llvm::json::Value>);
189+
void onMethodSearchAST(const SearchASTArgs &, Callback<llvm::json::Value>);
190190

191191
/// Outgoing LSP calls.
192192
LSPBinder::OutgoingMethod<ApplyWorkspaceEditParams,

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,16 @@ void ClangdServer::findAST(SearchASTArgs const &Args,
817817
if (!InpAST)
818818
return CB(InpAST.takeError());
819819
auto BoundNodes = clangd::locateASTQuery(InpAST->AST, Args);
820-
if (BoundNodes.empty())
820+
if (!BoundNodes)
821+
return CB(BoundNodes.takeError());
822+
if (BoundNodes->empty())
821823
return CB(error("No matching AST nodes found"));
822824

823825
auto &&AST = InpAST->AST;
824826
// Convert BoundNodes to a vector of vectors to ASTNode's.
825827
std::vector<std::vector<ASTNode>> Result;
826-
Result.reserve(BoundNodes.size());
827-
for (auto &&BN : BoundNodes) {
828+
Result.reserve(BoundNodes->size());
829+
for (auto &&BN : *BoundNodes) {
828830
auto &&Map = BN.getMap();
829831
std::vector<ASTNode> Nodes;
830832
Nodes.reserve(Map.size());

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "llvm/Support/ErrorHandling.h"
7474
#include "llvm/Support/Path.h"
7575
#include "llvm/Support/raw_ostream.h"
76+
#include <algorithm>
7677
#include <cmath>
7778
#include <optional>
7879
#include <string>
@@ -782,57 +783,54 @@ const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
782783
}
783784

784785
auto locateASTQuery(ParsedAST &AST, SearchASTArgs const &Query)
785-
-> std::vector<ast_matchers::BoundNodes> {
786+
-> llvm::Expected<std::vector<ast_matchers::BoundNodes>> {
786787
using namespace ast_matchers;
787788
using namespace ast_matchers::dynamic;
788789
using ast_matchers::dynamic::Parser;
789790

790791
Diagnostics Diag;
791792
auto MatcherSource = llvm::StringRef(Query.searchQuery).ltrim();
792-
auto OrigMatcherSource = MatcherSource;
793793

794794
std::optional<DynTypedMatcher> Matcher = Parser::parseMatcherExpression(
795795
MatcherSource,
796796
nullptr /* is this sema instance usefull, to reduce overhead?*/,
797797
nullptr /*we currently don't support let*/, &Diag);
798798
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());
801800
}
802-
auto ActualSource = OrigMatcherSource.slice(0, OrigMatcherSource.size() -
803-
MatcherSource.size());
804-
auto *Q = new query::MatchQuery(ActualSource, *Matcher);
805-
Q->RemainingContent = MatcherSource;
806801

807-
// Q->run(AST);:
808-
//==
809802

810803
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) {}
813807
void run(const MatchFinder::MatchResult &Result) override {
814-
Bindings.push_back(Result.Nodes);
808+
Bindings->push_back(Result.Nodes);
815809
}
816810
};
817811

818-
MatchFinder Finder;
819-
std::vector<BoundNodes> Matches;
820812
DynTypedMatcher MaybeBoundMatcher = *Matcher;
821813
if (Query.BindRoot) {
822814
std::optional<DynTypedMatcher> M = Matcher->tryBind("root");
823815
if (M)
824816
MaybeBoundMatcher = *M;
825817
}
818+
std::vector<BoundNodes> Matches;
826819
CollectBoundNodes Collect(Matches);
820+
821+
MatchFinder::MatchFinderOptions Opt;
822+
Opt.IgnoreSystemHeaders = true;
823+
MatchFinder Finder{Opt};
827824
if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
828-
log("Not a valid top-level matcher.\n");
829-
return {/* TODO */};
825+
return error("Can't add matcher.");
830826
}
831827

832828
ASTContext &Ctx = AST.getASTContext();
829+
830+
auto OldTK = Ctx.getParentMapContext().getTraversalKind();
833831
Ctx.getParentMapContext().setTraversalKind(Query.Tk);
834832
Finder.matchAST(Ctx);
835-
833+
Ctx.getParentMapContext().setTraversalKind(OldTK);
836834
return Matches;
837835
}
838836

clang-tools-extra/clangd/XRefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct LocatedAST {
4040
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const LocatedAST &);
4141

4242
auto locateASTQuery(ParsedAST &AST, SearchASTArgs const &)
43-
-> std::vector<ast_matchers::BoundNodes>;
43+
-> llvm::Expected<std::vector<ast_matchers::BoundNodes>>;
4444

4545
// Describes where a symbol is declared and defined (as far as clangd knows).
4646
// There are three cases:

0 commit comments

Comments
 (0)