Skip to content

Commit 429819f

Browse files
authored
[NFC][MLIR][TableGen] Eliminate llvm:: for common types in LSP Server (#110867)
1 parent 557f7e1 commit 429819f

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <optional>
2323

2424
using namespace mlir;
25+
using llvm::Record;
26+
using llvm::RecordKeeper;
27+
using llvm::RecordVal;
28+
using llvm::SourceMgr;
2529

2630
/// Returns the range of a lexical token given a SMLoc corresponding to the
2731
/// start of an token location. The range is computed heuristically, and
@@ -32,7 +36,7 @@ static SMRange convertTokenLocToRange(SMLoc loc) {
3236

3337
/// Returns a language server uri for the given source location. `mainFileURI`
3438
/// corresponds to the uri for the main file of the source manager.
35-
static lsp::URIForFile getURIFromLoc(const llvm::SourceMgr &mgr, SMLoc loc,
39+
static lsp::URIForFile getURIFromLoc(const SourceMgr &mgr, SMLoc loc,
3640
const lsp::URIForFile &mainFileURI) {
3741
int bufferId = mgr.FindBufferContainingLoc(loc);
3842
if (bufferId == 0 || bufferId == static_cast<int>(mgr.getMainFileID()))
@@ -47,12 +51,12 @@ static lsp::URIForFile getURIFromLoc(const llvm::SourceMgr &mgr, SMLoc loc,
4751
}
4852

4953
/// Returns a language server location from the given source range.
50-
static lsp::Location getLocationFromLoc(llvm::SourceMgr &mgr, SMRange loc,
54+
static lsp::Location getLocationFromLoc(SourceMgr &mgr, SMRange loc,
5155
const lsp::URIForFile &uri) {
5256
return lsp::Location(getURIFromLoc(mgr, loc.Start, uri),
5357
lsp::Range(mgr, loc));
5458
}
55-
static lsp::Location getLocationFromLoc(llvm::SourceMgr &mgr, SMLoc loc,
59+
static lsp::Location getLocationFromLoc(SourceMgr &mgr, SMLoc loc,
5660
const lsp::URIForFile &uri) {
5761
return getLocationFromLoc(mgr, convertTokenLocToRange(loc), uri);
5862
}
@@ -61,7 +65,7 @@ static lsp::Location getLocationFromLoc(llvm::SourceMgr &mgr, SMLoc loc,
6165
static std::optional<lsp::Diagnostic>
6266
getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag,
6367
const lsp::URIForFile &uri) {
64-
auto *sourceMgr = const_cast<llvm::SourceMgr *>(diag.getSourceMgr());
68+
auto *sourceMgr = const_cast<SourceMgr *>(diag.getSourceMgr());
6569
if (!sourceMgr || !diag.getLoc().isValid())
6670
return std::nullopt;
6771

@@ -79,17 +83,17 @@ getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag,
7983

8084
// Convert the severity for the diagnostic.
8185
switch (diag.getKind()) {
82-
case llvm::SourceMgr::DK_Warning:
86+
case SourceMgr::DK_Warning:
8387
lspDiag.severity = lsp::DiagnosticSeverity::Warning;
8488
break;
85-
case llvm::SourceMgr::DK_Error:
89+
case SourceMgr::DK_Error:
8690
lspDiag.severity = lsp::DiagnosticSeverity::Error;
8791
break;
88-
case llvm::SourceMgr::DK_Note:
92+
case SourceMgr::DK_Note:
8993
// Notes are emitted separately from the main diagnostic, so we just treat
9094
// them as remarks given that we can't determine the diagnostic to relate
9195
// them to.
92-
case llvm::SourceMgr::DK_Remark:
96+
case SourceMgr::DK_Remark:
9397
lspDiag.severity = lsp::DiagnosticSeverity::Information;
9498
break;
9599
}
@@ -100,16 +104,15 @@ getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag,
100104

101105
/// Get the base definition of the given record value, or nullptr if one
102106
/// couldn't be found.
103-
static std::pair<const llvm::Record *, const llvm::RecordVal *>
104-
getBaseValue(const llvm::Record *record, const llvm::RecordVal *value) {
107+
static std::pair<const Record *, const RecordVal *>
108+
getBaseValue(const Record *record, const RecordVal *value) {
105109
if (value->isTemplateArg())
106110
return {nullptr, nullptr};
107111

108112
// Find a base value for the field in the super classes of the given record.
109113
// On success, `record` is updated to the new parent record.
110114
StringRef valueName = value->getName();
111-
auto findValueInSupers =
112-
[&](const llvm::Record *&record) -> llvm::RecordVal * {
115+
auto findValueInSupers = [&](const Record *&record) -> RecordVal * {
113116
for (auto [parentRecord, loc] : record->getSuperClasses()) {
114117
if (auto *newBase = parentRecord->getValue(valueName)) {
115118
record = parentRecord;
@@ -120,8 +123,8 @@ getBaseValue(const llvm::Record *record, const llvm::RecordVal *value) {
120123
};
121124

122125
// Try to find the lowest definition of the record value.
123-
std::pair<const llvm::Record *, const llvm::RecordVal *> baseValue = {};
124-
while (const llvm::RecordVal *newBase = findValueInSupers(record))
126+
std::pair<const Record *, const RecordVal *> baseValue = {};
127+
while (const RecordVal *newBase = findValueInSupers(record))
125128
baseValue = {record, newBase};
126129

127130
// Check that the base isn't the same as the current value (e.g. if the value
@@ -140,15 +143,15 @@ namespace {
140143
/// contains the definition of the symbol, the location of the symbol, and any
141144
/// recorded references.
142145
struct TableGenIndexSymbol {
143-
TableGenIndexSymbol(const llvm::Record *record)
146+
TableGenIndexSymbol(const Record *record)
144147
: definition(record),
145148
defLoc(convertTokenLocToRange(record->getLoc().front())) {}
146-
TableGenIndexSymbol(const llvm::RecordVal *value)
149+
TableGenIndexSymbol(const RecordVal *value)
147150
: definition(value), defLoc(convertTokenLocToRange(value->getLoc())) {}
148151
virtual ~TableGenIndexSymbol() = default;
149152

150153
// The main definition of the symbol.
151-
PointerUnion<const llvm::Record *, const llvm::RecordVal *> definition;
154+
PointerUnion<const Record *, const RecordVal *> definition;
152155

153156
/// The source location of the definition.
154157
SMRange defLoc;
@@ -158,37 +161,33 @@ struct TableGenIndexSymbol {
158161
};
159162
/// This class represents a single record symbol.
160163
struct TableGenRecordSymbol : public TableGenIndexSymbol {
161-
TableGenRecordSymbol(const llvm::Record *record)
162-
: TableGenIndexSymbol(record) {}
164+
TableGenRecordSymbol(const Record *record) : TableGenIndexSymbol(record) {}
163165
~TableGenRecordSymbol() override = default;
164166

165167
static bool classof(const TableGenIndexSymbol *symbol) {
166-
return symbol->definition.is<const llvm::Record *>();
168+
return symbol->definition.is<const Record *>();
167169
}
168170

169171
/// Return the value of this symbol.
170-
const llvm::Record *getValue() const {
171-
return definition.get<const llvm::Record *>();
172-
}
172+
const Record *getValue() const { return definition.get<const Record *>(); }
173173
};
174174
/// This class represents a single record value symbol.
175175
struct TableGenRecordValSymbol : public TableGenIndexSymbol {
176-
TableGenRecordValSymbol(const llvm::Record *record,
177-
const llvm::RecordVal *value)
176+
TableGenRecordValSymbol(const Record *record, const RecordVal *value)
178177
: TableGenIndexSymbol(value), record(record) {}
179178
~TableGenRecordValSymbol() override = default;
180179

181180
static bool classof(const TableGenIndexSymbol *symbol) {
182-
return symbol->definition.is<const llvm::RecordVal *>();
181+
return symbol->definition.is<const RecordVal *>();
183182
}
184183

185184
/// Return the value of this symbol.
186-
const llvm::RecordVal *getValue() const {
187-
return definition.get<const llvm::RecordVal *>();
185+
const RecordVal *getValue() const {
186+
return definition.get<const RecordVal *>();
188187
}
189188

190189
/// The parent record of this symbol.
191-
const llvm::Record *record;
190+
const Record *record;
192191
};
193192

194193
/// This class provides an index for definitions/uses within a TableGen
@@ -199,7 +198,7 @@ class TableGenIndex {
199198
TableGenIndex() : intervalMap(allocator) {}
200199

201200
/// Initialize the index with the given RecordKeeper.
202-
void initialize(const llvm::RecordKeeper &records);
201+
void initialize(const RecordKeeper &records);
203202

204203
/// Lookup a symbol for the given location. Returns nullptr if no symbol could
205204
/// be found. If provided, `overlappedRange` is set to the range that the
@@ -217,15 +216,15 @@ class TableGenIndex {
217216
llvm::IntervalMapHalfOpenInfo<const char *>>;
218217

219218
/// Get or insert a symbol for the given record.
220-
TableGenIndexSymbol *getOrInsertDef(const llvm::Record *record) {
219+
TableGenIndexSymbol *getOrInsertDef(const Record *record) {
221220
auto it = defToSymbol.try_emplace(record, nullptr);
222221
if (it.second)
223222
it.first->second = std::make_unique<TableGenRecordSymbol>(record);
224223
return &*it.first->second;
225224
}
226225
/// Get or insert a symbol for the given record value.
227-
TableGenIndexSymbol *getOrInsertDef(const llvm::Record *record,
228-
const llvm::RecordVal *value) {
226+
TableGenIndexSymbol *getOrInsertDef(const Record *record,
227+
const RecordVal *value) {
229228
auto it = defToSymbol.try_emplace(value, nullptr);
230229
if (it.second) {
231230
it.first->second =
@@ -246,7 +245,7 @@ class TableGenIndex {
246245
};
247246
} // namespace
248247

249-
void TableGenIndex::initialize(const llvm::RecordKeeper &records) {
248+
void TableGenIndex::initialize(const RecordKeeper &records) {
250249
intervalMap.clear();
251250
defToSymbol.clear();
252251

@@ -282,7 +281,7 @@ void TableGenIndex::initialize(const llvm::RecordKeeper &records) {
282281
llvm::make_pointee_range(llvm::make_second_range(records.getClasses()));
283282
auto defs =
284283
llvm::make_pointee_range(llvm::make_second_range(records.getDefs()));
285-
for (const llvm::Record &def : llvm::concat<llvm::Record>(classes, defs)) {
284+
for (const Record &def : llvm::concat<Record>(classes, defs)) {
286285
auto *sym = getOrInsertDef(&def);
287286
insertRef(sym, sym->defLoc, /*isDef=*/true);
288287

@@ -293,7 +292,7 @@ void TableGenIndex::initialize(const llvm::RecordKeeper &records) {
293292
insertRef(sym, loc);
294293

295294
// Add definitions for any values.
296-
for (const llvm::RecordVal &value : def.getValues()) {
295+
for (const RecordVal &value : def.getValues()) {
297296
auto *sym = getOrInsertDef(&def, &value);
298297
insertRef(sym, sym->defLoc, /*isDef=*/true);
299298
for (SMRange refLoc : value.getReferenceLocs())
@@ -359,13 +358,12 @@ class TableGenTextFile {
359358

360359
std::optional<lsp::Hover> findHover(const lsp::URIForFile &uri,
361360
const lsp::Position &hoverPos);
362-
lsp::Hover buildHoverForRecord(const llvm::Record *record,
361+
lsp::Hover buildHoverForRecord(const Record *record,
363362
const SMRange &hoverRange);
364-
lsp::Hover buildHoverForTemplateArg(const llvm::Record *record,
365-
const llvm::RecordVal *value,
363+
lsp::Hover buildHoverForTemplateArg(const Record *record,
364+
const RecordVal *value,
366365
const SMRange &hoverRange);
367-
lsp::Hover buildHoverForField(const llvm::Record *record,
368-
const llvm::RecordVal *value,
366+
lsp::Hover buildHoverForField(const Record *record, const RecordVal *value,
369367
const SMRange &hoverRange);
370368

371369
private:
@@ -383,10 +381,10 @@ class TableGenTextFile {
383381
std::vector<std::string> includeDirs;
384382

385383
/// The source manager containing the contents of the input file.
386-
llvm::SourceMgr sourceMgr;
384+
SourceMgr sourceMgr;
387385

388386
/// The record keeper containing the parsed tablegen constructs.
389-
std::unique_ptr<llvm::RecordKeeper> recordKeeper;
387+
std::unique_ptr<RecordKeeper> recordKeeper;
390388

391389
/// The index of the parsed file.
392390
TableGenIndex index;
@@ -430,8 +428,8 @@ void TableGenTextFile::initialize(const lsp::URIForFile &uri,
430428
int64_t newVersion,
431429
std::vector<lsp::Diagnostic> &diagnostics) {
432430
version = newVersion;
433-
sourceMgr = llvm::SourceMgr();
434-
recordKeeper = std::make_unique<llvm::RecordKeeper>();
431+
sourceMgr = SourceMgr();
432+
recordKeeper = std::make_unique<RecordKeeper>();
435433

436434
// Build a buffer for this file.
437435
auto memBuffer = llvm::MemoryBuffer::getMemBuffer(contents, uri.file());
@@ -442,7 +440,7 @@ void TableGenTextFile::initialize(const lsp::URIForFile &uri,
442440
sourceMgr.setIncludeDirs(includeDirs);
443441
sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
444442

445-
// This class provides a context argument for the llvm::SourceMgr diagnostic
443+
// This class provides a context argument for the SourceMgr diagnostic
446444
// handler.
447445
struct DiagHandlerContext {
448446
std::vector<lsp::Diagnostic> &diagnostics;
@@ -543,13 +541,13 @@ TableGenTextFile::findHover(const lsp::URIForFile &uri,
543541
// Build hover for a RecordVal, which is either a template argument or a
544542
// field.
545543
auto *recordVal = cast<TableGenRecordValSymbol>(symbol);
546-
const llvm::RecordVal *value = recordVal->getValue();
544+
const RecordVal *value = recordVal->getValue();
547545
if (value->isTemplateArg())
548546
return buildHoverForTemplateArg(recordVal->record, value, hoverRange);
549547
return buildHoverForField(recordVal->record, value, hoverRange);
550548
}
551549

552-
lsp::Hover TableGenTextFile::buildHoverForRecord(const llvm::Record *record,
550+
lsp::Hover TableGenTextFile::buildHoverForRecord(const Record *record,
553551
const SMRange &hoverRange) {
554552
lsp::Hover hover(lsp::Range(sourceMgr, hoverRange));
555553
{
@@ -570,7 +568,7 @@ lsp::Hover TableGenTextFile::buildHoverForRecord(const llvm::Record *record,
570568
auto printAndFormatField = [&](StringRef fieldName) {
571569
// Check that the record actually has the given field, and that it's a
572570
// string.
573-
const llvm::RecordVal *value = record->getValue(fieldName);
571+
const RecordVal *value = record->getValue(fieldName);
574572
if (!value || !value->getValue())
575573
return;
576574
auto *stringValue = dyn_cast<llvm::StringInit>(value->getValue());
@@ -593,10 +591,8 @@ lsp::Hover TableGenTextFile::buildHoverForRecord(const llvm::Record *record,
593591
return hover;
594592
}
595593

596-
lsp::Hover
597-
TableGenTextFile::buildHoverForTemplateArg(const llvm::Record *record,
598-
const llvm::RecordVal *value,
599-
const SMRange &hoverRange) {
594+
lsp::Hover TableGenTextFile::buildHoverForTemplateArg(
595+
const Record *record, const RecordVal *value, const SMRange &hoverRange) {
600596
lsp::Hover hover(lsp::Range(sourceMgr, hoverRange));
601597
{
602598
llvm::raw_string_ostream hoverOS(hover.contents.value);
@@ -609,8 +605,8 @@ TableGenTextFile::buildHoverForTemplateArg(const llvm::Record *record,
609605
return hover;
610606
}
611607

612-
lsp::Hover TableGenTextFile::buildHoverForField(const llvm::Record *record,
613-
const llvm::RecordVal *value,
608+
lsp::Hover TableGenTextFile::buildHoverForField(const Record *record,
609+
const RecordVal *value,
614610
const SMRange &hoverRange) {
615611
lsp::Hover hover(lsp::Range(sourceMgr, hoverRange));
616612
{

0 commit comments

Comments
 (0)