From f74e91e17369bbc076e875da6e20d45764ec0f7e Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Wed, 19 Oct 2016 21:03:38 +0000 Subject: [PATCH] [CUDA] Emit errors for wrong-side calls made on the same line as non-wrong-side calls. Summary: This fixes two related bugs: 1) Previously, if you had a non-wrong side call at some source code location L, we wouldn't emit errors for wrong-side calls that appeared at L. 2) We'd only emit one wrong-side error per source code location, when we actually want to emit it twice if we hit this line more than once due to e.g. template instantiation. Reviewers: tra Subscribers: rnk, cfe-commits Differential Revision: https://reviews.llvm.org/D25702 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284643 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 8 ++--- lib/Sema/SemaCUDA.cpp | 10 ++++--- test/SemaCUDA/bad-calls-on-same-line.cu | 39 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 test/SemaCUDA/bad-calls-on-same-line.cu diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index bb6859f8d3..627aa5bfb3 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -9252,10 +9252,10 @@ public: llvm::DenseMap> CUDADeferredDiags; - /// Raw encodings of SourceLocations for which CheckCUDACall has emitted a - /// (maybe deferred) "bad call" diagnostic. We use this to avoid emitting the - /// same deferred diag twice. - llvm::DenseSet LocsWithCUDACallDiags; + /// FunctionDecls plus raw encodings of SourceLocations for which + /// CheckCUDACall has emitted a (maybe deferred) "bad call" diagnostic. We + /// use this to avoid emitting the same deferred diag twice. + llvm::DenseSet> LocsWithCUDACallDiags; /// The set of CUDA functions that we've discovered must be emitted by tracing /// the call graph. Functions that we can tell a priori must be emitted diff --git a/lib/Sema/SemaCUDA.cpp b/lib/Sema/SemaCUDA.cpp index 75ec5f2bbf..423aef370b 100644 --- a/lib/Sema/SemaCUDA.cpp +++ b/lib/Sema/SemaCUDA.cpp @@ -714,20 +714,22 @@ bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) { } }(); + if (DiagKind == CUDADiagBuilder::K_Nop) + return true; + // Avoid emitting this error twice for the same location. Using a hashtable // like this is unfortunate, but because we must continue parsing as normal // after encountering a deferred error, it's otherwise very tricky for us to // ensure that we only emit this deferred error once. - if (!LocsWithCUDACallDiags.insert(Loc.getRawEncoding()).second) + if (!LocsWithCUDACallDiags.insert({Caller, Loc.getRawEncoding()}).second) return true; - bool IsImmediateErr = - CUDADiagBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this) + CUDADiagBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this) << IdentifyCUDATarget(Callee) << Callee << IdentifyCUDATarget(Caller); CUDADiagBuilder(DiagKind, Callee->getLocation(), diag::note_previous_decl, Caller, *this) << Callee; - return !IsImmediateErr; + return DiagKind != CUDADiagBuilder::K_Immediate; } void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) { diff --git a/test/SemaCUDA/bad-calls-on-same-line.cu b/test/SemaCUDA/bad-calls-on-same-line.cu new file mode 100644 index 0000000000..e91baff5d2 --- /dev/null +++ b/test/SemaCUDA/bad-calls-on-same-line.cu @@ -0,0 +1,39 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// The hd function template is instantiated three times. +// +// Two of those instantiations call a device function, which is an error when +// compiling for host. Clang should report both errors. + +#include "Inputs/cuda.h" + +template +struct Selector {}; + +template <> +struct Selector { + __host__ void f() {} +}; + +template <> +struct Selector { + __device__ void f() {} // expected-note {{declared here}} +}; + +template <> +struct Selector { + __device__ void f() {} // expected-note {{declared here}} +}; + +template +inline __host__ __device__ void hd() { + Selector().f(); + // expected-error@-1 {{reference to __device__ function}} + // expected-error@-2 {{reference to __device__ function}} +} + +void host_fn() { + hd(); + hd(); // expected-note {{function template specialization 'hd'}} + hd(); // expected-note {{function template specialization 'hd'}} +} -- 2.40.0