Just because the development target language is C ++, there is no reason not to use any other language. To check the health of the source code you are writing in C ++, you can check it using a scripting language.
If you modify the existing code by trial and error, it may become strange code before you know it. You may be in a strange situation when you cut and paste another program into one set to enhance its functionality.
-Although it is defined in the header file, macro constants and macro functions that are not used in the scope of the target program are left unattended. ・ Unused extern declaration -Existence of macro definition with the same name
If you don't review the code you wrote in "Move it first", you'll end up with such weird code. If the number of lines of code is small grep '#define' *.h *.cpp It will be somehow, but In a situation where the total number of lines in a program is increasing and unfortunately many macros are included, you want a tool that is better than grep.
Macro is #define DATA_SIZE (1000) #define square(x) ((x) * (x))
It is described as being separated by spaces and tabs, such as There is a rule that the first field is "#define". Taking advantage of this, the above-mentioned health check tool can be created by creating the following tools.
-Split into fields as space / tab delimited text on lines that contain #define. -When the first field matches "#define", create a dictionary with the next field as a keyword. The value of the dictionary is a list of the file names of the source code of the occurrence location. -After processing a series of source code in this way, if the dictionary value contains multiple source code file names, display them.
Such a tool can be easily written in a scripting language. Let's live a comfortable C ++ life using a scripting language.
I wrote a reference script.
doubledMacro.py
# -*- coding: utf-8 -*-
#pylint: disable=C0103
import os
u"""
Script to find duplicate macros in header files
"""
def findDoubledMacro(wdir):
d = {}
for root, dirs, files in os.walk(wdir):
files = [p for p in files if os.path.splitext(p)[1] in ('.h', '.hpp')]
for p in files:
fullname = os.path.join(root, p)
for line in open(fullname, "rt"):
oline=line.replace("(", " ")
oline=oline.replace(")", " ")
oline=oline.replace(",", " ")
f = oline.strip().split()
if len(f) >= 2 and f[0] == "#define":
if not d.has_key(f[1]):
d[f[1]] = []
d[f[1]].append(p)
for k in d.keys():
if len(d[k]) > 1:
print k, d[k]
if __name__ == "__main__":
wdir = "."
findDoubledMacro(wdir)
Example of execution result
python
max ['sourceA.h', 'sourceB.h']
_UNICODE ['sourceB.h', 'sourceC.h']
DATA_SIZE ['sourceA.h', 'sourceB.h']
Input data used for execution results
sourceA.h
#ifndef SOURCEB_H
#define SOURCEB_H
#define DATA_SIZE (100)
#define _UNICODE
#define DEBUG_VIEW
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
sourceB.h
#ifndef SOURCEB_H
#define SOURCEB_H
#define DATA_SIZE (100)
#define _UNICODE
#define DEBUG_VIEW
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
subDir/sourceC.h
#ifndef SOURCEC_H
#define SOURCEC_H
#define DATA_SIZEC (100)
#define _UNICODE
#define DEBUG_VIEW2
#endif
Postscript:
[Let's use a scripting language for a comfortable C ++ life 2 Automatically generate C ++ source] (http://qiita.com/nonbiri15/items/3e5361613390a8ff516e)
Recommended Posts