From aaaad6cc33097851606bc462cd0807d3546bbf8b Mon Sep 17 00:00:00 2001 From: zhaoxuhui Date: Sat, 25 Feb 2023 11:35:42 +0800 Subject: [PATCH] [BSC] bugfix for nullptr problem --- clang/include/clang/AST/Decl.h | 2 +- clang/include/clang/AST/DeclCXX.h | 6 ++--- clang/include/clang/Sema/DeclSpec.h | 2 +- clang/include/clang/Sema/Sema.h | 2 +- clang/lib/AST/Decl.cpp | 2 +- clang/lib/Parse/ParseDecl.cpp | 10 ++++---- clang/lib/Sema/SemaDecl.cpp | 23 +++++++++++-------- .../BuiltInType/int_this_param_check.cbs | 4 ++++ .../BSC/Method/Enum/enum_this_param_check.cbs | 9 ++++++++ .../Method/Struct/struct_this_param_check.cbs | 7 ++++++ .../Method/Union/union_this_param_check.cbs | 11 +++++++++ 11 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 clang/test/BSC/Method/BuiltInType/int_this_param_check.cbs create mode 100644 clang/test/BSC/Method/Enum/enum_this_param_check.cbs create mode 100644 clang/test/BSC/Method/Struct/struct_this_param_check.cbs create mode 100644 clang/test/BSC/Method/Union/union_this_param_check.cbs diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 1e54e91736a3..254e050b7777 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -1645,7 +1645,7 @@ public: SourceRange getSourceRange() const override LLVM_READONLY; - bool isThisParam = false; + bool IsThisParam = false; void setObjCMethodScopeInfo(unsigned parameterIndex) { ParmVarDeclBits.IsObjCMethodParam = true; diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 499320e554df..58ba43559e0e 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -1952,8 +1952,8 @@ public: SourceLocation EndLocation, Expr *TrailingRequiresClause = nullptr); static BSCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID); - bool getHasThisParam() const { return hasThisParam; } - void setHasThisParam(bool hasThisParam) { this->hasThisParam = hasThisParam; } + bool getHasThisParam() const { return HasThisParam; } + void setHasThisParam(bool HasThisParam) { this->HasThisParam = HasThisParam; } QualType getExtendedType() const { return ExtendedType; } void setExtendedType(QualType ExtendedType) { this->ExtendedType = ExtendedType; @@ -1965,7 +1965,7 @@ public: private: QualType ExtendedType; - bool hasThisParam = false; + bool HasThisParam = false; }; /// Represents a static or instance method of a struct/union/class. diff --git a/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h index e797da753ad7..f57c94ff35a3 100644 --- a/clang/include/clang/Sema/DeclSpec.h +++ b/clang/include/clang/Sema/DeclSpec.h @@ -1928,7 +1928,7 @@ public: clear(); } // Used to mark parameters that include 'this'. - bool hasThisParam = false; + bool HasThisParam = false; /// getDeclSpec - Return the declaration-specifier that this declarator was /// declared with. const DeclSpec &getDeclSpec() const { return DS; } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 5809edddd755..7d3b1be8bbc2 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2676,7 +2676,7 @@ public: bool IsDefinition); void CheckFunctionOrTemplateParamDeclarator(Scope *S, Declarator &D); Decl *ActOnParamDeclarator(Scope *S, Declarator &D, int ParamSize = 0, - const Type *typePtr = nullptr); + const Type *TypePtr = nullptr); ParmVarDecl *BuildParmVarDeclForTypedef(DeclContext *DC, SourceLocation Loc, QualType T); diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 9c4b8959111f..80b9be706ac4 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3342,7 +3342,7 @@ void FunctionDecl::setParams(ASTContext &C, unsigned FunctionDecl::getMinRequiredArguments() const { if (getASTContext().getLangOpts().BSC) { int num = getNumParams(); - if (num > 0 && parameters()[0] && parameters()[0]->isThisParam) { + if (num > 0 && parameters()[0] && parameters()[0]->IsThisParam) { num--; } return num; diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 22be56c90274..ac03c25ee8dd 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -1978,7 +1978,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, if (!D.getExtendedType().isNull()) { if (auto *MD = dyn_cast(TheDecl)) { MD->setExtendedType(D.getExtendedType()); - MD->setHasThisParam(D.hasThisParam); + MD->setHasThisParam(D.HasThisParam); } } return Actions.ConvertDeclToDeclGroup(TheDecl); @@ -2049,7 +2049,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, D.complete(FirstDecl); if (FirstDecl) { if (auto FD = dyn_cast(FirstDecl)) - FD->setHasThisParam(D.hasThisParam); + FD->setHasThisParam(D.HasThisParam); DeclsInGroup.push_back(FirstDecl); } @@ -6538,9 +6538,9 @@ void Parser::ParseFunctionDeclarator(Declarator &D, if (Tok.isNot(tok::r_paren)) { const Type *typePtr = nullptr; if (!D.getExtendedType().isNull()) { - typePtr = D.getExtendedType().getTypePtr(); + typePtr = D.getExtendedType().getTypePtrOrNull(); if (typePtr) - typePtr = typePtr->getCanonicalTypeUnqualified().getTypePtr(); + typePtr = typePtr->getCanonicalTypeUnqualified().getTypePtrOrNull(); } ParseParameterDeclarationClause(D.getContext(), FirstArgAttrs, ParamInfo, EllipsisLoc, typePtr); @@ -6551,7 +6551,7 @@ void Parser::ParseFunctionDeclarator(Declarator &D, ParmVarDecl *PD = dyn_cast_or_null(ParamInfo.data()[0].Param); if (PD && D.getBSCScopeSpec()) - D.hasThisParam = PD->isThisParam; + D.HasThisParam = PD->IsThisParam; } HasProto = ParamInfo.size() || getLangOpts().CPlusPlus || getLangOpts().OpenCL; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 1f46f0c784df..718ef43f7cd3 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13569,7 +13569,7 @@ void Sema::CheckFunctionOrTemplateParamDeclarator(Scope *S, Declarator &D) { /// ActOnParamDeclarator - Called from Parser::ParseFunctionDeclarator() /// to introduce parameters into function prototype scope. Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D, int ParamSize, - const Type *typePtr) { + const Type *TypePtr) { const DeclSpec &DS = D.getDeclSpec(); // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. @@ -13614,18 +13614,21 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D, int ParamSize, // Check for redeclaration of parameters, e.g. int foo(int x, int x); IdentifierInfo *II = D.getIdentifier(); - bool isThisParam = false; - if (typePtr != nullptr && DeclarationName(II).getAsString() == "this") { + bool IsThisParam = false; + if (TypePtr != nullptr && DeclarationName(II).getAsString() == "this") { if (ParamSize == 0) { - auto thisTypePtr = parmDeclType.getTypePtr() - ->getPointeeType() - ->getCanonicalTypeUnqualified() - .getTypePtr(); - if (typePtr == nullptr || typePtr != thisTypePtr) { + auto ThisTypePtr = parmDeclType.getTypePtrOrNull(); + if (ThisTypePtr) { + ThisTypePtr = ThisTypePtr->getPointeeType().getTypePtrOrNull(); + if (ThisTypePtr) + ThisTypePtr = + ThisTypePtr->getCanonicalTypeUnqualified().getTypePtrOrNull(); + } + if (TypePtr != ThisTypePtr) { Diag(D.getBeginLoc(), diag::err_type_unsupported) << parmDeclType.getAsString(); } - isThisParam = true; + IsThisParam = true; } else { Diag(D.getIdentifierLoc(), diag::err_bad_parameter_name) << GetNameForDeclarator(D).getName(); @@ -13661,7 +13664,7 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D, int ParamSize, ParmVarDecl *New = CheckParameter(Context.getTranslationUnitDecl(), D.getBeginLoc(), D.getIdentifierLoc(), II, parmDeclType, TInfo, SC); - New->isThisParam = isThisParam; + New->IsThisParam = IsThisParam; if (D.isInvalidType()) New->setInvalidDecl(); diff --git a/clang/test/BSC/Method/BuiltInType/int_this_param_check.cbs b/clang/test/BSC/Method/BuiltInType/int_this_param_check.cbs new file mode 100644 index 000000000000..09fb8971031a --- /dev/null +++ b/clang/test/BSC/Method/BuiltInType/int_this_param_check.cbs @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -verify %s + +void int::increase(int this) { // expected-error {{int is not supported on this target}} +} \ No newline at end of file diff --git a/clang/test/BSC/Method/Enum/enum_this_param_check.cbs b/clang/test/BSC/Method/Enum/enum_this_param_check.cbs new file mode 100644 index 000000000000..2c68b3fa2813 --- /dev/null +++ b/clang/test/BSC/Method/Enum/enum_this_param_check.cbs @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -verify %s + +enum E { + X3, + X4 +}; + +int enum E::getA(enum E this); // expected-error {{enum E is not supported on this target}} + diff --git a/clang/test/BSC/Method/Struct/struct_this_param_check.cbs b/clang/test/BSC/Method/Struct/struct_this_param_check.cbs new file mode 100644 index 000000000000..240cca115de7 --- /dev/null +++ b/clang/test/BSC/Method/Struct/struct_this_param_check.cbs @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -verify %s + +struct Foo { +}; +int struct Foo::getA(struct Foo this) { // expected-error {{struct Foo is not supported on this target}} + return 1; +} diff --git a/clang/test/BSC/Method/Union/union_this_param_check.cbs b/clang/test/BSC/Method/Union/union_this_param_check.cbs new file mode 100644 index 000000000000..a1a88c796d02 --- /dev/null +++ b/clang/test/BSC/Method/Union/union_this_param_check.cbs @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -verify %s + +union SimpleUnion { + float uf; + int ui; + char uc; +}; + +int union SimpleUnion::getA(union SimpleUnion this) { // expected-error {{union SimpleUnion is not supported on this target}} + return 1; +} \ No newline at end of file -- Gitee