From 13ca96a238089fb8e622791bc0dc441eb2dd29b4 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Sat, 24 Jan 2009 23:49:55 +0000 Subject: [PATCH] Fix for PR2100: merge types for variables. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62947 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 8 ++++---- test/Sema/decl-type-merging.c | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 test/Sema/decl-type-merging.c diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 0fb939e23a..9f308842d9 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -658,15 +658,15 @@ VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) { MergeAttributes(New, Old); - // Verify the types match. - QualType OldCType = Context.getCanonicalType(Old->getType()); - QualType NewCType = Context.getCanonicalType(New->getType()); - if (OldCType != NewCType && !Context.typesAreCompatible(OldCType, NewCType)) { + // Merge the types + QualType MergedT = Context.mergeTypes(New->getType(), Old->getType()); + if (MergedT.isNull()) { Diag(New->getLocation(), diag::err_redefinition_different_type) << New->getDeclName(); Diag(Old->getLocation(), diag::note_previous_definition); return New; } + New->setType(MergedT); // C99 6.2.2p4: Check if we have a static decl followed by a non-static. if (New->getStorageClass() == VarDecl::Static && (Old->getStorageClass() == VarDecl::None || diff --git a/test/Sema/decl-type-merging.c b/test/Sema/decl-type-merging.c new file mode 100644 index 0000000000..89bc494475 --- /dev/null +++ b/test/Sema/decl-type-merging.c @@ -0,0 +1,16 @@ +// RUN: clang -fsyntax-only -std=c99 -verify -pedantic %s + +int x[10]; +int x[] = {1,2,3}; +int testx[(sizeof(x) == sizeof(int) * 10) ? 1 : -1]; + +int (*a)(int (*x)[10], int (*y)[]); +int (*a)(int (*x)[], int (*y)[5]); +int b() { +int x[10], y[5]; +a(&x, &y); +a(&y, &y); // expected-warning {{incompatible pointer}} +a(&x, &x); // expected-warning {{incompatible pointer}} +} + + -- 2.40.0