diff --git a/applications/projects/SceneChecking/CMakeLists.txt b/applications/projects/SceneChecking/CMakeLists.txt index f4ce6b70cab..b76e7d9688f 100644 --- a/applications/projects/SceneChecking/CMakeLists.txt +++ b/applications/projects/SceneChecking/CMakeLists.txt @@ -19,6 +19,7 @@ set(HEADER_FILES ${SCENECHECK_SRC_DIR}/SceneCheckEmptyNodeName.h ${SCENECHECK_SRC_DIR}/SceneCheckMapping.h ${SCENECHECK_SRC_DIR}/SceneCheckMissingRequiredPlugin.h + ${SCENECHECK_SRC_DIR}/SceneCheckSpecialCharacters.h ${SCENECHECK_SRC_DIR}/SceneCheckUsingAlias.h ${SCENECHECK_SRC_DIR}/SceneCheckerListener.h ${SCENECHECK_SRC_DIR}/SceneCheckerVisitor.h @@ -34,6 +35,7 @@ set(SOURCE_FILES ${SCENECHECK_SRC_DIR}/SceneCheckEmptyNodeName.cpp ${SCENECHECK_SRC_DIR}/SceneCheckMapping.cpp ${SCENECHECK_SRC_DIR}/SceneCheckMissingRequiredPlugin.cpp + ${SCENECHECK_SRC_DIR}/SceneCheckSpecialCharacters.cpp ${SCENECHECK_SRC_DIR}/SceneCheckUsingAlias.cpp ${SCENECHECK_SRC_DIR}/SceneCheckerListener.cpp ${SCENECHECK_SRC_DIR}/SceneCheckerVisitor.cpp diff --git a/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.cpp b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.cpp new file mode 100644 index 00000000000..5289ffe9b05 --- /dev/null +++ b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.cpp @@ -0,0 +1,103 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ + +#include +#include +#include + +namespace sofa::scenechecking +{ + +const bool SceneCheckSpecialCharactersRegistered = sofa::simulation::SceneCheckMainRegistry::addToRegistry(SceneCheckSpecialCharacters::newSPtr()); + +const std::string SceneCheckSpecialCharacters::getName() { return "SceneCheckSpecialCharacters"; } + +const std::string SceneCheckSpecialCharacters::getDesc() +{ + return "Check if nodes and components have special characters that may lead to undefined " + "behavior."; +} + +void SceneCheckSpecialCharacters::doInit(sofa::simulation::Node* node) +{ + SOFA_UNUSED(node); + m_numWithSpecialChars = 0; +} + +namespace +{ + +std::string containsSpecialCharacters(const std::string& str) +{ + static constexpr std::string_view specialChars = " !@#$%^&*()+=[]{}|;':\",./\\<>?"; + + std::string foundChars {}; + + for (const auto& c : specialChars) + { + if (str.find_first_of(c) != std::string::npos) + { + foundChars += c; + } + } + + return foundChars; +} + +} + +void SceneCheckSpecialCharacters::doCheckOn(sofa::simulation::Node* node) +{ + if (node == nullptr) + return; + + if (const auto nodeSpecialChars = containsSpecialCharacters(node->getName()); + !nodeSpecialChars.empty()) + { + msg_warning(node) << "The node has the following special characters in its name: '" << nodeSpecialChars << "'"; + ++m_numWithSpecialChars; + } + + for (auto& object : node->object ) + { + if (const sofa::core::objectmodel::BaseComponent* o = object.get()) + { + if (const auto componentSpecialChars = containsSpecialCharacters(o->getName()); + !componentSpecialChars.empty()) + { + msg_warning(o) << "The component has the following special characters in its name: '" << componentSpecialChars << "'"; + ++m_numWithSpecialChars; + } + } + } +} + +void SceneCheckSpecialCharacters::doPrintSummary() +{ + if (m_numWithSpecialChars != 0) + { + msg_warning(this->getName()) << "Found " << m_numWithSpecialChars + << " nodes or components with special characters in their names. It can lead to undefined behavior."; + } +} + +} // namespace sofa::scenechecking diff --git a/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.h b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.h new file mode 100644 index 00000000000..1c85522ff28 --- /dev/null +++ b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckSpecialCharacters.h @@ -0,0 +1,46 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +#include + +namespace sofa::scenechecking +{ + +class SOFA_SCENECHECKING_API SceneCheckSpecialCharacters : public sofa::simulation::SceneCheck +{ +public: + typedef std::shared_ptr SPtr; + static SPtr newSPtr() { return std::make_shared(); } + const std::string getName() override; + const std::string getDesc() override; + void doInit(sofa::simulation::Node* node) override; + void doCheckOn(sofa::simulation::Node* node) override; + void doPrintSummary() override; + +private: + + std::size_t m_numWithSpecialChars {}; +}; + +}