Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c_api] Improve ANSI compatibility by avoiding <stdbool.h> #4697

Merged
merged 6 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ if [[ $TASK == "lint" ]]; then
echo "Linting R code"
Rscript ${BUILD_DIRECTORY}/.ci/lint_r_code.R ${BUILD_DIRECTORY} || exit -1
echo "Linting C++ code"
cpplint --filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length --recursive ./src ./include ./R-package ./swig ./tests || exit -1
cpplint --filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length,-runtime/printf ./include/LightGBM/c_api.h || exit -1
cpplint --filter=-build/c++11,-build/include_subdir,-build/header_guard,-whitespace/line_length --recursive --exclude=./include/LightGBM/c_api.h ./src ./include ./R-package ./swig ./tests || exit -1
drewmiller marked this conversation as resolved.
Show resolved Hide resolved
cmake_files=$(find . -name CMakeLists.txt -o -path "*/cmake/*.cmake")
cmakelint --linelength=120 ${cmake_files} || true
exit 0
Expand Down
11 changes: 8 additions & 3 deletions include/LightGBM/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#endif


Expand Down Expand Up @@ -434,12 +433,12 @@ LIGHTGBM_C_EXPORT int LGBM_DatasetAddFeaturesFrom(DatasetHandle target,
/* --- start Booster interfaces */

/*!
* \brief Get boolean representing whether booster is fitting linear trees.
* \brief Get int representing whether booster is fitting linear trees.
* \param handle Handle of booster
* \param[out] out The address to hold linear trees indicator
* \return 0 when succeed, -1 when failure happens
*/
LIGHTGBM_C_EXPORT int LGBM_BoosterGetLinear(BoosterHandle handle, bool* out);
LIGHTGBM_C_EXPORT int LGBM_BoosterGetLinear(BoosterHandle handle, int* out);

/*!
* \brief Create a new boosting learner.
Expand Down Expand Up @@ -1355,11 +1354,17 @@ static char* LastErrorMsg() { static THREAD_LOCAL char err_msg[512] = "Everythin
#endif
/*!
* \brief Set string message of the last error.
* \note
* This will call unsafe ``sprintf`` when compiled using C standards before C99.
* \param msg Error message
*/
INLINE_FUNCTION void LGBM_SetLastError(const char* msg) {
const int err_buf_len = 512;
drewmiller marked this conversation as resolved.
Show resolved Hide resolved
#if !defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 199901L))
sprintf(LastErrorMsg(), "%s", msg);
drewmiller marked this conversation as resolved.
Show resolved Hide resolved
#else
snprintf(LastErrorMsg(), err_buf_len, "%s", msg);
#endif
}

#endif /* LIGHTGBM_C_API_H_ */
8 changes: 6 additions & 2 deletions src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1657,10 +1657,14 @@ int LGBM_BoosterGetNumClasses(BoosterHandle handle, int* out_len) {
API_END();
}

int LGBM_BoosterGetLinear(BoosterHandle handle, bool* out) {
int LGBM_BoosterGetLinear(BoosterHandle handle, int* out) {
API_BEGIN();
Booster* ref_booster = reinterpret_cast<Booster*>(handle);
*out = ref_booster->GetBoosting()->IsLinear();
if (ref_booster->GetBoosting()->IsLinear()) {
*out = 1;
} else {
*out = 0;
}
API_END();
}

Expand Down