Where i can find out how implement such features?

I create attribute clang::invariant(foo-name), i want to adl-call foo-name with ‘this’ every time when method marked with this attribute is called.
The prolems:

  1. Its literally assume, so i want to do [[clang::assume]] instead of function call, but dont know how
  2. on which compilation stage i really need to do it? As i understand it must be in Sema, but its not obvious how to do it
  3. it must behave literaly after invoke, even in compound statements

It seams as very easy feature, like “just make assumption every time method called”, but im stuck, please help()

It’d be helpful if you can give an example or two.

void foo(auto* self) {
[[assume(self->x == 5)]];
[[assume(self->y > 10)]];
}

struct E {
int x;
int y;
[[clang::invariant(foo)]] void bar() {
x = 5;
y = 11;
}
}

After each invoke of ‘bar’ ‘foo’ must be called with ‘this’ pointer of ‘E’

Why not simply write this as

void check_invariants(auto* self) {
  assert(self->x == 5);
  assert(self->y > 10);
}

struct E {
  int x;
  int y;

  void bar() {
    x = 5;
    y = 11;
    check_invariants(this);
  }
};

?

  1. I want reuse my invariants
  2. i want declare invariants in interface for end-user and for compiler too, so compiler will know invariants even if function in other TU
  3. Better optimizations, because it will be always inline function(while it will be hard for compiler to ‘get’ such invariants from non inlined function)
  4. I want use this case as example, im trying to learn how to do things that i need in compiler

P. S.
Today i write something, but when i not pass ‘fsyntax-only’ clang tries to link… WITH A GCC(im on windows…) and i dont know to how fix it…

I guess you’re using clang++, which is a compiler driver compatible with g++. On Windows, you should have a better luck with clang-cl.

The latest gcc has preliminary support for contracts.

Im not about ‘where i can use contracts’, i just want to undestand how to write it correctly in clang