I'll provide a robust C linter to check forbidden functions without using regex, supporting function name patterns and exclusion rules. Here's a comprehensive implementation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fnmatch.h>
#define MAX_FUNCS 100
#define MAX_PATH_LENGTH 1024
#define MAX_LINE_LENGTH 4096
typedef struct {
char **allowed_funcs;
int allowed_count;
char **exclude_paths;
int exclude_count;
} LinterConfig;
int is_path_excluded(const char *path, LinterConfig *config) {
for (int i = 0; i < config->exclude_count; i++) {
if (strstr(path, config->exclude_paths[i]) != NULL) {
return 1;
}
}
return 0;
}
int is_function_allowed(const char *func_name, LinterConfig *config) {
for (int i = 0; i < config->allowed_count; i++) {
// Use fnmatch for pattern matching without regex
if (fnmatch(config->allowed_funcs[i], func_name, 0) == 0) {
return 1;
}
}
return 0;
}
int check_file_for_forbidden_funcs(const char *filename, LinterConfig *config) {
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Could not open file: %s\n", filename);
return 0;
}
char line[MAX_LINE_LENGTH];
int line_number = 0;
int found_forbidden = 0;
while (fgets(line, sizeof(line), file)) {
line_number++;
char *token = strtok(line, " \t\n()[];,");
while (token) {
// Basic function call detection
if (strchr(token, '(') != NULL) {
// Remove parenthesis
char *func_name = strtok(token, "(");
// Check if function is forbidden
if (func_name && !is_function_allowed(func_name, config)) {
printf("Forbidden function '%s' found in %s at line %d\n",
func_name, filename, line_number);
found_forbidden = 1;
}
}
token = strtok(NULL, " \t\n()[];,");
}
}
fclose(file);
return found_forbidden;
}
void traverse_directory(const char *dir_path, LinterConfig *config) {
DIR *dir;
struct dirent *entry;
char path[MAX_PATH_LENGTH];
dir = opendir(dir_path);
if (!dir) {
fprintf(stderr, "Cannot open directory: %s\n", dir_path);
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') continue; // Skip hidden files/dirs
snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);
struct stat path_stat;
stat(path, &path_stat);
if (S_ISDIR(path_stat.st_mode)) {
// Recursively check subdirectories
traverse_directory(path, config);
} else if (strstr(path, ".c") != NULL) {
// Only check .c files
if (!is_path_excluded(path, config)) {
check_file_for_forbidden_funcs(path, config);
}
}
}
closedir(dir);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <project_directory>\n", argv[0]);
return 1;
}
// Configuration setup
LinterConfig config = {0};
// Allowed functions (with wildcard support)
char *allowed_funcs[] = {
"malloc",
"printf",
"hello_*"
};
config.allowed_funcs = allowed_funcs;
config.allowed_count = sizeof(allowed_funcs) / sizeof(allowed_funcs[0]);
// Excluded paths
char *exclude_paths[] = {
"folder",
"file.c"
};
config.exclude_paths = exclude_paths;
config.exclude_count = sizeof(exclude_paths) / sizeof(exclude_paths[0]);
// Start linting from the specified directory
traverse_directory(argv[1], &config);
return 0;
}
Key features of this linter:
-
Robust Function Detection:
- Handles function calls with various syntax
- Uses
strtok() for tokenization
- Removes parentheses to extract function names
-
Pattern Matching:
- Uses
fnmatch() for wildcard support (e.g., "hello_*")
- No regex dependency
- Supports flexible function name patterns
-
Directory Traversal:
- Recursively checks all .c files in the project
- Skips hidden files and directories
- Supports nested directory structures
-
Exclusion Mechanism:
- Can exclude specific folders or files
- Checks path substrings for exclusion
-
Detailed Reporting:
- Prints filename and line number for forbidden function calls
- Provides clear error messages
Compilation:
gcc -o linter linter.c -lfnmatch
Usage:
./linter /path/to/project
Limitations and Potential Improvements:
- Basic static analysis (doesn't handle complex preprocessor macros)
- Might have false positives in comments or string literals
- Could be extended with more sophisticated parsing
Recommendations:
- Customize
allowed_funcs and exclude_paths as needed
- Add more sophisticated parsing for edge cases
- Consider integrating with build systems
This implementation provides a flexible, robust way to lint C projects for forbidden function calls without relying on regex.