This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C++ Patch] PR 80955 (Macros expanded in definition of user-defined literals)
- From: Jason Merrill <jason at redhat dot com>
- To: Mukesh Kapoor <mukesh dot kapoor at oracle dot com>
- Cc: Nathan Sidwell <nathan at acm dot org>, gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 2 Nov 2017 10:42:19 -0400
- Subject: Re: [C++ Patch] PR 80955 (Macros expanded in definition of user-defined literals)
- Authentication-results: sourceware.org; auth=none
- References: <6bd671aa-97cf-1dcc-ca90-5948ab533113@oracle.com> <58ca43f6-e0a6-0419-8e46-cbb6b78f845c@acm.org> <8d9f3e13-9b65-da73-499c-05e569eef046@oracle.com> <4e6e0a54-cf50-8944-a1bd-18f11d8b506c@oracle.com> <CADzB+2n4zC4wzLaPi-2hfd-MKMqsEcaz_iDe2JDo3mMfhosMxw@mail.gmail.com> <ccf1b0a0-6b6c-3302-8522-6a5385f71ca0@oracle.com> <d2fd029c-6d23-7f2d-f9b4-5d8f4ac5249f@acm.org> <531f815d-01b5-8155-d0dd-4a6f53e07d53@oracle.com> <e6af83a5-a307-d821-f147-e492ac9484f7@oracle.com> <CADzB+2nXAasqRaUN5z5YdXFZrPv+ridhaaVnA5agp-dTzu7eDg@mail.gmail.com> <f85e7d56-05a8-1f8f-1af6-18862074a2b3@oracle.com>
On Wed, Nov 1, 2017 at 4:45 PM, Mukesh Kapoor <mukesh.kapoor@oracle.com> wrote:
> On 11/1/2017 1:02 PM, Jason Merrill wrote:
>>
>> On Tue, Oct 31, 2017 at 12:17 PM, Mukesh Kapoor
>> <mukesh.kapoor@oracle.com> wrote:
>>>
>>> On 10/25/2017 6:44 PM, Mukesh Kapoor wrote:
>>>>
>>>> On 10/25/2017 4:20 AM, Nathan Sidwell wrote:
>>>>>
>>>>> On 10/25/2017 12:03 AM, Mukesh Kapoor wrote:
>>>>>
>>>>>> Thanks for pointing this out. Checking in the front end will be
>>>>>> difficult because the front end gets tokens after macro expansion. I
>>>>>> think
>>>>>> the difficulty of fixing this bug comes because of the requirement to
>>>>>> maintain backward compatibility with the option -Wliteral-suffix for
>>>>>> -std=c++11.
>>>>>
>>>>>
>>>>> IIUC the warning's intent is to catch cases of:
>>>>> printf ("some format"PRIx64 ..., ...);
>>>>> where there's no space between the string literals and the PRIx64
>>>>> macro.
>>>>> I suspect it's very common for there to be a following string-literal,
>>>>> so
>>>>> perhaps the preprocessor could detect:
>>>>>
>>>>> <string-literal>NON-FN-MACRO<maybe-space><string-literal>
>>>>>
>>>>> and warn on that sequence?
>>>>
>>>>
>>>> Yes, this can be done easily and this is also the usage mentioned in the
>>>> man page. I made this change in the compiler, bootstrapped it and ran
>>>> the
>>>> tests. The following two tests fail after the fix:
>>>>
>>>> g++.dg/cpp0x/Wliteral-suffix.C
>>>> g++.dg/cpp0x/warn_cxx0x4.C
>>>>
>>>> Both tests have code similar to the following (from Wliteral-suffix.C):
>>>>
>>>> #define BAR "bar"
>>>> #define PLUS_ONE + 1
>>>>
>>>> char c = '3'PLUS_ONE; // { dg-warning "invalid suffix on literal" }
>>>> char s[] = "foo"BAR; // { dg-warning "invalid suffix on literal" }
>>>>
>>>> Other compilers don't accept this code. Maybe I should just modify these
>>>> tests to have error messages instead of warnings and submit my revised
>>>> fix?
>>>
>>>
>>> Actually, according to the man page for -Wliteral-suffix, only macro
>>> names
>>> that don't start with an underscore should be considered when issuing a
>>> warning:
>>>
>>> -Wliteral-suffix (C++ and Objective-C++ only)
>>> Warn when a string or character literal is followed by a
>>> ud-suffix
>>> which does not begin with an underscore...
>>>
>>> So the fix is simply to check if the macro name in is_macro() starts with
>>> an
>>> underscore. The function is_macro() is called only at three places. At
>>> two
>>> places it's used to check for the warning related to -Wliteral-suffix and
>>> the check for underscore should be made for these two cases; at one place
>>> it
>>> is used to check for the warning related to -Wc++11-compat and there is
>>> no
>>> need to check for underscore for this case.
>>>
>>> The fix is simply to pass a bool flag as an additional argument to
>>> is_macro() to decide whether the macro name starts with an underscore or
>>> not. I have tested the attached patch on x86_64-linux. Thanks.
>>
>> Rather than add a mysterious parameter to is_macro, how about checking
>> *cur != '_' before we call it?
>
> This is a good suggestion. I have attached the revised patch. Thanks.
OK, thanks!
Jason