Skip to content

Commit 3d378a3

Browse files
committed
tools/clang/codesearch: scape type string in TraverseFunctionDecl
Fixes #6680
1 parent 40acda8 commit 3d378a3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tools/clang/codesearch/codesearch.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,28 @@ Indexer::NamedDeclEmitter::~NamedDeclEmitter() {
181181
Parent->Out.emit(std::move(Def));
182182
}
183183

184+
// Helper to escape quotes for JSON
185+
static std::string EscapeTypeString(std::string s) {
186+
std::string result;
187+
result.reserve(s.size() + 10); // reserve a little extra space
188+
for (char c : s) {
189+
if (c == '"') {
190+
result += "\\\""; // Turn " into \"
191+
} else if (c == '\\') {
192+
result += "\\\\"; // Turn \ into \\
193+
} else {
194+
result += c;
195+
}
196+
}
197+
return result;
198+
}
199+
184200
bool Indexer::TraverseFunctionDecl(FunctionDecl* Func) {
185201
if (!Func->doesThisDeclarationHaveABody())
186202
return Base::TraverseFunctionDecl(Func);
187-
NamedDeclEmitter Emitter(this, Func, EntityKindFunction, Func->getType().getAsString(), Func->isStatic());
203+
// Escape the type string to handle quotes in attributes like btf_type_tag("user")
204+
std::string typeStr = EscapeTypeString(Func->getType().getAsString());
205+
NamedDeclEmitter Emitter(this, Func, EntityKindFunction, typeStr, Func->isStatic());
188206
return Base::TraverseFunctionDecl(Func);
189207
}
190208

0 commit comments

Comments
 (0)