T009 patches and enhenced test case

Issue #56 new
Former user created an issue

Hello,

I'm proposing the following update to the T009 rule check to allow some flexibility in some coding rules.

The goal is to not show error when a coma is preceeded by spaces and only spaces, allowing specific indentation rules.

Here is the test case T009.cpp revisited:

// OK
void fun(int x, int y, int z);
int a[] = {5, 6, 7};
class A : public B,
          public C
{
     // ...
};
class B
{
public:
    B()
    : a(0)
    , b(1)
    int a;
    int b;
};
void operator,(const A &left, const A &right);
void operator , (const A &left, const A &right);
// not OK
void fun(int x , int y , int z);
void fun2(int x,int y,int z);
int a[] = {5 , 6 , 7};
class A : public B ,
          public C,public D
{
     // ...
};

And here is the TCL script T009.tcl patched:

#!/usr/bin/tclsh
# Comma should not be preceded by whitespace, but should be followed by one

foreach f [getSourceFileNames] {
    foreach t [getTokens $f 1 0 -1 -1 {comma}] {
        set line [lindex $t 1]
        set column [lindex $t 2]
        set preceding [getTokens $f $line 0 $line $column {}]
        if {$preceding == {}} {
            report $f $line "comma should not be preceded by whitespace"
        } else {
            set sizeList [llength $preceding]
            set lastPreceding [lindex [lindex $preceding end] 3]
            # if there are only spaces (number of preceding token equal to one), we do not detect an error
            if {$lastPreceding == "space" && $sizeList != 1} {
                report $f $line "comma should not be preceded by whitespace"
            }
        }
        set following [getTokens $f $line [expr $column + 1] [expr $line + 1] -1 {}]
        if {$following != {}} {
            set firstFollowing [lindex [lindex $following 0] 3]
            if {$firstFollowing != "space" && $firstFollowing != "newline" &&
                !($lastPreceding == "operator" && $firstFollowing == "leftparen")} {
                report $f $line "comma should be followed by whitespace"
            }
        }
    }
}

Moreover, in bonus, I have modified the test case to add some NOK example of coma not followed by a space.

Anthony

Comments (1)

  1. Former user Account Deleted

    Addendum: the output is:

    U:\>"C:\Program Files (x86)\vera++\bin\vera++.exe" -r %CD%\vera++ --rule T009 T009.cpp
    T009.cpp:19: comma should not be preceded by whitespace
    T009.cpp:21: comma should not be preceded by whitespace
    T009.cpp:21: comma should not be preceded by whitespace
    T009.cpp:22: comma should be followed by whitespace
    T009.cpp:22: comma should be followed by whitespace
    T009.cpp:23: comma should not be preceded by whitespace
    T009.cpp:23: comma should not be preceded by whitespace
    T009.cpp:24: comma should not be preceded by whitespace
    T009.cpp:25: comma should be followed by whitespace
    
  2. Log in to comment