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..f3778422 100644 --- a/src/main/kotlin/app/extractors/ExtractorInterface.kt +++ b/src/main/kotlin/app/extractors/ExtractorInterface.kt @@ -278,6 +278,15 @@ interface ExtractorInterface { return emptyList() } + // 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() + } + 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()) + } } })