Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using named constants in cn specs #230

Closed
yav opened this issue Apr 15, 2024 · 3 comments
Closed

Using named constants in cn specs #230

yav opened this issue Apr 15, 2024 · 3 comments
Labels

Comments

@yav
Copy link
Collaborator

yav commented Apr 15, 2024

It is common to use #define to name various constants in C, but it looks like these cannot be referenced in CN specifications. I am guessing that this happens because CN specs are comments and the preprocessor does not expand macros in comments.

Is there a known work around for this?

@yav
Copy link
Collaborator Author

yav commented Apr 15, 2024

One workaround that can be used at the moment is define a constant function, for example:

int c_INT_MAX() { return INT_MAX; }

@dc-mak dc-mak added the cn label Apr 23, 2024
@dc-mak
Copy link
Contributor

dc-mak commented Apr 25, 2024

Precisely as you identify, because of the C pre-processor and comments interaction. Perhaps one for the list of engineering-y/polish features to add. Anyways, below is the idiomatic way to do something like this.

/*@ function (i32) INT_MAX() @*/
int c_INT_MAX() /*@ cn_function INT_MAX @*/ { return INT_MAX; }

@dc-mak
Copy link
Contributor

dc-mak commented May 8, 2024

I am going to close this issue since the lack of explanation is noted in rems-project/cn-tutorial#4 and also it's not really something CN (or even Cerberus) can do much about.

Pre-processor shenanigans like macros are a tricky problem to solve in the general case (a Clang plugin would be required: https://github.com/rems-project/c-tree-carver/blob/main/cpp/src/reachability_analyzer.hpp) .

Perhaps a simple workaround would be to run a simple grep over a project, gather the constants in a temporary header file, and --include that file so that CN reads the definitions from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants