[PATCH] D126258: [Clang] Avoid misleading 'conflicting types' diagnostic with no-prototype decls.

Cyndy Ishida via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 23 16:47:36 PDT 2022


cishida created this revision.
cishida added reviewers: jyknight, aaron.ballman.
Herald added a subscriber: ributzka.
Herald added a project: All.
cishida requested review of this revision.
Herald added a project: clang.

Clang has recently started diagnosing prototype redeclaration errors like rG385e7df33046 <https://reviews.llvm.org/rG385e7df33046d7292612ee1e3ac00a59d8bc0441>

This flagged legitimate issues in a codebase but was confusing to resolve because it actually conflicted with a function declaration from a system header and not from the one emitted with "note: ".

This patch updates the error handling to use the canonical declaration's source location instead to avoid misleading errors like the one described.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126258

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/prototype-redecls.c


Index: clang/test/Sema/prototype-redecls.c
===================================================================
--- clang/test/Sema/prototype-redecls.c
+++ clang/test/Sema/prototype-redecls.c
@@ -12,6 +12,10 @@
 void blerp(short);      // expected-note {{previous}}
 void blerp(x) int x; {} // expected-error {{conflicting types for 'blerp'}}
 
+void foo(int); // expected-note {{previous}}
+void foo();
+void foo() {} // expected-error {{conflicting types for 'foo'}}
+
 void glerp(int);
 void glerp(x) short x; {} // Okay, promoted type is fine
 
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -3911,6 +3911,7 @@
     // ASTContext::typesAreCompatible().
     if (Old->hasPrototype() && !New->hasWrittenPrototype() && NewDeclIsDefn &&
         Old->getNumParams() != New->getNumParams()) {
+      Old = Old->getCanonicalDecl();
       Diag(New->getLocation(), diag::err_conflicting_types) << New;
       Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126258.431524.patch
Type: text/x-patch
Size: 1109 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220523/9be99db2/attachment.bin>


More information about the cfe-commits mailing list