Add Strings probe to Charmonizer For now, the Strings probe only tests for a C99-compatible snprintf function and for _scprintf and _snprintf from MSVCRT.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/6360a7c8 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/6360a7c8 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/6360a7c8 Branch: refs/heads/master Commit: 6360a7c827e671588b6cde58a2bbc66fa621dc78 Parents: d874b33 Author: Nick Wellnhofer <[email protected]> Authored: Wed Dec 26 19:17:55 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Dec 28 22:22:58 2012 +0100 ---------------------------------------------------------------------- charmonizer/src/Charmonizer/Probe/Strings.c | 89 ++++++++++++++++++++++ charmonizer/src/Charmonizer/Probe/Strings.h | 39 ++++++++++ 2 files changed, 128 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/6360a7c8/charmonizer/src/Charmonizer/Probe/Strings.c ---------------------------------------------------------------------- diff --git a/charmonizer/src/Charmonizer/Probe/Strings.c b/charmonizer/src/Charmonizer/Probe/Strings.c new file mode 100644 index 0000000..dabe9e2 --- /dev/null +++ b/charmonizer/src/Charmonizer/Probe/Strings.c @@ -0,0 +1,89 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Charmonizer/Core/Compiler.h" +#include "Charmonizer/Core/ConfWriter.h" +#include "Charmonizer/Probe/Strings.h" + +/* Check for C99-compatible snprintf and possible replacements. + */ +static void +chaz_Strings_probe_c99_snprintf(void); + +void +chaz_Strings_run(void) { + chaz_ConfWriter_start_module("Strings"); + + /* Check for C99 snprintf. */ + chaz_Strings_probe_c99_snprintf(); + + chaz_ConfWriter_end_module(); +} + +static void +chaz_Strings_probe_c99_snprintf(void) { + static const char snprintf_code[] = + CHAZ_QUOTE( #include <stdio.h> ) + CHAZ_QUOTE( int main() { ) + CHAZ_QUOTE( char buf[4]; ) + CHAZ_QUOTE( int result; ) + CHAZ_QUOTE( result = snprintf(buf, 4, "%s", "12345"); ) + CHAZ_QUOTE( printf("%d", result); ) + CHAZ_QUOTE( return 0; ) + CHAZ_QUOTE( } ); + static const char detect__scprintf_code[] = + CHAZ_QUOTE( #include <stdio.h> ) + CHAZ_QUOTE( int main() { ) + CHAZ_QUOTE( int result; ) + CHAZ_QUOTE( result = _scprintf("%s", "12345"); ) + CHAZ_QUOTE( printf("%d", result); ) + CHAZ_QUOTE( return 0; ) + CHAZ_QUOTE( } ); + static const char detect__snprintf_code[] = + CHAZ_QUOTE( #include <stdio.h> ) + CHAZ_QUOTE( int main() { ) + CHAZ_QUOTE( char buf[6]; ) + CHAZ_QUOTE( int result; ) + CHAZ_QUOTE( result = _snprintf(buf, 6, "%s", "12345"); ) + CHAZ_QUOTE( printf("%d", result); ) + CHAZ_QUOTE( return 0; ) + CHAZ_QUOTE( } ); + char *output = NULL; + size_t output_len; + + /* If the buffer passed to snprintf is too small, verify that snprintf + * returns the length of the untruncated string which would have been + * written to a large enough buffer. + */ + output = chaz_CC_capture_output(snprintf_code, &output_len); + if (output != NULL) { + long result = strtol(output, NULL, 10); + if (result == 5) { + chaz_ConfWriter_add_def("HAS_C99_SNPRINTF", NULL); + } + free(output); + } + + /* Test for _scprintf and _snprintf found in the MSVCRT. + */ + if (chaz_CC_test_compile(detect__scprintf_code)) { + chaz_ConfWriter_add_def("HAS__SCPRINTF", NULL); + } + if (chaz_CC_test_compile(detect__snprintf_code)) { + chaz_ConfWriter_add_def("HAS__SNPRINTF", NULL); + } +} + http://git-wip-us.apache.org/repos/asf/lucy/blob/6360a7c8/charmonizer/src/Charmonizer/Probe/Strings.h ---------------------------------------------------------------------- diff --git a/charmonizer/src/Charmonizer/Probe/Strings.h b/charmonizer/src/Charmonizer/Probe/Strings.h new file mode 100644 index 0000000..031459d --- /dev/null +++ b/charmonizer/src/Charmonizer/Probe/Strings.h @@ -0,0 +1,39 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Charmonizer/Probe/Strings.h + */ + +#ifndef H_CHAZ_STRINGS +#define H_CHAZ_STRINGS + +#ifdef __cplusplus +extern "C" { +#endif + +/* The Strings module attempts to detect whether snprintf works as specified + * by the C99 standard. It also looks for system-specific functions which can + * be used to emulate snprintf. + */ +void chaz_Strings_run(void); + +#ifdef __cplusplus +} +#endif + +#endif /* H_CHAZ_STRINGS */ + +
