Skip to content

basic: fix FreeBSD build failure around usage of assert() #64379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mhjacobson
Copy link
Contributor

@mhjacobson mhjacobson commented Mar 15, 2023

LLVM's CASReference.h uses assert() without first including <cassert>, which poses a problem here when CASReference.h is included first thing in a submodule.

In FreeBSD's <assert.h>, assert() expands to a call to a function that is declared within header guards. That is, only the first include of <assert.h> gets the function declaration.

This is module-unfriendly, since it means that if multiple submodules of a module both include <assert.h>, only one of them ends up with a usable assert() macro. If you haven't (transitively) included the right submodule, and you haven't included <assert.h> yourself, then you get a compile error that looks like the following:

llvm-project/llvm/include/llvm/CAS/CASReference.h:75:5: error: declaration of '__assert' must be imported from module 'LLVM_Utils.Support.MathExtras' before it is required
    assert(InternalRef != getDenseMapEmptyRef() && "Reserved for DenseMapInfo");

/usr/include/assert.h:77:6: note: declaration here is not visible
void __assert(const char *, const char *, int, const char *) __dead2;

swift/include/swift/AST/DeclContext.h:31:10: error: could not build module 'BasicBridging'
#include "swift/Basic/SourceLoc.h"

swift/SwiftCompilerSources/Sources/Basic/SourceLoc.swift:13:8: error: could not build C module 'ASTBridging'
import ASTBridging

Ideally CASReference.h would include <cassert> itself. But use of assert() without a prior include of <cassert> is common in LLVM. It's even encouraged by the LLVM "coding standards", which say:

Use the “assert” macro to its fullest. [...] The “<cassert>” header file is
probably already included by the header files you are using, so it doesn’t
cost anything to use it.

Since the include of CASReference.h appears to be a temporary workaround, add to it by including <cassert>, too.

LLVM's `CASReference.h` uses `assert()` without first including `<cassert>`,
which poses a problem here when `CASReference.h` is included first thing in a
submodule.

In FreeBSD's <assert.h>, `assert()` expands to a call to a function that is
declared within header guards.  That is, only the first include of `<assert.h>`
gets the function declaration.

This is module-unfriendly, since it means that if multiple submodules of a
module both include `<assert.h>`, only one of them ends up with a usable
`assert()` macro.  If you haven't (transitively) included the right submodule,
and you haven't included `<assert.h>` yourself, then you get a compile error
that looks like the following:

```
llvm-project/llvm/include/llvm/CAS/CASReference.h:75:5: error: declaration of '__assert' must be imported from module 'LLVM_Utils.Support.MathExtras' before it is required
    assert(InternalRef != getDenseMapEmptyRef() && "Reserved for DenseMapInfo");

/usr/include/assert.h:77:6: note: declaration here is not visible
void __assert(const char *, const char *, int, const char *) __dead2;

swift/include/swift/AST/DeclContext.h:31:10: error: could not build module 'BasicBridging'
#include "swift/Basic/SourceLoc.h"

swift/SwiftCompilerSources/Sources/Basic/SourceLoc.swift:13:8: error: could not build C module 'ASTBridging'
import ASTBridging
```

Ideally `CASReference.h` would include `<cassert>` itself.  But use of
`assert()` without a prior include of `<cassert>` is common in LLVM.  It's even
encouraged by the LLVM "coding standards", which say:

> Use the “assert” macro to its fullest. [...] The “<cassert>” header file is
> probably already included by the header files you are using, so it doesn’t
> cost anything to use it.

Since the include of `CASReference.h` appears to be a temporary workaround, add
to it by including `<cassert>`, too.
@mhjacobson mhjacobson requested a review from eeckstein March 15, 2023 10:52
@mhjacobson
Copy link
Contributor Author

@swift-ci please smoke test

@mhjacobson
Copy link
Contributor Author

@swift-ci please test Windows platform

@mhjacobson
Copy link
Contributor Author

@swift-ci Please test Windows platform

1 similar comment
@xwu
Copy link
Collaborator

xwu commented Mar 20, 2023

@swift-ci Please test Windows platform

@mhjacobson
Copy link
Contributor Author

@eeckstein, would you mind to take a look (or to suggest someone else whom I should ask)? Thanks!

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

lgtm

@eeckstein eeckstein merged commit 09c3e39 into swiftlang:main Apr 6, 2023
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.

3 participants