# Wrapping function()

**URL:** https://discourse.cmake.org/t/wrapping-function/1410
**Category:** Usage
**Created:** [June 19, 2020, 8:08am UTC](https://discourse.cmake.org/t/wrapping-function/1410 "2020-06-19T08:08:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [June 19, 2020, 8:08am UTC](https://discourse.cmake.org/t/wrapping-function/1410/1 "2020-06-19T08:08:29Z")

</div>

Why does this not work?

```
macro(myfunction FUNCTION_NAME)
   function(${FUNCTION_NAME})
endmacro()
macro(endmyfunction)
   endfunction()
endmacro()

myfunction(new_function)
endmyfunction()

```

This results in:

```
A logical block opening on the line x.cmake:39 (function) is not closed.

```

Why? A macro should not check if its own “scope” is complete since it will will be replaced into the code.

(Note: I am on 3.10)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 19, 2020, 12:35pm UTC](https://discourse.cmake.org/t/wrapping-function/1410/2 "2020-06-19T12:35:48Z")

</div>

CMake’s top-level parser still needs to be able to determine scoping which means being able to see all `function`, `macro`, `if`, `while`, and `foreach` commands with their corresponding closing commands before actually executing anything. I don’t see the C preprocessor macro being able to make half-blocks as a benefit so I’m happy this doesn’t work 🙂 . But this is really a side-effect of macros being runtime-expansions.

One problem case I don’t know how to possibly handle:

```cmake
foreach (func IN …)
  myfunction("${func}")
endforeach ()

```

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [June 24, 2020, 1:00pm UTC](https://discourse.cmake.org/t/wrapping-function/1410/3 "2020-06-24T13:00:44Z")

</div>

Well, I wanted use the macros for creating Mocks of CMake default functions that are not scriptable.

```
mock(add_executable)
   assert(5 ARGC)
   assert_called(1)
   # more manual checks
endmock()

```

I clould try to make mock() have a load of arguments that track every case you could check. This may be possible, but well this is how i started.

I also dont agree with you that it has no benefit in C. Here is a very simple example, that could be useful.

> **[Compiler Explorer - C++ (x86-64 gcc 9.2)](https://godbolt.org/z/_zLHwM)**
>
> \#define myfunction(name) int name () { \\
> printf("Log: entering %s\\n", \_\_FUNCTION\_\_ );
> \#define endmyfunction(retval) \\
> printf("Log: leaving %s\\n", \_\_FUNCTION\_\_ ); \\
> return retval; }
> 
> 
> myfunction(main)
> printf("Hello...

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 24, 2020, 2:00pm UTC](https://discourse.cmake.org/t/wrapping-function/1410/4 "2020-06-24T14:00:01Z")

</div>

> [@KUGA2](#):
>
> I clould try to make mock() have a load of arguments that track every case you could check. This may be possible, but well this is how i started.

I’d just use `function` here. What is `mock()` gaining you here?

> [@KUGA2](#):
>
> I also dont agree with you that it has no benefit in C. Here is a very simple example, that could be useful.

I was more referring to the preprocessor not allowing you to make new `#if` statements that it actually sees. Sure, you can stamp out whatever C you want (because it doesn’t actually care about C at all beyond the constant folding specification), but you can’t stamp out C preprocessor blocks with macros. That is what you’re asking for here.

---

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [June 24, 2020, 3:32pm UTC](https://discourse.cmake.org/t/wrapping-function/1410/5 "2020-06-24T15:32:07Z")

</div>

You are right, there are probably other ways to do this with normal overwrite. The time window where I had time for this passed anyways… ☹

> [@ben.boeckel](#):
>
> That is what you’re asking for here.

Now I see what you mean 🙂 Thanks
