Skip to content

Commit 41a94de

Browse files
authored
[clang] Refactor attr diagnostics to use %select (#122473)
A cleanup follow-up to #118501 and #118567.
1 parent f459819 commit 41a94de

File tree

8 files changed

+39
-21
lines changed

8 files changed

+39
-21
lines changed

clang/examples/Attribute/Attribute.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
4242
const Decl *D) const override {
4343
// This attribute appertains to functions only.
4444
if (!isa<FunctionDecl>(D)) {
45-
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
46-
<< Attr << Attr.isRegularKeywordAttribute() << "functions";
45+
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
46+
<< Attr << Attr.isRegularKeywordAttribute() << ExpectedFunction;
4747
return false;
4848
}
4949
return true;
@@ -99,8 +99,9 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
9999
const Stmt *St) const override {
100100
// This attribute appertains to for loop statements only.
101101
if (!isa<ForStmt>(St)) {
102-
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
103-
<< Attr << Attr.isRegularKeywordAttribute() << "for loop statements";
102+
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
103+
<< Attr << Attr.isRegularKeywordAttribute()
104+
<< ExpectedForLoopStatement;
104105
return false;
105106
}
106107
return true;

clang/examples/CallSuperAttribute/CallSuperAttrInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ struct CallSuperAttrInfo : public ParsedAttrInfo {
168168
const Decl *D) const override {
169169
const auto *TheMethod = dyn_cast_or_null<CXXMethodDecl>(D);
170170
if (!TheMethod || !TheMethod->isVirtual()) {
171-
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
172-
<< Attr << Attr.isRegularKeywordAttribute() << "virtual functions";
171+
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
172+
<< Attr << Attr.isRegularKeywordAttribute()
173+
<< ExpectedVirtualFunction;
173174
return false;
174175
}
175176
MarkedMethods.insert(TheMethod);

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3799,7 +3799,14 @@ def warn_attribute_wrong_decl_type : Warning<
37993799
"|types and namespaces"
38003800
"|variables, functions and classes"
38013801
"|kernel functions"
3802-
"|non-K&R-style functions}2">,
3802+
"|non-K&R-style functions"
3803+
"|for loop statements"
3804+
"|virtual functions"
3805+
"|parameters and implicit object parameters"
3806+
"|non-member functions"
3807+
"|functions, classes, or enumerations"
3808+
"|classes"
3809+
"|typedefs}2">,
38033810
InGroup<IgnoredAttributes>;
38043811
def err_attribute_wrong_decl_type : Error<warn_attribute_wrong_decl_type.Summary>;
38053812
def warn_type_attribute_wrong_type : Warning<

clang/include/clang/Sema/ParsedAttr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,13 @@ enum AttributeDeclKind {
10991099
ExpectedFunctionVariableOrClass,
11001100
ExpectedKernelFunction,
11011101
ExpectedFunctionWithProtoType,
1102+
ExpectedForLoopStatement,
1103+
ExpectedVirtualFunction,
1104+
ExpectedParameterOrImplicitObjectParameter,
1105+
ExpectedNonMemberFunction,
1106+
ExpectedFunctionOrClassOrEnum,
1107+
ExpectedClass,
1108+
ExpectedTypedef,
11021109
};
11031110

11041111
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,

clang/lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/Parse/RAIIObjectsForParser.h"
2525
#include "clang/Sema/EnterExpressionEvaluationContext.h"
2626
#include "clang/Sema/Lookup.h"
27+
#include "clang/Sema/ParsedAttr.h"
2728
#include "clang/Sema/ParsedTemplate.h"
2829
#include "clang/Sema/Scope.h"
2930
#include "clang/Sema/SemaCUDA.h"
@@ -3708,9 +3709,9 @@ void Parser::ParseDeclarationSpecifiers(
37083709
continue;
37093710

37103711
if (PA.getKind() == ParsedAttr::AT_LifetimeBound)
3711-
Diag(PA.getLoc(), diag::err_attribute_wrong_decl_type_str)
3712+
Diag(PA.getLoc(), diag::err_attribute_wrong_decl_type)
37123713
<< PA << PA.isRegularKeywordAttribute()
3713-
<< "parameters and implicit object parameters";
3714+
<< ExpectedParameterOrImplicitObjectParameter;
37143715
else
37153716
Diag(PA.getLoc(), diag::err_attribute_not_type_attr)
37163717
<< PA << PA.isRegularKeywordAttribute();

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,8 +1868,8 @@ static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
18681868
// This form is not allowed to be written on a member function (static or
18691869
// nonstatic) when in Microsoft compatibility mode.
18701870
if (S.getLangOpts().MSVCCompat && isa<CXXMethodDecl>(D)) {
1871-
S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
1872-
<< AL << AL.isRegularKeywordAttribute() << "non-member functions";
1871+
S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
1872+
<< AL << AL.isRegularKeywordAttribute() << ExpectedNonMemberFunction;
18731873
return;
18741874
}
18751875
}
@@ -2761,9 +2761,9 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
27612761
// The standard attribute cannot be applied to variable declarations such
27622762
// as a function pointer.
27632763
if (isa<VarDecl>(D))
2764-
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
2764+
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
27652765
<< AL << AL.isRegularKeywordAttribute()
2766-
<< "functions, classes, or enumerations";
2766+
<< ExpectedFunctionOrClassOrEnum;
27672767

27682768
// If this is spelled as the standard C++17 attribute, but not in C++17,
27692769
// warn about using it as an extension. If there are attribute arguments,
@@ -5555,8 +5555,8 @@ static void handleNullableTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
55555555

55565556
if (auto *CRD = dyn_cast<CXXRecordDecl>(D);
55575557
!CRD || !(CRD->isClass() || CRD->isStruct())) {
5558-
S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type_str)
5559-
<< AL << AL.isRegularKeywordAttribute() << "classes";
5558+
S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
5559+
<< AL << AL.isRegularKeywordAttribute() << ExpectedClass;
55605560
return;
55615561
}
55625562

clang/lib/Sema/SemaSwift.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ void SemaSwift::handleNewType(Decl *D, const ParsedAttr &AL) {
650650
}
651651

652652
if (!isa<TypedefNameDecl>(D)) {
653-
Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
654-
<< AL << AL.isRegularKeywordAttribute() << "typedefs";
653+
Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
654+
<< AL << AL.isRegularKeywordAttribute() << ExpectedTypedef;
655655
return;
656656
}
657657

clang/lib/Sema/SemaType.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7983,8 +7983,9 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr,
79837983
if (!FnTy) {
79847984
// SME ACLE attributes are not supported on K&R-style unprototyped C
79857985
// functions.
7986-
S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
7987-
attr << attr.isRegularKeywordAttribute() << ExpectedFunctionWithProtoType;
7986+
S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
7987+
<< attr << attr.isRegularKeywordAttribute()
7988+
<< ExpectedFunctionWithProtoType;
79887989
attr.setInvalid();
79897990
return false;
79907991
}
@@ -8676,9 +8677,9 @@ static void HandleLifetimeBoundAttr(TypeProcessingState &State,
86768677
CurType, CurType);
86778678
return;
86788679
}
8679-
State.getSema().Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type_str)
8680+
State.getSema().Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
86808681
<< Attr << Attr.isRegularKeywordAttribute()
8681-
<< "parameters and implicit object parameters";
8682+
<< ExpectedParameterOrImplicitObjectParameter;
86828683
}
86838684

86848685
static void HandleLifetimeCaptureByAttr(TypeProcessingState &State,

0 commit comments

Comments
 (0)