Skip to content

Commit

Permalink
Add a sample to test C++ exceptions support (#432) (#434)
Browse files Browse the repository at this point in the history
Add a sample to test C++ exceptions support
  • Loading branch information
voltur01 committed Apr 26, 2024
1 parent a7b75d8 commit c271c3f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packagetest/cpp-baremetal-semihosting-exceptions.test
@@ -0,0 +1,5 @@
# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions clean
# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions run BIN_PATH=%unpack_directory/bin 2>&1 | FileCheck %s
# RUN: make -C %samples_dir/src/cpp-baremetal-semihosting-exceptions clean
# CHECK: No exceptions.
# CHECK: Exception caught.
39 changes: 39 additions & 0 deletions samples/src/cpp-baremetal-semihosting-exceptions/Makefile
@@ -0,0 +1,39 @@
#
# Copyright (c) 2024, Arm Limited and affiliates.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

include ../../Makefile.conf

build: hello.hex hello-exn.hex

hello.hex: hello.cpp
$(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) $(CPP_FLAGS) -print-multi-directory -g -T ../../ldscripts/microbit.ld -o hello.elf $^ | grep -v "_exn_"
$(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) $(CPP_FLAGS) -g -T ../../ldscripts/microbit.ld -o hello.elf $^
$(BIN_PATH)/llvm-objcopy -O ihex hello.elf hello.hex

hello-exn.hex: hello-exn.cpp
$(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) -print-multi-directory -g -T ../../ldscripts/microbit.ld -o hello.elf $^ | grep "_exn_"
$(BIN_PATH)/clang++ $(MICROBIT_TARGET) $(CRT_SEMIHOST) -g -T ../../ldscripts/microbit.ld -o hello-exn.elf $^
$(BIN_PATH)/llvm-objcopy -O ihex hello-exn.elf hello-exn.hex

run: hello.hex hello-exn.hex
qemu-system-arm -M microbit -semihosting -nographic -device loader,file=hello.hex
qemu-system-arm -M microbit -semihosting -nographic -device loader,file=hello-exn.hex 2>&1 | grep "caught"

clean:
rm -f *.elf *.hex

.PHONY: clean run
4 changes: 4 additions & 0 deletions samples/src/cpp-baremetal-semihosting-exceptions/README.md
@@ -0,0 +1,4 @@
# Bare-metal semihosting exceptions sample

This sample shows and tests that C++ exceptions and corresponding
library variant selection work correctly.
12 changes: 12 additions & 0 deletions samples/src/cpp-baremetal-semihosting-exceptions/hello-exn.cpp
@@ -0,0 +1,12 @@
#include <iostream>

int main(void) {
try {
throw "error";
} catch(...) {
std::cout << "Exception caught." << std::endl;
return 0;
}
std::cout << "Exception skipped." << std::endl;
return 1;
}
6 changes: 6 additions & 0 deletions samples/src/cpp-baremetal-semihosting-exceptions/hello.cpp
@@ -0,0 +1,6 @@
#include <iostream>

int main(void) {
std::cout << "No exceptions." << std::endl;
return 0;
}

0 comments on commit c271c3f

Please sign in to comment.