Skip to content

Commit

Permalink
cgen: optimize out the empty #ifdef NOT_CURRENT_TARGET_OS #endif output
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Apr 2, 2021
1 parent 6a5f49a commit 53cbdbc
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module c
import os
import v.ast
import v.util
import v.pref

fn (mut g Gen) comptime_selector(node ast.ComptimeSelector) {
g.expr(node.left)
Expand Down Expand Up @@ -188,6 +189,27 @@ fn (mut g Gen) comp_at(node ast.AtExpr) {
}

fn (mut g Gen) comp_if(node ast.IfExpr) {
if !node.is_expr && !node.has_else && node.branches.len == 1 {
if node.branches[0].stmts.len == 0 {
// empty ifdef; result of target OS != conditional => skip
return
}
if !g.pref.output_cross_c {
if node.branches[0].cond is ast.Ident {
if g.pref.os == (pref.os_from_string(node.branches[0].cond.name) or {
pref.OS._auto
}) {
// Same target OS as the conditional...
// => skip the #if defined ... #endif wrapper
// and just generate the branch statements:
g.indent--
g.stmts(node.branches[0].stmts)
g.indent++
return
}
}
}
}
line := if node.is_expr {
stmt_str := g.go_before_stmt(0)
g.write(util.tabs(g.indent))
Expand Down

0 comments on commit 53cbdbc

Please sign in to comment.