diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index fbd71357..c62b558c 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -85,6 +85,7 @@ namespace Cxxi Transform.Preprocess(Library); var passes = new PassBuilder(Library); + passes.CleanUnit(); passes.SortDeclarations(); passes.ResolveIncompleteDecls(TypeDatabase); passes.CheckFlagEnums(); diff --git a/src/Generator/Passes/CleanUnitPass.cs b/src/Generator/Passes/CleanUnitPass.cs new file mode 100644 index 00000000..23132a0e --- /dev/null +++ b/src/Generator/Passes/CleanUnitPass.cs @@ -0,0 +1,45 @@ +namespace Cxxi.Passes +{ + public class CleanUnitPass : TranslationUnitPass + { + public DriverOptions Options; + public PassBuilder Passes; + + public override bool VisitTranslationUnit(TranslationUnit unit) + { + // Try to get an include path that works from the original include + // directories paths. + + unit.IncludePath = GetIncludePath(unit.FilePath); + return true; + } + + string GetIncludePath(string filePath) + { + var includePath = filePath; + var shortestIncludePath = filePath; + + foreach (var path in Options.IncludeDirs) + { + int idx = filePath.IndexOf(path, System.StringComparison.Ordinal); + if (idx == -1) continue; + + string inc = filePath.Substring(path.Length); + + if (inc.Length < includePath.Length && inc.Length < shortestIncludePath.Length) + shortestIncludePath = inc; + } + + return Options.IncludePrefix + shortestIncludePath.TrimStart(new char[] { '\\', '/' }); + } + } + + public static class CleanUnitPassExtensions + { + public static void CleanUnit(this PassBuilder builder) + { + var pass = new CleanUnitPass(); + builder.AddPass(pass); + } + } +} \ No newline at end of file