[swift-users] A problem about interact with C macros

Geordie Jay geojay at gmail.com
Sun Dec 17 07:45:20 CST 2017


Thinking about this a bit more:

Imagine you imported another header later that “#undef SITUATION2” and then
redefined it with a different value. The fact that SITUATIONALL is an
expression and not a value would then produce a different result. For some
this would be expected and for others a debugging nightmare. Since there is
no “right” answer, I’d say macro expressions like this will never be
imported by Swift.

- Geordie
Geordie Jay via swift-users <swift-users at swift.org> schrieb am So. 17. Dez.
2017 um 14:40:

> I think this is because the C preprocessor will interpret SITUATIONALL not
> as a value but as an expression. That means your C code at that spot will
> be turned into “(SITUATION1 | SITUATION2 | etc)” and not into the result of
> that expression directly.
>
> The clang importer could probably figure this out, but I suspect there are
> diminishing returns in figuring out what really is a constant as opposed to
> another expression etc. Especially in this example, the inconvenience of
> defining your own SITUATIONALL in Swift is likely lower than the
> inconvenience of that where the macro expression imports incorrectly and
> does unexpected things.
>
> Please (anyone) correct me if I’m wrong here though, I’d like to know the
> definitive answer myself.
>
> - Geordie
>
> Li Keqing via swift-users <swift-users at swift.org> schrieb am So. 17. Dez.
> 2017 um 13:08:
>
>> Here are some macros in C headers such as
>>
>> #define SITUATION1 0x00000001
>> #define SITUATION1 0x00000002
>> #define SITUATION3 0x00000004
>> #define SITUATION4 0x00000008
>> #define SITUATIONALL (SITUATION1 | SITUATION2 | SITUATION3 | SITUATION4)
>>
>> when use these value in swift
>> those lines are okay
>> let value1 = SITUATION1
>> let value2 = SITUATION2
>> let value3 = SITUATION3
>> let value4 = SITUATION4
>> but this line would show error
>> let valueAll = SITUATIONALL
>>
>>  Is it a bug ? or just be designed as it?
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171217/d6a4ad85/attachment.html>


More information about the swift-users mailing list