42 c project have list allow function per project, libft is allow or not,.. i want write…
- User
42 c project have list allow function per project, libft is allow or not,.. i want write a linter to check it
- Assistant
Got it! You want to write a linter for your 42 C projects that checks which functions are allowed or disallowed per project. For example, in some projects, using
libftfunctions might be allowed, while in others, they might be forbidden.
What you want to do:
- Parse the C source files in a project.
- Detect function calls.
- Check if those functions are allowed or disallowed according to the project rules.
- Report violations.
Step-by-step approach to build this linter:
1. Define the rules per project
Create a config file (e.g.,
rules.json) that lists allowed and disallowed functions per project. Example:{ "project_name": { "allowed": ["write", "read", "malloc", "free"], "disallowed": ["printf", "puts", "libft_*"] } }You can use wildcards like
libft_*to block all functions starting withlibft_.
2. Parse C files to find function calls
You can do this in several ways:
-
Simple regex (not perfect but might work for simple cases):
\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\(This matches function calls by capturing the function name before the
(. -
Use a C parser (more robust):
- Use tools like clang's libclang Python bindings.
- Or use a C parser library like
pycparser(though it only supports C99 and might need preprocessing).
3. Check function calls against rules
For each function call found:
- Check if it matches any disallowed pattern.
- If yes, report an error.
- If allowed list exists, check if the function is in allowed list; if not, report error.
4. Output the results
Print errors with file name, line number, and function name.
Example: Simple Python script using regex
import re import sys import json from pathlib import Path # Load rules with open('rules.json') as f: rules = json.load(f) project = sys.argv[1] source_dir = Path(sys.argv[2]) allowed = rules[project].get("allowed", []) disallowed = rules[project].get("disallowed", []) # Compile regex for function calls func_call_re = re.compile(r'\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\(') def matches_pattern(name, patterns): for p in patterns: if p.endswith('*'): if name.startswith(p[:-1]): return True elif name == p: return True return False for file in source_dir.rglob('*.c'): with open(file) as f: for i, line in enumerate(f, 1): for match in func_call_re.finditer(line): func = match.group(1) if matches_pattern(func, disallowed): print(f"{file}:{i}: disallowed function '{func}' used") elif allowed and not matches_pattern(func, allowed): print(f"{file}:{i}: function '{func}' not in allowed list")
How to use:
python linter.py project_name path/to/source
Improvements you can add:
- Handle header files
.h. - Use a real C parser for accuracy.
- Support more complex rules (e.g., allow
libftonly if linked). - Integrate with your build system or git hooks.
If you want, I can help you build a more advanced version or integrate with libclang! Just ask.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.