diff --git a/rclcpp/resource/logging.hpp.em b/rclcpp/resource/logging.hpp.em index 7b5b0f349f..050c2afc5b 100644 --- a/rclcpp/resource/logging.hpp.em +++ b/rclcpp/resource/logging.hpp.em @@ -143,6 +143,12 @@ def get_rclcpp_suffix_from_features(features): @[ if 'stream' in feature_combination]@ std::stringstream rclcpp_stream_ss_; \ rclcpp_stream_ss_ << @(stream_arg); \ +@[ else]@ + { \ + constexpr bool starts_with_double_quotes = (#__VA_ARGS__)[0] == '"'; \ + static_assert(starts_with_double_quotes, \ + "Second argument to RCLCPP_@(severity) must be a string literal"); \ + } \ @[ end if]@ RCUTILS_LOG_@(severity)@(get_suffix_from_features(feature_combination))_NAMED( \ @{params = ['get_time_point' if p == 'clock' and 'throttle' in feature_combination else p for p in params]}@ diff --git a/rclcpp/test/rclcpp/CMakeLists.txt b/rclcpp/test/rclcpp/CMakeLists.txt index 6f915feef5..4fb9acbc5f 100644 --- a/rclcpp/test/rclcpp/CMakeLists.txt +++ b/rclcpp/test/rclcpp/CMakeLists.txt @@ -271,12 +271,17 @@ endif() # rclcpp_add_build_failure_test(build_failure__get_node_topics_interface_const_ref_wrapped_node # node_interfaces/test_does_not_compile/get_node_topics_interface_const_ref_wrapped_node.cpp) -# target_link_libraries(build_failure__get_node_topics_interface_const_ref_rclcpp_node +# target_link_libraries(build_failure__get_node_topics_interface_const_ref_wrapped_node # ${PROJECT_NAME}) # rclcpp_add_build_failure_test(build_failure__get_node_topics_interface_const_ptr_wrapped_node # node_interfaces/test_does_not_compile/get_node_topics_interface_const_ptr_wrapped_node.cpp) -# target_link_libraries(build_failure__get_node_topics_interface_const_ptr_rclcpp_node +# target_link_libraries(build_failure__get_node_topics_interface_const_ptr_wrapped_node +# ${PROJECT_NAME}) + +# rclcpp_add_build_failure_test(build_failure__pass_non_string_literal_to_logging_macro +# test_does_not_compile/pass_non_string_literal_to_logging_macro.cpp) +# target_link_libraries(build_failure__pass_non_string_literal_to_logging_macro # ${PROJECT_NAME}) ament_add_gtest(test_node_global_args test_node_global_args.cpp) diff --git a/rclcpp/test/rclcpp/test_does_not_compile/pass_non_string_literal_to_logging_macro.cpp b/rclcpp/test/rclcpp/test_does_not_compile/pass_non_string_literal_to_logging_macro.cpp new file mode 100644 index 0000000000..5b5e5427ff --- /dev/null +++ b/rclcpp/test/rclcpp/test_does_not_compile/pass_non_string_literal_to_logging_macro.cpp @@ -0,0 +1,24 @@ +// Copyright 2022 Open Source Robotics Foundation, Inc. +// +// 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 + +#include "rclcpp/logger.hpp" +#include "rclcpp/logging.hpp" + +int main(void) +{ + std::string s = "message"; + RCLCPP_ERROR(rclcpp::get_logger("name"), s.c_str()); +}