Clang format is incorrectly formatting macros where keywords are redefined
See https://bugs.llvm.org/show_bug.cgi?id=39719
The following code
- #define true ((foo)1) - #define false ((foo)0)
becomes
+ #define true((foo)1) + #define false((foo)0)
Differential D60362
[clang-format] [PR39719] clang-format converting object-like macro to function-like macro Authored by MyDeveloperDay on Apr 6 2019, 5:41 AM.
Details Clang format is incorrectly formatting macros where keywords are redefined See https://bugs.llvm.org/show_bug.cgi?id=39719 The following code - #define true ((foo)1) - #define false ((foo)0) becomes + #define true((foo)1) + #define false((foo)0)
Diff Detail Event Timeline
Comment Actions @klimek one possible solution to this might be to replace the "keyword" back to an identifier in a '#define <keywoord>' scenario Maybe something like this? bool FormatTokenLexer::tryConvertKeyWordDefines() {
// ensure #define keyword x = tok::hash,tok::identifier,tok::identifier
if (Tokens.size() < 3)
return false;
auto &Hash = *(Tokens.end() - 3);
auto &Define = *(Tokens.end() - 2);
auto &Keyword = *(Tokens.end() - 1);
if (!Hash->is(tok::hash))
return false;
if (!Define->is(tok::identifier))
return false;
// Already an identifier
if (Keyword->is(tok::identifier))
return false;
if (!Define->Tok.getIdentifierInfo() ||
Define->Tok.getIdentifierInfo()->getPPKeywordID() != tok::pp_define)
return false;
// switch the type to be an identifier
Keyword->Tok.setKind(tok::identifier);
return true;
}Comment Actions A more straightforward way, IMO, is to add to the spaceRequiredBetween function a separate if statement that returns false for the sequence of tokens: #, define, tok::identifier, and ( Comment Actions Abandoning in favor of D60853: clang-format converts a keyword macro definition to a macro function | ||||||||||||||||||||||||||||||||||||
I think it can be more precise and simplified to something like this:
if (Left.Previous && Left.Previous->is(tok::pp_define) && Left.isNot(tok::identifier) && Right.is(tok::l_paren))