From fd36daf55d7f9e8ab412fcbf0441658a8d8be727 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 5 Feb 2026 13:59:08 +0100 Subject: tools/clang: compile clang tools into the binary Compiled clang tools into Go binaries using cgo. This significantly simplifies building and deployment. This also enables unit testing of clang tools. Now raw go test for clang tools will build them, run, and verify output. Each clang tool is still started as a subprocess. I've experimented with running them in-process, but this makes stdout/stderr interception extremly complicated, and it seems that clang tools still use unsynchronized global state, which breaks when invoked multiple times. Subprocesses also make it safer in the face of potential memory leaks, or memory corruptions in clang tools. Fixes #6645 --- tools/clang/codesearch/codesearch.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'tools/clang/codesearch/codesearch.cpp') diff --git a/tools/clang/codesearch/codesearch.cpp b/tools/clang/codesearch/codesearch.cpp index 047edad10..a2d6883a2 100644 --- a/tools/clang/codesearch/codesearch.cpp +++ b/tools/clang/codesearch/codesearch.cpp @@ -1,6 +1,9 @@ // Copyright 2025 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. +// Clang-based tool that indexes kernel source code to power +// pkg/aflow/tool/codesearcher/codesearcher.go agentic tool. + #include "json.h" #include "output.h" @@ -12,6 +15,7 @@ #include "clang/AST/RecordLayout.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/Basic/SourceManager.h" +#include "clang/Basic/Version.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" @@ -311,7 +315,11 @@ bool Indexer::TraverseRecordDecl(RecordDecl* Decl) { uint64_t OffsetInBits = Layout.getFieldOffset(Field->getFieldIndex()); uint64_t SizeInBits; if (Field->isBitField()) { - SizeInBits = Field->getBitWidthValue(Context); + SizeInBits = Field->getBitWidthValue( +#if CLANG_VERSION_MAJOR == 19 + Context +#endif + ); } else { TypeInfo Info = Context.getTypeInfo(Field->getType()); SizeInBits = Info.Width; @@ -338,8 +346,8 @@ bool Indexer::TraverseTypedefDecl(TypedefDecl* Decl) { return Base::TraverseTypedefDecl(Decl); } -int main(int argc, const char** argv) { - llvm::cl::OptionCategory Options("syz-indexer options"); +static int Main(int argc, const char** argv) { + llvm::cl::OptionCategory Options("codesearch options"); auto OptionsParser = tooling::CommonOptionsParser::create(argc, argv, Options); if (!OptionsParser) { llvm::errs() << OptionsParser.takeError(); @@ -351,5 +359,12 @@ int main(int argc, const char** argv) { if (Tool.run(tooling::newFrontendActionFactory(&Instance, &Instance).get())) return 1; Output.print(); + fflush(stdout); return 0; } + +__attribute__((constructor(1000))) static void ctor(int argc, const char** argv) { + const char* run = getenv("SYZ_RUN_CLANGTOOL"); + if (run && !strcmp(run, "codesearch")) + exit(Main(argc, argv)); +} -- cgit mrf-deployment