简体   繁体   中英

selective preprocessing of #ifdefs in c

I have large .c and .h files from which i have to select only preprocessors directives satisfying these conditions as below

#if(VALUE==5) ||(VALUE==6)

Rest of preprocessors directives should remain untouched.I have already looked into unifdef and sunifdef they will allow me to identify #if and #ifdef but i want to chose selectively not all #if , #ifdefs . Is there a utility thats allows me to give, eg, VALUE==5 and chose only those?

Basically I need preprocessor directives selection based on my input value.

Try Running the Script, it will print the lines of code and code present inside the #if block. This only scans the file statically. You can give it a try.

# ./run.sh filename "String to search in if"
# example ./run.sh test.h "#if VALUE"

# Get the line number of Searching string locations
# store it in a file
awk "/$2/{ print NR }" $1 > temp_if

# to store total lines present inside the if contruct
totalLinesCount=0

# read one line at a time, that is go through one occurance of string at a time
while read matchLineNum
do
    printf "Match Found in line number $matchLineNum\n"
    #search for first occurance of #elif from the matched string.
    elifLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#elif/{ print NR;exit }")
    if [ "$elifLineNum" == "" ]
    then
        # if not found set it to some high value
        elifLineNum=$(wc -c $1)
    fi
    elseLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#else/{ print NR;exit }")
    if [ "$elseLineNum" == "" ]
    then
        elseLineNum=$(wc -c $1)
    fi
    endifLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#endif/{ print NR;exit }")

    # check if #endif / #elif / #else occurs first
    if [ $endifLineNum -lt $elseLineNum -a $endifLineNum -lt $elifLineNum ]
    then
        # store the code count
        totalLinesCount=$(expr $totalLinesCount + $(expr $endifLineNum - 1))
        # store the code
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $endifLineNum - 1) >> out

    elif [ $elseLineNum -lt $endifLineNum -a $elseLineNum -lt $elifLineNum ]
    then
        totalLinesCount=$(expr $totalLinesCount + $(expr $elseLineNum - 1))
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $elseLineNum - 1) >> out

    elif [ $elifLineNum -lt $elseLineNum -a $elifLineNum -lt $endifLineNum ]
    then
        totalLinesCount=$(expr $totalLinesCount + $(expr $elifLineNum - 1))
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $elifLineNum - 1) >> out

    else
        printf "\n Error \n"
    fi
done < temp_if
printf "\nTotal number of lines present inside the given condition is $totalLinesCount\n"
rm -f temp_if
printf '\nFinal Extracted Out\n'
cat out
rm -f out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM