Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: InferBear: Add support for other languages #941

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions bears/java/InferBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ class InferBear:
"""
Checks the code with ``infer``.
"""
LANGUAGES = {"Java"}
LANGUAGES = {"Java", "Objective-C", "C"}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
ASCIINEMA_URL = 'https://asciinema.org/a/1g2k0la7xo5az9t8f1v5zy66q'
CAN_DETECT = {'Security'}

@staticmethod
def create_arguments(filename, file, config_file):
return '-npb', '--', 'javac', filename
def create_arguments(self, filename, file, config_file, language: str):
"""
:param language: The language to analyze (Java, Objective-C or C).
"""
language_args = {
'java': ('javac',),
'objective-c': ('clang', '-c'),
'c': ('gcc', '-c')
}
if language not in language_args:
self.err("Invalid language given! Falling back to Java.")
language = 'java'

return ('-npb', '--') + language_args[language.lower()] + (filename,)
44 changes: 38 additions & 6 deletions tests/java/InferBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from tests.LocalBearTestHelper import verify_local_bear


good_file = """
# All examples taken from http://fbinfer.com/docs/hello-world.html

good_java_file = """
class InferGood {
int test() {
String s = null;
Expand All @@ -11,7 +13,7 @@ class InferGood {
}
"""

bad_file = """
bad_java_file = """
class InferBad {
int test() {
String s = null;
Expand All @@ -20,8 +22,38 @@ class InferBad {
}
"""

good_c_file = """
#include <stdlib.h>

void test() {
int *s = NULL;
if (s != NULL) {
*s = 42;
}
}
"""

bad_c_file = """
#include <stdlib.h>

void test() {
int *s = NULL;
*s = 42;
}
"""


InferBearJavaTest = verify_local_bear(
InferBear, valid_files=(good_java_file,), invalid_files=(bad_java_file,),
tempfile_kwargs={"suffix": ".java"}, settings={'language': 'JAVA'})


# InferBear falls back to java if no valid language is given
InferBearFallbackLanguageTest = verify_local_bear(
InferBear, valid_files=(good_java_file,), invalid_files=(bad_java_file,),
tempfile_kwargs={"suffix": ".java"}, settings={'language': 'bullshit'})


InferBearTest = verify_local_bear(InferBear,
valid_files=(good_file,),
invalid_files=(bad_file,),
tempfile_kwargs={"suffix": ".java"})
InferBearCTest = verify_local_bear(
InferBear, valid_files=(good_c_file,), invalid_files=(bad_c_file,),
tempfile_kwargs={"suffix": ".c"}, settings={'language': 'objective-c'})