When tackling a 42 project, you often have a strict list of allowed functions defined by the project’s subject. This list can include some standard library functions (like write, read, malloc, etc.), while forbidding others. In many cases, your own libft (a custom-made library of functions that you wrote in an earlier project) is allowed as long as you understand that what’s really being checked is which functions are ultimately called. In other words, even if you call a function from your libft, the actual functions (e.g., your own version of ft_strlen, ft_memcpy, etc.) are still subject to the project’s allowed/forbidden function rules.
Note:
Whether libft itself is “allowed” depends on the project’s specific rules. Some subjects explicitly allow you to use your own libft, while others might require you not to use any library functions except the ones explicitly mentioned. Always double-check the subject’s instructions.
If you want to write a linter to verify that only the allowed functions (and only those functions) are used in a project, here’s a general roadmap to build your tool:
1. Define Your Allowed Functions List
-
Compile the list:
Read the subject’s guidelines and create a list (or dictionary) of allowed functions. For example:
allowed_functions = [
# Standard functions allowed by the subject, e.g.:
"write", "read", "malloc", "free", "exit",
# Plus your own libft functions if allowed:
"ft_strlen", "ft_strcpy", "ft_memcpy", "ft_strcmp"
]
-
Decide how to treat libft:
If using libft is allowed, then list the functions you expose in your libft. If not, then your linter should flag them.
2. Parse the C Code
Option A: Using a C Parser (Recommended)
Option B: Using Regex (For Simpler Cases)
-
Warning:
This approach is less robust. You’ll need to ensure comments and strings are stripped out before matching.
-
Example Approach:
- Preprocess the file to remove comments and string literals.
- Use a regular expression like
\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\( to find function names preceding an opening parenthesis.
- Aggregate and compare these names against your allowed functions list.
import re
# A very simplistic example:
def extract_function_calls(code):
# Remove simple comments (this is not robust for all edge cases)
code_no_comments = re.sub(r'//.*?$|/\*.*?\*/', '', code, flags=re.DOTALL | re.MULTILINE)
pattern = re.compile(r'\b([a-zA-Z_][a-zA-Z0-9_]*)\s*\(')
return pattern.findall(code_no_comments)
with open('your_source.c', 'r') as file:
code = file.read()
function_calls = extract_function_calls(code)
violation_list = [func for func in function_calls if func not in allowed_functions]
if violation_list:
print("Forbidden function calls found:", set(violation_list))
else:
print("All function calls are allowed.")
Note: This is a simplified example. A production-quality linter would need to handle cases such as function pointer declarations, macros, and many other edge cases.
3. Integrate and Run the Linter
- Command-Line Tool:
Design your linter as a command-line tool that takes file(s) as input and outputs any violations.
- Reporting:
Provide meaningful messages so that if an unallowed function is used, the linter identifies the file and specific line number (if possible).
Summary
-
Libft Usage:
Check your project’s subject to determine if using libft (or its individual functions) is allowed. If allowed, include your libft functions in your list of permitted functions.
-
Linter Implementation:
Building the linter involves:
- Creating a definitive list of allowed functions.
- Parsing the C code (preferably using a parser that builds an AST).
- Extracting and verifying function calls against your allowed list.
Using tools like Clang’s LibTooling or pycparser provides a more robust approach than relying solely on regex.
Good luck with your linter, and make sure to test it thoroughly with your project’s codebase to handle all the edge cases in C parsing!