clang-format (12.0.1) 的什么设置不会在 R 前缀和 C++ 中的原始字符串之间添加空格?

rti*_*ery 5 c++ rawstring clang-format

clang-format 12 (12.0.1) 的选项是什么,它在 R 前缀和原始字符串之间放置一个空格:

std::string str = R"(raw string)";
Run Code Online (Sandbox Code Playgroud)

std::string str = R "(raw string)";
Run Code Online (Sandbox Code Playgroud)

gcc (9.3.0) 或 VSCode 1.62.3 使用的 clang 不接受后者,因此这实际上破坏了源文件/构建。

我的 .clang-format 文件设置会导致出现空格,内置的 gnu 预设也会出现这种情况。其他预设(llvm、google、chromium、microsoft、mozilla 或 webkit)都不会导致此问题。

我已经转储了所有格式,并逐个设置地比较了它们。我首先删除了配置中与未导致此问题的预设之一相匹配的所有设置。然后我消除了 gnu 预设和未添加空格的预设之间相同的任何剩余设置。最后,我检查了剩下的几个设置,并将 .clang-format 文件中的设置更改为工作预设中的值,以查看它们是否解决了问题,但它们没有。

这样做,我似乎已经消除了所有的设置作为原因。

在我花更多时间、几个小时挖掘实际的源代码之前,我希望有人对此有一些见解,并能够向我指出导致问题的设置。

作为参考,我在下面包含了我的 .clang-format 设置文件。但正如所指出的,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
Run Code Online (Sandbox Code Playgroud)

编辑:================================================== ===========

参考 @Ted Lyngmo 的评论,这里是测试输入和输出:

原文:(注意空格和不空格)

std::string foo()
{
    std::string str1 = R"(raw string 1)";
    std::string str2 = R "(raw string 1)";
    return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

我的 .clang 格式:

std::string foo()
{
    std::string str1 = R "(raw string 1)";
    std::string str2 = R "(raw string 1)";
    return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

格努:

std::string
foo ()
{
  std::string str1 = R "(raw string 1)";
  std::string str2 = R "(raw string 1)";
  return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

llvm、google、chromium:(请注意,正如@Ted Lyngmo 提到的那样,原始内容被保留)

std::string foo() {
  std::string str1 = R"(raw string 1)";
  std::string str2 = R "(raw string 1)";
  return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

微软,webkit:(注意原始内容保留)

std::string foo()
{
    std::string str1 = R"(raw string 1)";
    std::string str2 = R "(raw string 1)";
    return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

mozilla:(注意原始内容保留)

std::string
foo()
{
  std::string str1 = R"(raw string 1)";
  std::string str2 = R "(raw string 1)";
  return str1 + str2;
}
Run Code Online (Sandbox Code Playgroud)

这很有趣。我没有看到任何删除空格的设置,但我的和 gnu 添加了空格。这似乎是个好信息,但我仍然需要知道什么设置将保留格式而不添加空格,以便我可以修改自己的配置。

rti*_*ery 5

好吧,在 @TedLyngmo 的帮助下,我相信我找到了设置。

Standard: C++03导致添加空间。Standard: AutoStandard: C++11保留源中的格式。

根据我自己的喜好,我更愿意设置StandardC++03,以便 clang-format 将添加(而不是删除)复合模板的尾尖括号之间的空格。

这两件事联系在一起有点臭。

  • 因为在 C++03 中,它不是字符串文字前缀。 (2认同)