Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -315,6 +315,9 @@ def warn_redundant_parens_around_declarator : Warning< "redundant parentheses surrounding declarator">, InGroup>, DefaultIgnore; +def warn_misleading_pointer_decl : Warning< + "declaring a variable of type %0; did you mean to declare a pointer?">, + InGroup>, DefaultIgnore; def note_additional_parens_for_variable_declaration : Note< "add a pair of parentheses to declare a variable">; def note_raii_guard_add_name : Note< Index: lib/Sema/SemaStmt.cpp =================================================================== --- lib/Sema/SemaStmt.cpp +++ lib/Sema/SemaStmt.cpp @@ -77,7 +77,24 @@ // If we have an invalid decl, just return an error. if (DG.isNull()) return StmtError(); - return new (Context) DeclStmt(DG, StartLoc, EndLoc); + DeclStmt *DS = new (Context) DeclStmt(DG, StartLoc, EndLoc); + VarDecl *FirstVarDecl = nullptr; + for (auto *DI : DS->decls()) { + VarDecl *VD = dyn_cast(DI); + if (!VD) + continue; + + if (!FirstVarDecl) { + if (!VD->getType()->isPointerType()) + break; + FirstVarDecl = VD; + } else { + if (VD->getType() == FirstVarDecl->getType()->getPointeeType()) + Diag(VD->getLocation(), diag::warn_misleading_pointer_decl) << VD->getType(); + } + } + + return DS; } void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { Index: test/Sema/misleading-pointer-decl.c =================================================================== --- test/Sema/misleading-pointer-decl.c +++ test/Sema/misleading-pointer-decl.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -verify -Wmisleading-pointer-declarator -fsyntax-only + +void test(void) { + int *a, b; // expected-warning {{declaring a variable of type 'int'; did you mean to declare a pointer?}} + int x, *y; + int *g, *h, *i, **j; + + for (int *a, b;;) { } // expected-warning {{declaring a variable of type 'int'; did you mean to declare a pointer?}} + for (int x, *y;;) { } + for (int *g, *h, *i, **j;;) { } +}