{"id":18561831,"url":"https://github.com/pseudomanifold/libclang-experiments","last_synced_at":"2025-04-10T03:31:09.773Z","repository":{"id":92125983,"uuid":"102826295","full_name":"Pseudomanifold/libclang-experiments","owner":"Pseudomanifold","description":"Experiments with `libclang`","archived":false,"fork":false,"pushed_at":"2017-09-08T07:22:25.000Z","size":8629,"stargazers_count":23,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T15:47:23.449Z","etag":null,"topics":["abstract-syntax-tree","clang","code-analysis","cplusplus","libclang","llvm"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pseudomanifold.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-08T06:30:46.000Z","updated_at":"2024-10-21T02:01:23.000Z","dependencies_parsed_at":"2023-03-13T17:33:25.379Z","dependency_job_id":null,"html_url":"https://github.com/Pseudomanifold/libclang-experiments","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pseudomanifold%2Flibclang-experiments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pseudomanifold%2Flibclang-experiments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pseudomanifold%2Flibclang-experiments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pseudomanifold%2Flibclang-experiments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pseudomanifold","download_url":"https://codeload.github.com/Pseudomanifold/libclang-experiments/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248150828,"owners_count":21055988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstract-syntax-tree","clang","code-analysis","cplusplus","libclang","llvm"],"created_at":"2024-11-06T22:08:00.938Z","updated_at":"2025-04-10T03:31:09.265Z","avatar_url":"https://github.com/Pseudomanifold.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `libclang-experiments` \u0026mdash; Some experiments with `libclang` \n\nThis repository contains some of [my](http://bastian.rieck.ru)\nexperiments with `libclang`, or, to be more precise, the `C` bindings of\n`libclang`. I already detailed some of my experiments in two blog posts.\nThe first deals with [walking an abstract syntax tree](http://bastian.rieck.ru/blog/posts/2015/baby_steps_libclang_ast),\nwhile the second deals with [counting the extents of a function](http://bastian.rieck.ru/blog/posts/2016/baby_steps_libclang_function_extents).  \n\n# Requirements\n\n* `libclang` (for parsing ASTs)\n* `clang` (for creating ASTs)\n\n# Building the examples\n\n    $ mkdir build\n    $ cd build\n    $ cmake ../\n    $ make\n\n# Obtaining an AST\n\nSome of the example programs require the explicit creation of an\nabstract syntax tree, while others make use of a database of compile\ncommands. In order to obtain an abstract syntax tree, call `clang` with\nthe following parameters:\n\n    clang++ -std=c++11 -emit-ast FILE\n\nOptionally, you can add `-I` or any other parameter that is required for\ncompiling a given program.\n\n# The experiments\n\nLet me briefly explain how to use some of the experiments.\n\n## `ast-dumper` \u0026 `ast-walker`\n\nThese programs walk an abstract syntax tree. They roughly follow the\nimplementation given in [this blog post](http://bastian.rieck.ru/blog/posts/2015/baby_steps_libclang_ast).\nTo use one of them\u0026nbsp;(I am exemplarily demonstrating `ast-walker`),\nyou require the creation of an abstract syntax tree:\n\n    $ clang++ -std=c++11 -emit-ast ../examples/tuple.cc\n    $ ./ast-walker tuple.ast\n\nThis should result in an output similar to this:\n\n     ClassDecl (foo)\n    - CXXAccessSpecifier ()\n    - CXXConstructor (foo)\n    -- ParmDecl (n_)\n    -- ParmDecl (c_)\n    -- ParmDecl (d_)\n    -- MemberRef (n)\n    -- InitListExpr ()\n    --- UnexposedExpr (n_)\n    ---- DeclRefExpr (n_)\n    -- MemberRef (c)\n    -- InitListExpr ()\n    --- UnexposedExpr (c_)\n    ---- DeclRefExpr (c_)\n    -- MemberRef (d)\n    -- InitListExpr ()\n    --- UnexposedExpr (d_)\n    ---- DeclRefExpr (d_)\n    -- CompoundStmt ()\n    - FriendDecl ()\n    -- FunctionDecl (operator\u003c)\n    --- ParmDecl (lh)\n    ---- TypeRef (class foo)\n    --- ParmDecl (rh)\n    ---- TypeRef (class foo)\n    --- CompoundStmt ()\n    ---- ReturnStmt ()\n    ----- UnexposedExpr ()\n    ------ CallExpr (operator\u003c)\n    ------- UnexposedExpr ()\n    -------- UnexposedExpr (tie)\n    --------- CallExpr (tie)\n    ---------- UnexposedExpr (tie)\n    ----------- DeclRefExpr (tie)\n    ------------ NamespaceRef (std)\n    ---------- MemberRefExpr (n)\n    ----------- DeclRefExpr (lh)\n    ---------- MemberRefExpr (c)\n    ----------- DeclRefExpr (lh)\n    ---------- MemberRefExpr (d)\n    ----------- DeclRefExpr (lh)\n    ------- UnexposedExpr (operator\u003c)\n    -------- DeclRefExpr (operator\u003c)\n    ------- UnexposedExpr ()\n    -------- UnexposedExpr (tie)\n    --------- CallExpr (tie)\n    ---------- UnexposedExpr (tie)\n    ----------- DeclRefExpr (tie)\n    ------------ NamespaceRef (std)\n    ---------- MemberRefExpr (n)\n    ----------- DeclRefExpr (rh)\n    ---------- MemberRefExpr (c)\n    ----------- DeclRefExpr (rh)\n    ---------- MemberRefExpr (d)\n    ----------- DeclRefExpr (rh)\n    - CXXAccessSpecifier ()\n    - FieldDecl (n)\n    - FieldDecl (c)\n    - FieldDecl (d)\n\n## `count-function-extents`\n\nGetting this program to run can be a *tiny* bit more trickier. We should\nsupply it with the default paths required by `clang++`. To determine\nthem, call  \n\n    $ clang++ -v -c FILE\n\nwhere `FILE` is a source file of your choice. The resulting output\nshould contain the line \n\n    #include \u003c...\u003e search starts here:\n\nand a list of paths. Copy them and put them into one command-line\nvariable, like so:\n\n    $ export CPLUS_INCLUDE_PATH=/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1:/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/x86_64-pc-linux-gnu:/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/backward:/usr/local/include:/usr/bin/../lib/clang/4.0.1/include\n\nAfterwards, you can call the program on a test executable:\n\n    $ ./count-function-extents ../examples/functions.cc\n\nThis should result in the following output:\n\n    Obtained 0 compile commands\n      theAnswerToLifeTheUniverseAndEverything: 3\n      sum: 3\n      square: 3\n      cube: 3\n      factorial: 6\n      fibonacci: 14\n\nYou can also call the function for any executable specified in\na `compile_commands.json` data base:\n\n    $ ./count-function-extents ../count-function-extents.cc\n    Parsing /home/brieck/Projects/libclang-experiments/count-function-extents.cc...\n    Obtained 1 compile commands\n      getCursorSpelling: 7\n      resolvePath: 17\n      functionVisitor: 24\n      main: 74\n\nI am sure that these programs could be easily improved. I look forward\nto your changes!\n\n# Additional information\n\n* https://clang.llvm.org/doxygen/group__CINDEX.html\n* https://stackoverflow.com/questions/20280744/libclang-returns-too-much-info-about-function-declarations\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpseudomanifold%2Flibclang-experiments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpseudomanifold%2Flibclang-experiments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpseudomanifold%2Flibclang-experiments/lists"}