Skip to content

Commit 42b721d

Browse files
committed
Enhance Clang AST dumping
This commit extends our existing Clang AST dumping to include more information, such as a cursor's canonical, referenced, and declarations cursors if they exist. It prints out most of the information that libclang gives us directly, but not the information that we attempt to (re)construct on top of the libclang APIs.
1 parent c1aaa6a commit 42b721d

File tree

1 file changed

+144
-9
lines changed

1 file changed

+144
-9
lines changed

src/clang.rs

Lines changed: 144 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,21 +1273,156 @@ pub fn type_to_str(x: CXTypeKind) -> String {
12731273

12741274
/// Dump the Clang AST to stdout for debugging purposes.
12751275
pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
1276-
fn print_indent(depth: isize, s: &str) {
1276+
fn print_indent<S: AsRef<str>>(depth: isize, s: S) {
12771277
for _ in 0..depth {
1278-
print!("\t");
1278+
print!(" ");
1279+
}
1280+
println!("{}", s.as_ref());
1281+
}
1282+
1283+
fn print_cursor<S: AsRef<str>>(depth: isize, prefix: S, c: &Cursor) {
1284+
let prefix = prefix.as_ref();
1285+
print_indent(depth, format!(" {}kind = {}", prefix, kind_to_str(c.kind())));
1286+
print_indent(depth, format!(" {}spelling = \"{}\"", prefix, c.spelling()));
1287+
print_indent(depth, format!(" {}location = {}", prefix, c.location()));
1288+
print_indent(depth, format!(" {}is-definition? {}", prefix, c.is_definition()));
1289+
print_indent(depth, format!(" {}is-declaration? {}", prefix, c.is_declaration()));
1290+
print_indent(depth, format!(" {}is-anonymous? {}", prefix, c.is_anonymous()));
1291+
print_indent(depth, format!(" {}is-inlined-function? {}", prefix, c.is_inlined_function()));
1292+
1293+
let templ_kind = c.template_kind();
1294+
if templ_kind != CXCursor_NoDeclFound {
1295+
print_indent(depth, format!(" {}template-kind = {}", prefix, kind_to_str(templ_kind)));
1296+
}
1297+
if let Some(usr) = c.usr() {
1298+
print_indent(depth, format!(" {}usr = \"{}\"", prefix, usr));
1299+
}
1300+
if let Ok(num) = c.num_args() {
1301+
print_indent(depth, format!(" {}number-of-args = {}", prefix, num));
1302+
}
1303+
if let Some(num) = c.num_template_args() {
1304+
print_indent(depth, format!(" {}number-of-template-args = {}", prefix, num));
1305+
}
1306+
if let Some(width) = c.bit_width() {
1307+
print_indent(depth, format!(" {}bit-width = {}", prefix, width));
1308+
}
1309+
if let Some(ty) = c.enum_type() {
1310+
print_indent(depth, format!(" {}enum-type = {}", prefix, type_to_str(ty.kind())));
1311+
}
1312+
if let Some(val) = c.enum_val_signed() {
1313+
print_indent(depth, format!(" {}enum-val = {}", prefix, val));
1314+
}
1315+
if let Some(ty) = c.typedef_type() {
1316+
print_indent(depth, format!(" {}typedef-type = {}", prefix, type_to_str(ty.kind())));
1317+
}
1318+
1319+
if let Some(def) = c.definition() {
1320+
if def != *c {
1321+
println!();
1322+
print_cursor(depth, String::from(prefix) + "definition.", &def);
1323+
}
1324+
}
1325+
1326+
if let Some(refd) = c.referenced() {
1327+
if refd != *c {
1328+
println!();
1329+
print_cursor(depth, String::from(prefix) + "referenced.", &refd);
1330+
}
1331+
}
1332+
1333+
let canonical = c.canonical();
1334+
if canonical != *c {
1335+
println!();
1336+
print_cursor(depth, String::from(prefix) + "canonical.", &canonical);
1337+
}
1338+
1339+
if let Some(specialized) = c.specialized() {
1340+
if specialized != *c {
1341+
println!();
1342+
print_cursor(depth, String::from(prefix) + "specialized.", &specialized);
1343+
}
1344+
}
1345+
}
1346+
1347+
fn print_type<S: AsRef<str>>(depth: isize, prefix: S, ty: &Type) {
1348+
let prefix = prefix.as_ref();
1349+
1350+
let kind = ty.kind();
1351+
print_indent(depth, format!(" {}kind = {}", prefix, type_to_str(kind)));
1352+
if kind == CXType_Invalid {
1353+
return;
1354+
}
1355+
1356+
print_indent(depth, format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
1357+
let num_template_args = unsafe {
1358+
clang_Type_getNumTemplateArguments(ty.x)
1359+
};
1360+
if num_template_args >= 0 {
1361+
print_indent(depth, format!(" {}number-of-template-args = {}",
1362+
prefix,
1363+
num_template_args));
1364+
}
1365+
if let Some(num) = ty.num_elements() {
1366+
print_indent(depth, format!(" {}number-of-elements = {}", prefix, num));
1367+
}
1368+
print_indent(depth, format!(" {}is-variadic? {}", prefix, ty.is_variadic()));
1369+
1370+
let canonical = ty.canonical_type();
1371+
if canonical != *ty {
1372+
println!();
1373+
print_type(depth, String::from(prefix) + "canonical.", &canonical);
1374+
}
1375+
1376+
if let Some(pointee) = ty.pointee_type() {
1377+
if pointee != *ty {
1378+
println!();
1379+
print_type(depth, String::from(prefix) + "pointee.", &pointee);
1380+
}
1381+
}
1382+
1383+
if let Some(elem) = ty.elem_type() {
1384+
if elem != *ty {
1385+
println!();
1386+
print_type(depth, String::from(prefix) + "elements.", &elem);
1387+
}
1388+
}
1389+
1390+
if let Some(ret) = ty.ret_type() {
1391+
if ret != *ty {
1392+
println!();
1393+
print_type(depth, String::from(prefix) + "return.", &ret);
1394+
}
1395+
}
1396+
1397+
let named = ty.named();
1398+
if named != *ty && named.is_valid() {
1399+
println!();
1400+
print_type(depth, String::from(prefix) + "named.", &named);
12791401
}
1280-
println!("{}", s);
12811402
}
12821403

1283-
print_indent(depth,
1284-
&format!("(kind: {}, spelling: {}, type: {}",
1285-
kind_to_str(c.kind()),
1286-
c.spelling(),
1287-
type_to_str(c.cur_type().kind())));
1404+
print_indent(depth, "(");
1405+
print_cursor(depth, "", c);
1406+
1407+
println!();
1408+
let ty = c.cur_type();
1409+
print_type(depth, "type.", &ty);
1410+
1411+
let declaration = ty.declaration();
1412+
if declaration != *c && declaration.kind() != CXCursor_NoDeclFound {
1413+
println!();
1414+
print_cursor(depth, "type.declaration.", &declaration);
1415+
}
12881416

12891417
// Recurse.
1290-
c.visit(|s| ast_dump(&s, depth + 1));
1418+
let mut found_children = false;
1419+
c.visit(|s| {
1420+
if !found_children {
1421+
println!();
1422+
found_children = true;
1423+
}
1424+
ast_dump(&s, depth + 1)
1425+
});
12911426

12921427
print_indent(depth, ")");
12931428

0 commit comments

Comments
 (0)