[Mlir-commits] [mlir] 99e2412 - [TableGen:LSP] Resolve "go-to-def" on a let field to the base definition
River Riddle
llvmlistbot at llvm.org
Tue Sep 27 23:48:55 PDT 2022
Author: River Riddle
Date: 2022-09-27T23:48:16-07:00
New Revision: 99e24123e9578acc54b47a238ee54d591d1a64d1
URL: https://github.com/llvm/llvm-project/commit/99e24123e9578acc54b47a238ee54d591d1a64d1
DIFF: https://github.com/llvm/llvm-project/commit/99e24123e9578acc54b47a238ee54d591d1a64d1.diff
LOG: [TableGen:LSP] Resolve "go-to-def" on a let field to the base definition
This allows for go-to-def on the a `let` field to resolve to the definition
of the base class. This is kind of like how C++ works with go-to-def
from use->def->decl, with the decl in this case being the base definition
of the field.
Differential Revision: https://reviews.llvm.org/D134264
Added:
Modified:
mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp
mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h
mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp
mlir/test/tblgen-lsp-server/definition.test
Removed:
################################################################################
diff --git a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp
index 3adef2c3169f..3e029171bffd 100644
--- a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp
+++ b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.cpp
@@ -109,6 +109,11 @@ Optional<std::string> lsp::extractSourceDocComment(llvm::SourceMgr &sourceMgr,
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/lsp-server-support/SourceMgrUtils.h b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h
index c21f611592d3..69e0584501cd 100644
--- a/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h
+++ b/mlir/lib/Tools/lsp-server-support/SourceMgrUtils.h
@@ -33,6 +33,11 @@ SMRange convertTokenLocToRange(SMLoc loc);
Optional<std::string> extractSourceDocComment(llvm::SourceMgr &sourceMgr,
SMLoc loc);
+/// Returns true if the given range contains the given source location. Note
+/// that this has
diff erent behavior than SMRange because it is inclusive of the
+/// end location.
+bool contains(SMRange range, SMLoc loc);
+
//===----------------------------------------------------------------------===//
// SourceMgrInclude
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
index cb500926ac97..476bf16efd7d 100644
--- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
+++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
@@ -62,14 +62,6 @@ static lsp::Location getLocationFromLoc(llvm::SourceMgr &mgr, SMRange range,
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
diff erent 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<lsp::Diagnostic>
getLspDiagnoticFromDiag(llvm::SourceMgr &sourceMgr, const ast::Diagnostic &diag,
@@ -1188,7 +1180,8 @@ void PDLDocument::getInlayHints(const lsp::URIForFile &uri,
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
index 11dd35d6dc8e..42e321faa954 100644
--- a/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp
+++ b/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp
@@ -471,6 +471,17 @@ void TableGenTextFile::getLocationsOf(const lsp::URIForFile &uri,
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<TableGenRecordValSymbol>(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
index d2f26a224762..e05e8d619683 100644
--- 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"
More information about the Mlir-commits
mailing list