Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8a8dcd3
[C23] Implement WG14 N3037
AaronBallman Mar 21, 2025
c0cc59b
Start using structural equivalence checking.
AaronBallman Mar 24, 2025
0485d26
Handle enumerations
AaronBallman Mar 24, 2025
5d6c4f5
Handle attributes, add a ton of tests
AaronBallman Mar 24, 2025
8cebad5
Update website
AaronBallman Mar 24, 2025
8c89a77
Fix failing tests
AaronBallman Mar 24, 2025
e44a162
Add additional test cases
AaronBallman Mar 24, 2025
980a9f7
Add codegen test
AaronBallman Mar 24, 2025
255e4b8
Add an enum test with a fixed underlying type
AaronBallman Mar 24, 2025
2a57c85
Properly diagnose bit-fields
AaronBallman Mar 25, 2025
ea24cf3
Improve diagnostic consistency
AaronBallman Mar 25, 2025
350666b
Fix failing tests
AaronBallman Mar 25, 2025
eac0f5c
Add another test, crank up diagnostic levels
AaronBallman Mar 25, 2025
9b3b5b0
Even more test cases
AaronBallman Mar 25, 2025
1543f75
Add additional test coverage
AaronBallman Mar 25, 2025
557d726
Restore code that was still used, just not in an obvious way
AaronBallman Mar 25, 2025
f37909e
Change formatting based on review feedback
AaronBallman Mar 25, 2025
ffe77b5
Fix comments; NFC
AaronBallman Mar 26, 2025
d6b390f
Remove unused code
AaronBallman Mar 26, 2025
2bde3a9
Fix bit-width checking and add a test case
AaronBallman Mar 26, 2025
d4fb30b
Use back_inserter; NFC
AaronBallman Mar 26, 2025
1fa6761
Fix formatting; NFC
AaronBallman Mar 26, 2025
13ce82d
Update test case to say this is implemented in Clang 21
AaronBallman Mar 27, 2025
fa3357c
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman Mar 27, 2025
730e758
Add an explicit triple to the codegen test
AaronBallman Mar 27, 2025
e2867ee
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman Apr 1, 2025
f71107f
Rework attribute handling based on review feedback
AaronBallman Apr 1, 2025
23c58b5
Add test codegen test for attribute handling
AaronBallman Apr 1, 2025
17ba4a7
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman Apr 3, 2025
59d1601
Add another test case
AaronBallman Apr 4, 2025
51afbe4
Oops, does not actually fix this issue.
AaronBallman Apr 4, 2025
f25b8a8
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman Apr 7, 2025
94ebba9
mergeTagTypes -> mergeTagDefinitions
AaronBallman Apr 11, 2025
82ba2c9
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman Apr 11, 2025
4cc2089
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3037
AaronBallman May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ C23 Feature Support
scope.
- Fixed a bug where you could not cast a null pointer constant to type
``nullptr_t``. Fixes #GH133644.
- Implemented `WG14 N3037 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3037.pdf>`_
which allows tag types to be redefined within the same translation unit so
long as both definitions are structurally equivalent (same tag types, same
tag names, same tag members, etc).

Non-comprehensive list of changes in this release
-------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -3130,6 +3130,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
QualType mergeTransparentUnionType(QualType, QualType,
bool OfBlockPointer=false,
bool Unqualified = false);
QualType mergeTagTypes(QualType, QualType);

QualType mergeObjCGCQualifiers(QualType, QualType);

