[clang] [WIP] 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/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. --- 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] [WIP] 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/5] [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 3e46a5da3fc043f..c8354fbb108a27d 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 d86c4eba6a22511..25ddfd105ab8776 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 8f2714e142bbe3e..b57ab36939d07c0 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 d4e5310fb3abc60..489644ca5acf869 100644 ---
[clang] [WIP] 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/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. --- 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 3e46a5da3fc043f..c8354fbb108a27d 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 d86c4eba6a22511..25ddfd105ab8776 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 8f2714e142bbe3e..b57ab36939d07c0 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 d4e5310fb3abc60..489644ca5acf869 100644 ---
[clang] [WIP] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 3bbc912d37f03d9ad3be330b81d91c2eaf6c37f2 e65a99a00faba70960e75b4b8edb5acecb675197 -- clang/include/clang/AST/ASTContext.h clang/include/clang/AST/RecursiveASTVisitor.h clang/include/clang/AST/Type.h clang/include/clang/AST/TypeLoc.h clang/include/clang/Parse/Parser.h clang/include/clang/Sema/Sema.h clang/include/clang/Serialization/ASTRecordReader.h clang/include/clang/Serialization/ASTRecordWriter.h clang/lib/AST/ASTContext.cpp clang/lib/AST/ASTImporter.cpp clang/lib/AST/ASTStructuralEquivalence.cpp clang/lib/AST/ItaniumMangle.cpp clang/lib/AST/Type.cpp clang/lib/AST/TypeLoc.cpp clang/lib/AST/TypePrinter.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGExpr.cpp clang/lib/CodeGen/CodeGenFunction.cpp clang/lib/Parse/ParseDecl.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclAttr.cpp clang/lib/Sema/SemaExpr.cpp clang/lib/Sema/SemaType.cpp clang/lib/Sema/TreeTransform.h clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTWriter.cpp clang/tools/libclang/CIndex.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index f88191ef5f..65f2e67fe7 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -3055,10 +3055,8 @@ private: ParsedAttr::Form Form); void ParseBoundsAttribute(IdentifierInfo , -SourceLocation AttrNameLoc, -ParsedAttributes , -IdentifierInfo *ScopeName, -SourceLocation ScopeLoc, +SourceLocation AttrNameLoc, ParsedAttributes , +IdentifierInfo *ScopeName, SourceLocation ScopeLoc, ParsedAttr::Form Form); void ParseTypeofSpecifier(DeclSpec ); diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 027afb5d70..849692079c 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -1341,7 +1341,10 @@ public: /// \brief Describes whether we are in an expression constext which we have /// to handle differently. enum ExpressionKind { - EK_Decltype, EK_TemplateArgument, EK_BoundsAttrArgument, EK_Other + EK_Decltype, + EK_TemplateArgument, + EK_BoundsAttrArgument, + EK_Other } ExprContext; // A context can be nested in both a discarded statement context and @@ -1437,11 +1440,11 @@ public: getCurrentMangleNumberContext(const DeclContext *DC); bool isBoundsAttrArgument() const { -return ExprEvalContexts.back().ExprContext == - ExpressionEvaluationContextRecord::ExpressionKind::EK_BoundsAttrArgument; +return ExprEvalContexts.back().ExprContext == + ExpressionEvaluationContextRecord::ExpressionKind:: + EK_BoundsAttrArgument; } - /// SpecialMemberOverloadResult - The overloading result for a special member /// function. /// @@ -2617,8 +2620,9 @@ public: QualType BuiltinChangeSignedness(QualType BaseType, UTTKind UKind, SourceLocation Loc); - QualType BuildCountAttributedArrayType(QualType WrappedTy, Expr *CountExpr, - const llvm::SmallVector ); + QualType BuildCountAttributedArrayType( + QualType WrappedTy, Expr *CountExpr, + const llvm::SmallVector ); //======// // Symbol table / Decl tracking callbacks: SemaDecl.cpp. diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 1bbccfd47c..83a1e09ebd 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -630,8 +630,8 @@ void Parser::ParseGNUAttributeArgs( ScopeLoc, Form); return; } else if (AttrKind == ParsedAttr::AT_CountedBy) { -ParseBoundsAttribute(*AttrName, AttrNameLoc, Attrs, ScopeName, - ScopeLoc, Form); +ParseBoundsAttribute(*AttrName, AttrNameLoc, Attrs, ScopeName, ScopeLoc, + Form); return; } @@ -3167,11 +3167,14 @@ void Parser::ParseAlignmentSpecifier(ParsedAttributes , /// Bounds attributes (e.g., counted_by): /// AttrName '(' expression ')' -void Parser::ParseBoundsAttribute(IdentifierInfo , SourceLocation AttrNameLoc, ParsedAttributes , IdentifierInfo *ScopeName, - SourceLocation ScopeLoc, - ParsedAttr::Form Form) { +void Parser::ParseBoundsAttribute(IdentifierInfo , + SourceLocation AttrNameLoc, +
[clang] [WIP] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Yeoul Na (rapidsna) Changes In `-fbounds-safety`, bounds annotations are considered type attributes rather than declaration attributes. Constructing them as type attributes allows us to extend the attribute to apply nested pointers, which is essential to annotate functions that involve out parameters: `void foo(int *__counted_by(*out_count) *out_buf, int *out_count)`. We introduce a new sugar type to support bounds annotated types, `CountAttributedType`. In order to maintain extra data (the bounds expression and the dependent declaration information) that is not trackable in `AttributedType` we create a new type dedicate to this functionality. This patch also extends the parsing logic to parse the `counted_by` argument as an expression, which will allow us to extend the model to support arguments beyond an identifier, e.g., `__counted_by(n + m)` in the future as specified by `-fbounds-safety`. This also adjusts `__bdos` and array-bounds sanitizer code that already uses `CountedByAttr` to check `CountAttributedType` instead to get the field referred to by the attribute. --- Patch is 50.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/78000.diff 33 Files Affected: - (modified) clang/include/clang/AST/ASTContext.h (+7) - (modified) clang/include/clang/AST/PropertiesBase.td (+1) - (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+9) - (modified) clang/include/clang/AST/Type.h (+152) - (modified) clang/include/clang/AST/TypeLoc.h (+26) - (modified) clang/include/clang/AST/TypeProperties.td (+19) - (modified) clang/include/clang/Basic/Attr.td (+4-3) - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+10-4) - (modified) clang/include/clang/Basic/TypeNodes.td (+2) - (modified) clang/include/clang/Parse/Parser.h (+7) - (modified) clang/include/clang/Sema/Sema.h (+9-3) - (modified) clang/include/clang/Serialization/ASTRecordReader.h (+2) - (modified) clang/include/clang/Serialization/ASTRecordWriter.h (+5) - (modified) clang/include/clang/Serialization/TypeBitCodes.def (+1) - (modified) clang/lib/AST/ASTContext.cpp (+56) - (modified) clang/lib/AST/ASTImporter.cpp (+12) - (modified) clang/lib/AST/ASTStructuralEquivalence.cpp (+7) - (modified) clang/lib/AST/ItaniumMangle.cpp (+1) - (modified) clang/lib/AST/Type.cpp (+65) - (modified) clang/lib/AST/TypeLoc.cpp (+4) - (modified) clang/lib/AST/TypePrinter.cpp (+23) - (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+1) - (modified) clang/lib/CodeGen/CGExpr.cpp (+8-27) - (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+1) - (modified) clang/lib/Parse/ParseDecl.cpp (+50) - (modified) clang/lib/Sema/SemaDecl.cpp (-6) - (modified) clang/lib/Sema/SemaDeclAttr.cpp (+71-102) - (modified) clang/lib/Sema/SemaExpr.cpp (+21-2) - (modified) clang/lib/Sema/SemaType.cpp (+11) - (modified) clang/lib/Sema/TreeTransform.h (+7) - (modified) clang/lib/Serialization/ASTReader.cpp (+8) - (modified) clang/lib/Serialization/ASTWriter.cpp (+4) - (modified) clang/tools/libclang/CIndex.cpp (+4) ``diff diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3e46a5da3fc043..c8354fbb108a27 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 d86c4eba6a2251..25ddfd105ab877 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 8f2714e142bbe3..b57ab36939d07c 100644 ---
[clang] [WIP] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
github-actions[bot] wrote: Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using `@` followed by their GitHub username. If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the [LLVM GitHub User Guide](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). 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] [WIP] Turn 'counted_by' into a type attribute and parse it into 'CountAttributedType' (PR #78000)
https://github.com/rapidsna created https://github.com/llvm/llvm-project/pull/78000 In `-fbounds-safety`, bounds annotations are considered type attributes rather than declaration attributes. Constructing them as type attributes allows us to extend the attribute to apply nested pointers, which is essential to annotate functions that involve out parameters: `void foo(int *__counted_by(*out_count) *out_buf, int *out_count)`. We introduce a new sugar type to support bounds annotated types, `CountAttributedType`. In order to maintain extra data (the bounds expression and the dependent declaration information) that is not trackable in `AttributedType` we create a new type dedicate to this functionality. This patch also extends the parsing logic to parse the `counted_by` argument as an expression, which will allow us to extend the model to support arguments beyond an identifier, e.g., `__counted_by(n + m)` in the future as specified by `-fbounds-safety`. This also adjusts `__bdos` and array-bounds sanitizer code that already uses `CountedByAttr` to check `CountAttributedType` instead to get the field referred to by the attribute. >From 1a17c254ddf09cd4faf5217b2f72da3f44622f8a 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. --- 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 3e46a5da3fc043..c8354fbb108a27 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 d86c4eba6a2251..25ddfd105ab877 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 8f2714e142bbe3..b57ab36939d07c 100644 ---