]> granicus.if.org Git - clang/commitdiff
Implement missing-braces warning and add a test case.
authorTanya Lattner <tonic@nondot.org>
Sun, 7 Mar 2010 04:17:15 +0000 (04:17 +0000)
committerTanya Lattner <tonic@nondot.org>
Sun, 7 Mar 2010 04:17:15 +0000 (04:17 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97893 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaInit.cpp
test/Sema/warn-missing-braces.c [new file with mode: 0644]

index c3c0cf5d0c7c9b2c9e91cc3f3aa45379ada74fc6..2446df18c9297772b2c40a64b581a18ce09884ee 100644 (file)
@@ -48,7 +48,7 @@ def : DiagGroup<"init-self">;
 def : DiagGroup<"inline">;
 def : DiagGroup<"int-to-pointer-cast">;
 def : DiagGroup<"invalid-pch">;
-def : DiagGroup<"missing-braces">;
+def MissingBraces : DiagGroup<"missing-braces">;
 def : DiagGroup<"missing-declarations">;
 def : DiagGroup<"missing-format-attribute">;
 def : DiagGroup<"missing-include-dirs">;
@@ -156,6 +156,7 @@ def Most : DiagGroup<"most", [
     Format,
     Implicit,
     MismatchedTags,
+    MissingBraces,
     MultiChar,
     ReturnType,
     Switch,
index badd64c31b56ba9e25b7ad92734a58835299caec..71c66f9d9d042415025ff9f6799a62c8f3ce0be6 100644 (file)
@@ -1580,6 +1580,9 @@ def err_bitfield_width_exceeds_type_size : Error<
   "size of bit-field %0 exceeds size of its type (%1 bits)">;
 def err_anon_bitfield_width_exceeds_type_size : Error<
   "size of anonymous bit-field exceeds size of its type (%0 bits)">;
+def warn_missing_braces : Warning<
+  "suggest braces around initialization of subobject">,
+  InGroup<DiagGroup<"missing-braces">>, DefaultIgnore;
 
 def err_redefinition_of_label : Error<"redefinition of label '%0'">;
 def err_undeclared_label_use : Error<"use of undeclared label '%0'">;
index bf9f73c96150e095c0d165ac28b7ad8192d31d35..386bb1fea3730aa4167f54f76868ee915c86242c 100644 (file)
@@ -18,6 +18,7 @@
 #include "SemaInit.h"
 #include "Lookup.h"
 #include "Sema.h"
+#include "clang/Lex/Preprocessor.h"
 #include "clang/Parse/Designator.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ExprCXX.h"
@@ -497,6 +498,20 @@ void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
       = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
     StructuredSubobjectInitList->setRBraceLoc(EndLoc);
   }
+  
+  // Warn about missing braces.
+  if (T->isArrayType() || T->isRecordType()) {
+    SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), 
+                     +                 diag::warn_missing_braces)
+    << StructuredSubobjectInitList->getSourceRange()
+    << CodeModificationHint::CreateInsertion(
+                                    StructuredSubobjectInitList->getLocStart(), 
+                                             llvm::StringRef("{"))
+    << CodeModificationHint::CreateInsertion(
+                                    SemaRef.PP.getLocForEndOfToken(
+StructuredSubobjectInitList->getLocEnd()), 
+                                             llvm::StringRef("}"));
+  }
 }
 
 void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
diff --git a/test/Sema/warn-missing-braces.c b/test/Sema/warn-missing-braces.c
new file mode 100644 (file)
index 0000000..07eb61a
--- /dev/null
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -fsyntax-only -Wmissing-braces -verify %s
+
+int a[2][2] = { 0, 1, 2, 3 }; // expected-warning{{suggest braces}} expected-warning{{suggest braces}}
\ No newline at end of file