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/Tools/lsp-server-support/Protocol.h" 18 #include "llvm/Support/SourceMgr.h" 19 #include <optional> 20 21 namespace mlir { 22 namespace lsp { 23 //===----------------------------------------------------------------------===// 24 // Utils 25 //===----------------------------------------------------------------------===// 26 27 /// Returns the range of a lexical token given a SMLoc corresponding to the 28 /// start of an token location. The range is computed heuristically, and 29 /// supports identifier-like tokens, strings, etc. 30 SMRange convertTokenLocToRange(SMLoc loc, StringRef identifierChars = ""); 31 32 /// Extract a documentation comment for the given location within the source 33 /// manager. Returns std::nullopt if no comment could be computed. 34 std::optional<std::string> extractSourceDocComment(llvm::SourceMgr &sourceMgr, 35 SMLoc loc); 36 37 /// Returns true if the given range contains the given source location. Note 38 /// that this has different behavior than SMRange because it is inclusive of the 39 /// end location. 40 bool contains(SMRange range, SMLoc loc); 41 42 //===----------------------------------------------------------------------===// 43 // SourceMgrInclude 44 //===----------------------------------------------------------------------===// 45 46 /// This class represents a single include within a root file. 47 struct SourceMgrInclude { SourceMgrIncludeSourceMgrInclude48 SourceMgrInclude(const lsp::URIForFile &uri, const lsp::Range &range) 49 : uri(uri), range(range) {} 50 51 /// Build a hover for the current include file. 52 Hover buildHover() const; 53 54 /// The URI of the file that is included. 55 lsp::URIForFile uri; 56 57 /// The range of the include directive. 58 lsp::Range range; 59 }; 60 61 /// Given a source manager, gather all of the processed include files. These are 62 /// assumed to be all of the files other than the main root file. 63 void gatherIncludeFiles(llvm::SourceMgr &sourceMgr, 64 SmallVectorImpl<SourceMgrInclude> &includes); 65 66 } // namespace lsp 67 } // namespace mlir 68 69 #endif 70