Skip to content

Warnings for c-run-time.c #1650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cgay opened this issue Jan 16, 2025 · 1 comment · May be fixed by #1706
Open

Warnings for c-run-time.c #1650

cgay opened this issue Jan 16, 2025 · 1 comment · May be fixed by #1706

Comments

@cgay
Copy link
Member

cgay commented Jan 16, 2025

There are many many warnings of this type for c-run-time.c:

c-run-time.c:1032:20: warning: passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]

$ /usr/bin/clang -v
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.6.0
@ZERO-70
Copy link

ZERO-70 commented Jan 25, 2025

1. Declare Function Prototypes

  • Ensure that every function used in your code has a prototype declared before it is called.
  • If the function is defined in the same file, move the function definition above where it is called, or declare a prototype at the top of the file.

Example:

// Function prototype declaration
void myFunction(int arg1, char arg2);

int main() {
    myFunction(10, 'a'); // Now the compiler knows the prototype
    return 0;
}

// Function definition
void myFunction(int arg1, char arg2) {
    // Function implementation
}

2. Include Header Files

  • If the function is defined in another file, ensure that you include the corresponding header file where the function prototype is declared.

Example:

// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H

void myFunction(int arg1, char arg2); // Function prototype

#endif
// main.c
#include "myheader.h" // Include the header file

int main() {
    myFunction(10, 'a'); // Now the compiler knows the prototype
    return 0;
}

3. Check for Implicit Function Declarations

  • Older C code might rely on implicit function declarations, which is no longer allowed. Make sure all functions are explicitly declared.

Example of Implicit Declaration (Bad):

int main() {
    myFunction(10, 'a'); // No prototype declared
    return 0;
}

void myFunction(int arg1, char arg2) {
    // Function implementation
}

Fix:

void myFunction(int arg1, char arg2); // Add prototype

int main() {
    myFunction(10, 'a'); // Now the compiler knows the prototype
    return 0;
}

void myFunction(int arg1, char arg2) {
    // Function implementation
}

4. Use Modern C Standards

  • Ensure your code is compiled with a modern C standard (e.g., C11 or C17). This will enforce stricter rules and help catch such issues.

Compiler Flag Example:

clang -std=c17 -Wall -Wextra -Werror c-run-time.c -o output
  • -std=c17: Use the C17 standard.
  • -Wall -Wextra: Enable all warnings.
  • -Werror: Treat warnings as errors (optional but recommended).

5. Review the Code for Legacy Issues

  • If c-run-time.c is part of a legacy codebase, it might contain many instances of this issue. Use tools like ctags, cscope, or an IDE (e.g., VSCode, CLion) to locate all function calls and ensure they have proper prototypes.

6. Automated Tools

  • Use static analysis tools like clang-tidy or cppcheck to automatically detect and fix such issues.

Example with clang-tidy:

clang-tidy c-run-time.c --fix-errors --checks=*

By following these steps, you should be able to resolve the warnings and ensure your code complies with modern C standards.

fraya added a commit to fraya/opendylan that referenced this issue May 2, 2025
Creates different arities for DFLN.

typedef dylan_value (*DLFN0)(void);
typedef dylan_value (*DLFN1)(dylan_value);
typedef dylan_value (*DLFN2)(dylan_value, dylan_value);
... etc

Changing DLFN to a union

typedef union {
  void* raw;
  DLFN0 mep0;
  DLFN1 mep1;
  DLFN2 mep2;
  DLFN3 mep3;
  DLFN4 mep4;
  DLFN5 mep5;
  DLFN6 mep6;
  DLFN7 mep7;
  DLFN8 mep8;
  DLFN9 mep9;
  DLFN10 mep10;
  DLFN64 mep64;
} DLFN;

Then rewrite the call logic using the correct
type per arity.

switch (teb->argument_count) {
  case 0: return (mep.mep0)();
  case 1: return (mep.mep1)(v[0]);
  ...

Closes dylan-lang#1650
@fraya fraya linked a pull request May 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants