diff --git a/src/main/CTests/test.c b/src/main/CTests/test.c index 0eed7db..d895d56 100644 --- a/src/main/CTests/test.c +++ b/src/main/CTests/test.c @@ -1,49 +1,37 @@ #include -#define PLUS(first,later) first+later -// 你看我还能用 define -// 当然也能写注释 -int add(int a, int b) { - int c = PLUS(a,b); - return c; + +struct Point {int x, y;}; + +void A(struct Point *p, int a, int b) { + int m, n; + m = a + b; // m-def + n = a * b; // n-def + p->x = m; // p->x-def + p->y = n; // p->y-def + for (int i = 0; i < 5; i++) { // i-def + m = m - i; // m-def, i-use + if (n > m) { // n-use, m-use + n = n + i; // n-def, i-use + } + } + printf("A: m = %d, n = %d, p->x = %d, p->y = %d\n", m, n, p->x, p->y); } -int subtract(int a, int b) { - int d = a - b; - return d; -} - -int multiply(int a, int b) { - int e = a * b; - return e; -} - -int divide(int a, int b) { - int f = a / b; - return f; -} - -int calculate(int a, int b) { - int sum = add(a, b); // 计算和 - int diff = subtract(b, a); // 计算差 - int prod = multiply(sum, diff); // 计算和与差的乘积 - return prod; // 返回乘积 -} - -short complexCalculation(int a, int b) { - int result = calculate(a, b); // 调用 calculate 函数 - int divResult = divide(result, b); // 使用 b 进行除法 - return divResult; // 返回结果 +void B(struct Point *po,int x, int y, int z) { + int p = x + y; // p-def + int q = y * z; // q-def + for (int i = 0; i < 10; i++) { // i-def + p = p - i; // p-def, i-use + q = q + i; // q-def, i-use + } + A(po, p, q); // A: x-use, p-use, q-use + printf("B: p = %d, q = %d\n", p, q); } int main() { - int a = 6; - int b = 3; - - int result1 = calculate(a, b); // 计算 a 和 b 的结果 - short finalResult = complexCalculation(a, b); // 进行复杂计算 - - printf("Result of calculate: %d\n", result1); - printf("Final result of complexCalculation: %d\n", finalResult); - + int x = 5, y = 3, z = 2; // x-def, y-def, z-def + struct Point p1 = {1, 2}; // p1-def + B(&p1,x, y, z); // B: x-use, y-use, z-use, p1-use + printf("main: p1.x = %d, p1.y = %d\n", p1.x, p1.y); return 0; -} +} \ No newline at end of file diff --git a/src/main/kotlin/core/CFunction.kt b/src/main/kotlin/core/CFunction.kt index 68cb69c..ae8ad68 100644 --- a/src/main/kotlin/core/CFunction.kt +++ b/src/main/kotlin/core/CFunction.kt @@ -2,6 +2,7 @@ package core import core.CLanguage.Companion.OPERATION.* import core.CLanguage.Companion.typenameToTypeEnum +import kotlinx.coroutines.delay import utils.Helper.Companion.antiC class CFunction( @@ -39,18 +40,18 @@ class CFunction( val param = it.split(' ').last() val name = if (param.contains('`')) { // struct`*po -> *po) param.split('`').last() - }else{ + } else { param } antiC(name) } } return paramsList?.map { param -> - if (param.contains('`')){ // struct`*po -> *po + if (param.contains('`')) { // struct`*po -> *po val name = antiC(param.split(' ').last()) val type = CLanguage.Companion.TYPES.void mapOf(name to type) - }else { + } else { val parts = param.split(" ") val type = typenameToTypeEnum(parts[0]) ?: return@map emptyMap() val name = antiC(parts.getOrNull(1) ?: "") @@ -93,6 +94,22 @@ class CFunction( // 然后根据句子来获得 TraceTree var paramIndex = 0 // 标记是第几个参数 cParser.sentenceList.forEachIndexed { index, sentence -> + // 修复 node list + if (sentence.left.contains('.')) { + if (sentence.left.split('.').first() in cParser.defineList) { + if (nodes.findNode(sentence.left) == null) { + nodes.add(TraceTree(sentence.left, mutableListOf())) + } + } + } + if (sentence.right.contains('.')) { + if (sentence.right.split('.').first() in cParser.defineList) { + if (nodes.findNode(sentence.right) == null) { + nodes.add(TraceTree(sentence.right, mutableListOf())) + } + } + } + if (sentence.type == DEFINE_VALUE_PARAM || sentence.type == CHANGE_VALUE_PARAM) { // 例子: // x,A(z),CHANGE_VALUE @@ -106,10 +123,12 @@ class CFunction( left = cParser.sentenceList[index - i].left i++ } - val lNode = nodes.findNode(left) - val rNode = nodes.findNode(sentence.right) - if (lNode != null && rNode != null) { - rNode.changes.add(lNode) + if (left != sentence.right) { + val lNode = nodes.findNode(left) + val rNode = nodes.findNode(sentence.right) + if (lNode != null && rNode != null) { + rNode.changes.add(lNode) + } } } } else if (sentence.type == PARAM && invokeInfo != null) { @@ -157,29 +176,38 @@ fun List.getInvokeTree(name: String): Map> { var funcName = "" val resultMap = mutableMapOf>() + fun update() { + val function = this.find(funcName) + if (function != null) { + val info = InvokeInfo(name, args) + val tt = function.getTraceTree(info) + // 过滤 trace tree + invokeTrees.addAll(tt.filter { it.name.startsWith(name) }) + } + if (funcName != "") { + val newTree = invokeTrees.toList() + resultMap[funcName] = newTree // 因为之后要清空 + invokeTrees.clear() + args.clear() + funcName = "" + } + } + func.cParser.sentenceList.forEach { sentence -> // 🌰: // A,z,CHANGE_VALUE_PARAM - if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM) { + if (sentence.type == CHANGE_VALUE_PARAM || sentence.type == DEFINE_VALUE_PARAM || sentence.type == FUNCTION_INVOCATION) { if (sentence.left != "-") { // 左边就是函数名 + if (sentence.left != funcName) { + update() + } args.add(sentence.right) funcName = sentence.left + } else { + update() } } else { // 更新 - val function = this.find(funcName) - if (function != null) { - val info = InvokeInfo(name, args) - val tt = function.getTraceTree(info) - // 过滤 trace tree - invokeTrees.addAll(tt.filter { it.name.startsWith(name) }) - } - if (funcName != "") { - val newTree = invokeTrees.toList() - resultMap[funcName] = newTree // 因为之后要清空 - invokeTrees.clear() - args.clear() - funcName = "" - } + update() } } return resultMap diff --git a/src/main/kotlin/core/SourceFile.kt b/src/main/kotlin/core/SourceFile.kt index 0016a5e..e91427e 100644 --- a/src/main/kotlin/core/SourceFile.kt +++ b/src/main/kotlin/core/SourceFile.kt @@ -284,9 +284,7 @@ class SourceFile { if (it.paramNameList.isEmpty()) { funcResult.add("void") } else { - it.paramNameList.forEach { param -> - funcResult.add("$param-def") - } + funcResult.add(it.paramNameList.joinToString(separator = "-def;", postfix = "-def")) } // 后面的 @@ -295,7 +293,11 @@ class SourceFile { val ifCache = StringBuilder() fun invalidCache(){ + useFuncName = "" if (useCache.isNotEmpty()) { + if (useCache.toString().last() == ';'){ + useCache.setLength(useCache.length - 1) + } funcResult.add(useCache.toString()) useCache.clear() // 清空 } @@ -308,18 +310,24 @@ class SourceFile { it.cParser.sentenceList.forEach { sentence -> when (sentence.type) { FUNCTION_INVOCATION -> { - invalidCache() - if (useFuncName != sentence.left && useFuncName.isNotEmpty()){ + if (ifCache.isNotEmpty()) { + funcResult.add(ifCache.toString().dropLast(1)) + ifCache.clear() + } + if (useFuncName != sentence.left){ useFuncName = sentence.left + if (useCache.toString().last() == ';'){ + useCache.dropLast(1) + } funcResult.add(useCache.toString()) useCache.clear() // 清空 } if (sentence.right in it.cParser.defineList) { useCache.append("${sentence.right}-use;") }else if (sentence.right.contains('.')) { - val param = sentence.right.split('.').first() + val param = sentence.right.split('.').first().trim() if (param in it.cParser.defineList){ - useCache.append("${param}-use;") + useCache.append("${sentence.right}-use;") } } } @@ -331,7 +339,7 @@ class SourceFile { }else if (sentence.right.contains('.')) { val param = sentence.right.split('.').first() if (param in it.cParser.defineList){ - funcResult.add("${param}-use") + funcResult.add("${sentence.right}-use") } } } @@ -347,6 +355,9 @@ class SourceFile { IF_PARAM -> { if (useCache.isNotEmpty()) { + if (useCache.toString().last() == ';'){ + useCache.setLength(useCache.length - 1) + } funcResult.add(useCache.toString()) useCache.clear() } @@ -357,7 +368,10 @@ class SourceFile { } } if (useCache.isNotEmpty()) { - funcResult.add(useCache.toString())s + if (useCache.toString().last() == ';'){ + useCache.setLength(useCache.length - 1) + } + funcResult.add(useCache.toString()) } globalResult[it.name] = funcResult } diff --git a/src/main/kotlin/utils/stringMagic.kt b/src/main/kotlin/utils/stringMagic.kt index 98b6aa4..462bdb1 100644 --- a/src/main/kotlin/utils/stringMagic.kt +++ b/src/main/kotlin/utils/stringMagic.kt @@ -22,6 +22,7 @@ fun getUseList(useList: Map>): String = .replace("], ", "]}\n\n") // 保证不是最后一个 .replace(", ", "],[") // 逗号后面有空格 .replace(";", "、") + .replace("、]","]") fun getTraceTreeString(traceTreeStr: String, funcName: String): String { val sb = StringBuilder("$funcName:\n")