Permalink
Browse files

add test for recursive headerscanning, and refactor

  • Loading branch information...
1 parent e45502b commit 8fcbfed2356b52e44a86978467bfc7438c999122 Lucas Meijer committed Apr 14, 2012
Showing with 60 additions and 31 deletions.
  1. +8 −21 CCompilerTask.cs
  2. +4 −3 IncludeScanner.cs
  3. +35 −0 RecursiveIncludeScanner.cs
  4. +6 −7 Tests/CCompilerTests.cs
  5. +6 −0 Tests/IncludeScannerTest.cs
  6. +1 −0 bs.csproj
View
@@ -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
@@ -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
@@ -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
@@ -1,22 +1,21 @@
using System.IO;
-using System.Linq;
-using System.Text;
using NUnit.Framework;
-using System.Collections.Generic;
namespace bs.Tests
{
[TestFixture]
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);
}
}
}
@@ -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
@@ -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" />

0 comments on commit 8fcbfed

Please sign in to comment.