Generating Complex Code Analyzers from Natural Language Questions

Amirmohammad Nazari, Sadra Sabouri, Wang Bill Zhu, Robin Jia, Souti Chattopadhyay, Mukund RaghothamanarXiv 2026

TL;DR

Merlin is a novel system that allows developers to search large codebases using natural language questions by automatically translating them into complex CodeQL queries. By combining Large Language Models (LLMs) with an iterative "self-test" and "assistive query" debugging loop, Merlin achieves a 3.8x increase in bug-finding accuracy while reducing developer effort by 31%.

What Problem Does This Paper Address?

When developers work on massive codebases—spanning millions of lines across thousands of files—simple tools like grep fall short. They can find text, but they can't answer deep analytical questions like, "Which constructors in my codebase call overridable methods?" Such questions require understanding the program's global structure, types, and call relationships.

While LLMs like GPT-4 or Claude are great at answering questions, they struggle with large-scale code due to context window limits and a tendency to hallucinate. Conversely, professional program analysis frameworks like CodeQL are incredibly powerful and precise but have a notoriously steep learning curve. Developers often know what they want to find but don't know how to write the complex, SQL-like logic required by CodeQL to find it.

Methods

Merlin acts as a bridge, using an LLM to synthesize CodeQL queries from natural language. However, simply asking an LLM to write CodeQL often results in queries that either don't compile or return no results. To solve this, the authors developed a sophisticated workflow:

  1. Iterative RAG & Preprocessing: Merlin first identifies keywords in the user's question and retrieves relevant CodeQL documentation. It then prompts the LLM to generate "self-test" code snippets—small examples that should or should not satisfy the user's goal.
  2. Syntax Correction Loop: The system uses compiler feedback to iteratively fix syntax errors in the generated query.
  3. Semantic Debugging with Assistive Queries: This is Merlin's "secret sauce." If a query compiles but fails the self-tests (e.g., it returns an empty result when it shouldn't), Merlin generates assistive queries. These act like diagnostic "print statements," helping the LLM explore the codebase's internal properties (like variable types or call graphs) to understand why the main query is failing and how to refine it.
Figure 3: Overall architecture of Merlin. Merlin first uses an LLM to retrieve relevant documentation and generate test cases that reflect the user’s goal. It then repeatedly uses the LLM to generate a candidate CodeQL query and addresses syntax errors using RAG-based debugging and resolves semantic errors by issuing assistive queries. The final query is executed the entire codebase in order to produce the final table that is presented to the user.

Datasets

The authors evaluated Merlin using a robust suite of benchmarks:

  • SpotBugs: 142 detectors from a popular open-source Java static analyzer.
  • GitHub Security Lab: 40 complex CodeQL queries designed to find security vulnerabilities across multiple languages (Java, C++, C#, JavaScript).
  • Real-world Codebases: 13 large projects ranging from 220 to over 5,700 files, totaling up to 6.4 million lines of code.

Key Insights and Ideas

  • CodeQL as a "Certificate": Unlike a chat-based LLM that might give a non-deterministic answer, Merlin provides the actual CodeQL query. This makes the analysis auditable, repeatable, and exhaustive across the entire codebase.
  • The Power of Self-Tests: By forcing the LLM to write its own test cases (Figure 4a), Merlin creates a "litmus test" for the synthesized analyzer. This prevents the system from returning "empty" results caused by subtle semantic mismatches.
  • Assistive Queries for Discovery: The authors found that LLMs often fail because they don't know which specific library classes to use. Assistive queries (Figure 4d) allow the LLM to "probe" the codebase to discover the correct relationships before finalizing the main query.
Figure 4: Our running example in Section 3. (4a): The test case generated by the LLMs with an example use of Object.equals() to compare arrays. The snippet contains three violations of the coding guideline, when each of the variables r1, r2 and r3 are initialized. (4e): The final CodeQL query generated by Merlin which successfully detects all three violations of the guideline. (4b): The first CodeQL query in Merlin’s workflow, which contains syntactic errors. (4c): Initial syntactically correct suggestion. Note that because this query is looking specifically for comparisons between arrays of type int[], it does not detect any of the violations in the test case. (4d): The assistive query generated by Merlin. It prints the types of o1 and o2 for every method invocation of the form o1.equals(o2), and helps the LLM to discover that the objects o1 and o2, despite needing to be arrays, need not necessarily only contain integers.

Key Results

Merlin was tested against state-of-the-art baselines like Gemini 1.5 Pro and Claude Code.

  • Higher Accuracy: In a user study, developers with access to Merlin were 3.8x more accurate at identifying coding violations than those using standard tools or general-purpose AI assistants.
  • Faster Resolution: Users completed their tasks 31% faster with Merlin.
  • Superior Precision/Recall: When compared against SpotBugs, Merlin discovered many genuine bugs that the baseline tool missed. Human experts confirmed that Merlin achieved both higher precision and higher recall.
  • Efficiency: Because Merlin only sends the query logic to the LLM—not the entire codebase—it uses significantly less context and avoids the privacy and cost issues associated with uploading millions of lines of code to a cloud provider.

Limitations and Future Work

While powerful, Merlin has some notable boundaries:

  • Static Analysis Only: It is currently limited to what the CodeQL backend can analyze. It cannot handle dynamic properties that only appear during program execution.
  • Vague Predicates: The system struggles with subjective terms like "untrusted source" or "suspicious method," which require more interactive clarification from the user.
  • User Study Scale: The study involved 18 participants, primarily students. The authors suggest that testing with a larger, more diverse group of professional engineers is a key next step.
  • LLM Dependency: The system currently relies on GPT-4o; exploring how it performs with open-source models would improve accessibility and reproducibility.

Summary updated

BibTeX
@misc{nazari2026generating,
  author = {Amirmohammad Nazari and Sadra Sabouri and Wang Bill Zhu and Robin Jia and Souti Chattopadhyay and Mukund Raghothaman},
  title = {{Generating Complex Code Analyzers from Natural Language Questions}},
  year = {2026},
  eprint = {2605.09304},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.09304},
}
Figure