[PYTHON] Use a scripting language for a comfortable C ++ life

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.

** Bonus: Script to find duplicate macros in header files **

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)

Let's use a scripting language for a comfortable C ++ life 3-Let's leave graph creation to matplotlib-

Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-

Use a scripting language for a comfortable C ++ life 5-Use the Spyder integrated environment to check numerical data-

Recommended Posts

Use a scripting language for a comfortable C ++ life
Let's use a scripting language for a comfortable C ++ life 2 Automatically generate C ++ source
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Let's use a scripting language for a comfortable C ++ life C ++ implementation after verification with python
Let's use a scripting language for a comfortable C ++ life 6-How about developing a program as a library for Python?
Use a scripting language for a comfortable C ++ life 5 --Use the Spyder integrated environment to check numerical data-
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Scripting Language C ——How a text file without a shebang is executed
Prepare a programming language environment for data analysis
Mute Twitter trends and have a comfortable Twitter life! !!
Create an executable file in a scripting language
Created a header-only library management tool for C / C ++
Wrap C with Cython for use from Python
Set up a UDP server in C language
Wrap C ++ with Cython for use from Python
[C] Use qsort ()
Create a dataset of images to use for learning
Set up a development environment for natural language processing
Try to make a Python module in C language
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
Convenient to use matplotlib subplots in a for statement