Author: Hunter Baker
Date: 2026-02-23T10:44:25Z
New Revision: 20bdcbd41db797be47d525525811fdbcf6c15870

URL: 
https://github.com/llvm/llvm-project/commit/20bdcbd41db797be47d525525811fdbcf6c15870
DIFF: 
https://github.com/llvm/llvm-project/commit/20bdcbd41db797be47d525525811fdbcf6c15870.diff

LOG: [APINotes] Fix fatal error when using the Type key on fields (#180672)

This fixes a clang crash when importing a module with apinotes that sets
the Type key on a Field:
```
# .---command stderr------------
# | API notes allowed a type on an unknown declaration
# | UNREACHABLE executed at 
<path>/llvm-project/clang/lib/Sema/SemaAPINotes.cpp:419!
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ 
and include the crash backtrace, preprocessed source, and associated run script.
# | Stack dump:
# | 0.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:4:12: 
current parser token ';'
# | 1.  <path>/llvm-project/clang/test/APINotes/Inputs/Headers/Fields.h:3:1: 
parsing struct/union/class body 'IntWrapper'
...
# |  #8 0x0000000109540868 clang::Sema::ApplyAPINotesType(clang::Decl*, 
llvm::StringRef)
# |  #9 0x000000010955c6ec applyAPINotesType(clang::Sema&, clang::Decl*, 
llvm::StringRef, (anonymous namespace)::VersionedInfoMetadata)
```
```yaml
Tags:
- Name: IntWrapper
  Fields:
  - Name: value
    Type: ValueType  # Fails because of this
```

Previously, the case that the Decl passed to `ApplyAPINotesType` could
be a `FieldDecl` was overlooked.
```cpp
struct Field {
  …
  StringRef Type;
  ...
}
...
template <> struct MappingTraits<Field> {
  static void mapping(IO &IO, Field &F) {
    …
    IO.mapOptional("Type", F.Type, StringRef(""));  // ‘Type' is already a 
valid key in apinotes files.
    ….
  }
}
```
This adds the case to handle `FieldDecl`s.

@egorzhdan @compnerd

Added: 
    

Modified: 
    clang/lib/Sema/SemaAPINotes.cpp
    clang/test/APINotes/Inputs/Headers/Fields.apinotes
    clang/test/APINotes/Inputs/Headers/Fields.h
    clang/test/APINotes/fields.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaAPINotes.cpp b/clang/lib/Sema/SemaAPINotes.cpp
index 059eb0dd767b7..12929895b3789 100644
--- a/clang/lib/Sema/SemaAPINotes.cpp
+++ b/clang/lib/Sema/SemaAPINotes.cpp
@@ -409,6 +409,12 @@ void Sema::ApplyAPINotesType(Decl *D, StringRef 
TypeString) {
                                           property->getType(), Type)) {
           property->setType(Type, TypeInfo);
         }
+      } else if (auto field = dyn_cast<FieldDecl>(D)) {
+        if (!checkAPINotesReplacementType(*this, field->getLocation(),
+                                          field->getType(), Type)) {
+          field->setType(Type);
+          field->setTypeSourceInfo(TypeInfo);
+        }
       } else {
         llvm_unreachable("API notes allowed a type on an unknown declaration");
       }

diff  --git a/clang/test/APINotes/Inputs/Headers/Fields.apinotes 
b/clang/test/APINotes/Inputs/Headers/Fields.apinotes
index 931da52ba29d1..44545ab8c7d27 100644
--- a/clang/test/APINotes/Inputs/Headers/Fields.apinotes
+++ b/clang/test/APINotes/Inputs/Headers/Fields.apinotes
@@ -4,6 +4,7 @@ Tags:
 - Name: IntWrapper
   Fields:
   - Name: value
+    Type: ValueType
     Availability: none
     AvailabilityMsg: "oh no"
 - Name: Outer

diff  --git a/clang/test/APINotes/Inputs/Headers/Fields.h 
b/clang/test/APINotes/Inputs/Headers/Fields.h
index dbd342da47789..cc2bc5814c00a 100644
--- a/clang/test/APINotes/Inputs/Headers/Fields.h
+++ b/clang/test/APINotes/Inputs/Headers/Fields.h
@@ -1,3 +1,5 @@
+enum class ValueType {};
+
 struct IntWrapper {
   int value;
 

diff  --git a/clang/test/APINotes/fields.cpp b/clang/test/APINotes/fields.cpp
index 8dea2229b4e0a..1522f098634cd 100644
--- a/clang/test/APINotes/fields.cpp
+++ b/clang/test/APINotes/fields.cpp
@@ -6,7 +6,7 @@
 #include "Fields.h"
 
 // CHECK-FIELD: Dumping IntWrapper::value:
-// CHECK-FIELD-NEXT: FieldDecl {{.+}} value
+// CHECK-FIELD-NEXT: FieldDecl {{.+}} value 'ValueType'
 // CHECK-FIELD: UnavailableAttr {{.+}} <<invalid sloc>> "oh no"
 
 // CHECK-DEEP-FIELD: Dumping Outer::Inner::value:


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to