Skip to content

Commit e960202

Browse files
Merge pull request #682 from rolfbjarne/nameddecl-qualifiedname
Expose NamedDecl->getQualifiedNameAsString() to ClangSharp.
2 parents 8747fd4 + f281c4f commit e960202

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

sources/ClangSharp.Interop/Extensions/CXCursor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,8 @@ public readonly ReadOnlySpan<CXCursor> OverriddenCursors
13581358

13591359
public readonly CXPrintingPolicy PrintingPolicy => (kind != default) ? (CXPrintingPolicy)clang.getCursorPrintingPolicy(this) : default;
13601360

1361+
public readonly CXString QualifiedName => clangsharp.Cursor_getQualifiedName(this);
1362+
13611363
public readonly CXString RawCommentText => clang.Cursor_getRawCommentText(this);
13621364

13631365
public readonly CXType ReceiverType => !IsExpression ? default : clang.Cursor_getReceiverType(this);

sources/ClangSharp.Interop/clangsharp/clangsharp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ public static unsafe partial class @clangsharp
812812
[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getProtocol", ExactSpelling = true)]
813813
public static extern CXCursor Cursor_getProtocol(CXCursor C, [NativeTypeName("unsigned int")] uint i);
814814

815+
[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getQualifiedName", ExactSpelling = true)]
816+
public static extern CXString Cursor_getQualifiedName(CXCursor C);
817+
815818
[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getRedeclContext", ExactSpelling = true)]
816819
public static extern CXCursor Cursor_getRedeclContext(CXCursor C);
817820

sources/ClangSharp/Cursors/Decls/NamedDecl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ private protected NamedDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX
2626

2727
public string Name => Spelling;
2828

29+
public string QualifiedName => Handle.QualifiedName.ToString();
30+
2931
public NamedDecl UnderlyingDecl => _underlyingDecl.Value;
3032

3133
public CXVisibilityKind Visibility => Handle.Visibility;

sources/libClangSharp/ClangSharp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,18 @@ CXCursor clangsharp_Cursor_getProtocol(CXCursor C, unsigned i) {
39753975
return clang_getNullCursor();
39763976
}
39773977

3978+
CXString clangsharp_Cursor_getQualifiedName(CXCursor C) {
3979+
if (isDeclOrTU(C.kind)) {
3980+
const Decl* D = getCursorDecl(C);
3981+
3982+
if (const NamedDecl* ND = dyn_cast<NamedDecl>(D)) {
3983+
return createDup(ND->getQualifiedNameAsString());
3984+
}
3985+
}
3986+
3987+
return createEmpty();
3988+
}
3989+
39783990
CXCursor clangsharp_Cursor_getRedeclContext(CXCursor C) {
39793991
if (isDeclOrTU(C.kind)) {
39803992
const Decl* D = getCursorDecl(C);

sources/libClangSharp/ClangSharp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ CLANGSHARP_LINKAGE unsigned clangsharp_Cursor_getPropertyAttributes(CXCursor C);
695695

696696
CLANGSHARP_LINKAGE CXCursor clangsharp_Cursor_getProtocol(CXCursor C, unsigned i);
697697

698+
CLANGSHARP_LINKAGE CXString clangsharp_Cursor_getQualifiedName(CXCursor C);
699+
698700
CLANGSHARP_LINKAGE CXCursor clangsharp_Cursor_getRedeclContext(CXCursor C);
699701

700702
CLANGSHARP_LINKAGE CXCursor clangsharp_Cursor_getReferenced(CXCursor C);

tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ struct B {
126126
Assert.That(structB.IsPOD, Is.False, "struct B should be not POD");
127127
}
128128

129+
130+
[Test]
131+
public void QualifiedNameTest()
132+
{
133+
AssertNeedNewClangSharp();
134+
135+
var inputContents = """
136+
class C {
137+
void M();
138+
int F;
139+
};
140+
141+
struct S {
142+
void M();
143+
int F;
144+
};
145+
""";
146+
147+
using var translationUnit = CreateTranslationUnit(inputContents);
148+
149+
var records = translationUnit.TranslationUnitDecl.Decls.OfType<CXXRecordDecl>().ToArray();
150+
var classDecl = records.Single(v => v.Name == "C");
151+
var structDecl = records.Single(v => v.Name == "S");
152+
var classM = classDecl.Decls.OfType<FunctionDecl>().Single();
153+
var structM = structDecl.Decls.OfType<FunctionDecl>().Single();
154+
155+
Assert.That(classDecl.QualifiedName, Is.EqualTo("C"), "Class");
156+
Assert.That(classM.QualifiedName, Is.EqualTo("C::M"), "Class Method");
157+
158+
Assert.That(structDecl.QualifiedName, Is.EqualTo("S"), "Struct");
159+
Assert.That(structM.QualifiedName, Is.EqualTo("S::M"), "Struct Method");
160+
}
161+
129162
[Test]
130163
public void UnsignedValue()
131164
{

0 commit comments

Comments
 (0)