10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/Support/Path.h"
17 using llvm::lsp::Hover;
18 using llvm::lsp::Range;
19 using llvm::lsp::URIForFile;
29 while (
char c = *curPtr++) {
31 if (StringRef(
"\"\n\v\f").
contains(c))
37 if (*curPtr ==
'"' || *curPtr ==
'\\' || *curPtr ==
'n' || *curPtr ==
't')
39 else if (llvm::isHexDigit(*curPtr) && llvm::isHexDigit(curPtr[1]))
54 const char *curPtr = loc.getPointer();
63 auto isIdentifierChar = [=](
char c) {
64 return isalnum(c) || c ==
'_' || identifierChars.contains(c);
67 while (*curPtr && isIdentifierChar(*(++curPtr)))
71 return SMRange(loc, SMLoc::getFromPointer(curPtr));
74 std::optional<std::string>
81 int bufferId = sourceMgr.FindBufferContainingLoc(loc);
84 const char *bufferStart =
85 sourceMgr.getMemoryBuffer(bufferId)->getBufferStart();
86 StringRef buffer(bufferStart, loc.getPointer() - bufferStart);
89 auto popLastLine = [&]() -> std::optional<StringRef> {
90 size_t newlineOffset = buffer.find_last_of(
'\n');
91 if (newlineOffset == StringRef::npos)
93 StringRef lastLine = buffer.drop_front(newlineOffset).trim();
94 buffer = buffer.take_front(newlineOffset);
104 while (std::optional<StringRef> line = popLastLine()) {
106 if (!line->starts_with(
"//"))
110 commentLines.push_back(line->ltrim(
'/'));
113 if (commentLines.empty())
115 return llvm::join(llvm::reverse(commentLines),
"\n");
119 return range.Start.getPointer() <= loc.getPointer() &&
120 loc.getPointer() < range.End.getPointer();
130 llvm::raw_string_ostream hoverOS(hover.contents.value);
131 hoverOS <<
"`" << llvm::sys::path::filename(
uri.file()) <<
"`\n***\n"
139 for (
unsigned i = 1, e = sourceMgr.getNumBuffers(); i < e; ++i) {
141 SMLoc includeLoc = sourceMgr.getBufferInfo(i + 1).IncludeLoc;
142 if (!includeLoc.isValid() || sourceMgr.FindBufferContainingLoc(
143 includeLoc) != sourceMgr.getMainFileID())
147 auto *buffer = sourceMgr.getMemoryBuffer(i + 1);
149 llvm::sys::path::remove_dots(path,
true);
152 if (!includedFileURI)
156 const char *includeStart = includeLoc.getPointer() - 2;
157 while (*(--includeStart) !=
'\"')
161 SMRange includeRange(SMLoc::getFromPointer(includeStart), includeLoc);
162 includes.emplace_back(*includedFileURI, Range(sourceMgr, includeRange));
static const char * lexLocStringTok(const char *curPtr)
Find the end of a string whose contents start at the given curPtr.
void gatherIncludeFiles(llvm::SourceMgr &sourceMgr, SmallVectorImpl< SourceMgrInclude > &includes)
Given a source manager, gather all of the processed include files.
bool contains(SMRange range, SMLoc loc)
Returns true if the given range contains the given source location.
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.
llvm::lsp::Hover buildHover() const
Build a hover for the current include file.
llvm::lsp::URIForFile uri
The URI of the file that is included.
llvm::lsp::Range range
The range of the include directive.