[clang-tools-extra] 147bce7 - [clangd] textDocument/documentLink to support include statements with macro argument (#137550)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 4 02:37:41 PDT 2025
Author: frayien
Date: 2025-08-04T11:37:37+02:00
New Revision: 147bce78588b53db661888257e8bb915f1de98d9
URL: https://github.com/llvm/llvm-project/commit/147bce78588b53db661888257e8bb915f1de98d9
DIFF: https://github.com/llvm/llvm-project/commit/147bce78588b53db661888257e8bb915f1de98d9.diff
LOG: [clangd] textDocument/documentLink to support include statements with macro argument (#137550)
See issue https://github.com/clangd/clangd/issues/2375
The clangd textDocument/documentLink request does not support include
statements with macro argument and fails by returning an invalid range.
This PR adds support for macro argument by detecting what form of
include statement is currently being processed and returning a range
accordingly
First time contributing to LLVM, so if you have advice, questions or if
I messed up procedure please tell me !
---------
Co-authored-by: Adrien Garbani <garbani.adrien at frayien.fr>
Added:
Modified:
clang-tools-extra/clangd/XRefs.cpp
clang-tools-extra/clangd/unittests/XRefsTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index 5bbc681cf04e2..83a8b7289aec3 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -859,17 +859,47 @@ std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST) {
for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
if (Inc.Resolved.empty())
continue;
+
+ // Get the location of the # symbole of the "#include ..." statement
auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
+
+ // get the # Token itself, std::next to get the "include" token and the
+ // first token after (aka "File Token")
const auto *HashTok = AST.getTokens().spelledTokenContaining(HashLoc);
assert(HashTok && "got inclusion at wrong offset");
const auto *IncludeTok = std::next(HashTok);
const auto *FileTok = std::next(IncludeTok);
- // FileTok->range is not sufficient here, as raw lexing wouldn't yield
- // correct tokens for angled filenames. Hence we explicitly use
- // Inc.Written's length.
- auto FileRange =
- syntax::FileRange(SM, FileTok->location(), Inc.Written.length())
- .toCharRange(SM);
+
+ // The File Token can either be of kind :
+ // "less" if using the "#include <h-char-sequence> new-line" syntax
+ // "string_literal" if using the "#include "q-char-sequence" new-line"
+ // syntax something else (most likely "identifier") if using the "#include
+ // pp-tokens new-line" syntax (#include with macro argument)
+
+ CharSourceRange FileRange;
+
+ if (FileTok->kind() == tok::TokenKind::less) {
+ // FileTok->range would only include the '<' char. Hence we explicitly use
+ // Inc.Written's length.
+ FileRange =
+ syntax::FileRange(SM, FileTok->location(), Inc.Written.length())
+ .toCharRange(SM);
+ } else if (FileTok->kind() == tok::TokenKind::string_literal) {
+ // FileTok->range includes the quotes for string literals so just return
+ // it.
+ FileRange = FileTok->range(SM).toCharRange(SM);
+ } else {
+ // FileTok is the first Token of a macro spelling
+
+ // Report the range of the first token (as it should be the macro
+ // identifier)
+ // We could use the AST to find the last spelled token of the macro and
+ // report a range spanning the full macro expression, but it would require
+ // using token-buffers that are deemed too unstable and crash-prone
+ // due to optimizations in cland
+
+ FileRange = FileTok->range(SM).toCharRange(SM);
+ }
Result.push_back(
DocumentLink({halfOpenToRange(SM, FileRange),
diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index b04d6431f89f9..17204a47ba3bc 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2784,14 +2784,24 @@ TEST(GetNonLocalDeclRefs, All) {
TEST(DocumentLinks, All) {
Annotations MainCpp(R"cpp(
+ #define HEADER_AA "faa.h"
+ #define HEADER_BB "fbb.h"
+ #define GET_HEADER(X) HEADER_ ## X
+
#/*comments*/include /*comments*/ $foo[["foo.h"]] //more comments
int end_of_preamble = 0;
#include $bar[[<bar.h>]]
+ #include $AA[[GET_HEADER]](AA) // Some comment !
+ # /* What about */ \
+ include /* multiple line */ \
+ $BB[[GET_HEADER]]( /* statements ? */ \
+ BB /* :) */ )
)cpp");
TestTU TU;
TU.Code = std::string(MainCpp.code());
- TU.AdditionalFiles = {{"foo.h", ""}, {"bar.h", ""}};
+ TU.AdditionalFiles = {
+ {"faa.h", ""}, {"fbb.h", ""}, {"foo.h", ""}, {"bar.h", ""}};
TU.ExtraArgs = {"-isystem."};
auto AST = TU.build();
@@ -2801,7 +2811,11 @@ TEST(DocumentLinks, All) {
DocumentLink({MainCpp.range("foo"),
URIForFile::canonicalize(testPath("foo.h"), "")}),
DocumentLink({MainCpp.range("bar"),
- URIForFile::canonicalize(testPath("bar.h"), "")})));
+ URIForFile::canonicalize(testPath("bar.h"), "")}),
+ DocumentLink({MainCpp.range("AA"),
+ URIForFile::canonicalize(testPath("faa.h"), "")}),
+ DocumentLink({MainCpp.range("BB"),
+ URIForFile::canonicalize(testPath("fbb.h"), "")})));
}
} // namespace
More information about the cfe-commits
mailing list