[cfe-dev] Newline vs space in C++ parsing

Nico Weber thakis at chromium.org
Fri Jan 18 10:42:27 PST 2013


On Fri, Jan 18, 2013 at 10:26 AM, Nico Weber <thakis at chromium.org> wrote:
> Hi,
>
> I think this is a bug that was introduced in r159549 / r159164. This fixes it:
>
> Index: lib/Parse/ParseDeclCXX.cpp
> ===================================================================
> --- lib/Parse/ParseDeclCXX.cpp  (revision 172830)
> +++ lib/Parse/ParseDeclCXX.cpp  (working copy)
> @@ -964,6 +964,7 @@
>    case tok::annot_template_id:  // struct foo {...} a<int>    ::b;
>    case tok::l_paren:            // struct foo {...} (         x);
>    case tok::comma:              // __builtin_offsetof(struct foo{...} ,
> +  case tok::kw_operator:        // struct foo operator++() {...}
>      return true;
>    case tok::colon:
>      return CouldBeBitfield;     // enum E { ... }   :         2;
>
> I'll upload this for review once I added tests.

Actually, this looks simple enough to qualify for post-commit review.
Fixed in r172834.

>
> Thanks!
>
> On Fri, Jan 18, 2013 at 10:01 AM, Nickolai Zeldovich
> <nickolai at csail.mit.edu> wrote:
>> I'm seeing surprising behavior in Clang 3.2 and 3.3-svn (as of a few days
>> ago), where newlines seem to be treated differently from spaces in parsing
>> C++ code.  In particular, the following code compiles just fine:
>>
>>   % cat x.cc
>>   struct s {};
>>
>>   struct s operator++(struct s a)
>>   { return a; }
>>   % clang -c x.cc
>>   %
>>
>> but this version, with a newline instead of a space, generates an error:
>>
>>   % cat x.cc
>>   struct s {};
>>
>>   struct s
>>   operator++(struct s a)
>>   { return a; }
>>   % clang -c x.cc
>>   x.cc:3:9: error: expected ';' after struct
>>   struct s
>>           ^
>>           ;
>>   x.cc:4:1: error: C++ requires a type specifier for all declarations
>>   operator++(struct s a)
>>   ^~~~~~~~
>>   x.cc:5:10: error: no viable conversion from 'struct s' to 'int'
>>   { return a; }
>>            ^
>>   3 errors generated.
>>   %
>>
>> g++ 4.7.2 accepts either variation, and I was expecting newlines and spaces
>> to be both treated as whitespace in C++.  Is this a bug in Clang or
>> something more subtle?
>>
>> Nickolai.
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>



More information about the cfe-dev mailing list