Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions .github/workflows/basic-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@ env:

jobs:
format-check:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- run: |
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
echo "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-18 main" | sudo tee /etc/apt/sources.list.d/llvm-18.list

- name: Update apt
run: sudo apt-get update

- name: Install clang-format
run: |
sudo apt-get remove clang-format-*
sudo apt-get install -t llvm-toolchain-noble-18 clang-format-18

- name: Format source code
run: |
find demo lib test \
find src include test \
-type f \
-a \( -name "*.c" -o -name "*.cpp" -o -name "*.h" \) \
-print0 \
| xargs -0 clang-format-14 -i
| xargs -0 clang-format-18 -i

- name: Format check
run: |
Expand All @@ -33,28 +45,31 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4
- uses: codespell-project/actions-codespell@v2
- uses: actions/checkout@v5
- uses: codespell-project/actions-codespell@v2.2

build-project:
strategy:
fail-fast: false
matrix:
include:
- llvm-version: 12
os: ubuntu-20.04
preset: develop
- llvm-version: 14
os: ubuntu-22.04
preset: develop
- llvm-version: 18
- llvm-version: 21
os: ubuntu-24.04
preset: develop

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5

- name: LLVM apt
if: ${{ matrix.llvm-version >= 19 }}
run: |
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
echo "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-${{ matrix.llvm-version }} main" | sudo tee /etc/apt/sources.list.d/llvm-${{ matrix.llvm-version }}.list

- name: Update apt
run: sudo apt-get update
Expand All @@ -73,12 +88,18 @@ jobs:
echo "CLANG_CMAKE_DIR=/usr/lib/llvm-${{ matrix.llvm-version }}/lib/cmake/clang" >> $GITHUB_ENV
echo "EXTERNAL_LIT=/usr/lib/llvm-${{ matrix.llvm-version }}/build/utils/lit/lit.py" >> $GITHUB_ENV

- name: Build ASTPrinter
run: |
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DLLVM_DIR=${LLVM_CMAKE_DIR} -DClang_DIR=${CLANG_CMAKE_DIR}
cmake --build build --parallel

- name: Build ASTPrinter release
run: |
cmake -B build_rel -DCMAKE_BUILD_TYPE=Release -DLLVM_DIR=${LLVM_CMAKE_DIR} -DClang_DIR=${CLANG_CMAKE_DIR}
cmake --preset release -DLLVM_DIR=${LLVM_CMAKE_DIR} -DClang_DIR=${CLANG_CMAKE_DIR} -DLLVM_EXTERNAL_LIT=${EXTERNAL_LIT}
cmake --build build_rel --parallel --target install

- name: Test and Coverage
run: |
cmake --preset coverage -DLLVM_DIR=${LLVM_CMAKE_DIR} -DClang_DIR=${CLANG_CMAKE_DIR} -DLLVM_EXTERNAL_LIT=${EXTERNAL_LIT}
cmake --build build_cov --target coverage-astprinter

