[cfe-dev] Patch for c++ operator aliases in preprocessor

Chris Goller goller at gmail.com
Fri Dec 12 11:37:07 PST 2008


Oh whoops, I forgot my patch on the first email... doh!

Well, I've attached two possible patches (I really did this time!).  I'm not
sure what one y'all would like to use.

The first removes the strings altogether and uses the identifiertable.  The
second simply adds the missing operator aliases to the check.

Regards,

Chris

On Fri, Dec 12, 2008 at 11:15 AM, Chris Lattner <clattner at apple.com> wrote:

>
> On Dec 11, 2008, at 7:59 PM, Chris Goller wrote:
>
> Hi, I'm a first time submitter to clang (Doug G pointed me to the
> project).  I dipped my toe into the shallow end and came up with this
> operator alias patch.  All it does is add three more aliases as invalid
> macro tokens.
>
> Also, is there any way to get at the isCPlusPlusOperatorKeyword function at
> that point in the lexer?  If so, I could change the patch so all those
> string compares could be removed.
>
>
> Ah, I see what you're talking about (PPDirectives.cpp):
>
> /// isCXXNamedOperator - Returns "true" if the token is a named operator in
> C++.
> static bool isCXXNamedOperator(const std::string &Spelling) {
>   return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor"||
>     Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
>     Spelling == "or" || Spelling == "xor";
> }
>
> You're right that it is really ugly!  The issue here is that the only
> caller is this code:
>
>   IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
>   if (II == 0) {
>     std::string Spelling = getSpelling(MacroNameTok);
>     if (isCXXNamedOperator(Spelling))
>       // C++ 2.5p2: Alternative tokens behave the same as its primary
> token
>       // except for their spellings.
>       Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) <<
> Spelling;
>     else
>       Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
>
> This call only happens on an error case, so we don't really care about its
> performance.  If you're concerned about the maintenance cost of maintaining
> the extra list of keywords, a good solution would be to re-look-up the
> identifier in the identifier table (in the preprocessor).  This would let
> you query its isCPlusPlusOperatorKeyword field.
>
> -Chris
>



-- 
"Like a poet has to write poetry, I wake up in the morning and I have to
write a computer program." -- Knuth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20081212/b3aed017/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: operator_keyword_string_removal.patch
Type: application/octet-stream
Size: 1316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20081212/b3aed017/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: operator_alias.patch
Type: application/octet-stream
Size: 669 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20081212/b3aed017/attachment-0001.obj>


More information about the cfe-dev mailing list