简体   繁体   English

如何向特定功能添加编译时元数据/行为

[英]how to add compile-time metadata/behavior to specific function

My codebase is C++ on NXP ARM M4 with custom C++ RTOS. 我的代码库是带有自定义C ++ RTOS的NXP ARM M4上的C ++。

There exists a certain function, DWB() aka DangerWillRobinson() , that if called could have unexpected side-effects (which are valid when it's used correctly). 有一个特定的函数DWB() DangerWillRobinson() ,如果调用该函数可能会产生意想不到的副作用(正确使用时才有效)。

DWB() could be called in a very deeply nested manner as in A()->B()->C()->D()->...->DWB() DWB()可以像A()->B()->C()->D()->...->DWB()那样以非常深层的嵌套方式调用

I want author of any functions that, directly or indirectly, call DWB() to be aware that DWB() is called and I want to force them to acknowledge that they know DWB() is called. 我希望任何直接或间接调用DWB()函数的作者都知道DWB()已被调用, 并且我想强迫他们承认他们知道DWB()已被调用。

I know this could be accomplished by parsing the linker's generated call-trees using Python but I'd much prefer this to be a compile-time error thing. 我知道这可以通过使用Python解析链接器生成的调用树来实现,但是我非常希望这是编译时错误。

Here's how I'd like this to work: 这就是我想要的工作方式:

void A()
{
    B();
}

Author goes to compile: 作者去编译:

ERROR: DWB() is called. 错误:DWB()被调用。 Please acknowledge. 请确认。

Author then thinks about side-effects of calling DWB() and finds no issues. 然后,作者考虑了调用DWB()的副作用,没有发现任何问题。

void A()
{
    // some macro-thing ... ?
    B();
}

If possible, I do not want this to be a runtime check. 如果可能,我不希望这是运行时检查。 I think in theory, this is possible. 我认为从理论上讲,这是可能的。 If Base is inherited, I would like all possible classes' call-trees to be considered; 如果Base是继承的,我希望考虑所有可能的类的调用树。 it's ok if none of them are even instantiated but not ideal. 如果它们都没有实例化但并不理想,那是可以的。

QUESTION

Is this compile-time metadata/behavior of a function possible? 这种编译时元数据/函数行为可能吗?

Is this compile-time metadata/behavior of a function possible? 这种编译时元数据/函数行为可能吗?

No, it isn't. 不,不是。 There might be some configurable SCA tools available (consider a commercial high level one), but not with plain c-preprocessing or meta-template-programming magic. 可能会有一些可配置的SCA工具可用(考虑到商业上的高级工具),但是没有普通的c预处理或meta-template-programming魔术。


I want author of any functions that, directly or indirectly, call DWB() to be aware that DWB() is called and I want to force them to acknowledge that they know DWB() is called. 我希望任何直接或间接调用DWB()函数的作者都知道DWB()已被调用,并且我想强迫他们承认他们知道DWB()已被调用。

The least intrusive way to do that is to mark the DWB() function [[deprecated]] . 这样做的最不麻烦的方法是标记DWB()函数[[deprecated]]

I just suppose you prefer to factor out such error prone function in a midterm roadmap, and replace it with something more stable. 我只是假设您希望在中期路线图中排除这种容易出错的功能,并用更稳定的功能代替它。

The most quick way in case you are sure what all the conditions of a correct DWB() call are is to apply a bunch of assert() calls at the start of that function. 如果您确定正确的DWB()调用的所有条件是什么,最快捷的方法是在该函数的开头应用一堆assert()调用。

There might come up complaints from fellow developers, which are trying to use that function incorrectly. 可能会有来自其他开发人员的投诉,他们试图错误地使用该功能。 Give them best advice as you can in the assertion messages. 尽可能在断言消息中给他们最好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM