Skip to content

Commit 4c45407

Browse files
author
bors-servo
authored
Auto merge of #469 - fitzgen:enhanced-clang-ast-dumping, r=emilio
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. r? @emilio or @Yamakaky
2 parents b10ba43 + 67ba2ad commit 4c45407

File tree

1 file changed

+137
-9
lines changed

1 file changed

+137
-9
lines changed

src/clang.rs

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,21 +1273,149 @@ 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(refd) = c.referenced() {
1320+
if refd != *c {
1321+
println!();
1322+
print_cursor(depth, String::from(prefix) + "referenced.", &refd);
1323+
}
1324+
}
1325+
1326+
let canonical = c.canonical();
1327+
if canonical != *c {
1328+
println!();
1329+
print_cursor(depth, String::from(prefix) + "canonical.", &canonical);
1330+
}
1331+
1332+
if let Some(specialized) = c.specialized() {
1333+
if specialized != *c {
1334+
println!();
1335+
print_cursor(depth, String::from(prefix) + "specialized.", &specialized);
1336+
}
12791337
}
1280-
println!("{}", s);
12811338
}
12821339

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())));
1340+
fn print_type<S: AsRef<str>>(depth: isize, prefix: S, ty: &Type) {
1341+
let prefix = prefix.as_ref();
1342+
1343+
let kind = ty.kind();
1344+
print_indent(depth, format!(" {}kind = {}", prefix, type_to_str(kind)));
1345+
if kind == CXType_Invalid {
1346+
return;
1347+
}
1348+
1349+
print_indent(depth, format!(" {}spelling = \"{}\"", prefix, ty.spelling()));
1350+
let num_template_args = unsafe {
1351+
clang_Type_getNumTemplateArguments(ty.x)
1352+
};
1353+
if num_template_args >= 0 {
1354+
print_indent(depth, format!(" {}number-of-template-args = {}",
1355+
prefix,
1356+
num_template_args));
1357+
}
1358+
if let Some(num) = ty.num_elements() {
1359+
print_indent(depth, format!(" {}number-of-elements = {}", prefix, num));
1360+
}
1361+
print_indent(depth, format!(" {}is-variadic? {}", prefix, ty.is_variadic()));
1362+
1363+
let canonical = ty.canonical_type();
1364+
if canonical != *ty {
1365+
println!();
1366+
print_type(depth, String::from(prefix) + "canonical.", &canonical);
1367+
}
1368+
1369+
if let Some(pointee) = ty.pointee_type() {
1370+
if pointee != *ty {
1371+
println!();
1372+
print_type(depth, String::from(prefix) + "pointee.", &pointee);
1373+
}
1374+
}
1375+
1376+
if let Some(elem) = ty.elem_type() {
1377+
if elem != *ty {
1378+
println!();
1379+
print_type(depth, String::from(prefix) + "elements.", &elem);
1380+
}
1381+
}
1382+
1383+
if let Some(ret) = ty.ret_type() {
1384+
if ret != *ty {
1385+
println!();
1386+
print_type(depth, String::from(prefix) + "return.", &ret);
1387+
}
1388+
}
1389+
1390+
let named = ty.named();
1391+
if named != *ty && named.is_valid() {
1392+
println!();
1393+
print_type(depth, String::from(prefix) + "named.", &named);
1394+
}
1395+
}
1396+
1397+
print_indent(depth, "(");
1398+
print_cursor(depth, "", c);
1399+
1400+
println!();
1401+
let ty = c.cur_type();
1402+
print_type(depth, "type.", &ty);
1403+
1404+
let declaration = ty.declaration();
1405+
if declaration != *c && declaration.kind() != CXCursor_NoDeclFound {
1406+
println!();
1407+
print_cursor(depth, "type.declaration.", &declaration);
1408+
}
12881409

12891410
// Recurse.
1290-
c.visit(|s| ast_dump(&s, depth + 1));
1411+
let mut found_children = false;
1412+
c.visit(|s| {
1413+
if !found_children {
1414+
println!();
1415+
found_children = true;
1416+
}
1417+
ast_dump(&s, depth + 1)
1418+
});
12911419

12921420
print_indent(depth, ")");
12931421

0 commit comments

Comments
 (0)