[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
master
Jeremy Dubreil 8 years ago committed by Facebook Github Bot
parent 8a3707825c
commit 027bdc32e8

@ -1,3 +1,5 @@
[buildfile]
includes = //DEFS
[project]
ignore = .git, .ml, .mli

47
DEFS

@ -0,0 +1,47 @@
import os
original_java_library = java_library
def java_library(
name,
deps=[],
**kwargs
):
compile_name = name + '_compile'
top_deps = []
if 'GENERATE_INFER_GENRULES' in os.environ:
export_srcs_name = name + '_export_srcs'
genrule(
name = export_srcs_name,
srcs = kwargs.get('srcs', []),
cmd = 'mkdir -p $OUT && cp -R $SRCDIR/* $OUT/',
out = 'src_copy',
)
infer_name = name + '_infer'
genrule(
name = infer_name,
cmd = ' '.join([
os.getenv('INFER_BIN', 'infer'),
'--results-dir', '$OUT',
'--classpath', '$(classpath :{})'.format(compile_name),
'--sourcepath', '$(location :{})'.format(export_srcs_name),
'--generated-classes', '$(location :{})'.format(compile_name),
'--', 'genrule'
]),
out = 'infer_out',
)
top_deps += [':' + infer_name, ':' + export_srcs_name]
original_java_library(
name=name,
exported_deps=[
':' + compile_name,
],
deps=top_deps,
visibility = kwargs.get('visibility', [])
)
original_java_library(
name=compile_name,
deps=deps,
**kwargs
)

@ -24,7 +24,7 @@ $(JAR_OUTPUT): $(JAVA_SOURCE_FILES)
.PHONY: genrule
genrule: $(JAR_OUTPUT)
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)
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)
infer-out/report.json: genrule $(INFER_BIN) $(JAVA_SOURCE_FILES)
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)

@ -0,0 +1,7 @@
java_library(
name='annotations',
srcs=['Nullable.java'],
visibility=[
'PUBLIC'
],
)

@ -0,0 +1,4 @@
package genrule.annotations;
public @interface Nullable {
}

@ -0,0 +1,10 @@
java_library(
name='module1',
srcs=['Class1.java'],
deps=[
'//infer/tests/build_systems/genrule/annotations:annotations',
],
visibility=[
'PUBLIC'
],
)

@ -0,0 +1,17 @@
package genrule.module1;
import genrule.annotations.Nullable;
public class Class1 {
@Nullable
public static String returnsNull() {
return null;
}
void localNPE1() {
Object obj = null;
obj.toString();
}
}

@ -0,0 +1,7 @@
java_library(
name='module2',
srcs=['Class2.java'],
deps=[
'//infer/tests/build_systems/genrule/module1:module1'
]
)

@ -0,0 +1,17 @@
package genrule.module2;
import genrule.module1.Class1;
public class Class2 {
void interTargetNPE() {
Object obj = Class1.returnsNull();
obj.toString();
}
void localNPE2() {
Object obj = null;
obj.toString();
}
}

@ -2,11 +2,9 @@
import os
sources = glob(['**/*.java'])
java_library(
name = 'compile',
srcs = sources,
srcs = glob(['**/*.java']),
deps = [
'//dependencies/java/guava:guava',
'//dependencies/java/jsr-305:jsr-305',
@ -19,21 +17,3 @@ java_library(
'PUBLIC'
]
)
genrule(
name = 'run_infer',
srcs = sources,
out = 'infer-out',
bash = ' '.join([
os.getenv('INFER_BIN', 'infer'),
'--sourcepath',
'$SRCDIR',
'--classpath',
'$(classpath :compile)',
'--generated-classes',
'$(location :compile)',
'--out',
'$OUT',
'--',
'genrule']),
)

Loading…
Cancel
Save