qjoyel opened a new issue, #787:
URL: https://github.com/apache/logging-log4cxx/issues/787

   ### Description
   
   When a project includes the log4cxx public headers and is compiled with MSVC 
at warning level 4 (`/W4`), MSVC reports warning C4100 "unreferenced formal 
parameter" inside the log4cxx headers.
   
   These warnings come from inline and virtual functions defined in the 
headers, so they show up in the build output of every translation unit that 
includes them, not just once when building log4cxx. In larger projects this 
produces a lot of noise and can hide real warnings in the project's own code.
   
   For projects that also build with `/WX` (treat warnings as errors), the 
build fails. The only workaround is to wrap every log4cxx include in:
   
   ```cpp
   #pragma warning(push)
   #pragma warning(disable: 4100)
   #include <log4cxx/logger.h>
   #pragma warning(pop)
   ```
   
   Example output from a consumer project:
   
   ```
   <install>\include\log4cxx\<header>.h(<line>): warning C4100: '<param>': 
unreferenced formal parameter
   <install>\include\log4cxx\<header>.h(<line>): warning C4100: '<param>': 
unreferenced formal parameter
   ```
   
   ### Steps to reproduce
   
   Minimal consumer project:
   
   `CMakeLists.txt`
   ```cmake
   cmake_minimum_required(VERSION 3.16)
   project(log4cxx_w4_repro LANGUAGES CXX)
   
   set(CMAKE_CXX_STANDARD 17)
   set(CMAKE_CXX_STANDARD_REQUIRED ON)
   
   find_package(log4cxx REQUIRED)
   
   add_executable(repro main.cpp)
   target_link_libraries(repro PRIVATE log4cxx)
   
   if(MSVC)
       target_compile_options(repro PRIVATE /W4 /WX)
   endif()
   ```
   
   `main.cpp`
   ```cpp
   #include <log4cxx/logger.h>
   #include <log4cxx/basicconfigurator.h>
   
   int main()
   {
       log4cxx::BasicConfigurator::configure();
       LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "Hello");
       return 0;
   }
   ```
   
   Configure and build, pointing CMake to the installed log4cxx:
   
   ```
   cmake -B build -DCMAKE_PREFIX_PATH=<log4cxx-install-dir>
   cmake --build build --config Release
   ```
   
   The build fails with C4100 errors pointing into the log4cxx public headers. 
Without `/WX`, the same diagnostics are reported as warnings.
   
   Including log4cxx public headers should not produce warnings in client code 
at `/W4`, so consumers can keep strict warning levels (and `/WX`) without 
suppressing warnings around the includes.
   
   ### Suggested fix
   
   In the public headers (`src/main/include/log4cxx/**`), mark unused 
parameters explicitly. Possible approaches:
   
   1. **Omit or comment out the parameter name**
   
      No macro needed, and it works with any compiler and standard:
      ```cpp
      void activateOptions(helpers::Pool& /* pool */) override {}
      ```
   
   2. **Cast to `void` through a macro**
   
      ```cpp
      #define LOG4CXX_IGNORE_UNUSED(x) (void)(x)
      ```
      ```cpp
      void activateOptions(helpers::Pool& pool) override
      {
          LOG4CXX_IGNORE_UNUSED(pool);
      }
      ```
      Works with any compiler and standard, keeps the parameter name visible, 
and makes the intent explicit and searchable.
   
   3. **Attribute through a macro, based on C++17 `[[maybe_unused]]`**
   
      ```cpp
      #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 
201703L)
      #define LOG4CXX_MAYBE_UNUSED [[maybe_unused]]
      #elif defined(__GNUC__) || defined(__clang__)
      #define LOG4CXX_MAYBE_UNUSED __attribute__((unused))
      #else
      #define LOG4CXX_MAYBE_UNUSED
      #endif
      ```
      ```cpp
      void activateOptions(LOG4CXX_MAYBE_UNUSED helpers::Pool& pool) override {}
      ```
      MSVC has no pre-C++17 equivalent. With MSVC in C++11/14 mode the macro 
expands to nothing and C4100 is still emitted. It becomes a complete solution 
once C++17 is the minimum requirement.
   
   Options 1 and 2 fix the issue for all currently supported configurations.
   The macros could be defined in a common public header such as 
`log4cxx/log4cxx.h`.
   
   ### Environment
   
   - log4cxx version: 1.8.0
   - Compiler: e.g. MSVC 19.4x / Visual Studio 2022 17.x


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to