Skip to content

Commit 0d8b236

Browse files
committed
Ignore external (no module) translation units
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 851ec5e commit 0d8b236

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

src/Generator.Tests/ASTTestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ protected void ParseLibrary(params string[] files)
1818
var parserOptions = new ParserOptions();
1919

2020
var testsPath = GeneratorTest.GetTestsDirectory("Native");
21-
parserOptions.AddIncludeDirs(testsPath);
2221
parserOptions.SkipPrivateDeclarations = true;
2322

2423
var module = options.AddModule("Test");
24+
module.IncludeDirs.Add(testsPath);
2525
module.Headers.AddRange(files);
2626

2727
Driver = new Driver(options)

src/Generator.Tests/GeneratorTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ public virtual void Setup(Driver driver)
4242
testModule.IncludeDirs.Add(path);
4343

4444
Diagnostics.Message("Looking for tests in: {0}", path);
45-
var files = Directory.EnumerateFiles(path, "*.h");
45+
var files = Directory.EnumerateFiles(path, "*.h", SearchOption.AllDirectories);
4646
foreach (var file in files)
47+
{
48+
string includeDir = Path.GetDirectoryName(file);
49+
if (!testModule.IncludeDirs.Contains(includeDir))
50+
{
51+
testModule.IncludeDirs.Add(includeDir);
52+
}
4753
testModule.Headers.Add(Path.GetFileName(file));
54+
}
4855
}
4956

5057
public virtual void Preprocess(Driver driver, ASTContext ctx)

src/Generator/Passes/CleanUnitPass.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ private Module GetModule(TranslationUnit unit)
3131
includeDir = ".";
3232
includeDir = Path.GetFullPath(includeDir);
3333

34-
return Options.Modules.FirstOrDefault(
35-
m => m.IncludeDirs.Any(i => Path.GetFullPath(i) == includeDir)) ??
36-
Options.Modules[1];
34+
Module module = Options.Modules.Find(
35+
m => m.IncludeDirs.Any(i => Path.GetFullPath(i) == includeDir));
36+
if (module == null)
37+
{
38+
unit.ExplicitlyIgnore();
39+
module = Options.Modules[1];
40+
}
41+
return module;
3742
}
3843

3944
public override bool VisitDeclarationContext(DeclarationContext context)
4045
{
4146
return false;
4247
}
4348

44-
string GetIncludePath(string filePath)
49+
private string GetIncludePath(string filePath)
4550
{
4651
var includePath = filePath;
47-
var shortestIncludePath = filePath;
4852

4953
for (uint i = 0; i < Context.ParserOptions.IncludeDirsCount; ++i)
5054
{
@@ -57,16 +61,15 @@ string GetIncludePath(string filePath)
5761
idx = filePath.IndexOf(path, System.StringComparison.Ordinal);
5862
}
5963

60-
if (idx == -1) continue;
61-
62-
string inc = filePath.Substring(path.Length);
63-
64-
if (inc.Length < includePath.Length && inc.Length < shortestIncludePath.Length)
65-
shortestIncludePath = inc;
64+
if (idx != -1)
65+
{
66+
includePath = filePath[path.Length..];
67+
break;
68+
}
6669
}
6770

6871
includePath = Options.IncludePrefix
69-
+ shortestIncludePath.TrimStart(new char[] { '\\', '/' });
72+
+ includePath.TrimStart(new char[] { '\\', '/' });
7073

7174
return includePath.Replace('\\', '/');
7275
}

src/Generator/Passes/DelegatesPass.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ private DeclarationContext GetDeclContextForDelegates(DeclarationContext @namesp
215215
Namespace parent = null;
216216
if (string.IsNullOrEmpty(module.OutputNamespace))
217217
{
218-
var groups = module.Units.SelectMany(u => u.Declarations).OfType<Namespace>(
218+
var groups = module.Units.Where(u => u.IsGenerated).SelectMany(
219+
u => u.Declarations).OfType<Namespace>(
219220
).GroupBy(d => d.Name).Where(g => g.Any(d => d.HasDeclarations)).ToList();
220221
if (groups.Count == 1)
221222
parent = groups.Last().Last();
@@ -231,7 +232,7 @@ private DeclarationContext GetDeclContextForDelegates(DeclarationContext @namesp
231232
}
232233

233234
if (parent == null)
234-
parent = module.Units.Last();
235+
parent = module.Units.Last(u => u.IsGenerated);
235236

236237
var namespaceDelegates = new Namespace
237238
{

0 commit comments

Comments
 (0)