fix: 过静态分析

This commit is contained in:
Kagura 2024-11-01 00:13:16 +08:00
parent 2fd68e991f
commit 3460f3133d
7 changed files with 33 additions and 27 deletions

View file

@ -1,3 +1,5 @@
@file:Suppress("FunctionName")
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*

View file

@ -6,10 +6,11 @@ import core.CLanguage.Companion.typenameToTypeEnum
class CFunction(
val name: String, source: String // 函数名
) {
val returnType: CLanguage.Companion.CTYPES // 返回类型
val params: List<Map<String, CLanguage.Companion.CTYPES>>? // 参数 Map<name,T> 与实际相反
val returnType: CLanguage.Companion.TYPES // 返回类型
val params: List<Map<String, CLanguage.Companion.TYPES>>? // 参数 Map<name,T> 与实际相反
var paramNameList: List<String> = emptyList()
private set
@Suppress("EmptyMethod")
private set // 单写一个 private set 是保持可变但可以限制改变条件,而且可以保证能直接使用变量名称
val content: String
val cParser: CParser
@ -17,7 +18,7 @@ class CFunction(
this.params = parseParameters(source)
this.content = parseContent(source)
cParser = CParser(content, paramNameList)
returnType = typenameToTypeEnum(parseReturnType(source)) ?: CLanguage.Companion.CTYPES.void
returnType = typenameToTypeEnum(parseReturnType(source)) ?: CLanguage.Companion.TYPES.void
}
private fun parseReturnType(source: String): String {
@ -26,7 +27,7 @@ class CFunction(
return match?.groups?.get(1)?.value ?: ""
}
private fun parseParameters(source: String): List<Map<String, CLanguage.Companion.CTYPES>>? {
private fun parseParameters(source: String): List<Map<String, CLanguage.Companion.TYPES>>? {
val paramPattern = Regex("$name[ |\n]?\\((.*?)\\)[ |\n]?\\{") // main[这里随便空格换行](params...){
val match = paramPattern.find(source)
val paramsList = match?.groups?.get(1)?.value?.split(",")?.map { it.trim() } // 函数最开始的那个括号里面
@ -130,7 +131,7 @@ fun List<CFunction>.find(name: String): CFunction? {
* List<CFunction>.getInvokeTree
* @param List<CFunction>: 要求包含所有函数以查找
* @param name: 从哪个函数开始查找
* @return 特制的 TraceTree funcName -> tracetree[], 包含的是调用者的参数
* @return 特制的 TraceTree funcName -> traceTree[], 包含的是调用者的参数
*/
fun List<CFunction>.getInvokeTree(name: String): Map<String, List<TraceTree>> {
// 先找到开始的函数

View file

@ -7,14 +7,15 @@ class CLanguage {
enum class OPERATION {
DEFINE_VALUE, // `type` identifier = RHand 题目里 def-use
FUNCTION_INVOCATION, // func(value-param) 题目里
CHANGE_VALUE, // identifier = RHAND
CHANGE_VALUE, // identifier = RightHandValue
DEFINE_VALUE_PARAM, // int a=b, 一直接在上一个后面
CHANGE_VALUE_PARAM, // a = b
PARAM, // 函数的参数初始化
RETURN // 字面意思
}
enum class CTYPES {
@Suppress("EnumEntryName")
enum class TYPES {
void,
char,
int,
@ -28,16 +29,16 @@ class CLanguage {
val typeRegex = types.joinToString("|") { "\\b$it\\b" }
val declarationRegex = Regex("($typeRegex)\\s+([^;]+);")
fun typenameToTypeEnum(typename: String): CTYPES? {
fun typenameToTypeEnum(typename: String): TYPES? {
return when (typename) {
"void" -> CTYPES.void
"char" -> CTYPES.char
"int" -> CTYPES.int
"short" -> CTYPES.short
"long" -> CTYPES.long
"float" -> CTYPES.float
"double" -> CTYPES.double
"struct" -> CTYPES.struct
"void" -> TYPES.void
"char" -> TYPES.char
"int" -> TYPES.int
"short" -> TYPES.short
"long" -> TYPES.long
"float" -> TYPES.float
"double" -> TYPES.double
"struct" -> TYPES.struct
else -> null // 无法解析的内容
}
}

View file

@ -5,12 +5,10 @@ import core.CLanguage.Companion.OPERATION.*
class Sentence(val left: String, val right: String, val type: CLanguage.Companion.OPERATION)
class CParser(content: String, paramNameList: List<String>) {
companion object {
val lrexp_regex = "(.*)[ ]?=[ ]?(.*)[ ]?;?$".toRegex() // 左1右2
val func_invoke_regex = "(.*)[ ]?\\((.*)\\)[ ]?$".toRegex() // 匹配到的是1:函数名2:参数列表
val left_eq_right_regex = "(.*) ?= ?(.*) ?;?$".toRegex() // 左1右2
val func_invoke_regex = "(.*) ?\\((.*)\\) ?$".toRegex() // 匹配到的是1:函数名2:参数列表
val c_op_list = listOf("+", "-", "*", "/", "%", "<<", ">>")
}
val sentenceList = mutableListOf<Sentence>()
@ -31,7 +29,7 @@ class CParser(content: String, paramNameList: List<String>) {
if (line.contains('=')) { // 出现了left hand 和 right hand
if (line.split(' ').first() in CLanguage.types) { // 最开始是一个 type
// 说明这是一个 define 语句
val match = lrexp_regex.find(" " + line) // 如果是后面的就要这个
val match = left_eq_right_regex.find(" $line") // 如果是后面的就要这个
if (match != null) {
val leftHand = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
@ -41,7 +39,7 @@ class CParser(content: String, paramNameList: List<String>) {
}
} else { // 重新定义
val match = lrexp_regex.find(line.trim())
val match = left_eq_right_regex.find(line.trim())
if (match != null) {
val leftHand = match.groupValues[1].trim() // Left-hand side (variable)
val rightHand = match.groupValues[2].trim() // Right-hand side (value)
@ -55,11 +53,11 @@ class CParser(content: String, paramNameList: List<String>) {
} else { // 调用函数,解析参数就行了
val match = func_invoke_regex.find(line) // 如果是后面的就要这个
if (match != null) {
val func_name = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
val funcName = match.groupValues[1].trim().split(' ').last() // Left-hand side (variable)
val args = match.groupValues[2].trim() // Right-hand side (value)
args.split(',').forEach { arg ->
if (arg in defineList) { // 定义过的变量
sentenceList.add(Sentence(func_name, arg, FUNCTION_INVOCATION))
sentenceList.add(Sentence(funcName, arg, FUNCTION_INVOCATION))
}
}
}

View file

@ -6,6 +6,7 @@ import core.CLanguage.Companion.declarationRegex
import core.CLanguage.Companion.types
class SourceFile {
@Suppress("unused") // IDE 错误
companion object {
val function_define_regex = """\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\([^)]*\)\s*\{""".toRegex() // main(){ 匹配函数名

View file

@ -1,3 +1,5 @@
@file:Suppress("FunctionName")
package ui
import androidx.compose.foundation.background
@ -24,6 +26,7 @@ import androidx.compose.ui.window.Dialog
import core.SourceFile
import utils.DefaultCode
@Suppress("FunctionName")
@OptIn(ExperimentalTextApi::class)
@Composable
fun TextfieldWithLineNumber(

View file

@ -7,7 +7,7 @@ fun getDefineList(defList: Map<String, List<String>>): String =
// {main=[x, y, z], A=[a, m]}
defList.toString()
.replace("{", "")
.replace("}", "") // 解决左括号
.replace("}", "") // 解决左边和右边的括号
.replace("=", ":def")
.replace("], ","]\n")
@ -16,7 +16,7 @@ fun getUseList(useList: Map<String, List<String>>): String =
// Example:
// {main=[void, x-def, y-def, z-def, z-use, x-def;z-use, y-def;x-use, y-use], A=[a-def, m-def;a-use, m-use]}
useList.toString()
.replace("{}", "") // 如果是空就直接删了
.replace("{}", "") // 如果这里是空就直接删了
.replace("{", "") // 右括号留着
.replace("=", ":{") // 换掉
.replace("], ", "]}\n\n") // 保证不是最后一个