Skip to content

Commit 77e9c1f

Browse files
authored
Fix a few issues with the C generator (part 6) (#20332)
* [C] Fail build on implicit function declarations * [C] Complete changes from patch 34c3f8c * Update samples * [C] Fail build for global functions with no declaration * [C] Use "static" for apiClient.c internal functions * Update samples
1 parent 8790f7c commit 77e9c1f

File tree

12 files changed

+39
-36
lines changed

12 files changed

+39
-36
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
910

1011
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1112

@@ -14,7 +15,7 @@ find_package(OpenSSL)
1415
if (OPENSSL_FOUND)
1516
message (STATUS "OPENSSL found")
1617

17-
set(CMAKE_C_FLAGS "-DOPENSSL")
18+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1819
if(CMAKE_VERSION VERSION_LESS 3.4)
1920
include_directories(${OPENSSL_INCLUDE_DIR})
2021
include_directories(${OPENSSL_INCLUDE_DIRS})

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

+7-7
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,17 @@ void sslConfig_free(sslConfig_t *sslConfig) {
173173
free(sslConfig);
174174
}
175175

176-
void replaceSpaceWithPlus(char *stringToProcess) {
176+
static void replaceSpaceWithPlus(char *stringToProcess) {
177177
for(int i = 0; i < strlen(stringToProcess); i++) {
178178
if(stringToProcess[i] == ' ') {
179179
stringToProcess[i] = '+';
180180
}
181181
}
182182
}
183183

184-
char *assembleTargetUrl(const char *basePath,
185-
const char *operationParameter,
186-
list_t *queryParameters) {
184+
static char *assembleTargetUrl(const char *basePath,
185+
const char *operationParameter,
186+
list_t *queryParameters) {
187187
int neededBufferSizeForQueryParameters = 0;
188188
listEntry_t *listEntry;
189189
@@ -234,7 +234,7 @@ char *assembleTargetUrl(const char *basePath,
234234
return targetUrl;
235235
}
236236

237-
char *assembleHeaderField(char *key, char *value) {
237+
static char *assembleHeaderField(char *key, char *value) {
238238
char *header = malloc(strlen(key) + strlen(value) + 3);
239239
240240
strcpy(header, key),
@@ -244,13 +244,13 @@ char *assembleHeaderField(char *key, char *value) {
244244
return header;
245245
}
246246

247-
void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
247+
static void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
248248
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters);
249249
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE,
250250
(curl_off_t)bodyParametersLength);
251251
}
252252

253-
int lengthOfKeyPair(keyValuePair_t *keyPair) {
253+
static int lengthOfKeyPair(keyValuePair_t *keyPair) {
254254
long length = 0;
255255
if((keyPair->key != NULL) &&
256256
(keyPair->value != NULL) )

modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
376376
{{/isBoolean}}
377377
{{#isEnum}}
378378
{{#isString}}
379-
if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{name}}}{{classname}}_ToString({{{classname}}}->{{{name}}})) == NULL)
379+
if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{classname}}_{{name}}_ToString({{{classname}}}->{{{name}}})) == NULL)
380380
{
381381
goto fail; //Enum
382382
}

samples/client/others/c/bearerAuth/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
910

1011
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1112

@@ -14,7 +15,7 @@ find_package(OpenSSL)
1415
if (OPENSSL_FOUND)
1516
message (STATUS "OPENSSL found")
1617

17-
set(CMAKE_C_FLAGS "-DOPENSSL")
18+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1819
if(CMAKE_VERSION VERSION_LESS 3.4)
1920
include_directories(${OPENSSL_INCLUDE_DIR})
2021
include_directories(${OPENSSL_INCLUDE_DIRS})

samples/client/others/c/bearerAuth/src/apiClient.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ void sslConfig_free(sslConfig_t *sslConfig) {
8989
free(sslConfig);
9090
}
9191

92-
void replaceSpaceWithPlus(char *stringToProcess) {
92+
static void replaceSpaceWithPlus(char *stringToProcess) {
9393
for(int i = 0; i < strlen(stringToProcess); i++) {
9494
if(stringToProcess[i] == ' ') {
9595
stringToProcess[i] = '+';
9696
}
9797
}
9898
}
9999

100-
char *assembleTargetUrl(const char *basePath,
101-
const char *operationParameter,
102-
list_t *queryParameters) {
100+
static char *assembleTargetUrl(const char *basePath,
101+
const char *operationParameter,
102+
list_t *queryParameters) {
103103
int neededBufferSizeForQueryParameters = 0;
104104
listEntry_t *listEntry;
105105

@@ -150,7 +150,7 @@ char *assembleTargetUrl(const char *basePath,
150150
return targetUrl;
151151
}
152152

153-
char *assembleHeaderField(char *key, char *value) {
153+
static char *assembleHeaderField(char *key, char *value) {
154154
char *header = malloc(strlen(key) + strlen(value) + 3);
155155

156156
strcpy(header, key),
@@ -160,13 +160,13 @@ char *assembleHeaderField(char *key, char *value) {
160160
return header;
161161
}
162162

163-
void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
163+
static void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
164164
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters);
165165
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE,
166166
(curl_off_t)bodyParametersLength);
167167
}
168168

169-
int lengthOfKeyPair(keyValuePair_t *keyPair) {
169+
static int lengthOfKeyPair(keyValuePair_t *keyPair) {
170170
long length = 0;
171171
if((keyPair->key != NULL) &&
172172
(keyPair->value != NULL) )

samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
910

1011
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1112

@@ -14,7 +15,7 @@ find_package(OpenSSL)
1415
if (OPENSSL_FOUND)
1516
message (STATUS "OPENSSL found")
1617

17-
set(CMAKE_C_FLAGS "-DOPENSSL")
18+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
1819
if(CMAKE_VERSION VERSION_LESS 3.4)
1920
include_directories(${OPENSSL_INCLUDE_DIR})
2021
include_directories(${OPENSSL_INCLUDE_DIRS})

samples/client/petstore/c-useJsonUnformatted/model/order.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ cJSON *order_convertToJSON(order_t *order) {
9494

9595
// order->status
9696
if(order->status != openapi_petstore_order_STATUS_NULL) {
97-
if(cJSON_AddStringToObject(item, "status", statusorder_ToString(order->status)) == NULL)
97+
if(cJSON_AddStringToObject(item, "status", order_status_ToString(order->status)) == NULL)
9898
{
9999
goto fail; //Enum
100100
}

samples/client/petstore/c-useJsonUnformatted/model/pet.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
148148

149149
// pet->status
150150
if(pet->status != openapi_petstore_pet_STATUS_NULL) {
151-
if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL)
151+
if(cJSON_AddStringToObject(item, "status", pet_status_ToString(pet->status)) == NULL)
152152
{
153153
goto fail; //Enum
154154
}

samples/client/petstore/c-useJsonUnformatted/src/apiClient.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ void sslConfig_free(sslConfig_t *sslConfig) {
116116
free(sslConfig);
117117
}
118118

119-
void replaceSpaceWithPlus(char *stringToProcess) {
119+
static void replaceSpaceWithPlus(char *stringToProcess) {
120120
for(int i = 0; i < strlen(stringToProcess); i++) {
121121
if(stringToProcess[i] == ' ') {
122122
stringToProcess[i] = '+';
123123
}
124124
}
125125
}
126126

127-
char *assembleTargetUrl(const char *basePath,
128-
const char *operationParameter,
129-
list_t *queryParameters) {
127+
static char *assembleTargetUrl(const char *basePath,
128+
const char *operationParameter,
129+
list_t *queryParameters) {
130130
int neededBufferSizeForQueryParameters = 0;
131131
listEntry_t *listEntry;
132132

@@ -177,7 +177,7 @@ char *assembleTargetUrl(const char *basePath,
177177
return targetUrl;
178178
}
179179

180-
char *assembleHeaderField(char *key, char *value) {
180+
static char *assembleHeaderField(char *key, char *value) {
181181
char *header = malloc(strlen(key) + strlen(value) + 3);
182182

183183
strcpy(header, key),
@@ -187,13 +187,13 @@ char *assembleHeaderField(char *key, char *value) {
187187
return header;
188188
}
189189

190-
void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
190+
static void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
191191
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters);
192192
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE,
193193
(curl_off_t)bodyParametersLength);
194194
}
195195

196-
int lengthOfKeyPair(keyValuePair_t *keyPair) {
196+
static int lengthOfKeyPair(keyValuePair_t *keyPair) {
197197
long length = 0;
198198
if((keyPair->key != NULL) &&
199199
(keyPair->value != NULL) )

samples/client/petstore/c/model/order.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ cJSON *order_convertToJSON(order_t *order) {
9494

9595
// order->status
9696
if(order->status != openapi_petstore_order_STATUS_NULL) {
97-
if(cJSON_AddStringToObject(item, "status", statusorder_ToString(order->status)) == NULL)
97+
if(cJSON_AddStringToObject(item, "status", order_status_ToString(order->status)) == NULL)
9898
{
9999
goto fail; //Enum
100100
}

samples/client/petstore/c/model/pet.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
148148

149149
// pet->status
150150
if(pet->status != openapi_petstore_pet_STATUS_NULL) {
151-
if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL)
151+
if(cJSON_AddStringToObject(item, "status", pet_status_ToString(pet->status)) == NULL)
152152
{
153153
goto fail; //Enum
154154
}

samples/client/petstore/c/src/apiClient.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ void sslConfig_free(sslConfig_t *sslConfig) {
116116
free(sslConfig);
117117
}
118118

119-
void replaceSpaceWithPlus(char *stringToProcess) {
119+
static void replaceSpaceWithPlus(char *stringToProcess) {
120120
for(int i = 0; i < strlen(stringToProcess); i++) {
121121
if(stringToProcess[i] == ' ') {
122122
stringToProcess[i] = '+';
123123
}
124124
}
125125
}
126126

127-
char *assembleTargetUrl(const char *basePath,
128-
const char *operationParameter,
129-
list_t *queryParameters) {
127+
static char *assembleTargetUrl(const char *basePath,
128+
const char *operationParameter,
129+
list_t *queryParameters) {
130130
int neededBufferSizeForQueryParameters = 0;
131131
listEntry_t *listEntry;
132132

@@ -177,7 +177,7 @@ char *assembleTargetUrl(const char *basePath,
177177
return targetUrl;
178178
}
179179

180-
char *assembleHeaderField(char *key, char *value) {
180+
static char *assembleHeaderField(char *key, char *value) {
181181
char *header = malloc(strlen(key) + strlen(value) + 3);
182182

183183
strcpy(header, key),
@@ -187,13 +187,13 @@ char *assembleHeaderField(char *key, char *value) {
187187
return header;
188188
}
189189

190-
void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
190+
static void postData(CURL *handle, const char *bodyParameters, size_t bodyParametersLength) {
191191
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters);
192192
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE,
193193
(curl_off_t)bodyParametersLength);
194194
}
195195

196-
int lengthOfKeyPair(keyValuePair_t *keyPair) {
196+
static int lengthOfKeyPair(keyValuePair_t *keyPair) {
197197
long length = 0;
198198
if((keyPair->key != NULL) &&
199199
(keyPair->value != NULL) )

0 commit comments

Comments
 (0)