Author: abataev Date: Thu Jan 14 22:36:32 2016 New Revision: 257868 URL: http://llvm.org/viewvc/llvm-project?rev=257868&view=rev Log: PR26111: segmentation fault with __attribute__((mode(QI))) on function declaration, by Denis Zobnin Allow "mode" attribute to be applied to VarDecl, not ValueDecl (which includes FunctionDecl and EnumConstantDecl), emit an error if this attribute is used with function declarations and enum constants. Differential Revision: http://reviews.llvm.org/D16112
Modified: cfe/trunk/include/clang/AST/Decl.h cfe/trunk/include/clang/Basic/Attr.td cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/lib/Sema/SemaDeclAttr.cpp cfe/trunk/test/Sema/attr-mode.c Modified: cfe/trunk/include/clang/AST/Decl.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=257868&r1=257867&r2=257868&view=diff ============================================================================== --- cfe/trunk/include/clang/AST/Decl.h (original) +++ cfe/trunk/include/clang/AST/Decl.h Thu Jan 14 22:36:32 2016 @@ -1954,6 +1954,7 @@ public: unsigned getMinRequiredArguments() const; QualType getReturnType() const { + assert(getType()->getAs<FunctionType>() && "Expected a FunctionType!"); return getType()->getAs<FunctionType>()->getReturnType(); } @@ -1964,6 +1965,7 @@ public: /// \brief Determine the type of an expression that calls this function. QualType getCallResultType() const { + assert(getType()->getAs<FunctionType>() && "Expected a FunctionType!"); return getType()->getAs<FunctionType>()->getCallResultType(getASTContext()); } Modified: cfe/trunk/include/clang/Basic/Attr.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=257868&r1=257867&r2=257868&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/Attr.td (original) +++ cfe/trunk/include/clang/Basic/Attr.td Thu Jan 14 22:36:32 2016 @@ -879,6 +879,8 @@ def MipsInterrupt : InheritableAttr, Tar def Mode : Attr { let Spellings = [GCC<"mode">]; + let Subjects = SubjectList<[Var, TypedefName], ErrorDiag, + "ExpectedVariableOrTypedef">; let Args = [IdentifierArgument<"Mode">]; let Documentation = [Undocumented]; } Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=257868&r1=257867&r2=257868&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Jan 14 22:36:32 2016 @@ -2851,8 +2851,6 @@ def warn_vector_mode_deprecated : Warnin InGroup<DeprecatedAttributes>; def err_complex_mode_vector_type : Error< "type of machine mode does not support base vector types">; -def err_attr_wrong_decl : Error< - "%0 attribute invalid on this declaration, requires typedef or value">; def warn_attribute_nonnull_no_pointers : Warning< "'nonnull' attribute applied to function with no pointer arguments">, InGroup<IgnoredAttributes>; Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=257868&r1=257867&r2=257868&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original) +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Thu Jan 14 22:36:32 2016 @@ -3391,13 +3391,8 @@ static void handleModeAttr(Sema &S, Decl QualType OldTy; if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) OldTy = TD->getUnderlyingType(); - else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) - OldTy = VD->getType(); - else { - S.Diag(D->getLocation(), diag::err_attr_wrong_decl) - << Attr.getName() << Attr.getRange(); - return; - } + else + OldTy = cast<VarDecl>(D)->getType(); // Base type can also be a vector type (see PR17453). // Distinguish between base type and base element type. @@ -3470,7 +3465,7 @@ static void handleModeAttr(Sema &S, Decl if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); else - cast<ValueDecl>(D)->setType(NewTy); + cast<VarDecl>(D)->setType(NewTy); D->addAttr(::new (S.Context) ModeAttr(Attr.getRange(), S.Context, Name, Modified: cfe/trunk/test/Sema/attr-mode.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-mode.c?rev=257868&r1=257867&r2=257868&view=diff ============================================================================== --- cfe/trunk/test/Sema/attr-mode.c (original) +++ cfe/trunk/test/Sema/attr-mode.c Thu Jan 14 22:36:32 2016 @@ -24,6 +24,9 @@ typedef unsigned unwind_word __attribute int **__attribute((mode(QI)))* i32; // expected-error{{mode attribute}} +__attribute__((mode(QI))) int invalid_func() { return 1; } // expected-error{{'mode' attribute only applies to variables and typedefs}} +enum invalid_enum { A1 __attribute__((mode(QI))) }; // expected-error{{'mode' attribute only applies to variables and typedefs}} + typedef _Complex double c32 __attribute((mode(SC))); int c32_test[sizeof(c32) == 8 ? 1 : -1]; typedef _Complex float c64 __attribute((mode(DC))); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits