thradams / cake

Cake a C23 front end and transpiler written in C
http://thradams.com/cake/index.html
GNU General Public License v3.0
544 stars 24 forks source link

error in flow analysis #117

Open thradams opened 8 months ago

thradams commented 8 months ago
void* _Owner malloc(unsigned long size);
void free(void* _Owner ptr);

struct X{
  struct X* _Owner next;
};
void x_delete(struct X * _Owner);
struct X* _Owner new_x();

struct X* _Owner f(int condition1, int condition2)
{
    struct X* _Owner p1 = nullptr;
    try
    {
        p1 = new_x();
        if (p1 == nullptr) throw;

        while (condition2)
        {
            struct X* _Owner p2 = new_x();
            static_set(p2->next, "null");
            if (p2 == nullptr) throw;
            p2->next = p1; //MOVED
            if (condition1)
            {
                x_delete(p2);
                throw; //p1 dangling pointer
            }
            p1 = p2;
        }
    }
    static_debug(p1); //p1 can be MOVED 
    return p1;
}
thradams commented 8 months ago
int f(int condition){
  int * _Owner p = 0;
  try
  {
    int * _Owner p2 = p;
    if (condition) throw;
    p = 0;
  }
  static_debug(p); //shows "null" - it should be "null or moved"
}
thradams commented 8 months ago

some warning are create in flow analysis ..the problem flow analysis work as second pass on AST were tokens are not present. Each flow analysis runs after the end of function. The the pragma token is consumed then it is not executed on second flow analsys walk. to fix this provisionally we need to add a dumny non null token.

int* _Owner  get();

void f() {
    int* _Owner p = 0;
    p = get();
}

void dummy() {} 

#pragma cake diagnostic check "-Wmissing-destructor"

the alternative is to use static_state instead of pragma.. but pragma also works on preprocessor. static_state can work in parser and flow analysis.

static_state ("check", "-Wmissing-destructor")