[PATCH] D52791: [Diagnostics] Check for misleading variable declarations

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 2 11:14:11 PDT 2018


xbolva00 updated this revision to Diff 167985.
xbolva00 added a comment.

- Addressed review comments


https://reviews.llvm.org/D52791

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaStmt.cpp
  test/Sema/misleading-pointer-decl.c


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;;) { }
+}
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<VarDecl>(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: 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<DiagGroup<"redundant-parens">>, DefaultIgnore;
+def warn_misleading_pointer_decl : Warning<
+  "declaring a variable of type %0; did you mean to declare a pointer?">,
+  InGroup<DiagGroup<"misleading-pointer-declarator">>, 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<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52791.167985.patch
Type: text/x-patch
Size: 2296 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181002/a2bba8b7/attachment.bin>


More information about the cfe-commits mailing list