在编译时强制执行函数隔离

发布于 2025-01-17 12:41:06 字数 939 浏览 2 评论 0原文

我正在 C Visual Studio 中处理一个项目,我有两组函数,我们将它们称为 SET_1SET_2

我想知道是否有办法确保 SET_1 中的函数仅调用 SET_1 中的函数,而不调用 SET_2 中的函数。

最简单的解决方案是将项目一分为二,但我想避免任何重大的重构。我可能可以进行一些运行时检查,但我也想避免这种情况......

所以,我想知道是否有类似 SAL 注释之类的东西可以用来在编译时强制执行这种隔离?

这是我想要的示例:

#define SET_1 ...
#define SET_2 ...

SET_1
void Fct1()
{
    // ...
}

SET_1
void Fct2()
{
    Fct1(); // Ok, both functions have SET_1 tag
}

SET_2
void Fct3()
{
    Fct1(); // Compile error, Fct1 has a different tag
}

我不想编写某种代码解析器来手动强制执行此规则。


我有多个文件,一个文件包含两组的函数。这些函数没有任何共同特征,我需要手动指定每个函数的集合。

该解决方案可以在编译时或构建时进行。我只是想确保 set1 中的函数不会从 set2 调用

我可以修改代码,并且我知道正确的解决方案是重构项目,但我很好奇是否还有其他解决方案。

例如,如果代码是 C++ 语言,我可以将 set1 中的所有函数包含在一个命名空间内,并将 set2 中的所有函数包含在另一个命名空间内。但是,如果我们有一个类,其函数成员位于不同的集合中,则这将不起作用。

I am working on a project in C Visual Studio, and I have two sets of functions, let’s call them SET_1 and SET_2.

I wonder if there is a way to ensure that a function from SET_1 calls only functions from SET_1 and not functions from SET_2.

The simplest solution will be to split the project in 2, but I want to avoid any major refactoring. I probably can make some runtime checks but I want to avoid this too…

So, I am wondering if there is something like SAL annotations that I can use to enforce this isolation at compile time?

Here is an example of what I want:

#define SET_1 ...
#define SET_2 ...

SET_1
void Fct1()
{
    // ...
}

SET_1
void Fct2()
{
    Fct1(); // Ok, both functions have SET_1 tag
}

SET_2
void Fct3()
{
    Fct1(); // Compile error, Fct1 has a different tag
}

I don’t want to write some kind of code parser to manually enforce this rule.


I have multiple files and a file contains functions from both sets. The functions don’t have any common characteristic, I manually need to specify the set for each function.

The solution can be at compile time or at build time. I just want to make sure that a function from set1 will not be called from set2

I can modify the code and I know that the right solution will be to refactor the project, but I am curious if there is another solution.

For example, if the code was in C++, I could include all functions from set1 inside a namespace and those from set2 inside another namespace. But this will not work if we have a class with function members in different sets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文