checkpatch: avoid sscanf test duplicated messages
[firefly-linux-kernel-4.4.55.git] / scripts / checkpatch.pl
index 19591af206c59d1225c69b864f77e453524d2d4e..e7c50977fe3d54d0cf4b1fd3515cba2a9b47acc9 100755 (executable)
@@ -289,11 +289,12 @@ our $Int_type     = qr{(?i)llu|ull|ll|lu|ul|l|u};
 our $Binary    = qr{(?i)0b[01]+$Int_type?};
 our $Hex       = qr{(?i)0x[0-9a-f]+$Int_type?};
 our $Int       = qr{[0-9]+$Int_type?};
+our $Octal     = qr{0[0-7]+$Int_type?};
 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
 our $Float     = qr{$Float_hex|$Float_dec|$Float_int};
-our $Constant  = qr{$Float|$Binary|$Hex|$Int};
+our $Constant  = qr{$Float|$Binary|$Octal|$Hex|$Int};
 our $Assignment        = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
 our $Compare    = qr{<=|>=|==|!=|<|>};
 our $Arithmetic = qr{\+|-|\*|\/|%};
@@ -378,6 +379,15 @@ our @modifierList = (
        qr{fastcall},
 );
 
+our @mode_permission_funcs = (
+       ["module_param", 3],
+       ["module_param_(?:array|named|string)", 4],
+       ["module_param_array_named", 5],
+       ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
+       ["proc_create(?:_data|)", 2],
+       ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
+);
+
 our $allowed_asm_includes = qr{(?x:
        irq|
        memory
@@ -423,7 +433,7 @@ our $Typecast       = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
 # Any use must be runtime checked with $^V
 
 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
-our $LvalOrFunc        = qr{($Lval)\s*($balanced_parens{0,1})\s*};
+our $LvalOrFunc        = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
 
 sub deparenthesize {
@@ -3921,6 +3931,19 @@ sub process {
                        }
                }
 
+# don't use __constant_<foo> functions outside of include/uapi/
+               if ($realfile !~ m@^include/uapi/@ &&
+                   $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
+                       my $constant_func = $1;
+                       my $func = $constant_func;
+                       $func =~ s/^__constant_//;
+                       if (WARN("CONSTANT_CONVERSION",
+                                "$constant_func should be $func\n" . $herecurr) &&
+                           $fix) {
+                               $fixed[$linenr - 1] =~ s/\b$constant_func\b/$func/g;
+                       }
+               }
+
 # prefer usleep_range over udelay
                if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
                        my $delay = $1;
@@ -4165,7 +4188,7 @@ sub process {
 # check for naked sscanf
                if ($^V && $^V ge 5.10.0 &&
                    defined $stat &&
-                   $stat =~ /\bsscanf\b/ &&
+                   $line =~ /\bsscanf\b/ &&
                    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
                     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
                     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
@@ -4473,6 +4496,30 @@ sub process {
                        WARN("EXPORTED_WORLD_WRITABLE",
                             "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
                }
+
+               foreach my $entry (@mode_permission_funcs) {
+                       my $func = $entry->[0];
+                       my $arg_pos = $entry->[1];
+
+                       my $skip_args = "";
+                       if ($arg_pos > 1) {
+                               $arg_pos--;
+                               $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
+                       }
+                       my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
+                       if ($^V && $^V ge 5.10.0 &&
+                           $line =~ /$test/) {
+                               my $val = $1;
+                               $val = $6 if ($skip_args ne "");
+
+                               if ($val !~ /^0$/ &&
+                                   (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
+                                    length($val) ne 4)) {
+                                       ERROR("NON_OCTAL_PERMISSIONS",
+                                             "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
+                               }
+                       }
+               }
        }
 
        # If we have no input at all, then there is nothing to report on