From 75be9e4685c686c6c32ea7d310f84e62190caab1 Mon Sep 17 00:00:00 2001 From: zebullon Date: Sun, 13 Oct 2024 20:09:35 +0900 Subject: [PATCH] Stash attributesOf Stash attributesOf --- clang/include/clang/AST/APValue.h | 4 +- .../clang/Basic/DiagnosticMetafnKinds.td | 2 + clang/include/clang/Sema/Sema.h | 1 + clang/lib/AST/APValue.cpp | 5 +- clang/lib/AST/ExprConstantMeta.cpp | 49 +++++-------------- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h index f3ff6f3ff21709..892a7f165a5fc7 100644 --- a/clang/include/clang/AST/APValue.h +++ b/clang/include/clang/AST/APValue.h @@ -32,6 +32,7 @@ template class BasicReaderBase; class AddrLabelExpr; class ASTContext; + class AttributeCommonInfo; class CharUnits; class CXX26AnnotationAttr; class CXXRecordDecl; @@ -40,7 +41,6 @@ template class BasicReaderBase; class Expr; class FieldDecl; class NamespaceDecl; - class ParsedAttr; struct PrintingPolicy; class Type; class ValueDecl; @@ -683,7 +683,7 @@ class APValue { CXXBaseSpecifier *getReflectedBaseSpecifier() const; TagDataMemberSpec *getReflectedDataMemberSpec() const; CXX26AnnotationAttr *getReflectedAnnotation() const; - ParsedAttr *getReflectedAttribute() const; + AttributeCommonInfo *getReflectedAttribute() const; void setInt(APSInt I) { assert(isInt() && "Invalid accessor"); diff --git a/clang/include/clang/Basic/DiagnosticMetafnKinds.td b/clang/include/clang/Basic/DiagnosticMetafnKinds.td index 3cc79bbbfabe9c..2c13309857b3ef 100644 --- a/clang/include/clang/Basic/DiagnosticMetafnKinds.td +++ b/clang/include/clang/Basic/DiagnosticMetafnKinds.td @@ -90,6 +90,8 @@ def metafn_result_not_representable : Note< "provided %select{value|object}0 cannot be represented by a reflection">; def metafn_p3385_trace_execution_checkpoint : Note< "(p3385-metafn) trace_execution_checkpoint %0">; +def metafn_p3385_non_standard_attribute : Warning< + "(p3385-metafn) Non standard attribute discovered %0">; // Class definition. def metafn_name_invalid_identifier : Note< diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 79124386f7fd33..e6de857724d7b0 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -35,6 +35,7 @@ #include "clang/AST/Type.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/AttrSubjectMatchRules.h" +#include "clang/Basic/AttributeCommonInfo.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/CapturedStmt.h" #include "clang/Basic/Cuda.h" diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index 6a68b888c6d509..decb53c9f03caa 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -22,6 +22,7 @@ #include "clang/AST/ExprCXX.h" #include "clang/AST/LocInfoType.h" #include "clang/AST/Type.h" +#include "clang/Basic/AttributeCommonInfo.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" using namespace clang; @@ -943,10 +944,10 @@ CXX26AnnotationAttr *APValue::getReflectedAnnotation() const { const_cast(getOpaqueReflectionData())); } -ParsedAttr *APValue::getReflectedAttribute() const { +AttributeCommonInfo *APValue::getReflectedAttribute() const { assert(getReflectionKind() == ReflectionKind::Attribute && "not a reflection of an attribute"); - return reinterpret_cast( + return reinterpret_cast( const_cast(getOpaqueReflectionData())); } diff --git a/clang/lib/AST/ExprConstantMeta.cpp b/clang/lib/AST/ExprConstantMeta.cpp index 2032233d2edc4e..84252f8a41735f 100644 --- a/clang/lib/AST/ExprConstantMeta.cpp +++ b/clang/lib/AST/ExprConstantMeta.cpp @@ -28,7 +28,6 @@ #include "clang/Basic/SourceManager.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/Preprocessor.h" -#include "clang/Sema/ParsedAttr.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" @@ -769,7 +768,7 @@ static APValue makeReflection(CXX26AnnotationAttr *A) { return APValue(ReflectionKind::Annotation, A); } -static APValue makeReflection(ParsedAttr *Attr) { +static APValue makeReflection(AttributeCommonInfo *Attr) { return APValue(ReflectionKind::Attribute, Attr); } @@ -1444,13 +1443,6 @@ bool DiagnoseReflectionKind(DiagFn Diagnoser, SourceRange Range, // Metafunction implementations // ----------------------------------------------------------------------------- -// FIXME Reconciliate Attr and Attribute by just storing AttributeCommonInfo -struct AttributeScratchpad { - AttributeFactory factory; - ParsedAttributes attributes; - AttributeScratchpad() : factory(), attributes(factory) {} -}; - bool get_ith_attribute_of(APValue &Result, ASTContext &C, MetaActions &Meta, EvalFn Evaluator, DiagFn Diagnoser, QualType ResultTy, @@ -1458,8 +1450,6 @@ bool get_ith_attribute_of(APValue &Result, ASTContext &C, assert(Args[0]->getType()->isReflectionType()); assert(ResultTy == C.MetaInfoTy); - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of"; - APValue RV; if (!Evaluator(RV, Args[0], true)) return true; @@ -1476,48 +1466,33 @@ bool get_ith_attribute_of(APValue &Result, ASTContext &C, switch (RV.getReflectionKind()) { case ReflectionKind::Attribute: { - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case attribute"; // We don't allow ^^[[ attribute, attribute]], so when reflected type is // attribute idx must be 0 if (idx == 0) { - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case attribute idx = 0"; - ParsedAttr *Attr = RV.getReflectedAttribute(); - return SetAndSucceed(Result, makeReflection(Attr)); + AttributeCommonInfo *attr = RV.getReflectedAttribute(); + if (attr->getForm().getSyntax() == AttributeCommonInfo::Syntax::AS_CXX11) { + return SetAndSucceed(Result, makeReflection(attr)); + } + Diagnoser(Range.getBegin(), diag::metafn_p3385_non_standard_attribute) << attr->getAttrName(); } - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case attribute idx > 0"; return SetAndSucceed(Result, Sentinel); } case ReflectionKind::Declaration: { - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case declaration"; Decl *D = RV.getReflectedDecl(); auto attrs = C.getDeclAttrs(D); if (attrs.empty()) { - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case declaration, empty "; return SetAndSucceed(Result, Sentinel); } if (idx == attrs.size()) { - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case declaration, finished "; return SetAndSucceed(Result, Sentinel); } - Diagnoser(Range.getBegin(), diag::metafn_p3385_trace_execution_checkpoint) << "get_ith_attribute_of : case declaration, ith "; Attr* attr = attrs[idx]; - static AttributeScratchpad scratchpad; - - if (attr->getForm().getSyntax() != AttributeCommonInfo::Syntax::AS_CXX11) { - // FIXME Filter them instead of erroring - return DiagnoseReflectionKind(Diagnoser, Range, "a standard CXX11 attribute", - DescriptionOf(RV)); - } - scratchpad.attributes.addNew( - const_cast(attr->getAttrName()), // FIXME... - attr->getRange(), - nullptr, - attr->getLocation(), - nullptr, nullptr, nullptr, - attr->getForm()); - - return SetAndSucceed(Result, makeReflection(&scratchpad.attributes[0])); + if (attr->getForm().getSyntax() == AttributeCommonInfo::Syntax::AS_CXX11) { + return SetAndSucceed(Result, makeReflection(attr)); + } + Diagnoser(Range.getBegin(), diag::metafn_p3385_non_standard_attribute) << attr->getAttrName(); + return SetAndSucceed(Result, Sentinel); } case ReflectionKind::Type: case ReflectionKind::Null: @@ -1996,7 +1971,7 @@ bool identifier_of(APValue &Result, ASTContext &C, MetaActions &Meta, break; } case ReflectionKind::Attribute: { - ParsedAttr* attr = RV.getReflectedAttribute(); + AttributeCommonInfo* attr = RV.getReflectedAttribute(); Name = attr->getAttrName()->getName(); break; }