1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
// 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.
//go:build linux
#include "json.h"
#include "output.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Comment.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclarationName.h"
#include "clang/AST/ParentMapContext.h"
#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"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include <algorithm>
#include <filesystem>
#include <string>
#include <unordered_map>
using namespace clang;
// MacroDef/MacroMap hold information about macros defined in the file.
struct MacroDef {
std::string Value; // value as written in the source
SourceRange Range; // source range of the value
};
using MacroMap = std::unordered_map<std::string, MacroDef>;
class Instance : public tooling::SourceFileCallbacks {
public:
Instance(Output& Output) : Out(Output) {}
std::unique_ptr<ASTConsumer> newASTConsumer();
private:
Output& Out;
MacroMap Macros;
bool handleBeginSource(CompilerInstance& CI) override;
};
// PPCallbacksTracker records all macro definitions (name/value/source location).
class PPCallbacksTracker : public PPCallbacks {
public:
PPCallbacksTracker(Preprocessor& PP, MacroMap& Macros) : SM(PP.getSourceManager()), Macros(Macros) {}
private:
SourceManager& SM;
MacroMap& Macros;
void MacroDefined(const Token& MacroName, const MacroDirective* MD) override { (void)Macros; }
};
class IndexerAstConsumer : public ASTConsumer {
public:
IndexerAstConsumer(Output& Output, const MacroMap& Macros) : Out(Output), Macros(Macros) {}
private:
Output& Out;
const MacroMap& Macros;
void HandleTranslationUnit(ASTContext& context) override;
};
class Indexer : public RecursiveASTVisitor<Indexer> {
public:
Indexer(ASTContext& Context, Output& Output, const MacroMap& Macros)
: Context(Context), SM(Context.getSourceManager()), Out(Output) {}
bool TraverseFunctionDecl(FunctionDecl*);
bool TraverseRecordDecl(RecordDecl*);
bool TraverseEnumDecl(EnumDecl*);
bool TraverseTypedefDecl(TypedefDecl*);
bool TraverseCallExpr(CallExpr*);
bool TraverseCStyleCastExpr(CStyleCastExpr*);
bool TraverseVarDecl(VarDecl*);
bool TraverseParmVarDecl(ParmVarDecl*);
bool VisitDeclRefExpr(const DeclRefExpr*);
bool VisitTagType(const TagType*);
bool VisitTypedefType(const TypedefType*);
bool VisitMemberExpr(const MemberExpr*);
private:
ASTContext& Context;
SourceManager& SM;
Output& Out;
Definition* Current = nullptr;
bool InCallee = false;
// If set, record references to struct types as uses.
SourceLocation TypeRefingLocation;
const Stmt* GetParent(const Stmt* S) const;
void EmitReference(SourceLocation Loc, const NamedDecl* Named, const char* EntityKind, const char* RefKind);
void EmitReference(SourceLocation Loc, const std::string& Name, const char* EntityKind, const char* RefKind);
struct NamedDeclEmitter {
NamedDeclEmitter(Indexer* Parent, const NamedDecl* Decl, const char* Kind, const std::string& Type, bool IsStatic);
~NamedDeclEmitter();
Indexer* const Parent;
ASTContext& Context;
SourceManager& SM;
const NamedDecl* const Decl;
Definition Def;
Definition* SavedCurrent = nullptr;
};
using Base = RecursiveASTVisitor<Indexer>;
};
template <typename T> struct ScopedState {
T* const Var;
T Saved;
ScopedState(T* Var, T ScopeValue) : Var(Var), Saved(*Var) { *Var = ScopeValue; }
~ScopedState() { *Var = Saved; }
};
bool Instance::handleBeginSource(CompilerInstance& CI) {
Preprocessor& PP = CI.getPreprocessor();
PP.addPPCallbacks(std::make_unique<PPCallbacksTracker>(PP, Macros));
return true;
}
std::unique_ptr<ASTConsumer> Instance::newASTConsumer() { return std::make_unique<IndexerAstConsumer>(Out, Macros); }
void IndexerAstConsumer::HandleTranslationUnit(ASTContext& Context) {
Indexer Indexer(Context, Out, Macros);
Indexer.TraverseDecl(Context.getTranslationUnitDecl());
}
Indexer::NamedDeclEmitter::NamedDeclEmitter(Indexer* Parent, const NamedDecl* Decl, const char* Kind,
const std::string& Type, bool IsStatic)
: Parent(Parent), Context(Parent->Context), SM(Parent->SM), Decl(Decl) {
auto Range = Decl->getSourceRange();
const std::string& SourceFile = std::filesystem::relative(SM.getFilename(SM.getExpansionLoc(Range.getBegin())).str());
int StartLine = SM.getExpansionLineNumber(Range.getBegin());
int EndLine = SM.getExpansionLineNumber(Range.getEnd());
std::string CommentSourceFile;
int CommentStartLine = 0;
int CommentEndLine = 0;
if (auto Comment = Context.getRawCommentForDeclNoCache(Decl)) {
const auto& begin = Comment->getBeginLoc();
const auto& end = Comment->getEndLoc();
CommentSourceFile = std::filesystem::relative(SM.getFilename(SM.getExpansionLoc(begin)).str());
CommentStartLine = SM.getExpansionLineNumber(begin);
CommentEndLine = SM.getExpansionLineNumber(end);
// Expand body range to include the comment, if they intersect.
if (SourceFile == CommentSourceFile &&
std::max(StartLine, CommentStartLine) <= std::min(EndLine, CommentEndLine) + 1) {
StartLine = std::min(StartLine, CommentStartLine);
EndLine = std::max(EndLine, CommentEndLine);
}
}
// Clang's SourceRange begin and end locations can resolve to different files
// when a declaration is produced by nested macro expansions (e.g. Linux kernel
// DEFINE_MUTEX, DEFINE_PER_CPU). This happens because getExpansionLoc()
// resolves each token independently to its outermost call site, and with
// multi-level macros the opening and closing tokens of the range may originate
// from different headers. EndLine < StartLine can occur when the end location
// is invalid or synthetic (e.g. compiler-generated declarations). In both
// cases, clamp to a single line so the database always records a valid,
// same-file range anchored at the macro invocation site.
if (EndLine < StartLine ||
SM.getFileID(SM.getExpansionLoc(Range.getBegin())) != SM.getFileID(SM.getExpansionLoc(Range.getEnd()))) {
EndLine = StartLine;
}
Def = Definition{
.Kind = Kind,
.Name = Decl->getNameAsString(),
.Type = Type,
.IsStatic = IsStatic,
.Body =
LineRange{
.File = SourceFile,
.StartLine = StartLine,
.EndLine = EndLine,
},
.Comment =
LineRange{
.File = CommentSourceFile,
.StartLine = CommentStartLine,
.EndLine = CommentEndLine,
},
};
SavedCurrent = Parent->Current;
Parent->Current = &Def;
}
Indexer::NamedDeclEmitter::~NamedDeclEmitter() {
Parent->Current = SavedCurrent;
if (!Def.Name.empty())
Parent->Out.emit(std::move(Def));
}
bool Indexer::TraverseFunctionDecl(FunctionDecl* Func) {
if (!Func->doesThisDeclarationHaveABody())
return Base::TraverseFunctionDecl(Func);
NamedDeclEmitter Emitter(this, Func, EntityKindFunction, Func->getType().getAsString(), Func->isStatic());
return Base::TraverseFunctionDecl(Func);
}
bool Indexer::TraverseCallExpr(CallExpr* CE) {
{
ScopedState<bool> Scoped(&InCallee, true);
TraverseStmt(CE->getCallee());
}
for (auto* Arg : CE->arguments())
TraverseStmt(Arg);
return true;
}
bool Indexer::VisitDeclRefExpr(const DeclRefExpr* DeclRef) {
if (const auto* Func = dyn_cast_if_present<FunctionDecl>(DeclRef->getDecl()))
EmitReference(DeclRef->getBeginLoc(), DeclRef->getDecl(), EntityKindFunction,
InCallee ? RefKindCall : RefKindTakesAddr);
return true;
}
bool Indexer::TraverseVarDecl(VarDecl* Decl) {
if (Decl->isFileVarDecl() && Decl->isThisDeclarationADefinition() == VarDecl::Definition) {
// Preserves whether this variable can be referenced from other translation
// units. A static global (internal linkage) is only referenceable within
// its own file, while a non-static global (external linkage) can be
// referenced from anywhere in the kernel.
const bool IsInternalLinkage = Decl->getStorageClass() == SC_Static;
NamedDeclEmitter Emitter(this, Decl, EntityKindGlobalVariable, Decl->getType().getAsString(), IsInternalLinkage);
}
ScopedState<SourceLocation> Scoped(&TypeRefingLocation, Decl->getBeginLoc());
return Base::TraverseVarDecl(Decl);
}
bool Indexer::TraverseParmVarDecl(ParmVarDecl* Decl) {
ScopedState<SourceLocation> Scoped(&TypeRefingLocation, Decl->getBeginLoc());
return Base::TraverseParmVarDecl(Decl);
}
bool Indexer::TraverseCStyleCastExpr(CStyleCastExpr* Cast) {
ScopedState<SourceLocation> Scoped(&TypeRefingLocation, Cast->getBeginLoc());
return Base::TraverseCStyleCastExpr(Cast);
}
bool Indexer::VisitTagType(const TagType* T) {
if (TypeRefingLocation.isInvalid())
return true;
const auto* Tag = T->getAsTagDecl();
const char* EntityKind = nullptr;
if (Tag->isStruct())
EntityKind = EntityKindStruct;
else if (Tag->isUnion())
EntityKind = EntityKindUnion;
else if (Tag->isEnum())
EntityKind = EntityKindEnum;
else
return true;
EmitReference(TypeRefingLocation, Tag, EntityKind, RefKindUses);
return true;
}
bool Indexer::VisitTypedefType(const TypedefType* T) {
if (TypeRefingLocation.isInvalid())
return true;
EmitReference(TypeRefingLocation, T->getDecl(), EntityKindTypedef, RefKindUses);
// If it's a struct typedef, also note the struct use.
if (const auto* Tag = dyn_cast_if_present<TagType>(T->getCanonicalTypeInternal().getTypePtr()))
VisitTagType(Tag);
return true;
}
bool Indexer::VisitMemberExpr(const MemberExpr* E) {
auto* Record = E->getBase()->getType()->getAsRecordDecl();
if (auto* Ptr = dyn_cast_if_present<PointerType>(E->getBase()->getType()))
Record = Ptr->getPointeeType()->getAsRecordDecl();
if (!Record)
return true;
const std::string Field = Record->getNameAsString() + "::" + E->getMemberDecl()->getNameAsString();
const char* RefKind = RefKindRead;
const Stmt* P = GetParent(E);
if (auto* BO = dyn_cast_if_present<BinaryOperator>(P)) {
if (E == BO->getLHS() && (BO->isAssignmentOp() || BO->isCompoundAssignmentOp() || BO->isShiftAssignOp()))
RefKind = RefKindWrite;
}
if (auto* UO = dyn_cast_if_present<UnaryOperator>(P))
RefKind = RefKindTakesAddr;
EmitReference(E->getMemberLoc(), Field, EntityKindField, RefKind);
return true;
}
const Stmt* Indexer::GetParent(const Stmt* S) const {
for (;;) {
const auto& Parents = Context.getParents(*S);
if (!Parents.empty())
S = Parents[0].get<Stmt>();
else
S = nullptr;
// Presumably ParentExpr is never interesting.
if (S && isa<ParenExpr>(S))
continue;
return S;
}
}
void Indexer::EmitReference(SourceLocation Loc, const NamedDecl* Named, const char* EntityKind, const char* RefKind) {
if (Named)
EmitReference(Loc, Named->getNameAsString(), EntityKind, RefKind);
}
void Indexer::EmitReference(SourceLocation Loc, const std::string& Name, const char* EntityKind, const char* RefKind) {
if (!Current || Name.empty())
return;
Current->Refs.push_back(Reference{
.Kind = RefKind,
.EntityKind = EntityKind,
.Name = Name,
.Line = static_cast<int>(SM.getExpansionLineNumber(Loc)),
});
}
bool Indexer::TraverseRecordDecl(RecordDecl* Decl) {
if (!Decl->isThisDeclarationADefinition())
return Base::TraverseRecordDecl(Decl);
NamedDeclEmitter Emitter(this, Decl, Decl->isStruct() ? EntityKindStruct : EntityKindUnion, "", false);
if (Decl->isCompleteDefinition()) {
const auto& Layout = Context.getASTRecordLayout(Decl);
for (const auto* Field : Decl->fields()) {
uint64_t OffsetInBits = Layout.getFieldOffset(Field->getFieldIndex());
uint64_t SizeInBits;
if (Field->isBitField()) {
SizeInBits = Field->getBitWidthValue(
#if CLANG_VERSION_MAJOR == 19
Context
#endif
);
} else {
TypeInfo Info = Context.getTypeInfo(Field->getType());
SizeInBits = Info.Width;
}
Emitter.Def.Fields.push_back(FieldInfo{
.Name = Field->getNameAsString(),
.OffsetBits = OffsetInBits,
.SizeBits = SizeInBits,
});
}
}
return Base::TraverseRecordDecl(Decl);
}
bool Indexer::TraverseEnumDecl(EnumDecl* Decl) {
if (!Decl->isThisDeclarationADefinition())
return Base::TraverseEnumDecl(Decl);
NamedDeclEmitter Emitter(this, Decl, EntityKindEnum, "", false);
return Base::TraverseEnumDecl(Decl);
}
bool Indexer::TraverseTypedefDecl(TypedefDecl* Decl) {
NamedDeclEmitter Emitter(this, Decl, EntityKindTypedef, "", false);
return Base::TraverseTypedefDecl(Decl);
}
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();
return 1;
}
Output Output;
Instance Instance(Output);
tooling::ClangTool Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList());
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));
}
|