Patch for c++ operator aliases in preprocessor

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.

Thanks,

Chris

Hi, I'm a first time submitter to clang (Doug G pointed me to the project).

Awesome!

  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.

Do you mean operator aliases like "and" "xor" etc? I think they are already supported in clang,

-Chris

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.

There's no file attached to your posting.

Sebastian

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

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

operator_keyword_string_removal.patch (1.29 KB)

operator_alias.patch (669 Bytes)

Awesome, I applied the first one here:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20081208/010174.html

Thanks Chris!

-Chris