[clang] [Clang] Add __builtin_counted_by_ref builtin (PR #114495)
@@ -13,7 +13,10 @@ void init(void * __attribute__((pass_dynamic_object_size(0; // CHECK-LABEL: define dso_local void @_ZN3foo3barC1Ev( // CHECK-SAME: ptr noundef nonnull align 4 dereferenceable(1) [[THIS:%.*]]) unnamed_addr #[[ATTR0:[0-9]+]] align 2 { // CHECK-NEXT: entry: -// CHECK-NEXT:tail call void @_Z4initPvU25pass_dynamic_object_size0(ptr noundef nonnull align 4 dereferenceable(1) [[THIS]], i64 noundef -1) #[[ATTR2:[0-9]+]] rapidsna wrote: I also don't see why the other tests need to be updated here, except if you have to remove test cases that no longer works with new restrictions. Still, the rest of the tests and file checks should stay the same. https://github.com/llvm/llvm-project/pull/114495 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __builtin_counted_by_ref builtin (PR #114495)
@@ -16,6 +16,8 @@ void test1(struct fam_struct *ptr, int size, int idx) { *__builtin_counted_by_ref(ptr->array) = size; // ok *__builtin_counted_by_ref(&ptr->array[idx]) = size; // ok + *__builtin_counted_by_ref(&ptr->array) = size;// ok rapidsna wrote: I guess this will just work too? `*__builtin_counted_by_ref(ptr->array[idx]) = size;` Do you have a use case? I'd prefer these being ill-formed because the user might find it working unexpectedly. But I don't have a strong opinion on this. I'd leave it up to you/Linux users if you find this would be useful. https://github.com/llvm/llvm-project/pull/114495 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __builtin_counted_by_ref builtin (PR #114495)
@@ -0,0 +1,182 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -O2 -emit-llvm -o - %s | FileCheck %s --check-prefix=X86_64 +// RUN: %clang_cc1 -triple i386-unknown-unknown -O2 -emit-llvm -o - %s | FileCheck %s --check-prefix=I386 + +struct a { + char x; + short count; + int array[] __attribute__((counted_by(count))); +}; + +// X86_64-LABEL: define dso_local noalias noundef ptr @test1( +// X86_64-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// X86_64-NEXT: [[ENTRY:.*:]] +// X86_64-NEXT:[[CONV:%.*]] = sext i32 [[SIZE]] to i64 +// X86_64-NEXT:[[MUL:%.*]] = shl nsw i64 [[CONV]], 2 +// X86_64-NEXT:[[ADD:%.*]] = add nsw i64 [[MUL]], 4 +// X86_64-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i64 noundef [[ADD]]) #[[ATTR3:[0-9]+]] +// X86_64-NEXT:[[CONV1:%.*]] = trunc i32 [[SIZE]] to i16 +// X86_64-NEXT:[[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[CALL]], i64 2 +// X86_64-NEXT:store i16 [[CONV1]], ptr [[DOT_COUNTED_BY_GEP]], align 2, !tbaa [[TBAA2:![0-9]+]] +// X86_64-NEXT:ret ptr [[CALL]] +// +// I386-LABEL: define dso_local noalias noundef ptr @test1( +// I386-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// I386-NEXT: [[ENTRY:.*:]] +// I386-NEXT:[[MUL:%.*]] = shl i32 [[SIZE]], 2 +// I386-NEXT:[[ADD:%.*]] = add i32 [[MUL]], 4 +// I386-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i32 noundef [[ADD]]) #[[ATTR3:[0-9]+]] +// I386-NEXT:[[CONV:%.*]] = trunc i32 [[SIZE]] to i16 +// I386-NEXT:[[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[CALL]], i32 2 +// I386-NEXT:store i16 [[CONV]], ptr [[DOT_COUNTED_BY_GEP]], align 2, !tbaa [[TBAA3:![0-9]+]] +// I386-NEXT:ret ptr [[CALL]] +// +struct a *test1(int size) { + struct a *p = __builtin_malloc(sizeof(struct a) + sizeof(int) * size); + + *__builtin_counted_by_ref(p->array) = size; + *__builtin_counted_by_ref(&p->array[0]) = size; + return p; +} + +struct b { + int _filler; + struct { +int __filler; +struct { + int ___filler; + struct { +char count; + }; +}; + }; + struct { +int filler_; +struct { + int filler__; + struct { +long array[] __attribute__((counted_by(count))); + }; +}; + }; +}; + +// X86_64-LABEL: define dso_local noalias noundef ptr @test2( +// X86_64-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR0]] { +// X86_64-NEXT: [[ENTRY:.*:]] +// X86_64-NEXT:[[CONV:%.*]] = sext i32 [[SIZE]] to i64 +// X86_64-NEXT:[[MUL:%.*]] = shl nsw i64 [[CONV]], 2 +// X86_64-NEXT:[[ADD:%.*]] = add nsw i64 [[MUL]], 4 +// X86_64-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i64 noundef [[ADD]]) #[[ATTR3]] +// X86_64-NEXT:[[CONV1:%.*]] = trunc i32 [[SIZE]] to i8 +// X86_64-NEXT:[[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[CALL]], i64 12 +// X86_64-NEXT:store i8 [[CONV1]], ptr [[DOT_COUNTED_BY_GEP]], align 1, !tbaa [[TBAA6:![0-9]+]] +// X86_64-NEXT:ret ptr [[CALL]] +// +// I386-LABEL: define dso_local noalias noundef ptr @test2( +// I386-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR0]] { +// I386-NEXT: [[ENTRY:.*:]] +// I386-NEXT:[[MUL:%.*]] = shl i32 [[SIZE]], 2 +// I386-NEXT:[[ADD:%.*]] = add i32 [[MUL]], 4 +// I386-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i32 noundef [[ADD]]) #[[ATTR3]] +// I386-NEXT:[[CONV:%.*]] = trunc i32 [[SIZE]] to i8 +// I386-NEXT:[[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[CALL]], i32 12 +// I386-NEXT:store i8 [[CONV]], ptr [[DOT_COUNTED_BY_GEP]], align 1, !tbaa [[TBAA7:![0-9]+]] +// I386-NEXT:ret ptr [[CALL]] +// +struct b *test2(int size) { + struct b *p = __builtin_malloc(sizeof(struct a) + sizeof(int) * size); + + *__builtin_counted_by_ref(p->array) = size; + *__builtin_counted_by_ref(&p->array[0]) = size; + return p; +} + +struct c { + char x; + short count; + int array[]; +}; + +// X86_64-LABEL: define dso_local noalias noundef ptr @test3( +// X86_64-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR2:[0-9]+]] { +// X86_64-NEXT: [[ENTRY:.*:]] +// X86_64-NEXT:[[CONV:%.*]] = sext i32 [[SIZE]] to i64 +// X86_64-NEXT:[[MUL:%.*]] = shl nsw i64 [[CONV]], 2 +// X86_64-NEXT:[[ADD:%.*]] = add nsw i64 [[MUL]], 4 +// X86_64-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i64 noundef [[ADD]]) #[[ATTR3]] +// X86_64-NEXT:ret ptr [[CALL]] +// +// I386-LABEL: define dso_local noalias noundef ptr @test3( +// I386-SAME: i32 noundef [[SIZE:%.*]]) local_unnamed_addr #[[ATTR2:[0-9]+]] { +// I386-NEXT: [[ENTRY:.*:]] +// I386-NEXT:[[MUL:%.*]] = shl i32 [[SIZE]], 2 +// I386-NEXT:[[ADD:%.*]] = add i32 [[MUL]], 4 +// I386-NEXT:[[CALL:%.*]] = tail call ptr @malloc(i32 noundef [[ADD]]) #[[ATTR3]] +// I386-NEXT:ret ptr [[CALL]] +// +struct c *test3(int size) { +
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -1105,16 +1120,16 @@ enum AttributeDeclKind { inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, const ParsedAttr &At) { - DB.AddTaggedVal(reinterpret_cast(At.getAttrName()), + const IdentifierInfo *AttrName = + At.printAsMacro() ? At.getMacroIdentifier() : At.getAttrName(); + DB.AddTaggedVal(reinterpret_cast(AttrName), rapidsna wrote: How about always printing the macro name (if exists) and printing "(aka 'foo')" only when it's not system-defined macro? https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/107619 >From 1d00f0fca700c058320a39cd3f40cdd58c37cf8f Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 5 Sep 2024 09:52:41 -0700 Subject: [PATCH 1/3] [Parser][BoundsSafety] Print attribute as macro if it's system defined This correctly adds macro identifier for late parsed attrs, which has been missing. 'operator<<' for StreamingDiagnostic and ParsedAttr checks if Attr should be printed as macro and does it. This also fixes the bug that the macro identifier included the parenthesis and arguments. --- clang/include/clang/Sema/ParsedAttr.h | 39 +-- clang/lib/Parse/ParseCXXInlineMethods.cpp | 11 ++ clang/lib/Parse/ParseDecl.cpp | 18 - .../test/Frontend/Inputs/macro_defined_type.h | 4 ++ clang/test/Frontend/macro_defined_type.cpp| 18 - .../Inputs/warn-thread-safety-parsing.h | 3 ++ .../SemaCXX/warn-thread-safety-parsing.cpp| 21 -- 7 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 clang/test/Frontend/Inputs/macro_defined_type.h create mode 100644 clang/test/SemaCXX/Inputs/warn-thread-safety-parsing.h diff --git a/clang/include/clang/Sema/ParsedAttr.h b/clang/include/clang/Sema/ParsedAttr.h index 22cbd0d90ee432..29ddfada0f9371 100644 --- a/clang/include/clang/Sema/ParsedAttr.h +++ b/clang/include/clang/Sema/ParsedAttr.h @@ -191,6 +191,10 @@ class ParsedAttr final LLVM_PREFERRED_TYPE(bool) mutable unsigned IsPragmaClangAttribute : 1; + /// Determines if the attribute will be printed as macro in the diagnostics. + LLVM_PREFERRED_TYPE(bool) + mutable unsigned PrintAsMacro : 1; + /// The location of the 'unavailable' keyword in an /// availability attribute. SourceLocation UnavailableLoc; @@ -225,7 +229,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion)); } @@ -242,8 +246,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -UnavailableLoc(unavailable), MessageExpr(messageExpr), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), UnavailableLoc(unavailable), +MessageExpr(messageExpr), Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(Parm); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); new (getAvailabilityData()) @@ -260,7 +264,8 @@ class ParsedAttr final NumArgs(3), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion *Args = getArgsBuffer(); Args[0] = Parm1; Args[1] = Parm2; @@ -276,7 +281,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(ArgKind); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot(); @@ -294,7 +300,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(true), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { new (&getTypeBuffer()) ParsedType(typeArg); } @@ -306,7 +312,8 @@ class ParsedAttr final NumArgs(0), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId); } @@ -493,9 +500,11 @@ class ParsedAttr final /// Set the macro identifier info object tha
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -1105,16 +1120,16 @@ enum AttributeDeclKind { inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, const ParsedAttr &At) { - DB.AddTaggedVal(reinterpret_cast(At.getAttrName()), + const IdentifierInfo *AttrName = + At.printAsMacro() ? At.getMacroIdentifier() : At.getAttrName(); + DB.AddTaggedVal(reinterpret_cast(AttrName), rapidsna wrote: @AaronBallman @erichkeane Thanks for the valuable feedback! Our motivation was that with `-fbounds-safety`, we would have a toolchain header defines this: `#define __counted_by(c) __attribute__((__counted_by(c)__))` then the user code uses it as `__counted_by` spelling, we'd like it to be just printed as `__counted_by` and we don't want it to be printed as `__counted_by__` nor having a redundant aka (`__counted_by__` ) next to `__counted_by`. Also, that was why we were limiting this change specifically to "system-defined" macros. Does it make sense? I guess there might be a better way to implement this behavior in Clang? https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -5055,6 +5058,17 @@ void Parser::ParseLexedCAttribute(LateParsedAttribute &LA, bool EnterScope, ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr, nullptr, SourceLocation(), ParsedAttr::Form::GNU(), nullptr); + const auto &SM = PP.getSourceManager(); + CharSourceRange ExpansionRange = SM.getExpansionRange(LA.AttrNameLoc); + StringRef FoundName = + Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts()) + .split('(') + .first; + IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName); + for (unsigned i = 0; i < Attrs.size(); ++i) +Attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin(), rapidsna wrote: > when multiple are defined by the same one? Do you have a specific example in mind? > Also, I find myself wondering if we should just use MacroIdentifier != > nullptr instead of this? We could, I just wanted to make sure a system-defined macro is printed as macro, which likely provides a meaningful name, then user-defined macros. And doing so has much less impact on the existing regression tests. https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -1105,16 +1120,16 @@ enum AttributeDeclKind { inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, const ParsedAttr &At) { - DB.AddTaggedVal(reinterpret_cast(At.getAttrName()), + const IdentifierInfo *AttrName = + At.printAsMacro() ? At.getMacroIdentifier() : At.getAttrName(); + DB.AddTaggedVal(reinterpret_cast(AttrName), rapidsna wrote: @erichkeane Currently, a note is emitted to show the line where it's expanded from that includes the original attribute spelling: https://github.com/llvm/llvm-project/pull/107619/files#diff-3d962c5c4f372776e40887d273f9404efe3cd306f9915505d8566b791021914bR49 Not sure how I can suppress the follow-on note that lists macro expansions, but I can try the `ak_attr` approach as @AaronBallman suggested. https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -5080,6 +5083,17 @@ void Parser::ParseLexedCAttribute(LateParsedAttribute &LA, bool EnterScope, ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr, nullptr, SourceLocation(), ParsedAttr::Form::GNU(), nullptr); + const auto &SM = PP.getSourceManager(); + CharSourceRange ExpansionRange = SM.getExpansionRange(LA.AttrNameLoc); + StringRef FoundName = + Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts()) + .split('(') + .first; + IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName); rapidsna wrote: Oops, yes, I missed the condition check here. Good catch! https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -14,12 +16,26 @@ void Func() { auto NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} } +// The diagnostic message is hard-coded as 'noderef' so using a system macro doesn't change the behavior +void Func_system_macro() { + int _SYS_NODEREF i; // expected-warning{{'noderef' can only be used on an array or pointer type}} + int _SYS_NODEREF *i_ptr; + + auto _SYS_NODEREF *auto_i_ptr2 = i_ptr; + auto _SYS_NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} +} + + // Added test for fix for P41835 #define _LIBCPP_FLOAT_ABI __attribute__((pcs("aapcs"))) struct A { _LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'pcs' calling convention is not supported for this target}} }; +struct A_system_macro { + _SYS_LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'_SYS_LIBCPP_FLOAT_ABI' calling convention is not supported for this target}} rapidsna wrote: > if we're always pointing the caret at the attribute itself so we get an > 'expanded from macro' note, that seems fine. @zygoloid I just added some file checks here (411e3435873bad900e84309d90fe13f6085b3a1a) to verify that. https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/107619 >From 1d00f0fca700c058320a39cd3f40cdd58c37cf8f Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 5 Sep 2024 09:52:41 -0700 Subject: [PATCH 1/2] [Parser][BoundsSafety] Print attribute as macro if it's system defined This correctly adds macro identifier for late parsed attrs, which has been missing. 'operator<<' for StreamingDiagnostic and ParsedAttr checks if Attr should be printed as macro and does it. This also fixes the bug that the macro identifier included the parenthesis and arguments. --- clang/include/clang/Sema/ParsedAttr.h | 39 +-- clang/lib/Parse/ParseCXXInlineMethods.cpp | 11 ++ clang/lib/Parse/ParseDecl.cpp | 18 - .../test/Frontend/Inputs/macro_defined_type.h | 4 ++ clang/test/Frontend/macro_defined_type.cpp| 18 - .../Inputs/warn-thread-safety-parsing.h | 3 ++ .../SemaCXX/warn-thread-safety-parsing.cpp| 21 -- 7 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 clang/test/Frontend/Inputs/macro_defined_type.h create mode 100644 clang/test/SemaCXX/Inputs/warn-thread-safety-parsing.h diff --git a/clang/include/clang/Sema/ParsedAttr.h b/clang/include/clang/Sema/ParsedAttr.h index 22cbd0d90ee432..29ddfada0f9371 100644 --- a/clang/include/clang/Sema/ParsedAttr.h +++ b/clang/include/clang/Sema/ParsedAttr.h @@ -191,6 +191,10 @@ class ParsedAttr final LLVM_PREFERRED_TYPE(bool) mutable unsigned IsPragmaClangAttribute : 1; + /// Determines if the attribute will be printed as macro in the diagnostics. + LLVM_PREFERRED_TYPE(bool) + mutable unsigned PrintAsMacro : 1; + /// The location of the 'unavailable' keyword in an /// availability attribute. SourceLocation UnavailableLoc; @@ -225,7 +229,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion)); } @@ -242,8 +246,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -UnavailableLoc(unavailable), MessageExpr(messageExpr), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), UnavailableLoc(unavailable), +MessageExpr(messageExpr), Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(Parm); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); new (getAvailabilityData()) @@ -260,7 +264,8 @@ class ParsedAttr final NumArgs(3), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion *Args = getArgsBuffer(); Args[0] = Parm1; Args[1] = Parm2; @@ -276,7 +281,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(ArgKind); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot(); @@ -294,7 +300,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(true), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { new (&getTypeBuffer()) ParsedType(typeArg); } @@ -306,7 +312,8 @@ class ParsedAttr final NumArgs(0), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId); } @@ -493,9 +500,11 @@ class ParsedAttr final /// Set the macro identifier info object tha
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -14,12 +16,26 @@ void Func() { auto NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} } +// The diagnostic message is hard-coded as 'noderef' so using a system macro doesn't change the behavior +void Func_system_macro() { + int _SYS_NODEREF i; // expected-warning{{'noderef' can only be used on an array or pointer type}} + int _SYS_NODEREF *i_ptr; + + auto _SYS_NODEREF *auto_i_ptr2 = i_ptr; + auto _SYS_NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} +} + + // Added test for fix for P41835 #define _LIBCPP_FLOAT_ABI __attribute__((pcs("aapcs"))) struct A { _LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'pcs' calling convention is not supported for this target}} }; +struct A_system_macro { + _SYS_LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'_SYS_LIBCPP_FLOAT_ABI' calling convention is not supported for this target}} rapidsna wrote: Fwiw, my test may not be a good example. Since this is only for system-defined macro, so I'd expect the macro name should more helpful in real use cases. https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
@@ -14,12 +16,26 @@ void Func() { auto NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} } +// The diagnostic message is hard-coded as 'noderef' so using a system macro doesn't change the behavior +void Func_system_macro() { + int _SYS_NODEREF i; // expected-warning{{'noderef' can only be used on an array or pointer type}} + int _SYS_NODEREF *i_ptr; + + auto _SYS_NODEREF *auto_i_ptr2 = i_ptr; + auto _SYS_NODEREF auto_i2 = i; // expected-warning{{'noderef' can only be used on an array or pointer type}} +} + + // Added test for fix for P41835 #define _LIBCPP_FLOAT_ABI __attribute__((pcs("aapcs"))) struct A { _LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'pcs' calling convention is not supported for this target}} }; +struct A_system_macro { + _SYS_LIBCPP_FLOAT_ABI int operator()() throw(); // expected-warning{{'_SYS_LIBCPP_FLOAT_ABI' calling convention is not supported for this target}} rapidsna wrote: @zygoloid Thanks for the comment! Right, the compiler actually does emit "note" like below, which I forgot to add in the test. I will add it. Would it address your concern? ``` clang/test/Frontend/Inputs/macro_defined_type.h:4:46: note: expanded from macro '_SYS_LIBCPP_FLOAT_ABI' 4 | #define _SYS_LIBCPP_FLOAT_ABI __attribute__((pcs("aapcs"))) | ^ ``` https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/107619 >From 1a0e2dede95bd69ab6badc9aafbd60d5eaf5d407 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 5 Sep 2024 09:52:41 -0700 Subject: [PATCH] [Parser][BoundsSafety] Print attribute as macro if it's system defined This correctly adds macro identifier for late parsed attrs, which has been missing. 'operator<<' for StreamingDiagnostic and ParsedAttr checks if Attr should be printed as macro and does it. This also fixes the bug that the macro identifier included the parenthesis and arguments. --- clang/include/clang/Sema/ParsedAttr.h | 39 +-- clang/lib/Parse/ParseCXXInlineMethods.cpp | 11 ++ clang/lib/Parse/ParseDecl.cpp | 18 - .../test/Frontend/Inputs/macro_defined_type.h | 4 ++ clang/test/Frontend/macro_defined_type.cpp| 18 - .../Inputs/warn-thread-safety-parsing.h | 3 ++ .../SemaCXX/warn-thread-safety-parsing.cpp| 21 -- 7 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 clang/test/Frontend/Inputs/macro_defined_type.h create mode 100644 clang/test/SemaCXX/Inputs/warn-thread-safety-parsing.h diff --git a/clang/include/clang/Sema/ParsedAttr.h b/clang/include/clang/Sema/ParsedAttr.h index 22cbd0d90ee432..29ddfada0f9371 100644 --- a/clang/include/clang/Sema/ParsedAttr.h +++ b/clang/include/clang/Sema/ParsedAttr.h @@ -191,6 +191,10 @@ class ParsedAttr final LLVM_PREFERRED_TYPE(bool) mutable unsigned IsPragmaClangAttribute : 1; + /// Determines if the attribute will be printed as macro in the diagnostics. + LLVM_PREFERRED_TYPE(bool) + mutable unsigned PrintAsMacro : 1; + /// The location of the 'unavailable' keyword in an /// availability attribute. SourceLocation UnavailableLoc; @@ -225,7 +229,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion)); } @@ -242,8 +246,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -UnavailableLoc(unavailable), MessageExpr(messageExpr), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), UnavailableLoc(unavailable), +MessageExpr(messageExpr), Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(Parm); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); new (getAvailabilityData()) @@ -260,7 +264,8 @@ class ParsedAttr final NumArgs(3), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion *Args = getArgsBuffer(); Args[0] = Parm1; Args[1] = Parm2; @@ -276,7 +281,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(ArgKind); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot(); @@ -294,7 +300,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(true), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { new (&getTypeBuffer()) ParsedType(typeArg); } @@ -306,7 +312,8 @@ class ParsedAttr final NumArgs(0), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId); } @@ -493,9 +500,11 @@ class ParsedAttr final /// Set the macro identifier info object that th
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
rapidsna wrote: Will fix several test failures. https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/107619 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Parser][BoundsSafety] Print attribute as macro if it's system defined (PR #107619)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/107619 This correctly adds macro identifier for late parsed attrs, which has been missing. 'operator<<' for StreamingDiagnostic and ParsedAttr checks if Attr should be printed as macro and does it. This also fixes the bug that the macro identifier included the parenthesis and arguments. >From 7db45bc95556c8fe01067c6a7c94728f37ab38de Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 5 Sep 2024 09:52:41 -0700 Subject: [PATCH] [Parser][BoundsSafety] Print attribute as macro if it's system defined This correctly adds macro identifier for late parsed attrs, which has been missing. 'operator<<' for StreamingDiagnostic and ParsedAttr checks if Attr should be printed as macro and does it. This also fixes the bug that the macro identifier included the parenthesis and arguments. --- clang/include/clang/Sema/ParsedAttr.h | 39 +-- clang/lib/Parse/ParseCXXInlineMethods.cpp | 11 ++ clang/lib/Parse/ParseDecl.cpp | 18 - clang/test/Frontend/macro_defined_type.cpp| 18 - .../SemaCXX/warn-thread-safety-parsing.cpp| 21 -- 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/clang/include/clang/Sema/ParsedAttr.h b/clang/include/clang/Sema/ParsedAttr.h index 22cbd0d90ee432..29ddfada0f9371 100644 --- a/clang/include/clang/Sema/ParsedAttr.h +++ b/clang/include/clang/Sema/ParsedAttr.h @@ -191,6 +191,10 @@ class ParsedAttr final LLVM_PREFERRED_TYPE(bool) mutable unsigned IsPragmaClangAttribute : 1; + /// Determines if the attribute will be printed as macro in the diagnostics. + LLVM_PREFERRED_TYPE(bool) + mutable unsigned PrintAsMacro : 1; + /// The location of the 'unavailable' keyword in an /// availability attribute. SourceLocation UnavailableLoc; @@ -225,7 +229,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { if (numArgs) memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion)); } @@ -242,8 +246,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), IsPragmaClangAttribute(false), -UnavailableLoc(unavailable), MessageExpr(messageExpr), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), UnavailableLoc(unavailable), +MessageExpr(messageExpr), Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(Parm); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); new (getAvailabilityData()) @@ -260,7 +264,8 @@ class ParsedAttr final NumArgs(3), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion *Args = getArgsBuffer(); Args[0] = Parm1; Args[1] = Parm2; @@ -276,7 +281,8 @@ class ParsedAttr final NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { ArgsUnion PVal(ArgKind); memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot(); @@ -294,7 +300,7 @@ class ParsedAttr final UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(true), HasProcessingCache(false), IsPragmaClangAttribute(false), -Info(ParsedAttrInfo::get(*this)) { +PrintAsMacro(false), Info(ParsedAttrInfo::get(*this)) { new (&getTypeBuffer()) ParsedType(typeArg); } @@ -306,7 +312,8 @@ class ParsedAttr final NumArgs(0), Invalid(false), UsedAsTypeAttr(false), IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true), HasParsedType(false), HasProcessingCache(false), -IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { +IsPragmaClangAttribute(false), PrintAsMacro(false), +Info(ParsedAttrInfo::get(*this)) { new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId); } @@ -493,9 +500,11 @@ class ParsedAttr final /// Set the mac
[clang] [Driver][BoundsSafety] Add -fexperimental-bounds-safety flag (PR #70480)
rapidsna wrote: > Also, I think there was a PR that added delayed parsing of attributes? Is > that correct, or a false memory? :-) @bwendling That too has already been merged in https://github.com/llvm/llvm-project/pull/88596 https://github.com/llvm/llvm-project/pull/70480 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Driver][BoundsSafety] Add -fexperimental-bounds-safety flag (PR #70480)
rapidsna wrote: @bwendling We've already landed the flag in https://github.com/llvm/llvm-project/pull/92623. I will close this one. https://github.com/llvm/llvm-project/pull/70480 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Driver][BoundsSafety] Add -fexperimental-bounds-safety flag (PR #70480)
https://github.com/rapidsna closed https://github.com/llvm/llvm-project/pull/70480 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][NFC] Specify taking address of a variable referred to by '__counted_by' is forbidden (PR #106147)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/106147 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Specify taking address of a variable referred to by '__counted_by' is forbidden (PR #106147)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/106147 `-fbounds-safety` doesn't allow taking address of a variable referred to by `__counted_by`, in order to prevent code from using the pointer to update the variable without necessary checks to keep the invariant of `__counted_by`. As shown in the example below, with -fbounds-safety the compiler ensures that the `__counted_by` pointer/or array has at least as many as elements that the attribute indicates, by requiring the count and the buf are always updated side by side and emitting run-time checks to ensure the new values are valid. ``` struct counted_buf { sized_t count; int *__counted_by(count) buf; }; // BEFORE FIX void foo(struct counted_buf *p) { p->count = 10; // error: assignment to 'count' requires corresponding assignment to 'int *__counted_by(count) buf') } // FIXED by adding assignment to `p->buf` void foo(struct counted_buf *p) { // The compiler is now happy because p->buf = (int *)malloc(sizeof(int) *5); // run-time checks are emitted to make sure new buf has at least `10` elements. p->count = 10; } ``` Consequently, `-fbounds-safety` prevents taking address of a variable referred to by `__counted_by`, because otherwise, the compiler cannot check the updates through the pointer pointing to the count: ``` void foo(struct counted_buf *p) { int *count_p = &p->count; // error: variable referred to by '__counted_by' cannot be pointed to by any other variable *count_p = 10; // without the above error, `count_p` is a normal `int *` so the compiler cannot check the value it updates against `__counted_by` } ``` This PR is to explicitly specify this restriction and avoid future conflicts. >From 8ed704c9e85b917d526df1e468e325a302f5a4d2 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 26 Aug 2024 14:17:59 -0700 Subject: [PATCH] Specify taking address of a variable referred to by '__counted_by' is forbidden --- clang/docs/BoundsSafety.rst | 19 ++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/docs/BoundsSafety.rst b/clang/docs/BoundsSafety.rst index 8fd655663edb00..e4ddd3c62db65d 100644 --- a/clang/docs/BoundsSafety.rst +++ b/clang/docs/BoundsSafety.rst @@ -759,7 +759,24 @@ relationship must hold even after any of these related variables are updated. To this end, the model requires that assignments to ``buf`` and ``count`` must be side by side, with no side effects between them. This prevents ``buf`` and ``count`` from temporarily falling out of sync due to updates happening at a -distance. +distance. In addition, taking address of ``count`` is not allowed in order to +prevent the programmers from updating the ``count`` through the pointer, which +will evade the necessary checks to make ``count`` and ``buf`` in sync. + +.. code-block:: c + + struct counted_buf { + int *__counted_by(count) buf; + size_t count; + }; + + void foo(struct counted_buf *p) { + int *pointer_to_count = &p->count; // error: variable referred to by + // '__counted_by' cannot be pointed to by any other variable; exception is + // when the pointer is passed as a compatible argument to a function. + *pointer_to_count = SIZE_MAX; // Without reporting the error above, the + // compiler cannot prevent count from getting an invalid value. + } The example below shows a function ``alloc_buf`` that initializes a struct that members that use the ``__counted_by`` annotation. The compiler allows these ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][NFC] Remove the unused parameter 'Decls' from 'Sema::C… (PR #102076)
https://github.com/rapidsna closed https://github.com/llvm/llvm-project/pull/102076 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][NFC] Remove the unused parameter 'Decls' from 'Sema::C… (PR #102076)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/102076 …heckCountedByAttrOnField' llvm::SmallVectorImpl &Decls is a vector of declarations referred to by the argument of 'counted_by' attributes and frields. Since 'BuildCountAttributedArrayOrPointerType' has been made self-contained to produce the 'Decls' within itself to allow 'TreeTransform' to invoke the functinon without having to call 'Sema::CheckCountedByAttrOnField' again. >From 31ebb67f8f29ee22ee018836b9f023ff69a9ee80 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 5 Aug 2024 15:37:35 -0700 Subject: [PATCH] [BoundsSafety][NFC] Remove the unused parameter 'Decls' from 'Sema::CheckCountedByAttrOnField' llvm::SmallVectorImpl &Decls is a vector of declarations referred to by the argument of 'counted_by' attributes and frields. Since 'BuildCountAttributedArrayOrPointerType' has been made self-contained to produce the 'Decls' within itself to allow 'TreeTransform' to invoke the functinon without having to call 'Sema::CheckCountedByAttrOnField' again. --- clang/include/clang/Sema/Sema.h | 6 ++ clang/lib/Sema/SemaBoundsSafety.cpp | 8 ++-- clang/lib/Sema/SemaDeclAttr.cpp | 3 +-- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 2ec6367eccea0..d00b3511059f7 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -15086,10 +15086,8 @@ class Sema final : public SemaBase { /// `counted_by_or_null` attribute. /// /// \returns false iff semantically valid. - bool CheckCountedByAttrOnField( - FieldDecl *FD, Expr *E, - llvm::SmallVectorImpl &Decls, bool CountInBytes, - bool OrNull); + bool CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, + bool OrNull); ///@} }; diff --git a/clang/lib/Sema/SemaBoundsSafety.cpp b/clang/lib/Sema/SemaBoundsSafety.cpp index 290c820938898..d63a2389ea11d 100644 --- a/clang/lib/Sema/SemaBoundsSafety.cpp +++ b/clang/lib/Sema/SemaBoundsSafety.cpp @@ -48,10 +48,8 @@ enum class CountedByInvalidPointeeTypeKind { VALID, }; -bool Sema::CheckCountedByAttrOnField( -FieldDecl *FD, Expr *E, -llvm::SmallVectorImpl &Decls, bool CountInBytes, -bool OrNull) { +bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, + bool OrNull) { // Check the context the attribute is used in unsigned Kind = getCountAttrKind(CountInBytes, OrNull); @@ -185,8 +183,6 @@ bool Sema::CheckCountedByAttrOnField( return true; } } - - Decls.push_back(TypeCoupledDeclRefInfo(CountFD, /*IsDref*/ false)); return false; } diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 9011fa547638e..bcb1424825df0 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5898,8 +5898,7 @@ static void handleCountedByAttrField(Sema &S, Decl *D, const ParsedAttr &AL) { llvm_unreachable("unexpected counted_by family attribute"); } - llvm::SmallVector Decls; - if (S.CheckCountedByAttrOnField(FD, CountExpr, Decls, CountInBytes, OrNull)) + if (S.CheckCountedByAttrOnField(FD, CountExpr, CountInBytes, OrNull)) return; QualType CAT = S.BuildCountAttributedArrayOrPointerType( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][NFC] Move Bounds Safety Sema code to `SemaBoundsSafety.cpp` (PR #99330)
https://github.com/rapidsna approved this pull request. https://github.com/llvm/llvm-project/pull/99330 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Remove unnecessary copy (PR #97902)
https://github.com/rapidsna approved this pull request. Thanks for fixing this issue! I left a minor comment. Other than that, LGTM. https://github.com/llvm/llvm-project/pull/97902 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Remove unnecessary copy (PR #97902)
@@ -1551,7 +1551,7 @@ ASTNodeImporter::VisitCountAttributedType(const CountAttributedType *T) { Expr *CountExpr = importChecked(Err, T->getCountExpr()); SmallVector CoupledDecls; - for (auto TI : T->dependent_decls()) { + for (const TypeCoupledDeclRefInfo &TI : T->dependent_decls()) { rapidsna wrote: Was there a reason to suggest it back to `auto` from `TypeCoupledDeclRefInfo`? I thought Clang prefers to have an explicit type name when the deduced type is not explicit from RHS. https://github.com/llvm/llvm-project/pull/97902 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support `guarded_by` attribute and related attributes inside C structs and support late parsing them (PR #95455)
https://github.com/rapidsna approved this pull request. LGTM. Let me know if you need me to merge this change. https://github.com/llvm/llvm-project/pull/95455 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Bounds-Safety] Add sized_by, counted_by_or_null & sized_by_or_null (PR #93231)
rapidsna wrote: Still LGTM https://github.com/llvm/llvm-project/pull/93231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
https://github.com/rapidsna closed https://github.com/llvm/llvm-project/pull/95964 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
rapidsna wrote: @AaronBallman Thanks for your prompt feedback! https://github.com/llvm/llvm-project/pull/95964 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/95964 >From 6ed8ac33952bc8f9a9b672740d45f25872ae1dbb Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Tue, 18 Jun 2024 11:05:17 -0700 Subject: [PATCH 1/2] [BoundsSafety][doc] Make it clear that the feature is work-in-progress --- clang/docs/BoundsSafety.rst | 7 +-- clang/docs/BoundsSafetyImplPlans.rst | 65 ++-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/clang/docs/BoundsSafety.rst b/clang/docs/BoundsSafety.rst index f1837675ec9bf..bd891733b7754 100644 --- a/clang/docs/BoundsSafety.rst +++ b/clang/docs/BoundsSafety.rst @@ -8,6 +8,9 @@ Overview +**NOTE:** This is a design document and the feature is not available for users yet. +Please find :doc:`BoundsSafetyImplPlans` for more details. + ``-fbounds-safety`` is a C extension to enforce bounds safety to prevent out-of-bounds (OOB) memory accesses, which remain a major source of security vulnerabilities in C. ``-fbounds-safety`` aims to eliminate this class of bugs @@ -55,9 +58,7 @@ adopt, offering these properties that make it widely adoptable in practice: * It has a relatively low adoption cost. This document discusses the key designs of ``-fbounds-safety``. The document is -subject to be actively updated with a more detailed specification. The -implementation plan can be found in :doc:`BoundsSafetyImplPlans`. - +subject to be actively updated with a more detailed specification. Programming Model = diff --git a/clang/docs/BoundsSafetyImplPlans.rst b/clang/docs/BoundsSafetyImplPlans.rst index 4fbf87f966350..93c2ed7b43402 100644 --- a/clang/docs/BoundsSafetyImplPlans.rst +++ b/clang/docs/BoundsSafetyImplPlans.rst @@ -5,8 +5,31 @@ Implementation plans for ``-fbounds-safety`` .. contents:: :local: +Gradual updates with experimental flag +== + +The feature will be implemented as a series of smaller PRs and we will guard our +implementation with an experimental flag ``-fexperimental-bounds-safety`` until +the usable model is fully available. Once the model is ready for use, we will +expose the flag ``-fbounds-safety``. + +Possible patch sets +--- + +* External bounds annotations and the (late) parsing logic. +* Internal bounds annotations (wide pointers) and their parsing logic. +* Clang code generation for wide pointers with debug information. +* Pointer cast semantics involving bounds annotations (this could be divided + into multiple sub-PRs). +* CFG analysis for pairs of related pointer and count assignments and the likes. +* Bounds check expressions in AST and the Clang code generation (this could also + be divided into multiple sub-PRs). + +Proposed implementation +=== + External bounds annotations -=== +--- The bounds annotations are C type attributes appertaining to pointer types. If an attribute is added to the position of a declaration attribute, e.g., ``int @@ -14,7 +37,7 @@ an attribute is added to the position of a declaration attribute, e.g., ``int type of the declaration (``int *``). New sugar types -=== +--- An external bounds annotation creates a type sugar of the underlying pointer types. We will introduce a new sugar type, ``DynamicBoundsPointerType`` to @@ -29,7 +52,7 @@ overloading. However, this design requires a separate logic to walk through the entire type hierarchy to check type compatibility of bounds annotations. Late parsing for C -== +-- A bounds annotation such as ``__counted_by(count)`` can be added to type of a struct field declaration where count is another field of the same struct @@ -43,7 +66,7 @@ same logic. This requires introducing late parsing logic for C/C++ type attributes. Internal bounds annotations -=== +--- ``__indexable`` and ``__bidi_indexable`` alter pointer representations to be equivalent to a struct with the pointer and the corresponding bounds fields. @@ -65,7 +88,7 @@ operations returning wide pointers. Alternatively, a new ``TEK`` and an expression emitter dedicated to wide pointers could be introduced. Default bounds annotations -== +-- The model may implicitly add ``__bidi_indexable`` or ``__single`` depending on the context of the declaration that has the pointer type. ``__bidi_indexable`` @@ -79,7 +102,7 @@ This also requires the parser to reset the type of the declaration with the newly created type with the right default attribute. Promotion expression - + A new expression will be introduced to represent the conversion from a pointer with an external bounds annotation, such as ``__counted_by``, to @@ -88,7 +111,7 @@ CastExprs because it requires an extra sub
[clang] Support `guarded_by` attribute and related attributes inside C structs and support late parsing them (PR #95455)
@@ -1,10 +1,11 @@ // RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta -fexperimental-late-parse-attributes -DLATE_PARSING %s #define LOCKABLE__attribute__ ((lockable)) #define SCOPED_LOCKABLE __attribute__ ((scoped_lockable)) -#define GUARDED_BY(x) __attribute__ ((guarded_by(x))) +#define GUARDED_BY(...) __attribute__ ((guarded_by(__VA_ARGS__))) rapidsna wrote: What's the reason to change it to take va_arg? https://github.com/llvm/llvm-project/pull/95455 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Bounds-Safety] Add sized_by, counted_by_or_null & sized_by_or_null (PR #93231)
@@ -8697,9 +8708,10 @@ static bool CheckCountedByAttrOnField( InvalidTypeKind = CountedByInvalidPointeeTypeKind::FLEXIBLE_ARRAY_MEMBER; } - if (InvalidTypeKind != CountedByInvalidPointeeTypeKind::VALID) { + if (InvalidTypeKind != CountedByInvalidPointeeTypeKind::VALID && + !CountInBytes) { rapidsna wrote: I agree with @delcypher. https://github.com/llvm/llvm-project/pull/93231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/95964 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
rapidsna wrote: @AaronBallman do you think we should remove the doc from the release notes because it's not ready yet? https://github.com/llvm/llvm-project/pull/95964 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety][doc] Make it clear that the feature is work-in-progress (PR #95964)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/95964 The `-fbounds-safety` feature is not available yet and the implementation is still in progress, which will be guarded by `-fexperimental-bounds-safety`. This is to make it more explicit in the document that the feature is not available yet and it only describes the model. >From 6ed8ac33952bc8f9a9b672740d45f25872ae1dbb Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Tue, 18 Jun 2024 11:05:17 -0700 Subject: [PATCH] [BoundsSafety][doc] Make it clear that the feature is work-in-progress --- clang/docs/BoundsSafety.rst | 7 +-- clang/docs/BoundsSafetyImplPlans.rst | 65 ++-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/clang/docs/BoundsSafety.rst b/clang/docs/BoundsSafety.rst index f1837675ec9bf..bd891733b7754 100644 --- a/clang/docs/BoundsSafety.rst +++ b/clang/docs/BoundsSafety.rst @@ -8,6 +8,9 @@ Overview +**NOTE:** This is a design document and the feature is not available for users yet. +Please find :doc:`BoundsSafetyImplPlans` for more details. + ``-fbounds-safety`` is a C extension to enforce bounds safety to prevent out-of-bounds (OOB) memory accesses, which remain a major source of security vulnerabilities in C. ``-fbounds-safety`` aims to eliminate this class of bugs @@ -55,9 +58,7 @@ adopt, offering these properties that make it widely adoptable in practice: * It has a relatively low adoption cost. This document discusses the key designs of ``-fbounds-safety``. The document is -subject to be actively updated with a more detailed specification. The -implementation plan can be found in :doc:`BoundsSafetyImplPlans`. - +subject to be actively updated with a more detailed specification. Programming Model = diff --git a/clang/docs/BoundsSafetyImplPlans.rst b/clang/docs/BoundsSafetyImplPlans.rst index 4fbf87f966350..93c2ed7b43402 100644 --- a/clang/docs/BoundsSafetyImplPlans.rst +++ b/clang/docs/BoundsSafetyImplPlans.rst @@ -5,8 +5,31 @@ Implementation plans for ``-fbounds-safety`` .. contents:: :local: +Gradual updates with experimental flag +== + +The feature will be implemented as a series of smaller PRs and we will guard our +implementation with an experimental flag ``-fexperimental-bounds-safety`` until +the usable model is fully available. Once the model is ready for use, we will +expose the flag ``-fbounds-safety``. + +Possible patch sets +--- + +* External bounds annotations and the (late) parsing logic. +* Internal bounds annotations (wide pointers) and their parsing logic. +* Clang code generation for wide pointers with debug information. +* Pointer cast semantics involving bounds annotations (this could be divided + into multiple sub-PRs). +* CFG analysis for pairs of related pointer and count assignments and the likes. +* Bounds check expressions in AST and the Clang code generation (this could also + be divided into multiple sub-PRs). + +Proposed implementation +=== + External bounds annotations -=== +--- The bounds annotations are C type attributes appertaining to pointer types. If an attribute is added to the position of a declaration attribute, e.g., ``int @@ -14,7 +37,7 @@ an attribute is added to the position of a declaration attribute, e.g., ``int type of the declaration (``int *``). New sugar types -=== +--- An external bounds annotation creates a type sugar of the underlying pointer types. We will introduce a new sugar type, ``DynamicBoundsPointerType`` to @@ -29,7 +52,7 @@ overloading. However, this design requires a separate logic to walk through the entire type hierarchy to check type compatibility of bounds annotations. Late parsing for C -== +-- A bounds annotation such as ``__counted_by(count)`` can be added to type of a struct field declaration where count is another field of the same struct @@ -43,7 +66,7 @@ same logic. This requires introducing late parsing logic for C/C++ type attributes. Internal bounds annotations -=== +--- ``__indexable`` and ``__bidi_indexable`` alter pointer representations to be equivalent to a struct with the pointer and the corresponding bounds fields. @@ -65,7 +88,7 @@ operations returning wide pointers. Alternatively, a new ``TEK`` and an expression emitter dedicated to wide pointers could be introduced. Default bounds annotations -== +-- The model may implicitly add ``__bidi_indexable`` or ``__single`` depending on the context of the declaration that has the pointer type. ``__bidi_indexable`` @@ -79,7 +102,7 @@ This also requires the parser to reset the type of the declaration with the newly created type with the right default attribute. P
[clang] [BoundsSafety] Add `-fexperimental-bounds-safety` CC1 and language option and use it to tweak `counted_by`'s semantics (PR #92623)
rapidsna wrote: @AaronBallman We haven't exposed the `-fbounds-safety` flag yet. The idea was to guard it under the experimental flag called `-fexperimental-bounds-safety` as a CC1 flag for testing until we have the full feature available (the feature is work-in-progress and is going to take some time to implement). Then, we would create to `-fbounds-safety` exposed as driver and CC1 flags. We would deprecate `-fexperimental-bounds-safety` after that point but might have to keep as an alias of `-fbounds-safety` for a release so people can switch over to `-fbounds-safety`. Does it sound like a reasonable strategy for you? https://github.com/llvm/llvm-project/pull/92623 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
https://github.com/rapidsna approved this pull request. LGTM assuming you will be fixing the buildkite failures. You may still need to wait on @AaronBallman's approval. https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
rapidsna wrote: > > You may also want to consider making the attribute late parsed in C when > > `-fexperimental-late-parse-attributes` is enabled. See > > https://github.com/llvm/llvm-project/pull/93121/files#diff-ae2ec9524bdbeea1f06917607482634dd89af5bcbb929805032463e5dafe79e7R2260 > > That will allow the code like below: > > ``` > > struct Foo { > >int a_value GUARDED_BY(mu_); // attribute comes before `mu_` which needs > > to be late parsed > >struct Mutext *mu_; > > } > > ``` > > Adopted `LateAttrParseExperimentalExt`. Let me know if that looks okay. Thanks! Could you please also add a test taking advantage of late parsing? https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
@@ -28,7 +28,12 @@ struct LOCKABLE Mutex {}; struct Foo { - struct Mutex *mu_; +struct Mutex *mu_; +struct Bar { +struct Mutex *other_mu; +} bar; + int a_value GUARDED_BY(mu_); rapidsna wrote: Nit: Please check indent https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
@@ -3330,6 +3340,63 @@ void Parser::DistributeCLateParsedAttrs(Decl *Dcl, } } +/// GuardedBy attributes (e.g., guarded_by): +/// AttrName '(' expression ')' +void Parser::ParseGuardedByAttribute(IdentifierInfo &AttrName, + SourceLocation AttrNameLoc, + ParsedAttributes &Attrs, + IdentifierInfo *ScopeName, + SourceLocation ScopeLoc, + SourceLocation *EndLoc, + ParsedAttr::Form Form) { + assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); + + BalancedDelimiterTracker Parens(*this, tok::l_paren); + Parens.consumeOpen(); + + if (Tok.is(tok::r_paren)) { +Diag(Tok.getLocation(), diag::err_argument_required_after_attribute); +Parens.consumeClose(); +return; + } + + ArgsVector ArgExprs; + // Don't evaluate argument when the attribute is ignored. + using ExpressionKind = + Sema::ExpressionEvaluationContextRecord::ExpressionKind; + EnterExpressionEvaluationContext EC( + Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, nullptr, + ExpressionKind::EK_BoundsAttrArgument); rapidsna wrote: @AaronBallman Renaming it makes a lot of sense to me. https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Support [[guarded_by(mutex)]] attribute inside C struct (PR #94216)
https://github.com/rapidsna commented: You may also want to consider making the attribute late parsed in C when `-fexperimental-late-parse-attributes` is enabled. See https://github.com/llvm/llvm-project/pull/93121/files#diff-ae2ec9524bdbeea1f06917607482634dd89af5bcbb929805032463e5dafe79e7R2260 That will allow the code like below: ``` struct Foo { int a_value GUARDED_BY(mu_); // attribute comes before `mu_` which needs to be late parsed struct Mutext *mu_; } ``` https://github.com/llvm/llvm-project/pull/94216 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Bounds-Safety] Add sized_by, counted_by_or_null & sized_by_or_null (PR #93231)
https://github.com/rapidsna approved this pull request. https://github.com/llvm/llvm-project/pull/93231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Bounds-Safety] Add sized_by, counted_by_or_null & sized_by_or_null (PR #93231)
@@ -425,6 +425,12 @@ Attribute Changes in Clang size_t count; }; +- The attributes ``sized_by``, ``counted_by_or_null`` and ``sized_by_or_null``` + have been added as variants on ``counted_by``, each with slightly different semantics. + ``sized_by`` takes a byte size parameter instead of an element count, allowing pointees + with unknown size. The ``counted_by_or_null`` and ``sized_by_or_null`` variants are equivalent + to their base variants, except the pointer can be null regardless of count/size value. rapidsna wrote: It would be nice to add a suggested use case for `sized_by_or_null`. e.g., an allocator that returns either a buffer of size or nullptr. https://github.com/llvm/llvm-project/pull/93231 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/93121 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
@@ -4944,6 +4944,26 @@ void Parser::ParseStructDeclaration( } } +// TODO: All callers of this function should be moved to +// `Parser::ParseLexedAttributeList`. +void Parser::ParseLexedCAttributeList(LateParsedAttrList &LAs, bool EnterScope, + ParsedAttributes *OutAttrs) { + assert(LAs.parseSoon() && + "Attribute list should be marked for immediate parsing."); +#ifndef NDEBUG + auto LangStd = getLangOpts().LangStd; + if (LangStd != LangStandard::lang_unspecified) { +auto Lang = LangStandard::getLangStandardForKind(LangStd).getLanguage(); +assert(Lang == Language::C || Lang == Language::OpenCL); rapidsna wrote: > That doesn't sound equivalent. The language could also be objective-C or > objective-C++. I think `getLangOpts().CPlusPlus` should set true when it is `Objective-C++`. https://github.com/llvm/llvm-project/pull/93121 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
@@ -4944,6 +4944,26 @@ void Parser::ParseStructDeclaration( } } +// TODO: All callers of this function should be moved to +// `Parser::ParseLexedAttributeList`. +void Parser::ParseLexedCAttributeList(LateParsedAttrList &LAs, bool EnterScope, + ParsedAttributes *OutAttrs) { + assert(LAs.parseSoon() && + "Attribute list should be marked for immediate parsing."); +#ifndef NDEBUG + auto LangStd = getLangOpts().LangStd; + if (LangStd != LangStandard::lang_unspecified) { +auto Lang = LangStandard::getLangStandardForKind(LangStd).getLanguage(); +assert(Lang == Language::C || Lang == Language::OpenCL); rapidsna wrote: Was there a reason this couldn't be `!getLangOpts().CPlusPlus`? https://github.com/llvm/llvm-project/pull/93121 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
@@ -0,0 +1,196 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +#define __counted_by(f) __attribute__((counted_by(f))) + +struct bar; + +struct not_found { + int count; + struct bar *fam[] __counted_by(bork); // expected-error {{use of undeclared identifier 'bork'}} +}; + +struct no_found_count_not_in_substruct { + unsigned long flags; + unsigned char count; // expected-note {{'count' declared here}} + struct A { +int dummy; +int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the flexible array}} + } a; +}; + +struct not_found_count_not_in_unnamed_substruct { + unsigned char count; // expected-note {{'count' declared here}} + struct { +int dummy; +int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the flexible array}} + } a; +}; + +struct not_found_count_not_in_unnamed_substruct_2 { + struct { +unsigned char count; // expected-note {{'count' declared here}} + }; + struct { +int dummy; +int array[] __counted_by(count); // expected-error {{'counted_by' field 'count' isn't within the same struct as the flexible array}} + } a; +}; + +struct not_found_count_in_other_unnamed_substruct { + struct { +unsigned char count; + } a1; + + struct { +int dummy; +int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}} + }; +}; + +struct not_found_count_in_other_substruct { + struct _a1 { +unsigned char count; + } a1; + + struct { +int dummy; +int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}} + }; +}; + +struct not_found_count_in_other_substruct_2 { + struct _a2 { +unsigned char count; + } a2; + + int array[] __counted_by(count); // expected-error {{use of undeclared identifier 'count'}} +}; + +struct not_found_suggest { + int bork; + struct bar *fam[] __counted_by(blork); // expected-error {{use of undeclared identifier 'blork'}} +}; + +int global; // expected-note {{'global' declared here}} + +struct found_outside_of_struct { + int bork; + struct bar *fam[] __counted_by(global); // expected-error {{field 'global' in 'counted_by' not inside structure}} +}; + +struct self_referrential { + int bork; + struct bar *self[] __counted_by(self); // expected-error {{use of undeclared identifier 'self'}} +}; + +struct non_int_count { + double dbl_count; + struct bar *fam[] __counted_by(dbl_count); // expected-error {{'counted_by' requires a non-boolean integer type argument}} +}; + +struct array_of_ints_count { + int integers[2]; + struct bar *fam[] __counted_by(integers); // expected-error {{'counted_by' requires a non-boolean integer type argument}} +}; + +struct not_a_fam { + int count; + // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct bar' is an incomplete type}} + struct bar *non_fam __counted_by(count); +}; + +struct not_a_c99_fam { + int count; + struct bar *non_c99_fam[0] __counted_by(count); // expected-error {{'counted_by' on arrays only applies to C99 flexible array members}} +}; + +struct annotated_with_anon_struct { + unsigned long flags; + struct { +unsigned char count; +int array[] __counted_by(crount); // expected-error {{use of undeclared identifier 'crount'}} + }; +}; + +//== +// __counted_by on a struct VLA with element type that has unknown size +//== + +struct size_unknown; // expected-note 2{{forward declaration of 'struct size_unknown'}} +struct on_member_arr_incomplete_ty_ty_pos { + int count; + // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}} + // expected-error@+1{{array has incomplete element type 'struct size_unknown'}} + struct size_unknown buf[] __counted_by(count); +}; + +struct on_member_arr_incomplete_const_ty_ty_pos { + int count; + // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}} + // expected-error@+1{{array has incomplete element type 'const struct size_unknown'}} + const struct size_unknown buf[] __counted_by(count); +}; + +struct on_member_arr_void_ty_ty_pos { + int count; + // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}} + // expected-error@+1{{array has incomplete element type 'void'}} + void buf[] __counted_by(count); +}; + +typedef void(fn_ty)(int); + +struct on_member_arr_fn_ptr_ty { + int count; + // An Array of function pointers is allowed + fn_ty* buf[] __counted_by(count); +}; + +struct on_member_arr_fn_ty { + int count; + // An array of functions is not allowed. + // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}} + // expected-error@+1{{'buf' d
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
https://github.com/rapidsna edited https://github.com/llvm/llvm-project/pull/93121 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Reland #90786 ([BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C) (PR #93121)
https://github.com/rapidsna approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/93121 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C (PR #90786)
rapidsna wrote: @bwendling @kees Likely, we should not put `__counted_by` in that case. Could we fix the source? https://github.com/llvm/llvm-project/pull/90786 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C (PR #90786)
rapidsna wrote: @bwendling @kees Wait. `ATOM_PPLIB_STATE_V2` is also a struct with flexible array member? This is concerning because `ucNumEntries * sizeof(ATOM_PPLIB_STATE_V2)` is not the correct size anyway. Do you know the semantics of this structure? https://github.com/llvm/llvm-project/pull/90786 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C (PR #90786)
rapidsna wrote: @bwendling Thanks for reporting. We will relax the restrictions for arrays to not break the existing users. https://github.com/llvm/llvm-project/pull/90786 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C (PR #90786)
rapidsna wrote: > The main concern I have with delaying support for this is that header files > could find themselves in a state where they could not be refactored without > removing counted_by attributes that refer to now-incomplete structs. @kees Agreed. We will work on a follow up patch to support this, and a separate patch to support `sized_by` for `void *` and cases where it needs a byte size. https://github.com/llvm/llvm-project/pull/90786 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] Allow 'counted_by' attribute on pointers in structs in C (PR #90786)
rapidsna wrote: > I've been thinking about this restriction. Why is this necessary? My > assumption was that applying counted_by to a pointer causes a bounds check on > an index into the pointer rather than its underlying type. @bwendling It's because these types are not indexable really. **void:** `void` doesn't have a size and C standard doesn't allow indexing into `void *`. I understand `void *` can be indexable under a GNU extension, but I don't see not supporting it is a problem because we can use `__sized_by` to annotate `void *` to clearly indicate the byte size. We will upstream `__sized_by` support soon so you can use it for `void *`. **function types** Although, again, the GNU extension allows it, we don't really want to index into function pointers. We can still use `__sized_by` if we really need to. **Incomplete structs** You can't really index into an incomplete struct. Though as @apple-fcloutier mentioned, by the point when the pointer is actually indexed, you should have the complete type definition. Otherwise, indexing will be an error anyway. So we have been considering relaxing this requirement, and move the error point to where the pointer is actually used in a way it requires the concrete element size (e.g, places you would insert `__dynamic_builtin_object_size`, you need the element size to calculate the byte size; indexing into a pointer to incomplete struct is already an error). https://github.com/llvm/llvm-project/pull/90786 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] WIP: Make 'counted_by' work for pointer fields; late parsing for 'counted_by' on decl attr position (PR #87596)
https://github.com/rapidsna closed https://github.com/llvm/llvm-project/pull/87596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] WIP: Make 'counted_by' work for pointer fields; late parsing for 'counted_by' on decl attr position (PR #87596)
@@ -8547,15 +8547,25 @@ static const RecordDecl *GetEnclosingNamedOrTopAnonRecord(const FieldDecl *FD) { return RD; } -static bool -CheckCountExpr(Sema &S, FieldDecl *FD, Expr *E, - llvm::SmallVectorImpl &Decls) { +static bool CheckCountedByAttrOnField( +Sema &S, FieldDecl *FD, Expr *E, +llvm::SmallVectorImpl &Decls) { if (FD->getParent()->isUnion()) { S.Diag(FD->getBeginLoc(), diag::err_counted_by_attr_in_union) << FD->getSourceRange(); return true; } + const auto FieldTy = FD->getType(); + if (FieldTy->isPointerType() && + FieldTy->getPointeeType()->isIncompleteType()) { rapidsna wrote: `isIncompleteType()` won't be sufficient. We should also prevent a pointer to sizeless type, function type, and struct with flexible array member, etc. https://github.com/llvm/llvm-project/pull/87596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as supporting an experimental late parsing mode "extension" (PR #88596)
@@ -1371,14 +1371,23 @@ class Parser : public CodeCompletionHandler { }; // A list of late-parsed attributes. Used by ParseGNUAttributes. - class LateParsedAttrList: public SmallVector { + class LateParsedAttrList : public SmallVector { rapidsna wrote: Nit: seemingly unnecessary whitespace only change https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as supporting an experimental late parsing mode "extension" (PR #88596)
https://github.com/rapidsna approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as supporting an experimental late parsing mode "extension" (PR #88596)
@@ -2101,9 +2173,21 @@ bool PragmaClangAttributeSupport::isAttributedSupported( return SpecifiedResult; // Opt-out rules: - // An attribute requires delayed parsing (LateParsed is on) - if (Attribute.getValueAsBit("LateParsed")) + + // An attribute requires delayed parsing (LateParsed is on). + switch (getLateAttrParseKind(&Attribute)) { + case LateAttrParseKind::Never: +break; + case LateAttrParseKind::Standard: +return false; + case LateAttrParseKind::ExperimentalExt: +// FIXME: fix this comment rapidsna wrote: ^^ so you won't miss this FIXME https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as supporting an experimental late parsing mode "extension" (PR #88596)
@@ -89,13 +89,23 @@ static StringRef normalizeAttrName(StringRef Name) { return Name; } -/// isAttributeLateParsed - Return true if the attribute has arguments that -/// require late parsing. -static bool isAttributeLateParsed(const IdentifierInfo &II) { +/// returns true iff attribute is annotated with `LateAttrParseExperimentalExt` +/// in `Attr.td`. +static bool IsAttributeLateParsedExperimentalExt(const IdentifierInfo &II) { rapidsna wrote: Nit: should we make it consistent `IsAttributeLateParsedExperimentalExt` and `isAttributeLateParsedStandard`? One starts with capital I and the other starts with lower-case i. https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as only supporting late parsing when passing an experimental feature flag (PR #88596)
@@ -91,11 +91,24 @@ static StringRef normalizeAttrName(StringRef Name) { /// isAttributeLateParsed - Return true if the attribute has arguments that /// require late parsing. -static bool isAttributeLateParsed(const IdentifierInfo &II) { +bool Parser::isAttributeLateParsed(const IdentifierInfo &II) { rapidsna wrote: I think the function also needs some update to make it so that attributes marked as `LateParseAttrExperimentalExt` will fall back to the behavior equivalent to `LateParseAttrStandard`. Currently, it seems that the function will always return `false` for attributes with `LateParseAttrExperimentalExt` when the experimental flag is disabled. https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as only supporting late parsing when passing an experimental feature flag (PR #88596)
@@ -592,6 +592,46 @@ class AttrSubjectMatcherAggregateRule { def SubjectMatcherForNamed : AttrSubjectMatcherAggregateRule; +// Late Attribute parsing mode enum +class LateAttrParseKind { + int Kind = val; +} + +// Never late parsed +def LateAttrParseNever : LateAttrParseKind<0>; + +// Standard late attribute parsing +// +// This is language dependent. For example: +// +// * For C++ enables late parsing of a declaration attributes +// * For C does not enable late parsing of attributes +// +def LateAttrParseStandard: LateAttrParseKind<1>; + +// Experimental extension to standard late attribute parsing +// +// This extension behaves like `LateAttrParseStandard` but allows +// late parsing attributes in more contexts. +// +// This extension changes behavior depending on whether the +// `-fexperimental-late-parse-attributes` +// (`LangOpts.ExperimentalLateParseAttributes`) flag is enabled. +// +// * If the flag is disabled then the attribute is parsed just like +// `LateAttrParseStandard`. +// +// * If the flag is enabled and if the attribute is being used in a context +// supported by this extension it will be late parsed. Otherwise the attribute +// will be parsed just like `LateAttrParseStandard`. +// +// Currently this extension extends `LateAttrParseStandard` by allowing: +// +// * For C, late parsing of the `counted_by` as a type or decl attribute. +// (TODO: Not implemented yet). rapidsna wrote: I don't have a strong opinion here. Having TODO in the comment seems okay to me. https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as only supporting late parsing when passing an experimental feature flag (PR #88596)
@@ -592,6 +592,46 @@ class AttrSubjectMatcherAggregateRule { def SubjectMatcherForNamed : AttrSubjectMatcherAggregateRule; +// Late Attribute parsing mode enum rapidsna wrote: @delcypher Thanks! LGTM. https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Attributes] Support Attributes being declared as only supporting late parsing when passing an experimental feature flag (PR #88596)
@@ -592,6 +592,16 @@ class AttrSubjectMatcherAggregateRule { def SubjectMatcherForNamed : AttrSubjectMatcherAggregateRule; +// Late Attribute parsing mode enum +class LateAttrParseKind { + int Kind = val; +} +def LateAttrParseNever : LateAttrParseKind<0>; // Never late parsed +def LateAttrParseAlways: LateAttrParseKind<1>; // Always late parsed +// Late parsed if `-fexperimental-late-parse-attributes` is on and parsed +// normally if off. +def LateAttrParseExperimentalOnly : LateAttrParseKind<2>; rapidsna wrote: I'd propose slightly different semantics for `LateAttrParseAlways` and `LateAttrParseExperimentalOnly`, and renaming accordingly. Before this patch, if an attribute was marked `LateParsed=1`, it meant late parsing is enabled for C++ and at the declaration attributes position. However, the same attribute was not late parsed when it is compiled for C. Therefore, having `LateParsed=1` mean "always late parsed" would be misleading. Instead, it should be something like `LateAttrParseStandard` to indicate the attribute will be late parsed in a conventional way, for C++ on the declaration attribute positions. Then, `LateParsed=2` should capture the experimental (extended) semantics to enable late parsing for C on both the decl and type attribute positions (in the future), which would be controlled by `-fexperimental-late-parse-attributes`. When the flag is disabled, it should fall back to the same behavior as `LateParsed=1`. For `counted_by` which is a C only attribute, it means no late parsing would enabled because it will be compiled only for C. Consider `guarded_by`, which is marked `LateParsed=1` and is also available for C++. Let's say we adopt `LateAttrParseExperimental` for `guarded_by`. With the flag, the code below will compile for C (though that is a lie because a naked field reference doesn't work for C atm https://github.com/llvm/llvm-project/issues/20777, but that's a separate issue and let's assume we've somehow resolved that issue). ``` struct guarded_int { int value __attribute__((guarded_by(m)); struct mutex m; }; ``` Without the flag, it won't be late parsed for C, but it should continue to be late parsed for C++ on the decl attribute positions like before. https://github.com/llvm/llvm-project/pull/88596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] WIP: Make 'counted_by' work for pointer fields; late parsing for 'counted_by' on decl attr position (PR #87596)
rapidsna wrote: > This should be fine, because count is declared before use—unless I'm getting > type attributes confused with field attributes.. `struct size_unknown *__counted_by(count) buf` indicates that `buf` has `sizeof(struct size_unknown) * count` bytes. So the problem is here that the compiler doesn't know what's the actual byte size of `buf` because `sizeof(struct unknown_size)` is not available here. https://github.com/llvm/llvm-project/pull/87596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [BoundsSafety] WIP: Make 'counted_by' work for pointer fields; late parsing for 'counted_by' on decl attr position (PR #87596)
@@ -4997,7 +5087,11 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc, ParsedAttributes attrs(AttrFactory); // If attributes exist after struct contents, parse them. - MaybeParseGNUAttributes(attrs); + MaybeParseGNUAttributes(attrs, &LateFieldAttrs); + + assert(!getLangOpts().CPlusPlus); rapidsna wrote: @delcypher C++ already has late parsing support for C++ classes and structs and I believe it's handled separately in `ParseDeclCXX.cpp`. I added the assertion to ensure that the code path isn't taken in C++. We should run clang tests if that breaks anything. https://github.com/llvm/llvm-project/pull/87596 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Unwrap CountAttributed for debug info (PR #86017)
https://github.com/rapidsna closed https://github.com/llvm/llvm-project/pull/86017 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Unwrap CountAttributed for debug info (PR #86017)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/86017 >From abfcb60e7b65e755733f4d41795aa9cfd44e0cc3 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 21 Mar 2024 06:47:05 +0900 Subject: [PATCH 1/3] Unwrap CountAttributed for debug info Fix crash caused by 3eb9ff30959a670559bcba03d149d4c51bf7c9c9 --- clang/lib/CodeGen/CGDebugInfo.cpp | 3 +++ .../test/CodeGen/attr-counted-by-debug-info.c | 18 ++ 2 files changed, 21 insertions(+) create mode 100644 clang/test/CodeGen/attr-counted-by-debug-info.c diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 07ecaa81c47d84..7453ed14aef414 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3463,6 +3463,9 @@ static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { case Type::BTFTagAttributed: T = cast(T)->getWrappedType(); break; +case Type::CountAttributed: + T = cast(T)->desugar(); + break; case Type::Elaborated: T = cast(T)->getNamedType(); break; diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c new file mode 100644 index 00..f3e7897e12d667 --- /dev/null +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -0,0 +1,18 @@ +// RUN: %clang -emit-llvm -DCOUNTED_BY -S -g %s -o - | FileCheck %s +// RUN: %clang -emit-llvm -S -g %s -o - | FileCheck %s + +#ifdef COUNTED_BY +#define __counted_by(member)__attribute__((__counted_by__(member))) +#else +#define __counted_by(member) +#endif + +struct { + int num_counters; + long value[] __counted_by(num_counters); +} agent_send_response_port_num; + +// CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) +// ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// ![[ELEMENTS]] = !{![[COUNT]]} +// ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file >From 5bb879be895b90dc942d160fbe9dfe657e73db1d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 21 Mar 2024 06:59:11 +0900 Subject: [PATCH 2/3] Add missing CHECKs --- clang/test/CodeGen/attr-counted-by-debug-info.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c index f3e7897e12d667..15197f79e4370c 100644 --- a/clang/test/CodeGen/attr-counted-by-debug-info.c +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -13,6 +13,6 @@ struct { } agent_send_response_port_num; // CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) -// ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) -// ![[ELEMENTS]] = !{![[COUNT]]} -// ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file +// CHECK: ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// CHECK: ![[ELEMENTS]] = !{![[COUNT:.*]]} +// CHECK: ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file >From c1da42c5c5154fdc1fe8324dccd5368568d5304e Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Fri, 22 Mar 2024 05:45:31 +0900 Subject: [PATCH 3/3] Fix test.attr-counted-by-debug-info.c to also cover 32-bit long --- clang/test/CodeGen/attr-counted-by-debug-info.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c index 15197f79e4370c..a6c2b1382b796b 100644 --- a/clang/test/CodeGen/attr-counted-by-debug-info.c +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -13,6 +13,6 @@ struct { } agent_send_response_port_num; // CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) -// CHECK: ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// CHECK: ![[BT]] = !DIBasicType(name: "long", size: {{.*}}, encoding: DW_ATE_signed) // CHECK: ![[ELEMENTS]] = !{![[COUNT:.*]]} // CHECK: ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Unwrap CountAttributed for debug info (PR #86017)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/86017 >From abfcb60e7b65e755733f4d41795aa9cfd44e0cc3 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 21 Mar 2024 06:47:05 +0900 Subject: [PATCH 1/2] Unwrap CountAttributed for debug info Fix crash caused by 3eb9ff30959a670559bcba03d149d4c51bf7c9c9 --- clang/lib/CodeGen/CGDebugInfo.cpp | 3 +++ .../test/CodeGen/attr-counted-by-debug-info.c | 18 ++ 2 files changed, 21 insertions(+) create mode 100644 clang/test/CodeGen/attr-counted-by-debug-info.c diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 07ecaa81c47d84..7453ed14aef414 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3463,6 +3463,9 @@ static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { case Type::BTFTagAttributed: T = cast(T)->getWrappedType(); break; +case Type::CountAttributed: + T = cast(T)->desugar(); + break; case Type::Elaborated: T = cast(T)->getNamedType(); break; diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c new file mode 100644 index 00..f3e7897e12d667 --- /dev/null +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -0,0 +1,18 @@ +// RUN: %clang -emit-llvm -DCOUNTED_BY -S -g %s -o - | FileCheck %s +// RUN: %clang -emit-llvm -S -g %s -o - | FileCheck %s + +#ifdef COUNTED_BY +#define __counted_by(member)__attribute__((__counted_by__(member))) +#else +#define __counted_by(member) +#endif + +struct { + int num_counters; + long value[] __counted_by(num_counters); +} agent_send_response_port_num; + +// CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) +// ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// ![[ELEMENTS]] = !{![[COUNT]]} +// ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file >From 5bb879be895b90dc942d160fbe9dfe657e73db1d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 21 Mar 2024 06:59:11 +0900 Subject: [PATCH 2/2] Add missing CHECKs --- clang/test/CodeGen/attr-counted-by-debug-info.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c index f3e7897e12d667..15197f79e4370c 100644 --- a/clang/test/CodeGen/attr-counted-by-debug-info.c +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -13,6 +13,6 @@ struct { } agent_send_response_port_num; // CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) -// ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) -// ![[ELEMENTS]] = !{![[COUNT]]} -// ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file +// CHECK: ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// CHECK: ![[ELEMENTS]] = !{![[COUNT:.*]]} +// CHECK: ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Unwrap CountAttributed for debug info (PR #86017)
rapidsna wrote: Fix crash in https://github.com/llvm/llvm-project/pull/78000 https://github.com/llvm/llvm-project/pull/86017 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: Thanks @nathanchance. Opened PR to fix the crash. https://github.com/llvm/llvm-project/pull/86017 https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Unwrap CountAttributed for debug info (PR #86017)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/86017 Fix crash caused by 3eb9ff30959a670559bcba03d149d4c51bf7c9c9 >From abfcb60e7b65e755733f4d41795aa9cfd44e0cc3 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Thu, 21 Mar 2024 06:47:05 +0900 Subject: [PATCH] Unwrap CountAttributed for debug info Fix crash caused by 3eb9ff30959a670559bcba03d149d4c51bf7c9c9 --- clang/lib/CodeGen/CGDebugInfo.cpp | 3 +++ .../test/CodeGen/attr-counted-by-debug-info.c | 18 ++ 2 files changed, 21 insertions(+) create mode 100644 clang/test/CodeGen/attr-counted-by-debug-info.c diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 07ecaa81c47d84..7453ed14aef414 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3463,6 +3463,9 @@ static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { case Type::BTFTagAttributed: T = cast(T)->getWrappedType(); break; +case Type::CountAttributed: + T = cast(T)->desugar(); + break; case Type::Elaborated: T = cast(T)->getNamedType(); break; diff --git a/clang/test/CodeGen/attr-counted-by-debug-info.c b/clang/test/CodeGen/attr-counted-by-debug-info.c new file mode 100644 index 00..f3e7897e12d667 --- /dev/null +++ b/clang/test/CodeGen/attr-counted-by-debug-info.c @@ -0,0 +1,18 @@ +// RUN: %clang -emit-llvm -DCOUNTED_BY -S -g %s -o - | FileCheck %s +// RUN: %clang -emit-llvm -S -g %s -o - | FileCheck %s + +#ifdef COUNTED_BY +#define __counted_by(member)__attribute__((__counted_by__(member))) +#else +#define __counted_by(member) +#endif + +struct { + int num_counters; + long value[] __counted_by(num_counters); +} agent_send_response_port_num; + +// CHECK: !DICompositeType(tag: DW_TAG_array_type, baseType: ![[BT:.*]], elements: ![[ELEMENTS:.*]]) +// ![[BT]] = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed) +// ![[ELEMENTS]] = !{![[COUNT]]} +// ![[COUNT]] = !DISubrange(count: -1) \ No newline at end of file ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From 3d2716ad6088f600c14d6ff724aa90453130a1f7 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/14] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 34 files changed, 679 insertions(+), 160 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index ff6b64c7f72d57..002f36ecbbaa3f 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 5080551ada4fc6..4a1ff222ecadcd 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1110,6 +1110,12 @@ DEF_TRAVERSE_TYPE(Injected
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: @AaronBallman Thank you! The -fbounds-safety documentation is currently shown https://clang.llvm.org/docs/BoundsSafety.html, but yes, we're planning to add the feature in the release note once we have more functionalities in place. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From 3d2716ad6088f600c14d6ff724aa90453130a1f7 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/11] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 34 files changed, 679 insertions(+), 160 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index ff6b64c7f72d57..002f36ecbbaa3f 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 5080551ada4fc6..4a1ff222ecadcd 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1110,6 +1110,12 @@ DEF_TRAVERSE_TYPE(Injected
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From 3d2716ad6088f600c14d6ff724aa90453130a1f7 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/10] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 34 files changed, 679 insertions(+), 160 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index ff6b64c7f72d57..002f36ecbbaa3f 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 5080551ada4fc6..4a1ff222ecadcd 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1110,6 +1110,12 @@ DEF_TRAVERSE_TYPE(Injected
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: @Endilll I just rebased it so you can review `Sema.h`. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From 3d2716ad6088f600c14d6ff724aa90453130a1f7 Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/9] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 34 files changed, 679 insertions(+), 160 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index ff6b64c7f72d57..002f36ecbbaa3f 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 5080551ada4fc6..4a1ff222ecadcd 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1110,6 +1110,12 @@ DEF_TRAVERSE_TYPE(InjectedCl
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/9] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/8] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: @AaronBallman thanks for the review! I addressed your comments. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -2000,6 +2001,21 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase { unsigned NumExpansions; }; + class CountAttributedTypeBitfields { +friend class CountAttributedType; + +LLVM_PREFERRED_TYPE(TypeBitfields) +unsigned : NumTypeBits; + +/// The limit is 15. rapidsna wrote: Thanks! I added `static_assert` and removed the comment. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -1117,6 +1117,32 @@ class ObjCInterfaceTypeLoc : public ConcreteTypeLoc { +public: + TypeLoc getInnerLoc() const { return this->getInnerTypeLoc(); } + QualType getInnerType() const { return this->getTypePtr()->desugar(); } + void initializeLocal(ASTContext &Context, SourceLocation Loc) { +// nothing to do + } + unsigned getLocalDataAlignment() const { return 1; } + unsigned getLocalDataSize() const { return 0; } rapidsna wrote: I think you're right. I just removed them. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -4259,7 +4240,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E, if (const auto *ME = dyn_cast(Array); ME && ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) && - ME->getMemberDecl()->hasAttr()) { + ME->getMemberDecl()->getType()->getAs()) { rapidsna wrote: `ME->getMemberDecl()->getType()` might be wrapped in another sugar type, so `isa` might not work in that case. To deal with that, I just added `isCountAttributedType()`. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -13233,6 +13262,32 @@ static QualType getCommonSugarTypeNode(ASTContext &Ctx, const Type *X, return QualType(); return Ctx.getUsingType(CD, Ctx.getQualifiedType(Underlying)); } + case Type::CountAttributed: { +const auto *DX = cast(X), + *DY = cast(Y); +if (DX->isCountInBytes() != DY->isCountInBytes()) + return QualType(); +if (DX->isOrNull() != DY->isOrNull()) + return QualType(); +const auto CEX = DX->getCountExpr(); +const auto CEY = DY->getCountExpr(); +const auto CDX = DX->getCoupledDecls(); rapidsna wrote: Done! https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -7239,6 +7239,13 @@ QualType TreeTransform::TransformAttributedType(TypeLocBuilder &TLB, }); } +template +QualType TreeTransform::TransformCountAttributedType( +TypeLocBuilder &TLB, CountAttributedTypeLoc TL) { + // TODO + llvm_unreachable("Unexpected TreeTransform for CountAttributedType"); rapidsna wrote: @AaronBallman thanks for letting me know. I just implemented `TransformCountAttributedType`. I'll see if I can fine a test case to exercise this. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/7] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: Gentle reminder @bwendling @AaronBallman. I'd like to land this patch soon, so we can open source more of -fbounds-safety work. Could you please take a look? https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/6] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -8463,133 +8463,115 @@ static void handleZeroCallUsedRegsAttr(Sema &S, Decl *D, const ParsedAttr &AL) { D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL)); } -static void handleCountedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) { - if (!AL.isArgIdent(0)) { -S.Diag(AL.getLoc(), diag::err_attribute_argument_type) -<< AL << AANT_ArgumentIdentifier; -return; +static const RecordDecl *GetEnclosingNamedOrTopAnonRecord(const FieldDecl *FD) { + const auto *RD = FD->getParent(); + // An unnamed struct is anonymous struct only if it's not instantiated. + // However, the struct may not be fully processed yet to determine + // whether it's anonymous or not. In that case, this function treats it as + // an anonymous struct and tries to find a named parent. + while (RD && (RD->isAnonymousStructOrUnion() || rapidsna wrote: @bwendling I updated the logic so that it distinguishes between anonymous struct and unnamed struct that's still processing so whether it's anonymous struct is not determined yet. We treat it as anonymous struct here and what that means is that we couldn't handle the error case like below here: ``` struct Parent { struct { int count; }; struct { int dummy; int arr[__counted_by(count)]; // error: 'counted_by' field 'count' isn't within the same struct as the flexible array } unnamed_s; // anonymous struct }; ``` but such a remaining error case will be handled later in https://github.com/llvm/llvm-project/pull/78000/files#diff-bf37b284dfa0a76af702a5493cde01dec7f88619533c1be15e74654862299770R4841 where the unnamed struct is determined whether it's actually anonymous or not. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/4] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna updated https://github.com/llvm/llvm-project/pull/78000 >From c39871ed2ec642ab00360f2c3a18fba7c915f82d Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 1/3] [BoundsSafety] Introduce CountAttributedType CountAttributedType is a sugar type to represent a type with a 'counted_by' attribute and the likes, which provides bounds information to the underlying type. The type contains an the argument of attribute as an expression. Additionally, the type holds metadata about declarations referenced by the expression in order to make it easier for Sema to access declarations on which the type depends. This also adjusts the CountedBy attribute definition and implements parsing CountAttributedType. __bdos and array-checks sanitizer use CountAttributedType instead of hasAttr. Implements special lookup for counted_by argument in structs. Adjust test/Sema/attr-counted-by.c to match the default diags generated by the expression parser. --- clang/include/clang/AST/ASTContext.h | 7 + clang/include/clang/AST/PropertiesBase.td | 1 + clang/include/clang/AST/RecursiveASTVisitor.h | 9 + clang/include/clang/AST/Type.h| 152 +++ clang/include/clang/AST/TypeLoc.h | 26 +++ clang/include/clang/AST/TypeProperties.td | 19 ++ clang/include/clang/Basic/Attr.td | 7 +- .../clang/Basic/DiagnosticSemaKinds.td| 14 +- clang/include/clang/Basic/TypeNodes.td| 2 + clang/include/clang/Parse/Parser.h| 5 + clang/include/clang/Sema/Sema.h | 16 +- .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 2 +- clang/lib/AST/ASTContext.cpp | 55 ++ clang/lib/AST/ASTImporter.cpp | 22 +++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 65 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 30 +++ clang/lib/CodeGen/CGBuiltin.cpp | 6 +- clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CGExpr.cpp | 37 +--- clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Parse/ParseDecl.cpp | 90 + clang/lib/Sema/SemaDecl.cpp | 6 - clang/lib/Sema/SemaDeclAttr.cpp | 183 -- clang/lib/Sema/SemaExpr.cpp | 25 ++- clang/lib/Sema/SemaType.cpp | 12 ++ clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/test/Sema/attr-counted-by.c | 20 +- clang/tools/libclang/CIndex.cpp | 4 + 35 files changed, 692 insertions(+), 163 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 12ce9af1e53f63..08795af245c06b 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -250,6 +250,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1341,6 +1343,11 @@ class ASTContext : public RefCountedBase { return CanQualType::CreateUnsafe(getPointerType((QualType) T)); } + QualType + getCountAttributedType(QualType T, Expr *CountExpr, bool CountInBytes, + bool OrNull, + ArrayRef DependentDecls) const; + /// Return the uniqued reference to a type adjusted from the original /// type to a new type. QualType getAdjustedType(QualType Orig, QualType New) const; diff --git a/clang/include/clang/AST/PropertiesBase.td b/clang/include/clang/AST/PropertiesBase.td index 0270c086d06b6a..6df1d93a7ba2eb 100644 --- a/clang/include/clang/AST/PropertiesBase.td +++ b/clang/include/clang/AST/PropertiesBase.td @@ -143,6 +143,7 @@ def UInt32 : CountPropertyType<"uint32_t">; def UInt64 : CountPropertyType<"uint64_t">; def UnaryTypeTransformKind : EnumPropertyType<"UnaryTransformType::UTTKind">; def VectorKind : EnumPropertyType<"VectorKind">; +def TypeCoupledDeclRefInfo : PropertyType; def ExceptionSpecInfo : PropertyType<"FunctionProtoType::ExceptionSpecInfo"> { let BufferElementTypes = [ QualType ]; diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index 9da5206a21c34c..b058e0435912eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisit
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: @AaronBallman @erichkeane gentle reminder. Does my reasoning [above](https://github.com/llvm/llvm-project/pull/78000#issuecomment-1932496432) to make a new sugar type make sense? https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
rapidsna wrote: Thanks @AaronBallman! > I think AttributedType might be a reasonable way forward. That's how we model > nullability qualifiers, for example: The difference from nullability qualifiers is that they are actually encoded as `AttributeKind`, whereas `__counted_by` has to store more than `Kind`, i.e., an expression and some dependent declarations. And the reason why we created a new sugar type instead of using `AttributedType` is that `AttributedType` itself doesn't have an extended data other than `AttributeKind` as an integer value. And more data seems to be stored in `TypeLoc`. Related to this, I remember we talked about having `CountAttributedType` to inherit `AttributedType`. I didn't want this to be picked up by `getAs` so that we won't have to conditionalize `CountAttributedType` where `AttributeType` is generally handled and there's no other type inheriting `AttributedType` so far. E.g., BTagAttributedType is a sugar type that doesn't inherit `AttributedType`. https://github.com/llvm/llvm-project/pull/78000 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits