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

Chris Lattner clattner at apple.com
Fri Dec 12 09:15:59 PST 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20081212/d3a9e848/attachment.html>


More information about the cfe-dev mailing list