|
10 | 10 |
|
11 | 11 | #include "clang/Sema/SemaSYCL.h" |
12 | 12 | #include "clang/AST/Mangle.h" |
| 13 | +#include "clang/AST/SYCLKernelInfo.h" |
13 | 14 | #include "clang/AST/TypeOrdering.h" |
| 15 | +#include "clang/Basic/Diagnostic.h" |
14 | 16 | #include "clang/Sema/Attr.h" |
15 | 17 | #include "clang/Sema/ParsedAttr.h" |
16 | 18 | #include "clang/Sema/Sema.h" |
@@ -206,3 +208,157 @@ void SemaSYCL::handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL) { |
206 | 208 | D->addAttr(::new (SemaRef.Context) |
207 | 209 | SYCLKernelEntryPointAttr(SemaRef.Context, AL, TSI)); |
208 | 210 | } |
| 211 | + |
| 212 | +// Given a potentially qualified type, SourceLocationForUserDeclaredType() |
| 213 | +// returns the source location of the canonical declaration of the unqualified |
| 214 | +// desugared user declared type, if any. For non-user declared types, an |
| 215 | +// invalid source location is returned. The intended usage of this function |
| 216 | +// is to identify an appropriate source location, if any, for a |
| 217 | +// "entity declared here" diagnostic note. |
| 218 | +static SourceLocation SourceLocationForUserDeclaredType(QualType QT) { |
| 219 | + SourceLocation Loc; |
| 220 | + const Type *T = QT->getUnqualifiedDesugaredType(); |
| 221 | + if (const TagType *TT = dyn_cast<TagType>(T)) |
| 222 | + Loc = TT->getDecl()->getLocation(); |
| 223 | + else if (const ObjCInterfaceType *ObjCIT = dyn_cast<ObjCInterfaceType>(T)) |
| 224 | + Loc = ObjCIT->getDecl()->getLocation(); |
| 225 | + return Loc; |
| 226 | +} |
| 227 | + |
| 228 | +static bool CheckSYCLKernelName(Sema &S, SourceLocation Loc, |
| 229 | + QualType KernelName) { |
| 230 | + assert(!KernelName->isDependentType()); |
| 231 | + |
| 232 | + if (!KernelName->isStructureOrClassType()) { |
| 233 | + // SYCL 2020 section 5.2, "Naming of kernels", only requires that the |
| 234 | + // kernel name be a C++ typename. However, the definition of "kernel name" |
| 235 | + // in the glossary states that a kernel name is a class type. Neither |
| 236 | + // section explicitly states whether the kernel name type can be |
| 237 | + // cv-qualified. For now, kernel name types are required to be class types |
| 238 | + // and that they may be cv-qualified. The following issue requests |
| 239 | + // clarification from the SYCL WG. |
| 240 | + // https://github.com/KhronosGroup/SYCL-Docs/issues/568 |
| 241 | + S.Diag(Loc, diag::warn_sycl_kernel_name_not_a_class_type) << KernelName; |
| 242 | + SourceLocation DeclTypeLoc = SourceLocationForUserDeclaredType(KernelName); |
| 243 | + if (DeclTypeLoc.isValid()) |
| 244 | + S.Diag(DeclTypeLoc, diag::note_entity_declared_at) << KernelName; |
| 245 | + return true; |
| 246 | + } |
| 247 | + |
| 248 | + return false; |
| 249 | +} |
| 250 | + |
| 251 | +void SemaSYCL::CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD) { |
| 252 | + // Ensure that all attributes present on the declaration are consistent |
| 253 | + // and warn about any redundant ones. |
| 254 | + SYCLKernelEntryPointAttr *SKEPAttr = nullptr; |
| 255 | + for (auto *SAI : FD->specific_attrs<SYCLKernelEntryPointAttr>()) { |
| 256 | + if (!SKEPAttr) { |
| 257 | + SKEPAttr = SAI; |
| 258 | + continue; |
| 259 | + } |
| 260 | + if (!getASTContext().hasSameType(SAI->getKernelName(), |
| 261 | + SKEPAttr->getKernelName())) { |
| 262 | + Diag(SAI->getLocation(), diag::err_sycl_entry_point_invalid_redeclaration) |
| 263 | + << SAI->getKernelName() << SKEPAttr->getKernelName(); |
| 264 | + Diag(SKEPAttr->getLocation(), diag::note_previous_attribute); |
| 265 | + SAI->setInvalidAttr(); |
| 266 | + } else { |
| 267 | + Diag(SAI->getLocation(), |
| 268 | + diag::warn_sycl_entry_point_redundant_declaration); |
| 269 | + Diag(SKEPAttr->getLocation(), diag::note_previous_attribute); |
| 270 | + } |
| 271 | + } |
| 272 | + assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute"); |
| 273 | + |
| 274 | + // Ensure the kernel name type is valid. |
| 275 | + if (!SKEPAttr->getKernelName()->isDependentType() && |
| 276 | + CheckSYCLKernelName(SemaRef, SKEPAttr->getLocation(), |
| 277 | + SKEPAttr->getKernelName())) |
| 278 | + SKEPAttr->setInvalidAttr(); |
| 279 | + |
| 280 | + // Ensure that an attribute present on the previous declaration |
| 281 | + // matches the one on this declaration. |
| 282 | + FunctionDecl *PrevFD = FD->getPreviousDecl(); |
| 283 | + if (PrevFD && !PrevFD->isInvalidDecl()) { |
| 284 | + const auto *PrevSKEPAttr = PrevFD->getAttr<SYCLKernelEntryPointAttr>(); |
| 285 | + if (PrevSKEPAttr && !PrevSKEPAttr->isInvalidAttr()) { |
| 286 | + if (!getASTContext().hasSameType(SKEPAttr->getKernelName(), |
| 287 | + PrevSKEPAttr->getKernelName())) { |
| 288 | + Diag(SKEPAttr->getLocation(), |
| 289 | + diag::err_sycl_entry_point_invalid_redeclaration) |
| 290 | + << SKEPAttr->getKernelName() << PrevSKEPAttr->getKernelName(); |
| 291 | + Diag(PrevSKEPAttr->getLocation(), diag::note_previous_decl) << PrevFD; |
| 292 | + SKEPAttr->setInvalidAttr(); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 298 | + if (!MD->isStatic()) { |
| 299 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 300 | + << /*non-static member function*/ 0; |
| 301 | + SKEPAttr->setInvalidAttr(); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + if (FD->isVariadic()) { |
| 306 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 307 | + << /*variadic function*/ 1; |
| 308 | + SKEPAttr->setInvalidAttr(); |
| 309 | + } |
| 310 | + |
| 311 | + if (FD->isDefaulted()) { |
| 312 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 313 | + << /*defaulted function*/ 3; |
| 314 | + SKEPAttr->setInvalidAttr(); |
| 315 | + } else if (FD->isDeleted()) { |
| 316 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 317 | + << /*deleted function*/ 2; |
| 318 | + SKEPAttr->setInvalidAttr(); |
| 319 | + } |
| 320 | + |
| 321 | + if (FD->isConsteval()) { |
| 322 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 323 | + << /*consteval function*/ 5; |
| 324 | + SKEPAttr->setInvalidAttr(); |
| 325 | + } else if (FD->isConstexpr()) { |
| 326 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 327 | + << /*constexpr function*/ 4; |
| 328 | + SKEPAttr->setInvalidAttr(); |
| 329 | + } |
| 330 | + |
| 331 | + if (FD->isNoReturn()) { |
| 332 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 333 | + << /*function declared with the 'noreturn' attribute*/ 6; |
| 334 | + SKEPAttr->setInvalidAttr(); |
| 335 | + } |
| 336 | + |
| 337 | + if (FD->getReturnType()->isUndeducedType()) { |
| 338 | + Diag(SKEPAttr->getLocation(), |
| 339 | + diag::err_sycl_entry_point_deduced_return_type); |
| 340 | + SKEPAttr->setInvalidAttr(); |
| 341 | + } else if (!FD->getReturnType()->isDependentType() && |
| 342 | + !FD->getReturnType()->isVoidType()) { |
| 343 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_return_type); |
| 344 | + SKEPAttr->setInvalidAttr(); |
| 345 | + } |
| 346 | + |
| 347 | + if (!FD->isInvalidDecl() && !FD->isTemplated() && |
| 348 | + !SKEPAttr->isInvalidAttr()) { |
| 349 | + const SYCLKernelInfo *SKI = |
| 350 | + getASTContext().findSYCLKernelInfo(SKEPAttr->getKernelName()); |
| 351 | + if (SKI) { |
| 352 | + if (!declaresSameEntity(FD, SKI->getKernelEntryPointDecl())) { |
| 353 | + // FIXME: This diagnostic should include the origin of the kernel |
| 354 | + // FIXME: names; not just the locations of the conflicting declarations. |
| 355 | + Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict); |
| 356 | + Diag(SKI->getKernelEntryPointDecl()->getLocation(), |
| 357 | + diag::note_previous_declaration); |
| 358 | + SKEPAttr->setInvalidAttr(); |
| 359 | + } |
| 360 | + } else { |
| 361 | + getASTContext().registerSYCLEntryPointFunction(FD); |
| 362 | + } |
| 363 | + } |
| 364 | +} |
0 commit comments