CodeFuse-Query Documentation

repository·main·Indexed 18 days ago

https://github.com/codefuse-ai/codefuse-query

A data-centric static code analysis system that transforms source code into a standardized relational model (COREF). It features Gödel, a Datalog-inspired language for complex logic-based queries, and provides source extractors for C-family languages (C, C++, Objective-C), Go, and ArkTS.

Tokens
61.7K
Snippets
166
Records
243
Agent score
57%

What's inside CodeFuse-Query

  1. What is CodeFuse-Query?

    main

    CodeFuse-Query (also referred to as CodeQuery) is a centralized, data-centric static code analysis platform designed for large-scale software development. Unlike traditional tool-centric static analysis tools (like Clang Static Analyzer or PMD) that are often purpose-built for specific objectives, CodeFuse-Query treats source code and analysis results as data and the execution process as big data processing.

    Key Characteristics

    • Highly Scalable: Capable of handling massive codebases and adapting to varying analysis needs (e.g., security-focused taint analysis vs. productivity-focused cyclomatic complexity metrics).
    • Data-Centric: Views code as a highly structured, interconnected dataset, making it suitable for environments where codebases evolve incrementally.
    • Highly Integrated: Designed to integrate with enterprise-grade big data and cloud infrastructure, including:
      • Data warehouses
      • Data computation facilities (e.g., MaxCompute, Hive)
      • OSS object storage
      • Flexible computing resources (e.g., Kubernetes)
    • Supports Diverse Needs: Accommodates Quality of Service (QoS) analysis, cross-language analysis, and varying algorithmic/performance requirements.
  2. Overview of GödelScript Query Language

    main
    GödelScript is the domain-specific query language used within the CodeFuse-Query system for performing data-centric static code analysis. It allows users to write scripts to query the COREF (Code Representation) data model. The language supports structured programming concepts including basic types, functions, statements, schemas, and database interactions to navigate and extract information from code representations.
  3. What is CodeFuse-Query?

    main

    CodeFuse-Query is a data-centric static code analysis platform designed for large-scale and complex codebase analysis. It transforms source code into a standardized data model to enable efficient querying and analysis.

    The platform consists of three main components:

    1. Code Data Model (COREF): A standardized representation of code.
    2. Code Query DSL (Gödel): A logic-based language for querying the COREF data.
    3. Platform Services: Includes the Sparrow CLI for local data generation and querying, and the Query Center for online service experimentation.
  4. Overview of CodeFuse-Query

    main

    CodeFuse-Query is a data-centric static code analysis system designed for large-scale software development. It treats code analysis as a data computation task, enabling high-throughput scanning (up to 10 billion lines of code daily) and support for hundreds of analysis tasks.

    The system is built on three pillars:

    1. COREF Code Data Model: A standardized representation of code.
    2. Gödel Query DSL: A declarative logic-oriented language for querying code data.
    3. Platform Services: Includes the Sparrow CLI for local execution and the Query Center for online queries.
  5. What is the COREF data model?

    main

    COREF is the standardized data model used by CodeFuse-Query to represent code across different programming languages. It ensures that regardless of the source language, the resulting data can be queried using a unified logic.

    COREF components include:

    • AST (Abstract Syntax Tree)
    • ASG (Abstract Semantic Graph)
    • CFG (Control Flow Graph)
    • PDG (Program Dependency Graph)
    • Call Graph
    • Class Hierarchy
    • Documentation (Comments/Information)

    Note: Not all languages support all components. Basic support typically includes AST, ASG, Call Graph, Class Hierarchy, and Documentation. CFG and PDG support is being gradually added.

  6. What is Gödel and why use it instead of SQL or an SDK?

    main

    Gödel is the custom Domain-Specific Language (DSL) used to query COREF data. It is a declarative, logic-based reasoning language built on Datalog.

    Key Characteristics:

    • Declarative: You describe the desired result (the "what") rather than the execution steps (the "how").
    • Monotonic and Terminating: Inherits these properties from Datalog, ensuring high performance and predictable computation.

    Comparison:

    • Vs. SDKs: Gödel is easier to learn and use because it is declarative; users don't need to manage intermediate computations.
    • Vs. SQL: Gödel has stronger descriptive capabilities and faster computation for complex operations like recursive algorithms and multi-table joins, which are difficult or inefficient in SQL.
  7. Overview of GödelScript DSL

    main
    GödelScript is a Domain-Specific Language (DSL) used by CodeQuery for querying and data processing. It features a Rust-like syntax with strict type checking, type inference, and intelligent error reporting. It is designed to provide a more convenient way to write complex queries while avoiding common errors like Soufflé Ungrounded Errors through strict ungrounded (unassigned/unbound) detection.
  8. What is the Godel Kernel for CodeFuse-Query?

    main

    The Godel Kernel is a specialized Jupyter kernel designed for interactive CodeFuse-Query analysis. It provides the following capabilities:

    • Godel Script Execution: Write and execute Godel scripts directly in Jupyter cells.
    • Python Integration: Use %%python or %python magic commands to perform data visualization and post-processing (e.g., using pandas) on query results.
    • Database Configuration: Set the path to the COREF database using magic commands.
    • Output Management: Dump cell outputs to specific files.
    • CLI Integration: The kernel automatically handles running the sparrow query run command behind the scenes when you execute Godel scripts.
  9. Understand the COREF Data Model relations

    main

    The COREF (Code Relation) data model represents code structures as relations. The available relations vary depending on the programming language being analyzed. Use these relations to build queries that traverse code hierarchies, identify declarations, and map locations.

    Java Relations

    • field(id, tid, name, locId): A field with its ID, type ID, name, and location ID.
    • method(id, tid, name): A method with its ID, type ID, and name.
    • parent(id1, id2): A parent-child relationship between two entities.
    • class(id, name): A class definition.
    • interface(id, name): An interface definition.
    • type(id, n): A type definition.
    • javaLoc(locId, s, e, path): Location details (start line, end line, file path).

    XML Relations

    • xmlEle(): XML element.
    • xmlAttri(): XML attribute.

    JS/TS Relations

    • enclose(exprId, methodId): An expression enclosed within a method.
    • callExpr(exprId, methodId, argExprId, callSiteId): A function call expression.
    • reference(refId, defId): A reference to a definition.
    • service(id, appId, locId): A service associated with an app and location.
    • jsLoc(locId, s, e, path): Location details (start line, end line, file path).
    • app(id, name): An application definition.
    • api(id, apiName, apiSignature, appId): An API definition.
    • functionLikeDeclaration(id, name): A function-like declaration.
    • parent(id1, id2): Parent-child relationship.
    • class(id, name): Class definition.
    // Example Java Relations
    field(id, tid, name, locId)
    method(id, tid, name)
    parent(id1, id2)
    class(id, name)
    interface(id, name)
    type(id, n)
    javaLoc(locId, s, e, path)
    
    // Example JS/TS Relations
    enclose(exprId, methodId)
    callExpr(exprId, methodId, argExprId, callSiteId)
    reference(refId, defId)
    service(id, appId, locId)
    jsLoc(locId, s, e, path)
    app(id, name)
    api(id, apiName, apiSignature, appId)
    functionLikeDeclaration(id, name)
    parent(id1, id2)
    class(id, name)
  10. How the Gödel query DSL works

    main

    CodeFuse-Query uses a custom Domain Specific Language (DSL) called Gödel to perform code analysis on the COREF data.

    Key Characteristics of Gödel:

    • Logic-based: It is a logic reasoning language based on Datalog. It works by describing "facts" and "rules" to derive new facts.
    • Declarative: Users describe what they want to find rather than how to compute it, leaving the implementation to the engine.
    • Performance: By inheriting Datalog's properties, it offers monotonicity and termination, providing higher performance for complex queries compared to standard SQL.

    Comparison:

    • vs. SDK: Gödel is easier to learn and use because it is declarative; users don't need to manage intermediate computation steps.
    • vs. SQL: Gödel has stronger expressive power and faster computation for complex tasks like recursive algorithms and multi-table joins, which are difficult in SQL.