yamaguchi created this revision.
This is an update patch for bug [1].
-Wmissing-braces should not fire for system headers.
[1] https://bugs.llvm.org/show_bug.cgi?id=24007
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,24 @@
// RUN: %clang_cc1 -fsyntax-only -Wmissing-braces -verify %s
+#ifdef BE_THE_HEADER
+#pragma clang system_header
+
+typedef struct _GUID {
+ unsigned Data1;
+ unsigned short Data2;
+ unsigned short Data3;
+ unsigned char Data4[8];
+} GUID;
+
+#define REGISTRY_EXTENSION_GUID { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
int a[2][2] = { 0, 1, 2, 3 }; // expected-warning{{suggest braces}}
expected-warning{{suggest braces}}
+
+GUID g = REGISTRY_EXTENSION_GUID; // 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.isValid() &&
+ SemaRef.getSourceManager().isInSystemHeader(SpellingLoc))) {
+ SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
+ diag::warn_missing_braces)
+ << StructuredSubobjectInitList->getSourceRange()
+ << FixItHint::CreateInsertion(
+ StructuredSubobjectInitList->getLocStart(), "{")
+ << FixItHint::CreateInsertion(
+ SemaRef.getLocForEndOfToken(
+ StructuredSubobjectInitList->getLocEnd()),
+ "}");
+ }
}
}
}
Index: test/Sema/warn-missing-braces.c
===================================================================
--- test/Sema/warn-missing-braces.c
+++ test/Sema/warn-missing-braces.c
@@ -1,3 +1,24 @@
// RUN: %clang_cc1 -fsyntax-only -Wmissing-braces -verify %s
+#ifdef BE_THE_HEADER
+#pragma clang system_header
+
+typedef struct _GUID {
+ unsigned Data1;
+ unsigned short Data2;
+ unsigned short Data3;
+ unsigned char Data4[8];
+} GUID;
+
+#define REGISTRY_EXTENSION_GUID { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
+
+#else
+
+#define BE_THE_HEADER
+#include __FILE__
+
int a[2][2] = { 0, 1, 2, 3 }; // expected-warning{{suggest braces}} expected-warning{{suggest braces}}
+
+GUID g = REGISTRY_EXTENSION_GUID; // 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.isValid() &&
+ SemaRef.getSourceManager().isInSystemHeader(SpellingLoc))) {
+ SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
+ diag::warn_missing_braces)
+ << StructuredSubobjectInitList->getSourceRange()
+ << FixItHint::CreateInsertion(
+ StructuredSubobjectInitList->getLocStart(), "{")
+ << FixItHint::CreateInsertion(
+ SemaRef.getLocForEndOfToken(
+ StructuredSubobjectInitList->getLocEnd()),
+ "}");
+ }
}
}
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits