ros2 / rcpputils

Apache License 2.0
32 stars 54 forks source link

Static Thread Safety Analysis via Code Annotation does not raise warnings with ros2 humble #189

Closed medino98 closed 8 months ago

medino98 commented 8 months ago

Hi all!

I am new to ros2 and I came across with this guide for generating thread safety warnings with clang on the humble documentation page. https://docs.ros.org/en/humble/The-ROS2-Project/Contributing/Quality-Guide.html

I wanted to try it myself however no matter what I have tried I was not able to generate any warnings with the documentation given example.

My project structure: project_structure

The content of the trial.cpp file:

#include "rcpputils/thread_safety_annotations.hpp"

class Foo {
public:
  void incr(int amount) {
    std::lock_guard<std::mutex> lock(mutex_);
    bar += amount;
  }

  int get() const {
    return bar;
  }

private:
  mutable std::mutex mutex_;
  int bar RCPPUTILS_TSA_GUARDED_BY(mutex_);
};

int main(int argc, char const *argv[])
{
  return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)
project(trial)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

# adding executables
add_executable(${PROJECT_NAME} src/trial.cpp) 
ament_target_dependencies(trial rclcpp)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wthread-safety)   # for your whole package
endif()

#INSTALL
install(TARGETS
  trial
  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

package.xml:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>trial</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="root@todo.todo">root</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <!-- <depend>std_msgs</depend> -->

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

The commands I ran for the build of the workspace (all the usual stuff and the commands referenced in the documentation)

  1. source /opt/ros/humble/setup.bash
  2. ros2 pkg create --build-type ament_cmake trial
  3. rosdep install -i --from-path src --rosdistro foxy -y
  4. CC=clang CXX=clang++ colcon build --cmake-args -DCMAKE_CXX_FLAGS='-stdlib=libc++ -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS' -DFORCE_BUILD_VENDOR_PKG=ON --no-warn-unused-cli --event-handlers console_direct+ --cmake-args -DCMAKE_VERBOSE_MAKEFILE=ON

I could not find any more resources in this matter so that is why I come here. I would be glad if someone could give me an insight into the matter in any way. Thanks in advance!

clalancette commented 8 months ago

Please open questions like this on https://robotics.stackexchange.com/questions/tagged/ros, which is our central Question and Answer site. You'll get a better answer there, and it will be searchable for the future.

Make sure to include a lot of information on what platform you are using, which ROS distribution you are using, and the exact steps you took.