digitalmars.D.learn - Is it possible to set/override the name of the source file when piping
- Siarhei Siamashka (17/17) Dec 13 2023 Example:
- Adam D Ruppe (6/9) Dec 13 2023 the sequence `#line "filename.d" 1` at the top of the thing might
- Siarhei Siamashka (30/39) Dec 13 2023 Thanks a lot! Looks like it does the job:
- H. S. Teoh (11/31) Dec 13 2023 Add a module declaration to your source file. For example:
- H. S. Teoh (8/18) Dec 13 2023 [...]
Example: ```D import std; void main() { deliberate syntax error here } ``` ```bash $ cat example.d | dmd -run - __stdin.d(3): Error: found `error` when expecting `;` or `=`, did you mean `deliberate syntax = here`? __stdin.d(3): Error: found `}` when expecting `;` or `=`, did you mean `error here = End of File`? ``` Now I'm curious. Is it possible to somehow communicate the real source file name to `dmd`, so that it shows up in the error log instead of "__stdin.d"?
Dec 13 2023
On Wednesday, 13 December 2023 at 19:37:09 UTC, Siarhei Siamashka wrote:Now I'm curious. Is it possible to somehow communicate the real source file name to `dmd`, so that it shows up in the error log instead of "__stdin.d"?the sequence `#line "filename.d" 1` at the top of the thing might do what you need. https://dlang.org/spec/lex.html#special-token-sequence might also suggest putting a `module` declaration in the file.
Dec 13 2023
On Wednesday, 13 December 2023 at 19:51:11 UTC, Adam D Ruppe wrote:On Wednesday, 13 December 2023 at 19:37:09 UTC, Siarhei Siamashka wrote:Thanks a lot! Looks like it does the job: ```D /+dub.sdl:+/ #line 2 "example.d" import std; void main() { deliberate syntax error here } ``` At least this way there's no undesired line numbers mismatch between different ways to compile and run the same file: ``` $ dub example.d example.d(4,14): Error: found `error` when expecting `;` or `=`, did you mean `deliberate syntax = here`? example.d(4,27): Error: found `}` when expecting `;` or `=`, did you mean `error here = End of File`? $ cat example.d | dmd -run - example.d(4): Error: found `error` when expecting `;` or `=`, did you mean `deliberate syntax = here`? example.d(4): Error: found `}` when expecting `;` or `=`, did you mean `error here = End of File`? ``` But the compiler command line doesn't provide any alternative solutions to override the "__stdin.d" placeholder, right? My primary concern is that the file name and the special token or module directive have to match each other. And unless this is reliably enforced, it may become an additional headache to worry about.Now I'm curious. Is it possible to somehow communicate the real source file name to `dmd`, so that it shows up in the error log instead of "__stdin.d"?the sequence `#line "filename.d" 1` at the top of the thing might do what you need. https://dlang.org/spec/lex.html#special-token-sequence might also suggest putting a `module` declaration in the file.
Dec 13 2023
On Wed, Dec 13, 2023 at 07:37:09PM +0000, Siarhei Siamashka via Digitalmars-d-learn wrote:Example: ```D import std; void main() { deliberate syntax error here } ``` ```bash $ cat example.d | dmd -run - __stdin.d(3): Error: found `error` when expecting `;` or `=`, did you mean `deliberate syntax = here`? __stdin.d(3): Error: found `}` when expecting `;` or `=`, did you mean `error here = End of File`? ``` Now I'm curious. Is it possible to somehow communicate the real source file name to `dmd`, so that it shows up in the error log instead of "__stdin.d"?Add a module declaration to your source file. For example: echo 'module abc; import std; void main(){writefln(__MODULE__);}' | dmd -run - Output: abc `__stdin` is used as a placeholder when no module declaration is present, and dmd doesn't know the filename (which is what it would normally have used for the module name in this case). T -- Век живи - век учись. А дураком помрёшь.
Dec 13 2023
On Wed, Dec 13, 2023 at 11:58:42AM -0800, H. S. Teoh via Digitalmars-d-learn wrote: [...]Add a module declaration to your source file. For example: echo 'module abc; import std; void main(){writefln(__MODULE__);}' | dmd -run - Output: abc `__stdin` is used as a placeholder when no module declaration is present, and dmd doesn't know the filename (which is what it would normally have used for the module name in this case).[...] Hmm, apparently the module declaration doesn't change the placeholder filename. Using `#line 1 abc.d` does the trick, as Adam suggests. T -- People tell me I'm stubborn, but I refuse to accept it!
Dec 13 2023