refactor(document links): use Lexer for unified directive argument scanning (#421)

## Summary
- Replace hand-written character scanning in `document_links.cpp` with
the project's `Lexer` class for finding filename arguments in
preprocessor directives
- Extend `Lexer` to activate `header_name` mode for
`#embed`/`#include_next`, and expose `set_header_name_mode()` for
`__has_include`/`__has_embed` contexts
- Remove unused `Include::filename_range` field (had a latent assert
crash on macro-expanded includes)
- Add `MacroInclude` unit test covering `#include MACRO` scenario

## Test plan
- [x] 498 unit tests pass (including new `MacroInclude` test)
- [x] 119 integration tests pass
- [x] 2/2 smoke tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Document links now resolve includes written via macros; directive
parsing recognizes include, include_next, embed and __has_* patterns
more reliably using lexer-driven argument detection.

* **Refactor**
* Removed an internal filename-range field previously stored for include
directives.

* **Tests**
* Added unit tests covering directive argument extraction and
macro-based include linking.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ykiko
2026-04-11 17:17:10 +08:00
committed by GitHub
parent 9c89d20e76
commit d42d9d5b29
7 changed files with 183 additions and 37 deletions

View File

@@ -83,5 +83,87 @@ int x = 1;
}
}; // TEST_SUITE(SourceText)
TEST_SUITE(DirectiveArgument) {
void EXPECT_RANGE(llvm::StringRef content, std::uint32_t offset, llvm::StringRef expected) {
auto result = find_directive_argument(content, offset);
ASSERT_TRUE(result.has_value());
ASSERT_EQ(content.substr(result->begin, result->length()), expected);
}
void EXPECT_NONE(llvm::StringRef content, std::uint32_t offset) {
auto result = find_directive_argument(content, offset);
ASSERT_FALSE(result.has_value());
}
TEST_CASE(IncludeQuoted) {
llvm::StringRef src = R"(#include "foo.h")";
EXPECT_RANGE(src, 0, R"("foo.h")");
}
TEST_CASE(IncludeAngled) {
llvm::StringRef src = "#include <iostream>";
EXPECT_RANGE(src, 0, "<iostream>");
}
TEST_CASE(IncludeMacro) {
llvm::StringRef src = "#include HEADER";
EXPECT_RANGE(src, 0, "HEADER");
}
TEST_CASE(HasIncludeQuoted) {
llvm::StringRef src = R"(#if __has_include("foo.h"))";
// offset at __has_include
auto pos = src.find("__has_include");
EXPECT_RANGE(src, static_cast<std::uint32_t>(pos), R"("foo.h")");
}
TEST_CASE(HasIncludeAngled) {
llvm::StringRef src = "#if __has_include(<vector>)";
auto pos = src.find("__has_include");
EXPECT_RANGE(src, static_cast<std::uint32_t>(pos), "<vector>");
}
TEST_CASE(EmbedQuoted) {
llvm::StringRef src = R"(#embed "data.bin")";
EXPECT_RANGE(src, 0, R"("data.bin")");
}
TEST_CASE(HasEmbedQuoted) {
llvm::StringRef src = R"(#if __has_embed("data.bin"))";
auto pos = src.find("__has_embed");
EXPECT_RANGE(src, static_cast<std::uint32_t>(pos), R"("data.bin")");
}
TEST_CASE(MultilineOffset) {
llvm::StringRef src = "#include \"a.h\"\n#include \"b.h\"";
// offset pointing into the second line
auto pos = src.find("#include \"b.h\"");
EXPECT_RANGE(src, static_cast<std::uint32_t>(pos), R"("b.h")");
}
TEST_CASE(EmptyDirective) {
llvm::StringRef src = "#include \n";
EXPECT_NONE(src, 0);
}
TEST_CASE(HasIncludeFromLineStart) {
llvm::StringRef src = "#if __has_include(<vector>)";
EXPECT_RANGE(src, 0, "<vector>");
}
TEST_CASE(HasEmbedFromLineStart) {
llvm::StringRef src = R"(#if __has_embed("data.bin"))";
EXPECT_RANGE(src, 0, R"("data.bin")");
}
TEST_CASE(IncludeNext) {
llvm::StringRef src = "#include_next <stdlib.h>";
EXPECT_RANGE(src, 0, "<stdlib.h>");
}
}; // TEST_SUITE(DirectiveArgument)
} // namespace
} // namespace clice::testing