Skip to content

Commit

Permalink
Merge pull request #47 from codefuse-ai/lhk_dev
Browse files Browse the repository at this point in the history
[doc] Fix logic of `UnusedMethod.gdl` & Add example file `CallChain.gdl`
  • Loading branch information
zhouang777 authored Apr 30, 2024
2 parents 9cc19a9 + a5186fe commit 2eccd87
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
16 changes: 15 additions & 1 deletion doc/tools/generate_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,16 @@ def dump_reference_main_doc():

dump_reference_main_doc()

assets_count = 0

for file in input_file_list:
file_full_path = file["path"] + "/" + file["name"]
print("Generate markdown for " + file_full_path)
semantic_info = json.loads(semantic_dict[file["name"]])
comment_list = semantic_info["comments"]

# generate reference.md
assets_count += 1
output_data = "---\n"
output_data += "title: \"coref::" + name_mapper[file["name"]] + "\"\n"
output_data += "layout: default\n"
Expand All @@ -251,6 +255,7 @@ def dump_reference_main_doc():
output_file_path = markdown_output_path + "/" + name_mapper[file["name"]] + "/reference.md"
open(output_file_path, "w").write(output_data)

# generate database.md
output_data = "---\n"
output_data += "title: \"database\"\n"
output_data += "layout: default\n"
Expand All @@ -259,13 +264,16 @@ def dump_reference_main_doc():
output_data += "---\n"
output_data += "# Database of " + file["name"] + "\n\n"
database_list = semantic_info["semantic"]["database"]
assets_count += 1
for database in database_list:
output_data += dump_database(database)
output_file_path = markdown_output_path + "/" + name_mapper[file["name"]] + "/database.md"
print("Generate", output_file_path)
open(output_file_path, "w").write(output_data)

# generate function.md
function_list = semantic_info["semantic"]["function"]
assets_count += 1
output_data = "---\n"
output_data += "title: \"function\"\n"
output_data += "layout: default\n"
Expand All @@ -290,13 +298,17 @@ def dump_reference_main_doc():
print("Generate", output_file_path)
open(output_file_path, "w").write(output_data)

# generate schema documents
schema_list = semantic_info["semantic"]["schema"]
assets_count += len(schema_list)
print("Generate schema documents for", file_full_path, ":", len(schema_list))
for schema in schema_list:
output_data = dump_schema(comment_list, schema)
output_file_path = markdown_output_path + "/" + name_mapper[file["name"]] + "/schema/" + schema["name"] + ".md"
open(output_file_path, "w").write(output_data)

# generate schema hierarchy document
assets_count += 1
output_data = "---\n"
output_data += "title: \"schema\"\n"
output_data += "layout: default\n"
Expand All @@ -307,4 +319,6 @@ def dump_reference_main_doc():
output_data += dump_schema_tree_view(schema_list)
output_file_path = markdown_output_path + "/" + name_mapper[file["name"]] + "/schema.md"
open(output_file_path, "w").write(output_data)
print("Generate schema documents for", file_full_path, ": Done")
print("Generate schema documents for", file_full_path, ": Done")

print("Generation complete, total api count:", assets_count)
56 changes: 56 additions & 0 deletions example/java/CallChain.gdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// script
use coref::java::*

pub fn default_java_db() -> JavaDB {
return JavaDB::load("coref_java_src.db")
}

pub fn specified_callable_signature(name: string) -> bool {
// give specified callables' signatures here
[
{"com.alipay.demo.Main.test:void()"},
{"xxx"},
{"xxx"}
]
}

schema SpecifiedCallable extends Callable {}

impl SpecifiedCallable {
@data_constraint
pub fn __all__(db: JavaDB) -> *Self {
for(c in Callable(db)) {
if (specified_callable_signature(c.getSignature())) {
yield SpecifiedCallable { id: c.id }
}
}
}
}

pub fn getIndirectEdges(b: Callable, c: Callable) -> bool {
for(a in SpecifiedCallable(default_java_db())) {
if (b in a.getAnAncestorCallee() && c in b.getCallee()) {
return true
}
}
}

pub fn getDirectEdges(b: Callable, c: Callable) -> bool {
for(a in SpecifiedCallable(default_java_db())) {
if (c in a.getCallee() && b.key_eq(a)) {
return true
}
}
}

pub fn output_signature(caller: string, callee: string) -> bool {
for(b in Callable(default_java_db()), c in Callable(default_java_db())) {
if (getIndirectEdges(b, c) || getDirectEdges(b, c)) {
return caller = b.getSignature() && callee = c.getSignature()
}
}
}

pub fn main() {
output(output_signature())
}
12 changes: 10 additions & 2 deletions example/java/UnusedMethod.gdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ fn default_java_db() -> JavaDB {
return JavaDB::load("coref_java_src.db")
}

fn usedMethod(m: Method) -> bool {
for(c in CallableBinding(default_java_db())) {
if (c.getCallee().key_eq(m)) {
return true
}
}
}

// find unused methods
fn unused_method(unused: string) -> bool {
for(c in Callable(default_java_db()), method in Callable(default_java_db()), caller in method.getCaller()) {
if (c != caller && unused = method.getSignature()) {
for(m in Method(default_java_db())) {
if (!usedMethod(m) && unused = m.getSignature()) {
return true
}
}
Expand Down

0 comments on commit 2eccd87

Please sign in to comment.