Generalize the detection of assertion failures to any kind of custom errors defined programatically in the orginal source code
Summary: public This diff cleans up the detection of assertion failures in C, C++ and Objective C which was previously hacked on top of the tracing mode for Java. The code is also generalized to detect any custom errors which can be defined using the `__infer_fail` builtin, and the case of assertion failure is now just the specific case of translating `assert` using `__infer_fail` directly in the clang frontend. Reviewed By: jberdine Differential Revision: D2786574 fb-gh-sync-id: dd1e1cfmaster
parent
43ee904a1f
commit
6d91199be7
@ -0,0 +1 @@
|
||||
../generic.mk
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import <math.h>
|
||||
|
||||
void __infer_fail(char*);
|
||||
|
||||
void check_exponent(int x) {
|
||||
if (x < 0) __infer_fail("UNEXPECTED_NEGATIVE_EXPONENT");
|
||||
}
|
||||
|
||||
int power(int x) {
|
||||
check_exponent(x);
|
||||
return pow(2, x);
|
||||
}
|
||||
|
||||
int pif() {
|
||||
int a = 3;
|
||||
return power(a);
|
||||
}
|
||||
|
||||
int paf() {
|
||||
int a = -3;
|
||||
return power(a);
|
||||
}
|
||||
|
||||
int global;
|
||||
|
||||
void set_global() {
|
||||
global = -2;
|
||||
}
|
||||
|
||||
int pouf() {
|
||||
set_global();
|
||||
return power(global);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package endtoend.c;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
|
||||
public class CustomErrorTest {
|
||||
|
||||
public static final String SOURCE_FILE =
|
||||
"custom_error/custom.c";
|
||||
|
||||
public static final String CUSTOM_ERROR = "UNEXPECTED_NEGATIVE_EXPONENT";
|
||||
|
||||
private static InferResults inferResults;
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferResults = InferResults.loadCInferResults(CustomErrorTest.class, SOURCE_FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRunsOnAssertionFailureThenAssertionFailureIsFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String[] methods = {
|
||||
"paf",
|
||||
"pouf",
|
||||
};
|
||||
assertThat(
|
||||
"Results should contain " + CUSTOM_ERROR,
|
||||
inferResults,
|
||||
containsExactly(
|
||||
CUSTOM_ERROR,
|
||||
SOURCE_FILE,
|
||||
methods
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue