Skip to contentSkip to conversation

how to write an spatch in order to replace guard vs #prama once

cocci PICCA Frederic-Emmanuel 3 participants 6 messages Feb 11, 2025

lore
Share & tools
lore
#0 lore raw

Hello,

I am trying to write an spatch in order to replace in my code, the header guard, by the #pragma once.

Is there somewhere a repository of spatches, where I can inspire myself ?

thanks for your help

Frederic

#1 lore raw
Quoted 5 lines · PICCA Frederic-Emmanuel

Hello,

I am trying to write an spatch in order to replace in my code, the header guard, by the #pragma once.

Is there somewhere a repository of spatches, where I can inspire myself ?

Could you make a small example of what you are trying to do?

There is this page, but I don't know if it has examples related to #pragma,

http://coccinellery.org/

julia

#2 lore raw

something like this

// replace guard by pragma once

@@
expression E;
@@

-#ifndef E
-#define E
+#pragma once

...

-#endif

where the #endif is the matching one of the first #ifndef

Fred

#3 lore raw
Quoted 18 lines · PICCA Frederic-Emmanuel

something like this

// replace guard by pragma once

@@
expression E;
@@

(quoted diff) +1 -3
-#ifndef E
-#define E
+#pragma once
 
...
 
-#endif
Quoted 1 line

where the #endif is the matching one of the first #ifndef

I'm sorry, but I doubt that it will be possible. The treatment of #ifdef etc is quite limited, and there is nothing to match the #ifndef up to the corresponding #endif. Coccinelle parses the source code, and these preprocessor options can appear in places that don't really follow the parse tree structure.

julia

#4 lore raw
Quoted 5 lines

I'm sorry, but I doubt that it will be possible. The treatment of #ifdef etc is quite limited, and there is nothing to match the #ifndef up to the corresponding #endif. Coccinelle parses the source code, and these preprocessor options can appear in places that don't really follow the parse tree structure.

snif ;)

have a good end of day.

Fred

#5 lore raw
In reply to PICCA Frederic-Emmanuel
Quoted 18 lines

something like this

// replace guard by pragma once

@@
expression E;
@@

(quoted diff) +1 -3
-#ifndef E
-#define E
+#pragma once
 
...
 
-#endif
Quoted 1 line

where the #endif is the matching one of the first #ifndef

There are several software development challenges involved so far for such a source code transformation.
I am unsure under which circumstances the corresponding support can grow also for preprocessor directives.

Regards,
Markus