Skip to content

Commit a896a93

Browse files
authored
FIX: fix current cmake warnings in linux and add support to treat warnings as errors in linux (#353)
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#37803](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37803) <!-- External contributors: GitHub Issue --> > GitHub Issue: #<ISSUE_NUMBER> ------------------------------------------------------------------- ### Summary <!-- Insert your summary of changes below. Minimum 10 characters required. --> This pull request introduces improvements to the build configuration and code safety for the `mssql_python/pybind` module. The main changes focus on enforcing stricter warning and error handling in the build system, improving cross-platform compatibility, and ensuring safe type casting in parameter binding. **Build system improvements:** * Enforced treating CMake warnings and deprecated features as errors by setting `CMAKE_ERROR_DEPRECATED` and `CMAKE_WARN_DEPRECATED` to `TRUE` in `CMakeLists.txt`, ensuring deprecated usage is caught early. * Added compiler warning flags for GCC and Clang (`-Werror`, `-Wattributes`, `-Wint-to-pointer-cast`) to treat warnings as errors and catch visibility and type casting issues in `CMakeLists.txt`. **Code safety and compatibility:** * Suppressed visibility attribute warnings for the `ParamInfo` struct on Linux using GCC diagnostic pragmas, while maintaining compatibility with Windows. [[1]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R117-R121) [[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R132-R134) * Updated type casting in the `BindParameters` function to use `reinterpret_cast` and `static_cast` for safe conversion of numeric precision and scale values to `SQLPOINTER`, preventing potential type safety issues. <!-- ### PR Title Guide > For feature requests FEAT: (short-description) > For non-feature requests like test case updates, config updates , dependency updates etc CHORE: (short-description) > For Fix requests FIX: (short-description) > For doc update requests DOC: (short-description) > For Formatting, indentation, or styling update STYLE: (short-description) > For Refactor, without any feature changes REFACTOR: (short-description) > For release related changes, without any feature changes RELEASE: #<RELEASE_VERSION> (short-description) ### Contribution Guidelines External contributors: - Create a GitHub issue first: https://github.com/microsoft/mssql-python/issues/new - Link the GitHub issue in the "GitHub Issue" section above - Follow the PR title format and provide a meaningful summary mssql-python maintainers: - Create an ADO Work Item following internal processes - Link the ADO Work Item in the "ADO Work Item" section above - Follow the PR title format and provide a meaningful summary -->
1 parent 4d2634a commit a896a93

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

mssql_python/pybind/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
# Enable verbose output to see actual compiler/linker commands
99
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "Verbose output" FORCE)
1010

11+
# Treat CMake warnings as errors
12+
set(CMAKE_ERROR_DEPRECATED TRUE)
13+
set(CMAKE_WARN_DEPRECATED TRUE)
14+
1115
if (MSVC)
1216
# Security compiler options for OneBranch compliance
1317
message(STATUS "Applying MSVC security compiler options for OneBranch compliance")
@@ -302,6 +306,21 @@ if(MSVC)
302306
target_compile_options(ddbc_bindings PRIVATE /W4 /WX)
303307
endif()
304308

309+
# Add warning flags for GCC/Clang on Linux and macOS
310+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
311+
target_compile_options(ddbc_bindings PRIVATE
312+
-Werror # Treat warnings as errors
313+
-Wattributes # Enable attribute warnings (cross-compiler)
314+
)
315+
316+
# GCC-specific warning flags
317+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
318+
target_compile_options(ddbc_bindings PRIVATE
319+
-Wint-to-pointer-cast # GCC-specific warning for integer-to-pointer casts
320+
)
321+
endif()
322+
endif()
323+
305324
# Add macOS-specific string conversion fix
306325
if(APPLE)
307326
message(STATUS "Enabling macOS string conversion fix")

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ py::object get_uuid_class() {
114114

115115
// Struct to hold parameter information for binding. Used by SQLBindParameter.
116116
// This struct is shared between C++ & Python code.
117+
// Suppress -Wattributes warning for ParamInfo struct
118+
// The warning is triggered because pybind11 handles visibility attributes automatically,
119+
// and having additional attributes on the struct can cause conflicts on Linux with GCC
120+
#ifdef __GNUC__
121+
#pragma GCC diagnostic push
122+
#pragma GCC diagnostic ignored "-Wattributes"
123+
#endif
117124
struct ParamInfo {
118125
SQLSMALLINT inputOutputType;
119126
SQLSMALLINT paramCType;
@@ -124,6 +131,9 @@ struct ParamInfo {
124131
bool isDAE = false; // Indicates if we need to stream
125132
py::object dataPtr;
126133
};
134+
#ifdef __GNUC__
135+
#pragma GCC diagnostic pop
136+
#endif
127137

128138
// Mirrors the SQL_NUMERIC_STRUCT. But redefined to replace val char array
129139
// with std::string, because pybind doesn't allow binding char array.
@@ -713,23 +723,23 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
713723
}
714724
SQL_NUMERIC_STRUCT* numericPtr = reinterpret_cast<SQL_NUMERIC_STRUCT*>(dataPtr);
715725
rc = SQLSetDescField_ptr(hDesc, 1, SQL_DESC_PRECISION,
716-
(SQLPOINTER)numericPtr->precision, 0);
726+
reinterpret_cast<SQLPOINTER>(static_cast<uintptr_t>(numericPtr->precision)), 0);
717727
if (!SQL_SUCCEEDED(rc)) {
718728
LOG("BindParameters: SQLSetDescField(SQL_DESC_PRECISION) "
719729
"failed for param[%d] - SQLRETURN=%d",
720730
paramIndex, rc);
721731
return rc;
722732
}
723733

724-
rc = SQLSetDescField_ptr(hDesc, 1, SQL_DESC_SCALE, (SQLPOINTER)numericPtr->scale, 0);
734+
rc = SQLSetDescField_ptr(hDesc, 1, SQL_DESC_SCALE, reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(numericPtr->scale)), 0);
725735
if (!SQL_SUCCEEDED(rc)) {
726736
LOG("BindParameters: SQLSetDescField(SQL_DESC_SCALE) failed "
727737
"for param[%d] - SQLRETURN=%d",
728738
paramIndex, rc);
729739
return rc;
730740
}
731741

732-
rc = SQLSetDescField_ptr(hDesc, 1, SQL_DESC_DATA_PTR, (SQLPOINTER)numericPtr, 0);
742+
rc = SQLSetDescField_ptr(hDesc, 1, SQL_DESC_DATA_PTR, reinterpret_cast<SQLPOINTER>(numericPtr), 0);
733743
if (!SQL_SUCCEEDED(rc)) {
734744
LOG("BindParameters: SQLSetDescField(SQL_DESC_DATA_PTR) failed "
735745
"for param[%d] - SQLRETURN=%d",

0 commit comments

Comments
 (0)