Actually, I meant to send out this version of the second patch (identical except that there is unsigned int instead of int in the helper function).
Cheers, Caitlin On Tue, Jun 28, 2011 at 3:23 PM, Caitlin Sadowski <[email protected]> wrote: > Here is a second refactoring patch, building on the 1 character patch > sent out previously, which refactors SemaDeclAttr to use a helper > method when checking if the number of args matches, if the expected > number is the same as the check. > > Also, feel free to give code review comments: > > http://codereview.appspot.com/4635075 > > Cheers, > > Caitlin > > On Mon, Jun 27, 2011 at 6:44 PM, Caitlin Sadowski <[email protected]> wrote: >> This bugfix corrects a suspected mismatch between the number of >> arguments expected and the number listed in the error message if less >> arguments are presented in HandleReqdWorkGroupSize. >> >> Cheers, >> >> Caitlin >> >
From abdfcdb682103197ee5488a1821861bcd12711fe Mon Sep 17 00:00:00 2001 From: Caitlin Sadowski <[email protected]> Date: Mon, 27 Jun 2011 17:49:36 -0700 Subject: [PATCH] Refactoring SemaDeclAttr to use a helper method when checking if the number of args matches, if the expected number is the same as the check. --- lib/Sema/SemaDeclAttr.cpp | 134 ++++++++++++++++++--------------------------- 1 files changed, 53 insertions(+), 81 deletions(-) diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 9d37b4c..a7c0dc7 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -194,6 +194,16 @@ static inline bool isCFStringType(QualType T, ASTContext &Ctx) { return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); } +static inline bool wrongNumArgs(const AttributeList &Attr, + Sema &S, unsigned int num) { + if (Attr.getNumArgs() != num){ + S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << num; + return true; + } + + return false; +} + //===----------------------------------------------------------------------===// // Attribute Implementations //===----------------------------------------------------------------------===// @@ -227,10 +237,9 @@ static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, sizeExpr = Size.get(); } else { // check the attribute arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if (wrongNumArgs(Attr, S, 1)) return; - } + sizeExpr = Attr.getArg(0); } @@ -248,10 +257,8 @@ static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() > 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } if (TagDecl *TD = dyn_cast<TagDecl>(d)) TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); @@ -277,10 +284,8 @@ static void HandleMsStructAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() > 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } // The IBAction attributes only apply to instance methods. if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) @@ -294,10 +299,8 @@ static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() > 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } // The IBOutlet attributes only apply to instance variables of // Objective-C classes. @@ -754,10 +757,8 @@ static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleNakedAttr(Decl *d, const AttributeList &Attr, Sema &S) { // Check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } if (!isa<FunctionDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -805,16 +806,14 @@ static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); } static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { - assert(Attr.isInvalid() == false); + assert(!Attr.isInvalid()); if (isa<VarDecl>(d)) d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context)); else @@ -823,7 +822,7 @@ static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { } static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { - assert(Attr.isInvalid() == false); + assert(!Attr.isInvalid()); if (isa<VarDecl>(d)) d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context)); else @@ -861,10 +860,8 @@ static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, // The checking path for 'noreturn' and 'analyzer_noreturn' are different // because 'analyzer_noreturn' does not impact the type. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; - return; - } + if(wrongNumArgs(Attr, S, 0)) + return; if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { ValueDecl *VD = dyn_cast<ValueDecl>(d); @@ -1148,10 +1145,8 @@ static void HandleAvailabilityAttr(Decl *d, const AttributeList &Attr, static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if(wrongNumArgs(Attr, S, 1)) return; - } Expr *Arg = Attr.getArg(0); Arg = Arg->IgnoreParenCasts(); @@ -1237,10 +1232,9 @@ static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr, static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, Sema &S) { - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); if (OCI == 0) { @@ -1252,10 +1246,12 @@ static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, } static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { + if (Attr.getNumArgs() != 0) { S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; return; } + if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { QualType T = TD->getUnderlyingType(); if (!T->isPointerType() || @@ -1401,10 +1397,8 @@ static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -1453,10 +1447,9 @@ static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) { static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + // weak_import only applies to variable & function declarations. bool isDef = false; @@ -1482,10 +1475,8 @@ static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, Sema &S) { // Attribute has 3 arguments. - if (Attr.getNumArgs() != 3) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; + if (wrongNumArgs(Attr, S, 3)) return; - } unsigned WGSize[3]; for (unsigned i = 0; i < 3; ++i) { @@ -1506,10 +1497,8 @@ static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { // Attribute has no arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if (wrongNumArgs(Attr, S, 1)) return; - } // Make sure that there is a string literal as the sections's single // argument. @@ -1571,10 +1560,8 @@ static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); } @@ -1642,10 +1629,9 @@ static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { /// Handle __attribute__((format_arg((idx)))) attribute based on /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if (wrongNumArgs(Attr, S, 1)) return; - } + if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << Attr.getName() << ExpectedFunction; @@ -1959,10 +1945,9 @@ static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + // Try to find the underlying union declaration. RecordDecl *RD = 0; @@ -2026,10 +2011,9 @@ static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if (wrongNumArgs(Attr, S, 1)) return; - } + Expr *ArgExpr = Attr.getArg(0); StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); @@ -2103,10 +2087,9 @@ static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { // the width of an int or unsigned int to the specified size. // Check that there aren't any arguments - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + IdentifierInfo *Name = Attr.getParameterName(); if (!Name) { @@ -2265,10 +2248,8 @@ static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() > 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } if (!isFunctionOrMethod(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2281,10 +2262,9 @@ static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + if (!isa<FunctionDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2298,10 +2278,9 @@ static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + if (!isa<FunctionDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2356,10 +2335,8 @@ static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { if (S.LangOpts.CUDA) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } if (!isa<FunctionDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2391,10 +2368,9 @@ static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { if (S.LangOpts.CUDA) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + if (!isa<FunctionDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2411,10 +2387,9 @@ static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { if (S.LangOpts.CUDA) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } + if (!isa<VarDecl>(d)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) @@ -2430,10 +2405,8 @@ static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. - if (Attr.getNumArgs() != 0) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; + if (wrongNumArgs(Attr, S, 0)) return; - } FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); if (Fn == 0) { @@ -2856,10 +2829,9 @@ static bool isKnownDeclSpecAttr(const AttributeList &Attr) { static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) { if (S.LangOpts.Microsoft || S.LangOpts.Borland) { // check the attribute arguments. - if (Attr.getNumArgs() != 1) { - S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; + if (wrongNumArgs(Attr, S, 1)) return; - } + Expr *Arg = Attr.getArg(0); StringLiteral *Str = dyn_cast<StringLiteral>(Arg); if (Str == 0 || Str->isWide()) { -- 1.7.3.1
_______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
