Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: __SOURCE__ constant like __FILE__, __LINE__, __FUNCTION__ C++ macro #37620

Open
kokizzu opened this issue Mar 3, 2020 · 7 comments
Open

Comments

@kokizzu
Copy link

@kokizzu kokizzu commented Mar 3, 2020

those macros are very useful for debugging and caching:

__SOURCE__ = __FILE__:__LINE__ __FUNCTION__
/foo/bar.go:123 foo.Bar.HomeHandler(Context)

usage example

func (b *Bar) HomeHandler(ctx *Context) {
    res := DoQuery(__SOURCE__, sql, parameters)
    ...
}
func DoQuery(source, sql string, args ...interface{}) *sql.Rows {
    // cache using __SOURCE__ and args
    sql = ` -- ` + __SOURCE__ + `
    ` + sql
    ...
}

on code above, if there's sql error, we can know who's the caller directly if there's SQL error (without needing to access runtime.Stack/FuncForPC (which i currently use), this is a bit similar to C# [System.Runtime.CompilerServices.CallerMemberName], [System.Runtime.CompilerServices.CallerFilePath], [System.Runtime.CompilerServices.CallerLineNumber] but works on compile time instead of runtime.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 3, 2020

You can get all of this information by calling runtime.Caller.

We could choose to implement runtime.Caller directly in the compiler if that seems to be useful.

@kokizzu

This comment has been minimized.

Copy link
Author

@kokizzu kokizzu commented Mar 3, 2020

i think if the string generated at compile time (then sent to the other function) would be more efficient than inspecting stack trace at runtime.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 3, 2020

I'm saying that if this seems important then we could implement runtime.Caller in the compiler such that the string is determined at compile time.

It's not obvious to me that we need two different ways to get the same information.

@dmitshur dmitshur added this to the Backlog milestone Mar 3, 2020
@dmitshur dmitshur changed the title feature request: __SOURCE__ constant like __FILE__, __LINE__, __FUNCTION__ C++ macro cmd/compile: __SOURCE__ constant like __FILE__, __LINE__, __FUNCTION__ C++ macro Mar 3, 2020
@kokizzu

This comment has been minimized.

Copy link
Author

@kokizzu kokizzu commented Mar 3, 2020

but i'm really not sure if we could get info about the caller at compile time '__')

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 4, 2020

The compiler can trivially compute the file and line results of runtime.Caller(0) at compile time.

It takes a little more work to trace the PC result passed into runtime.FuncForPC, but that too is doable.

@kokizzu

This comment has been minimized.

Copy link
Author

@kokizzu kokizzu commented Mar 4, 2020

I mean, if there's function A and B that calls function C, wouldn't that be require runtime instead of compile time?

func A() { C() }
func B() { C() }
func C() { can be A or B }

also if using __SOURCE__ it's more controllable, since we could do something like this:

func A() { B(__SOURCE__) }
func X() { B(__SOURCE__) }
func B(src) { C(src) }
func C(src) { use the src } // which can be A or X
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 5, 2020

func A() {
    _, source, _, _ := runtime.Caller(0)
    B(source)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.