[PATCH] D116635: Add warning to detect when calls passing arguments are made to functions without prototypes.

Dan Liew via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 4 17:07:42 PST 2022


delcypher created this revision.
delcypher added reviewers: arphaman, rapidsna, fcloutier, NoQ.
delcypher requested review of this revision.
Herald added a project: clang.

The warning is currently disabled by default but can be enabled with
`-Wstrict-calls-without-prototype`. Enabling it by default will be
handled by future patches.

The warning is intended to catch C code like this:

  int f(); // No prototype, accepts any number of arguments of any type.
  
  int call(void) {
    return f(1, 2, 3);
  }

While code like this is legal it can very easily invoke undefined behavior
by passing the wrong number of arguments or wrong types.

The warning only fires when arguments are passed to make it less noisy, i.e.
so that the warning does not fire on code like this:

  int g();
  int call(void) {
      return g();
  }

While the above code could invoke undefined behavior, if the definition of `g()` takes no
arguments then the lack of a prototype is benign.

This warning while similar to `-Wstrict-prototypes` is distinct because
it warns at call sites rather at declarations.

This patch currently warns on calls to K&R style function declarations where a
previous declaration has a prototype. It is currently not clear if this is the correct behavior
as noted in the included test case.

rdar://87118271


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116635

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/warn-calls-without-prototype.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116635.397430.patch
Type: text/x-patch
Size: 6878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220105/4b4715d4/attachment-0001.bin>


More information about the cfe-commits mailing list