summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2015-12-10 23:31:01 +0000
committerJohn McCall <rjmccall@apple.com>2015-12-10 23:31:01 +0000
commit83760378617dfa9670ce9439e9bfdcf2a5c81b90 (patch)
tree3001c116d3d00bbc1df3fe26a74d4568f85a68c4
parent07206ea19dd55c8180d443a0d55a25bd67fb0858 (diff)
downloadbcm5719-llvm-83760378617dfa9670ce9439e9bfdcf2a5c81b90.tar.gz
bcm5719-llvm-83760378617dfa9670ce9439e9bfdcf2a5c81b90.zip
In Objective-C, ignore attempts to redefine the ARC/GC qualifier macros.
This works around existing system headers which unconditionally redefine these macros. This is reasonably safe to do because we used to warn about it anyway (outside of system headers). Continue to warn if the redefinition would have changed the expansion. Still permit redefinition if the macro is explicitly #undef'ed first. rdar://23788307 llvm-svn: 255311
-rw-r--r--clang/include/clang/Basic/DiagnosticLexKinds.td3
-rw-r--r--clang/lib/Lex/PPDirectives.cpp24
-rw-r--r--clang/test/Lexer/objc_macros.m22
3 files changed, 49 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 34e4759a85f..38599d6b1f5 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -293,6 +293,9 @@ def warn_pp_macro_hides_keyword : Extension<
def warn_pp_macro_is_reserved_id : Warning<
"macro name is a reserved identifier">, DefaultIgnore,
InGroup<ReservedIdAsMacro>;
+def warn_pp_objc_macro_redef_ignored : Warning<
+ "ignoring redefinition of Objective-C qualifier macro">,
+ InGroup<DiagGroup<"objc-macro-redefinition">>;
def pp_invalid_string_literal : Warning<
"invalid string literal, ignoring final '\\'">;
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index fd16168203a..298543e2e95 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -2266,6 +2266,30 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok,
// Finally, if this identifier already had a macro defined for it, verify that
// the macro bodies are identical, and issue diagnostics if they are not.
if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
+ // In Objective-C, ignore attempts to directly redefine the builtin
+ // definitions of the ownership qualifiers. It's still possible to
+ // #undef them.
+ auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
+ return II->isStr("__strong") ||
+ II->isStr("__weak") ||
+ II->isStr("__unsafe_unretained") ||
+ II->isStr("__autoreleasing");
+ };
+ if (getLangOpts().ObjC1 &&
+ SourceMgr.getFileID(OtherMI->getDefinitionLoc())
+ == getPredefinesFileID() &&
+ isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
+ // Warn if it changes the tokens.
+ if ((!getDiagnostics().getSuppressSystemWarnings() ||
+ !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
+ !MI->isIdenticalTo(*OtherMI, *this,
+ /*Syntactic=*/LangOpts.MicrosoftExt)) {
+ Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
+ }
+ assert(!OtherMI->isWarnIfUnused());
+ return;
+ }
+
// It is very common for system headers to have tons of macro redefinitions
// and for warnings to be disabled in system headers. If this is the case,
// then don't bother calling MacroInfo::isIdenticalTo.
diff --git a/clang/test/Lexer/objc_macros.m b/clang/test/Lexer/objc_macros.m
new file mode 100644
index 00000000000..0223bed4e3b
--- /dev/null
+++ b/clang/test/Lexer/objc_macros.m
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only "-triple" "x86_64-apple-macosx10.10.0" -fobjc-runtime-has-weak -fobjc-weak %s -verify %s
+
+#define __strong
+// expected-warning@-1 {{ignoring redefinition of Objective-C qualifier macro}}
+#define __weak
+// expected-warning@-1 {{ignoring redefinition of Objective-C qualifier macro}}
+#define __unsafe_unretained
+// expected-warning@-1 {{ignoring redefinition of Objective-C qualifier macro}}
+#define __autoreleased
+// No warning because this is the default expansion anyway.
+
+// Check that this still expands to the right text.
+void test() {
+ goto label; // expected-error {{cannot jump from this goto statement to its label}}
+ __weak id x; // expected-note {{jump bypasses initialization of __weak variable}}
+label:
+ return;
+}
+
+#undef __strong
+#define __strong
+// No warning.
OpenPOWER on IntegriCloud