v: support `$if prod` (#9228)

pull/9238/head
zakuro 2021-03-11 02:26:34 +09:00 committed by GitHub
parent 99abd46ac9
commit 791dec7b01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 6 deletions

View File

@ -3516,6 +3516,10 @@ $if test {
$if debug {
println('debugging')
}
// v -prod ...
$if prod {
println('production build')
}
// v -d option ...
$if option ? {
println('custom option')
@ -3529,8 +3533,8 @@ If you're using a custom ifdef, then you do need `$if option ? {}` and compile w
Full list of builtin options:
| OS | Compilers | Platforms | Other |
| --- | --- | --- | --- |
| `windows`, `linux`, `macos` | `gcc`, `tinyc` | `amd64`, `aarch64` | `debug`, `test`, `js` |
| `mac`, `darwin`, `ios`, | `clang`, `mingw` | `x64`, `x32` | `glibc`, `prealloc` |
| `windows`, `linux`, `macos` | `gcc`, `tinyc` | `amd64`, `aarch64` | `debug`, `prod`, `test` |
| `mac`, `darwin`, `ios`, | `clang`, `mingw` | `x64`, `x32` | `js`, `glibc`, `prealloc` |
| `android`,`mach`, `dragonfly` | `msvc` | `little_endian` | `no_bounds_checking` |
| `gnu`, `hpux`, `haiku`, `qnx` | `cplusplus` | `big_endian` | |
| `solaris`, `linux_or_macos` | | | |

View File

@ -27,7 +27,7 @@ const (
]
valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
valid_comp_if_platforms = ['amd64', 'aarch64', 'x64', 'x32', 'little_endian', 'big_endian']
valid_comp_if_other = ['js', 'debug', 'test', 'glibc', 'prealloc', 'no_bounds_checking']
valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking']
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort',
'contains', 'index', 'wait']
)
@ -5150,6 +5150,7 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool {
match cond.name {
'js' { return c.pref.backend != .js }
'debug' { return !c.pref.is_debug }
'prod' { return !c.pref.is_prod }
'test' { return !c.pref.is_test }
'glibc' { return false } // TODO
'prealloc' { return !c.pref.prealloc }

View File

@ -423,6 +423,9 @@ pub fn (mut g Gen) init() {
if g.pref.is_debug || 'debug' in g.pref.compile_defines {
g.comptime_defines.writeln('#define _VDEBUG (1)')
}
if g.pref.is_prod || 'prod' in g.pref.compile_defines {
g.comptime_defines.writeln('#define _VPROD (1)')
}
if g.pref.is_test || 'test' in g.pref.compile_defines {
g.comptime_defines.writeln('#define _VTEST (1)')
}

View File

@ -498,6 +498,9 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string
'debug' {
return '_VDEBUG'
}
'prod' {
return '_VPROD'
}
'test' {
return '_VTEST'
}

View File

@ -17,4 +17,11 @@ fn get_st() MyStruct {
fn main() {
s := get_st()
println(s)
$if prod {
println('prod mode is on')
assert true
} $else {
println('prod mode is off')
assert false
}
}

View File

@ -1,3 +1,4 @@
MyStruct{
s: '6'
}
prod mode is on