From c0dad16cb26a052b52cbe43ddd28e426eb4d4b12 Mon Sep 17 00:00:00 2001 From: Liubov Yaronskaya Date: Fri, 3 Nov 2017 15:13:16 +0300 Subject: [PATCH 1/3] wip: add c model --- src/main/kotlin/app/extractors/CExtractor.kt | 15 +++++++++++++++ .../kotlin/app/extractors/ExtractorInterface.kt | 4 ++++ .../kotlin/test/tests/extractors/ExtractorTest.kt | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/main/kotlin/app/extractors/CExtractor.kt b/src/main/kotlin/app/extractors/CExtractor.kt index f68539d1..3e1ae61c 100644 --- a/src/main/kotlin/app/extractors/CExtractor.kt +++ b/src/main/kotlin/app/extractors/CExtractor.kt @@ -11,6 +11,7 @@ class CExtractor : ExtractorInterface { companion object { val LANGUAGE_NAME = "c" val FILE_EXTS = listOf("c") + val evaluator = ExtractorInterface.getLibrariesModelEvaluator(LANGUAGE_NAME) } override fun extract(files: List): List { @@ -32,4 +33,18 @@ class CExtractor : ExtractorInterface { return imports.toList() } + + override fun tokenize(line: String): List { + val importRegex = Regex("""^([^\n]*#include)\s[^\n]*""") + val commentRegex = Regex("""^([^\n]*//)[^\n]*""") + var newLine = importRegex.replace(line, "") + newLine = commentRegex.replace(newLine, "") + return super.tokenize(newLine) + } + + override fun getLineLibraries(line: String, + fileLibraries: List): List { + + return super.getLineLibraries(line, fileLibraries, evaluator, LANGUAGE_NAME) + } } diff --git a/src/main/kotlin/app/extractors/ExtractorInterface.kt b/src/main/kotlin/app/extractors/ExtractorInterface.kt index e3f2972a..9b2b1356 100644 --- a/src/main/kotlin/app/extractors/ExtractorInterface.kt +++ b/src/main/kotlin/app/extractors/ExtractorInterface.kt @@ -278,6 +278,10 @@ interface ExtractorInterface { return emptyList() } + if (languageLabel == "c" && languageLabel in selectedCategories) { + return emptyList() + } + val lineLibraries = fileLibraries.filter { it in selectedCategories } return lineLibraries } diff --git a/src/test/kotlin/test/tests/extractors/ExtractorTest.kt b/src/test/kotlin/test/tests/extractors/ExtractorTest.kt index 065310ca..30397edc 100644 --- a/src/test/kotlin/test/tests/extractors/ExtractorTest.kt +++ b/src/test/kotlin/test/tests/extractors/ExtractorTest.kt @@ -90,6 +90,12 @@ class ExtractorTest : Spek({ assertExtractsLineLibraries("Tebru\\Retrofit", line, PhpExtractor()) } + + it("c extractor extracts the library") { + val line = "grpc_test_init(argc, argv);" + assertExtractsLineLibraries("grpc", + line, CExtractor()) + } } given("code line doesn't use libraries" ) { @@ -142,5 +148,10 @@ class ExtractorTest : Spek({ val line = "std::cerr << status.ToString() << std::endl;" assertExtractsNoLibraries(line, CppExtractor()) } + + it("c extractor returns empty list") { + val line = "int main(int argc, char **argv) {" + assertExtractsNoLibraries(line, CExtractor()) + } } }) From 917842b3e34da31c774e113ced8f4930e0a6c39c Mon Sep 17 00:00:00 2001 From: Liubov Yaronskaya Date: Fri, 3 Nov 2017 17:54:39 +0300 Subject: [PATCH 2/3] chore: comments --- src/main/kotlin/app/extractors/ExtractorInterface.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/app/extractors/ExtractorInterface.kt b/src/main/kotlin/app/extractors/ExtractorInterface.kt index 9b2b1356..d85b4a58 100644 --- a/src/main/kotlin/app/extractors/ExtractorInterface.kt +++ b/src/main/kotlin/app/extractors/ExtractorInterface.kt @@ -278,7 +278,11 @@ interface ExtractorInterface { return emptyList() } - if (languageLabel == "c" && languageLabel in selectedCategories) { + // For C language. + // Consider line with language label being the one with high probability + // as not having library. + // Keep it while the number of libraries is small. + if (languageLabel == CExtractor.LANGUAGE_NAME && languageLabel in selectedCategories) { return emptyList() } From 5604554649361158d8f6eb9690daf73f7ba88786 Mon Sep 17 00:00:00 2001 From: Liubov Yaronskaya Date: Fri, 3 Nov 2017 17:59:06 +0300 Subject: [PATCH 3/3] chore: style --- src/main/kotlin/app/extractors/ExtractorInterface.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/app/extractors/ExtractorInterface.kt b/src/main/kotlin/app/extractors/ExtractorInterface.kt index d85b4a58..f3778422 100644 --- a/src/main/kotlin/app/extractors/ExtractorInterface.kt +++ b/src/main/kotlin/app/extractors/ExtractorInterface.kt @@ -282,7 +282,8 @@ interface ExtractorInterface { // Consider line with language label being the one with high probability // as not having library. // Keep it while the number of libraries is small. - if (languageLabel == CExtractor.LANGUAGE_NAME && languageLabel in selectedCategories) { + if (languageLabel == CExtractor.LANGUAGE_NAME && + languageLabel in selectedCategories) { return emptyList() }