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/Support/VirtualFileSystem.h"
21 #include "llvm/TableGen/Parser.h"
22 #include "llvm/TableGen/Record.h"
27 using llvm::RecordKeeper;
28 using llvm::RecordVal;
29 using llvm::SourceMgr;
40 static llvm::lsp::URIForFile
42 const llvm::lsp::URIForFile &mainFileURI) {
43 int bufferId = mgr.FindBufferContainingLoc(loc);
44 if (bufferId == 0 || bufferId ==
static_cast<int>(mgr.getMainFileID()))
47 llvm::lsp::URIForFile::fromFile(
48 mgr.getBufferInfo(bufferId).Buffer->getBufferIdentifier());
51 llvm::lsp::Logger::error(
"Failed to create URI for include file: {0}",
57 static llvm::lsp::Location
59 const llvm::lsp::URIForFile &uri) {
60 return llvm::lsp::Location(
getURIFromLoc(mgr, loc.Start, uri),
61 llvm::lsp::Range(mgr, loc));
63 static llvm::lsp::Location
65 const llvm::lsp::URIForFile &uri) {
70 static std::optional<llvm::lsp::Diagnostic>
72 const llvm::lsp::URIForFile &uri) {
73 auto *sourceMgr =
const_cast<SourceMgr *
>(
diag.getSourceMgr());
74 if (!sourceMgr || !
diag.getLoc().isValid())
77 llvm::lsp::Diagnostic lspDiag;
78 lspDiag.source =
"tablegen";
79 lspDiag.category =
"Parse Error";
83 lspDiag.range = loc.range;
90 switch (
diag.getKind()) {
91 case SourceMgr::DK_Warning:
92 lspDiag.severity = llvm::lsp::DiagnosticSeverity::Warning;
94 case SourceMgr::DK_Error:
97 case SourceMgr::DK_Note:
101 case SourceMgr::DK_Remark:
102 lspDiag.severity = llvm::lsp::DiagnosticSeverity::Information;
105 lspDiag.message =
diag.getMessage().str();
112 static std::pair<const Record *, const RecordVal *>
114 if (value->isTemplateArg())
115 return {
nullptr,
nullptr};
119 StringRef valueName = value->getName();
120 auto findValueInSupers = [&](
const Record *&record) ->
const RecordVal * {
121 for (
const Record *parentRecord : record->getSuperClasses()) {
122 if (
auto *newBase = parentRecord->getValue(valueName)) {
123 record = parentRecord;
131 std::pair<const Record *, const RecordVal *> baseValue = {};
132 while (
const RecordVal *newBase = findValueInSupers(record))
133 baseValue = {record, newBase};
137 if (!baseValue.second || baseValue.second->getLoc() == value->getLoc())
138 return {
nullptr,
nullptr};
150 struct TableGenIndexSymbol {
151 TableGenIndexSymbol(
const Record *record)
152 : definition(record),
154 TableGenIndexSymbol(
const RecordVal *value)
156 virtual ~TableGenIndexSymbol() =
default;
168 struct TableGenRecordSymbol :
public TableGenIndexSymbol {
169 TableGenRecordSymbol(
const Record *record) : TableGenIndexSymbol(record) {}
170 ~TableGenRecordSymbol()
override =
default;
172 static bool classof(
const TableGenIndexSymbol *symbol) {
173 return isa<const Record *>(symbol->definition);
177 const Record *getValue()
const {
return cast<const Record *>(definition); }
180 struct TableGenRecordValSymbol :
public TableGenIndexSymbol {
181 TableGenRecordValSymbol(
const Record *record,
const RecordVal *value)
182 : TableGenIndexSymbol(value), record(record) {}
183 ~TableGenRecordValSymbol()
override =
default;
185 static bool classof(
const TableGenIndexSymbol *symbol) {
186 return isa<const RecordVal *>(symbol->definition);
190 const RecordVal *getValue()
const {
191 return cast<const RecordVal *>(definition);
195 const Record *record;
201 class TableGenIndex {
203 TableGenIndex() : intervalMap(allocator) {}
206 void initialize(
const RecordKeeper &records);
211 const TableGenIndexSymbol *lookup(SMLoc loc,
212 SMRange *overlappedRange =
nullptr)
const;
217 using MapT = llvm::IntervalMap<
218 const char *,
const TableGenIndexSymbol *,
219 llvm::IntervalMapImpl::NodeSizer<
const char *,
220 const TableGenIndexSymbol *>::LeafSize,
221 llvm::IntervalMapHalfOpenInfo<const char *>>;
224 TableGenIndexSymbol *getOrInsertDef(
const Record *record) {
225 auto it = defToSymbol.try_emplace(record,
nullptr);
227 it.first->second = std::make_unique<TableGenRecordSymbol>(record);
228 return &*it.first->second;
231 TableGenIndexSymbol *getOrInsertDef(
const Record *record,
232 const RecordVal *value) {
233 auto it = defToSymbol.try_emplace(value,
nullptr);
236 std::make_unique<TableGenRecordValSymbol>(record, value);
238 return &*it.first->second;
242 MapT::Allocator allocator;
253 void TableGenIndex::initialize(
const RecordKeeper &records) {
257 auto insertRef = [&](TableGenIndexSymbol *sym, SMRange refLoc,
258 bool isDef =
false) {
259 const char *startLoc = refLoc.Start.getPointer();
260 const char *endLoc = refLoc.End.getPointer();
264 if (startLoc == endLoc) {
266 startLoc = refLoc.Start.getPointer();
267 endLoc = refLoc.End.getPointer();
271 if (startLoc == endLoc)
279 if (!intervalMap.overlaps(startLoc, endLoc))
280 intervalMap.insert(startLoc, endLoc, sym);
283 sym->references.push_back(refLoc);
286 llvm::make_pointee_range(llvm::make_second_range(records.getClasses()));
288 llvm::make_pointee_range(llvm::make_second_range(records.getDefs()));
289 for (
const Record &def : llvm::concat<Record>(classes, defs)) {
290 auto *sym = getOrInsertDef(&def);
291 insertRef(sym, sym->defLoc,
true);
294 for (SMLoc loc : def.getLoc().drop_front())
296 for (SMRange loc : def.getReferenceLocs())
300 for (
const RecordVal &value : def.getValues()) {
301 auto *sym = getOrInsertDef(&def, &value);
302 insertRef(sym, sym->defLoc,
true);
303 for (SMRange refLoc : value.getReferenceLocs())
304 insertRef(sym, refLoc);
309 const TableGenIndexSymbol *
310 TableGenIndex::lookup(SMLoc loc, SMRange *overlappedRange)
const {
311 auto it = intervalMap.find(loc.getPointer());
312 if (!it.valid() || loc.getPointer() < it.start())
315 if (overlappedRange) {
316 *overlappedRange = SMRange(SMLoc::getFromPointer(it.start()),
317 SMLoc::getFromPointer(it.stop()));
328 class TableGenTextFile {
330 TableGenTextFile(
const llvm::lsp::URIForFile &uri, StringRef fileContents,
332 const std::vector<std::string> &extraIncludeDirs,
333 std::vector<llvm::lsp::Diagnostic> &diagnostics);
336 int64_t getVersion()
const {
return version; }
341 update(
const llvm::lsp::URIForFile &uri, int64_t newVersion,
343 std::vector<llvm::lsp::Diagnostic> &diagnostics);
349 void getLocationsOf(
const llvm::lsp::URIForFile &uri,
350 const llvm::lsp::Position &defPos,
351 std::vector<llvm::lsp::Location> &locations);
352 void findReferencesOf(
const llvm::lsp::URIForFile &uri,
353 const llvm::lsp::Position &pos,
354 std::vector<llvm::lsp::Location> &references);
360 void getDocumentLinks(
const llvm::lsp::URIForFile &uri,
361 std::vector<llvm::lsp::DocumentLink> &links);
367 std::optional<llvm::lsp::Hover>
368 findHover(
const llvm::lsp::URIForFile &uri,
369 const llvm::lsp::Position &hoverPos);
370 llvm::lsp::Hover buildHoverForRecord(
const Record *record,
371 const SMRange &hoverRange);
372 llvm::lsp::Hover buildHoverForTemplateArg(
const Record *record,
373 const RecordVal *value,
374 const SMRange &hoverRange);
375 llvm::lsp::Hover buildHoverForField(
const Record *record,
376 const RecordVal *value,
377 const SMRange &hoverRange);
381 void initialize(
const llvm::lsp::URIForFile &uri, int64_t newVersion,
382 std::vector<llvm::lsp::Diagnostic> &diagnostics);
385 std::string contents;
391 std::vector<std::string> includeDirs;
397 std::unique_ptr<RecordKeeper> recordKeeper;
407 TableGenTextFile::TableGenTextFile(
408 const llvm::lsp::URIForFile &uri, StringRef fileContents, int64_t version,
409 const std::vector<std::string> &extraIncludeDirs,
410 std::vector<llvm::lsp::Diagnostic> &diagnostics)
411 : contents(fileContents.str()), version(version) {
414 llvm::sys::path::remove_filename(uriDirectory);
415 includeDirs.push_back(uriDirectory.str().str());
416 llvm::append_range(includeDirs, extraIncludeDirs);
419 initialize(uri, version, diagnostics);
422 LogicalResult TableGenTextFile::update(
423 const llvm::lsp::URIForFile &uri, int64_t newVersion,
425 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
426 if (
failed(llvm::lsp::TextDocumentContentChangeEvent::applyTo(changes,
428 llvm::lsp::Logger::error(
"Failed to update contents of {0}", uri.file());
433 initialize(uri, newVersion, diagnostics);
437 void TableGenTextFile::initialize(
438 const llvm::lsp::URIForFile &uri, int64_t newVersion,
439 std::vector<llvm::lsp::Diagnostic> &diagnostics) {
440 version = newVersion;
441 sourceMgr = SourceMgr();
442 recordKeeper = std::make_unique<RecordKeeper>();
445 auto memBuffer = llvm::MemoryBuffer::getMemBuffer(contents, uri.file());
447 llvm::lsp::Logger::error(
"Failed to create memory buffer for file",
451 sourceMgr.setIncludeDirs(includeDirs);
452 sourceMgr.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
453 sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
457 struct DiagHandlerContext {
458 std::vector<llvm::lsp::Diagnostic> &diagnostics;
459 const llvm::lsp::URIForFile &uri;
460 } handlerContext{diagnostics, uri};
463 sourceMgr.setDiagHandler(
464 [](
const llvm::SMDiagnostic &
diag,
void *rawHandlerContext) {
465 auto *ctx =
reinterpret_cast<DiagHandlerContext *
>(rawHandlerContext);
467 ctx->diagnostics.push_back(*lspDiag);
470 bool failedToParse = llvm::TableGenParseFile(sourceMgr, *recordKeeper);
478 index.initialize(*recordKeeper);
485 void TableGenTextFile::getLocationsOf(
486 const llvm::lsp::URIForFile &uri,
const llvm::lsp::Position &defPos,
487 std::vector<llvm::lsp::Location> &locations) {
488 SMLoc posLoc = defPos.getAsSMLoc(sourceMgr);
489 const TableGenIndexSymbol *symbol = index.lookup(posLoc);
496 auto *valSym = dyn_cast<TableGenRecordValSymbol>(symbol);
498 if (
auto *val =
getBaseValue(valSym->record, valSym->getValue()).second) {
507 void TableGenTextFile::findReferencesOf(
508 const llvm::lsp::URIForFile &uri,
const llvm::lsp::Position &pos,
509 std::vector<llvm::lsp::Location> &references) {
510 SMLoc posLoc = pos.getAsSMLoc(sourceMgr);
511 const TableGenIndexSymbol *symbol = index.lookup(posLoc);
516 for (SMRange refLoc : symbol->references)
524 void TableGenTextFile::getDocumentLinks(
525 const llvm::lsp::URIForFile &uri,
526 std::vector<llvm::lsp::DocumentLink> &links) {
528 links.emplace_back(include.range, include.uri);
535 std::optional<llvm::lsp::Hover>
536 TableGenTextFile::findHover(
const llvm::lsp::URIForFile &uri,
537 const llvm::lsp::Position &hoverPos) {
540 if (include.range.contains(hoverPos))
541 return include.buildHover();
545 SMLoc posLoc = hoverPos.getAsSMLoc(sourceMgr);
546 const TableGenIndexSymbol *symbol = index.lookup(posLoc, &hoverRange);
551 if (
auto *record = dyn_cast<TableGenRecordSymbol>(symbol))
552 return buildHoverForRecord(record->getValue(), hoverRange);
556 auto *recordVal = cast<TableGenRecordValSymbol>(symbol);
557 const RecordVal *value = recordVal->getValue();
558 if (value->isTemplateArg())
559 return buildHoverForTemplateArg(recordVal->record, value, hoverRange);
560 return buildHoverForField(recordVal->record, value, hoverRange);
564 TableGenTextFile::buildHoverForRecord(
const Record *record,
565 const SMRange &hoverRange) {
566 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
568 llvm::raw_string_ostream hoverOS(hover.contents.value);
571 if (record->isClass()) {
572 hoverOS <<
"**class** `" << record->getName() <<
"`";
573 }
else if (record->isAnonymous()) {
574 hoverOS <<
"**anonymous class**";
576 hoverOS <<
"**def** `" << record->getName() <<
"`";
578 hoverOS <<
"\n***\n";
582 auto printAndFormatField = [&](StringRef fieldName) {
585 const RecordVal *value = record->getValue(fieldName);
586 if (!value || !value->getValue())
588 auto *stringValue = dyn_cast<llvm::StringInit>(value->getValue());
593 ros.printReindented(stringValue->getValue().rtrim(
" \t"));
594 hoverOS <<
"\n***\n";
596 printAndFormatField(
"summary");
597 printAndFormatField(
"description");
600 if (std::optional<std::string> doc =
602 hoverOS <<
"\n" << *doc <<
"\n";
608 llvm::lsp::Hover TableGenTextFile::buildHoverForTemplateArg(
609 const Record *record,
const RecordVal *value,
const SMRange &hoverRange) {
610 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
612 llvm::raw_string_ostream hoverOS(hover.contents.value);
613 StringRef name = value->getName().rsplit(
':').second;
615 hoverOS <<
"**template arg** `" << name <<
"`\n***\nType: `";
616 value->getType()->print(hoverOS);
622 llvm::lsp::Hover TableGenTextFile::buildHoverForField(
623 const Record *record,
const RecordVal *value,
const SMRange &hoverRange) {
624 llvm::lsp::Hover hover(llvm::lsp::Range(sourceMgr, hoverRange));
626 llvm::raw_string_ostream hoverOS(hover.contents.value);
627 hoverOS <<
"**field** `" << value->getName() <<
"`\n***\nType: `";
628 value->getType()->print(hoverOS);
629 hoverOS <<
"`\n***\n";
632 if (std::optional<std::string> doc =
634 hoverOS <<
"\n" << *doc <<
"\n";
635 hoverOS <<
"\n***\n";
640 auto [baseRecord, baseValue] =
getBaseValue(record, value);
642 if (std::optional<std::string> doc =
644 hoverOS <<
"\n *From `" << baseRecord->getName() <<
"`*:\n\n"
668 llvm::StringMap<std::unique_ptr<TableGenTextFile>>
files;
681 std::vector<Diagnostic> &diagnostics) {
683 std::vector<std::string> additionalIncludeDirs =
impl->options.extraDirs;
684 const auto &fileInfo =
impl->compilationDatabase.getFileInfo(uri.file());
685 llvm::append_range(additionalIncludeDirs, fileInfo.includeDirs);
687 impl->files[uri.file()] = std::make_unique<TableGenTextFile>(
688 uri, contents, version, additionalIncludeDirs, diagnostics);
693 int64_t version, std::vector<Diagnostic> &diagnostics) {
695 auto it =
impl->files.find(uri.file());
696 if (it ==
impl->files.end())
701 if (
failed(it->second->update(uri, version, changes, diagnostics)))
702 impl->files.erase(it);
705 std::optional<int64_t>
707 auto it =
impl->files.find(uri.file());
708 if (it ==
impl->files.end())
711 int64_t version = it->second->getVersion();
712 impl->files.erase(it);
717 const Position &defPos,
718 std::vector<Location> &locations) {
719 auto fileIt =
impl->files.find(uri.file());
720 if (fileIt !=
impl->files.end())
721 fileIt->second->getLocationsOf(uri, defPos, locations);
726 std::vector<Location> &references) {
727 auto fileIt =
impl->files.find(uri.file());
728 if (fileIt !=
impl->files.end())
729 fileIt->second->findReferencesOf(uri, pos, references);
733 const URIForFile &uri, std::vector<DocumentLink> &documentLinks) {
734 auto fileIt =
impl->files.find(uri.file());
735 if (fileIt !=
impl->files.end())
736 return fileIt->second->getDocumentLinks(uri, documentLinks);
739 std::optional<llvm::lsp::Hover>
741 const Position &hoverPos) {
742 auto fileIt =
impl->files.find(uri.file());
743 if (fileIt !=
impl->files.end())
744 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.