From ec36dad1b2089ec92370a36615b7828152e34a75 Mon Sep 17 00:00:00 2001 From: Sven van Haastregt Date: Wed, 22 May 2019 13:12:20 +0000 Subject: [PATCH] [OpenCL] Support pipe keyword in C++ mode Support the OpenCL C pipe feature in C++ for OpenCL mode, to preserve backwards compatibility with OpenCL C. Various changes had to be made in Parse and Sema to enable pipe-specific diagnostics, so enable a SemaOpenCL test for C++. Differential Revision: https://reviews.llvm.org/D62181 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361382 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/TokenKinds.def | 2 +- lib/Basic/Builtins.cpp | 5 +++-- lib/Parse/ParseDecl.cpp | 14 +++++++++++--- lib/Parse/ParseTentative.cpp | 2 ++ lib/Sema/SemaDecl.cpp | 2 +- lib/Sema/SemaType.cpp | 6 ++++-- test/SemaOpenCL/invalid-pipes-cl2.0.cl | 5 ++++- 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/clang/Basic/TokenKinds.def b/include/clang/Basic/TokenKinds.def index 77803b2ec5..65f2c5d982 100644 --- a/include/clang/Basic/TokenKinds.def +++ b/include/clang/Basic/TokenKinds.def @@ -568,7 +568,7 @@ KEYWORD(vec_step , KEYOPENCLC | KEYALTIVEC | KEYZVECTOR) // OpenMP Type Traits KEYWORD(__builtin_omp_required_simd_align, KEYALL) -KEYWORD(pipe , KEYOPENCLC) +KEYWORD(pipe , KEYOPENCLC | KEYOPENCLCXX) // Borland Extensions. KEYWORD(__pascal , KEYALL) diff --git a/lib/Basic/Builtins.cpp b/lib/Basic/Builtins.cpp index cfc5927774..d23c280d47 100644 --- a/lib/Basic/Builtins.cpp +++ b/lib/Basic/Builtins.cpp @@ -70,8 +70,9 @@ bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo, bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG; bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 && (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG; - bool OclC2Unsupported = LangOpts.OpenCLVersion != 200 && - (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG; + bool OclC2Unsupported = + (LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) && + (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG; bool OclCUnsupported = !LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES); bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index edadc90708..5589297953 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -2560,6 +2560,11 @@ bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, return false; } + // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic + // for incomplete declarations such as `pipe p`. + if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe()) + return false; + if (getLangOpts().CPlusPlus && DS.getStorageClassSpec() == DeclSpec::SCS_auto) { // Don't require a type specifier if we have the 'auto' storage class @@ -3769,7 +3774,8 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); break; case tok::kw_pipe: - if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200)) { + if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 && + !getLangOpts().OpenCLCPlusPlus)) { // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should // support the "pipe" word as identifier. Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); @@ -4896,7 +4902,8 @@ bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { default: return false; case tok::kw_pipe: - return getLangOpts().OpenCL && (getLangOpts().OpenCLVersion >= 200); + return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) || + getLangOpts().OpenCLCPlusPlus; case tok::identifier: // foo::bar // Unfortunate hack to support "Class.factoryMethod" notation. @@ -5384,7 +5391,8 @@ static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, if (Kind == tok::star || Kind == tok::caret) return true; - if ((Kind == tok::kw_pipe) && Lang.OpenCL && (Lang.OpenCLVersion >= 200)) + if (Kind == tok::kw_pipe && + ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus)) return true; if (!Lang.CPlusPlus) diff --git a/lib/Parse/ParseTentative.cpp b/lib/Parse/ParseTentative.cpp index e97a8e5b54..13d43d5e68 100644 --- a/lib/Parse/ParseTentative.cpp +++ b/lib/Parse/ParseTentative.cpp @@ -1461,6 +1461,8 @@ Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult, case tok::kw___read_only: case tok::kw___write_only: case tok::kw___read_write: + // OpenCL pipe + case tok::kw_pipe: // GNU case tok::kw_restrict: diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 5b7a0d2cc3..a035f200fc 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -9288,7 +9288,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, // OpenCL 2.0 pipe restrictions forbids pipe packet types to be non-value // types. - if (getLangOpts().OpenCLVersion >= 200) { + if (getLangOpts().OpenCLVersion >= 200 || getLangOpts().OpenCLCPlusPlus) { if(const PipeType *PipeTy = PT->getAs()) { QualType ElemTy = PipeTy->getElementType(); if (ElemTy->isReferenceType() || ElemTy->isPointerType()) { diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index bd091a0691..6ba4bdcf1d 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -1356,7 +1356,7 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) { // "At least one type specifier shall be given in the declaration // specifiers in each declaration, and in the specifier-qualifier list in // each struct declaration and type name." - if (S.getLangOpts().CPlusPlus) { + if (S.getLangOpts().CPlusPlus && !DS.isTypeSpecPipe()) { S.Diag(DeclLoc, diag::err_missing_type_specifier) << DS.getSourceRange(); @@ -1364,7 +1364,9 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) { // value being declared, poison it as invalid so we don't get chains of // errors. declarator.setInvalidType(true); - } else if (S.getLangOpts().OpenCLVersion >= 200 && DS.isTypeSpecPipe()){ + } else if ((S.getLangOpts().OpenCLVersion >= 200 || + S.getLangOpts().OpenCLCPlusPlus) && + DS.isTypeSpecPipe()) { S.Diag(DeclLoc, diag::err_missing_actual_pipe_type) << DS.getSourceRange(); declarator.setInvalidType(true); diff --git a/test/SemaOpenCL/invalid-pipes-cl2.0.cl b/test/SemaOpenCL/invalid-pipes-cl2.0.cl index 463fd3d0da..afe5dc6a60 100644 --- a/test/SemaOpenCL/invalid-pipes-cl2.0.cl +++ b/test/SemaOpenCL/invalid-pipes-cl2.0.cl @@ -1,9 +1,10 @@ // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL2.0 +// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=c++ global pipe int gp; // expected-error {{type '__global read_only pipe int' can only be used as a function parameter in OpenCL}} global reserve_id_t rid; // expected-error {{the '__global reserve_id_t' type cannot be used to declare a program scope variable}} -extern pipe write_only int get_pipe(); // expected-error {{type '__global write_only pipe int (void)' can only be used as a function parameter in OpenCL}} +extern pipe write_only int get_pipe(); // expected-error-re{{type '__global write_only pipe int ({{(void)?}})' can only be used as a function parameter in OpenCL}} kernel void test_invalid_reserved_id(reserve_id_t ID) { // expected-error {{'reserve_id_t' cannot be used as the type of a kernel parameter}} } @@ -35,6 +36,7 @@ bool test_id_comprision(void) { } // Tests ASTContext::mergeTypes rejects this. +#ifndef __OPENCL_CPP_VERSION__ int f(pipe int x, int y); // expected-note {{previous declaration is here}} int f(x, y) // expected-error {{conflicting types for 'f}} pipe short x; @@ -42,3 +44,4 @@ int y; { return y; } +#endif -- 2.40.0