- name: Upload coverage
uses: actions/upload-artifact@v6
with:
name: coverage-report-llvm${{ matrix.llvm-version }}
path: build_cov/coverage_report/
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
PROJECT(astprinter
VERSION 0.3
VERSION 0.4
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
if(PROJECT_IS_TOP_LEVEL)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -17,11 +19,15 @@ list(APPEND CMAKE_MODULE_PATH
include(ToolchainOptions)
include(CMakePackageConfigHelpers)

add_format_target(format-sources
astprinter_add_format_target(astprinter-format-sources
"Formats project source files"
TARGETS src/*.cpp
src/*.h
include/*.h
)

add_subdirectory(src)

if(PROJECT_IS_TOP_LEVEL)
add_subdirectory(test)
endif()
73 changes: 73 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "clang-toolchain",
"hidden": true,
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++"
}
},
{
"name": "develop",
"displayName": "Develop (Debug)",
"description": "Default develop build options for Clang",
"binaryDir": "${sourceDir}/build",
"inherits": [
"clang-toolchain"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "release",
"displayName": "Release",
"description": "Default release build options for Clang",
"binaryDir": "${sourceDir}/build_rel",
"inherits": [
"clang-toolchain"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "coverage",
"displayName": "Coverage (LLVM)",
"description": "Default coverage build options for Clang (LLVM-based)",
"binaryDir": "${sourceDir}/build_cov",
"inherits": [
"clang-toolchain"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"ASTPRINTER_ENABLE_COVERAGE": "ON",
"CMAKE_C_FLAGS": "-fprofile-instr-generate -fcoverage-mapping",
"CMAKE_CXX_FLAGS": "-fprofile-instr-generate -fcoverage-mapping",
"CMAKE_EXE_LINKER_FLAGS": "-fprofile-instr-generate -fcoverage-mapping"
}
}
],
"buildPresets": [
{
"name": "develop",
"configurePreset": "develop"
},
{
"name": "release",
"configurePreset": "release"
},
{
"name": "coverage",
"configurePreset": "coverage"
}
]
}
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,48 @@ Assume *test.c* contains the code:
In this example we
1) load `test.c` with standard Clang flags,
2) list all function declarations in `test.c`,
3) print the AST definition of foo, and finally,
4) print the AST of the return statement in line 2.
3) print the AST definition of foo,,
4) print the AST of the return statement in line 2, and, finally,
5) enable source output; print the source *and* AST of foo in lines 1-3.

```console
ahueck@sys:~/astprint/install$ ./bin/astprinter ../test.c --
ahueck@sys:~/astprint/install$ ./bin/clang-ast-printer ../test.c --
ast-printer> l
foo:~/astprint/install/../test.c:1:3->3:4
main:~/astprint/install/../test.c:4:3->8:4
foo:/path/to/test.c:1:1->3:2
main:/path/to/test.c:4:1->8:2

ast-printer> p foo
FunctionDecl 0x3977120 <test.c:1:3, line:3:3> line:1:7 used foo 'int ()'
`-CompoundStmt 0x3977238 <col:14, line:3:3>
`-ReturnStmt 0x3977220 <line:2:7, col:14>
`-IntegerLiteral 0x3977200 <col:14> 'int' 2
~/astprint/install/../test.c:1:3->3:4
FunctionDecl 0x62eed65573d8 </path/to/test.c:1:1, line:3:1> line:1:5 used foo 'int ()'
`-CompoundStmt 0x62eed65574f8 <col:12, line:3:1>
`-ReturnStmt 0x62eed65574e8 <line:2:5, col:12>
`-IntegerLiteral 0x62eed65574c8 <col:12> 'int' 2
/path/to/test.c:1:1->3:2

ast-printer> 2
ReturnStmt 0x1ba3220 <test.c:2:7, col:14>
`-IntegerLiteral 0x1ba3200 <col:14> 'int' 2
~/astprint/install/../test.c:2:7->2:15
ReturnStmt 0x62eed65574e8 </path/to/test.c:2:5, col:12>
`-IntegerLiteral 0x62eed65574c8 <col:12> 'int' 2
/path/to/test.c:2:5->2:13

ast-printer> source
Show source on.
ast-printer> 1 3
int foo() {
return 2;
}

FunctionDecl 0x62eed65573d8 </path/to/test.c:1:1, line:3:1> line:1:5 used foo 'int ()'
`-CompoundStmt 0x62eed65574f8 <col:12, line:3:1>
`-ReturnStmt 0x62eed65574e8 <line:2:5, col:12>
`-IntegerLiteral 0x62eed65574c8 <col:12> 'int' 2
/path/to/test.c:1:1->3:2
```

## How to build

###### Requirements

- CMake >= 3.20
- Clang/LLVM 12, 14, 18 (CMake needs to find the installation, see
- CMake >= 3.21
- Clang/LLVM 14, 18-21 (CMake needs to find the installation, see
the [LLVM CMake documentation](https://llvm.org/docs/CMake.html) or the [CI workflow](.github/workflows/basic-ci.yml))
- C++17 compiler

Expand All @@ -64,6 +78,6 @@ ReturnStmt 0x1ba3220 <test.c:2:7, col:14>
In the root project folder, execute the following commands (see also [CI workflow](.github/workflows/basic-ci.yml))

```
cmake -B build -DCMAKE_INSTALL_PREFIX=*your path*
cmake -B build --preset=release
cmake --build build --target install --parallel
```
5 changes: 4 additions & 1 deletion cmake/ToolchainOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ include(clang-format)
include(log-util)
include(target-util)

set(LOG_LEVEL 0 CACHE STRING "Granularity of the logger. 3 is most verbose, 0 is least.")
set(ASTPRINTER_LOG_LEVEL 0 CACHE STRING "Granularity of the logger. 3 is most verbose, 0 is least.")

option(ASTPRINTER_ENABLE_COVERAGE "Enable code coverage" OFF)
mark_as_advanced(ASTPRINTER_ENABLE_COVERAGE)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
Expand Down
2 changes: 1 addition & 1 deletion cmake/modules/clang-format.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(add_format_target target comment)
function(astprinter_add_format_target target comment)
macro(filter_dir _dir_name_)
foreach (SOURCE_FILE ${ALL_CXX_FILES})
string(FIND ${SOURCE_FILE} ${_dir_name_} EXCLUDE_FOUND)
Expand Down
12 changes: 6 additions & 6 deletions cmake/modules/clang-tidy.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(add_tidy_target target comment)
function(astprinter_add_tidy_target target comment)
macro(filter_dir _name_)
foreach (SOURCE_FILE ${ARG_SOURCES})
string(FIND ${SOURCE_FILE} ${_name_} EXCLUDE_FOUND)
Expand Down Expand Up @@ -33,23 +33,23 @@ function(add_tidy_target target comment)
endif()
endfunction()

function(add_tidy_fix_target target comment)
function(astprinter_add_tidy_fix_target target comment)
cmake_parse_arguments(ARG "" "" "SOURCES;EXCLUDES;OTHER" ${ARGN})
add_tidy_target(${target} "${comment}"
astprinter_add_tidy_target(${target} "${comment}"
SOURCES ${ARG_SOURCES}
EXCLUDES ${ARG_EXCLUDES}
OTHER ${ARG_OTHER} -fix
)
endfunction()

function(make_tidy_check name sources)
add_tidy_target(tidy-run-on-${name}
function(astprinter_make_tidy_check name sources)
astprinter_add_tidy_target(tidy-run-on-${name}
"Clang-tidy run on ${name} translation units"
SOURCES ${sources}
OTHER --header-filter=${CMAKE_CURRENT_SOURCE_DIR}
)

add_tidy_fix_target(tidy-fix-on-${name}
astprinter_add_tidy_fix_target(tidy-fix-on-${name}
"Clang-tidy run with fixes on ${name} translation units"
SOURCES ${sources}
OTHER --header-filter=${CMAKE_CURRENT_SOURCE_DIR} -checks=-*,modernize-*,llvm-namespace-comment,google-explicit-constructor
Expand Down
4 changes: 2 additions & 2 deletions cmake/modules/log-util.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(target_define_file_basename targetname)
function(astprinter_define_file_basename targetname)
get_target_property(source_files "${targetname}" SOURCES)

foreach(sourcefile ${source_files})
Expand All @@ -10,7 +10,7 @@ function(target_define_file_basename targetname)
get_filename_component(basename "${sourcefile}" NAME)

list(APPEND compile_defs
"LOG_BASENAME_FILE=\"${basename}\""
"ASTPRINTER_LOG_BASENAME_FILE=\"${basename}\""
)

set_source_files_properties("${sourcefile}"
Expand Down
Loading