Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ralian
Copy link

@ralian ralian commented Jul 18, 2024

  • Define _USE_MATH_DEFINES on MSVC wherever constants like M_PI are needed
  • Do not allow use of format attribute outside of GCC

@coderabbitai
Copy link

coderabbitai bot commented Jul 18, 2024

Walkthrough

Walkthrough

The changes primarily focus on enhancing compatibility and type safety across different compilers, especially Microsoft Visual Studio and GCC. By defining _USE_MATH_DEFINES where necessary, the updates ensure that mathematical constants are available during compilation on Windows. Additionally, the use of the format attribute with GCC functions improves compile-time checks for format string mismatches, promoting better error detection and code reliability.

Changes

File Change Summary
liblwgeom/liblwgeom_internal.h Introduced _USE_MATH_DEFINES for _MSC_VER to ensure math constants are available on Windows.
postgis/.../geography_centroid.c Added _USE_MATH_DEFINES for _MSC_VER and included <math.h>.
postgis/.../geography_measurement.c Added _USE_MATH_DEFINES for _MSC_VER and included <math.h>.
postgis/.../lwgeom_functions_basic.c Added _USE_MATH_DEFINES for _MSC_VER, included <math.h>, and adjusted header inclusion order.
postgis/.../lwgeom_spheroid.c Added _USE_MATH_DEFINES for _MSC_VER and included <math.h>.
liblwgeom/lwgeom_geos.h Added __attribute__ (( format(printf, 1, 0) )) for GEOSMessageHandler when using GCC.
liblwgeom/lwgeom_log.h Added format attribute for lwnotice, lwerror, and lwdebug functions under __GNUC__.

Poem

A rabbit's code, so swift and neat,
Ensures math constants are complete.
With GCC's format checks so wise,
Mismatched strings now meet their demise.
On Windows, constants now align,
Our software's future, ever so fine.
🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0341df4 and ea6eb43.

Files selected for processing (7)
  • liblwgeom/liblwgeom_internal.h (1 hunks)
  • liblwgeom/lwgeom_geos.h (1 hunks)
  • liblwgeom/lwgeom_log.h (3 hunks)
  • postgis/geography_centroid.c (1 hunks)
  • postgis/geography_measurement.c (1 hunks)
  • postgis/lwgeom_functions_basic.c (1 hunks)
  • postgis/lwgeom_spheroid.c (1 hunks)
Files skipped from review due to trivial changes (2)
  • postgis/geography_centroid.c
  • postgis/geography_measurement.c
Additional comments not posted (7)
liblwgeom/lwgeom_geos.h (1)

28-32: LGTM! The conditional attribute improves error detection.

The use of __attribute__ (( format(printf, 1, 0) )) within the #ifdef __GNUC__ block ensures compile-time format string checks with GCC, enhancing type safety and reliability.

liblwgeom/lwgeom_log.h (3)

129-133: LGTM! The conditional attribute improves error detection.

The use of __attribute__ ((format (printf, 1, 2))) within the #ifdef __GNUC__ block ensures compile-time format string checks with GCC, enhancing type safety and reliability.


143-147: LGTM! The conditional attribute improves error detection.

The use of __attribute__ ((format (printf, 1, 2))) within the #ifdef __GNUC__ block ensures compile-time format string checks with GCC, enhancing type safety and reliability.


156-160: LGTM! The conditional attribute improves error detection.

The use of __attribute__ ((format (printf, 2, 3))) within the #ifdef __GNUC__ block ensures compile-time format string checks with GCC, enhancing type safety and reliability.

postgis/lwgeom_spheroid.c (1)

28-30: LGTM! The preprocessor directive ensures compatibility with MSVC.

Defining _USE_MATH_DEFINES and including <math.h> ensures that mathematical constants are available when compiling with Microsoft Visual Studio, enhancing cross-platform compatibility.

liblwgeom/liblwgeom_internal.h (1)

44-46: LGTM! The preprocessor directive ensures compatibility with MSVC.

Defining _USE_MATH_DEFINES and including <math.h> ensures that mathematical constants are available when compiling with Microsoft Visual Studio, enhancing cross-platform compatibility.

postgis/lwgeom_functions_basic.c (1)

38-42: Ensure compatibility with other compilers.

The changes introduce a preprocessor directive for _MSC_VER to define _USE_MATH_DEFINES and include the <math.h> header to ensure mathematical constants are defined when compiling with Microsoft Visual Studio. The order of header inclusions has been adjusted accordingly.

However, ensure that these changes do not affect compatibility with other compilers.

Verification successful

Ensure compatibility with other compilers.

The changes introduce a preprocessor directive for _MSC_VER to define _USE_MATH_DEFINES and include the <math.h> header to ensure mathematical constants are defined when compiling with Microsoft Visual Studio. The order of header inclusions has been adjusted accordingly. The presence of conditional compilation blocks indicates that these changes should not affect compatibility with other compilers.

  • The _MSC_VER directive is only active when compiling with Microsoft Visual Studio.
  • Existing directives handle GCC-specific warnings.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify compatibility with other compilers.

# Test: Compile the code with different compilers and verify no errors.
# Expect: No compilation errors.

# Check for GCC
gcc -c postgis/lwgeom_functions_basic.c

# Check for Clang
clang -c postgis/lwgeom_functions_basic.c

# Check for MSVC (simulated)
# Note: MSVC cannot be directly tested in this environment.
echo "Simulate MSVC compilation check"

Length of output: 303


Script:

#!/bin/bash
# Search for preprocessor directives and conditional compilation blocks in the file.
ast-grep --lang c --pattern $'#ifdef $_
$$$
#endif' postgis/lwgeom_functions_basic.c

Length of output: 85


Script:

#!/bin/bash
# Search for all preprocessor directives in the file.
rg '^#' postgis/lwgeom_functions_basic.c -A 2

Length of output: 872


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

*/
void lwnotice(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
void lwnotice(const char *fmt, ...)
#ifdef __GNUC__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also available with CLANG https://clang.llvm.org/docs/AttributeReference.html#format (but with printf in the doc). I cannot test this week.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong for specific cases, but I believe __GNUC__ is also defined on clang - as it is meant to be compatible with GCC in almost every way. See this rather humorous SO post: https://stackoverflow.com/questions/38499462/how-to-tell-clang-to-stop-pretending-to-be-other-compilers

I still need to do some more testing as well, though. Let me know if you uncover anything I missed in your own testing.

@strk
Copy link
Member

strk commented Aug 23, 2024

See https://git.osgeo.org/gitea/postgis/postgis/pulls/208 for more comments (or best file a ticket so discussion remains there) - https://trac.osgeo.org/postgis

@robe2
Copy link
Member

robe2 commented Oct 13, 2025

I assume this pull request is obsolete? So I can close it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants