Skip to content

Commit e8fbdaa

Browse files
committed
Extract and re-use GetIncludePath across multiple generators.
1 parent f18022b commit e8fbdaa

File tree

6 files changed

+27
-33
lines changed

6 files changed

+27
-33
lines changed

src/Generator/Generators/C/CppHeaders.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public void GenerateIncludeForwardRefs(TranslationUnit unit)
9696
if (typeRefUnit != null && !typeRefUnit.IsDeclared)
9797
continue;
9898

99-
if(!string.IsNullOrEmpty(include.File) && include.InHeader)
100-
includes.Add(include.ToString());
99+
if (!string.IsNullOrEmpty(include.File) && include.InHeader)
100+
{
101+
includes.Add(Context.Options.GetIncludePath(typeRefUnit));
102+
}
101103
}
102104

103105
foreach (var include in includes)

src/Generator/Generators/C/CppSources.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ public override void Process()
2828
{
2929
GenerateFilePreamble(CommentKind.BCPL);
3030

31-
var file = Path.GetFileNameWithoutExtension(TranslationUnit.FileName)
32-
.Replace('\\', '/');
33-
34-
if (Context.Options.GenerateName != null)
35-
file = Context.Options.GenerateName(TranslationUnit);
36-
3731
PushBlock(BlockKind.Includes);
38-
WriteLine("#include \"{0}.h\"", file);
32+
var file = Context.Options.GetIncludePath(TranslationUnit);
33+
WriteLine($"#include \"{file}\"");
3934
GenerateForwardReferenceHeaders();
4035

4136
NewLine();

src/Generator/Generators/C/QuickJS/QuickJSModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public override void Process()
105105
public static string GetIncludeFileName(BindingContext context,
106106
TranslationUnit unit)
107107
{
108+
// TODO: Replace with GetIncludePath
108109
string file;
109110
if (context.Options.GenerateName != null)
110111
file = context.Options.GenerateName(unit);

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ public override void Process()
2727
{
2828
GenerateFilePreamble(CommentKind.BCPL);
2929

30-
var file = Path.GetFileNameWithoutExtension(TranslationUnit.FileName)
31-
.Replace('\\', '/');
32-
33-
if (Context.Options.GenerateName != null)
34-
file = Context.Options.GenerateName(TranslationUnit);
35-
3630
PushBlock(BlockKind.Includes);
37-
WriteLine("#include \"{0}.h\"", file);
31+
var file = Context.Options.GetIncludePath(TranslationUnit);
32+
WriteLine($"#include \"{file}\"");
3833
GenerateForwardReferenceHeaders();
3934

4035
NewLine();

src/Generator/Generators/CLI/CLITypeReferences.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public CLITypeReference GetTypeReference(Declaration decl)
5959

6060
@ref.Include = new CInclude
6161
{
62-
File = GetIncludePath(translationUnit),
62+
File = DriverOptions.GetIncludePath(translationUnit),
6363
TranslationUnit = translationUnit,
6464
Kind = translationUnit.IsGenerated ? CInclude.IncludeKind.Quoted : CInclude.IncludeKind.Angled
6565
};
@@ -145,22 +145,6 @@ private bool ShouldIncludeTranslationUnit(TranslationUnit unit)
145145
return !unit.IsSystemHeader && unit.IsValid && !unit.Ignore;
146146
}
147147

148-
private string GetIncludePath(TranslationUnit translationUnit)
149-
{
150-
if (!DriverOptions.UseHeaderDirectories && DriverOptions.GenerateName != null)
151-
{
152-
var extension = Path.GetExtension(TranslationUnit.FileName);
153-
return $"{DriverOptions.GenerateName(translationUnit)}{extension}";
154-
}
155-
else if (DriverOptions.UseHeaderDirectories)
156-
{
157-
var path = Path.Combine(translationUnit.FileRelativeDirectory, translationUnit.FileName);
158-
return path;
159-
}
160-
161-
return translationUnit.FileName;
162-
}
163-
164148
private bool IsBuiltinTypedef(Declaration decl)
165149
{
166150
var typedefDecl = decl as TypedefDecl;

src/Generator/Generators/ExtensionMethods.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Collections.Generic;
3+
using System.IO;
34
using CppSharp.AST;
45
using CppSharp.AST.Extensions;
56
using CppSharp.Types;
@@ -72,5 +73,21 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps,
7273

7374
return type.Desugar();
7475
}
76+
77+
public static string GetIncludePath(this DriverOptions driverOptions, TranslationUnit translationUnit)
78+
{
79+
if (driverOptions.GenerateName != null)
80+
{
81+
var extension = Path.GetExtension(translationUnit.FileName);
82+
return $"{driverOptions.GenerateName(translationUnit)}{extension}";
83+
}
84+
else if (driverOptions.UseHeaderDirectories)
85+
{
86+
var path = Path.Combine(translationUnit.FileRelativeDirectory, translationUnit.FileName);
87+
return path;
88+
}
89+
90+
return translationUnit.FileName;
91+
}
7592
}
7693
}

0 commit comments

Comments
 (0)