Skip to content

Commit 027bdc3

Browse files
jeremydubreilFacebook Github Bot
authored andcommitted
[infer][genrule] Add example of Buck DEFS macro to generate Infer analysis targets
Summary: Adding Buck `DEFS` macros for generating Infer genrules. The generated genrules can be used to run the analysis on any existing `java_library` targets. Reviewed By: sblackshear Differential Revision: D4291234 fbshipit-source-id: 6430e2e
1 parent 8a37078 commit 027bdc3

File tree

10 files changed

+113
-22
lines changed

10 files changed

+113
-22
lines changed

.buckconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[buildfile]
2+
includes = //DEFS
13

24
[project]
35
ignore = .git, .ml, .mli

DEFS

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
3+
original_java_library = java_library
4+
def java_library(
5+
name,
6+
deps=[],
7+
**kwargs
8+
):
9+
compile_name = name + '_compile'
10+
top_deps = []
11+
12+
if 'GENERATE_INFER_GENRULES' in os.environ:
13+
export_srcs_name = name + '_export_srcs'
14+
genrule(
15+
name = export_srcs_name,
16+
srcs = kwargs.get('srcs', []),
17+
cmd = 'mkdir -p $OUT && cp -R $SRCDIR/* $OUT/',
18+
out = 'src_copy',
19+
)
20+
infer_name = name + '_infer'
21+
genrule(
22+
name = infer_name,
23+
cmd = ' '.join([
24+
os.getenv('INFER_BIN', 'infer'),
25+
'--results-dir', '$OUT',
26+
'--classpath', '$(classpath :{})'.format(compile_name),
27+
'--sourcepath', '$(location :{})'.format(export_srcs_name),
28+
'--generated-classes', '$(location :{})'.format(compile_name),
29+
'--', 'genrule'
30+
]),
31+
out = 'infer_out',
32+
)
33+
top_deps += [':' + infer_name, ':' + export_srcs_name]
34+
35+
original_java_library(
36+
name=name,
37+
exported_deps=[
38+
':' + compile_name,
39+
],
40+
deps=top_deps,
41+
visibility = kwargs.get('visibility', [])
42+
)
43+
original_java_library(
44+
name=compile_name,
45+
deps=deps,
46+
**kwargs
47+
)

infer/tests/build_systems/buck/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $(JAR_OUTPUT): $(JAVA_SOURCE_FILES)
2424

2525
.PHONY: genrule
2626
genrule: $(JAR_OUTPUT)
27-
cd $(ROOT_DIR) && $(call silent_on_success, INFER_BIN=$(INFER_BIN) NO_BUCKD=1 buck build --no-cache //infer/tests/codetoanalyze/java/infer:run_infer)
27+
cd $(ROOT_DIR) && $(call silent_on_success, INFER_BIN=$(INFER_BIN) NO_BUCKD=1 GENERATE_INFER_GENRULES=1 buck build --no-cache //infer/tests/build_systems/genrule/module2:module2_infer)
2828

2929
infer-out/report.json: genrule $(INFER_BIN) $(JAVA_SOURCE_FILES)
3030
cd $(ROOT_DIR) && $(call silent_on_success, INFER_BIN=$(INFER_BIN) NO_BUCKD=1 $(INFER_BIN) -a $(ANALYZER) --results-dir $(CURDIR)/infer-out -- buck build --no-cache //infer/tests/codetoanalyze/java/infer:compile)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
java_library(
2+
name='annotations',
3+
srcs=['Nullable.java'],
4+
visibility=[
5+
'PUBLIC'
6+
],
7+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package genrule.annotations;
2+
3+
public @interface Nullable {
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
java_library(
2+
name='module1',
3+
srcs=['Class1.java'],
4+
deps=[
5+
'//infer/tests/build_systems/genrule/annotations:annotations',
6+
],
7+
visibility=[
8+
'PUBLIC'
9+
],
10+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package genrule.module1;
2+
3+
import genrule.annotations.Nullable;
4+
5+
public class Class1 {
6+
7+
@Nullable
8+
public static String returnsNull() {
9+
return null;
10+
}
11+
12+
void localNPE1() {
13+
Object obj = null;
14+
obj.toString();
15+
}
16+
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
java_library(
2+
name='module2',
3+
srcs=['Class2.java'],
4+
deps=[
5+
'//infer/tests/build_systems/genrule/module1:module1'
6+
]
7+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package genrule.module2;
2+
3+
import genrule.module1.Class1;
4+
5+
public class Class2 {
6+
7+
void interTargetNPE() {
8+
Object obj = Class1.returnsNull();
9+
obj.toString();
10+
}
11+
12+
void localNPE2() {
13+
Object obj = null;
14+
obj.toString();
15+
}
16+
17+
}

infer/tests/codetoanalyze/java/infer/BUCK

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import os
44

5-
sources = glob(['**/*.java'])
6-
75
java_library(
86
name = 'compile',
9-
srcs = sources,
7+
srcs = glob(['**/*.java']),
108
deps = [
119
'//dependencies/java/guava:guava',
1210
'//dependencies/java/jsr-305:jsr-305',
@@ -19,21 +17,3 @@ java_library(
1917
'PUBLIC'
2018
]
2119
)
22-
23-
genrule(
24-
name = 'run_infer',
25-
srcs = sources,
26-
out = 'infer-out',
27-
bash = ' '.join([
28-
os.getenv('INFER_BIN', 'infer'),
29-
'--sourcepath',
30-
'$SRCDIR',
31-
'--classpath',
32-
'$(classpath :compile)',
33-
'--generated-classes',
34-
'$(location :compile)',
35-
'--out',
36-
'$OUT',
37-
'--',
38-
'genrule']),
39-
)

0 commit comments

Comments
 (0)