From 9572a68b8832a09f716017742fbd3b4f1da3d131 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Tue, 5 Aug 2014 16:01:11 -0700 Subject: [PATCH] Add caffe/alt_fn rule to lint checks to check for functions (like memset & memcpy) with caffe_* alternatives that should be used instead. --- scripts/cpp_lint.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/scripts/cpp_lint.py b/scripts/cpp_lint.py index 3f80e72..61b8b0c 100755 --- a/scripts/cpp_lint.py +++ b/scripts/cpp_lint.py @@ -154,6 +154,7 @@ _ERROR_CATEGORIES = [ 'build/namespaces', 'build/printf_format', 'build/storage_class', + 'caffe/alt_fn', 'caffe/random_fn', 'legal/copyright', 'readability/alt_tokens', @@ -1559,6 +1560,37 @@ def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error): 'Use C++11 raw strings or concatenation instead.') +caffe_alt_function_list = ( + ('memset', ['caffe_set', 'caffe_memset']), + ('cudaMemset', ['caffe_gpu_set', 'caffe_gpu_memset']), + ('memcpy', ['caffe_copy', 'caffe_memcpy']), + ('cudaMemcpy', ['caffe_copy', 'caffe_gpu_memcpy']), + ) + + +def CheckCaffeAlternatives(filename, clean_lines, linenum, error): + """Checks for C(++) functions for which a Caffe substitute should be used. + + For certain native C functions (memset, memcpy), there is a Caffe alternative + which should be used instead. + + Args: + filename: The name of the current file. + clean_lines: A CleansedLines instance containing the file. + linenum: The number of the line to check. + error: The function to call with any errors found. + """ + line = clean_lines.elided[linenum] + for function, alts in caffe_alt_function_list: + ix = line.find(function + '(') + if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and + line[ix - 1] not in ('_', '.', '>'))): + disp_alts = ['%s(...)' % alt for alt in alts] + error(filename, linenum, 'caffe/alt_fn', 2, + 'Use Caffe function %s instead of %s(...).' % + (' or '.join(disp_alts), function)) + + c_random_function_list = ( 'rand(', 'rand_r(', @@ -4560,6 +4592,7 @@ def ProcessLine(filename, file_extension, clean_lines, line, CheckForNonStandardConstructs(filename, clean_lines, line, nesting_state, error) CheckVlogArguments(filename, clean_lines, line, error) + CheckCaffeAlternatives(filename, clean_lines, line, error) CheckCaffeRandom(filename, clean_lines, line, error) CheckPosixThreading(filename, clean_lines, line, error) CheckInvalidIncrement(filename, clean_lines, line, error) -- 2.7.4