Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ jobs:
env:
IGNORE_TESTS: ${{ matrix.ignore_tests }}

- name: Run glsl-lang-pp/additional tests
run: cargo test -p glsl-lang-pp --test additional --features full

- name: Run glsl-lang-pp/glslang tests
run: cargo test -p glsl-lang-pp --test glslang --features full

Expand Down
7 changes: 6 additions & 1 deletion lang-pp/src/processor/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ impl ExpandOne {
errors.push(ProcessingErrorKind::DirectiveElif(error));
}

value
if !self.if_stack.none() {
// Always false if the last if state was activated once
false
} else {
value
}
} else {
// Do not evaluate if the group is not active
true
Expand Down
8 changes: 8 additions & 0 deletions lang-pp/src/processor/expand/if_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl IfState {
}
}

pub fn none(&self) -> bool {
matches!(self, Self::None)
}

pub fn active(&self) -> bool {
matches!(self, Self::Active { .. })
}
Expand Down Expand Up @@ -95,6 +99,10 @@ impl IfStack {
self.stack.last().map(|top| top.active()).unwrap_or(true)
}

pub fn none(&self) -> bool {
self.stack.last().map(|top| top.none()).unwrap_or(false)
}

pub fn on_if_like(&mut self, expr: bool) {
if self.active() && expr {
self.stack.push(IfState::Active { else_seen: false });
Expand Down
55 changes: 55 additions & 0 deletions lang-pp/tests/additional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![cfg(feature = "full")]

use std::path::PathBuf;

use glsl_lang_pp::{
exts::DEFAULT_REGISTRY,
last::{Event, TokenState},
types::Token,
};

#[test]
fn test_issue_61() {
// See https://github.com/alixinne/glsl-lang/pull/61
let path: PathBuf = "../lang-pp/tests/issue_61.glsl".into();
let mut pp = glsl_lang_pp::processor::fs::StdProcessor::default();

let parsed = match pp.parse(&path) {
Ok(inner) => Ok(inner),
Err(err) => {
if err.kind() == std::io::ErrorKind::InvalidData {
std::fs::read(&path).map(|value| {
pp.parse_source(
encoding_rs::WINDOWS_1252.decode(&value).0.as_ref(),
path.parent().unwrap(),
)
})
} else {
Err(err)
}
}
}
.expect("failed to open file");

assert_eq!(
parsed
.into_iter()
.tokenize(300, false, &DEFAULT_REGISTRY)
.filter_map(|token| {
// Extract float constants which are not excluded by the preprocessor
if let Ok(Event::Token {
state: TokenState::Active,
token_kind: Token::FLOAT_CONST(val),
..
}) = token
{
return Some(val);
}

return None;
})
.collect::<Vec<_>>(),
// Only one pp branch should be active, so only one constant is produced
vec![1.0]
);
}
16 changes: 16 additions & 0 deletions lang-pp/tests/issue_61.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 300 es
#define CS1
#define CS2
#define CS3

vec3 test() {
#if defined(CS1)
return 1.0;
#elif defined(CS2)
return 2.0;
#elif defined(CS3)
return 3.0;
#else
return 4.0;
#endif
}
Loading