]> granicus.if.org Git - clang/commitdiff
Add basic fixits for -Wuninitialized-experimental
authorTed Kremenek <kremenek@apple.com>
Fri, 21 Jan 2011 19:41:46 +0000 (19:41 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 21 Jan 2011 19:41:46 +0000 (19:41 +0000)
to suggest initializations for pointer and
ObjC pointer types.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123995 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/AnalysisBasedWarnings.cpp
test/Sema/uninit-variables.c

index f595a1cc4b2830f1b0833299dcab30d1da98e065..e4b4685552a34d8198bca36de2070090d7de5d25 100644 (file)
@@ -824,6 +824,8 @@ def warn_var_is_uninit : Warning<"use of uninitialized variable %0">,
   InGroup<DiagGroup<"uninitialized-experimental">>, DefaultIgnore;
 def note_var_is_uninit : Note<
   "variable %0 is possibly uninitialized when used here">;
+def note_var_fixit_add_initialization : Note<
+  "add initialization to silence this warning">;
 def err_init_incomplete_type : Error<"initialization of incomplete type %0">;
 
 def err_temp_copy_no_viable : Error<
index 67ddbf5bf98c49d98e3019978f4422a04541502d..91f95a762ac2d29a7ed9999ab0ecaa4f6b66f0c6 100644 (file)
@@ -16,6 +16,7 @@
 #include "clang/Sema/AnalysisBasedWarnings.h"
 #include "clang/Sema/SemaInternal.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Lex/Preprocessor.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/ExprObjC.h"
@@ -405,7 +406,7 @@ public:
       
       S.Diag(vd->getLocStart(), diag::warn_var_is_uninit)
         << vd->getDeclName() << vd->getSourceRange();
-
+      
       // Sort the uses by their SourceLocations.  While not strictly
       // guaranteed to produce them in line/column order, this will provide
       // a stable ordering.
@@ -417,6 +418,24 @@ public:
         S.Diag(dr->getLocStart(), diag::note_var_is_uninit)
           << vd->getDeclName() << dr->getSourceRange();
       }
+
+      // Suggest possible initialization (if any).
+      const char *initialization = 0;
+      QualType vdTy = vd->getType();
+      
+      if (vdTy->getAs<ObjCObjectPointerType>()) {
+        initialization = " = nil";
+      }
+      else if (vdTy->getAs<PointerType>()) {
+        initialization = " = 0";
+      }
+      
+      if (initialization) {
+        SourceLocation loc = S.PP.getLocForEndOfToken(vd->getLocEnd());
+        S.Diag(loc, diag::note_var_fixit_add_initialization)
+          << FixItHint::CreateInsertion(loc, initialization);
+      }
+
       delete vec;
     }
     delete uses;
index 62fcda019a4768f303ac8784a5ddecd7dc5f0b3c..76c3dc12c2e749f57f8d058456bdc9f99ba1b782 100644 (file)
@@ -106,7 +106,7 @@ void test16() {
 void test17() {
   // Don't warn multiple times about the same uninitialized variable
   // along the same path.
-  int *x; // expected-warning{{use of uninitialized variable 'x'}}
+  int *x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
   *x = 1; // expected-note{{variable 'x' is possibly uninitialized when used here}}
   *x = 1; // no-warning
 }