diff --git a/rules/S1916/cfamily/rule.adoc b/rules/S1916/cfamily/rule.adoc index fbdf5a87e4..6cd4a4a8ea 100644 --- a/rules/S1916/cfamily/rule.adoc +++ b/rules/S1916/cfamily/rule.adoc @@ -1,20 +1,30 @@ == Why is this an issue? -The standard mentions that the line continuation character (``++\++``) should be immediately followed by a newline or be the very last character of the file in order for the lines to be joined. +include::../../../shared_content/cfamily/line_splicing.adoc[] +Several compilers have a loose implementation of _line-splicing_ and allow whitespace after the `\` character. +While this practice was non-portable until {cpp}23, it remains non-portable in C. +Furthermore, readers can easily be confused about the meaning of these whitespaces. -Several compilers relax this requirement by allowing whitespace after the ``++\++`` character, but this is not portable because other compilers may not do the same. - - -=== Compliant solution +Therefore, for portability and clarity, `\` should be immediately followed by a newline character, as in the following example. [source,cpp] ---- -// There should be no whitespace after the '\' +// Compliant: there is no whitespace after the '\' #define FOO BAR \ BAZ ---- +== Resources + +=== Documentation + +* {cpp} reference - https://en.cppreference.com/w/cpp/language/translation_phases[Phases of translation] + +=== Related rules + +* S2323 - Line-splicing should not be used in "//" comments + ifdef::env-github,rspecator-view[] ''' diff --git a/rules/S2323/cfamily/rule.adoc b/rules/S2323/cfamily/rule.adoc index 26b2681f54..17589a7de3 100644 --- a/rules/S2323/cfamily/rule.adoc +++ b/rules/S2323/cfamily/rule.adoc @@ -1,9 +1,6 @@ == Why is this an issue? -When a line of code ends with the `\` (backslash) character, the newline character is deleted. -The compiler considers the next line a continuation of the current line, resulting in only one logical line of code. - -This source code transformation, known as _line-splicing_, occurs during the second phase of C and {cpp} translation. +include::../../../shared_content/cfamily/line_splicing.adoc[] _Line-splicing_ is often harmless and is sometimes used to improve the readability of macros: @@ -36,7 +33,7 @@ bool isSpecialCharacter(char c) Compilers may also delete whitespace characters between a backslash and the newline characters. This practice is standard-compliant since {cpp}23. -In other words, trailing whitespace does not disable _line-splicing_. +In other words, trailing whitespaces do not disable _line-splicing_. == Resources diff --git a/shared_content/cfamily/line_splicing.adoc b/shared_content/cfamily/line_splicing.adoc new file mode 100644 index 0000000000..342fcd5fd8 --- /dev/null +++ b/shared_content/cfamily/line_splicing.adoc @@ -0,0 +1,4 @@ +When a line of code ends with the `\` (backslash) character, the newline character is deleted. +The compiler considers the next line a continuation of the current line, resulting in only one logical line of code. + +This source code transformation, known as _line-splicing_, occurs during the second phase of C and {cpp} translation.