[cfe-commits] [PATCH] missing braces warning

Douglas Gregor dgregor at apple.com
Mon Mar 1 08:23:11 PST 2010


On Feb 18, 2010, at 10:55 AM, Tanya Lattner wrote:

> Attached is a patch that implements missing braces warning and updates some test cases. Let me know what you think.


Sorry for the delay...

+def warn_missing_braces : Warning<
+    "suggest braces around initialization of subobject">;

You'll also need InGroup<DiagGroup<"missing-braces">>, DefaultIgnore as part of this, so that the warning is off by default but can be turned on with -Wmissing-braces.

The missing-braces group should also be added to -Wall (in include/clang/Basic/DiagnosticGroups.td), since GCC enables this warning as part of -Wall.

+  
+  // Warn about missing braces.
+  if (T->isArrayType()) {

Why restrict this warning to array types? GCC diagnoses struct/union types as well.

+    SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), 
+                     +                 diag::warn_missing_braces)
+    << StructuredSubobjectInitList->getSourceRange()
+    << CodeModificationHint::CreateInsertion(
+                                    StructuredSubobjectInitList->getLocStart(), 
+                                             llvm::StringRef("{"))
+    << CodeModificationHint::CreateInsertion(
+                                    StructuredSubobjectInitList->getLocEnd(), 
+                                             llvm::StringRef("}"));
+  }

getLocEnd() is going to point at the first character in the last token of the subobject initialization, so the '}' will be inserted in the wrong place. Use Preprocessor::getLocForEndOfToken() to adjust getLocEnd() to point just past the last character in the last token of the subobject initialization, which is the correct insertion point.

	- Doug



More information about the cfe-commits mailing list