diff --git a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs index bd6b10fb..ad796a1c 100644 --- a/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs +++ b/Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs @@ -81,6 +81,16 @@ int test() { }", new() { ["foo.h"] = "#include ", ["bar.h"] = "#include ", ["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() diff --git a/Cesium.Parser.Tests/PreprocessorTests/verified/PreprocessorTests.IgnoredInclude.verified.txt b/Cesium.Parser.Tests/PreprocessorTests/verified/PreprocessorTests.IgnoredInclude.verified.txt new file mode 100644 index 00000000..9e944f14 --- /dev/null +++ b/Cesium.Parser.Tests/PreprocessorTests/verified/PreprocessorTests.IgnoredInclude.verified.txt @@ -0,0 +1,3 @@ + + +test diff --git a/Cesium.Preprocessor/CPreprocessor.cs b/Cesium.Preprocessor/CPreprocessor.cs index 8ec55484..0c6e1d27 100644 --- a/Cesium.Preprocessor/CPreprocessor.cs +++ b/Cesium.Preprocessor/CPreprocessor.cs @@ -460,27 +460,30 @@ IEnumerable> 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>(); - if (IncludeTokens) + var includeFilePath = LookUpIncludeFile(filePath); + if (!IncludeContext.ShouldIncludeFile(includeFilePath)) { - var includeFilePath = LookUpIncludeFile(filePath); - if (!IncludeContext.ShouldIncludeFile(includeFilePath)) - { - return Array.Empty>(); - } + return Array.Empty>(); + } - 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;