Skip to content

Commit

Permalink
(#61) Preprocessor: ignore #include directives in disabled blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 17, 2024
1 parent 9b57451 commit 408af79
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ int test()
{
}", new() { ["foo.h"] = "#include <bar.h>", ["bar.h"] = "#include <baz.h>", ["baz.h"] = "int bar = 0;" });

[Fact]
public Task IgnoredInclude() => DoTest("""
#ifdef MY_INCLUDE
#include MY_INCLUDE
#endif
#ifndef MY_INCLUDE
test
#endif
""");

[Fact]
public Task PragmaOnce() => DoTest(@"
int test()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


test
35 changes: 19 additions & 16 deletions Cesium.Preprocessor/CPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,27 +460,30 @@ IEnumerable<IToken<CPreprocessorTokenType>> ConsumeLineAll()
{
case "include":
{
// If in disabled block, do not attempt to include files.
if (!IncludeTokens)
{
// Ignore everything after #include in a disabled block
foreach (var _ in ConsumeLineAll()) {}
return [];
}

var filePath = ConsumeNext(HeaderName).Text;
var tokensList = new List<IToken<CPreprocessorTokenType>>();
if (IncludeTokens)
var includeFilePath = LookUpIncludeFile(filePath);
if (!IncludeContext.ShouldIncludeFile(includeFilePath))
{
var includeFilePath = LookUpIncludeFile(filePath);
if (!IncludeContext.ShouldIncludeFile(includeFilePath))
{
return Array.Empty<IToken<CPreprocessorTokenType>>();
}
return Array.Empty<IToken<CPreprocessorTokenType>>();
}

if (!File.Exists(includeFilePath))
{
Console.Error.WriteLine($"Cannot find path to {filePath} during parsing {CompilationUnitPath}");
}
if (!File.Exists(includeFilePath))
{
Console.Error.WriteLine($"Cannot find path to {filePath} during parsing {CompilationUnitPath}");
}

using var reader = IncludeContext.OpenFileStream(includeFilePath);
await foreach (var token in ProcessInclude(includeFilePath, reader))
{
tokensList.Add(token);
}
using var reader = IncludeContext.OpenFileStream(includeFilePath);
await foreach (var token in ProcessInclude(includeFilePath, reader))
{
tokensList.Add(token);
}

bool hasRemaining;
Expand Down

0 comments on commit 408af79

Please sign in to comment.