Refactor compiler warning handling.

Compiler warnings are now accumulated in a vector of strings within the mjSpec object. New API functions `mjs_numWarnings` and `mjs_getWarning` are added to access these warnings. The compiler's log handler now chains warnings to the global log handler, ensuring they are still displayed immediately. Call sites in `mj_loadXML`, `mj_compile`, and the Python and WASM bindings have been updated to use the new warning API.

PiperOrigin-RevId: 933361650
Change-Id: I47cab98a460c57b0898c0a1a43fce2a5b9648eb1
This commit is contained in:
Yuval Tassa
2026-06-16 16:27:52 -07:00
committed by Copybara-Service
parent 55c6332f20
commit 6f8bb5ef55
25 changed files with 640 additions and 89 deletions
+26 -1
View File
@@ -44,7 +44,7 @@ namespace mujoco {
inline mjtNum MjTolScale() {
static const mjtNum scale = []() {
const char* env = std::getenv("MJTOL_SCALE");
return env ? std::atof(env) : 1.0;
return env ? std::strtod(env, nullptr) : 1.0;
}();
return scale;
}
@@ -106,6 +106,28 @@ class MujocoErrorTestGuard {
~MujocoErrorTestGuard();
};
// Mock handler for capturing and verifying mju_warning logs.
class MockWarningHandler {
public:
// Constructor that registers this handler as the active one.
MockWarningHandler();
// Destructor that restores the previously active handler.
~MockWarningHandler();
// Mock method called when a warning is intercepted.
MOCK_METHOD(void, Warn, (const std::string& msg));
// Allow any number of warnings without triggering test failure.
void ExpectWarnings();
// Returns the thread-local active mock warning handler.
static MockWarningHandler* GetActive();
private:
static thread_local MockWarningHandler* active_handler;
MockWarningHandler* prev_ = nullptr;
};
// A test fixture which simplifies writing tests for the MuJoCo C API.
// By default, any MuJoCo operation which triggers a warning or error will
// trigger a test failure.
@@ -128,6 +150,9 @@ class MujocoTest : public ::testing::Test {
}
~MujocoTest() { mj_freeLastXML(); }
protected:
MockWarningHandler mock_warning_handler;
private:
MujocoErrorTestGuard error_guard;
};