3

Using gcc/tcc/clang's extensions to the C language (C11), is it possible to detect whether a macro argument has a pointer type?

Details: I'm using a macro that should take a singly-indirect pointer, but it's tempting to pass a pointer to that pointer because that's what the corresponsing "constructor" macro takes, but doing so would be an error, so I'm currently currently using something like:

  #define ISPTR(X) (sizeof(X)==sizeof(void*)&&_Alignof(X)==_Alignof(void*)) /*inaccurate*/

   #define TAKE_SINGLY_INDIRECT(P) do{ _Static_assert(!ISPTR(*(P)), "pass a singly indirect pointer"); /*...*/ }while(0)

to protect myself from myself. Is there a better way to implement ISPTR()?

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • Sorry, I think I miss understood your question. Can you give the use case code, your description is not clear for me. – Stargateur Apr 17 '18 at 17:27
  • Just include a statement like `P[0];` and it will fail compilation for not a pointer (but might trigger UB is `P` is not pointing anywhere). – Eugene Sh. Apr 17 '18 at 17:30
  • @EugeneSh. It seems that compilation should fail if `P` is not a single indirection pointer. – user7860670 Apr 17 '18 at 17:30
  • @VTT Oh. Now that's complicated. – Eugene Sh. Apr 17 '18 at 17:31
  • I think it is only possible when passing target type as macro parameter. Then it can be checked with `__builtin_types_compatible_p(typeof(P), TARGET *)` – user7860670 Apr 17 '18 at 17:33
  • 1
    @VTT Exactly. `ISPTR(*(P))` should statically evaluate to false iff `*(P)` is a nonpointer type. I don't think it's a duplicate with the suggestion cuz differentiating ptr and arrays is a different matter (not to mention if C++ is allowed too). – Petr Skocik Apr 17 '18 at 17:42
  • @VTT It should be a type-generic macro. I can't think of a solution either, hence the question. – Petr Skocik Apr 17 '18 at 17:46
  • I think we got a xy problem, I don't see why in the word you need that, please again give real use case. The code inside your macro should not compile or at least produce warning itself with a double pointer. And why use macro ??? Don't you like function ? – Stargateur Apr 17 '18 at 18:12
  • `Is there a better way...` Indeed there is a better way. It is called "test". You implement a separate program that run your code and check whether or not the output is legit according to your specification. The method is cross platform and tool independent, compatible with every version of every programming language ever. (Please stop undermining proper testing by "clever" compiler hacks. No offence) – Andreas Apr 17 '18 at 18:34
  • @PSkocik Do you know the expected pointer type, or is it unknown (i.e. `T *` for some arbitrary `T`)? – Florian Weimer Sep 28 '18 at 12:23
  • @FlorianWeimer unknown – Petr Skocik Sep 28 '18 at 13:27
  • You could do something like `#define WARN_IF_NOT_PTR(X) { void * make_uniq_name = X; }` or even define a function taking a ptr arg... basically you just want to invoke the type checker, right? – sudo rm -rf slash Nov 18 '18 at 20:58

0 Answers0