diff --git a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h --- a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h +++ b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h @@ -33,6 +33,11 @@ Optional extractSourceDocComment(llvm::SourceMgr &sourceMgr, SMLoc loc); +/// Returns true if the given range contains the given source location. Note +/// that this has different behavior than SMRange because it is inclusive of the +/// end location. +bool contains(SMRange range, SMLoc loc); + //===----------------------------------------------------------------------===// // SourceMgrInclude //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp --- a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp +++ b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp @@ -109,6 +109,11 @@ return llvm::join(llvm::reverse(commentLines), "\n"); } +bool lsp::contains(SMRange range, SMLoc loc) { + return range.Start.getPointer() <= loc.getPointer() && + loc.getPointer() <= range.End.getPointer(); +} + //===----------------------------------------------------------------------===// // SourceMgrInclude //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp --- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp +++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp @@ -62,14 +62,6 @@ return lsp::Location(getURIFromLoc(mgr, range, uri), lsp::Range(mgr, range)); } -/// Returns true if the given range contains the given source location. Note -/// that this has different behavior than SMRange because it is inclusive of the -/// end location. -static bool contains(SMRange range, SMLoc loc) { - return range.Start.getPointer() <= loc.getPointer() && - loc.getPointer() <= range.End.getPointer(); -} - /// Convert the given MLIR diagnostic to the LSP form. static Optional getLspDiagnoticFromDiag(llvm::SourceMgr &sourceMgr, const ast::Diagnostic &diag, @@ -1188,7 +1180,8 @@ SMRange loc = node->getLoc(); // Check that the location of this node is within the input range. - if (!contains(rangeLoc, loc.Start) && !contains(rangeLoc, loc.End)) + if (!lsp::contains(rangeLoc, loc.Start) && + !lsp::contains(rangeLoc, loc.End)) return; // Handle hints for various types of nodes. diff --git a/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp b/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp --- a/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp +++ b/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp @@ -471,6 +471,17 @@ if (!symbol) return; + // If this symbol is a record value and the def position is already the def of + // the symbol, check to see if the value has a base definition. This allows + // for a "go-to-def" on a "let" to resolve the definition in the base class. + auto *valSym = dyn_cast(symbol); + if (valSym && lsp::contains(valSym->defLoc, posLoc)) { + if (auto *val = getBaseValue(valSym->record, valSym->getValue()).second) { + locations.push_back(getLocationFromLoc(sourceMgr, val->getLoc(), uri)); + return; + } + } + locations.push_back(getLocationFromLoc(sourceMgr, symbol->defLoc, uri)); } diff --git a/mlir/test/tblgen-lsp-server/definition.test b/mlir/test/tblgen-lsp-server/definition.test --- a/mlir/test/tblgen-lsp-server/definition.test +++ b/mlir/test/tblgen-lsp-server/definition.test @@ -40,11 +40,11 @@ // CHECK-NEXT: "range": { // CHECK-NEXT: "end": { // CHECK-NEXT: "character": 12, -// CHECK-NEXT: "line": 4 +// CHECK-NEXT: "line": 1 // CHECK-NEXT: }, // CHECK-NEXT: "start": { // CHECK-NEXT: "character": 6, -// CHECK-NEXT: "line": 4 +// CHECK-NEXT: "line": 1 // CHECK-NEXT: } // CHECK-NEXT: }, // CHECK-NEXT: "uri": "{{.*}}/foo.td"