[cfe-dev] Clang and CUDA with C++11 features

Richard Smith richard at metafoo.co.uk
Sun Jun 17 23:22:58 PDT 2012


On Thu, Jun 14, 2012 at 7:53 AM, Peter Colberg <peter at colberg.org> wrote:
> On Wed, Jun 13, 2012 at 09:35:10PM -0700, Sean Silva wrote:
>> On Wed, Jun 13, 2012 at 8:28 PM, Peter Colberg <peter at colberg.org> wrote:
>> > The parser interprets the compressed C++11 template parameter syntax
>> > as a call to a CUDA kernel function. Is there a way to disable parsing
>> > of the CUDA call syntax <<< >>>? I would be using a C++ wrapper around
>> > cudaConfigureCall, cudaSetupArgument and cudaLaunch anyway.
>>
>> Try:
>>
>> find $HEADER_DIR | xargs sed --i.bak -e 's/>>>/> > >/'
>>
>> I don't think there are any syntactic dark corners where that will break
>> otherwise valid C++.
>>
>> No guarantees though (that's what the -i.bak is for ;).
>
> The other way around: CUDA violates the C++ standard with regard to
> template parameter syntax, so I would like to disable the CUDA
> execution syntax, and use cudaLaunch with a pointer instead.
>
> The lexer of Clang is straight-forward :-).

I've just improved the diagnostic for this in Clang ToT:

test/Parser/cuda-kernel-call.cu:13:12: error: a space is required between
      consecutive right angle brackets (use '> >')
  S<S<S<int>>> s; // expected-error 2{{use '> >'}}
           ^~
           > >
test/Parser/cuda-kernel-call.cu:13:13: error: a space is required between
      consecutive right angle brackets (use '> >')
  S<S<S<int>>> s; // expected-error 2{{use '> >'}}
            ^~
            > >

If we want to support CUDA + C++11, I think the right fix for this
issue is to extend C++11's rule for '>>'-fission to CUDA's '>>>'
token:

Index: lib/Parse/ParseTemplate.cpp
===================================================================
--- lib/Parse/ParseTemplate.cpp (revision 158652)
+++ lib/Parse/ParseTemplate.cpp (working copy)
@@ -785,7 +785,8 @@
     Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");

   unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
-  if (getLangOpts().CPlusPlus0x && Tok.is(tok::greatergreater))
+  if (getLangOpts().CPlusPlus0x &&
+      (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
     DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
   else if (Tok.is(tok::greaterequal))
     DiagId = diag::err_right_angle_bracket_equal_needs_space;

This will allow us to correctly deal with '>>' in almost all cases in
CUDA mode, and the exceptions are truly bizarre and implausible
constructs like "(SomeType)&FnTmpl<ClassTmpl<T>>>>3;" -- here the
'>>>>' splits into '> > >>' in C++11 and would split into '> > > >' in
CUDA/C++11 with the proposed rule.



More information about the cfe-dev mailing list