[clang] [AST] StructuralEquivalence: avoid diagnostics when Complain=false in CheckStructurallyEquivalentAttributes (PR #157585)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 8 18:04:06 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Stephen Young (StephenYoung2754)
<details>
<summary>Changes</summary>
# Summary
Avoid diagnostics in `CheckStructurallyEquivalentAttributes` when
`StructuralEquivalenceContext::Complain` is `false`.
# Details
The original code always emitted diagnostics on attribute mismatches:
```cpp
Context.Diag2(DiagnoseDecl->getLocation(),
diag::warn_odr_tag_type_with_attributes)
```
This caused an assertion failure in `Diag2` when `Complain == false`:
```cpp
assert(Complain && "Not allowed to complain");
```
The patch adds an early return in non-complaining mode:
```cpp
if (!Context.Complain)
return false;
```
This prevents diagnostics from being emitted when they are not allowed.
# Testing
- Existing structural equivalence tests continue to pass.
- In non-complaining mode, attribute mismatches no longer trigger assertions.
- No regressions observed in `check-clang`.
# Issue
Fixes #<!-- -->153916
---
Full diff: https://github.com/llvm/llvm-project/pull/157585.diff
1 Files Affected:
- (modified) clang/lib/AST/ASTStructuralEquivalence.cpp (+12-9)
``````````diff
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 1292c30d47589..94512488d0738 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -471,15 +471,18 @@ CheckStructurallyEquivalentAttributes(StructuralEquivalenceContext &Context,
if (D2->hasAttrs())
D2Attr = *D2->getAttrs().begin();
if ((D1Attr || D2Attr) && !D1->isImplicit() && !D2->isImplicit()) {
- const auto *DiagnoseDecl = cast<TypeDecl>(PrimaryDecl ? PrimaryDecl : D2);
- Context.Diag2(DiagnoseDecl->getLocation(),
- diag::warn_odr_tag_type_with_attributes)
- << Context.ToCtx.getTypeDeclType(DiagnoseDecl)
- << (PrimaryDecl != nullptr);
- if (D1Attr)
- Context.Diag1(D1Attr->getLoc(), diag::note_odr_attr_here) << D1Attr;
- if (D2Attr)
- Context.Diag1(D2Attr->getLoc(), diag::note_odr_attr_here) << D2Attr;
+ if (!Context.Complain)
+ return false;
+ const auto *DiagnoseDecl = cast<TypeDecl>(PrimaryDecl ? PrimaryDecl : D2);
+ Context.Diag2(DiagnoseDecl->getLocation(),
+ diag::warn_odr_tag_type_with_attributes)
+ << Context.ToCtx.getTypeDeclType(DiagnoseDecl)
+ << (PrimaryDecl != nullptr);
+ if (D1Attr)
+ Context.Diag1(D1Attr->getLoc(), diag::note_odr_attr_here) << D1Attr;
+ if (D2Attr)
+ Context.Diag2(D2Attr->getLoc(), diag::note_odr_attr_here) << D2Attr;
+ return false;
}
// The above diagnostic is a warning which defaults to an error. If treated
``````````
</details>
https://github.com/llvm/llvm-project/pull/157585
More information about the cfe-commits
mailing list