什么 clang-format (12.0.1) 设置不会在 R 前缀和 C++ 中的原始字符串之间添加 space?
What setting of clang-format (12.0.1) will not add a space between the R prefix and a raw string in C++?
clang-format 12 (12.0.1) 在 R 前缀和原始字符串之间放置 space 的选项是什么:
std::string str = R"(raw string)";
对
std::string str = R "(raw string)";
后者不被 gcc (9.3.0) 或 VSCode 1.62.3 使用的 clang 接受,所以这实际上破坏了源代码 file/build.
我的 .clang-format 文件设置导致了 space,内置的 gnu 预设也是如此。 None 的其他预设(llvm、google、chromium、microsoft、mozilla 或 webkit)导致此问题。
我已经转储了所有格式并逐个比较它们。我首先删除了我的配置中与不会导致此问题的预设之一相匹配的所有设置。然后我删除了 gnu 预设和未添加 space 的预设之间相同的所有剩余设置。最后,我仔细检查了剩下的几个设置,并将我的 .clang-format 文件中的设置更改为工作预设中的值,以查看它们是否解决了问题,但它们没有。
这样做,我似乎已经排除了所有设置的原因。
在我花更多时间、数小时深入研究实际源代码之前,我希望有人对此有所了解,并能够指出导致问题的设置。
作为参考,我在下面包含了我的 .clang 格式设置文件。但如前所述,gnu 预设也表现出这种行为。
感谢您的帮助。
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeConceptDeclarations: true
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineBeforeAccessModifier: Always
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: true
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
- 'cxx'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: ''
BasedOnStyle: google
ReflowComments: false
SortIncludes: true
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: C++03
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
编辑:=========================================== =================
参考@Ted Lyngmo 的评论,这是一个测试输入和输出:
原文:(注意 space 且未提供 space)
std::string foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
我的 .clang 格式:
std::string foo()
{
std::string str1 = R "(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
gnu:
std::string
foo ()
{
std::string str1 = R "(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
llvm,google,铬:(请注意,正如@Ted Lyngmo 提到的那样,原件被单独留下)
std::string foo() {
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
microsoft, webkit:(注意原件单独留下)
std::string foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
mozilla:(注意原件单独留下)
std::string
foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
这很有趣。我没有看到任何删除 space 的设置,但我的和 gnu 添加了 space。这似乎是很好的信息,但我仍然需要知道什么设置将保留格式而不添加 space,以便我可以修改自己的配置。
嗯,在@TedLyngmo 的帮助下,我相信我找到了设置。
Standard: C++03
导致添加 space。 Standard: Auto
或 Standard: C++11
保留源中的格式。
根据我自己的喜好,我更愿意将 Standard
设置为 C++03
以便 clang-format 将添加(而不是删除)尾部尖括号之间的 space复合模板。
这两件事联系起来有点臭。
clang-format 12 (12.0.1) 在 R 前缀和原始字符串之间放置 space 的选项是什么:
std::string str = R"(raw string)";
对
std::string str = R "(raw string)";
后者不被 gcc (9.3.0) 或 VSCode 1.62.3 使用的 clang 接受,所以这实际上破坏了源代码 file/build.
我的 .clang-format 文件设置导致了 space,内置的 gnu 预设也是如此。 None 的其他预设(llvm、google、chromium、microsoft、mozilla 或 webkit)导致此问题。
我已经转储了所有格式并逐个比较它们。我首先删除了我的配置中与不会导致此问题的预设之一相匹配的所有设置。然后我删除了 gnu 预设和未添加 space 的预设之间相同的所有剩余设置。最后,我仔细检查了剩下的几个设置,并将我的 .clang-format 文件中的设置更改为工作预设中的值,以查看它们是否解决了问题,但它们没有。
这样做,我似乎已经排除了所有设置的原因。
在我花更多时间、数小时深入研究实际源代码之前,我希望有人对此有所了解,并能够指出导致问题的设置。
作为参考,我在下面包含了我的 .clang 格式设置文件。但如前所述,gnu 预设也表现出这种行为。
感谢您的帮助。
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterJavaFieldAnnotations: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakBeforeConceptDeclarations: true
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakConstructorInitializersBeforeComma: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineBeforeAccessModifier: Always
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: true
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
- 'cxx'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: ''
BasedOnStyle: google
ReflowComments: false
SortIncludes: true
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: C++03
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
编辑:=========================================== =================
参考@Ted Lyngmo 的评论,这是一个测试输入和输出:
原文:(注意 space 且未提供 space)
std::string foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
我的 .clang 格式:
std::string foo()
{
std::string str1 = R "(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
gnu:
std::string
foo ()
{
std::string str1 = R "(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
llvm,google,铬:(请注意,正如@Ted Lyngmo 提到的那样,原件被单独留下)
std::string foo() {
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
microsoft, webkit:(注意原件单独留下)
std::string foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
mozilla:(注意原件单独留下)
std::string
foo()
{
std::string str1 = R"(raw string 1)";
std::string str2 = R "(raw string 1)";
return str1 + str2;
}
这很有趣。我没有看到任何删除 space 的设置,但我的和 gnu 添加了 space。这似乎是很好的信息,但我仍然需要知道什么设置将保留格式而不添加 space,以便我可以修改自己的配置。
嗯,在@TedLyngmo 的帮助下,我相信我找到了设置。
Standard: C++03
导致添加 space。 Standard: Auto
或 Standard: C++11
保留源中的格式。
根据我自己的喜好,我更愿意将 Standard
设置为 C++03
以便 clang-format 将添加(而不是删除)尾部尖括号之间的 space复合模板。
这两件事联系起来有点臭。