14 #include "llvm/ADT/IntervalMap.h"
15 #include "llvm/ADT/PointerUnion.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/Support/LSP/Logging.h"
18 #include "llvm/Support/LSP/Protocol.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/TableGen/Parser.h"
21 #include "llvm/TableGen/Record.h"
26 using llvm::RecordKeeper;
27 using llvm::RecordVal;
28 using llvm::SourceMgr;
39 static llvm::lsp::URIForFile
41 const llvm::lsp::URIForFile &mainFileURI) {
42 int bufferId = mgr.FindBufferContainingLoc(loc);
43 if (bufferId == 0 || bufferId ==
static_cast<int>(mgr.getMainFileID()))
46 llvm::lsp::URIForFile::fromFile(
47 mgr.getBufferInfo(bufferId).Buffer->getBufferIdentifier());
50 llvm::lsp::Logger::error(
"Failed to create URI for include file: {0}",
56 static llvm::lsp::Location
58 const llvm::lsp::URIForFile &uri) {
59 return llvm::lsp::Location(
getURIFromLoc(mgr, loc.Start, uri),
60 llvm::lsp::Range(mgr, loc));
62 static llvm::lsp::Location
64 const llvm::lsp::URIForFile &uri) {
69 static std::optional<llvm::lsp::Diagnostic>
71 const llvm::lsp::URIForFile &uri) {
72 auto *sourceMgr =
const_cast<SourceMgr *
>(
diag.getSourceMgr());
73 if (!sourceMgr || !
diag.getLoc().isValid())
76 llvm::lsp::Diagnostic lspDiag;
77 lspDiag.source =
"tablegen";
78 lspDiag.category =
"Parse Error";
82 lspDiag.range = loc.range;
89 switch (
diag.getKind()) {
90 case SourceMgr::DK_Warning:
91 lspDiag.severity = llvm::lsp::DiagnosticSeverity::Warning;
93 case SourceMgr::DK_Error:
96 case SourceMgr::DK_Note:
100 case SourceMgr::DK_Remark:
101 lspDiag.severity = llvm::lsp::DiagnosticSeverity::Information;
104 lspDiag.message =
diag.getMessage().str();
111 static std::pair<const Record *, const RecordVal *>
113 if (value->isTemplateArg())
114 return {
nullptr,
nullptr};
118 StringRef valueName = value->getName();
119 auto findValueInSupers = [&](
const Record *&record) ->
const RecordVal * {
120 for (
const Record *parentRecord : record->getSuperClasses()) {
121 if (
auto *newBase = parentRecord->getValue(valueName)) {
122 record = parentRecord;
130 std::pair<const Record *, const RecordVal *> baseValue = {};
131 while (
const RecordVal *newBase = findValueInSupers(record))
132 baseValue = {record, newBase};
136 if (!baseValue.second || baseValue.second->getLoc() == value->getLoc())
137 return {
nullptr,
nullptr};
149 struct TableGenIndexSymbol {
150 TableGenIndexSymbol(
const Record *record)
151 : definition(record),
153 TableGenIndexSymbol(
const RecordVal *value)
155 virtual ~TableGenIndexSymbol() =
default;
167 struct TableGenRecordSymbol :
public TableGenIndexSymbol {
168 TableGenRecordSymbol(
const Record *record) : TableGenIndexSymbol(record) {}
169 ~TableGenRecordSymbol()
override =
default;
171 static bool classof(
const TableGenIndexSymbol *symbol) {
172 return isa<const Record *>(symbol->definition);
176 const Record *getValue()
const {
return cast<const Record *>(definition); }
179 struct TableGenRecordValSymbol :
public TableGenIndexSymbol {
180 TableGenRecordValSymbol(
const Record *record,
const RecordVal *value)
181 : TableGenIndexSymbol(value), record(record) {}
182 ~TableGenRecordValSymbol()
override =
default;
184 static bool classof(
const TableGenIndexSymbol *symbol) {
185 return isa<const RecordVal *>(symbol->definition);
189 const RecordVal *getValue()
const {
190 return cast<const RecordVal *>(definition);
194 const Record *record;
200 class TableGenIndex {
202 TableGenIndex() : intervalMap(allocator) {}
205 void initialize(
const RecordKeeper &records);
210 const TableGenIndexSymbol *lookup(SMLoc loc,
211 SMRange *overlappedRange =
nullptr)
const;
216 using MapT = llvm::IntervalMap<
217 const char *,
const TableGenIndexSymbol *,
218 llvm::IntervalMapImpl::NodeSizer<
const char *,
219 const TableGenIndexSymbol *>::LeafSize,
220 llvm::IntervalMapHalfOpenInfo<const char *>>;
223 TableGenIndexSymbol *getOrInsertDef(
const Record *record) {
224 auto it = defToSymbol.try_emplace(record,
nullptr);
226 it.first->second = std::make_unique<TableGenRecordSymbol>(record);
227 return &*it.first->second;
230 TableGenIndexSymbol *getOrInsertDef(
const Record *record,
231 const RecordVal *value) {
232 auto it = defToSymbol.try_emplace(value,
nullptr);
235 std::make_unique<TableGenRecordValSymbol>(record, value);
237 return &*it.first->second;
241 MapT::Allocator allocator;
252 void TableGenIndex::initialize(
const RecordKeeper &records) {
256 auto insertRef = [&](TableGenIndexSymbol *sym, SMRange refLoc,
257 bool isDef =
false) {
258 const char *startLoc = refLoc.Start.getPointer();
259 const char *endLoc = refLoc.End.getPointer();
263 if (startLoc == endLoc) {
265 startLoc = refLoc.Start.getPointer();
266 endLoc = refLoc.End.getPointer();
270 if (startLoc == endLoc)
278 if (!intervalMap.overlaps(startLoc, endLoc))
279 intervalMap.insert(startLoc, endLoc, sym);
282 sym->references.push_back(refLoc);
285 llvm::make_pointee_range(llvm::make_second_range(records.getClasses()));
287 llvm::make_pointee_range(llvm::make_second_range(records.getDefs()));
288 for (
const Record &def : llvm::concat<Record>(classes, defs)) {
289 auto *sym = getOrInsertDef(&def);
290 insertRef(sym, sym->defLoc,
true);
293 for (SMLoc loc : def.getLoc().drop_front())
295 for (SMRange loc : def.getReferenceLocs())
299 for (
const RecordVal &value : def.getValues()) {
300 auto *sym = getOrInsertDef(&def, &value);
301 insertRef(sym, sym->defLoc,
true);
302 for (SMRange refLoc : value.getReferenceLocs())
303 insertRef(sym, refLoc);
308 const TableGenIndexSymbol *
309 TableGenIndex::lookup(SMLoc loc, SMRange *overlappedRange)
const {
310 auto it = intervalMap.find(loc.getPointer());
311 if (!it.valid() || loc.getPointer() < it.start())
314 if (overlappedRange) {
315 *overlappedRange = SMRange(SMLoc::getFromPointer(it.start()),
316 SMLoc::getFromPointer(it.stop()));
327 class TableGenTextFile {
329 TableGenTextFile(
const llvm::lsp::URIForFile &uri, StringRef fileContents,
331 const std::vector<std::string> &extraIncludeDirs,
332 std::vector<llvm::lsp::Diagnostic> &diagnostics);
335 int64_t getVersion()
const {
return version; }
340 update(
const llvm::lsp::URIForFile &uri, int64_t newVersion,
342 std::vector<llvm::lsp::Diagnostic> &diagnostics);
348 void getLocationsOf(
const llvm::lsp::URIForFile &uri,
349 const llvm::lsp::Position &defPos,
350 std::vector<llvm::lsp::Location> &locations);
351 void findReferencesOf(
const llvm::lsp::URIForFile &uri,
352 const llvm::lsp::Position &pos,
353 std::vector<llvm::lsp::Location> &references);
359 void getDocumentLinks(
const llvm::lsp::URIForFile &uri,
360 std::vector<llvm::lsp::DocumentLink> &links);
366 std::optional<llvm::lsp::Hover>
367 findHover(
const llvm::lsp::URIForFile &uri,
368 const llvm::lsp::Position &hoverPos);
369 llvm::lsp::Hover buildHoverForRecord(
const Record *record,
370 const SMRange &hoverRange);
371 llvm::lsp::Hover buildHoverForTemplateArg(
const Record *record,
372 const RecordVal *value,
373 const SMRange &hoverRange);
374 llvm::lsp::Hover buildHoverForField(
const Record *record,
375 const RecordVal *value,
376 const SMRange &hoverRange);
380 void initialize(
const llvm::lsp::URIForFile &uri, int64_t newVersion,
381 std::vector<llvm::lsp::Diagnostic> &diagnostics);
384 std::string contents;
390 std::vector<std::string> includeDirs;
396 std::unique_ptr<RecordKeeper> recordKeeper;
406 TableGenTextFile::TableGenTextFile(
407 const llvm::lsp::URIForFile &uri, StringRef fileContents, int64_t version,
408 const std::vector<std::string> &extraIncludeDirs,
409 std::vector<llvm::lsp::Diagnostic> &diagnostics)
410 : contents(fileContents.str()), version(version) {
413 llvm::sys::path::remove_filename(uriDirectory);
414 includeDirs.push_back(uriDirectory.str().str());
415 llvm::append_range(includeDirs, extraIncludeDirs);
418 initialize(uri, version, diagnostics);
421 LogicalResult TableGenTextFile::update(
422 const llvm::lsp::URIForFile &uri, int64_t newVersion,
424 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
425 if (
failed(llvm::lsp::TextDocumentContentChangeEvent::applyTo(changes,
427 llvm::lsp::Logger::error(
"Failed to update contents of {0}", uri.file());
432 initialize(uri, newVersion, diagnostics);
436 void TableGenTextFile::initialize(
437 const llvm::lsp::URIForFile &uri, int64_t newVersion,
438 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
439 version = newVersion;
440 sourceMgr = SourceMgr();
441 recordKeeper = std::make_unique<RecordKeeper>();
444 auto memBuffer = llvm::MemoryBuffer::getMemBuffer(contents, uri.file());
446 llvm::lsp::Logger::error(
"Failed to create memory buffer for file",
450 sourceMgr.setIncludeDirs(includeDirs);
451 sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
455 struct DiagHandlerContext {
456 std::vector<llvm::lsp::Diagnostic> &diagnostics;
457 const llvm::lsp::URIForFile &uri;
458 } handlerContext{diagnostics, uri};
461 sourceMgr.setDiagHandler(
462 [](
const llvm::SMDiagnostic &
diag,
void *rawHandlerContext) {
463 auto *ctx =
reinterpret_cast<DiagHandlerContext *
>(rawHandlerContext);
465 ctx->diagnostics.push_back(*lspDiag);
468 bool failedToParse = llvm::TableGenParseFile(sourceMgr, *recordKeeper);
476 index.initialize(*recordKeeper);
483 void TableGenTextFile::getLocationsOf(
484 const llvm::lsp::URIForFile &uri,
const llvm::lsp::Position &defPos,
485 std::vector<llvm::lsp::Location> &locations) {
486 SMLoc posLoc = defPos.getAsSMLoc(sourceMgr);
487 const TableGenIndexSymbol *symbol = index.lookup(posLoc);
494 auto *valSym = dyn_cast<TableGenRecordValSymbol>(symbol);
496 if (
auto *val =
getBaseValue(valSym->record, valSym->getValue()).second) {
505 void TableGenTextFile::findReferencesOf(
506 const llvm::lsp::URIForFile &uri,
const llvm::lsp::Position &pos,
507 std::vector<llvm::lsp::Location> &references) {
508 SMLoc posLoc = pos.getAsSMLoc(sourceMgr);
509 const TableGenIndexSymbol *symbol = index.lookup(posLoc);
514 for (SMRange refLoc : symbol->references)
522 void TableGenTextFile::getDocumentLinks(
523 const llvm::lsp::URIForFile &uri,
524 std::vector<llvm::lsp::DocumentLink> &links) {
526 links.emplace_back(include.range, include.uri);
533 std::optional<llvm::lsp::Hover>
534 TableGenTextFile::findHover(
const llvm::lsp::URIForFile &uri,
535 const llvm::lsp::Position &hoverPos) {
538 if (include.range.contains(hoverPos))
539 return include.buildHover();
543 SMLoc posLoc = hoverPos.getAsSMLoc(sourceMgr);
544 const TableGenIndexSymbol *symbol = index.lookup(posLoc, &hoverRange);
549 if (
auto *record = dyn_cast<TableGenRecordSymbol>(symbol))
550 return buildHoverForRecord(record->getValue(), hoverRange);
554 auto *recordVal = cast<TableGenRecordValSymbol>(symbol);
555 const RecordVal *value = recordVal->getValue();
556 if (value->isTemplateArg())
557 return buildHoverForTemplateArg(recordVal->record, value, hoverRange);
558 return buildHoverForField(recordVal->record, value, hoverRange);
562 TableGenTextFile::buildHoverForRecord(
const Record *record,
563 const SMRange &hoverRange) {
564 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
566 llvm::raw_string_ostream hoverOS(hover.contents.value);
569 if (record->isClass()) {
570 hoverOS <<
"**class** `" << record->getName() <<
"`";
571 }
else if (record->isAnonymous()) {
572 hoverOS <<
"**anonymous class**";
574 hoverOS <<
"**def** `" << record->getName() <<
"`";
576 hoverOS <<
"\n***\n";
580 auto printAndFormatField = [&](StringRef fieldName) {
583 const RecordVal *value = record->getValue(fieldName);
584 if (!value || !value->getValue())
586 auto *stringValue = dyn_cast<llvm::StringInit>(value->getValue());
591 ros.printReindented(stringValue->getValue().rtrim(
" \t"));
592 hoverOS <<
"\n***\n";
594 printAndFormatField(
"summary");
595 printAndFormatField(
"description");
598 if (std::optional<std::string> doc =
600 hoverOS <<
"\n" << *doc <<
"\n";
606 llvm::lsp::Hover TableGenTextFile::buildHoverForTemplateArg(
607 const Record *record,
const RecordVal *value,
const SMRange &hoverRange) {
608 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
610 llvm::raw_string_ostream hoverOS(hover.contents.value);
611 StringRef name = value->getName().rsplit(
':').second;
613 hoverOS <<
"**template arg** `" << name <<
"`\n***\nType: `";
614 value->getType()->print(hoverOS);
620 llvm::lsp::Hover TableGenTextFile::buildHoverForField(
621 const Record *record,
const RecordVal *value,
const SMRange &hoverRange) {
622 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
624 llvm::raw_string_ostream hoverOS(hover.contents.value);
625 hoverOS <<
"**field** `" << value->getName() <<
"`\n***\nType: `";
626 value->getType()->print(hoverOS);
627 hoverOS <<
"`\n***\n";
630 if (std::optional<std::string> doc =
632 hoverOS <<
"\n" << *doc <<
"\n";
633 hoverOS <<
"\n***\n";
638 auto [baseRecord, baseValue] =
getBaseValue(record, value);
640 if (std::optional<std::string> doc =
642 hoverOS <<
"\n *From `" << baseRecord->getName() <<
"`*:\n\n"
666 llvm::StringMap<std::unique_ptr<TableGenTextFile>>
files;
679 std::vector<Diagnostic> &diagnostics) {
681 std::vector<std::string> additionalIncludeDirs =
impl->options.extraDirs;
682 const auto &fileInfo =
impl->compilationDatabase.getFileInfo(uri.file());
683 llvm::append_range(additionalIncludeDirs, fileInfo.includeDirs);
685 impl->files[uri.file()] = std::make_unique<TableGenTextFile>(
686 uri, contents, version, additionalIncludeDirs, diagnostics);
691 int64_t version, std::vector<Diagnostic> &diagnostics) {
693 auto it =
impl->files.find(uri.file());
694 if (it ==
impl->files.end())
699 if (
failed(it->second->update(uri, version, changes, diagnostics)))
700 impl->files.erase(it);
703 std::optional<int64_t>
705 auto it =
impl->files.find(uri.file());
706 if (it ==
impl->files.end())
709 int64_t version = it->second->getVersion();
710 impl->files.erase(it);
715 const Position &defPos,
716 std::vector<Location> &locations) {
717 auto fileIt =
impl->files.find(uri.file());
718 if (fileIt !=
impl->files.end())
719 fileIt->second->getLocationsOf(uri, defPos, locations);
724 std::vector<Location> &references) {
725 auto fileIt =
impl->files.find(uri.file());
726 if (fileIt !=
impl->files.end())
727 fileIt->second->findReferencesOf(uri, pos, references);
731 const URIForFile &uri, std::vector<DocumentLink> &documentLinks) {
732 auto fileIt =
impl->files.find(uri.file());
733 if (fileIt !=
impl->files.end())
734 return fileIt->second->getDocumentLinks(uri, documentLinks);
737 std::optional<llvm::lsp::Hover>
739 const Position &hoverPos) {
740 auto fileIt =
impl->files.find(uri.file());
741 if (fileIt !=
impl->files.end())
742 return fileIt->second->findHover(uri, hoverPos);
static std::string toString(bytecode::Section::ID sectionID)
Stringify the given section ID.
static bool contains(SMRange range, SMLoc loc)
Returns true if the given range contains the given source location.
static std::string diag(const llvm::Value &value)
static llvm::ManagedStatic< PassManagerOptions > options
static std::pair< const Record *, const RecordVal * > getBaseValue(const Record *record, const RecordVal *value)
Get the base definition of the given record value, or nullptr if one couldn't be found.
static llvm::lsp::URIForFile getURIFromLoc(const SourceMgr &mgr, SMLoc loc, const llvm::lsp::URIForFile &mainFileURI)
Returns a language server uri for the given source location.
static llvm::lsp::Location getLocationFromLoc(SourceMgr &mgr, SMRange loc, const llvm::lsp::URIForFile &uri)
Returns a language server location from the given source range.
static std::optional< llvm::lsp::Diagnostic > getLspDiagnoticFromDiag(const llvm::SMDiagnostic &diag, const llvm::lsp::URIForFile &uri)
Convert the given TableGen diagnostic to the LSP form.
static SMRange convertTokenLocToRange(SMLoc loc)
Returns the range of a lexical token given a SMLoc corresponding to the start of an token location.
This class contains a collection of compilation information for files provided to the language server...
std::optional< int64_t > removeDocument(const URIForFile &uri)
Remove the document with the given uri.
void updateDocument(const URIForFile &uri, ArrayRef< TextDocumentContentChangeEvent > changes, int64_t version, std::vector< Diagnostic > &diagnostics)
Update the document, with the provided version, at the given URI.
std::optional< Hover > findHover(const URIForFile &uri, const Position &hoverPos)
Find a hover description for the given hover position, or std::nullopt if one couldn't be found.
void getDocumentLinks(const URIForFile &uri, std::vector< DocumentLink > &documentLinks)
Return the document links referenced by the given file.
void addDocument(const URIForFile &uri, StringRef contents, int64_t version, std::vector< Diagnostic > &diagnostics)
Add the document, with the provided version, at the given URI.
void getLocationsOf(const URIForFile &uri, const Position &defPos, std::vector< Location > &locations)
Return the locations of the object pointed at by the given position.
void findReferencesOf(const URIForFile &uri, const Position &pos, std::vector< Location > &references)
Find all references of the object pointed at by the given position.
raw_ostream subclass that simplifies indention a sequence of code.
void gatherIncludeFiles(llvm::SourceMgr &sourceMgr, SmallVectorImpl< SourceMgrInclude > &includes)
Given a source manager, gather all of the processed include files.
std::optional< std::string > extractSourceDocComment(llvm::SourceMgr &sourceMgr, SMLoc loc)
Extract a documentation comment for the given location within the source manager.
SMRange convertTokenLocToRange(SMLoc loc, StringRef identifierChars="")
Returns the range of a lexical token given a SMLoc corresponding to the start of an token location.
Include the generated interface declarations.
Impl(const Options &options)
lsp::CompilationDatabase compilationDatabase
The compilation database containing additional information for files passed to the server.
const Options & options
TableGen LSP options.
llvm::StringMap< std::unique_ptr< TableGenTextFile > > files
The files held by the server, mapped by their URI file name.
This class represents a single include within a root file.