Skip to content

Commit

Permalink
Fix: Bad detection for global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Gautier committed Jul 8, 2017
1 parent 739d74f commit af5f184
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
28 changes: 22 additions & 6 deletions betty-style.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,7 @@ sub process {
$fixlinenr = -1;
my $nbfunc = 0;
my $inscope = 0;
my $infunction_params = 0;
my $funclines = 0;

foreach my $line (@lines) {
Expand Down Expand Up @@ -3593,12 +3594,12 @@ sub process {
}

# Check for global variables (not allowed).
if ($allow_global_variables == 0) {
if ($inscope == 0 &&
($line =~ /^\+\s*$Type\s*$Ident(?:\s+$Modifier)*(?:\s*=\s*.*)?;/ ||
$line =~ /^\+\s*$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(].*;/ ||
$line =~ /^\+\s*$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
$line =~ /^\+\s*$declaration_macros/)) {
if ($allow_global_variables == 0 &&
$inscope == 0 && $infunction_params == 0) {
if ($line =~ /^\+\s*$Type\s*$Ident(?:\s+$Modifier)*(?:\s*=\s*.*)?;/ ||
$line =~ /^\+\s*$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(].*;/ ||
$line =~ /^\+\s*$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
$line =~ /^\+\s*$declaration_macros/) {
ERROR("GLOBAL_DECLARATION",
"global variables are not allowed\n" . $herecurr);
}
Expand Down Expand Up @@ -3671,6 +3672,21 @@ sub process {
}
}

#check if in function parameters section
if ($line =~ /\b$Type\s+$Ident\s*\(/ ||
$line =~ /\b$Ident(?:\s+|\s*\*\s*)$Ident\s*\(/ &&
$line !~ /(?:else|if|while|for|switch)/) {
if ($line =~ /(\()/g) {
$infunction_params += $#-;
}
if ($line =~ /(\))/g) {
$infunction_params -= $#-;
}
}
if ($infunction_params > 0 && $line =~ /(\))/g) {
$infunction_params -= $#-;
}

# check for uses of DEFINE_PCI_DEVICE_TABLE
if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
if (WARN("DEFINE_PCI_DEVICE_TABLE",
Expand Down
9 changes: 9 additions & 0 deletions tests/style/variables/variables2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Fixed: token_t *token was detected as a global variable
*/

static const char *process_token(command_chain_t *chain, command_t **cmd,
token_t *token, const char *start)
{
return (NULL);
}
Empty file.

0 comments on commit af5f184

Please sign in to comment.