Repository: pyccel/pyccel (python)

Issue:
Bug when compiling with C when using variable `I`
### Describe the bug
When attempting to compile Python scripts using Pyccel with C, the process fails during the shared library generation stage when using the variable `I`. The error appears to be related to the handling of global variables in the generated C code. Could it be related to the imaginary unit `I`?

### To Reproduce

1. Clone git repo, setup virtual environment, and install Pyccel

```bash
git clone --recurse-submodules git@github.com:pyccel/pyccel.git
cd pyccel

python3 -m venv env_pyccel
source env_pyccel/bin/activate
pip install .
```
2. Create two simple Python files (`test_I.py` and `test_J.py`) that define variables `J` and `I`, respectively.

```
echo "J=1" > test_J.py
echo "I=1" > test_I.py
```


3. Compile test_I.py and test_J.py with fortran and C.

```
pyccel test_J.py --language=fortran
pyccel test_J.py --language=c
pyccel test_I.py --language=fortran
pyccel test_I.py --language=c
```

The three first tests work fine for me, but I get an error on the fourth.

### Error details
The error occurs specifically when compiling `test_I.py` with C.

Error messages:

```bash
> pyccel test_J.py --language=fortran
> pyccel test_J.py --language=c
> pyccel test_I.py --language=fortran
> pyccel test_I.py --language=c

ERROR at shared library generation stage
Traceback (most recent call last):
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/bin/pyccel", line 8, in <module>
    sys.exit(pyccel())
             ^^^^^^^^
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/pyccel/commands/console.py", line 266, in pyccel
    execute_pyccel(filename,
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/pyccel/codegen/pipeline.py", line 408, in execute_pyccel
    generated_filepath, shared_lib_timers = create_shared_library(codegen,
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/pyccel/codegen/python_wrapper.py", line 220, in create_shared_library
    wrapper_compiler.compile_module(wrapper_compile_obj,
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/pyccel/codegen/compiling/compilers.py", line 358, in compile_module
    self.run_command(cmd, verbose)
  File "/home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/pyccel/codegen/compiling/compilers.py", line 503, in run_command
    raise RuntimeError(err_msg)
RuntimeError: Failed to build module
In file included from /home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/numpy/_core/include/numpy/npy_common.h:380,
                 from /home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/numpy/_core/include/numpy/ndarraytypes.h:4,
                 from /home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/numpy/_core/include/numpy/ndarrayobject.h:12,
                 from /home/maxlinadmin/git_repos/pyccel/env_pyccel/lib/python3.12/site-packages/numpy/_core/include/numpy/arrayobject.h:5,
                 from /home/maxlinadmin/git_repos/pyccel/__pyccel__/test_I_wrapper.h:5,
                 from /home/maxlinadmin/git_repos/pyccel/__pyccel__/test_I_wrapper.c:4:
/home/maxlinadmin/git_repos/pyccel/__pyccel__/test_I.h:8:16: error: expected identifier or ‘(’ before ‘__extension__’
    8 | extern int64_t I;
      |                ^
/home/maxlinadmin/git_repos/pyccel/__pyccel__/test_I_wrapper.c: In function ‘PyInit_test_I’:
/home/maxlinadmin/git_repos/pyccel/__pyccel__/test_I_wrapper.c:56:34: error: lvalue required as unary ‘&’ operand
   56 |     Dummy_0000 = Int64_to_PyLong(&I);
      |                                  ^

```

### Expected behavior
The Python scripts `test_I.py` and `test_J.py` should be successfully compiled using Pyccel both with Fortran and C.

### Language

Error occurs only in C.

### Additional context

Pull request:
Flag `I` as a conflicting variable name
Add `I` to the list of names of variables which are not valid in C. Fixes #1998

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9f8be1c7..b2444ec1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -119,6 +119,7 @@ All notable changes to this project will be documented in this file.
 -   #1924 : Fix internal error arising in Duplicate or list comprehensions.
 -   #1970 : Fix missing `TypeError` for wrong type passed as optional argument.
 -   #1985 : Fix implementation of `gcd` and `lcm` for C and Fortran.
+-   #1998 : Fix compiler error when using a variable named `I`.
 
 ## \[1.12.0\] - 2024-05-13
 
diff --git a/pyccel/naming/cnameclashchecker.py b/pyccel/naming/cnameclashchecker.py
index 6485bcfd..27e9fa71 100644
--- a/pyccel/naming/cnameclashchecker.py
+++ b/pyccel/naming/cnameclashchecker.py
@@ -37,7 +37,8 @@ class CNameClashChecker(LanguageNameClashChecker):
         'GET_INDEX_FUNC_H2', 'GET_INDEX_FUNC', 'GET_INDEX',
         'INDEX', 'GET_ELEMENT', 'free_array', 'free_pointer',
         'get_index', 'numpy_to_ndarray_strides',
-        'numpy_to_ndarray_shape', 'get_size', 'order_f', 'order_c', 'array_copy_data'])
+        'numpy_to_ndarray_shape', 'get_size', 'order_f', 'order_c', 'array_copy_data',
+        'I'])
 
     def has_clash(self, name, symbols):
         """
diff --git a/pyccel/naming/languagenameclashchecker.py b/pyccel/naming/languagenameclashchecker.py
index fa672a90..3149e0c4 100644
--- a/pyccel/naming/languagenameclashchecker.py
+++ b/pyccel/naming/languagenameclashchecker.py
@@ -19,6 +19,10 @@ class LanguageNameClashChecker(metaclass = Singleton):
     """
     keywords = None
 
+    def __init__(self): #pylint: disable=useless-parent-delegation
+        # This __init__ function is required so the Singleton can detect a signature
+        super().__init__()
+
     def _get_collisionless_name(self, name, symbols):
         """
         Get a name which doesn't collision with keywords or symbols.


Test patch:
diff --git a/tests/epyccel/modules/consts.py b/tests/epyccel/modules/consts.py
index c5754ce7..94f8c2ba 100644
--- a/tests/epyccel/modules/consts.py
+++ b/tests/epyccel/modules/consts.py
@@ -13,3 +13,5 @@ method = 3
 compl = 3+5j
 
 tiny = np.int32(4)
+
+I = 4
diff --git a/tests/epyccel/test_epyccel_modules.py b/tests/epyccel/test_epyccel_modules.py
index 90de6432..67ede7a0 100644
--- a/tests/epyccel/test_epyccel_modules.py
+++ b/tests/epyccel/test_epyccel_modules.py
@@ -124,7 +124,7 @@ def test_module_6(language):
     modnew = epyccel(mod, language=language)
 
     atts = ('g', 'R0', 'rMin', 'rMax', 'skip_centre',
-            'method', 'compl', 'tiny')
+            'method', 'compl', 'tiny', 'I')
     for att in atts:
         mod_att = getattr(mod, att)
         modnew_att = getattr(modnew, att)