<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote">On Sat, Feb 9, 2013 at 10:22 AM, Karl Rupp <span dir="ltr"><<a href="mailto:rupp@mcs.anl.gov" target="_blank">rupp@mcs.anl.gov</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi,<br>
<br>
I'm super-late in jumping in (again) into the discussion. Before we can even think about doing anything C-AST-related, we need to reduce the CPP usage to a much smaller amount. I'll try to come up with a categorization of CPP usage in PETSc during next week.<br>


<br>
Some of the CPP macros are fairly simple to be integrated into a C-AST. Conditional objects including operations on them are a lot harder, e.g.<br>
<br>
void my_func()<br>
{<br>
  PetscInt   ierr;<br>
#if defined(PETSC_HAVE_SOMETHING)<br>
  PetscInt   headache;<br>
#endif<br>
<br>
  PetscFunctionBegin;<br>
  ...<br>
#if defined(PETSC_HAVE_SOMETHING)<br>
  some_func(headache);<br>
#else<br>
  other_func();<br>
#endif<br></blockquote><div><br></div><div>If we we did negative defines</div><div><br></div><div>#define PETSC_HAVE_SOMETHING 0</div><div><br></div><div>then we could have normal code</div>
<div><br></div><div>  if (PETSC_HAVE_SOMETHING) {</div><div>    some_func(ibuprofen);</div><div> } else { ... }</div><div><br></div><div>The problem with this is that (a) we always need to define everything so petscconf.h becomes larger and configure becomes more tightly coupled in some ways, and (b) any use of #if defined(PETSC_HAVE_SOMETHING) will evaluate true even for a negative define. This last instance is such a confusing source of bugs that almost every project avoids it.</div>

<div><br></div><div style>To convert this to usual C semantics, we could use (yet another) macro trick</div><div style><br></div><div style><a href="https://github.com/torvalds/linux/commit/69349c2dc01c489eccaa4c472542c08e370c6d7e">https://github.com/torvalds/linux/commit/69349c2dc01c489eccaa4c472542c08e370c6d7e</a><br>
</div><div style><a href="https://plus.google.com/+LinusTorvalds/posts/9gntjh57dXt">https://plus.google.com/+LinusTorvalds/posts/9gntjh57dXt</a> (crowd-sourced solution)<br></div><div style><br></div><div style>With this sort of thing, we could write<br>
</div><div><div><br></div><div>if (PETSC_HAVE_(SOMETHING)) {</div><div style>  some_func(no_headache);</div><div>} else { ... }<br></div></div><div><br></div><div>which can be reasoned about using normal C semantics. The condition evaluates at compile time so the code produced (with any optimization) is identical. If different functions are called in the two cases, the linker may request both of them. (This can be good or bad.)</div>
</div></div></div>