Skip to content

Commit 4c7aae3

Browse files
authored
addons/namingng.py: Improve file name checking feature. (#5802)
(note: comment updated after force push; initial PR was incomplete) namingng.py attempted to derive the source filename from the name of the dumpfile. However, the dumpfile is not necessarily named according to this pattern, e.g. cppcheck will add the pid to the filename, making RE_FILE rules fail. Taking the first item of data.files seem to be more robust. To get the basename of the file, `os.path.basename()` is used. This solves (theoretical) issues on platforms with a different path separator. With this patch, all filenames are checked, not just those provided on the cppcheck command line. This is useful as header files will now also be part of this check, even if not explicitly specified on the command line. The "RE_FILE" key of the configuration JSON may contain a list of regular expressions, where any match will lead to acceptance of the filename. Both the full path and the basename of the files are tested. One use case for this combination of features is: ``` "RE_FILE":[ "/.*\\.h\\Z", "[a-z][a-z0-9_]*[a-z0-9]\\.[ch]\\Z" ] ``` This will accept any file naming convention of the platform used (assuming platform files are all referenced using an absolute path), while enforcing a particular naming scheme for project files.
1 parent 4d9e69e commit 4c7aae3

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

addons/namingng.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import cppcheckdata
2626
import sys
27+
import os
2728
import re
2829
import argparse
2930
import json
@@ -96,10 +97,14 @@ def process(dumpfiles, configfile, debugprint=False):
9697

9798
# Check File naming
9899
if "RE_FILE" in conf and conf["RE_FILE"]:
99-
mockToken = DataStruct(afile[:-5], "0", afile[afile.rfind('/') + 1:-5])
100-
msgType = 'File name'
101-
for exp in conf["RE_FILE"]:
102-
evalExpr(conf["RE_FILE"], exp, mockToken, msgType, errors)
100+
for source_file in data.files:
101+
basename = os.path.basename(source_file)
102+
good = False
103+
for exp in conf["RE_FILE"]:
104+
good |= bool(re.match(exp, source_file))
105+
good |= bool(re.match(exp, basename))
106+
if not good:
107+
errors.append(reportError(source_file, 0, 'style', 'File name ' + source_file + ' violates naming convention'))
103108

104109
# Check Namespace naming
105110
if "RE_NAMESPACE" in conf and conf["RE_NAMESPACE"]:

0 commit comments

Comments
 (0)