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/
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)
CMAKE_MINIMUM_REQUIRED(VERSION 3.21)
PROJECT(astprinter
VERSION 0.3
)
Expand All @@ -17,11 +17,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"
}
]
}
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
55 changes: 53 additions & 2 deletions cmake/modules/target-util.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(target_project_compile_options target)
function(astprinter_project_compile_options target)
cmake_parse_arguments(ARG "" "" "PRIVATE_FLAGS;PUBLIC_FLAGS" ${ARGN})

target_compile_options(${target} PRIVATE
Expand All @@ -22,7 +22,7 @@ function(target_project_compile_options target)
endif ()
endfunction()

function(target_project_compile_definitions target)
function(astprinter_project_compile_definitions target)
cmake_parse_arguments(ARG "" "" "PRIVATE_DEFS;PUBLIC_DEFS" ${ARGN})

target_compile_definitions(${target} PRIVATE "LLVM_VERSION_MAJOR=${LLVM_VERSION_MAJOR}")
Expand All @@ -38,4 +38,55 @@ function(target_project_compile_definitions target)
"${ARG_PUBLIC_DEFS}"
)
endif ()
endfunction()

function(astprinter_find_llvm_progs target names)
cmake_parse_arguments(ARG "ABORT_IF_MISSING;SHOW_VAR" "DEFAULT_EXE" "HINTS" ${ARGN})
set(TARGET_TMP ${target})

find_program(
${target}
NAMES ${names}
PATHS ${LLVM_TOOLS_BINARY_DIR}
NO_DEFAULT_PATH
)
if(NOT ${target})
find_program(
${target}
NAMES ${names}
HINTS ${ARG_HINTS}
)
endif()

if(NOT ${target})
set(target_missing_message "")
if(ARG_DEFAULT_EXE)
unset(${target} CACHE)
set(${target}
${ARG_DEFAULT_EXE}
CACHE
STRING
"Default value for ${TARGET_TMP}."
)
set(target_missing_message "Using default: ${ARG_DEFAULT_EXE}")
endif()

set(message_status STATUS)
if(ARG_ABORT_IF_MISSING AND NOT ARG_DEFAULT_EXE)
set(message_status SEND_ERROR)
endif()
message(${message_status}
"Did find LLVM program " "${names}"
" in ${LLVM_TOOLS_BINARY_DIR}, in system path or hints " "\"${ARG_HINTS}\"" ". "
${target_missing_message}
)
endif()

set(${TARGET_TMP} "${${TARGET_TMP}}" PARENT_SCOPE)

if(ARG_SHOW_VAR)
mark_as_advanced(CLEAR ${target})
else()
mark_as_advanced(${target})
endif()
endfunction()
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ add_executable(clang-ast-printer${EXE_SUFFIX}
main.cpp
)

target_define_file_basename(clang-ast-printer${EXE_SUFFIX})
target_project_compile_options(clang-ast-printer${EXE_SUFFIX})
target_project_compile_definitions(clang-ast-printer${EXE_SUFFIX}
astprinter_define_file_basename(clang-ast-printer${EXE_SUFFIX})
astprinter_project_compile_options(clang-ast-printer${EXE_SUFFIX})
astprinter_project_compile_definitions(clang-ast-printer${EXE_SUFFIX}
PRIVATE_DEFS
LOG_LEVEL=${LOG_LEVEL}
ASTPRINTER_LOG_LEVEL=${ASTPRINTER_LOG_LEVEL}
)

target_include_directories(clang-ast-printer${EXE_SUFFIX}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int main(int argc, const char** argv) {

NodeFinder visitor(ctx, llvm::outs());
visitor.showColor(color);
visitor.showSource(source);

llvm::LineEditor le("ast-printer");
while (auto line = le.readLine()) {
Expand Down
8 changes: 4 additions & 4 deletions src/printer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ add_library(clang-ast-print STATIC
NodeFinder.cpp
)

target_define_file_basename(clang-ast-print)
target_project_compile_options(clang-ast-print)
target_project_compile_definitions(clang-ast-print
astprinter_define_file_basename(clang-ast-print)
astprinter_project_compile_options(clang-ast-print)
astprinter_project_compile_definitions(clang-ast-print
PRIVATE_DEFS
LOG_LEVEL=${LOG_LEVEL}
ASTPRINTER_LOG_LEVEL=${ASTPRINTER_LOG_LEVEL}
)

target_include_directories(clang-ast-print
Expand Down
Loading