A bash script that automatically parses C files, extracts function definitions, and adds their declarations at the top of the file. This helps maintain proper function prototypes in C codebases and avoid implicit declarations.
The add_declaration.sh
script is designed to help C developers maintain clean and organized code by automatically generating function declarations for their C files. This script simplifies the process of managing function prototypes and ensures that all necessary declarations are present at the top of the file.
The add_declaration.sh
script helps C developers maintain clean code by:
- Automatically finding all function definitions in C files
- Creating appropriate function declarations (prototypes)
- Adding these declarations at the top of the file in an organized manner
- Avoiding duplicate declarations by managing existing declaration sections
- Parses C files to extract function definitions
- Generates function declarations and adds them to the top of the file
- Handles multiple function formatting styles
- Manages existing declaration sections to avoid duplicates
- Creates backup files before making changes
-
Clone this repository:
git clone https://github.com/yourusername/declarations_script.git cd declarations_script
-
Make the script executable:
chmod +x add_declaration.sh
The script can be used in several ways:
./add_declaration.sh
This will process all .c
files found in the src
directory (the directory must exist).
./add_declaration.sh path/to/your/file.c
./add_declaration.sh path/to/directory
The script performs the following operations:
- Locates C files based on the provided arguments
- For each file:
- Uses AWK to extract function definitions
- Converts these definitions into proper declarations by adding semicolons
- Checks if the file already has a declarations section
- If it exists, removes the old declarations to avoid duplicates
- Inserts the new declarations section at the appropriate location:
- After the last
#include
statement if includes exist - After header comments if no includes exist
- At the beginning of the file otherwise
- After the last
- Creates a backup of the original file (
.bak
extension)
Given a C file like this:
#include <stdio.h>
void print_hello() {
printf("Hello, world!\n");
}
int calculate_sum(int a, int b) {
return a + b;
}
The script will transform it to:
#include <stdio.h>
/* Function declarations */
void print_hello();
int calculate_sum(int a, int b);
void print_hello() {
printf("Hello, world!\n");
}
int calculate_sum(int a, int b) {
return a + b;
}
- The script creates backup files with
.bak
extension before making changes - It handles multiple function formatting styles
- It properly manages existing declaration sections
- Comments in function definitions are properly removed from declarations
- Bash shell
- AWK (comes pre-installed on most Unix-like systems)
- Standard Unix tools (grep, find, etc.)
See the LICENSE file for details.