curdeius created this revision. curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan. curdeius requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/31086. Before the code: #define if(x) was erroneously formatted to: #define if (x) Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D118844 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1795,6 +1795,18 @@ AllowSimpleBracedStatements); } +TEST_F(FormatTest, UnderstandsMacros) { + verifyFormat("#define A (parentheses)"); + verifyFormat("#define true ((int)1)"); + verifyFormat("#define and(x)"); + verifyFormat("#define if(x) x"); + verifyFormat("#define return(x) (x)"); + verifyFormat("#define while(x) for (; x;)"); + verifyFormat("#define xor(x) (^(x))"); + verifyFormat("#define __except(x)"); + verifyFormat("#define __try(x)"); +} + TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { FormatStyle Style = getLLVMStyleWithColumns(60); Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1035,6 +1035,12 @@ } } + // In the context of a define, even keywords should be treated as normal + // identifiers. Setting the kind to identifier is not enough, because we need + // to treat additional keywords like __except as well, which are already + // identifiers. + FormatTok->Tok.setKind(tok::identifier); + FormatTok->Tok.setIdentifierInfo(nullptr); nextToken(); if (FormatTok->Tok.getKind() == tok::l_paren && !FormatTok->hasWhitespaceBefore())
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1795,6 +1795,18 @@ AllowSimpleBracedStatements); } +TEST_F(FormatTest, UnderstandsMacros) { + verifyFormat("#define A (parentheses)"); + verifyFormat("#define true ((int)1)"); + verifyFormat("#define and(x)"); + verifyFormat("#define if(x) x"); + verifyFormat("#define return(x) (x)"); + verifyFormat("#define while(x) for (; x;)"); + verifyFormat("#define xor(x) (^(x))"); + verifyFormat("#define __except(x)"); + verifyFormat("#define __try(x)"); +} + TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { FormatStyle Style = getLLVMStyleWithColumns(60); Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1035,6 +1035,12 @@ } } + // In the context of a define, even keywords should be treated as normal + // identifiers. Setting the kind to identifier is not enough, because we need + // to treat additional keywords like __except as well, which are already + // identifiers. + FormatTok->Tok.setKind(tok::identifier); + FormatTok->Tok.setIdentifierInfo(nullptr); nextToken(); if (FormatTok->Tok.getKind() == tok::l_paren && !FormatTok->hasWhitespaceBefore())
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits