Skip to content

Commit a8325ce

Browse files
committed
Add CppInclusionDirective
1 parent 9e9ca36 commit a8325ce

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/CppAst/CppElement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public string FullParentName
7171
/// <summary>
7272
/// Gets the source file of this element.
7373
/// </summary>
74-
public string SourceFile => string.IsNullOrWhiteSpace(Span.Start.File) ? (Parent as CppElement)?.SourceFile : Span.Start.File;
75-
74+
public string SourceFile => Span.Start.File;
7675
}
7776
}

src/CppAst/CppGlobalDeclarationContainer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Alexandre Mutel. All rights reserved.
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
22
// Licensed under the BSD-Clause 2 license.
33
// See license.txt file in the project root for full license information.
44

@@ -33,6 +33,7 @@ public CppGlobalDeclarationContainer()
3333
Attributes = new List<CppAttribute>();
3434
TokenAttributes = new List<CppAttribute>();
3535
Properties = new CppContainerList<CppProperty>(this);
36+
InclusionDirectives = new CppContainerList<CppInclusionDirective>(this);
3637
}
3738

3839
/// <summary>
@@ -72,6 +73,11 @@ public CppGlobalDeclarationContainer()
7273

7374
public MetaAttributeMap MetaAttributes { get; private set; } = new MetaAttributeMap();
7475

76+
/// <summary>
77+
/// Gets the list of inclusion directives for this container.
78+
/// </summary>
79+
public CppContainerList<CppInclusionDirective> InclusionDirectives { get; }
80+
7581
/// <inheritdoc />
7682
public virtual IEnumerable<ICppDeclaration> Children()
7783
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace CppAst;
6+
7+
/// <summary>
8+
/// Represents a header inclusion directive.
9+
/// </summary>
10+
public class CppInclusionDirective : CppElement
11+
{
12+
/// <summary>
13+
/// Gets or sets the file name being included.
14+
/// </summary>
15+
public string FileName { get; set; }
16+
17+
/// <inheritdoc />
18+
public override string ToString() => FileName ?? "<empty>";
19+
}

src/CppAst/CppModelBuilder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,19 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
624624
element = ParseMacro(cursor);
625625
break;
626626

627-
case CXCursorKind.CXCursor_MacroExpansion:
627+
628628
case CXCursorKind.CXCursor_InclusionDirective:
629+
var file = cursor.IncludedFile;
630+
CppInclusionDirective inclusionDirective = new()
631+
{
632+
FileName = file.Name.ToString()
633+
};
634+
element = inclusionDirective;
635+
var rootContainer = (CppGlobalDeclarationContainer)_rootContainerContext.DeclarationContainer;
636+
rootContainer.InclusionDirectives.Add(inclusionDirective);
637+
break;
638+
639+
case CXCursorKind.CXCursor_MacroExpansion:
629640
case CXCursorKind.CXCursor_FirstRef:
630641
case CXCursorKind.CXCursor_ObjCIvarDecl:
631642
case CXCursorKind.CXCursor_TemplateTypeParameter:

0 commit comments

Comments
 (0)