use crate::query::{find_first_descendant, walk_preorder, Walk};
use std::collections::{HashMap, HashSet};
use tree_sitter::Node;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallEdge {
pub caller: String,
pub callee: String,
pub is_external: bool,
}
pub fn call_edges(root: Node, source: &str) -> Vec<CallEdge> {
let local_names = collect_local_names(root, source);
let mut functions = Vec::new();
collect_functions(root, source, &mut functions);
let mut edges = Vec::new();
for (node, caller) in functions {
let mut callees = Vec::new();
let mut aliases = HashMap::new();
collect_fn_ptr_aliases(node, source, &mut aliases);
collect_call_names(node, source, node.has_error(), &aliases, &mut callees);
for callee in callees {
let is_external = !local_names.contains(&callee);
edges.push(CallEdge {
caller: caller.clone(),
callee,
is_external,
});
}
}
edges
}
fn collect_functions<'a>(root: Node<'a>, source: &str, out: &mut Vec<(Node<'a>, String)>) {
walk_preorder(root, |node| {
if is_function_kind(node.kind()) && !is_macro_function_definition(node) {
if let Some(name) = get_function_name(node, source) {
out.push((node, name));
}
}
Walk::Continue
});
}
pub fn is_function_kind(kind: &str) -> bool {
matches!(
kind,
"function_definition"
| "function_item"
| "function_declaration"
| "function_expression"
| "arrow_function"
| "method_definition"
| "generator_function_declaration"
| "generator_function"
| "subprogram_body"
| "expression_function_declaration"
| "task_body"
| "method_declaration"
| "func_literal"
| "constructor_declaration"
| "local_function_statement"
| "init_declaration"
| "function"
| "subroutine"
| "module_procedure"
| "program"
)
}
fn is_macro_function_definition(node: Node) -> bool {
node.kind() == "function_definition"
&& node
.child_by_field_name("declarator")
.map(|d| d.kind() == "parenthesized_declarator")
.unwrap_or(false)
}
pub fn get_function_name(node: Node, source: &str) -> Option<String> {
match node.kind() {
"function_item"
| "method_definition"
| "generator_function_declaration"
| "generator_function"
| "method_declaration"
| "constructor_declaration"
| "local_function_statement" => name_field(node, source),
"function_declaration" => name_field(node, source).or_else(|| {
let mut cursor = node.walk();
let found = node
.children(&mut cursor)
.find(|c| c.kind() == "simple_identifier");
found
.and_then(|c| c.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string())
}),
"function_definition" => name_field(node, source)
.or_else(|| get_c_name(node, source))
.or_else(|| get_lua_assignment_name(node, source)),
"function_expression" => {
name_field(node, source).or_else(|| get_name_from_assignment_context(node, source))
}
"arrow_function" => get_name_from_assignment_context(node, source),
"init_declaration" => Some("init".to_string()),
"func_literal" => None,
"subprogram_body" | "expression_function_declaration" => {
let mut cursor = node.walk();
let spec = node.children(&mut cursor).find(|c| {
matches!(
c.kind(),
"function_specification" | "procedure_specification"
)
})?;
name_field(spec, source)
}
"task_body" => {
let mut cursor = node.walk();
let found = node
.children(&mut cursor)
.find(|c| c.kind() == "identifier");
found
.and_then(|c| c.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string())
}
"function" => name_in_child(node, "function_statement", source),
"subroutine" => name_in_child(node, "subroutine_statement", source),
"module_procedure" => name_in_child(node, "module_procedure_statement", source),
"program" => {
let mut cursor = node.walk();
let stmt = node
.children(&mut cursor)
.find(|c| c.kind() == "program_statement")?;
let mut inner = stmt.walk();
let first_named = stmt.named_children(&mut inner).next();
first_named
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string())
.or_else(|| Some("program".to_string()))
}
_ => get_c_name(node, source),
}
}
fn name_field(node: Node, source: &str) -> Option<String> {
node.child_by_field_name("name")
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string())
}
fn name_in_child(node: Node, child_kind: &str, source: &str) -> Option<String> {
let mut cursor = node.walk();
let child = node
.children(&mut cursor)
.find(|c| c.kind() == child_kind)?;
name_field(child, source)
}
fn get_c_name(node: Node, source: &str) -> Option<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "function_declarator" {
return get_declarator_name(child, source);
}
if child.kind() == "pointer_declarator" {
if let Some(name) = get_function_name_from_declarator(child, source) {
return Some(name);
}
}
}
None
}
fn get_function_name_from_declarator(node: Node, source: &str) -> Option<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "function_declarator" {
return get_declarator_name(child, source);
} else if child.kind() == "pointer_declarator" {
if let Some(name) = get_function_name_from_declarator(child, source) {
return Some(name);
}
}
}
None
}
fn get_declarator_name(node: Node, source: &str) -> Option<String> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"identifier"
| "qualified_identifier"
| "destructor_name"
| "operator_name"
| "field_identifier" => {
return Some(child.utf8_text(source.as_bytes()).ok()?.to_string());
}
"pointer_declarator" | "function_declarator" => {
if let Some(name) = get_declarator_name(child, source) {
return Some(name);
}
}
_ => {}
}
}
None
}
fn get_lua_assignment_name(node: Node, source: &str) -> Option<String> {
let parent = node.parent()?;
match parent.kind() {
"field" => {
let mut cur = parent.walk();
let found = parent
.named_children(&mut cur)
.find(|c| c.kind() == "identifier");
found
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string())
}
"expression_list" => {
let idx = {
let mut cur = parent.walk();
let pos = parent
.named_children(&mut cur)
.position(|c| c.id() == node.id())
.unwrap_or(0);
pos
};
let assign = parent.parent()?;
if assign.kind() != "assignment_statement" {
return None;
}
let mut cur = assign.walk();
let found = assign
.children(&mut cur)
.find(|c| c.kind() == "variable_list");
let var_list = found?;
let mut cur2 = var_list.walk();
let var = var_list.named_children(&mut cur2).nth(idx)?;
if var.kind() != "identifier" {
return None;
}
var.utf8_text(source.as_bytes()).ok().map(|s| s.to_string())
}
_ => None,
}
}
fn get_name_from_assignment_context(node: Node, source: &str) -> Option<String> {
let parent = node.parent()?;
match parent.kind() {
"variable_declarator" => parent
.child_by_field_name("name")
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string()),
"pair" => parent
.child_by_field_name("key")
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string()),
"public_field_definition" => parent
.child_by_field_name("name")
.and_then(|n| n.utf8_text(source.as_bytes()).ok())
.map(|s| s.to_string()),
_ => None,
}
}
pub fn collect_local_names(root: Node, source: &str) -> HashSet<String> {
let mut names = HashSet::new();
collect_local_names_into(root, source, &mut names);
names
}
fn collect_local_names_into(root: Node, source: &str, names: &mut HashSet<String>) {
walk_preorder(root, |node| {
if is_function_kind(node.kind()) {
if let Some(name) = get_function_name(node, source) {
names.insert(name);
}
} else if matches!(node.kind(), "preproc_def" | "preproc_function_def") {
if let Some(name_node) = node.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
names.insert(name.to_string());
}
}
}
Walk::Continue
});
}
fn handle_call_node(
node: Node,
source: &str,
aliases: &HashMap<String, String>,
out: &mut Vec<String>,
) {
let Some(func_node) = node.child_by_field_name("function") else {
return;
};
match func_node.kind() {
"identifier" => {
if let Ok(name) = func_node.utf8_text(source.as_bytes()) {
let resolved = aliases.get(name).map(String::as_str).unwrap_or(name);
out.push(resolved.to_string());
}
}
"scoped_identifier"
| "attribute"
| "member_expression"
| "selector_expression"
| "member_access_expression" => {
if let Ok(name) = func_node.utf8_text(source.as_bytes()) {
out.push(name.to_string());
}
}
"field_expression" => {
if let Some(field) = func_node.child_by_field_name("field") {
if let Ok(method_name) = field.utf8_text(source.as_bytes()) {
out.push(method_name.to_string());
}
}
}
_ => {}
}
}
fn collect_call_names(
root: Node,
source: &str,
stop_at_nested: bool,
aliases: &HashMap<String, String>,
out: &mut Vec<String>,
) {
walk_preorder(root, |node| {
let pruned = stop_at_nested
&& node != root
&& is_function_kind(node.kind())
&& !is_macro_function_definition(node)
&& !is_error_recovery_debris(node, source);
if pruned {
return Walk::SkipChildren;
}
if node.kind() == "call_expression"
|| node.kind() == "call"
|| node.kind() == "invocation_expression"
{
handle_call_node(node, source, aliases, out);
}
if node.kind() == "procedure_call_statement" || node.kind() == "function_call" {
if let Some(name_node) = node.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
out.push(name.to_string());
}
}
}
if node.kind() == "method_invocation" {
if let Some(name_node) = node.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source.as_bytes()) {
out.push(name.to_string());
}
}
}
if node.kind() == "call_expression" && node.child_by_field_name("function").is_none() {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if !child.is_named() {
continue;
}
if matches!(
child.kind(),
"value_arguments" | "type_arguments" | "annotated_lambda" | "call_suffix"
) {
continue;
}
let text = child.utf8_text(source.as_bytes()).unwrap_or("").to_string();
out.push(text);
break;
}
}
if node.kind() == "object_creation_expression" {
if let Some(type_node) = node.child_by_field_name("type") {
if let Ok(name) = type_node.utf8_text(source.as_bytes()) {
out.push(name.to_string());
}
}
}
Walk::Continue
});
}
fn is_error_recovery_debris(node: Node, source: &str) -> bool {
node.kind() == "function_definition"
&& get_function_name(node, source).is_some_and(|name| is_c_keyword(&name))
}
fn is_c_keyword(name: &str) -> bool {
matches!(
name,
"if" | "else"
| "for"
| "while"
| "do"
| "switch"
| "case"
| "default"
| "return"
| "break"
| "continue"
| "goto"
| "sizeof"
| "typedef"
| "struct"
| "union"
| "enum"
)
}
fn collect_fn_ptr_aliases(root: Node, source: &str, aliases: &mut HashMap<String, String>) {
walk_preorder(root, |node| {
match node.kind() {
"declaration" => {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() != "init_declarator" {
continue;
}
let Some(decl) = child.child_by_field_name("declarator") else {
continue;
};
if !declarator_is_function_pointer(decl) {
continue;
}
let Some(name) = extract_innermost_identifier(decl, source) else {
continue;
};
if let Some(value) = child.child_by_field_name("value") {
if let Some(target) = rhs_target_function_name(value, source) {
aliases.insert(name, target);
}
}
}
}
"assignment_expression" => {
if let (Some(lhs), Some(rhs)) = (
node.child_by_field_name("left"),
node.child_by_field_name("right"),
) {
if lhs.kind() == "identifier" {
if let Ok(lhs_name) = lhs.utf8_text(source.as_bytes()) {
if aliases.contains_key(lhs_name) {
if let Some(target) = rhs_target_function_name(rhs, source) {
aliases.insert(lhs_name.to_string(), target);
}
}
}
}
}
}
_ => {}
}
Walk::Continue
});
}
fn declarator_is_function_pointer(node: Node) -> bool {
find_first_descendant(node, |n| n.kind() == "function_declarator").is_some()
&& find_first_descendant(node, |n| n.kind() == "pointer_declarator").is_some()
}
fn extract_innermost_identifier(node: Node, source: &str) -> Option<String> {
if node.kind() == "identifier" {
return node.utf8_text(source.as_bytes()).ok().map(String::from);
}
if let Some(inner) = node.child_by_field_name("declarator") {
if let Some(name) = extract_innermost_identifier(inner, source) {
return Some(name);
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(name) = extract_innermost_identifier(child, source) {
return Some(name);
}
}
None
}
fn rhs_target_function_name(node: Node, source: &str) -> Option<String> {
let mut n = node;
loop {
match n.kind() {
"parenthesized_expression" => {
if let Some(inner) = n.named_child(0) {
n = inner;
continue;
}
return None;
}
"cast_expression" => {
if let Some(value) = n.child_by_field_name("value") {
n = value;
continue;
}
return None;
}
"pointer_expression" | "unary_expression" => {
if let Some(arg) = n.child_by_field_name("argument") {
n = arg;
continue;
}
return None;
}
_ => break,
}
}
if n.kind() == "identifier" {
n.utf8_text(source.as_bytes()).ok().map(String::from)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::registry::language_for_key;
use tree_sitter::Parser;
fn edges(key: &str, code: &str) -> Vec<CallEdge> {
let language = language_for_key(key).unwrap_or_else(|| panic!("no grammar for {key}"));
let mut parser = Parser::new();
parser.set_language(&language).unwrap();
let tree = parser.parse(code, None).unwrap();
call_edges(tree.root_node(), code)
}
#[cfg(feature = "lang-rust")]
#[test]
fn rust_local_and_external_calls() {
let code =
"fn helper() {}\nfn main() { helper(); println!(\"hi\"); std::fs::read(\"x\"); }\n";
let e = edges("rust", code);
assert!(e.contains(&CallEdge {
caller: "main".into(),
callee: "helper".into(),
is_external: false,
}));
assert!(e
.iter()
.any(|c| c.caller == "main" && c.callee == "std::fs::read" && c.is_external));
}
#[cfg(feature = "lang-python")]
#[test]
fn python_calls() {
let code = "def helper():\n pass\n\ndef main():\n helper()\n os.path.join('a')\n";
let e = edges("python", code);
assert!(e.contains(&CallEdge {
caller: "main".into(),
callee: "helper".into(),
is_external: false,
}));
assert!(e
.iter()
.any(|c| c.caller == "main" && c.callee == "os.path.join" && c.is_external));
}
#[cfg(feature = "lang-go")]
#[test]
fn go_calls() {
let code =
"package main\nfunc helper() {}\nfunc main() {\n\thelper()\n\tfmt.Println(\"hi\")\n}\n";
let e = edges("go", code);
assert!(e.contains(&CallEdge {
caller: "main".into(),
callee: "helper".into(),
is_external: false,
}));
assert!(e
.iter()
.any(|c| c.caller == "main" && c.callee == "fmt.Println" && c.is_external));
}
#[cfg(feature = "lang-java")]
#[test]
fn java_method_invocation() {
let code = "class A {\n void helper() {}\n void main() {\n helper();\n System.out.println(\"hi\");\n }\n}\n";
let e = edges("java", code);
assert!(e.contains(&CallEdge {
caller: "main".into(),
callee: "helper".into(),
is_external: false,
}));
assert!(e
.iter()
.any(|c| c.caller == "main" && c.callee == "println" && c.is_external));
}
#[cfg(feature = "lang-c")]
#[test]
fn deeply_nested_input_does_not_overflow_the_stack() {
let depth = 20_000;
let mut code = String::from("void helper(void) {}\nvoid f(int x) {\n");
for _ in 0..depth {
code.push_str("if (x) {\n");
}
code.push_str("helper();\n");
for _ in 0..depth {
code.push_str("}\n");
}
code.push_str("}\n");
let e = edges("c", &code);
assert!(e.contains(&CallEdge {
caller: "f".into(),
callee: "helper".into(),
is_external: false,
}));
}
#[cfg(feature = "lang-c")]
#[test]
fn c_calls() {
let code =
"int helper() { return 1; }\nint main() { helper(); printf(\"hi\"); return 0; }\n";
let e = edges("c", code);
assert!(e.contains(&CallEdge {
caller: "main".into(),
callee: "helper".into(),
is_external: false,
}));
assert!(e
.iter()
.any(|c| c.caller == "main" && c.callee == "printf" && c.is_external));
}
#[cfg(feature = "lang-rust")]
#[test]
fn no_functions_yields_no_edges() {
assert!(edges("rust", "const X: i32 = 1;\n").is_empty());
}
#[test]
#[cfg(feature = "lang-c")]
fn c_ifdef_spanning_brace_does_not_leak_swallowed_sibling_calls() {
let code = r#"int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
int rc;
int i;
#ifndef SQLITE_OMIT_DEPRECATED
int size;
#endif
Db *pDb;
char const *azArg[6];
int meta[5];
InitData initData;
const char *zSchemaTabName;
int openedTransaction = 0;
int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
assert( iDb>=0 && iDb<db->nDb );
assert( db->aDb[iDb].pSchema );
assert( sqlite3_mutex_held(db->mutex) );
assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
db->init.busy = 1;
/* Construct the in-memory representation schema tables (sqlite_schema or
** sqlite_temp_schema) by invoking the parser directly. The appropriate
** table name will be inserted automatically by the parser so we can just
** use the abbreviation "x" here. The parser will also automatically tag
** the schema table as read-only. */
azArg[0] = "table";
azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
azArg[2] = azArg[1];
azArg[3] = "1";
azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
"rootpage int,sql text)";
azArg[5] = 0;
initData.db = db;
initData.iDb = iDb;
initData.rc = SQLITE_OK;
initData.pzErrMsg = pzErrMsg;
initData.mInitFlags = mFlags;
initData.nInitRow = 0;
initData.mxPage = 0;
sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
db->mDbFlags &= mask;
if( initData.rc ){
rc = initData.rc;
goto error_out;
}
/* Create a cursor to hold the database open
*/
pDb = &db->aDb[iDb];
if( pDb->pBt==0 ){
assert( iDb==1 );
DbSetProperty(db, 1, DB_SchemaLoaded);
rc = SQLITE_OK;
goto error_out;
}
/* If there is not already a read-only (or read-write) transaction opened
** on the b-tree database, open one now. If a transaction is opened, it
** will be closed before this function returns. */
sqlite3BtreeEnter(pDb->pBt);
if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
if( rc!=SQLITE_OK ){
sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
goto initone_error_out;
}
openedTransaction = 1;
}
/* Get the database meta information.
**
** Meta values are as follows:
** meta[0] Schema cookie. Changes with each schema change.
** meta[1] File format of schema layer.
** meta[2] Size of the page cache.
** meta[3] Largest rootpage (auto/incr_vacuum mode)
** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
** meta[5] User version
** meta[6] Incremental vacuum mode
** meta[7] unused
** meta[8] unused
** meta[9] unused
**
** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
** the possible values of meta[4].
*/
for(i=0; i<ArraySize(meta); i++){
sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
}
if( (db->flags & SQLITE_ResetDatabase)!=0 ){
memset(meta, 0, sizeof(meta));
}
pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
/* If opening a non-empty database, check the text encoding. For the
** main database, set sqlite3.enc to the encoding of the main database.
** For an attached db, it is an error if the encoding is not the same
** as sqlite3.enc.
*/
if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
u8 encoding;
#ifndef SQLITE_OMIT_UTF16
/* If opening the main database, set ENC(db). */
encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
if( encoding==0 ) encoding = SQLITE_UTF8;
#else
encoding = SQLITE_UTF8;
#endif
sqlite3SetTextEncoding(db, encoding);
}else{
/* If opening an attached database, the encoding much match ENC(db) */
if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
" text encoding as main database");
rc = SQLITE_ERROR;
goto initone_error_out;
}
}
}
pDb->pSchema->enc = ENC(db);
if( pDb->pSchema->cache_size==0 ){
#ifndef SQLITE_OMIT_DEPRECATED
size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
pDb->pSchema->cache_size = size;
#else
pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
#endif
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
}
/*
** file_format==1 Version 3.0.0.
** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
*/
pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
if( pDb->pSchema->file_format==0 ){
pDb->pSchema->file_format = 1;
}
if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
sqlite3SetString(pzErrMsg, db, "unsupported file format");
rc = SQLITE_ERROR;
goto initone_error_out;
}
/* Ticket #2804: When we open a database in the newer file format,
** clear the legacy_file_format pragma flag so that a VACUUM will
** not downgrade the database and thus invalidate any descending
** indices that the user might have created.
*/
if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
db->flags &= ~(u64)SQLITE_LegacyFileFmt;
}
/* Read the schema information out of the schema tables
*/
assert( db->init.busy );
initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
{
char *zSql;
zSql = sqlite3MPrintf(db,
"SELECT*FROM\"%w\".%s ORDER BY rowid",
db->aDb[iDb].zDbSName, zSchemaTabName);
#ifndef SQLITE_OMIT_AUTHORIZATION
{
sqlite3_xauth xAuth;
xAuth = db->xAuth;
db->xAuth = 0;
#endif
rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
#ifndef SQLITE_OMIT_AUTHORIZATION
db->xAuth = xAuth;
}
#endif
if( rc==SQLITE_OK ) rc = initData.rc;
sqlite3DbFree(db, zSql);
#ifndef SQLITE_OMIT_ANALYZE
if( rc==SQLITE_OK ){
sqlite3AnalysisLoad(db, iDb);
}
#endif
}
assert( pDb == &(db->aDb[iDb]) );
if( db->mallocFailed ){
rc = SQLITE_NOMEM_BKPT;
sqlite3ResetAllSchemasOfConnection(db);
pDb = &db->aDb[iDb];
}else
if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
/* Hack: If the SQLITE_NoSchemaError flag is set, then consider
** the schema loaded, even if errors (other than OOM) occurred. In
** this situation the current sqlite3_prepare() operation will fail,
** but the following one will attempt to compile the supplied statement
** against whatever subset of the schema was loaded before the error
** occurred.
**
** The primary purpose of this is to allow access to the sqlite_schema
** table even when its contents have been corrupted.
*/
DbSetProperty(db, iDb, DB_SchemaLoaded);
rc = SQLITE_OK;
}
/* Jump here for an error that occurs after successfully allocating
** curMain and calling sqlite3BtreeEnter(). For an error that occurs
** before that point, jump to error_out.
*/
initone_error_out:
if( openedTransaction ){
sqlite3BtreeCommit(pDb->pBt);
}
sqlite3BtreeLeave(pDb->pBt);
error_out:
if( rc ){
if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
sqlite3OomFault(db);
}
sqlite3ResetOneSchema(db, iDb);
}
db->init.busy = 0;
return rc;
}
/*
** Initialize all database files - the main database file, the file
** used to store temporary tables, and any additional database files
** created using ATTACH statements. Return a success code. If an
** error occurs, write an error message into *pzErrMsg.
**
** After a database is initialized, the DB_SchemaLoaded bit is set
** bit is set in the flags field of the Db structure.
*/
int sqlite3Init(sqlite3 *db, char **pzErrMsg){
int i, rc;
int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
assert( sqlite3_mutex_held(db->mutex) );
assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
assert( db->init.busy==0 );
ENC(db) = SCHEMA_ENC(db);
assert( db->nDb>0 );
/* Do the main schema first */
if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
if( rc ) return rc;
}
/* All other schemas after the main schema. The "temp" schema must be last */
for(i=db->nDb-1; i>0; i--){
assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
rc = sqlite3InitOne(db, i, pzErrMsg, 0);
if( rc ) return rc;
}
}
if( commit_internal ){
sqlite3CommitInternalChanges(db);
}
return SQLITE_OK;
}
"#;
let e = edges("c", code);
assert!(e.contains(&CallEdge {
caller: "sqlite3Init".into(),
callee: "sqlite3InitOne".into(),
is_external: false,
}));
assert!(!e
.iter()
.any(|c| c.caller == "sqlite3InitOne" && c.callee == "sqlite3InitOne"));
assert!(!e
.iter()
.any(|c| c.caller == "sqlite3InitOne" && c.callee == "sqlite3CommitInternalChanges"));
}
#[test]
#[cfg(feature = "lang-c")]
fn c_mis_parsed_if_does_not_lose_real_calls() {
let code = r#"static int valueFromExpr(
sqlite3 *db, /* The database connection */
const Expr *pExpr, /* The expression to evaluate */
u8 enc, /* Encoding to use */
u8 affinity, /* Affinity to use */
sqlite3_value **ppVal, /* Write the new value here */
struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
){
int op;
char *zVal = 0;
sqlite3_value *pVal = 0;
int negInt = 1;
const char *zNeg = "";
int rc = SQLITE_OK;
assert( pExpr!=0 );
while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
if( op==TK_REGISTER ) op = pExpr->op2;
/* Compressed expressions only appear when parsing the DEFAULT clause
** on a table column definition, and hence only when pCtx==0. This
** check ensures that an EP_TokenOnly expression is never passed down
** into valueFromFunction(). */
assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
if( op==TK_CAST ){
u8 aff;
assert( !ExprHasProperty(pExpr, EP_IntValue) );
aff = sqlite3AffinityType(pExpr->u.zToken,0);
rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
testcase( rc!=SQLITE_OK );
if( *ppVal ){
#ifdef SQLITE_ENABLE_STAT4
rc = ExpandBlob(*ppVal);
#else
/* zero-blobs only come from functions, not literal values. And
** functions are only processed under STAT4 */
assert( (ppVal[0][0].flags & MEM_Zero)==0 );
#endif
sqlite3VdbeMemCast(*ppVal, aff, enc);
sqlite3ValueApplyAffinity(*ppVal, affinity, enc);
}
return rc;
}
/* Handle negative integers in a single step. This is needed in the
** case when the value is -9223372036854775808. Except - do not do this
** for hexadecimal literals. */
if( op==TK_UMINUS ){
Expr *pLeft = pExpr->pLeft;
if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){
if( ExprHasProperty(pLeft, EP_IntValue)
|| pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
){
pExpr = pLeft;
op = pExpr->op;
negInt = -1;
zNeg = "-";
}
}
}
if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
pVal = valueNew(db, pCtx);
if( pVal==0 ) goto no_mem;
if( ExprHasProperty(pExpr, EP_IntValue) ){
sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
}else{
i64 iVal;
if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
sqlite3VdbeMemSetInt64(pVal, iVal*negInt);
}else{
zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
if( zVal==0 ) goto no_mem;
sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
}
}
if( affinity==SQLITE_AFF_BLOB ){
if( op==TK_FLOAT ){
assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
sqlite3AtoF(pVal->z, &pVal->u.r);
pVal->flags = MEM_Real;
}else if( op==TK_INTEGER ){
/* This case is required by -9223372036854775808 and other strings
** that look like integers but cannot be handled by the
** sqlite3DecOrHexToI64() call above. */
sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
}
}else{
sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
}
assert( (pVal->flags & MEM_IntReal)==0 );
if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
testcase( pVal->flags & MEM_Int );
testcase( pVal->flags & MEM_Real );
pVal->flags &= ~MEM_Str;
}
if( enc!=SQLITE_UTF8 ){
rc = sqlite3VdbeChangeEncoding(pVal, enc);
}
}else if( op==TK_UMINUS ) {
/* This branch happens for multiple negative signs. Ex: -(-5) */
if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
&& pVal!=0
){
sqlite3VdbeMemNumerify(pVal);
if( pVal->flags & MEM_Real ){
pVal->u.r = -pVal->u.r;
}else if( pVal->u.i==SMALLEST_INT64 ){
#ifndef SQLITE_OMIT_FLOATING_POINT
pVal->u.r = -(double)SMALLEST_INT64;
#else
pVal->u.r = LARGEST_INT64;
#endif
MemSetTypeFlag(pVal, MEM_Real);
}else{
pVal->u.i = -pVal->u.i;
}
sqlite3ValueApplyAffinity(pVal, affinity, enc);
}
}else if( op==TK_NULL ){
pVal = valueNew(db, pCtx);
if( pVal==0 ) goto no_mem;
sqlite3VdbeMemSetNull(pVal);
}
#ifndef SQLITE_OMIT_BLOB_LITERAL
else if( op==TK_BLOB ){
int nVal;
assert( !ExprHasProperty(pExpr, EP_IntValue) );
assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
assert( pExpr->u.zToken[1]=='\'' );
pVal = valueNew(db, pCtx);
if( !pVal ) goto no_mem;
zVal = &pExpr->u.zToken[2];
nVal = sqlite3Strlen30(zVal)-1;
assert( zVal[nVal]=='\'' );
sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
0, SQLITE_DYNAMIC);
}
#endif
#ifdef SQLITE_ENABLE_STAT4
else if( op==TK_FUNCTION && pCtx!=0 ){
rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
}
#endif
else if( op==TK_TRUEFALSE ){
assert( !ExprHasProperty(pExpr, EP_IntValue) );
pVal = valueNew(db, pCtx);
if( pVal ){
pVal->flags = MEM_Int;
pVal->u.i = pExpr->u.zToken[4]==0;
sqlite3ValueApplyAffinity(pVal, affinity, enc);
}
}
*ppVal = pVal;
return rc;
no_mem:
#ifdef SQLITE_ENABLE_STAT4
if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
#endif
sqlite3OomFault(db);
sqlite3DbFree(db, zVal);
assert( *ppVal==0 );
#ifdef SQLITE_ENABLE_STAT4
if( pCtx==0 ) sqlite3ValueFree(pVal);
#else
assert( pCtx==0 ); sqlite3ValueFree(pVal);
#endif
return SQLITE_NOMEM_BKPT;
}
/*
"#;
let e = edges("c", code);
assert!(
e.iter()
.any(|c| c.caller == "valueFromExpr" && c.callee == "valueFromFunction"),
"real call inside a mis-parsed if-block was dropped"
);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_resolves_fn_ptr_alias() {
let code = "void target(char *d) {}\n\
void caller(void) {\n\
void (*fp)(char *) = target;\n\
char *data = 0;\n\
fp(data);\n\
}";
let e = edges("c", code);
assert!(
e.iter()
.any(|c| c.caller == "caller" && c.callee == "target"),
"expected 'target' as a callee of 'caller', got {e:?}"
);
assert!(
!e.iter().any(|c| c.callee == "fp"),
"the raw pointer name 'fp' should never appear as a callee: {e:?}"
);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_resolves_fn_ptr_address_of() {
let code = "void target(int x) {}\n\
void caller(void) {\n\
int (*fp)(int) = ⌖\n\
fp(1);\n\
}";
let e = edges("c", code);
assert!(
e.iter()
.any(|c| c.caller == "caller" && c.callee == "target"),
"expected 'target' as a callee of 'caller', got {e:?}"
);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_plain_pointer_not_aliased() {
let code = "void caller(void) {\n\
int x = 0;\n\
int *p = &x;\n\
*p = 1;\n\
}";
let e = edges("c", code);
assert!(
!e.iter().any(|c| c.callee == "x"),
"plain pointer decl should not produce a callee edge: {e:?}"
);
}
}