[PATCH] D32646: Fix a bug that -Wmissing-braces fires on system headers

Yuka Takahashi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 30 05:42:18 PDT 2017


yamaguchi updated this revision to Diff 97224.
yamaguchi added a comment.

@v.g.vassilev 
I don't think early return is good idea, because someone might want to add some code under this function in the future.


https://reviews.llvm.org/D32646

Files:
  lib/Sema/SemaInit.cpp
  test/Sema/warn-missing-braces.c


Index: test/Sema/warn-missing-braces.c
===================================================================
--- test/Sema/warn-missing-braces.c
+++ test/Sema/warn-missing-braces.c
@@ -1,3 +1,21 @@
 // RUN: %clang_cc1 -fsyntax-only -Wmissing-braces -verify %s
 
+#ifdef BE_THE_HEADER
+#pragma clang system_header
+
+typedef struct _foo {
+    unsigned char Data[2];
+} foo;
+
+#define BAR { 0 }
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
 int a[2][2] = { 0, 1, 2, 3 }; // expected-warning{{suggest braces}} expected-warning{{suggest braces}}
+
+foo g = BAR; // should not show warnings
+
+#endif
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -885,17 +885,22 @@
       StructuredSubobjectInitList->setRBraceLoc(EndLoc);
     }
 
-    // Complain about missing braces.
+    // Complain about missing braces when rhs is not a macro from system header.
     if (T->isArrayType() || T->isRecordType()) {
-      SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
-                   diag::warn_missing_braces)
-          << StructuredSubobjectInitList->getSourceRange()
-          << FixItHint::CreateInsertion(
-                 StructuredSubobjectInitList->getLocStart(), "{")
-          << FixItHint::CreateInsertion(
-                 SemaRef.getLocForEndOfToken(
-                     StructuredSubobjectInitList->getLocEnd()),
-                 "}");
+      SourceLocation SpellingLoc = StructuredSubobjectInitList->getLocStart();
+      SpellingLoc = SemaRef.getSourceManager().getSpellingLoc(SpellingLoc);
+      if (SpellingLoc.isInvalid() || 
+            !(SemaRef.getSourceManager().isInSystemHeader(SpellingLoc))) {
+        SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
+                     diag::warn_missing_braces)
+            << StructuredSubobjectInitList->getSourceRange()
+            << FixItHint::CreateInsertion(
+                   StructuredSubobjectInitList->getLocStart(), "{")
+            << FixItHint::CreateInsertion(
+                   SemaRef.getLocForEndOfToken(
+                       StructuredSubobjectInitList->getLocEnd()),
+                   "}");
+      }
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32646.97224.patch
Type: text/x-patch
Size: 2245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170430/5225555b/attachment-0001.bin>


More information about the cfe-commits mailing list