tools/nxstyle.c: Fix detection of long single line comments.

This resolves issue 718:  nxstyle line width check was ignoring the line width check for single line comments.

This turned out to be an artifact in parsing.  Usually when parsing character by character, the file character to be parsed was '/n'.  Howefver, in the case of parsing single line comments, the final character was the NUL terminator.  This means that the lenth check was not being performed in the case of single line comments.

NOTE:  Currently, I have suppressed error reports for single line right-hand comments.  My fear is that this will unmask more standard violations than we can cope with.  It is easy to re-enable and perhaps we should do that in the future:

          /* Check for long lines
           *
           * REVISIT:  Long line checks suppressed on right hand comments
           * for now.  This just prevents a large number of difficult-to-
           * fix complaints that we would have otherwise.
           */

          if (m > g_maxline && !rhcomment)  <-- remote the second condition
This commit is contained in:
Gregory Nutt 2020-04-06 12:02:29 -06:00 committed by Abdelatif Guettouche
parent f9a7417be4
commit 58589db931

View File

@ -1721,7 +1721,9 @@ int main(int argc, char **argv, char **envp)
{ {
if (n > indent) if (n > indent)
{ {
/* REVISIT: dnest is always > 0 here if bfunctions == false */ /* REVISIT: dnest is always > 0 here if bfunctions ==
* false.
*/
if (dnest == 0 || !bfunctions || lineno == rbrace_lineno) if (dnest == 0 || !bfunctions || lineno == rbrace_lineno)
{ {
@ -1888,7 +1890,9 @@ int main(int argc, char **argv, char **envp)
} }
} }
/* The right brace should not be preceded with a a blank line */ /* The right brace should not be preceded with a a blank
* line.
*/
if (lineno == blank_lineno + 1) if (lineno == blank_lineno + 1)
{ {
@ -2367,29 +2371,72 @@ int main(int argc, char **argv, char **envp)
/* Loop terminates when NUL or newline character found */ /* Loop terminates when NUL or newline character found */
if (line[n] == '\n') if (line[n] == '\n' || line[n] == '\0')
{ {
/* If the parse terminated on the NULL, then back up to the last
* character (which should be the newline).
*/
int m = n;
if (line[m] == '\0' && m > 0)
{
m--;
}
/* Check for space at the end of the line. Except for carriage /* Check for space at the end of the line. Except for carriage
* returns which we have already reported (one time) above. * returns which we have already reported (one time) above.
*/ */
if (n > 1 && isspace((int)line[n - 1]) && line[n - 1] != '\r') if (m > 1 && isspace((int)line[m - 1]) &&
line[m - 1] != '\n' && line[m - 1] != '\r')
{ {
ERROR("Dangling whitespace at the end of line", lineno, n); ERROR("Dangling whitespace at the end of line", lineno, m);
} }
/* Check for long lines */ /* The line width is determined by the location of the final
* asterisk in block comments. The closing line of the block
* comment will exceed that by one one character, the '/'
* following the final asterisk.
*/
if (n > g_maxline) else if (m > g_maxline)
{
bool bslash;
int a;
for (bslash = false, a = m;
a > 2 && strchr("\n\r/", line[a]) != NULL;
a--)
{
if (line[a] == '/')
{
bslash = true;
}
}
if (bslash && line[a] == '*')
{
m = a + 1;
}
}
/* Check for long lines
*
* REVISIT: Long line checks suppressed on right hand comments
* for now. This just prevents a large number of difficult-to-
* fix complaints that we would have otherwise.
*/
if (m > g_maxline && !rhcomment)
{ {
if (g_file_type == C_SOURCE) if (g_file_type == C_SOURCE)
{ {
ERROR("Long line found", lineno, n); ERROR("Long line found", lineno, m);
} }
else if (g_file_type == C_HEADER) else if (g_file_type == C_HEADER)
{ {
WARN("Long line found", lineno, n); WARN("Long line found", lineno, m);
} }
} }
} }