| 1 | //===--- SourceMgrUtils.h - SourceMgr LSP Utils -----------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains an array of generally useful SourceMgr utilities for |
| 10 | // interacting with LSP components. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef MLIR_TOOLS_LSPSERVERSUPPORT_SOURCEMGRUTILS_H |
| 15 | #define MLIR_TOOLS_LSPSERVERSUPPORT_SOURCEMGRUTILS_H |
| 16 | |
| 17 | #include "mlir/Support/LLVM.h" |
| 18 | #include "llvm/Support/LSP/Protocol.h" |
| 19 | #include "llvm/Support/SourceMgr.h" |
| 20 | #include <optional> |
| 21 | |
| 22 | namespace mlir { |
| 23 | namespace lsp { |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Utils |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | /// Returns the range of a lexical token given a SMLoc corresponding to the |
| 29 | /// start of an token location. The range is computed heuristically, and |
| 30 | /// supports identifier-like tokens, strings, etc. |
| 31 | SMRange convertTokenLocToRange(SMLoc loc, StringRef identifierChars = "" ); |
| 32 | |
| 33 | /// Extract a documentation comment for the given location within the source |
| 34 | /// manager. Returns std::nullopt if no comment could be computed. |
| 35 | std::optional<std::string> (llvm::SourceMgr &sourceMgr, |
| 36 | SMLoc loc); |
| 37 | |
| 38 | /// Returns true if the given range contains the given source location. Note |
| 39 | /// that this has different behavior than SMRange because it is inclusive of the |
| 40 | /// end location. |
| 41 | bool contains(SMRange range, SMLoc loc); |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // SourceMgrInclude |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | /// This class represents a single include within a root file. |
| 48 | struct SourceMgrInclude { |
| 49 | SourceMgrInclude(const llvm::lsp::URIForFile &uri, |
| 50 | const llvm::lsp::Range &range) |
| 51 | : uri(uri), range(range) {} |
| 52 | |
| 53 | /// Build a hover for the current include file. |
| 54 | llvm::lsp::Hover buildHover() const; |
| 55 | |
| 56 | /// The URI of the file that is included. |
| 57 | llvm::lsp::URIForFile uri; |
| 58 | |
| 59 | /// The range of the include directive. |
| 60 | llvm::lsp::Range range; |
| 61 | }; |
| 62 | |
| 63 | /// Given a source manager, gather all of the processed include files. These are |
| 64 | /// assumed to be all of the files other than the main root file. |
| 65 | void gatherIncludeFiles(llvm::SourceMgr &sourceMgr, |
| 66 | SmallVectorImpl<SourceMgrInclude> &includes); |
| 67 | |
| 68 | } // namespace lsp |
| 69 | } // namespace mlir |
| 70 | |
| 71 | #endif |
| 72 | |