Searching through massive codebases for specific patterns is a daily reality for developers, whether they are refactoring legacy code, hunting for security vulnerabilities, or trying to understand a new API. While standard IDEs offer basic string matching, they often lack the "semantic depth" needed for complex patterns. On the other end of the spectrum, Datalog-based program analyzers like CodeQL are incredibly powerful but require users to master a steep learning curve and write intricate relational queries.
A new study introduces Squid, a multi-modal synthesis algorithm that bridges this gap. Squid allows developers to generate complex conjunctive queries using nothing more than a few code examples and a simple natural language description.
The Usability-Capability Gap in Code Search
The core challenge in advanced code search is the usability-capability gap. Mainstream IDEs are easy to use but limited to structural or string-based searches. Static program analyzers provide deep "program facts"—relations like "this method calls that function" or "this variable has this type"—but accessing them requires writing formal queries. For a tool like CodeQL, a user might have to navigate over a hundred different relations and thousands of attributes.
Writing these queries is error-prone and time-consuming. The search space for an automated synthesizer is even worse—it's effectively infinite. Previous attempts at query synthesis often suffered from over-fitting or poor performance when faced with large relational schemas.
The Squid Pipeline
Squid employs a three-stage pipeline to turn user intent into a formal query, combining positive/negative code examples with natural language descriptions. This multi-modal approach ensures that the resulting query is both technically accurate and semantically aligned with the user's intent.
1. Representation Reduction
Instead of looking at all 170+ possible relations in a Datalog schema, Squid identifies "dummy relations." A relation is considered "dummy" if it cannot help distinguish a positive example from a negative one. By discarding these early, Squid dramatically narrows the search space. Most relations in a complex program analysis schema are irrelevant to any single specific search; identifying these via undirected relation paths makes the synthesis tractable.
2. Bounded Refinement
Squid treats query synthesis as a graph enumeration problem, building a "query graph" step-by-step. It utilizes a "refinability" property: if a partial query already excludes a positive example, Squid stops exploring that branch, as adding more constraints will never bring that positive example back. By limiting the "multiplicity" (how many times a single relation can appear in a query), Squid handles the reality that most useful code search queries are relatively concise.
3. Candidate Selection via Dual Metrics
To avoid over-fitting, Squid prioritizes queries based on two quantitative metrics:
- Named Entity Coverage: This uses natural language as a semantic anchor. It measures how many keywords from the user’s description (e.g., "method," "parameter," "return") are reflected in the query's attributes.
- Structural Complexity: Following Occam’s Razor, Squid prefers the simplest query that satisfies the constraints.
Evaluation and Results
The authors evaluated Squid on a benchmark of 31 code search tasks covering variables, expressions, statements, methods, and classes. Tasks included both literature-based examples and real-world engineering demands, such as auditing log4j usage for security compliance. The underlying relational representation for the Java programs consisted of 173 relations and 1,093 attributes.
Squid achieved a 100% success rate, synthesizing correct queries for all 31 tasks with an average synthesis time of just 2.56 seconds. Ablation studies confirmed the importance of "representation reduction," without which the average time tripled and several tasks timed out. Furthermore, Squid significantly outperformed existing Datalog synthesizers like EGS, which solved fewer than half of the tasks.
Limitations and Future Directions
While highly effective, Squid has specific boundaries. It currently focuses on conjunctive ("AND") logic and does not support disjunctions ("OR"). It also relies on a pre-defined multiplicity bound (usually $K=2$), which might exclude extremely complex queries. Future work aims to support disjunctive queries and expand the tool's application to domains like serverless functions and network configurations.
In conclusion, Squid represents a significant step toward making advanced program analysis accessible to general developers by automating the bridge between high-level intent and formal relational queries.
Summary updated