开发者

How to mark a function as non-returning in Parasoft C++test?

We have a die function that outputs an error message and exits, e.g.:

void die(const char* msg) {
    fprintf(stderr, "Error: %s\n", msg);
    exit(1);
}

We use Parasoft C++test to statically analyze our code, but it doesn't realize that die is a non-returning function. So when it sees code like:

void foo(Bar* bar) {
    if(!bar) {
        die("bar is NULL");
    }
    Bar bar2 = *bar;
}

It warns that *bar might be dereferencing a null pointer, even though 开发者_运维百科bar being NULL would prevent that line from ever executing. Is there a way to mark die as non-returning in a way Parasoft would recognize?


Edit: I need something that works in both GCC and VS 2003, but I'm not above #ifdefing my way around things if somebody has a solution that only works in VS


I figured it out. It turns out Parasoft has a built-in list of NRFs you can customize; they're called "terminating functions". You can edit them through the VS GUI or through the configuration file if you run Parasoft outside of VS

Through Visual Studio

  • Open the Test Configurations dialog from the C++test menu
  • Select your configuration
  • Select the Static tab
  • Select the BugDetective Options subtab
  • Select the Terminators sub-subtab

    Screenshot of the window http://so.mrozekma.com/parasoft1.png

  • Add a new entry with the non-returning function name

    Screenshot of the entry http://so.mrozekma.com/parasoft2.png

Through the configuration file

Add lines like the following:

com.parasoft.xtest.checker.flowanalysis.terminators.api0.methods=active\=true|name\=die|type\=*|paramsn\=*|defsInSubClasses\=true;
com.parasoft.xtest.checker.flowanalysis.terminators.apis=active\=true|name\=foo;


In gcc, you should attribute the function with something like:

     die (const char *, ...) 
          __attribute__ ((format_arg (1)))
          __attribute__ ((noreturn))


If you're using Visual Studio 2005+, you can use __declspec(noreturn) like this:

__declspec(noreturn) void die(char const* msg){
  fprntf(stderr, "Error: %s\n"; msg);
  exit(1);
}

Maybe that helps Parasoft to recognize the function as non-returning.
Edit: GCC has __attribute__((noreturn)) (first example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