Skip to content

Commit

Permalink
[clang] Refactor attr diagnostics to use %select (#122473)
Browse files Browse the repository at this point in the history
A cleanup follow-up to #118501 and #118567.
  • Loading branch information
emaxx-google authored Jan 13, 2025
1 parent f459819 commit 41a94de
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 21 deletions.
9 changes: 5 additions & 4 deletions clang/examples/Attribute/Attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
const Decl *D) const override {
// This attribute appertains to functions only.
if (!isa<FunctionDecl>(D)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
<< Attr << Attr.isRegularKeywordAttribute() << "functions";
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< Attr << Attr.isRegularKeywordAttribute() << ExpectedFunction;
return false;
}
return true;
Expand Down Expand Up @@ -99,8 +99,9 @@ struct ExampleAttrInfo : public ParsedAttrInfo {
const Stmt *St) const override {
// This attribute appertains to for loop statements only.
if (!isa<ForStmt>(St)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
<< Attr << Attr.isRegularKeywordAttribute() << "for loop statements";
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< Attr << Attr.isRegularKeywordAttribute()
<< ExpectedForLoopStatement;
return false;
}
return true;
Expand Down
5 changes: 3 additions & 2 deletions clang/examples/CallSuperAttribute/CallSuperAttrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ struct CallSuperAttrInfo : public ParsedAttrInfo {
const Decl *D) const override {
const auto *TheMethod = dyn_cast_or_null<CXXMethodDecl>(D);
if (!TheMethod || !TheMethod->isVirtual()) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type_str)
<< Attr << Attr.isRegularKeywordAttribute() << "virtual functions";
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< Attr << Attr.isRegularKeywordAttribute()
<< ExpectedVirtualFunction;
return false;
}
MarkedMethods.insert(TheMethod);
Expand Down
9 changes: 8 additions & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3799,7 +3799,14 @@ def warn_attribute_wrong_decl_type : Warning<
"|types and namespaces"
"|variables, functions and classes"
"|kernel functions"
"|non-K&R-style functions}2">,
"|non-K&R-style functions"
"|for loop statements"
"|virtual functions"
"|parameters and implicit object parameters"
"|non-member functions"
"|functions, classes, or enumerations"
"|classes"
"|typedefs}2">,
InGroup<IgnoredAttributes>;
def err_attribute_wrong_decl_type : Error<warn_attribute_wrong_decl_type.Summary>;
def warn_type_attribute_wrong_type : Warning<
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Sema/ParsedAttr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,13 @@ enum AttributeDeclKind {
ExpectedFunctionVariableOrClass,
ExpectedKernelFunction,
ExpectedFunctionWithProtoType,
ExpectedForLoopStatement,
ExpectedVirtualFunction,
ExpectedParameterOrImplicitObjectParameter,
ExpectedNonMemberFunction,
ExpectedFunctionOrClassOrEnum,
ExpectedClass,
ExpectedTypedef,
};

inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "clang/Parse/RAIIObjectsForParser.h"
#include "clang/Sema/EnterExpressionEvaluationContext.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaCUDA.h"
Expand Down Expand Up @@ -3708,9 +3709,9 @@ void Parser::ParseDeclarationSpecifiers(
continue;

if (PA.getKind() == ParsedAttr::AT_LifetimeBound)
Diag(PA.getLoc(), diag::err_attribute_wrong_decl_type_str)
Diag(PA.getLoc(), diag::err_attribute_wrong_decl_type)
<< PA << PA.isRegularKeywordAttribute()
<< "parameters and implicit object parameters";
<< ExpectedParameterOrImplicitObjectParameter;
else
Diag(PA.getLoc(), diag::err_attribute_not_type_attr)
<< PA << PA.isRegularKeywordAttribute();
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,8 +1868,8 @@ static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
// This form is not allowed to be written on a member function (static or
// nonstatic) when in Microsoft compatibility mode.
if (S.getLangOpts().MSVCCompat && isa<CXXMethodDecl>(D)) {
S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
<< AL << AL.isRegularKeywordAttribute() << "non-member functions";
S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type)
<< AL << AL.isRegularKeywordAttribute() << ExpectedNonMemberFunction;
return;
}
}
Expand Down Expand Up @@ -2761,9 +2761,9 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
// The standard attribute cannot be applied to variable declarations such
// as a function pointer.
if (isa<VarDecl>(D))
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
<< AL << AL.isRegularKeywordAttribute()
<< "functions, classes, or enumerations";
<< ExpectedFunctionOrClassOrEnum;

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

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

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ void SemaSwift::handleNewType(Decl *D, const ParsedAttr &AL) {
}

if (!isa<TypedefNameDecl>(D)) {
Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
<< AL << AL.isRegularKeywordAttribute() << "typedefs";
Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
<< AL << AL.isRegularKeywordAttribute() << ExpectedTypedef;
return;
}

Expand Down
9 changes: 5 additions & 4 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7983,8 +7983,9 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr,
if (!FnTy) {
// SME ACLE attributes are not supported on K&R-style unprototyped C
// functions.
S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
attr << attr.isRegularKeywordAttribute() << ExpectedFunctionWithProtoType;
S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< attr << attr.isRegularKeywordAttribute()
<< ExpectedFunctionWithProtoType;
attr.setInvalid();
return false;
}
Expand Down Expand Up @@ -8676,9 +8677,9 @@ static void HandleLifetimeBoundAttr(TypeProcessingState &State,
CurType, CurType);
return;
}
State.getSema().Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type_str)
State.getSema().Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
<< Attr << Attr.isRegularKeywordAttribute()
<< "parameters and implicit object parameters";
<< ExpectedParameterOrImplicitObjectParameter;
}

static void HandleLifetimeCaptureByAttr(TypeProcessingState &State,
Expand Down

0 comments on commit 41a94de

Please sign in to comment.