Implement missing-braces warning and add a test case.

llvm-svn: 97893
This commit is contained in:
Tanya Lattner 2010-03-07 04:17:15 +00:00
parent 03b3eba151
commit 5029d56cc1
4 changed files with 23 additions and 1 deletions

View 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,

View 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'">;

View 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,

View File

@ -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}}