Skip to content

Invalid Capture Detection

Min-Yih Hsu edited this page Jun 10, 2020 · 1 revision

As shown in the motivation section in README, traditional C/C++ macro might capture the incorrect declaration due to its primitive text substitution system:

#define foo(arg) {\
    int x = 0;  \
    return arg + x; \
}

// ERROR: caller will always return 0 
// regardless the argument value
int caller(int x) {
    foo(x)
}

In nacro rule, the equivalent code:

// input.c
#pragma nacro rule foo
(arg:$expr) -> {
  int x = 0;
  return arg + x;
}

int caller(int x) {
  foo(x)
}

will throw error messages:

$ clang-nacro -fsyntax-only input.c
./input.c:9:7: error: a potential declaration leak detected
  foo(x)
      ^
./input.c:9:7: note: the reference to 'x' that comes from outside a nacro
./input.c:4:7: note: is bind to declaration within a nacro
  int x = 0;
      ^
1 error generated.
$

For now, we can do nothing but throw error messages, unfortunately. Fixing it automatically involve internal support from the parser. However, Clang's parser doesn't provide any plugin or customizations points.

Clone this wiki locally