[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] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
delcypher wrote: @nathanchance Thanks for reporting this. I'm going to have a quick go at reproducing this to see if fixing this is straight forward. If it is not I will revert this PR and then we can re-land this change with the problem you reported fixed. 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)
nathanchance wrote: I see a crash from an unreachable statement while building the Linux kernel with debug info enabled after this change. A trivial reproducer from `cvise`: ```c struct { int num_counters; long value[] __attribute__((__counted_by__(num_counters))); } agent_send_response_port_num; ``` ``` $ clang -g -c -o /dev/null agent.i type should have been unwrapped! UNREACHABLE executed at /mnt/nvme/tmp/cvise.DVoC71ctTu/src/clang/lib/CodeGen/CGDebugInfo.cpp:3681! PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: clang -g -c -o /dev/null agent.i 1. parser at end of file #0 0x564a42c9f028 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x534c028) #1 0x564a42c9cc6e llvm::sys::RunSignalHandlers() (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5349c6e) #2 0x564a42c1f526 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0 #3 0x7f91646c5770 (/usr/lib/libc.so.6+0x3c770) #4 0x7f916471632c (/usr/lib/libc.so.6+0x8d32c) #5 0x7f91646c56c8 raise (/usr/lib/libc.so.6+0x3c6c8) #6 0x7f91646ad4b8 abort (/usr/lib/libc.so.6+0x244b8) #7 0x564a42c2522f (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x52d222f) #8 0x564a42f3ccf1 (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e9cf1) #9 0x564a42f2aeab clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55d7eab) #10 0x564a42f31090 clang::CodeGen::CGDebugInfo::createFieldType(llvm::StringRef, clang::QualType, clang::SourceLocation, clang::AccessSpecifier, unsigned long, unsigned int, llvm::DIFile*, llvm::DIScope*, clang::RecordDecl const*, llvm::MDTupleTypedArrayWrapper) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55de090) #11 0x564a42f31dd2 clang::CodeGen::CGDebugInfo::CollectRecordNormalField(clang::FieldDecl const*, unsigned long, llvm::DIFile*, llvm::SmallVectorImpl&, llvm::DIType*, clang::RecordDecl const*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55dedd2) #12 0x564a42f322f8 clang::CodeGen::CGDebugInfo::CollectRecordFields(clang::RecordDecl const*, llvm::DIFile*, llvm::SmallVectorImpl&, llvm::DICompositeType*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55df2f8) #13 0x564a42f3765d clang::CodeGen::CGDebugInfo::CreateTypeDefinition(clang::RecordType const*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e465d) #14 0x564a42f37b3d clang::CodeGen::CGDebugInfo::CreateType(clang::RecordType const*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e4b3d) #15 0x564a42f3ca03 clang::CodeGen::CGDebugInfo::CreateTypeNode(clang::QualType, llvm::DIFile*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55e9a03) #16 0x564a42f2aeab clang::CodeGen::CGDebugInfo::getOrCreateType(clang::QualType, llvm::DIFile*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55d7eab) #17 0x564a42f475b4 clang::CodeGen::CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable*, clang::VarDecl const*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x55f45b4) #18 0x564a42ea15fd clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x554e5fd) #19 0x564a42ea2f30 clang::CodeGen::CodeGenModule::EmitTentativeDefinition(clang::VarDecl const*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x554ff30) #20 0x564a44895bd8 clang::Sema::ActOnEndOfTranslationUnit() (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6f42bd8) #21 0x564a4474bf8e clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6df8f8e) #22 0x564a447468be clang::ParseAST(clang::Sema&, bool, bool) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x6df38be) #23 0x564a438624af clang::FrontendAction::Execute() (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5f0f4af) #24 0x564a437d6efd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5e83efd) #25 0x564a43929a58 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x5fd6a58) #26 0x564a40e6688f cc1_main(llvm::ArrayRef, char const*, void*) (/home/nathan/Dev/tmp/cvise.DVoC71ctTu/install/llvm-bad/bin/clang-19+0x351388f) #27
[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 @@
[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/AaronBallman approved this pull request. Thank you for this, it's exciting to see more bounds safety being added to Clang! I think everything looks good to me (we can clean up any surprises in follow-ups at this point). I'm presuming you'll add a release note & docs once more of the functionality is 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/yinying-lisa-li 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/13] [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 @@
[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 @@
[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 @@
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/Endilll commented: `Sema.h` changes look good to me. 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: @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 @@
[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 +++
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
Endilll wrote: Can you resolve merge conflicts, so that I can review changes to `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 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 +++
[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 , 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 , 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 , }); } +template +QualType TreeTransform::TransformCountAttributedType( +TypeLocBuilder , 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 +++
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/erichkeane commented: No additional comments from me, if the other reviewers are happy, I'm happy with where this is/is going. 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. AaronBallman wrote: I think `NumTypesBits` is 19 (it's not based on the underlying type of the enum, `llvm::BitWidth` is clever there and uses the maximal enum value to calculate the number of bits needed), this structure adds 6 more bits, so that's up to 25 bits. I would expect that means we have 7 bits leftover so we keep this to a 32-bit value. 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 , SourceLocation Loc) { +// nothing to do + } + unsigned getLocalDataAlignment() const { return 1; } + unsigned getLocalDataSize() const { return 0; } AaronBallman wrote: Do we need to implement these at all? `ConcreteTypeLoc` should take care of that automatically I thought? 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 , }); } +template +QualType TreeTransform::TransformCountAttributedType( +TypeLocBuilder , CountAttributedTypeLoc TL) { + // TODO + llvm_unreachable("Unexpected TreeTransform for CountAttributedType"); AaronBallman wrote: Hmmm this might be problematic sooner rather than later; the attribute is only allowed in C mode so we don't have to worry about needing this for template instantiation, but we do use `TreeTransform` in C for OpenMP: https://github.com/llvm/llvm-project/blob/d2a9df2c8ffd21fd52fbd8199a191d10078f41af/clang/lib/Sema/SemaOpenMP.cpp#L5591 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)
@@ -1136,38 +1136,19 @@ llvm::Value *CodeGenFunction::EmitCountedByFieldExpr( } const FieldDecl *CodeGenFunction::FindCountedByField(const FieldDecl *FD) { - if (!FD || !FD->hasAttr()) + if (!FD) return nullptr; - const auto *CBA = FD->getAttr(); - if (!CBA) + auto *CAT = FD->getType()->getAs(); AaronBallman wrote: ```suggestion const auto *CAT = FD->getType()->getAs(); ``` Same edits elsewhere in this function. 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()) { AaronBallman wrote: ```suggestion isa(ME->getMemberDecl()->getType()) { ``` 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 , 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(); AaronBallman wrote: You should spell these types out (same below) because the type is not in the initializer. 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)
@@ -385,6 +385,26 @@ void DependentBitIntType::Profile(llvm::FoldingSetNodeID , NumBitsExpr->Profile(ID, Context, true); } +bool BoundsAttributedType::referencesFieldDecls() const { + for (const auto : dependent_decls()) +if (isa(Decl.getDecl())) + return true; + return false; AaronBallman wrote: ```suggestion return llvm::any_of(dependent_decls(), [](const TypeCoupledDeclRefInfo ) { return isa(Info.getDecl()); }); ``` Needs reformatting, but something like this should be a more clear equivalent. 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/AaronBallman commented: Thank you for your patience! Overall, I don't spot any major concerns, but I did spot some minor nits. I've added @erichkeane to the review as attributes code owner for a second set of eyes on those changes specifically. 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/AaronBallman edited 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. hnrklssn wrote: Why is the limit 15? From what I can tell `NumTypeBits` is 22, so with `CountInBytes` and `OrNull` that's 24 bits, which makes 15 seem very arbitrary. How about a static assert that `sizeof(CountAttributedTypeBitfields)` is less than or equal to some maximum, if that's the goal here. 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: 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 +++
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
@@ -8463,133 +8463,115 @@ static void handleZeroCallUsedRegsAttr(Sema , Decl *D, const ParsedAttr ) { D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL)); } -static void handleCountedByAttr(Sema , Decl *D, const ParsedAttr ) { - 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 +++
[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 +++
[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
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
AaronBallman wrote: > > One possibility would be to use a type qualifier? It has basically the kind > > of properties you want... the underlying type is > > still there, and places that strip qualifiers will automatically do the > > right thing in a lot of cases. It might require a bit more > > work to handle it in various places in semantic analysis, though. > > Thanks @efriedma-quic! We also thought about using qualifiers before but it's > tricky because we would like the bounds annotations to be able to apply to > function return types. WDYT? To clarify the problem, qualifiers are stripped from the return type in C when forming the function type itself (see C23 6.7.6.3p4). > We haven't experimented these alternative designs so I'm curious about > @AaronBallman's thoughts. I think that encoding this into `Qualifiers` would be difficult because that's a 32-bit opaque value and I think counted_by needs to store more information than we have room for: https://github.com/llvm/llvm-project/blob/b0c6fc81fe132f20eed38bd836620dfcb5ac17e9/clang/include/clang/AST/Type.h#L147 I think `AttributedType` might be a reasonable way forward. That's how we model nullability qualifiers, for example: https://github.com/llvm/llvm-project/blob/0aacd44a4698da012ee0704815123b28f2a06d0f/clang/lib/AST/Type.cpp#L4514 and https://github.com/llvm/llvm-project/blob/b0c6fc81fe132f20eed38bd836620dfcb5ac17e9/clang/include/clang/AST/Type.h#L5189 and counted_by feels roughly similar in terms of behavior (not able to overload based on presence/absence of the attribute, etc). CC @erichkeane as attributes code owner. 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: FWIW, in the context of `-fbounds-safety` (https://github.com/llvm/llvm-project/blob/main/clang/docs/BoundsSafety.rst), I think the best is to have the bounds-annotated types to be part of the canonical type system because they affect the semantics and the codegen (again with `-fbounds-safety`) and the annotations should also apply to rvalues (e.g., function return types). That said, sugar types happen to work fine overall because where the compiler is about to lose the type sugar (e.g., assigning it to a variable with type that doesn't have the same attribute), the model naturally prevented it by either emitting an error for incompatible type conversion, or promoting the type into a wide pointer type (i.e., `__bidi_indexable` for lvalue-to-rvalue conversion on `__counted_by`). That being said, we still encountered some cases where the type sugars are lost inconsistently due to implementation issues (e.g., getting unqualified type also removes the type sugar depending on the position it appertains to). So again, ideally, I like it to be part of the type system. However, without `-fbounds-safety`, `__counted_by` is merely used as a helper for sanitizers and the `__builtin_dynamic_object_size`, so it would make more sense to be a sugar type. Then, the question is whether we want the attribute to be parsed different with or without `-fbounds-safety` and would that be a problem. 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: > One possibility would be to use a type qualifier? It has basically the kind > of properties you want... the underlying type is > still there, and places > that strip qualifiers will automatically do the right thing in a lot of > cases. It might require a bit more > work to handle it in various places in > semantic analysis, though. Thanks @efriedma-quic! We also thought about using qualifiers before but it's tricky because we would like the bounds annotations to be able to apply to function return types and parameters. WDYT? 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)
@@ -2917,6 +2942,133 @@ class PointerType : public Type, public llvm::FoldingSetNode { static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } }; +/// [BoundsSafety] Represents information of declarations referenced by the +/// arguments of the `counted_by` attribute and the likes. +class TypeCoupledDeclRefInfo { +public: + using BaseTy = llvm::PointerIntPair; + +private: + enum { +DerefShift = 0, +DerefMask = 1, + }; + BaseTy Data; + +public: + /// \p D is to a declaration referenced by the argument of attribute. \p Deref + /// indicates whether \p D is referenced as a dereferenced form, e.g., \p + /// Deref is true for `*n` in `int *__counted_by(*n)`. + TypeCoupledDeclRefInfo(ValueDecl *D = nullptr, bool Deref = false); + + bool isDeref() const; + ValueDecl *getDecl() const; + unsigned getInt() const; + void *getOpaqueValue() const; + bool operator==(const TypeCoupledDeclRefInfo ) const; + void setFromOpaqueValue(void *V); +}; + +/// [BoundsSafety] Represents a parent type class for CountAttributedType and +/// similar sugar types that will be introduced to represent a type with a +/// bounds attribute. +/// +/// Provides a common interface to navigate declarations referred to by the +/// bounds expression. + +class BoundsAttributedType : public Type, public llvm::FoldingSetNode { + QualType WrappedTy; + +protected: + ArrayRef Decls; // stored in trailing objects + + BoundsAttributedType(TypeClass TC, QualType Wrapped, QualType Canon); + +public: + bool isSugared() const { return true; } + QualType desugar() const { return WrappedTy; } + + using decl_iterator = const TypeCoupledDeclRefInfo *; + using decl_range = llvm::iterator_range; + + decl_iterator dependent_decl_begin() const { return Decls.begin(); } + decl_iterator dependent_decl_end() const { return Decls.end(); } + + unsigned getNumCoupledDecls() const { return Decls.size(); } + + decl_range dependent_decls() const { +return decl_range(dependent_decl_begin(), dependent_decl_end()); + } + + ArrayRef getCoupledDecls() const { +return {dependent_decl_begin(), dependent_decl_end()}; + } + + bool referencesFieldDecls() const; + + static bool classof(const Type *T) { +switch (T->getTypeClass()) { rapidsna wrote: @bwendling I wrote this way as we will extend it to support other bounds attribute as well. I'll add a comment there. 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 edited 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)
@@ -8463,133 +8463,110 @@ static void handleZeroCallUsedRegsAttr(Sema , Decl *D, const ParsedAttr ) { D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL)); } -static void handleCountedByAttr(Sema , Decl *D, const ParsedAttr ) { - 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(); + while (RD && (RD->isAnonymousStructOrUnion() || RD->getName().empty())) { rapidsna wrote: > Are you sure this is processing after it's set? @bwendling No, it doesn't, but I was initially meant to find the other struct that was already parsed like below. ``` struct Parent { struct { int count; }; // anonymous struct int fam[__counted_by(count)]; // Sema is here } ; ``` But now I'm looking at it, the logic needs to be fixed because it still doesn't tell us apart from: ``` struct Parent { struct { int count; }; p // unnamed but non-anonymous struct int fam[__counted_by(count)]; // Sema is here } ; ``` I'll fix that. 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)
efriedma-quic wrote: One possibility would be to use a type qualifier? It has basically the kind of properties you want... the underlying type is still there, and places that strip qualifiers will automatically do the right thing in a lot of cases. It might require a bit more work to handle it in various places in semantic analysis, though. > `IncompleteArrayType` is `final` I don't see any such marking? I think the only reason some types are marked `final` is to ensure that TrailingObjects works correctly. 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: > It's generally not a good idea to use sugar to represent constructs that are > semantically significant: anything that uses the canonical type will throw it > away. We try to avoid throwing away sugar in most cases, but we aren't always > successful. > It's possible there are other considerations here (maybe it matters less if > the attribute is only relevant inside the definition?), but I'd like an > explanation of the tradeoffs. Thanks @efriedma-quic! Yes, these are really good points. The main reason that we decided it to be a sugar type was that this type of attribute doesn't change the shape of the underlying type so we wanted such as `isa(BoundAttributedTy)` or `isa(BoundAttributedTy)` still holds true depending on the underlying canonical type (in the current implementation the attribute only applies to array type but it will be extended to support pointer type as well). And we figured that adding a sugar type might less disruptive to the rest of Clang code base compared to extending the existing Clang types. But as you pointed out we encountered cases where the sugar wasn't preserved as it needed to be so we had to specially handle such cases in our own implementation, so we're open for other suggestions. An alternative design could be to create new canonical types that inherit `IncompleteArrayType` or `PointerType` respectively so `isa` still holds, but unfortunately `IncompleteArrayType` is `final`. We could also add an additional field in the `IncompleteArrayType` class to contain the optional `count` expression, then the question is what we want to do for `ConstantArrayType` later when we like to have it count attribute as well (this could be potentially relevant for flexible array member-like array member with a constant size). Similarly, we could add more fields to `PointerType` to contain the optional count expression (or upper bound expression) and necessary data. For this, we should be careful not to grow the size of `PointerType` by default, possibly by adding additional data as `TrailingObject`. We haven't experimented these alternative designs so I'm curious about @AaronBallman's thoughts. 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)
efriedma-quic wrote: It's generally not a good idea to use sugar to represent constructs that are semantically significant: anything that uses the canonical type will throw it away. We try to avoid throwing away sugar in most cases, but we aren't always successful. It's possible there are other considerations here (maybe it matters less if the attribute is only relevant inside the definition?), but I'd like an explanation of the tradeoffs. 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: @AaronBallman @bwendling I'd appreciate your feedback! 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/2] [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 12ce9af1e53f6..08795af245c06 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 0270c086d06b6..6df1d93a7ba2e 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 9da5206a21c34..b058e0435912e 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna ready_for_review 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] [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 12ce9af1e53f6..08795af245c06 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 0270c086d06b6..6df1d93a7ba2e 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 9da5206a21c34..b058e0435912e 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@
[clang-tools-extra] [llvm] [clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna converted_to_draft 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
[llvm] [clang-tools-extra] [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 1a17c254ddf09cd4faf5217b2f72da3f44622f8a Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/13] [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. --- 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/TypeNodes.td| 2 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 1 + clang/lib/AST/ASTContext.cpp | 56 +++ clang/lib/AST/ASTImporter.cpp | 12 ++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 57 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 23 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Sema/SemaExpr.cpp | 1 + clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/tools/libclang/CIndex.cpp | 4 + 24 files changed, 410 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc04..c8354fbb108a2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -247,6 +247,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1338,6 +1340,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 d86c4eba6a225..25ddfd105ab87 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 8f2714e142bbe..b57ab36939d07 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1099,6 +1099,12 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) DEF_TRAVERSE_TYPE(AttributedType, { TRY_TO(TraverseType(T->getModifiedType())); }) +DEF_TRAVERSE_TYPE(CountAttributedType, { + if (T->getCountExpr()) +TRY_TO(TraverseStmt(T->getCountExpr())); + TRY_TO(TraverseType(T->desugar())); +}) + DEF_TRAVERSE_TYPE(BTFTagAttributedType, { TRY_TO(TraverseType(T->getWrappedType())); }) @@ -1385,6 +1391,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType, DEF_TRAVERSE_TYPELOC(AttributedType, { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) +DEF_TRAVERSE_TYPELOC(CountAttributedType, + { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) + DEF_TRAVERSE_TYPELOC(BTFTagAttributedType, { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); }) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d4e5310fb3abc..489644ca5acf8 100644 ---
[llvm] [clang-tools-extra] [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 1a17c254ddf09cd4faf5217b2f72da3f44622f8a Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/12] [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. --- 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/TypeNodes.td| 2 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 1 + clang/lib/AST/ASTContext.cpp | 56 +++ clang/lib/AST/ASTImporter.cpp | 12 ++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 57 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 23 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Sema/SemaExpr.cpp | 1 + clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/tools/libclang/CIndex.cpp | 4 + 24 files changed, 410 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc04..c8354fbb108a2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -247,6 +247,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1338,6 +1340,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 d86c4eba6a225..25ddfd105ab87 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 8f2714e142bbe..b57ab36939d07 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1099,6 +1099,12 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) DEF_TRAVERSE_TYPE(AttributedType, { TRY_TO(TraverseType(T->getModifiedType())); }) +DEF_TRAVERSE_TYPE(CountAttributedType, { + if (T->getCountExpr()) +TRY_TO(TraverseStmt(T->getCountExpr())); + TRY_TO(TraverseType(T->desugar())); +}) + DEF_TRAVERSE_TYPE(BTFTagAttributedType, { TRY_TO(TraverseType(T->getWrappedType())); }) @@ -1385,6 +1391,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType, DEF_TRAVERSE_TYPELOC(AttributedType, { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) +DEF_TRAVERSE_TYPELOC(CountAttributedType, + { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) + DEF_TRAVERSE_TYPELOC(BTFTagAttributedType, { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); }) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d4e5310fb3abc..489644ca5acf8 100644 ---
[llvm] [clang-tools-extra] [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 1a17c254ddf09cd4faf5217b2f72da3f44622f8a Mon Sep 17 00:00:00 2001 From: Yeoul Na Date: Mon, 18 Dec 2023 10:58:16 +0900 Subject: [PATCH 01/12] [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. --- 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/TypeNodes.td| 2 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 1 + clang/lib/AST/ASTContext.cpp | 56 +++ clang/lib/AST/ASTImporter.cpp | 12 ++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 57 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 23 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Sema/SemaExpr.cpp | 1 + clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/tools/libclang/CIndex.cpp | 4 + 24 files changed, 410 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc04..c8354fbb108a2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -247,6 +247,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1338,6 +1340,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 d86c4eba6a225..25ddfd105ab87 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 8f2714e142bbe..b57ab36939d07 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1099,6 +1099,12 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) DEF_TRAVERSE_TYPE(AttributedType, { TRY_TO(TraverseType(T->getModifiedType())); }) +DEF_TRAVERSE_TYPE(CountAttributedType, { + if (T->getCountExpr()) +TRY_TO(TraverseStmt(T->getCountExpr())); + TRY_TO(TraverseType(T->desugar())); +}) + DEF_TRAVERSE_TYPE(BTFTagAttributedType, { TRY_TO(TraverseType(T->getWrappedType())); }) @@ -1385,6 +1391,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType, DEF_TRAVERSE_TYPELOC(AttributedType, { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) +DEF_TRAVERSE_TYPELOC(CountAttributedType, + { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) + DEF_TRAVERSE_TYPELOC(BTFTagAttributedType, { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); }) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d4e5310fb3abc..489644ca5acf8 100644 ---
[clang-tools-extra] [llvm] [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 1a17c254ddf09cd4faf5217b2f72da3f44622f8a 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. --- 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/TypeNodes.td| 2 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 1 + clang/lib/AST/ASTContext.cpp | 56 +++ clang/lib/AST/ASTImporter.cpp | 12 ++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 57 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 23 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Sema/SemaExpr.cpp | 1 + clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/tools/libclang/CIndex.cpp | 4 + 24 files changed, 410 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc04..c8354fbb108a2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -247,6 +247,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1338,6 +1340,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 d86c4eba6a225..25ddfd105ab87 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 8f2714e142bbe..b57ab36939d07 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1099,6 +1099,12 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) DEF_TRAVERSE_TYPE(AttributedType, { TRY_TO(TraverseType(T->getModifiedType())); }) +DEF_TRAVERSE_TYPE(CountAttributedType, { + if (T->getCountExpr()) +TRY_TO(TraverseStmt(T->getCountExpr())); + TRY_TO(TraverseType(T->desugar())); +}) + DEF_TRAVERSE_TYPE(BTFTagAttributedType, { TRY_TO(TraverseType(T->getWrappedType())); }) @@ -1385,6 +1391,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType, DEF_TRAVERSE_TYPELOC(AttributedType, { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) +DEF_TRAVERSE_TYPELOC(CountAttributedType, + { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) + DEF_TRAVERSE_TYPELOC(BTFTagAttributedType, { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); }) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d4e5310fb3abc..489644ca5acf8 100644 ---
[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 1a17c254ddf09cd4faf5217b2f72da3f44622f8a 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. --- 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/TypeNodes.td| 2 + .../clang/Serialization/ASTRecordReader.h | 2 + .../clang/Serialization/ASTRecordWriter.h | 5 + .../clang/Serialization/TypeBitCodes.def | 1 + clang/lib/AST/ASTContext.cpp | 56 +++ clang/lib/AST/ASTImporter.cpp | 12 ++ clang/lib/AST/ASTStructuralEquivalence.cpp| 7 + clang/lib/AST/ItaniumMangle.cpp | 1 + clang/lib/AST/Type.cpp| 57 +++ clang/lib/AST/TypeLoc.cpp | 4 + clang/lib/AST/TypePrinter.cpp | 23 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 1 + clang/lib/Sema/SemaExpr.cpp | 1 + clang/lib/Sema/TreeTransform.h| 7 + clang/lib/Serialization/ASTReader.cpp | 8 + clang/lib/Serialization/ASTWriter.cpp | 4 + clang/tools/libclang/CIndex.cpp | 4 + 24 files changed, 410 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc04..c8354fbb108a2 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -247,6 +247,8 @@ class ASTContext : public RefCountedBase { DependentBitIntTypes; llvm::FoldingSet BTFTagAttributedTypes; + mutable llvm::FoldingSet CountAttributedTypes; + mutable llvm::FoldingSet QualifiedTemplateNames; mutable llvm::FoldingSet DependentTemplateNames; mutable llvm::FoldingSet @@ -1338,6 +1340,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 d86c4eba6a225..25ddfd105ab87 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 8f2714e142bbe..b57ab36939d07 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -1099,6 +1099,12 @@ DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) DEF_TRAVERSE_TYPE(AttributedType, { TRY_TO(TraverseType(T->getModifiedType())); }) +DEF_TRAVERSE_TYPE(CountAttributedType, { + if (T->getCountExpr()) +TRY_TO(TraverseStmt(T->getCountExpr())); + TRY_TO(TraverseType(T->desugar())); +}) + DEF_TRAVERSE_TYPE(BTFTagAttributedType, { TRY_TO(TraverseType(T->getWrappedType())); }) @@ -1385,6 +1391,9 @@ DEF_TRAVERSE_TYPELOC(MacroQualifiedType, DEF_TRAVERSE_TYPELOC(AttributedType, { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) +DEF_TRAVERSE_TYPELOC(CountAttributedType, + { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) + DEF_TRAVERSE_TYPELOC(BTFTagAttributedType, { TRY_TO(TraverseTypeLoc(TL.getWrappedLoc())); }) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index d4e5310fb3abc..489644ca5acf8 100644 ---
[clang] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna edited 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