www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D is trying to parse C and C++ inside strings

reply Hipreme <msnmancini hotmail.com> writes:
I'm writing Metal shaders right now on my engine and I got the 
following warning (I use warn as errors so this is giving me nuts 
as I'll need to refactor my code to hide that string)


```d
string getCoolString()
{
     return q{
         #include <metal_stdlib>
         #include <simd/simd.h>
         using namespace metal;
     };
}

void main()
{
     writeln(getCoolString);
}
```

In the run.dlang, I get the following error:

```
Error: d++ cannot find file path for header 'metal_stdlib'
Error Program exited with code 1
```

In my code, I'm getting the following error:

```d
override string getFrameBufferVertex()
     {
         return q{
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct VertexInput
{
     float2 vPosition;
     float2 vTexST;
};
struct FragmentInput
{
     ///Unused
     float4 position [[position]];
     float2 inTexST;
};

vertex FragmentInput vertex_main(
     uint vertexID [[vertex_id]],
     VertexInput* input [[buffer(1)]]
)
{
     FragmentInput out;
     out.position = float4(input[vertexID].vPosition, 0.0, 1.0);
     out.inTexST = input[vertexID].vTexST;
     return out;
}
};
     }

```
modules/renderer/source/hip/hiprenderer/backend/metal/mtlshader.d(104,9):
Warning: C preprocessor directive `#include` is not supported

This looks like related to use token string.
Mar 17 2023
next sibling parent max haughton <maxhaton gmail.com> writes:
On Saturday, 18 March 2023 at 03:00:46 UTC, Hipreme wrote:
 I'm writing Metal shaders right now on my engine and I got the 
 following warning (I use warn as errors so this is giving me 
 nuts as I'll need to refactor my code to hide that string)


 ```d
 string getCoolString()
 {
     return q{
         #include <metal_stdlib>
         #include <simd/simd.h>
         using namespace metal;
     };
 }

 void main()
 {
     writeln(getCoolString);
 }
 ```

 In the run.dlang, I get the following error:

 ```
 Error: d++ cannot find file path for header 'metal_stdlib'
 Error Program exited with code 1
 ```

 In my code, I'm getting the following error:

 ```d
 override string getFrameBufferVertex()
     {
         return q{
 #include <metal_stdlib>
 #include <simd/simd.h>

 using namespace metal;

 struct VertexInput
 {
     float2 vPosition;
     float2 vTexST;
 };
 struct FragmentInput
 {
     ///Unused
     float4 position [[position]];
     float2 inTexST;
 };

 vertex FragmentInput vertex_main(
     uint vertexID [[vertex_id]],
     VertexInput* input [[buffer(1)]]
 )
 {
     FragmentInput out;
     out.position = float4(input[vertexID].vPosition, 0.0, 1.0);
     out.inTexST = input[vertexID].vTexST;
     return out;
 }
 };
     }

 ```
 modules/renderer/source/hip/hiprenderer/backend/metal/mtlshader.d(104,9):
Warning: C preprocessor directive `#include` is not supported

 This looks like related to use token string.
It's parsing (lexing, even), not trying to parse, D inside the q string literal and the D lexer spits out a warning when it sees #include. This arguably could be considered a bug inside a q string these days given that their original reason for existing has gone away quite significantly as far as I know.
Mar 17 2023
prev sibling next sibling parent Johan <j j.nl> writes:
On Saturday, 18 March 2023 at 03:00:46 UTC, Hipreme wrote:
 I'm writing Metal shaders right now on my engine and I got the 
 following warning (I use warn as errors so this is giving me 
 nuts as I'll need to refactor my code to hide that string)


 ```d
 string getCoolString()
 {
     return q{
         #include <metal_stdlib>
         #include <simd/simd.h>
         using namespace metal;
     };
 }

 void main()
 {
     writeln(getCoolString);
 }
 ```

 In the run.dlang, I get the following error:

 ```
 Error: d++ cannot find file path for header 'metal_stdlib'
 Error Program exited with code 1
 ```
Workaround: put the #include stuff in a normal string: ``` string getCoolString() { return "#include <metal_stdlib> #include <simd/simd.h>" ~ q{ using namespace metal; }; } void main() { import std.stdio; writeln(getCoolString); } ```
Mar 18 2023
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
On Saturday, 18 March 2023 at 03:00:46 UTC, Hipreme wrote:
 This looks like related to use token string.
Filed as https://issues.dlang.org/show_bug.cgi?id=23792
Mar 18 2023
prev sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Saturday, 18 March 2023 at 03:00:46 UTC, Hipreme wrote:
 modules/renderer/source/hip/hiprenderer/backend/metal/mtlshader.d(104,9):
Warning: C preprocessor directive `#include` is not supported

 This looks like related to use token string.
While the compiler shouldn't do that, token strings are intended to be used for D code. AFAIR the idea is that the syntax highlighter treats them as D, so you can do `mixin(format!q{int a = %s;}(5));`, and have D syntax coloring for the code inside the string. So putting another language in there is at least dubious.
Mar 21 2023