diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/MlirPdllLspServerMain.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/MlirPdllLspServerMain.cpp --- a/mlir/lib/Tools/mlir-pdll-lsp-server/MlirPdllLspServerMain.cpp +++ b/mlir/lib/Tools/mlir-pdll-lsp-server/MlirPdllLspServerMain.cpp @@ -51,6 +51,10 @@ llvm::cl::desc("Pretty-print JSON output"), llvm::cl::init(false), }; + llvm::cl::list extraIncludeDirs( + "I", llvm::cl::desc("Extra directory of include files"), + llvm::cl::value_desc("directory"), llvm::cl::Prefix); + llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR LSP Language Server"); if (litTest) { @@ -67,6 +71,7 @@ JSONTransport transport(stdin, llvm::outs(), inputStyle, prettyPrint); // Configure the servers and start the main language server. - PDLLServer server; + PDLLServer::Options options(extraIncludeDirs); + PDLLServer server(options); return runPdllLSPServer(server, transport); } diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.h b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.h --- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.h +++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.h @@ -28,7 +28,14 @@ /// separate from the logic that involves LSP server/client communication. class PDLLServer { public: - PDLLServer(); + struct Options { + Options(const std::vector &extraDirs) : extraDirs(extraDirs){}; + + /// Additional list of include directories to search. + const std::vector &extraDirs; + }; + + PDLLServer(const Options &options); ~PDLLServer(); /// Add or update the document, with the provided `version`, at the given URI. @@ -68,8 +75,10 @@ const Position &helpPos); private: - struct Impl; + /// PDLL LSP options. + const Options &options; + struct Impl; std::unique_ptr impl; }; diff --git a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp --- a/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp +++ b/mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp @@ -238,6 +238,7 @@ /// document. struct PDLDocument { PDLDocument(const lsp::URIForFile &uri, StringRef contents, + const std::vector &extraDirs, std::vector &diagnostics); PDLDocument(const PDLDocument &) = delete; PDLDocument &operator=(const PDLDocument &) = delete; @@ -315,6 +316,7 @@ } // namespace PDLDocument::PDLDocument(const lsp::URIForFile &uri, StringRef contents, + const std::vector &extraDirs, std::vector &diagnostics) : astContext(odsContext) { auto memBuffer = llvm::MemoryBuffer::getMemBufferCopy(contents, uri.file()); @@ -327,6 +329,7 @@ llvm::SmallString<32> uriDirectory(uri.file()); llvm::sys::path::remove_filename(uriDirectory); includeDirs.push_back(uriDirectory.str().str()); + includeDirs.insert(includeDirs.end(), extraDirs.begin(), extraDirs.end()); sourceMgr.setIncludeDirs(includeDirs); sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc()); @@ -991,8 +994,10 @@ struct PDLTextFileChunk { PDLTextFileChunk(uint64_t lineOffset, const lsp::URIForFile &uri, StringRef contents, + const std::vector &extraDirs, std::vector &diagnostics) - : lineOffset(lineOffset), document(uri, contents, diagnostics) {} + : lineOffset(lineOffset), + document(uri, contents, extraDirs, diagnostics) {} /// Adjust the line number of the given range to anchor at the beginning of /// the file, instead of the beginning of this chunk. @@ -1020,7 +1025,8 @@ class PDLTextFile { public: PDLTextFile(const lsp::URIForFile &uri, StringRef fileContents, - int64_t version, std::vector &diagnostics); + int64_t version, const std::vector &extraDirs, + std::vector &diagnostics); /// Return the current version of this text file. int64_t getVersion() const { return version; } @@ -1064,6 +1070,7 @@ PDLTextFile::PDLTextFile(const lsp::URIForFile &uri, StringRef fileContents, int64_t version, + const std::vector &extraDirs, std::vector &diagnostics) : contents(fileContents.str()), version(version), totalNumLines(0) { // Split the file into separate PDL documents. @@ -1073,13 +1080,13 @@ SmallVector subContents; StringRef(contents).split(subContents, "// -----"); chunks.emplace_back(std::make_unique( - /*lineOffset=*/0, uri, subContents.front(), diagnostics)); + /*lineOffset=*/0, uri, subContents.front(), extraDirs, diagnostics)); uint64_t lineOffset = subContents.front().count('\n'); for (StringRef docContents : llvm::drop_begin(subContents)) { unsigned currentNumDiags = diagnostics.size(); - auto chunk = std::make_unique(lineOffset, uri, - docContents, diagnostics); + auto chunk = std::make_unique( + lineOffset, uri, docContents, extraDirs, diagnostics); lineOffset += docContents.count('\n'); // Adjust locations used in diagnostics to account for the offset from the @@ -1226,14 +1233,15 @@ // PDLLServer //===----------------------------------------------------------------------===// -lsp::PDLLServer::PDLLServer() : impl(std::make_unique()) {} +lsp::PDLLServer::PDLLServer(const Options &options) + : options(options), impl(std::make_unique()) {} lsp::PDLLServer::~PDLLServer() = default; void lsp::PDLLServer::addOrUpdateDocument( const URIForFile &uri, StringRef contents, int64_t version, std::vector &diagnostics) { - impl->files[uri.file()] = - std::make_unique(uri, contents, version, diagnostics); + impl->files[uri.file()] = std::make_unique( + uri, contents, version, options.extraDirs, diagnostics); } Optional lsp::PDLLServer::removeDocument(const URIForFile &uri) {