Skip to content

Check and clean #includes in /src - #32592

Open
iccir wants to merge 2 commits into
matplotlib:mainfrom
iccir:check-cpp-includes
Open

iccir wants to merge 2 commits into
matplotlib:mainfrom
iccir:check-cpp-includes

Conversation

@iccir

@iccir iccir commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

This PR adds a new "check-cpp-includes.py" tool which wraps clang-include-cleaner from LLVM. I then ran this tool on all files in /src and used the output to fix our includes.

AI Disclosure

I used Claude to create a prototype to integrate meson with clang-include-cleaner. I heavily modified that original into the version included in this PR.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [/] Plotting related features are demonstrated in an example
  • [/] New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@iccir iccir changed the title Add check_cpp_includes.py tool and clean our includes Check and clean #includes in /src Nov 13, 2026
@iccir
iccir force-pushed the check-cpp-includes branch from 8d76750 to 99c3164 Compare November 13, 2026 19:05
@iccir
iccir marked this pull request as ready for review November 13, 2026 19:05
Comment thread src/ft2font_wrapper.cpp Outdated
#include <cstring>
#include <exception>
#include <iterator>
#include <math.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the same as cmath, but in the wrong namespace? Should just fix the usage instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's due to M_PI. <cmath> doesn't define it directly and std::numbers::pi is C++20 only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We hard-code a d_M_PI in path_converters.cpp as well. I could move everything to mpl::pi if you want and use that in all 3 places.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think consolidating makes sense

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh yes, I think I may have tried to delete a copy, but they were slightly different precision and it caused very small image comparison failures, but I could be misremembering.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I looked and M_PI and d_M_PI are the same (double d_M_PI = 3.14159265358979323846)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There are three in our sources:

// ft2font.h
#define M_PI          3.14159265358979323846264338328 

// ft2font_wrapper.cpp
#define M_PI          3.14159265358979323846264338328 

// path_converters.h
const double d_M_PI = 3.14159265358979323846;

On my machine, macOS defines M_PI as:
#define M_PI          3.14159265358979323846264338327950288          

macOS defines std::numbers::pi as:
                      3.141592653589793238462643383279502

(Whitespace adjusted to make it easier to read)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, and Agg is const double pi = 3.14159265358979323846 in agg_basics.h. That might be what @scottshambaugh was thinking of.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm making the following changes in 72f6403:

  • Define mpl::pi to be the same as std::numbers::pi in LLVM libc++. This is a change in precision from M_PI, but I don't think it should matter as M_PI can already vary from platform to platform.
  • Use mpl::pi in ft2… and remove <math.h>
  • Use agg::pi from agg_basics.h in path_converters.h

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, that was me looking at posix's M_PI (3.14159265358979323846) rather than grepping to see if we defined it...

Anyway, chalk up another one for moving to something consistent

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants