[cfe-commits] [PATCH] Add analyzer_assert for regression tests

Jordy Rose jediknil at belkadan.com
Sun May 13 09:13:30 PDT 2012


Hi, Anna, Ted, and everyone else. Currently a number of static analyzer regression tests have constructs like the following to test how the analyzer's handling its constraints:

void testUnsigned (unsigned a) {
  if (a > 0)
    return;

  void *sentinel = malloc(1);
  if (a)
    return; // expected-warning{{never executed}}
  free(sentinel);
}

void testSigned (int a) {
  if (a > 0)
    return;

  void *sentinel = malloc(1);
  if (a)
    return; // expected-warning{{leak}}
  free(sentinel);
}

That is, we're using MallocChecker, UnreachableCodeChecker, and others to trigger warnings based on the truth or falsehood of conditions. I propose a checker that looks for "calls" to functions named analyzer_assert and analyzer_assert_unknown. Unlike regular assertions, which are /added/ to the analyzer's constraint store, these assertions will warn if they do not match what is /already/ known. This would allow us to much more cleanly write the above tests:

// Any signature with an integral type will do.
// 'bool' would be canonical in C++.
void analyzer_assert(int);
void analyzer_assert_unknown(int);

void testUnsigned (unsigned a) {
  if (a > 0)
    return;

  analyzer_assert(a == 0);
}

void testSigned (int a) {
  if (a > 0)
    return;

  analyzer_assert_unknown(a == 0);
}

Proper use of these functions will never include an expected warning.

Comments? I think makes a lot of our regression tests a lot simpler.

Jordy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: DebugAsserts.patch
Type: application/octet-stream
Size: 5819 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120513/f89a8f5c/attachment.obj>


More information about the cfe-commits mailing list