[polly] [clang] [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9938,12 +9984,16 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { "return 1;\n" " }\n" " int g();\n" + " long\n" + " f::br();\n" owenca wrote: Delete it because it's not a definition? https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9906,12 +9944,16 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { verifyFormat("class B {\n" " int f() { return 1; }\n" " int g();\n" + " long\n" + " f::br();\n" "};\n" "int\n" "f() {\n" " return 1;\n" "}\n" - "int g();", + "int g();\n" + "int\n" + "f::bar();", owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9920,13 +9962,17 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { verifyFormat("class C {\n" " int f() { return 1; }\n" " int g();\n" + " long\n" + " f::br();\n" owenca wrote: Delete because it's not top-level? https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -914,16 +914,31 @@ struct FormatStyle { /// Different ways to break after the function definition or /// declaration return type. enum ReturnTypeBreakingStyle : int8_t { -/// Break after return type automatically. -/// ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. +/// This is **deprecated**. See ``Automatic`` below. +RTBS_None, +/// Break after return type based on ``PenaltyReturnTypeOnItsOwnLine``. /// \code /// class A { /// int f() { return 0; }; /// }; /// int f(); /// int f() { return 1; } +/// int +/// LongName::AnotherLongName(); /// \endcode -RTBS_None, +RTBS_Automatic, +/// Same as ``Automatic`` above, expect that there is no break after short owenca wrote: ```suggestion /// Same as ``Automatic`` above, except that there is no break after short ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Allow decltype in requires clause (PR #78847)
owenca wrote: Should we merge this patch? https://github.com/llvm/llvm-project/pull/78847 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -922,8 +922,39 @@ struct FormatStyle { /// }; /// int f(); /// int f() { return 1; } +/// int LongName:: +/// AnotherLongName(); /// \endcode RTBS_None, +/// Break after return type automatically. +/// This mode doesn't have the same inherent restriction on breaking after +/// short return types as ``RTBS_None`` and is solely based on +/// ``PenaltyReturnTypeOnItsOwnLine``. owenca wrote: For example: ```suggestion /// Break after return type based on ``PenaltyReturnTypeOnItsOwnLine``. ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
https://github.com/owenca commented: There is no need to update the release notes for new option values. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -922,8 +922,39 @@ struct FormatStyle { /// }; /// int f(); /// int f() { return 1; } +/// int LongName:: +/// AnotherLongName(); /// \endcode RTBS_None, +/// Break after return type automatically. +/// This mode doesn't have the same inherent restriction on breaking after +/// short return types as ``RTBS_None`` and is solely based on +/// ``PenaltyReturnTypeOnItsOwnLine``. +/// \code +/// class A { +/// int f() { return 0; }; +/// }; +/// int f(); +/// int f() { return 1; } +/// int +/// LongName::AnotherLongName(); +/// \endcode +RTBS_AllowShortType, +/// Break after return type automatically. +/// ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. +/// This mode will never break after short return types, unlike +/// ``RTBS_None`` which will only sometimes choose not to break after short +/// return types. owenca wrote: For example: ```suggestion /// Same as ``Automatic`` above, except that there is no break after short /// return types. ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -922,8 +922,39 @@ struct FormatStyle { /// }; /// int f(); /// int f() { return 1; } +/// int LongName:: +/// AnotherLongName(); /// \endcode owenca wrote: Change the comments (lines 917-927) to something like the following: ``` /// This is **deprecated**. See ``Automatic`` below. ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [polly] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
@@ -922,8 +922,39 @@ struct FormatStyle { /// }; /// int f(); /// int f() { return 1; } +/// int LongName:: +/// AnotherLongName(); /// \endcode RTBS_None, +/// Break after return type automatically. +/// This mode doesn't have the same inherent restriction on breaking after +/// short return types as ``RTBS_None`` and is solely based on +/// ``PenaltyReturnTypeOnItsOwnLine``. +/// \code +/// class A { +/// int f() { return 0; }; +/// }; +/// int f(); +/// int f() { return 1; } +/// int +/// LongName::AnotherLongName(); +/// \endcode +RTBS_AllowShortType, owenca wrote: ```suggestion RTBS_Automatic, ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: > Now that we've got both options, should we keep them? Currently, `None` > sometimes forbids breaking after short return types and sometimes allows it > and just bases things off the penalty configuration. I feel like there will > be some people who use `None` who would want the behaviour to be consistent > one way and the other group wanting it consistent the opposite. With both, > they can decide for themselves. Especially since the implementation for both > is pretty simple and self-contained. +1. In addition, we should deprecate `None` (in documentation only) and rename `AllowShortType` to `Automatic`, followed by `ExceptShortType` which means "`Automatic` except for short return types". Then IMO `ExceptShortType` is unambiguous. If you still think `NeverShortType` is clearer, I would change it to `NotShortType`. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
owenca wrote: See #80241. https://github.com/llvm/llvm-project/pull/79115 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/79115 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #80241)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/80241 Now with a8279a8bc541, we can make the update. >From fda19165bc1195be01159c7d071839822ff3f929 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Wed, 31 Jan 2024 20:43:29 -0800 Subject: [PATCH] [clang-format] Update FormatToken::isSimpleTypeSpecifier() --- clang/include/clang/Format/Format.h | 2 ++ clang/lib/Format/FormatToken.cpp | 35 +-- clang/lib/Format/FormatTokenLexer.cpp | 7 +++--- clang/lib/Format/FormatTokenLexer.h | 1 - 4 files changed, 7 insertions(+), 38 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index efcb4e1d87ea4..361fde1ffece6 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5141,6 +5141,8 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle , ArrayRef Ranges, StringRef FileName = ""); +extern LangOptions LangOpts; + /// Returns the ``LangOpts`` that the formatter expects you to set. /// /// \param Style determines specific settings for lexing mode. diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index b791c5a26bbe3..69f751db89630 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -34,41 +34,8 @@ const char *getTokenTypeName(TokenType Type) { return nullptr; } -// FIXME: This is copy from Sema. Put it in a common place and remove -// duplication. bool FormatToken::isSimpleTypeSpecifier() const { - switch (Tok.getKind()) { - case tok::kw_short: - case tok::kw_long: - case tok::kw___int64: - case tok::kw___int128: - case tok::kw_signed: - case tok::kw_unsigned: - case tok::kw_void: - case tok::kw_char: - case tok::kw_int: - case tok::kw_half: - case tok::kw_float: - case tok::kw_double: - case tok::kw___bf16: - case tok::kw__Float16: - case tok::kw___float128: - case tok::kw___ibm128: - case tok::kw_wchar_t: - case tok::kw_bool: -#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: -#include "clang/Basic/TransformTypeTraits.def" - case tok::annot_typename: - case tok::kw_char8_t: - case tok::kw_char16_t: - case tok::kw_char32_t: - case tok::kw_typeof: - case tok::kw_decltype: - case tok::kw__Atomic: -return true; - default: -return false; - } + return Tok.isSimpleTypeSpecifier(LangOpts); } bool FormatToken::isTypeOrIdentifier() const { diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index d7de09ef0e12a..53fe8a19b2fba 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -22,18 +22,20 @@ namespace clang { namespace format { +LangOptions LangOpts; + FormatTokenLexer::FormatTokenLexer( const SourceManager , FileID ID, unsigned Column, const FormatStyle , encoding::Encoding Encoding, llvm::SpecificBumpPtrAllocator , IdentifierTable ) : FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}), - Column(Column), TrailingWhitespace(0), - LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID), + Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID), Style(Style), IdentTable(IdentTable), Keywords(IdentTable), Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0), FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin), MacroBlockEndRegex(Style.MacroBlockEnd) { + LangOpts = getFormattingLangOpts(Style); Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts)); Lex->SetKeepWhitespaceMode(true); @@ -1442,7 +1444,6 @@ void FormatTokenLexer::readRawToken(FormatToken ) { void FormatTokenLexer::resetLexer(unsigned Offset) { StringRef Buffer = SourceMgr.getBufferData(ID); - LangOpts = getFormattingLangOpts(Style); Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts, Buffer.begin(), Buffer.begin() + Offset, Buffer.end())); Lex->SetKeepWhitespaceMode(true); diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h index 65dd733bd5335..52838f1d8a17f 100644 --- a/clang/lib/Format/FormatTokenLexer.h +++ b/clang/lib/Format/FormatTokenLexer.h @@ -120,7 +120,6 @@ class FormatTokenLexer { unsigned Column; unsigned TrailingWhitespace; std::unique_ptr Lex; - LangOptions LangOpts; const SourceManager FileID ID; const FormatStyle ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Simplify the AfterPlacementOperator option (PR #79796)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/79796 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token (PR #80101)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/80101 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token (PR #80101)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/80101 >From f3ad3ceebba663615e75b5a6fc8969f4a98a03ea Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 30 Jan 2024 19:11:30 -0800 Subject: [PATCH 1/2] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token So that it can be used by clang-format. --- clang/include/clang/Lex/Token.h | 3 +++ clang/include/clang/Sema/Sema.h | 2 -- clang/lib/Lex/Lexer.cpp | 45 + clang/lib/Parse/ParseExpr.cpp | 2 +- clang/lib/Parse/ParseObjc.cpp | 2 +- clang/lib/Sema/SemaDecl.cpp | 45 - 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h index 1409e2c58b550..0a9c73d120de2 100644 --- a/clang/include/clang/Lex/Token.h +++ b/clang/include/clang/Lex/Token.h @@ -13,6 +13,7 @@ #ifndef LLVM_CLANG_LEX_TOKEN_H #define LLVM_CLANG_LEX_TOKEN_H +#include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/TokenKinds.h" #include "llvm/ADT/ArrayRef.h" @@ -288,6 +289,8 @@ class Token { /// Return the ObjC keyword kind. tok::ObjCKeywordKind getObjCKeywordID() const; + bool isSimpleTypeSpecifier(const LangOptions ) const; + /// Return true if this token has trigraphs or escaped newlines in it. bool needsCleaning() const { return getFlag(NeedsCleaning); } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 490df817ddfa2..3b42850a7a85a 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2683,8 +2683,6 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(const Token ) const; - ParsedType getTypeName(const IdentifierInfo , SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS = nullptr, bool isClassName = false, bool HasTrailingDot = false, diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 50b56265f6e16..d927f28b47c27 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -74,6 +74,51 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const { return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; } +/// Determine whether the token kind starts a simple-type-specifier. +bool Token::isSimpleTypeSpecifier(const LangOptions ) const { + switch (getKind()) { + case tok::annot_typename: + case tok::annot_decltype: + case tok::annot_pack_indexing_type: +return true; + + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw___ibm128: + case tok::kw_wchar_t: + case tok::kw_bool: + case tok::kw__Bool: + case tok::kw__Accum: + case tok::kw__Fract: + case tok::kw__Sat: +#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: +#include "clang/Basic/TransformTypeTraits.def" + case tok::kw___auto_type: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::kw_decltype: + case tok::kw_char8_t: +return getIdentifierInfo()->isKeyword(LangOpts); + + default: +return false; + } +} + //===--===// // Lexer Class Implementation //===--===// diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index e5d4285b99183..52cebdb6f64ba 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1609,7 +1609,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, if (TryAnnotateTypeOrScopeToken()) return ExprError(); - if (!Actions.isSimpleTypeSpecifier(Tok)) + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) // We are trying to parse a simple-type-specifier but might not get such // a token after error recovery. return ExprError(); diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 4771b69eadb34..88bab0eb27a3e 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -2971,7 +2971,7 @@ bool Parser::ParseObjCXXMessageReceiver(bool , void *) { tok::annot_cxxscope)) TryAnnotateTypeOrScopeToken(); - if (!Actions.isSimpleTypeSpecifier(Tok)) { + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) { // objc-receiver: // expression // Make sure any typos in the receiver are corrected or diagnosed, so that diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 00c750e42285c..fd1c47008d685 100644 ---
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/79115 >From f3ad3ceebba663615e75b5a6fc8969f4a98a03ea Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 30 Jan 2024 19:11:30 -0800 Subject: [PATCH 1/2] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token So that it can be used by clang-format. --- clang/include/clang/Lex/Token.h | 3 +++ clang/include/clang/Sema/Sema.h | 2 -- clang/lib/Lex/Lexer.cpp | 45 + clang/lib/Parse/ParseExpr.cpp | 2 +- clang/lib/Parse/ParseObjc.cpp | 2 +- clang/lib/Sema/SemaDecl.cpp | 45 - 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h index 1409e2c58b550..0a9c73d120de2 100644 --- a/clang/include/clang/Lex/Token.h +++ b/clang/include/clang/Lex/Token.h @@ -13,6 +13,7 @@ #ifndef LLVM_CLANG_LEX_TOKEN_H #define LLVM_CLANG_LEX_TOKEN_H +#include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/TokenKinds.h" #include "llvm/ADT/ArrayRef.h" @@ -288,6 +289,8 @@ class Token { /// Return the ObjC keyword kind. tok::ObjCKeywordKind getObjCKeywordID() const; + bool isSimpleTypeSpecifier(const LangOptions ) const; + /// Return true if this token has trigraphs or escaped newlines in it. bool needsCleaning() const { return getFlag(NeedsCleaning); } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 490df817ddfa2..3b42850a7a85a 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2683,8 +2683,6 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(const Token ) const; - ParsedType getTypeName(const IdentifierInfo , SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS = nullptr, bool isClassName = false, bool HasTrailingDot = false, diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 50b56265f6e16..d927f28b47c27 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -74,6 +74,51 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const { return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; } +/// Determine whether the token kind starts a simple-type-specifier. +bool Token::isSimpleTypeSpecifier(const LangOptions ) const { + switch (getKind()) { + case tok::annot_typename: + case tok::annot_decltype: + case tok::annot_pack_indexing_type: +return true; + + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw___ibm128: + case tok::kw_wchar_t: + case tok::kw_bool: + case tok::kw__Bool: + case tok::kw__Accum: + case tok::kw__Fract: + case tok::kw__Sat: +#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: +#include "clang/Basic/TransformTypeTraits.def" + case tok::kw___auto_type: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::kw_decltype: + case tok::kw_char8_t: +return getIdentifierInfo()->isKeyword(LangOpts); + + default: +return false; + } +} + //===--===// // Lexer Class Implementation //===--===// diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index e5d4285b99183..52cebdb6f64ba 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1609,7 +1609,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, if (TryAnnotateTypeOrScopeToken()) return ExprError(); - if (!Actions.isSimpleTypeSpecifier(Tok)) + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) // We are trying to parse a simple-type-specifier but might not get such // a token after error recovery. return ExprError(); diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 4771b69eadb34..88bab0eb27a3e 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -2971,7 +2971,7 @@ bool Parser::ParseObjCXXMessageReceiver(bool , void *) { tok::annot_cxxscope)) TryAnnotateTypeOrScopeToken(); - if (!Actions.isSimpleTypeSpecifier(Tok)) { + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) { // objc-receiver: // expression // Make sure any typos in the receiver are corrected or diagnosed, so that diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 00c750e42285c..fd1c47008d685 100644 ---
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/79115 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/79115 >From f3ad3ceebba663615e75b5a6fc8969f4a98a03ea Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 30 Jan 2024 19:11:30 -0800 Subject: [PATCH] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token So that it can be used by clang-format. --- clang/include/clang/Lex/Token.h | 3 +++ clang/include/clang/Sema/Sema.h | 2 -- clang/lib/Lex/Lexer.cpp | 45 + clang/lib/Parse/ParseExpr.cpp | 2 +- clang/lib/Parse/ParseObjc.cpp | 2 +- clang/lib/Sema/SemaDecl.cpp | 45 - 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h index 1409e2c58b550..0a9c73d120de2 100644 --- a/clang/include/clang/Lex/Token.h +++ b/clang/include/clang/Lex/Token.h @@ -13,6 +13,7 @@ #ifndef LLVM_CLANG_LEX_TOKEN_H #define LLVM_CLANG_LEX_TOKEN_H +#include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/TokenKinds.h" #include "llvm/ADT/ArrayRef.h" @@ -288,6 +289,8 @@ class Token { /// Return the ObjC keyword kind. tok::ObjCKeywordKind getObjCKeywordID() const; + bool isSimpleTypeSpecifier(const LangOptions ) const; + /// Return true if this token has trigraphs or escaped newlines in it. bool needsCleaning() const { return getFlag(NeedsCleaning); } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 490df817ddfa2..3b42850a7a85a 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2683,8 +2683,6 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(const Token ) const; - ParsedType getTypeName(const IdentifierInfo , SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS = nullptr, bool isClassName = false, bool HasTrailingDot = false, diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 50b56265f6e16..d927f28b47c27 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -74,6 +74,51 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const { return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; } +/// Determine whether the token kind starts a simple-type-specifier. +bool Token::isSimpleTypeSpecifier(const LangOptions ) const { + switch (getKind()) { + case tok::annot_typename: + case tok::annot_decltype: + case tok::annot_pack_indexing_type: +return true; + + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw___ibm128: + case tok::kw_wchar_t: + case tok::kw_bool: + case tok::kw__Bool: + case tok::kw__Accum: + case tok::kw__Fract: + case tok::kw__Sat: +#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: +#include "clang/Basic/TransformTypeTraits.def" + case tok::kw___auto_type: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::kw_decltype: + case tok::kw_char8_t: +return getIdentifierInfo()->isKeyword(LangOpts); + + default: +return false; + } +} + //===--===// // Lexer Class Implementation //===--===// diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index e5d4285b99183..52cebdb6f64ba 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1609,7 +1609,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, if (TryAnnotateTypeOrScopeToken()) return ExprError(); - if (!Actions.isSimpleTypeSpecifier(Tok)) + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) // We are trying to parse a simple-type-specifier but might not get such // a token after error recovery. return ExprError(); diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 4771b69eadb34..88bab0eb27a3e 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -2971,7 +2971,7 @@ bool Parser::ParseObjCXXMessageReceiver(bool , void *) { tok::annot_cxxscope)) TryAnnotateTypeOrScopeToken(); - if (!Actions.isSimpleTypeSpecifier(Tok)) { + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) { // objc-receiver: // expression // Make sure any typos in the receiver are corrected or diagnosed, so that diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 00c750e42285c..fd1c47008d685 100644 ---
[clang] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token (PR #80101)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/80101 So that it can be used by clang-format. >From f3ad3ceebba663615e75b5a6fc8969f4a98a03ea Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 30 Jan 2024 19:11:30 -0800 Subject: [PATCH] [clang][NFC] Move isSimpleTypeSpecifier() from Sema to Token So that it can be used by clang-format. --- clang/include/clang/Lex/Token.h | 3 +++ clang/include/clang/Sema/Sema.h | 2 -- clang/lib/Lex/Lexer.cpp | 45 + clang/lib/Parse/ParseExpr.cpp | 2 +- clang/lib/Parse/ParseObjc.cpp | 2 +- clang/lib/Sema/SemaDecl.cpp | 45 - 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h index 1409e2c58b550..0a9c73d120de2 100644 --- a/clang/include/clang/Lex/Token.h +++ b/clang/include/clang/Lex/Token.h @@ -13,6 +13,7 @@ #ifndef LLVM_CLANG_LEX_TOKEN_H #define LLVM_CLANG_LEX_TOKEN_H +#include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/TokenKinds.h" #include "llvm/ADT/ArrayRef.h" @@ -288,6 +289,8 @@ class Token { /// Return the ObjC keyword kind. tok::ObjCKeywordKind getObjCKeywordID() const; + bool isSimpleTypeSpecifier(const LangOptions ) const; + /// Return true if this token has trigraphs or escaped newlines in it. bool needsCleaning() const { return getFlag(NeedsCleaning); } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 490df817ddfa2..3b42850a7a85a 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2683,8 +2683,6 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(const Token ) const; - ParsedType getTypeName(const IdentifierInfo , SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS = nullptr, bool isClassName = false, bool HasTrailingDot = false, diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 50b56265f6e16..d927f28b47c27 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -74,6 +74,51 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const { return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; } +/// Determine whether the token kind starts a simple-type-specifier. +bool Token::isSimpleTypeSpecifier(const LangOptions ) const { + switch (getKind()) { + case tok::annot_typename: + case tok::annot_decltype: + case tok::annot_pack_indexing_type: +return true; + + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw___ibm128: + case tok::kw_wchar_t: + case tok::kw_bool: + case tok::kw__Bool: + case tok::kw__Accum: + case tok::kw__Fract: + case tok::kw__Sat: +#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: +#include "clang/Basic/TransformTypeTraits.def" + case tok::kw___auto_type: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::kw_decltype: + case tok::kw_char8_t: +return getIdentifierInfo()->isKeyword(LangOpts); + + default: +return false; + } +} + //===--===// // Lexer Class Implementation //===--===// diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index e5d4285b99183..52cebdb6f64ba 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1609,7 +1609,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, if (TryAnnotateTypeOrScopeToken()) return ExprError(); - if (!Actions.isSimpleTypeSpecifier(Tok)) + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) // We are trying to parse a simple-type-specifier but might not get such // a token after error recovery. return ExprError(); diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 4771b69eadb34..88bab0eb27a3e 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -2971,7 +2971,7 @@ bool Parser::ParseObjCXXMessageReceiver(bool , void *) { tok::annot_cxxscope)) TryAnnotateTypeOrScopeToken(); - if (!Actions.isSimpleTypeSpecifier(Tok)) { + if (!Tok.isSimpleTypeSpecifier(getLangOpts())) { // objc-receiver: // expression // Make sure any typos in the receiver are corrected or diagnosed, so that diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index
[clang] [clang-format] Handles Elaborated type specifier for enum in trailing return (PR #80085)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/80085 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [libcxx] [compiler-rt] [llvm] [clang] [libc] [clang] Improved isSimpleTypeSpecifier (PR #79037)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType and ExceptShortType options for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: > Let's leave `None` alone and add `ExceptShortType` I meant to add `ExceptShortType` instead of `AllowShortType`. Sorry I didn't realize that adding `AllowShortType` and making it to mean "not `None`" might work, in which case we don't need to add `ExceptShortType`. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handles Elaborated type specifier for enum in trailing return (PR #80085)
@@ -1760,8 +1760,8 @@ void UnwrappedLineParser::parseStructuralElement( break; } case tok::kw_enum: - // Ignore if this is part of "template is(tok::less)) { + // Ignore if this is part of "template enum". + if (Previous && (Previous->is(tok::less) || Previous->is(tok::arrow))) { owenca wrote: You can use `isOneOf`. https://github.com/llvm/llvm-project/pull/80085 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Explicitly open DOC_FILE with utf-8 in dump_format_style.py (PR #79805)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/79805 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Explicitly open DOC_FILE with utf-8 in dump_format_style.py (PR #79805)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/79805 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
owenca wrote: > Does it look like a genuine failure I should address or a spurious build > quirk? The latter. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: Let's leave `None` alone and add `ExceptShortType`, e.g.: ``` --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -329,12 +329,18 @@ bool ContinuationIndenter::canBreak(const LineState ) { // Don't break after very short return types (e.g. "void") as that is often // unexpected. if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) { if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) return false; } + if (Current.is(TT_FunctionDeclarationName) && + Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) { +assert(State.Column >= State.FirstIndent); +if (State.Column - State.FirstIndent < 6) + return false; + } // If binary operators are moved to the next line (including commas for some // styles of constructor initializers), that's always ok. if (!Current.isOneOf(TT_BinaryOperator, tok::comma) && // Allow breaking opening brace of lambdas (when passed as function // arguments) to a new line when BeforeLambdaBody brace wrapping is ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle generic selections inside parentheses (PR #79785)
@@ -1694,8 +1694,11 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState , // Special case for generic selection expressions, its comma-separated // expressions are not aligned to the opening paren like regular calls, but // rather continuation-indented relative to the _Generic keyword. -if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic)) - NewParenState.Indent = CurrentState.LastSpace; +if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic) && +State.Stack.size() >= 2) { + NewParenState.Indent = + State.Stack.end()[-2].Indent + Style.ContinuationIndentWidth; owenca wrote: To be consistent with the other dozen similar sinppets: ```suggestion State.Stack.size() > 1) { NewParenState.Indent = State.Stack[State.Stack.size() - 2].Indent + Style.ContinuationIndentWidth; ``` https://github.com/llvm/llvm-project/pull/79785 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Simplify the AfterPlacementOperator option (PR #79796)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/79796 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Simplify the AfterPlacementOperator option (PR #79796)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/79796 >From d40f2ed71e72c9cee266e63abbae4b9aac1de47c Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 29 Jan 2024 00:43:19 -0800 Subject: [PATCH 1/2] [clang-format] Simplify the AfterPlacementOperator option Change AfterPlacementOperator to a boolean. Also add SBPO_None for never inserting a space before a left parenthesis and deprecate SBPO_Never, which meant never inserting a space except when after new/delete. Fixes #78892. --- clang/docs/ClangFormatStyleOptions.rst | 37 +++--- clang/include/clang/Format/Format.h| 36 - clang/lib/Format/Format.cpp| 32 +-- clang/lib/Format/TokenAnnotator.cpp| 13 ++-- clang/unittests/Format/ConfigParseTest.cpp | 27 clang/unittests/Format/FormatTest.cpp | 32 --- 6 files changed, 56 insertions(+), 121 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 4dc0de3a90f26..ce4bf867b2619 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5276,7 +5276,7 @@ the configuration (without a prefix: ``Auto``). Possible values: - * ``SBPO_Never`` (in configuration: ``Never``) + * ``SBPO_None`` (in configuration: ``None``) Never put a space before opening parentheses. .. code-block:: c++ @@ -5287,6 +5287,11 @@ the configuration (without a prefix: ``Auto``). } } + * ``SBPO_Never`` (in configuration: ``Never``) +This is **deprecated** and replaced by ``Custom`` below, with all +``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +``false``. + * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) Put a space before opening parentheses only after control statement keywords (``for/if/while...``). @@ -5425,32 +5430,14 @@ the configuration (without a prefix: ``Auto``). void operator++ (int a);vs.void operator++(int a); object.operator++ (10);object.operator++(10); - * ``AfterPlacementOperatorStyle AfterPlacementOperator`` :versionbadge:`clang-format 18` - -Defines in which cases to put a space between ``new/delete`` operators -and opening parentheses. + * ``bool AfterPlacementOperator`` If ``true``, put a space between operator ``new``/``delete`` and opening +parenthesis. -Possible values: - -* ``APO_Never`` (in configuration: ``Never``) - Remove space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new(buf) T; - delete(buf) T; - -* ``APO_Always`` (in configuration: ``Always``) - Always add space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new (buf) T; - delete (buf) T; - -* ``APO_Leave`` (in configuration: ``Leave``) - Leave placement ``new/delete`` expressions as they are. +.. code-block:: c++ + true: false: + new (buf) T;vs.new(buf) T; + delete (buf) T;delete(buf) T; * ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and opening parentheses, if there is one. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index bc9eecd42f9eb..5c536bc3f381f 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4165,6 +4165,10 @@ struct FormatStyle { /// } ///} /// \endcode +SBPO_None, +/// This is **deprecated** and replaced by ``Custom`` below, with all +/// ``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +/// ``false``. SBPO_Never, /// Put a space before opening parentheses only after control statement /// keywords (``for/if/while...``). @@ -4273,28 +4277,14 @@ struct FormatStyle { ///object.operator++ (10);object.operator++(10); /// \endcode bool AfterOverloadedOperator; -/// Styles for adding spacing between ``new/delete`` operators and opening -/// parentheses. -enum AfterPlacementOperatorStyle : int8_t { - /// Remove space after ``new/delete`` operators and before ``(``. - /// \code - ///new(buf) T; - ///delete(buf) T; - /// \endcode - APO_Never, - /// Always add space after ``new/delete`` operators and before ``(``. - /// \code - ///new (buf) T; - ///delete (buf) T; - /// \endcode - APO_Always, - /// Leave placement ``new/delete`` expressions as they are. - APO_Leave, -}; -/// Defines in which cases to put a space between ``new/delete`` operators -/// and opening parentheses. -/// \version 18 -AfterPlacementOperatorStyle
[clang] [clang-format] Simplify the AfterPlacementOperator option (PR #79796)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/79796 >From d40f2ed71e72c9cee266e63abbae4b9aac1de47c Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 29 Jan 2024 00:43:19 -0800 Subject: [PATCH] [clang-format] Simplify the AfterPlacementOperator option Change AfterPlacementOperator to a boolean. Also add SBPO_None for never inserting a space before a left parenthesis and deprecate SBPO_Never, which meant never inserting a space except when after new/delete. Fixes #78892. --- clang/docs/ClangFormatStyleOptions.rst | 37 +++--- clang/include/clang/Format/Format.h| 36 - clang/lib/Format/Format.cpp| 32 +-- clang/lib/Format/TokenAnnotator.cpp| 13 ++-- clang/unittests/Format/ConfigParseTest.cpp | 27 clang/unittests/Format/FormatTest.cpp | 32 --- 6 files changed, 56 insertions(+), 121 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 4dc0de3a90f2650..ce4bf867b26195f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5276,7 +5276,7 @@ the configuration (without a prefix: ``Auto``). Possible values: - * ``SBPO_Never`` (in configuration: ``Never``) + * ``SBPO_None`` (in configuration: ``None``) Never put a space before opening parentheses. .. code-block:: c++ @@ -5287,6 +5287,11 @@ the configuration (without a prefix: ``Auto``). } } + * ``SBPO_Never`` (in configuration: ``Never``) +This is **deprecated** and replaced by ``Custom`` below, with all +``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +``false``. + * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) Put a space before opening parentheses only after control statement keywords (``for/if/while...``). @@ -5425,32 +5430,14 @@ the configuration (without a prefix: ``Auto``). void operator++ (int a);vs.void operator++(int a); object.operator++ (10);object.operator++(10); - * ``AfterPlacementOperatorStyle AfterPlacementOperator`` :versionbadge:`clang-format 18` - -Defines in which cases to put a space between ``new/delete`` operators -and opening parentheses. + * ``bool AfterPlacementOperator`` If ``true``, put a space between operator ``new``/``delete`` and opening +parenthesis. -Possible values: - -* ``APO_Never`` (in configuration: ``Never``) - Remove space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new(buf) T; - delete(buf) T; - -* ``APO_Always`` (in configuration: ``Always``) - Always add space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new (buf) T; - delete (buf) T; - -* ``APO_Leave`` (in configuration: ``Leave``) - Leave placement ``new/delete`` expressions as they are. +.. code-block:: c++ + true: false: + new (buf) T;vs.new(buf) T; + delete (buf) T;delete(buf) T; * ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and opening parentheses, if there is one. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index bc9eecd42f9ebfd..5c536bc3f381fab 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4165,6 +4165,10 @@ struct FormatStyle { /// } ///} /// \endcode +SBPO_None, +/// This is **deprecated** and replaced by ``Custom`` below, with all +/// ``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +/// ``false``. SBPO_Never, /// Put a space before opening parentheses only after control statement /// keywords (``for/if/while...``). @@ -4273,28 +4277,14 @@ struct FormatStyle { ///object.operator++ (10);object.operator++(10); /// \endcode bool AfterOverloadedOperator; -/// Styles for adding spacing between ``new/delete`` operators and opening -/// parentheses. -enum AfterPlacementOperatorStyle : int8_t { - /// Remove space after ``new/delete`` operators and before ``(``. - /// \code - ///new(buf) T; - ///delete(buf) T; - /// \endcode - APO_Never, - /// Always add space after ``new/delete`` operators and before ``(``. - /// \code - ///new (buf) T; - ///delete (buf) T; - /// \endcode - APO_Always, - /// Leave placement ``new/delete`` expressions as they are. - APO_Leave, -}; -/// Defines in which cases to put a space between ``new/delete`` operators -/// and opening parentheses. -/// \version 18 -
[clang] [clang-format] Simplify the AfterPlacementOperator option (PR #79796)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/79796 Change AfterPlacementOperator to a boolean. Also add SBPO_None for never inserting a space before a left parenthesis and deprecate SBPO_Never, which meant never inserting a space except when after new/delete. Fixes #78892. >From 46f4a74f1bdb90751b6b2beb7c0e49c6ee83d2e3 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 29 Jan 2024 00:43:19 -0800 Subject: [PATCH] [clang-format] Simplify the AfterPlacementOperator option Change AfterPlacementOperator to a boolean. Also add SBPO_None for never inserting a space before a left parenthesis and deprecate SBPO_Never, which meant never inserting a space except when after new/delete. Fixes #78892. --- clang/docs/ClangFormatStyleOptions.rst | 37 +++--- clang/include/clang/Format/Format.h| 36 - clang/lib/Format/Format.cpp| 28 clang/lib/Format/TokenAnnotator.cpp| 9 +- clang/unittests/Format/ConfigParseTest.cpp | 21 ++-- clang/unittests/Format/FormatTest.cpp | 32 --- 6 files changed, 49 insertions(+), 114 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 4dc0de3a90f265..ce4bf867b26195 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -5276,7 +5276,7 @@ the configuration (without a prefix: ``Auto``). Possible values: - * ``SBPO_Never`` (in configuration: ``Never``) + * ``SBPO_None`` (in configuration: ``None``) Never put a space before opening parentheses. .. code-block:: c++ @@ -5287,6 +5287,11 @@ the configuration (without a prefix: ``Auto``). } } + * ``SBPO_Never`` (in configuration: ``Never``) +This is **deprecated** and replaced by ``Custom`` below, with all +``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +``false``. + * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) Put a space before opening parentheses only after control statement keywords (``for/if/while...``). @@ -5425,32 +5430,14 @@ the configuration (without a prefix: ``Auto``). void operator++ (int a);vs.void operator++(int a); object.operator++ (10);object.operator++(10); - * ``AfterPlacementOperatorStyle AfterPlacementOperator`` :versionbadge:`clang-format 18` - -Defines in which cases to put a space between ``new/delete`` operators -and opening parentheses. + * ``bool AfterPlacementOperator`` If ``true``, put a space between operator ``new``/``delete`` and opening +parenthesis. -Possible values: - -* ``APO_Never`` (in configuration: ``Never``) - Remove space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new(buf) T; - delete(buf) T; - -* ``APO_Always`` (in configuration: ``Always``) - Always add space after ``new/delete`` operators and before ``(``. - - .. code-block:: c++ - - new (buf) T; - delete (buf) T; - -* ``APO_Leave`` (in configuration: ``Leave``) - Leave placement ``new/delete`` expressions as they are. +.. code-block:: c++ + true: false: + new (buf) T;vs.new(buf) T; + delete (buf) T;delete(buf) T; * ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and opening parentheses, if there is one. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index bc9eecd42f9ebf..5c536bc3f381fa 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4165,6 +4165,10 @@ struct FormatStyle { /// } ///} /// \endcode +SBPO_None, +/// This is **deprecated** and replaced by ``Custom`` below, with all +/// ``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to +/// ``false``. SBPO_Never, /// Put a space before opening parentheses only after control statement /// keywords (``for/if/while...``). @@ -4273,28 +4277,14 @@ struct FormatStyle { ///object.operator++ (10);object.operator++(10); /// \endcode bool AfterOverloadedOperator; -/// Styles for adding spacing between ``new/delete`` operators and opening -/// parentheses. -enum AfterPlacementOperatorStyle : int8_t { - /// Remove space after ``new/delete`` operators and before ``(``. - /// \code - ///new(buf) T; - ///delete(buf) T; - /// \endcode - APO_Never, - /// Always add space after ``new/delete`` operators and before ``(``. - /// \code - ///new (buf) T; - ///delete (buf) T; - /// \endcode - APO_Always, - /// Leave placement ``new/delete``
[polly] [clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -587,7 +590,7 @@ bool ContinuationIndenter::mustBreak(const LineState ) { !State.Line->ReturnTypeWrapped && // Don't break before a C# function when no break after return type. (!Style.isCSharp() || - Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) && + Style.AlwaysBreakAfterReturnType > FormatStyle::RTBS_AllowShortType) && owenca wrote: What I suggested is more consistent with the existing code in clang-format. Also, explicitly including/excluding every enum value is cumbersome and doesn't provide any real advantage, IMO. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: > @mydeveloperday @HazardyKnusperkeks @rymiel this patch fixes a very old bug > and will cause behavior changes whether the default is changed to the new > `AllowShortType` or left at `None`. Which way should we go? Now I'm leaning toward keeping the existing (buggy) behavior of `None` and using the new `AllowShortType` to allow wrapping after short return types. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: > What would change if the default was kept at `None`? I don't see it. See https://github.com/llvm/llvm-project/pull/78011#issuecomment-1913052884. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
owenca wrote: LGTM, but please rebase and resolve conflicts. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] 179ade6 - [clang-format] Fix an issue reported by static analyzer
Author: Owen Pan Date: 2024-01-27T21:21:01-08:00 New Revision: 179ade6a6d78533179205640dd161bf179daa9ee URL: https://github.com/llvm/llvm-project/commit/179ade6a6d78533179205640dd161bf179daa9ee DIFF: https://github.com/llvm/llvm-project/commit/179ade6a6d78533179205640dd161bf179daa9ee.diff LOG: [clang-format] Fix an issue reported by static analyzer Fixes #79685. Added: Modified: clang/lib/Format/WhitespaceManager.cpp Removed: diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index df84f97a8e8ac1..f9eed7f516bbeb 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -1469,7 +1469,7 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start, while (NextNonComment->is(tok::comma)) NextNonComment = NextNonComment->getNextNonComment(); auto j = i; -while (Changes[j].Tok != NextNonComment && j < End) +while (j < End && Changes[j].Tok != NextNonComment) ++j; if (j < End && Changes[j].NewlinesBefore == 0 && Changes[j].Tok->isNot(tok::r_brace)) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[polly] [clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: @llvm/pr-subscribers-clang-format this patch fixes a very old bug and will cause behavior changes whether the default is changed to the new `AllowShortType` or left at `None`. Which way should we go? https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: OTOH, if we change `LLVMStyle.AlwaysBreakAfterReturnType` to `RTBS_AllowShortType`, we get the following: ``` $ ninja polly-check-format [24/105] Checking format of /Users/Owe...olly/include/polly/DependenceInfo.h... FAILED: tools/polly/polly-check-format16 /Users/Owen/llvm-project/build/tools/polly/polly-check-format16 cd /Users/Owen/llvm-project/build/tools/polly && /Users/Owen/llvm-project/build/bin/clang-format -sort-includes -style=llvm /Users/Owen/llvm-project/polly/include/polly/DependenceInfo.h | diff -u /Users/Owen/llvm-project/polly/include/polly/DependenceInfo.h - --- /Users/Owen/llvm-project/polly/include/polly/DependenceInfo.h 2024-01-05 20:24:43 +++ - 2024-01-27 02:13:13 @@ -332,8 +332,8 @@ void initializeDependenceInfoPass(llvm::PassRegistry &); void initializeDependenceInfoPrinterLegacyPassPass(llvm::PassRegistry &); void initializeDependenceInfoWrapperPassPass(llvm::PassRegistry &); -void initializeDependenceInfoPrinterLegacyFunctionPassPass( -llvm::PassRegistry &); +void +initializeDependenceInfoPrinterLegacyFunctionPassPass(llvm::PassRegistry &); } // namespace llvm #endif [26/105] Checking format of /Users/Owe...polly/include/polly/LinkAllPasses.h... FAILED: tools/polly/polly-check-format21 /Users/Owen/llvm-project/build/tools/polly/polly-check-format21 cd /Users/Owen/llvm-project/build/tools/polly && /Users/Owen/llvm-project/build/bin/clang-format -sort-includes -style=llvm /Users/Owen/llvm-project/polly/include/polly/LinkAllPasses.h | diff -u /Users/Owen/llvm-project/polly/include/polly/LinkAllPasses.h - --- /Users/Owen/llvm-project/polly/include/polly/LinkAllPasses.h 2024-01-05 20:58:16 +++ - 2024-01-27 02:13:13 @@ -138,8 +138,8 @@ void initializeDependenceInfoPass(llvm::PassRegistry &); void initializeDependenceInfoPrinterLegacyPassPass(llvm::PassRegistry &); void initializeDependenceInfoWrapperPassPass(llvm::PassRegistry &); -void initializeDependenceInfoPrinterLegacyFunctionPassPass( -llvm::PassRegistry &); +void +initializeDependenceInfoPrinterLegacyFunctionPassPass(llvm::PassRegistry &); void initializeIslAstInfoWrapperPassPass(llvm::PassRegistry &); void initializeIslAstInfoPrinterLegacyPassPass(llvm::PassRegistry &); void initializeCodeGenerationPass(llvm::PassRegistry &); [33/105] Checking format of /Users/Owe...ect/polly/unittests/Isl/IslTest.cpp... ninja: build stopped: subcommand failed. ``` which is an improvement IMO. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
owenca wrote: This would break polly: ``` $ ninja polly-check-format [27/105] Checking format of /Users/Owe...ude/polly/CodeGen/BlockGenerators.h... FAILED: tools/polly/polly-check-format1 /Users/Owen/llvm-project/build/tools/polly/polly-check-format1 cd /Users/Owen/llvm-project/build/tools/polly && /Users/Owen/llvm-project/build/bin/clang-format -sort-includes -style=llvm /Users/Owen/llvm-project/polly/include/polly/CodeGen/BlockGenerators.h | diff -u /Users/Owen/llvm-project/polly/include/polly/CodeGen/BlockGenerators.h - --- /Users/Owen/llvm-project/polly/include/polly/CodeGen/BlockGenerators.h 2024-01-05 20:58:16 +++ - 2024-01-26 23:25:21 @@ -794,9 +794,9 @@ /// @param BBMap A mapping from old values to their new values in this block. /// @param LTS A mapping from loops virtual canonical induction variable to /// their new values. - void - generateScalarStores(ScopStmt , LoopToScevMapT , ValueMapT , - __isl_keep isl_id_to_ast_expr *NewAccesses) override; + void generateScalarStores( + ScopStmt , LoopToScevMapT , ValueMapT , + __isl_keep isl_id_to_ast_expr *NewAccesses) override; /// Copy a single PHI instruction. /// [36/105] Checking format of /Users/Owe...ject/polly/include/polly/ScopInfo.h... FAILED: tools/polly/polly-check-format35 /Users/Owen/llvm-project/build/tools/polly/polly-check-format35 cd /Users/Owen/llvm-project/build/tools/polly && /Users/Owen/llvm-project/build/bin/clang-format -sort-includes -style=llvm /Users/Owen/llvm-project/polly/include/polly/ScopInfo.h | diff -u /Users/Owen/llvm-project/polly/include/polly/ScopInfo.h - --- /Users/Owen/llvm-project/polly/include/polly/ScopInfo.h 2024-01-22 03:28:44 +++ - 2024-01-26 23:25:21 @@ -1987,8 +1987,8 @@ void addAccessData(MemoryAccess *Access); /// Add new invariant access equivalence class - void - addInvariantEquivClass(const InvariantEquivClassTy ) { + void addInvariantEquivClass( + const InvariantEquivClassTy ) { InvariantEquivClasses.emplace_back(InvariantEquivClass); } ninja: build stopped: subcommand failed. ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
@@ -156,24 +161,19 @@ bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: #include "clang/Basic/TransformTypeTraits.def" case tok::kw___auto_type: -return true; - - case tok::annot_typename: + case tok::kw__Bool: case tok::kw_char16_t: case tok::kw_char32_t: case tok::kw_typeof: - case tok::annot_decltype: case tok::kw_decltype: -return getLangOpts().CPlusPlus; - case tok::kw_char8_t: -return getLangOpts().Char8; +if (!Tok.hasIdentifierInfo()) + return false; +return Tok.getIdentifierInfo()->isKeyword(LangOpts); owenca wrote: ```suggestion return Tok.getIdentifierInfo()->isKeyword(getLangOpts()); ``` https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
@@ -196,6 +196,13 @@ class Token { PtrData = (void*) II; } + bool hasIdentifierInfo() { +if (is(tok::raw_identifier) || isAnnotation() || isLiteral() || +is(tok::eof)) + return false; +return true; + } + owenca wrote: IMO we don't need this. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
@@ -128,10 +128,15 @@ class TypeNameValidatorCCC final : public CorrectionCandidateCallback { } // end anonymous namespace /// Determine whether the token kind starts a simple-type-specifier. -bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { +bool Sema::isSimpleTypeSpecifier(Token ) const { + auto Kind = Tok.getKind(); + auto LangOpts = getLangOpts(); + switch (Kind) { owenca wrote: ```suggestion bool Sema::isSimpleTypeSpecifier(const Token ) const { switch (Tok.getKind()) { ``` https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
@@ -2636,7 +2636,7 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(tok::TokenKind Kind) const; + bool isSimpleTypeSpecifier(Token ) const; owenca wrote: ```suggestion bool isSimpleTypeSpecifier(const Token ) const; ``` https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
@@ -156,24 +161,19 @@ bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: #include "clang/Basic/TransformTypeTraits.def" case tok::kw___auto_type: -return true; - - case tok::annot_typename: + case tok::kw__Bool: owenca wrote: I would move it up, to just below `kw_bool`. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in AnnotatingParser::rParenEndsCast() (PR #79549)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/79549 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -326,11 +326,13 @@ bool ContinuationIndenter::canBreak(const LineState ) { return false; } - // Don't break after very short return types (e.g. "void") as that is often - // unexpected. - if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) { -if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) + if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) { +// Don't break after very short return types (e.g. "void") as that is often +// unexpected. +if (Current.is(TT_FunctionDeclarationName) && +State.Column - State.FirstIndent < 6) { owenca wrote: We probably need to assert that `State.Column >= State.FirstIndent`. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -146,7 +146,7 @@ the configuration (without a prefix: ``Auto``). .. _BasedOnStyle: -**BasedOnStyle** (``String``) :ref:`¶ ` +**BasedOnStyle** (``String``) :ref:`¶ ` owenca wrote: ```suggestion **BasedOnStyle** (``String``) :ref:`¶ ` ``` Was this generated on Windows? It's possible that it was caused by https://reviews.llvm.org/D127270. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9938,12 +9980,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { "return 1;\n" " }\n" " int g();\n" + " long\n" + " " + "fooo::" owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9920,13 +9955,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { verifyFormat("class C {\n" " int f() { return 1; }\n" " int g();\n" + " long\n" + " " + "fooo::" owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9891,13 +9912,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { " }\n" " int\n" " g();\n" + " long\n" + " " + "fooo::" owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -587,7 +589,9 @@ bool ContinuationIndenter::mustBreak(const LineState ) { !State.Line->ReturnTypeWrapped && // Don't break before a C# function when no break after return type. (!Style.isCSharp() || - Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) && + (Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None && +Style.AlwaysBreakAfterReturnType != +FormatStyle::RTBS_AllowShortType)) && owenca wrote: ```suggestion Style.AlwaysBreakAfterReturnType > FormatStyle::RTBS_AllowShortType) && ``` https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9906,12 +9934,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { verifyFormat("class B {\n" " int f() { return 1; }\n" " int g();\n" + " long\n" + " " + "fooo::" owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AllowShortType option for AlwaysBreakAfterReturnType. (PR #78011)
@@ -9872,9 +9872,30 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { verifyFormat("class A {\n" " int f() { return 1; }\n" " int g();\n" + " long fooo::\n" + " br();\n" "};\n" "int f() { return 1; }\n" - "int g();", + "int g();\n" + "int f::\n" + "bar();", + Style); + + // It is now allowed to break after a short return type if necessary. + Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllowShortType; + verifyFormat("class A {\n" + " int f() { return 1; }\n" + " int g();\n" + " long\n" + " " + "fooo::" owenca wrote: ```suggestion " fooo::" ``` Alternatively, you can set `ColumnLimit` to 60 and shorten the function name. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
@@ -4569,23 +4602,40 @@ struct FormatStyle { bool InEmptyParentheses; /// Put a space in parentheses not covered by preceding options. /// \code -///true: false: -///t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; +/// true: +/// t f( Deleted & ) & = delete; +/// decltype( ( x ) ) +/// x = ( (int32)y ) +/// y = ( (int ( * )( int ))foo )( x ); +/// \endcode +/// \code +/// false: +/// t f(Deleted &) & = delete; +/// decltype((x)) owenca wrote: ```suggestion ///__attribute__((noreturn)) /// decltype((x)) ``` https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
@@ -4542,18 +4542,51 @@ struct FormatStyle { /// Other: true /// \endcode struct SpacesInParensCustom { +/// Override any of the following options to prevent addition of space +/// between the first two parentheses in situations where a pair of +/// parentheses have been used. +/// \code +/// true: +/// __attribute__(( noreturn )) +/// __decltype__(( x )) +/// if (( a = b )) +/// \endcode +/// false: +///Uses the applicable option. +bool ExceptDoubleParentheses; +/// Put a space in parentheses of attribute specifiers. +/// \code +///true: +///__attribute__( ( noreturn ) ) +/// \endcode +/// \code +///false: +///_attribute__((noreturn)) +/// \endcode +bool InAttributeSpecifiers; owenca wrote: I still think this is too specific. It should be included in `Other` below. https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
@@ -4569,23 +4602,40 @@ struct FormatStyle { bool InEmptyParentheses; /// Put a space in parentheses not covered by preceding options. /// \code -///true: false: -///t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; +/// true: +/// t f( Deleted & ) & = delete; +/// decltype( ( x ) ) owenca wrote: ```suggestion ///__attribute__( ( noreturn ) ) /// decltype( ( x ) ) ``` https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
owenca wrote: > This would be greatly simplified if we can annotate the `DoubleParenthesis` > situation inside of `UnwrappedLineParser::parseParens`. +1. https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in AnnotatingParser::rParenEndsCast() (PR #79549)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/79549 Fixes #78965. >From 550ab825a339307ecf2f02fe12ba987ab1351bf9 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 25 Jan 2024 20:16:37 -0800 Subject: [PATCH] [clang-format] Fix a bug in AnnotatingParser::rParenEndsCast() Fixes #78965. --- clang/lib/Format/TokenAnnotator.cpp | 2 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 5 + 2 files changed, 7 insertions(+) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 25fcceb87864379..df1c5bc19de1e84 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2488,6 +2488,8 @@ class AnnotatingParser { (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) { return false; } +if (Line.InPPDirective && Tok.Next->is(tok::minus)) + return false; // Search for unexpected tokens. for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen; Prev = Prev->Previous) { diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 3dbf504c35ed55e..67678c18963b1f3 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -611,6 +611,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) { EXPECT_TOKEN(Tokens[13], tok::r_paren, TT_Unknown); EXPECT_TOKEN(Tokens[14], tok::star, TT_BinaryOperator); + Tokens = annotate("#define foo(i) ((i) - bar)"); + ASSERT_EQ(Tokens.size(), 14u) << Tokens; + EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator); + Tokens = annotate("return (Foo) & 10;"); ASSERT_EQ(Tokens.size(), 8u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang] [llvm] [clang-format] Option to ignore macro definitions (PR #70338)
@@ -1157,7 +1157,15 @@ void UnwrappedLineParser::parsePPDefine() { // guard processing above, and changes preprocessing nesting. FormatTok->Tok.setKind(tok::identifier); FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define); - nextToken(); + + if (Style.SkipMacroDefinitionBody) { +do { + nextToken(); +} while (!eof()); + } else { +nextToken(); + } owenca wrote: Np! https://github.com/llvm/llvm-project/pull/70338 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
owenca wrote: See https://discourse.llvm.org/t/should-issimpletypespecifier-return-true-for-bool/74873/16. https://github.com/llvm/llvm-project/pull/79037 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca converted_to_draft https://github.com/llvm/llvm-project/pull/79115 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/79115 >From 6d11eaa2c9b5de0b42b82c6b52b35eca44b5d772 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 23 Jan 2024 02:35:09 -0800 Subject: [PATCH] [clang-format] Update FormatToken::isSimpleTypeSpecifier() --- clang/include/clang/Format/Format.h | 2 ++ clang/lib/Format/FormatToken.cpp | 36 ++- clang/lib/Format/FormatTokenLexer.cpp | 7 +++--- clang/lib/Format/FormatTokenLexer.h | 1 - 4 files changed, 8 insertions(+), 38 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index bc9eecd42f9ebf..2800f6db4a9786 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -5160,6 +5160,8 @@ tooling::Replacements sortUsingDeclarations(const FormatStyle , ArrayRef Ranges, StringRef FileName = ""); +extern LangOptions LangOpts; + /// Returns the ``LangOpts`` that the formatter expects you to set. /// /// \param Style determines specific settings for lexing mode. diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index b791c5a26bbe3a..a030d91d5589a5 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -34,41 +34,9 @@ const char *getTokenTypeName(TokenType Type) { return nullptr; } -// FIXME: This is copy from Sema. Put it in a common place and remove -// duplication. bool FormatToken::isSimpleTypeSpecifier() const { - switch (Tok.getKind()) { - case tok::kw_short: - case tok::kw_long: - case tok::kw___int64: - case tok::kw___int128: - case tok::kw_signed: - case tok::kw_unsigned: - case tok::kw_void: - case tok::kw_char: - case tok::kw_int: - case tok::kw_half: - case tok::kw_float: - case tok::kw_double: - case tok::kw___bf16: - case tok::kw__Float16: - case tok::kw___float128: - case tok::kw___ibm128: - case tok::kw_wchar_t: - case tok::kw_bool: -#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: -#include "clang/Basic/TransformTypeTraits.def" - case tok::annot_typename: - case tok::kw_char8_t: - case tok::kw_char16_t: - case tok::kw_char32_t: - case tok::kw_typeof: - case tok::kw_decltype: - case tok::kw__Atomic: -return true; - default: -return false; - } + const auto *IdentifierInfo = Tok.getIdentifierInfo(); + return IdentifierInfo && IdentifierInfo->isSimpleTypeSpecifier(LangOpts); } bool FormatToken::isTypeOrIdentifier() const { diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 52a55ea23b5f2f..e6575dd417438a 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -22,18 +22,20 @@ namespace clang { namespace format { +LangOptions LangOpts; + FormatTokenLexer::FormatTokenLexer( const SourceManager , FileID ID, unsigned Column, const FormatStyle , encoding::Encoding Encoding, llvm::SpecificBumpPtrAllocator , IdentifierTable ) : FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}), - Column(Column), TrailingWhitespace(0), - LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID), + Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID), Style(Style), IdentTable(IdentTable), Keywords(IdentTable), Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0), FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin), MacroBlockEndRegex(Style.MacroBlockEnd) { + LangOpts = getFormattingLangOpts(Style); Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts)); Lex->SetKeepWhitespaceMode(true); @@ -1411,7 +1413,6 @@ void FormatTokenLexer::readRawToken(FormatToken ) { void FormatTokenLexer::resetLexer(unsigned Offset) { StringRef Buffer = SourceMgr.getBufferData(ID); - LangOpts = getFormattingLangOpts(Style); Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts, Buffer.begin(), Buffer.begin() + Offset, Buffer.end())); Lex->SetKeepWhitespaceMode(true); diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h index 65dd733bd53352..52838f1d8a17f5 100644 --- a/clang/lib/Format/FormatTokenLexer.h +++ b/clang/lib/Format/FormatTokenLexer.h @@ -120,7 +120,6 @@ class FormatTokenLexer { unsigned Column; unsigned TrailingWhitespace; std::unique_ptr Lex; - LangOptions LangOpts; const SourceManager FileID ID; const FormatStyle ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Update FormatToken::isSimpleTypeSpecifier() (PR #79115)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/79115 Depends on #79037. >From 61a06b77e8e6f2b5c5a9a2aa0b7f46260545f5b4 Mon Sep 17 00:00:00 2001 From: Carl Peto Date: Mon, 22 Jan 2024 18:52:46 + Subject: [PATCH 1/2] [clang] - Sema::isSimpleTypeSpecifier return true for _Bool in c99 (currently returns false for _Bool, regardless of C dialect). (Fixes #72203) - move simple type decision code into shared location (IdentifierInfo) - replace the logic with a check for simple types and a proper check for a valid keyword in the appropriate dialect - change all call sites to match the above new API --- clang/include/clang/Basic/IdentifierTable.h | 4 ++ clang/include/clang/Sema/Sema.h | 2 +- clang/lib/Basic/IdentifierTable.cpp | 39 + clang/lib/Parse/ParseExpr.cpp | 3 +- clang/lib/Parse/ParseObjc.cpp | 3 +- clang/lib/Sema/SemaDecl.cpp | 48 + 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h index 1ac182d4fce26f..2c979e438e81bb 100644 --- a/clang/include/clang/Basic/IdentifierTable.h +++ b/clang/include/clang/Basic/IdentifierTable.h @@ -427,6 +427,10 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo { /// language. bool isCPlusPlusKeyword(const LangOptions ) const; + /// Return true if this token is a simple type specifier + /// in the specified language. + bool isSimpleTypeSpecifier(const LangOptions ) const; + /// Get and set FETokenInfo. The language front-end is allowed to associate /// arbitrary metadata with this token. void *getFETokenInfo() const { return FETokenInfo; } diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 0db39333b0ee34..bc1fd19b5c6de7 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2636,7 +2636,7 @@ class Sema final { void DiagnoseUseOfUnimplementedSelectors(); - bool isSimpleTypeSpecifier(tok::TokenKind Kind) const; + bool isSimpleTypeSpecifier(const IdentifierInfo ) const; ParsedType getTypeName(const IdentifierInfo , SourceLocation NameLoc, Scope *S, CXXScopeSpec *SS = nullptr, diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp index d0d8316385b452..78c783cdff6b5e 100644 --- a/clang/lib/Basic/IdentifierTable.cpp +++ b/clang/lib/Basic/IdentifierTable.cpp @@ -419,6 +419,45 @@ StringRef IdentifierInfo::deuglifiedName() const { return Name; } +/// Determine whether the token kind starts a simple-type-specifier. +bool IdentifierInfo::isSimpleTypeSpecifier(const LangOptions ) const { + auto Kind = getTokenID(); + + switch (Kind) { + case tok::kw_short: + case tok::kw_long: + case tok::kw___int64: + case tok::kw___int128: + case tok::kw_signed: + case tok::kw_unsigned: + case tok::kw_void: + case tok::kw_char: + case tok::kw_int: + case tok::kw_half: + case tok::kw_float: + case tok::kw_double: + case tok::kw___bf16: + case tok::kw__Float16: + case tok::kw___float128: + case tok::kw_wchar_t: + case tok::kw_bool: + case tok::kw___underlying_type: + case tok::kw___auto_type: + case tok::kw__Bool: + case tok::annot_typename: + case tok::kw_char16_t: + case tok::kw_char32_t: + case tok::kw_typeof: + case tok::annot_decltype: + case tok::kw_decltype: + case tok::kw_char8_t: +return isKeyword(LangOpts); + + default: +return false; + } +} + tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { // We use a perfect hash function here involving the length of the keyword, // the first and third character. For preprocessor ID's there are no diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index e862856a08ca11..8f9f918bf544fd 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1597,7 +1597,8 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, if (TryAnnotateTypeOrScopeToken()) return ExprError(); - if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) + if (!Tok.getIdentifierInfo() || + !Actions.isSimpleTypeSpecifier(*Tok.getIdentifierInfo())) // We are trying to parse a simple-type-specifier but might not get such // a token after error recovery. return ExprError(); diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp index 849fd1ac95a442..5565770610c491 100644 --- a/clang/lib/Parse/ParseObjc.cpp +++ b/clang/lib/Parse/ParseObjc.cpp @@ -2971,7 +2971,8 @@ bool Parser::ParseObjCXXMessageReceiver(bool , void *) { tok::annot_cxxscope)) TryAnnotateTypeOrScopeToken(); - if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) { + if (!Tok.getIdentifierInfo() || + !Actions.isSimpleTypeSpecifier(*Tok.getIdentifierInfo())) { // objc-receiver:
[clang] [clang-format] Allow decltype in requires clause (PR #78847)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/78847 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77699 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in ContinuationIndenter (PR #78921)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/78921 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] adds a space after not inside macros (PR #78176)
@@ -4842,19 +4842,19 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine , return true; } if (Left.is(TT_UnaryOperator)) { -if (Right.isNot(tok::l_paren)) { +if (Right.isOneOf(tok::identifier, tok::numeric_constant)) { // The alternative operators for ~ and ! are "compl" and "not". // If they are used instead, we do not want to combine them with // the token to the right, unless that is a left paren. if (Left.is(tok::exclaim) && Left.TokenText == "not") return true; if (Left.is(tok::tilde) && Left.TokenText == "compl") return true; - // Lambda captures allow for a lone &, so "&]" needs to be properly - // handled. - if (Left.is(tok::amp) && Right.is(tok::r_square)) -return Style.SpacesInSquareBrackets; } +// Lambda captures allow for a lone &, so "&]" needs to be properly +// handled. +if (Left.is(tok::amp) && Right.is(tok::r_square)) + return Style.SpacesInSquareBrackets; return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) || Right.is(TT_BinaryOperator); owenca wrote: ```suggestion return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim); ``` Would this suffice? https://github.com/llvm/llvm-project/pull/78176 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] adds a space after not inside macros (PR #78176)
@@ -24160,6 +24160,14 @@ TEST_F(FormatTest, AlternativeOperators) { verifyFormat("int a compl(5);"); verifyFormat("int a not(5);"); + verifyFormat("v(not)"); + verifyFormat("v(not!)"); + verifyFormat("Symbol(not, None)"); + verifyFormat("Symbol(not!, None)"); + + verifyFormat("assert(!\"fail\")"); + verifyFormat("assert(not\"fail\")"); owenca wrote: We should drop them as they are invalid code (i.e. trigger compiler warnings). https://github.com/llvm/llvm-project/pull/78176 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] adds a space after not inside macros (PR #78176)
@@ -4842,19 +4842,19 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine , return true; } if (Left.is(TT_UnaryOperator)) { -if (Right.isNot(tok::l_paren)) { +if (Right.isOneOf(tok::identifier, tok::numeric_constant)) { // The alternative operators for ~ and ! are "compl" and "not". // If they are used instead, we do not want to combine them with // the token to the right, unless that is a left paren. owenca wrote: The comment needs to be updated. https://github.com/llvm/llvm-project/pull/78176 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] BreakAfterAttributes did not take into account gnu attributes (PR #78102)
owenca wrote: > You either have to use a new option, or adapt the documentation. Latter > clearly only refers to `C++11 attributes`, which `__attribute__(())` isn't. > > I don't think a new option is needed. > > When you are already at it, you may also consider handling `__declspec()`. +1. https://github.com/llvm/llvm-project/pull/78102 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Improved is simple type specifier (PR #78903)
owenca wrote: > Here's the sort of approach I"m looking at @owenca Can you restore the clang-format files for this pull request? I'll open another PR that depends on this one. https://github.com/llvm/llvm-project/pull/78903 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [lld] [clang-tools-extra] [llvm] [libcxx] [lldb] [mlir] [compiler-rt] [clang-format] Add ShortReturnTypeColumn option. (PR #78011)
owenca wrote: > @mydeveloperday, do you have an opinion on this pull request? It addresses > #78010 with minimal changes, or are you interested in exploring other > possibilities? It's weird to add such a top-level option to fix a bug and keep the current behavior. See https://github.com/llvm/llvm-project/issues/78010#issuecomment-1903300176. https://github.com/llvm/llvm-project/pull/78011 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Allow decltype in requires clause (PR #78847)
@@ -1071,6 +1071,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { "concept C = (!Foo) && Bar;"); ASSERT_EQ(Tokens.size(), 19u) << Tokens; EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator); + + Tokens = annotate("void f() & requires(C) {}"); + ASSERT_EQ(Tokens.size(), 18u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause); + + Tokens = annotate("auto f() -> int& requires(C) {}"); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause); } owenca wrote: > The fix I would originally do was just look behind again and see if the ) > belongs to a decltype, I just also tried removing the case entirely, and was > surprised it works. If you prefer, I can follow my original plan and add an > extra check for decltype In general, I prefer to not include code that can't be reached by test cases as the code is likely superfluous. Because @HazardyKnusperkeks is ok with its removal, wouldn't be better to just do that? https://github.com/llvm/llvm-project/pull/78847 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Allow decltype in requires clause (PR #78847)
@@ -1071,6 +1071,37 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { "concept C = (!Foo) && Bar;"); ASSERT_EQ(Tokens.size(), 19u) << Tokens; EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator); + + Tokens = annotate("void f() & requires(C) {}"); + ASSERT_EQ(Tokens.size(), 18u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause); + + Tokens = annotate("auto f() -> int& requires(C) {}"); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause); + + Tokens = annotate("int foo(auto t) {\n" +"if (requires (decltype(t) x) {x.foo();}) {\n" +"return 1;\n" +"}\n" +"return 0;\n" +"}\n"); owenca wrote: Do we need the newline? https://github.com/llvm/llvm-project/pull/78847 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Allow decltype in requires clause (PR #78847)
@@ -1071,6 +1071,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { "concept C = (!Foo) && Bar;"); owenca wrote: +1. You don't even need to create a pull request for this kind of NFC changes. https://github.com/llvm/llvm-project/pull/78847 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
owenca wrote: > > It seems adding a boolean sub-option that targets double pairs of > > parentheses as I suggested before is feasible although a better name than > > `ConsecutiveParentheses` may be needed. > > I'm fine to fix but request a concrete suggestion or proposal before I spend > much more time on this. It might also work to suppress optionally the > additional space on the first/last parenthesis pair, for these special > "paired" parens. I think the way I already worked this patch is the right way > to go to preserve backward compatibility while still allowing control over > the repeated parentheses. > > Looks like it is called `MultipleParentheses` in the `RemoveParentheses` > option. I should rename to that. My idea was: - Keep the existing boolean suboptions and add a new one. Let's call it `ExceptDoubleParentheses` for now. - If it's set to true, assuming all other suboptions are set to true, the above examples would be formatted as: ``` __attribute__(( noreturn )) __attribute__(( __aligned__( x ) )) if (( i = j )) if ( ( i = j ) && ( k > 3 ) ) // not affected by ExceptDoubleParentheses decltype(( x )) while ( ( ( i + 1 ) * j - 2 ) * k > 3 ) // not affected by ExceptDoubleParentheses ``` - If it's set to false, assuming all other suboptions are set to true, the above examples would be formatted as: ``` __attribute__( ( noreturn ) ) __attribute__( ( __aligned__( x ) ) ) if ( ( i = j ) ) if ( ( i = j ) && ( k > 3 ) ) // not affected by ExceptDoubleParentheses decltype( ( x ) ) while ( ( ( i + 1 ) * j - 2 ) * k > 3 ) // not affected by ExceptDoubleParentheses ``` This way, we would avoid making all suboptions an `enum` and repeating `NonConsecutive` in every suboption. https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/77699 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in ContinuationIndenter (PR #78921)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/78921 Fixes #76991. >From bc3f566b7c512434179241796a2d7d4ac3e7b381 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 21 Jan 2024 17:14:53 -0800 Subject: [PATCH] [clang-format] Fix a bug in ContinuationIndenter Fixes #76991. --- clang/lib/Format/ContinuationIndenter.cpp | 3 ++- clang/lib/Format/WhitespaceManager.h | 2 +- clang/unittests/Format/FormatTest.cpp | 7 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index e6eaaa9ab45706..c204cc81331521 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -738,7 +738,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState , bool DryRun, if (!DryRun) { Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces, - State.Column + Spaces + PPColumnCorrection); + State.Column + Spaces + PPColumnCorrection, + /*IsAligned=*/false, State.Line->InMacroBody); } // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance diff --git a/clang/lib/Format/WhitespaceManager.h b/clang/lib/Format/WhitespaceManager.h index dc6f60e5deeedf..8ac73305871ae7 100644 --- a/clang/lib/Format/WhitespaceManager.h +++ b/clang/lib/Format/WhitespaceManager.h @@ -55,7 +55,7 @@ class WhitespaceManager { /// this replacement. It is needed for determining how \p Spaces is turned /// into tabs and spaces for some format styles. void replaceWhitespace(FormatToken , unsigned Newlines, unsigned Spaces, - unsigned StartOfTokenColumn, bool isAligned = false, + unsigned StartOfTokenColumn, bool IsAligned = false, bool InPPDirective = false); /// Adds information about an unchangeable token's whitespace. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 3fb55ae2c1f413..a42a254d61d419 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -21203,6 +21203,13 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { "});", Style); + Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; + verifyFormat("#define FOO \\\n" + " int foo[][2] = { \\\n" + " {0, 1} \\\n" + " };", + Style); + Style.Cpp11BracedListStyle = false; verifyFormat("struct test demo[] = {\n" " { 56, 23,\"hello\" },\n" ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
owenca wrote: > > Would `__attribute__((noreturn))`, `if ((i = j))`, `decltype((x))`, and > > `while (((i + 1) * j - 2) * k > 3)` be formatted as `__attribute__(( > > noreturn ))`, `if (( i = j ))`, `decltype(( x ))`, and `while ( ( ( i + 1 ) > > * j - 2 ) * k > 3 )`, respectively? > > Almost, it would also be `if ( ( i = j ) )`, Although repeated parens are > typically removed. So a better example is `if ( ( i = j ) && ( k > 3 ) )` > > We don't have `decltype` in our C code base. I can add support for it as an > option if desired. > > We also have cases such as `__attribute__(( __aligned__( x ) ))` I'm ok with all of the above except for `if ( ( i = j ) )`, which should be formatted as `if (( i = j ))` because the extra pair of parentheses is not superfluous. This is similar to `decltype(( x))`, and the`RemoveParentheses` option doesn't strip the extra pair for either. It seems adding a boolean sub-option that targets double pairs of parentheses as I suggested before is feasible although a better name than `ConsecutiveParentheses` may be needed. https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
owenca wrote: > The code is [self-hosted](https://git.rtems.org/rtems/) and [mirrored on > GitHub](https://github.com/RTEMS/rtems) with a [documented style > guide](https://docs.rtems.org/branches/master/eng/coding-formatting.html). In > addition, I have been and will continue to be willing to provide bug fix and > other maintenance support to clang-format based on the changes submitted to > support these style needs. > > At the moment, I need to satisfy a particular style rule that requires me to > add spaces inside of conditional and compound expressions. It seems the RTEMS style guide you linked above doesn't have any examples for conditional expressions and compound expressions, and by _conditional expressions_ I assume it means conditionals of control statements rather than ternary [conditional expressions](https://www.ibm.com/docs/en/zos/3.1.0?topic=operators-conditional-expressions). Would `__attribute__((noreturn))`, `if ((i = j))`, `decltype((x))`, and `while (((i + 1) * j - 2) * k > 3)` be formatted as `__attribute__(( noreturn ))`, `if (( i = j ))`, `decltype(( x ))`, and `while ( ( ( i + 1 ) * j - 2 ) * k > 3 )`, respectively? https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle templated elaborated type specifier in function… (PR #77013)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77013 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle templated elaborated type specifier in function… (PR #77013)
@@ -3873,6 +3873,9 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { const FormatToken = *FormatTok; nextToken(); + auto IsNonMacroIdentifier = [](FormatToken *Tok) { owenca wrote: Nit: ```suggestion auto IsNonMacroIdentifier = [](const FormatToken *Tok) { ``` https://github.com/llvm/llvm-project/pull/77013 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)
@@ -768,15 +768,25 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState , bool DryRun, // parenthesis by disallowing any further line breaks if there is no line // break after the opening parenthesis. Don't break if it doesn't conserve // columns. + const auto IsOpeningBracket = [&](const FormatToken ) { +const auto IsStartOfBracedList = [&]() { + return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) && + Style.Cpp11BracedListStyle; +}; +if (Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) || +IsStartOfBracedList()) { + if (!Tok.Previous) +return true; + if (Tok.Previous->isIf()) +return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak; + return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while, +tok::kw_switch); +} +return false; owenca wrote: ```suggestion if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) && !IsStartOfBracedList()) { return false; } if (!Tok.Previous) return true; if (Tok.Previous->isIf()) return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak; return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while, tok::kw_switch); ``` https://github.com/llvm/llvm-project/pull/77699 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)
@@ -768,15 +768,25 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState , bool DryRun, // parenthesis by disallowing any further line breaks if there is no line // break after the opening parenthesis. Don't break if it doesn't conserve // columns. + const auto IsOpeningBracket = [&](const FormatToken ) { +const auto IsStartOfBracedList = [&]() { owenca wrote: ```suggestion auto IsOpeningBracket = [&](const FormatToken ) { auto IsStartOfBracedList = [&]() { ``` https://github.com/llvm/llvm-project/pull/77699 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)
owenca wrote: > Covering all double parens in a single sub-option is not precise enough for > code bases that may want to have `__attribute__(( x ))` but also allow `if ( > ( x ) )`. We should not go overboard with supporting all kinds of options/suboptions imaginable. I don't think we should support spaces between consecutive parens in the first place, which was likely a bug. We can't just disregard the long-standing [policy](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options) on adding new options. If anyone thinks that policy is outdated, they should go through the RFC process and get it updated. https://github.com/llvm/llvm-project/pull/77522 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle templated elaborated type specifier in function… (PR #77013)
@@ -14583,9 +14583,10 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); verifyFormat("int i = SomeFunction(a b);"); - // FIXME: - // This now gets parsed incorrectly as class definition. - // verifyFormat("class A f() {\n}\nint n;"); + verifyFormat("class A f() {}\nint n;"); + verifyFormat("template class A f() {}\nint n;"); + + verifyFormat("template <> class Foo F() {\n} n;"); owenca wrote: Please wrap after newlines. https://github.com/llvm/llvm-project/pull/77013 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix poor spacing in `AlignArrayOfStructures: Left` (PR #77868)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77868 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix poor spacing in `AlignArrayOfStructures: Left` (PR #77868)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/77868 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix poor spacing in `AlignArrayOfStructures: Left` (PR #77868)
@@ -1366,11 +1366,12 @@ void WhitespaceManager::alignArrayInitializersLeftJustified( auto = CellDescs.Cells; // Now go through and fixup the spaces. auto *CellIter = Cells.begin(); - // The first cell needs to be against the left brace. - if (Changes[CellIter->Index].NewlinesBefore == 0) -Changes[CellIter->Index].Spaces = BracePadding; - else -Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces; + // The first cell of every row needs to be against the left brace. + for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) +if (Changes[Next->Index].NewlinesBefore == 0) + Changes[Next->Index].Spaces = BracePadding; +else + Changes[Next->Index].Spaces = CellDescs.InitialSpaces; owenca wrote: ```suggestion for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) { auto = Changes[Next->Index]; Change.Spaces = Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces; } ``` https://github.com/llvm/llvm-project/pull/77868 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add MainIncludeChar option. (PR #78752)
@@ -0,0 +1,27 @@ +// Test the combination of regrouped include directives, via regexes and owenca wrote: > Ok I will move them there, thanks :) Should I remove these lit tests or are > they complementary? Please remove them from the lit tests. https://github.com/llvm/llvm-project/pull/78752 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-tools-extra] [clang-format] Option to ignore macro definitions (PR #70338)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/70338 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SkipMacroDefinitionBody option (PR #78682)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/78682 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SkipMacroDefinitionBody option (PR #78682)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/78682 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add SkipMacroDefinitionBody option (PR #78682)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/78682 Closes #67991. Co-authored-by: @tomekpaszek >From 620e614b242055c3b8cbd81efd9bbc64a0ee4e56 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 18 Jan 2024 23:34:13 -0800 Subject: [PATCH] [clang-format] Option to ignore macro definitions --- clang/docs/ClangFormatStyleOptions.rst | 5 + clang/docs/ReleaseNotes.rst| 1 + clang/include/clang/Format/Format.h| 5 + clang/lib/Format/Format.cpp| 2 + clang/lib/Format/UnwrappedLineParser.cpp | 9 ++ clang/unittests/Format/ConfigParseTest.cpp | 1 + clang/unittests/Format/FormatTest.cpp | 132 + 7 files changed, 155 insertions(+) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 8bc13e45bf2f5f..4dc0de3a90f265 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4999,6 +4999,11 @@ the configuration (without a prefix: ``Auto``). int bar; int bar; } // namespace b } // namespace b +.. _SkipMacroDefinitionBody: + +**SkipMacroDefinitionBody** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ ` + Do not format macro definition body. + .. _SortIncludes: **SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`¶ ` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b400d75095421c..1045fd6b92346b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1202,6 +1202,7 @@ clang-format - Add ``PenaltyBreakScopeResolution`` option. - Add ``.clang-format-ignore`` files. - Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``. +- Add ``SkipMacroDefinitionBody`` option. libclang diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6fd7947bd21791..bc9eecd42f9ebf 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3932,6 +3932,10 @@ struct FormatStyle { /// \version 13 unsigned ShortNamespaceLines; + /// Do not format macro definition body. + /// \version 18 + bool SkipMacroDefinitionBody; + /// Include sorting options. enum SortIncludesOptions : int8_t { /// Includes are never sorted. @@ -4895,6 +4899,7 @@ struct FormatStyle { RequiresExpressionIndentation == R.RequiresExpressionIndentation && SeparateDefinitionBlocks == R.SeparateDefinitionBlocks && ShortNamespaceLines == R.ShortNamespaceLines && + SkipMacroDefinitionBody == R.SkipMacroDefinitionBody && SortIncludes == R.SortIncludes && SortJavaStaticImport == R.SortJavaStaticImport && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 7c2f4dcf3d2308..ff326dc784783b 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1085,6 +1085,7 @@ template <> struct MappingTraits { Style.RequiresExpressionIndentation); IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks); IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines); +IO.mapOptional("SkipMacroDefinitionBody", Style.SkipMacroDefinitionBody); IO.mapOptional("SortIncludes", Style.SortIncludes); IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport); IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations); @@ -1556,6 +1557,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope; LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave; LLVMStyle.ShortNamespaceLines = 1; + LLVMStyle.SkipMacroDefinitionBody = false; LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive; LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before; LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 50d41c9f57a670..c08ce86449b6ea 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1170,6 +1170,15 @@ void UnwrappedLineParser::parsePPDefine() { assert((int)Line->PPLevel >= 0); Line->InMacroBody = true; + if (Style.SkipMacroDefinitionBody) { +do { + FormatTok->Finalized = true; + nextToken(); +} while (!eof()); +addUnwrappedLine(); +return; + } + if (FormatTok->is(tok::identifier) && Tokens->peekNextToken()->is(tok::colon)) { nextToken(); diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 172aaab5988ce5..2a8d79359a49b4 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++