write me a linter to check forbidden function for c program in robust way (dont use regex), all defined function in project is allowed { "allow_funcs": ["malloc", "printf", "hello_*"] "excludes": ["folder", "file.c"] }
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a robust C function linter that checks for forbidden function calls without using regex. It uses the Clang AST (Abstract Syntax Tree) for accurate parsing and analysis.
Implementation
c
#include <clang-c/Index.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
char **allowed_funcs;
size_t allowed_count;
char **excluded_files;
size_t excluded_count;
} Config;
// Configuration from your example
Config config = {
.allowed_funcs = (char*[]){"malloc", "printf", "hello_*"},
.allowed_count = 3,
.excluded_files = (char*[]){"folder", "file.c"},
.excluded_count = 2
};
// Check if a file is excluded
bool is_file_excluded(const char *filename) {
for (size_t i = 0; i < config.excluded_count; i++) {
if (strstr(filename, config.excluded_files[i]) != NULL) {
return true;
}
}
return false;
}
// Check if a function is allowed
bool is_function_allowed(const char *func_name) {
for (size_t i = 0; i < config.allowed_count; i++) {
const char *allowed = config.allowed_funcs[i];
// Handle wildcard patterns (like "hello_*")
if (strchr(allowed, '*') != NULL) {
// Get the prefix before the wildcard
size_t prefix_len = strchr(allowed, '*') - allowed;
if (strncmp(func_name, allowed, prefix_len) == 0) {
return true;
}
}
// Exact match
else if (strcmp(func_name, allowed) == 0) {
return true;
}
}
return false;
}
// Visitor function for the AST
enum CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {
CXSourceLocation location = clang_getCursorLocation(cursor);
if (clang_Location_isInSystemHeader(location)) {
return CXChildVisit_Continue;
}
CXString filename;
unsigned line, column;
clang_getPresumedLocation(location, &filename, &line, &column);
const char *filename_str = clang_getCString(filename);
if (is_file_excluded(filename_str)) {
clang_disposeString(filename);
return CXChildVisit_Continue;
}
clang_disposeString(filename);
CXCursorKind kind = clang_getCursorKind(cursor);
if (kind == CXCursor_CallExpr) {
CXCursor referenced = clang_getCursorReferenced(cursor);
if (clang_getCursorKind(referenced) == CXCursor_FunctionDecl) {
CXString func_name = clang_getCursorSpelling(referenced);
const char *func_name_str = clang_getCString(func_name);
if (!is_function_allowed(func_name_str)) {
CXString cursor_spelling = clang_getCursorSpelling(cursor);
CXString cursor_display = clang_getCursorDisplayName(cursor);
printf("Forbidden function call at %s:%d:%d: %s (%s)\n",
filename_str, line, column,
clang_getCString(cursor_spelling),
clang_getCString(cursor_display));
clang_disposeString(cursor_spelling);
clang_disposeString(cursor_display);
}
clang_disposeString(func_name);
}
}
return CXChildVisit_Recurse;
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <source_file.c>\n", argv[0]);
return 1;
}
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index,
argv[1],
NULL, 0,
NULL, 0,
CXTranslationUnit_None);
if (unit == NULL) {
fprintf(stderr, "Unable to parse translation unit\n");
return 1;
}
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(cursor, visitor, NULL);
clang_disposeTranslationUnit(unit);
clang_disposeIndex(index);
return 0;
}
Build Instructions
Install LLVM and Clang development libraries
Compile with:
code
clang -lclang linter.c -o function-linter
Usage
code
./function-linter source_file.c
Features
Robust parsing: Uses Clang's AST parser for accurate analysis
Wildcard support: Handles patterns like "hello_*"
Exclusion support: Skips files/folders in the exclude list
Precise location reporting: Shows exact line and column of violations
System header awareness: Ignores functions from system headers
Configuration
Modify the Config struct at the top of the file to change:
allowed_funcs: List of allowed function names/patterns
excluded_files: List of files/folders to exclude from checking
Limitations
Requires Clang/LLVM to be installed
Only checks direct function calls (not through function pointers)
Doesn't handle macro expansions (though Clang can be configured to)
For a more complete solution, you might want to:
Add command-line argument parsing for configuration