Skip to content

verify: Turn off AssertMacros.h's names without underscores on Apple targets - #42

Merged
SwayamInSync merged 6 commits into
mainfrom
fix/darwin-assert-macros
Oct 21, 2026
Merged

SwayamInSync merged 6 commits into
mainfrom
fix/darwin-assert-macros

Conversation

@SwayamInSync

Copy link
Copy Markdown
Owner

Apple's <AssertMacros.h>, which some of Apple's framework headers include, defines check(assertion) as __Check(assertion) unless __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES is 0, and defaults it to 1. In production builds __Check expands to nothing. The macro is function-like and the preprocessor runs before the parser sees a construct, so every cppverify::check(...) after the header breaks. With Apple's latest published copy of the header (CarbonHeaders-18.1, apple-oss-distributions) and an arm64-apple-macosx14.0 target, main reports:

uses_check.cpp:8:19: error: expected unqualified-id
    8 |   cv::check(x > 0);
      |                   ^

The header's own comment names the fix: define the switch to 0, and it defines only its __Check, __Require, and __Verify names, which Apple recommends over the old ones.

The change: with -fverify-contracts on a Darwin target, InitializePreprocessor defines __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES to 0 before the implicit <cppverify.h>, unless the command line defines it. It comes after the command line's -D options, so -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=1 keeps the old names. A #define of 1 in the source also takes effect, with a -Wmacro-redefined warning. Other targets and compilations without the flag are unaffected.

Docs: the syntax page gains a section on macros named like a construct. It has a checked example of a check macro breaking the construct, with its exact error and the #undef that restores it, and explains Apple's header and how a file that uses the old macro names keeps them. DESIGN records the predefinition. The release notes get an entry under "C++ that compiles now".

Testing: Verify/assert_macros_darwin.cpp, with a mock Inputs/apple/AssertMacros.h that keeps the header's logic, runs:

  • on macOS and iOS triples, with -verify and no diagnostics;
  • with the switch at 1 from the command line, which gives the old error;
  • the final value of the switch, which is 0 with the flag on Apple, and 1 without the flag or on Linux.

On main exactly the three Apple-with-flag lines fail and the three control lines pass. With Apple's real header the function verifies through cpp-verify -- -target arm64-apple-macosx14.0 (main: the error above). I could not run it on macOS itself; the macOS CI job runs the Verify suite there.

Checked: the Verify and Preprocessor suites (813 tests) pass in a release build, except Preprocessor/llvm-windres.rc, which needs llvm-windres and that build does not have it. In a build with assertions they pass (800 passed, 13 unsupported). After the rebase onto #39, the documentation examples pass and the site builds with -W.

@SwayamInSync
SwayamInSync merged commit 9b94945 into main Oct 21, 2026
14 checks passed
@SwayamInSync
SwayamInSync deleted the fix/darwin-assert-macros branch October 21, 2026 00:26
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.

1 participant