Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ tests/unit/Local/
.pixi/*
!.pixi/config.toml

.codex/
.codex
.claude/*
!.claude/CLAUDE.md
!.claude/commands/
openspec/
4 changes: 4 additions & 0 deletions src/feature/semantic_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class SemanticTokensCollector : public SemanticVisitor<SemanticTokensCollector>
void handleDeclOccurrence(const clang::NamedDecl* decl,
RelationKind relation,
clang::SourceLocation location) {
if(relation.isReference() && !ast::can_highlight_name(decl->getDeclName())) {
return;
}

std::uint32_t modifiers = 0;
if(relation.is_one_of(RelationKind::Definition)) {
// todo: clangd add both Declaration and Definition modifiers for definitions.
Expand Down
27 changes: 27 additions & 0 deletions src/semantic/ast_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ bool is_implicit_template_instantiation(const clang::NamedDecl* decl) {
return is_template_specialization_kind(decl, clang::TSK_ImplicitInstantiation);
}

bool can_highlight_name(clang::DeclarationName name) {
switch(name.getNameKind()) {
case clang::DeclarationName::Identifier: {
auto* info = name.getAsIdentifierInfo();
return info && !info->getName().empty();
}

case clang::DeclarationName::CXXConstructorName:
case clang::DeclarationName::CXXDestructorName: {
return true;
}

case clang::DeclarationName::CXXConversionFunctionName:
case clang::DeclarationName::CXXOperatorName:
case clang::DeclarationName::CXXDeductionGuideName:
case clang::DeclarationName::CXXLiteralOperatorName:
case clang::DeclarationName::CXXUsingDirective:
case clang::DeclarationName::ObjCZeroArgSelector:
case clang::DeclarationName::ObjCOneArgSelector:
case clang::DeclarationName::ObjCMultiArgSelector: {
return false;
}
}

std::unreachable();
}

const static clang::CXXRecordDecl* getDeclContextForTemplateInstationPattern(const clang::Decl* D) {
if(const auto* CTSD = dyn_cast<clang::ClassTemplateSpecializationDecl>(D->getDeclContext())) {
return CTSD->getTemplateInstantiationPattern();
Expand Down
3 changes: 3 additions & 0 deletions src/semantic/ast_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ bool is_inside_main_file(clang::SourceLocation loc, const clang::SourceManager&
/// Checks whether the decl is an implicit template instantiation.
bool is_implicit_template_instantiation(const clang::NamedDecl* decl);

/// Whether a declaration name is backed by source text that should be highlighted.
bool can_highlight_name(clang::DeclarationName name);

/// Return the decl where it is instantiated from. If could be a template decl
/// or a member of a class template. If the decl is a full specialization, return
/// itself.
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/feature/semantic_tokens_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ void EXPECT_TOKEN(llvm::StringRef name,
ASSERT_EQ(token->modifiers, expected_modifiers);
}

void EXPECT_NO_TOKEN(llvm::StringRef name) {
ASSERT_TRUE(find_by_range(name) == nullptr);
}

TEST_CASE(BasicLexicalKinds) {
run_utf8(R"cpp(
@d1[#define] @m0[FOO]
Expand Down Expand Up @@ -264,6 +268,44 @@ int main() {
EXPECT_TOKEN("x3", SymbolKind::Variable, 0);
}

TEST_CASE(IneligibleOperatorReferenceIsSuppressed) {
run_utf8(R"cpp(
struct S {};

S operator+(S lhs, S rhs);

void use(S lhs, S rhs) {
(void)(lhs @plus[+] rhs);
}
)cpp");

EXPECT_NO_TOKEN("plus");
}

TEST_CASE(ConstructorAndDestructorNamesRemainHighlighted) {
run_utf8(R"cpp(
struct S {
@ctor_decl[S]();
@dtor_decl[~]S();
};

S::@ctor_def[S]() {}

void use(S* value) {
value->@dtor_ref[~]S();
}
)cpp");

auto declaration = modifier_mask({SymbolModifiers::Declaration});
auto definition = modifier_mask({SymbolModifiers::Definition});
auto special_member = modifier_mask({SymbolModifiers::ConstructorOrDestructor});

EXPECT_TOKEN("ctor_decl", SymbolKind::Method, declaration | special_member);
EXPECT_TOKEN("dtor_decl", SymbolKind::Method, declaration | special_member);
EXPECT_TOKEN("ctor_def", SymbolKind::Method, definition | special_member);
EXPECT_TOKEN("dtor_ref", SymbolKind::Method, special_member);
}

TEST_CASE(LegacyVarDeclTemplates) {
run_utf8(R"cpp(
extern int @x1[x];
Expand Down
Loading