MLIR  20.0.0git
Classes | Functions
MLIRServer.cpp File Reference
#include "MLIRServer.h"
#include "Protocol.h"
#include "mlir/AsmParser/AsmParser.h"
#include "mlir/AsmParser/AsmParserState.h"
#include "mlir/AsmParser/CodeComplete.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Support/ToolUtilities.h"
#include "mlir/Tools/lsp-server-support/Logging.h"
#include "mlir/Tools/lsp-server-support/SourceMgrUtils.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Base64.h"
#include "llvm/Support/SourceMgr.h"
#include <optional>

Go to the source code of this file.

Classes

struct  mlir::lsp::MLIRServer::Impl
 

Functions

static SMRange convertTokenLocToRange (SMLoc loc)
 Returns the range of a lexical token given a SMLoc corresponding to the start of an token location. More...
 
static std::optional< lsp::LocationgetLocationFromLoc (StringRef uriScheme, FileLineColLoc loc)
 Returns a language server location from the given MLIR file location. More...
 
static std::optional< lsp::LocationgetLocationFromLoc (llvm::SourceMgr &sourceMgr, Location loc, StringRef uriScheme, const lsp::URIForFile *uri=nullptr)
 Returns a language server location from the given MLIR location, or std::nullopt if one couldn't be created. More...
 
static void collectLocationsFromLoc (Location loc, std::vector< lsp::Location > &locations, const lsp::URIForFile &uri)
 Collect all of the locations from the given MLIR location that are not contained within the given URI. More...
 
static bool contains (SMRange range, SMLoc loc)
 Returns true if the given range contains the given source location. More...
 
static bool isDefOrUse (const AsmParserState::SMDefinition &def, SMLoc loc, SMRange *overlappedRange=nullptr)
 Returns true if the given location is contained by the definition or one of the uses of the given SMDefinition. More...
 
static std::optional< unsigned > getResultNumberFromLoc (SMLoc loc)
 Given a location pointing to a result, return the result number it refers to or std::nullopt if it refers to all of the results. More...
 
static std::optional< StringRef > getTextFromRange (SMRange range)
 Given a source location range, return the text covered by the given range. More...
 
static unsigned getBlockNumber (Block *block)
 Given a block, return its position in its parent region. More...
 
static void printDefBlockName (raw_ostream &os, Block *block, SMRange loc={})
 Given a block and source location, print the source name of the block to the given output stream. More...
 
static void printDefBlockName (raw_ostream &os, const AsmParserState::BlockDefinition &def)
 
static lsp::Diagnostic getLspDiagnoticFromDiag (llvm::SourceMgr &sourceMgr, Diagnostic &diag, const lsp::URIForFile &uri)
 Convert the given MLIR diagnostic to the LSP form. More...
 

Function Documentation

◆ collectLocationsFromLoc()

static void collectLocationsFromLoc ( Location  loc,
std::vector< lsp::Location > &  locations,
const lsp::URIForFile uri 
)
static

Collect all of the locations from the given MLIR location that are not contained within the given URI.

Definition at line 91 of file MLIRServer.cpp.

References mlir::WalkResult::advance(), getLocationFromLoc(), mlir::lsp::URIForFile::scheme(), and mlir::LocationAttr::walk().

◆ contains()

static bool contains ( SMRange  range,
SMLoc  loc 
)
static

Returns true if the given range contains the given source location.

Note that this has slightly different behavior than SMRange because it is inclusive of the end location.

Definition at line 111 of file MLIRServer.cpp.

Referenced by isDefOrUse(), and lexLocStringTok().

◆ convertTokenLocToRange()

static SMRange convertTokenLocToRange ( SMLoc  loc)
static

Returns the range of a lexical token given a SMLoc corresponding to the start of an token location.

The range is computed heuristically, and supports identifier-like tokens, strings, etc.

Definition at line 31 of file MLIRServer.cpp.

References mlir::lsp::convertTokenLocToRange().

Referenced by getLocationFromLoc().

◆ getBlockNumber()

static unsigned getBlockNumber ( Block block)
static

Given a block, return its position in its parent region.

Definition at line 176 of file MLIRServer.cpp.

References mlir::Region::begin(), and mlir::Block::getParent().

◆ getLocationFromLoc() [1/2]

static std::optional<lsp::Location> getLocationFromLoc ( llvm::SourceMgr &  sourceMgr,
Location  loc,
StringRef  uriScheme,
const lsp::URIForFile uri = nullptr 
)
static

Returns a language server location from the given MLIR location, or std::nullopt if one couldn't be created.

uriScheme is the scheme to use when building new uris. uri is an optional additional filter that, when present, is used to filter sub locations that do not share the same uri.

Definition at line 59 of file MLIRServer.cpp.

References mlir::WalkResult::advance(), convertTokenLocToRange(), mlir::FileLineColLoc::getColumn(), mlir::FileLineColLoc::getLine(), getLocationFromLoc(), mlir::WalkResult::interrupt(), max(), and mlir::LocationAttr::walk().

◆ getLocationFromLoc() [2/2]

static std::optional<lsp::Location> getLocationFromLoc ( StringRef  uriScheme,
FileLineColLoc  loc 
)
static

Returns a language server location from the given MLIR file location.

uriScheme is the scheme to use when building new uris.

Definition at line 37 of file MLIRServer.cpp.

References mlir::lsp::Position::character, mlir::lsp::Logger::error(), mlir::lsp::URIForFile::fromFile(), mlir::FileLineColLoc::getColumn(), mlir::FileLineColLoc::getFilename(), mlir::FileLineColLoc::getLine(), mlir::lsp::Position::line, and toString().

Referenced by collectLocationsFromLoc(), getLocationFromLoc(), and getLspDiagnoticFromDiag().

◆ getLspDiagnoticFromDiag()

static lsp::Diagnostic getLspDiagnoticFromDiag ( llvm::SourceMgr &  sourceMgr,
Diagnostic diag,
const lsp::URIForFile uri 
)
static

◆ getResultNumberFromLoc()

static std::optional<unsigned> getResultNumberFromLoc ( SMLoc  loc)
static

Given a location pointing to a result, return the result number it refers to or std::nullopt if it refers to all of the results.

Definition at line 141 of file MLIRServer.cpp.

◆ getTextFromRange()

static std::optional<StringRef> getTextFromRange ( SMRange  range)
static

Given a source location range, return the text covered by the given range.

If the range is invalid, returns std::nullopt.

Definition at line 168 of file MLIRServer.cpp.

◆ isDefOrUse()

static bool isDefOrUse ( const AsmParserState::SMDefinition def,
SMLoc  loc,
SMRange *  overlappedRange = nullptr 
)
static

Returns true if the given location is contained by the definition or one of the uses of the given SMDefinition.

If provided, overlappedRange is set to the range within def that the provided loc overlapped with.

Definition at line 119 of file MLIRServer.cpp.

References contains(), mlir::AsmParserState::SMDefinition::loc, and mlir::AsmParserState::SMDefinition::uses.

◆ printDefBlockName() [1/2]

static void printDefBlockName ( raw_ostream &  os,
Block block,
SMRange  loc = {} 
)
static

Given a block and source location, print the source name of the block to the given output stream.

Definition at line 182 of file MLIRServer.cpp.

Referenced by printDefBlockName().

◆ printDefBlockName() [2/2]

static void printDefBlockName ( raw_ostream &  os,
const AsmParserState::BlockDefinition def 
)
static