Sending a message from the compiler (e.g. #pragma message in Swift?)

Hey all-

I've seen this come up in various places but have not seen any real resolution. Is there any way to send a message from the compiler to stdout or whatever is getting the output? In clang there's #pragma message which generates a warning (which I can't decide if I like, compared to Microsoft's cl which does not, but merely sends the message to the console) but trying to include that in Swift code throws errors.

An example use case is when, trying to troubleshoot a problem, I want to comment out some unnecessary logic to simply get to the part I want to troubleshoot (e.g. don't check whether the funds are in the account, just go to the part of the code where the withdrawal logic happens).
Of course I want to make sure that commented-out code doesn't make it into production, so I would decorate it with #pragma message to scream, as part of the build, that there's something that needs attention (interesting side-note is that #pragma mesage in clang generates a non-fatal warning even if -Werror is specified).

XCode provides // MARK: but that's fine only if you're in the XCode; at the command line there's no way, as far as I know, to give any information to the programmer.

I was thinking of writing a proposal but wanted to make sure that this functionality doesn't already exist and I'm just unaware of it, or if there is a proposal and has already been accepted or rejected for reasons.

Thanks for any info!

Ron

You can do:

#warning("hello”)

Or

#error(“won’t compile”)

There’s a writeup about them here: https://swiftunboxed.com/internals/diagnostics-warning-error/

(I did not write that but it helped me with some other things.)

—Dan

2 Likes

Oh, that's exactly what I was looking for! Somehow I had not seen #warning before but that, in fact, does the trick. Thanks Daniel!