Skip to content
Merged
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
4 changes: 4 additions & 0 deletions sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,10 @@ unsigned clangsharp_Cursor_getIsExternC(CXCursor C) {
if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) {
return FD->isExternC();
}

if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
return VD->isExternC();
}
}

return 0;
Expand Down
35 changes: 35 additions & 0 deletions tests/ClangSharp.UnitTests/CXCursor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

Expand All @@ -25,4 +26,38 @@ public void AttrKindSpelling()
Assert.That (functionDecl.Handle.AttrKindSpelling, Is.Not.Null.Or.Empty, "AttrKindSpelling");
}
}

[Test]
public void IsExternC()
{
AssertNeedNewClangSharp();

var inputContents =
$$"""
extern "C" {
int theUniverseAndEverything = 0;
int hitchhike ();
}
""";

using var translationUnit = CreateTranslationUnit(inputContents);

var allDecls = new List<Decl>();
allDecls.AddRange(translationUnit.TranslationUnitDecl.Decls);
allDecls.AddRange(allDecls.OfType<LinkageSpecDecl>().SelectMany(v => v.Decls).ToList());

var functionDecls = allDecls.OfType<FunctionDecl>().ToList();
Assert.That(functionDecls.Count, Is.GreaterThan(0), "Function");
foreach (var decl in functionDecls)
{
Assert.That(decl.IsExternC, Is.True, "IsExternC function");
}

var varDecls = allDecls.OfType<VarDecl>().ToList();
Assert.That(varDecls.Count, Is.GreaterThan(0), "Var");
foreach (var decl in varDecls)
{
Assert.That(decl.IsExternC, Is.True, "IsExternC var");
}
}
}