Skip to content

Commit 080684d

Browse files
authored
v: support @DIR (as a comptime equivalent to os.dir(@file)) at runtime) (#24742)
1 parent e14853d commit 080684d

File tree

9 files changed

+51
-3
lines changed

9 files changed

+51
-3
lines changed

doc/docs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6052,6 +6052,7 @@ that are substituted at compile time:
60526052
- `@MOD` => replaced with the name of the current V module.
60536053
- `@STRUCT` => replaced with the name of the current V struct.
60546054
- `@FILE` => replaced with the absolute path of the V source file.
6055+
- `@DIR` => replaced with the absolute path of the *folder*, where the V source file is.
60556056
- `@LINE` => replaced with the V line number where it appears (as a string).
60566057
- `@FILE_LINE` => like `@FILE:@LINE`, but the file part is a relative path.
60576058
- `@LOCATION` => file, line and name of the current type + method; suitable for logging.

vlib/v/checker/checker.v

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,13 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
26362636
'include', 'insert', 'preinclude', 'postinclude' {
26372637
original_flag := node.main
26382638
mut flag := node.main
2639+
if flag.contains('@DIR') {
2640+
vdir := c.dir_path()
2641+
val := flag.replace('@DIR', vdir)
2642+
node.val = '${node.kind} ${val}'
2643+
node.main = val
2644+
flag = val
2645+
}
26392646
if flag.contains('@VROOT') {
26402647
// c.note(checker.vroot_is_deprecated_message, node.pos)
26412648
vroot := util.resolve_vmodroot(flag.replace('@VROOT', '@VMODROOT'), c.file.path) or {
@@ -2742,6 +2749,10 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
27422749
return
27432750
}
27442751
}
2752+
if flag.contains('@DIR') {
2753+
// expand `@DIR` to its absolute path
2754+
flag = flag.replace('@DIR', c.dir_path())
2755+
}
27452756
if flag.contains('@VEXEROOT') {
27462757
// expand `@VEXEROOT` to its absolute path
27472758
flag = flag.replace('@VEXEROOT', c.pref.vroot)
@@ -3914,6 +3925,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
39143925
.file_path {
39153926
node.val = os.real_path(c.file.path)
39163927
}
3928+
.file_dir {
3929+
node.val = os.real_path(os.dir(c.file.path))
3930+
}
39173931
.line_nr {
39183932
node.val = (node.pos.line_nr + 1).str()
39193933
}
@@ -5733,3 +5747,7 @@ pub fn (mut c Checker) update_unresolved_fixed_sizes() {
57335747
}
57345748
}
57355749
}
5750+
5751+
fn (mut c Checker) dir_path() string {
5752+
return os.real_path(os.dir(c.file.path))
5753+
}

vlib/v/parser/comptime.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ fn (mut p Parser) at() ast.AtExpr {
466466
'@MOD' { token.AtKind.mod_name }
467467
'@STRUCT' { token.AtKind.struct_name }
468468
'@FILE' { token.AtKind.file_path }
469+
'@DIR' { token.AtKind.file_dir }
469470
'@LINE' { token.AtKind.line_nr }
470471
'@FILE_LINE' { token.AtKind.file_path_line_nr }
471472
'@LOCATION' { token.AtKind.location }

vlib/v/scanner/tests/unknown_comptime_var_err.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ vlib/v/scanner/tests/unknown_comptime_var_err.vv:2:9: error: @ must be used befo
44
| ^
55
3 | }
66
Details: available compile time variables: @VROOT, @VMODROOT, @VEXEROOT, @FN, @METHOD, @MOD,
7-
@STRUCT, @VEXE, @FILE, @LINE, @COLUMN, @VHASH, @VCURRENTHASH, @VMOD_FILE, @VMODHASH,
7+
@STRUCT, @VEXE, @FILE, @DIR, @LINE, @COLUMN, @VHASH, @VCURRENTHASH, @VMOD_FILE, @VMODHASH,
88
@FILE_LINE, @LOCATION, @BUILD_DATE, @BUILD_TIME, @BUILD_TIMESTAMP

vlib/v/tests/comptime/comptime_at_test.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ fn test_at_file() {
8383
assert f == 'comptime_at_test.v'
8484
}
8585

86+
fn test_at_dir() {
87+
// Test @DIR
88+
f := os.file_name(@DIR)
89+
assert f == 'comptime'
90+
assert os.dir(@FILE) == @DIR
91+
d := @DIR
92+
assert d.len > 0
93+
assert !d.ends_with('.v')
94+
}
95+
8696
fn test_at_file_len() {
8797
// Test @FILE_LINE
8898
line1, line2 := '${@LINE}', '${@FILE_LINE}'

vlib/v/tests/project_with_c_code/main.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ module main
22

33
import v.tests.project_with_c_code.mod1
44

5+
#include "@DIR/relative.h"
6+
7+
fn C.abc() int
8+
59
fn main() {
610
res := mod1.vadd(1, 2)
711
println(res)
812
assert res == 1003
13+
res2 := C.abc()
14+
assert res2 == 142
15+
dump(res2)
916
}

vlib/v/tests/project_with_c_code/main1_test.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// vtest retry: 3
33
import v.tests.project_with_c_code.mod1
44

5+
#include "@DIR/relative.h"
6+
7+
fn C.abc() int
8+
59
fn test_using_c_code_in_the_same_module_works() {
610
assert 1003 == mod1.vadd(1, 2)
11+
assert 142 == C.abc()
712
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int abc() {
2+
return 142;
3+
}
4+
5+

vlib/v/token/token.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ pub enum AtKind {
174174
struct_name
175175
vexe_path
176176
file_path
177+
file_dir
177178
line_nr
178179
column_nr
179180
vhash
@@ -195,8 +196,8 @@ pub const assign_tokens = [Kind.assign, .decl_assign, .plus_assign, .minus_assig
195196
.left_shift_assign, .unsigned_right_shift_assign, .boolean_and_assign, .boolean_or_assign]
196197

197198
pub const valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT',
198-
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@VMODHASH',
199-
'@FILE_LINE', '@LOCATION', '@BUILD_DATE', '@BUILD_TIME', '@BUILD_TIMESTAMP']
199+
'@VEXE', '@FILE', '@DIR', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE',
200+
'@VMODHASH', '@FILE_LINE', '@LOCATION', '@BUILD_DATE', '@BUILD_TIME', '@BUILD_TIMESTAMP']
200201

201202
pub const token_str = build_token_str()
202203

0 commit comments

Comments
 (0)