[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 10:42:58 PDT 2018


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

- Avoid possible crash


https://reviews.llvm.org/D52791

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


Index: test/Sema/misleading-var-type-decl.c
===================================================================
--- test/Sema/misleading-var-type-decl.c
+++ test/Sema/misleading-var-type-decl.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 %s -verify -Wmisleading-declarations -fsyntax-only
+
+void test(void) {
+    int *a, b; //  expected-warning {{misleading variable declaration, supposed to be a pointer type instead ?}}
+    int x, *y;
+    int *g, *h, *i, **j;
+
+    for (int *a, b;;) { } // expected-warning {{misleading variable declaration, supposed to be a pointer type instead ?}}
+    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,26 @@
   // 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);
+  if (DS) {
+    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_var_type_decl);
+      }
+    }
+  }
+
+  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_var_type_decl : Warning<
+  "misleading variable declaration, supposed to be a pointer type instead ?">,
+  InGroup<DiagGroup<"misleading-declarations">>;
 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.167978.patch
Type: text/x-patch
Size: 2312 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181002/65c2a705/attachment.bin>


More information about the cfe-commits mailing list