Skip to content

Commit

Permalink
Deprecate __FILE__ and __LINE__ (#1072)
Browse files Browse the repository at this point in the history
Unlike C, these constants are not convenient for logging in macros,
since they always report the same data (their location in the macro).

Fixes #1068
  • Loading branch information
Rangi42 authored Sep 30, 2022
1 parent 68ad926 commit a47da5f
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 88 deletions.
2 changes: 0 additions & 2 deletions man/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -1346,8 +1346,6 @@ The following symbols are defined by the assembler:
.It Dv @ Ta Ic EQU Ta PC value (essentially, the current memory address)
.It Dv _RS Ta Ic = Ta _RS Counter
.It Dv _NARG Ta Ic EQU Ta Number of arguments passed to macro, updated by Ic SHIFT
.It Dv __LINE__ Ta Ic EQU Ta The current line number
.It Dv __FILE__ Ta Ic EQUS Ta The current filename
.It Dv __DATE__ Ta Ic EQUS Ta Today's date
.It Dv __TIME__ Ta Ic EQUS Ta The current time
.It Dv __ISO_8601_LOCAL__ Ta Ic EQUS Ta ISO 8601 timestamp (local)
Expand Down
7 changes: 6 additions & 1 deletion src/asm/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ static int32_t Callback_NARG(void)

static int32_t Callback__LINE__(void)
{
warning(WARNING_OBSOLETE, "`__LINE__` is deprecated\n");

return lexer_GetLineNo();
}

static char const *Callback__FILE__(void)
{
// FIXME: this is dangerous, and here's why this is CURRENTLY okay. It's still bad, fix it.
warning(WARNING_OBSOLETE, "`__FILE__` is deprecated\n");

// There are only two call sites for this; one copies the contents directly, the other is
// EQUS expansions, which cannot straddle file boundaries. So this should be fine.
static char *buf = NULL;
Expand Down Expand Up @@ -680,7 +683,9 @@ void sym_Init(time_t now)
{
PCSymbol = createBuiltinSymbol("@");
struct Symbol *_NARGSymbol = createBuiltinSymbol("_NARG");
// __LINE__ is deprecated
struct Symbol *__LINE__Symbol = createBuiltinSymbol("__LINE__");
// __FILE__ is deprecated
struct Symbol *__FILE__Symbol = createBuiltinSymbol("__FILE__");

PCSymbol->type = SYM_LABEL;
Expand Down
5 changes: 3 additions & 2 deletions test/asm/builtin-overwrite.asm
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ PRINTLN \1
endm

; Representative numeric and string builtins
tickle __LINE__, 1
tickle __FILE__, 0
; (SOURCE_DATE_EPOCH in test.sh makes this reproducible)
tickle __UTC_YEAR__, 1
tickle __ISO_8601_UTC__, 0
88 changes: 44 additions & 44 deletions test/asm/builtin-overwrite.err
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(7):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(8):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(11):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(12):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(16):
Built-in symbol '__LINE__' cannot be purged
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(17):
Built-in symbol '__LINE__' cannot be purged
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(20):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(21):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(24):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(25):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(28):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(29):
'__LINE__' already defined at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(32):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(33):
'__LINE__' already defined as constant at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(36):
'__LINE__' already defined as non-EQUS at <builtin>
error: builtin-overwrite.asm(42) -> builtin-overwrite.asm::tickle(37):
'__LINE__' already defined as non-EQUS at <builtin>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(7):
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(8):
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(11):
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(12):
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(16):
Built-in symbol '__FILE__' cannot be purged
Built-in symbol '__UTC_YEAR__' cannot be purged
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(17):
Built-in symbol '__FILE__' cannot be purged
Built-in symbol '__UTC_YEAR__' cannot be purged
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(20):
'__FILE__' already defined at <builtin>
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(21):
'__FILE__' already defined at <builtin>
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(24):
'__FILE__' already defined as constant at <builtin>
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(25):
'__FILE__' already defined as constant at <builtin>
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(28):
'__FILE__' already defined at <builtin>
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(29):
'__FILE__' already defined at <builtin>
'__UTC_YEAR__' already defined at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(32):
'__FILE__' already defined as constant at <builtin>
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(33):
'__FILE__' already defined as constant at <builtin>
'__UTC_YEAR__' already defined as constant at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(36):
Built-in symbol '__FILE__' cannot be redefined
'__UTC_YEAR__' already defined as non-EQUS at <command-line>
error: builtin-overwrite.asm(43) -> builtin-overwrite.asm::tickle(37):
Built-in symbol '__FILE__' cannot be redefined
'__UTC_YEAR__' already defined as non-EQUS at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(16):
Built-in symbol '__ISO_8601_UTC__' cannot be purged
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(17):
Built-in symbol '__ISO_8601_UTC__' cannot be purged
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(20):
'__ISO_8601_UTC__' already defined at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(21):
'__ISO_8601_UTC__' already defined at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(24):
'__ISO_8601_UTC__' already defined as constant at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(25):
'__ISO_8601_UTC__' already defined as constant at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(28):
'__ISO_8601_UTC__' already defined at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(29):
'__ISO_8601_UTC__' already defined at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(32):
'__ISO_8601_UTC__' already defined as constant at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(33):
'__ISO_8601_UTC__' already defined as constant at <command-line>
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(36):
Built-in symbol '__ISO_8601_UTC__' cannot be redefined
error: builtin-overwrite.asm(44) -> builtin-overwrite.asm::tickle(37):
Built-in symbol '__ISO_8601_UTC__' cannot be redefined
error: Assembly aborted (28 errors)!
28 changes: 14 additions & 14 deletions test/asm/builtin-overwrite.out
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
$9
$D
$12
$16
$1A
$1E
$22
$26
builtin-overwrite.asm
builtin-overwrite.asm
builtin-overwrite.asm
builtin-overwrite.asm
builtin-overwrite.asm
builtin-overwrite.asm
$7C5
$7C5
$7C5
$7C5
$7C5
$7C5
$7C5
$7C5
1989-04-21T12:34:56Z
1989-04-21T12:34:56Z
1989-04-21T12:34:56Z
1989-04-21T12:34:56Z
1989-04-21T12:34:56Z
1989-04-21T12:34:56Z
3 changes: 0 additions & 3 deletions test/asm/compound-assignment.asm
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@ endm
_RS += 100
println _RS

__LINE__ *= 200
println __LINE__

UnDeFiNeD ^= 300
println UnDeFiNeD
4 changes: 1 addition & 3 deletions test/asm/compound-assignment.err
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
error: compound-assignment.asm(36):
'__LINE__' already defined as constant at <builtin>
error: compound-assignment.asm(39):
Expected constant expression: 'UnDeFiNeD' is not constant at assembly time
error: Assembly aborted (2 errors)!
error: Assembly aborted (1 error)!
1 change: 0 additions & 1 deletion test/asm/compound-assignment.out
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ $5
$14
$A
$64
$25
$0
2 changes: 2 additions & 0 deletions test/asm/file-sym.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
warning: file-sym.asm(1): [-Wobsolete]
`__FILE__` is deprecated
2 changes: 1 addition & 1 deletion test/asm/preinclude.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
warn "main {__FILE__}"
warn "main file"
def v3 = v1 + v2
println "{d:v1} + {d:v2} = {d:v3}"
4 changes: 2 additions & 2 deletions test/asm/preinclude.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: preinclude.asm(0) -> preinclude.inc(1): [-Wuser]
pre-include "preinclude.inc"
pre-included file
warning: preinclude.asm(1): [-Wuser]
main "preinclude.asm"
main file
2 changes: 1 addition & 1 deletion test/asm/preinclude.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warn "pre-include {__FILE__}"
warn "pre-included file"

def v1 = 12
rept 3
Expand Down
17 changes: 3 additions & 14 deletions test/asm/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export LC_ALL=C

# Game Boy release date, 1989-04-21T12:34:56Z (for reproducible test results)
export SOURCE_DATE_EPOCH=609165296

o="$(mktemp)"
gb="$(mktemp)"
input="$(mktemp)"
Expand Down Expand Up @@ -52,20 +55,6 @@ else
rm -f version.asm
fi

# Add the quote test, except on Windows
if uname | grep -viq mingw; then
cat > quote\"file.asm <<EOF
WARN __FILE__
EOF
cat > quote\"file.out <<EOF
EOF
cat > quote\"file.err <<EOF
warning: quote"file.asm(1): [-Wuser]
quote"file.asm
while expanding symbol "__FILE__"
EOF
fi

# Check whether to use '.simple.err' files if they exist
# (rgbasm with pre-3.0 Bison just reports "syntax error")
$RGBASM -Weverything -o $o syntax-error.asm > $output 2> $errput
Expand Down

0 comments on commit a47da5f

Please sign in to comment.