Expand Down
11 changes: 8 additions & 3 deletions clang/include/clang/AST/ASTStructuralEquivalence.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct StructuralEquivalenceContext {
/// key: (from, to, IgnoreTemplateParmDepth)
using NonEquivalentDeclSet = llvm::DenseSet<std::tuple<Decl *, Decl *, int>>;

/// The language options to use for making a structural equivalence check.
const LangOptions &LangOpts;

/// AST contexts for which we are checking structural equivalence.
ASTContext &FromCtx, &ToCtx;

Expand Down Expand Up @@ -76,15 +79,17 @@ struct StructuralEquivalenceContext {
/// Whether to ignore comparing the depth of template param(TemplateTypeParm)
bool IgnoreTemplateParmDepth;

StructuralEquivalenceContext(ASTContext &FromCtx, ASTContext &ToCtx,
StructuralEquivalenceContext(const LangOptions &LangOpts, ASTContext &FromCtx,
ASTContext &ToCtx,
NonEquivalentDeclSet &NonEquivalentDecls,
StructuralEquivalenceKind EqKind,
bool StrictTypeSpelling = false,
bool Complain = true,
bool ErrorOnTagTypeMismatch = false,
bool IgnoreTemplateParmDepth = false)
: FromCtx(FromCtx), ToCtx(ToCtx), NonEquivalentDecls(NonEquivalentDecls),
EqKind(EqKind), StrictTypeSpelling(StrictTypeSpelling),
: LangOpts(LangOpts), FromCtx(FromCtx), ToCtx(ToCtx),
NonEquivalentDecls(NonEquivalentDecls), EqKind(EqKind),
StrictTypeSpelling(StrictTypeSpelling),
ErrorOnTagTypeMismatch(ErrorOnTagTypeMismatch), Complain(Complain),
IgnoreTemplateParmDepth(IgnoreTemplateParmDepth) {}

Expand Down
15 changes: 11 additions & 4 deletions clang/include/clang/Basic/DiagnosticASTKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,23 @@ def warn_odr_function_type_inconsistent : Warning<
"external function %0 declared with incompatible types in different "
"translation units (%1 vs. %2)">,
InGroup<ODR>;
def warn_odr_tag_type_with_attributes : Warning<
"type %0 has %select{an attribute|a member with an attribute}1 which "
"currently causes the types to be treated as though they are incompatible">,
InGroup<ODR>, DefaultError;
def note_odr_attr_here : Note<"attribute %0 here">;
def err_odr_tag_type_inconsistent
: Error<"type %0 has incompatible definitions in different translation "
"units">;
: Error<"type %0 has incompatible definitions%select{| in different "
"translation units}1">;
def warn_odr_tag_type_inconsistent
: Warning<"type %0 has incompatible definitions in different translation "
"units">,
: Warning<"type %0 has incompatible definitions%select{| in different "
"translation units}1">,
InGroup<ODR>;
def note_odr_tag_kind_here: Note<
"%0 is a %select{struct|interface|union|class|enum}1 here">;
def note_odr_field : Note<"field %0 has type %1 here">;
def note_odr_field_bit_width : Note<"bit-field %0 has bit-width %1 here">;
def note_odr_field_not_bit_field : Note<"field %0 is not a bit-field">;
def note_odr_field_name : Note<"field has name %0 here">;
def note_odr_missing_field : Note<"no corresponding field here">;
def note_odr_base : Note<"class has base type %0">;
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,8 @@ class Parser : public CodeCompletionHandler {
void ParseEnumSpecifier(SourceLocation TagLoc, DeclSpec &DS,
const ParsedTemplateInfo &TemplateInfo,
AccessSpecifier AS, DeclSpecContext DSC);
void ParseEnumBody(SourceLocation StartLoc, Decl *TagDecl);
void ParseEnumBody(SourceLocation StartLoc, Decl *TagDecl,
SkipBodyInfo *SkipBody = nullptr);
void ParseStructUnionBody(SourceLocation StartLoc, DeclSpec::TST TagType,
RecordDecl *TagDecl);

Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -4078,7 +4078,8 @@ class Sema final : public SemaBase {
Decl *ActOnEnumConstant(Scope *S, Decl *EnumDecl, Decl *LastEnumConstant,
SourceLocation IdLoc, IdentifierInfo *Id,
const ParsedAttributesView &Attrs,
SourceLocation EqualLoc, Expr *Val);
SourceLocation EqualLoc, Expr *Val,
SkipBodyInfo *SkipBody = nullptr);
void ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
Decl *EnumDecl, ArrayRef<Decl *> Elements, Scope *S,
const ParsedAttributesView &Attr);
Expand Down
19 changes: 18 additions & 1 deletion clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "clang/AST/APValue.h"
#include "clang/AST/ASTConcept.h"
#include "clang/AST/ASTMutationListener.h"
#include "clang/AST/ASTStructuralEquivalence.h"
#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/Attr.h"
#include "clang/AST/AttrIterator.h"
Expand Down Expand Up @@ -11399,6 +11400,22 @@ static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET,
return {};
}

QualType ASTContext::mergeTagTypes(QualType LHS, QualType RHS) {
// C17 and earlier and C++ disallow two tag definitions within the same TU
// from being compatible.
if (LangOpts.CPlusPlus || !LangOpts.C23)
return {};

// C23, on the other hand, requires the members to be "the same enough", so
// we use a structural equivalence check.
StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;
StructuralEquivalenceContext Ctx(
getLangOpts(), *this, *this, NonEquivalentDecls,
StructuralEquivalenceKind::Default, /*StrictTypeSpelling=*/false,
/*Complain=*/false, /*ErrorOnTagTypeMismatch=*/true);
return Ctx.IsEquivalent(LHS, RHS) ? LHS : QualType{};
}

QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer,
bool Unqualified, bool BlockReturnType,
bool IsConditionalOperator) {
Expand Down Expand Up @@ -11695,7 +11712,7 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer,
/*AllowCXX=*/false, IsConditionalOperator);
case Type::Record:
case Type::Enum:
return {};
return mergeTagTypes(LHS, RHS);
case Type::Builtin:
// Only exactly equal builtin types are compatible, which is tested above.
return {};
Expand Down
14 changes: 8 additions & 6 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2489,8 +2489,9 @@ bool ASTNodeImporter::IsStructuralMatch(Decl *From, Decl *To, bool Complain,
}

StructuralEquivalenceContext Ctx(
Importer.getFromContext(), Importer.getToContext(),
Importer.getNonEquivalentDecls(), getStructuralEquivalenceKind(Importer),
Importer.getToContext().getLangOpts(), Importer.getFromContext(),
Importer.getToContext(), Importer.getNonEquivalentDecls(),
getStructuralEquivalenceKind(Importer),
/*StrictTypeSpelling=*/false, Complain, /*ErrorOnTagTypeMismatch=*/false,
IgnoreTemplateParmDepth);
return Ctx.IsEquivalent(From, To);
Expand Down Expand Up @@ -4345,7 +4346,8 @@ static bool IsEquivalentFriend(ASTImporter &Importer, FriendDecl *FD1,

ASTImporter::NonEquivalentDeclSet NonEquivalentDecls;
StructuralEquivalenceContext Ctx(
FD1->getASTContext(), FD2->getASTContext(), NonEquivalentDecls,
Importer.getToContext().getLangOpts(), FD1->getASTContext(),
FD2->getASTContext(), NonEquivalentDecls,
StructuralEquivalenceKind::Default,
/* StrictTypeSpelling = */ false, /* Complain = */ false);
return Ctx.IsEquivalent(FD1, FD2);
Expand Down Expand Up @@ -10563,8 +10565,8 @@ bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
}
}

StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
getStructuralEquivalenceKind(*this), false,
Complain);
StructuralEquivalenceContext Ctx(
getToContext().getLangOpts(), FromContext, ToContext, NonEquivalentDecls,
getStructuralEquivalenceKind(*this), false, Complain);
return Ctx.IsEquivalent(From, To);
}
Loading
Loading