Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add test for recursive headerscanning, and refactor

  • Loading branch information...
commit 8fcbfed2356b52e44a86978467bfc7438c999122 1 parent e45502b
Lucas Meijer authored
View
29 CCompilerTask.cs
@@ -1,5 +1,4 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using bs.Tests;
namespace bs
@@ -8,37 +7,25 @@ public class CCompilerTask
{
private readonly string _targetFile;
private readonly string _sourceFile;
+ private readonly string[] _includePaths;
- public CCompilerTask(string targetFile, string sourceFile)
+ public CCompilerTask(string targetFile, string sourceFile, string[] includePaths)
{
_targetFile = targetFile;
_sourceFile = sourceFile;
+ _includePaths = includePaths;
}
public IEnumerable<string> GetInputFiles()
{
+ var scanner = new RecursiveIncludeScanner(_includePaths, new IncludeScanner());
yield return _sourceFile;
- foreach (var file in GetFilesIncludedBy(_sourceFile))
+ foreach (var file in scanner.GetFilesIncludedBy(_sourceFile))
yield return file;
}
- private static IEnumerable<string> GetFilesIncludedBy(string file)
- {
- var includeScanner = new IncludeScanner();
- foreach(var includedFile in includeScanner.Scan(file))
- {
- var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(includedFile);
- if (foundIncludeFile == null)
- throw new InvalidOperationException("unable to find the header file: "+includedFile+" in the header search paths.");
- yield return foundIncludeFile;
- foreach (var file2 in GetFilesIncludedBy(foundIncludeFile))
- yield return file2;
- }
- }
+
- private static string FindSpecifiedIncludeFileInSearchDirs(string includedFile)
- {
- return includedFile;
- }
+
}
}
View
7 IncludeScanner.cs
@@ -1,17 +1,18 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace bs
{
- internal class IncludeScanner
+ public class IncludeScanner
{
- Regex _regex = new Regex("^[ ]*#[ ]*include[ ]*[<\"]([^\">]*)[\">].*$", RegexOptions.Multiline);
+ static readonly Regex _regex = new Regex("^[ ]*#[ ]*include[ ]*[<\"]([^\">]*)[\">].*$", RegexOptions.Multiline);
public IEnumerable<string> Scan(string file)
{
- yield break;
+ return ScanText(File.ReadAllText(file));
}
public IEnumerable<string> ScanText(string fileContents)
View
35 RecursiveIncludeScanner.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+
+namespace bs
+{
+ public class RecursiveIncludeScanner
+ {
+ private readonly string[] _includePaths;
+ private readonly IncludeScanner _includeScanner;
+
+ public RecursiveIncludeScanner(string[] includePaths, IncludeScanner scanner)
+ {
+ _includePaths = includePaths;
+ _includeScanner = scanner;
+ }
+
+ public IEnumerable<string> GetFilesIncludedBy(string file)
+ {
+ foreach (var includedFile in _includeScanner.Scan(file))
+ {
+ var foundIncludeFile = FindSpecifiedIncludeFileInSearchDirs(includedFile);
+ if (foundIncludeFile == null)
+ throw new InvalidOperationException("unable to find the header file: " + includedFile + " in the header search paths.");
+ yield return foundIncludeFile;
+ foreach (var file2 in GetFilesIncludedBy(foundIncludeFile))
+ yield return file2;
+ }
+ }
+
+ private string FindSpecifiedIncludeFileInSearchDirs(string includedFile)
+ {
+ return includedFile;
+ }
+ }
+}
View
13 Tests/CCompilerTests.cs
@@ -1,8 +1,5 @@
using System.IO;
-using System.Linq;
-using System.Text;
using NUnit.Framework;
-using System.Collections.Generic;
namespace bs.Tests
{
@@ -10,13 +7,15 @@ namespace bs.Tests
public class CCompilerTests
{
[Test]
- public void Test()
+ public void GetInputFilesFindsRecursiveHeaderDependencies()
{
- File.WriteAllText("test.c", "void main() {}");
- var task = new CCompilerTask("test.o", "test.c");
+ File.WriteAllText("test.c", "#include <test.h>\nvoid main() {}");
+ File.WriteAllText("test.h", "#include <shared.h>");
+ File.WriteAllText("shared.h", "//like a boss");
+ var task = new CCompilerTask("test.o", "test.c", new[] {""});
var inputfiles = task.GetInputFiles();
- CollectionAssert.AreEquivalent(new List<string> {"test.c"}, inputfiles);
+ CollectionAssert.AreEquivalent(new [] {"test.c","test.h","shared.h"}, inputfiles);
}
}
}
View
6 Tests/IncludeScannerTest.cs
@@ -25,6 +25,12 @@ public void CanFindMultipleIncludes()
RunTest("#include <windows.h>\n#include \"myfolder/myfile.h\"", new[] { "windows.h", "myfolder/myfile.h" });
}
+ [Test]
+ public void CorrectlyParseRealHardOne()
+ {
+ RunTest("#include <time.h> /* XXX should use <ctime> */", new[] { "time.h" });
+ }
+
private static void RunTest(string fileContents, IEnumerable<string> expected)
{
var scanner = new IncludeScanner();
View
1  bs.csproj
@@ -49,6 +49,7 @@
<Compile Include="GenerationRecord.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="RecursiveIncludeScanner.cs" />
<Compile Include="TargetBuildInstructions.cs" />
<Compile Include="TargetBuildSettings.cs" />
<Compile Include="CCompilerTask.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.