r255311 - In Objective-C, ignore attempts to redefine the ARC/GC qualifier macros.

John McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 10 15:31:02 PST 2015


Author: rjmccall
Date: Thu Dec 10 17:31:01 2015
New Revision: 255311

URL: http://llvm.org/viewvc/llvm-project?rev=255311&view=rev
Log:
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

Added:
    cfe/trunk/test/Lexer/objc_macros.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/lib/Lex/PPDirectives.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=255311&r1=255310&r2=255311&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Thu Dec 10 17:31:01 2015
@@ -293,6 +293,9 @@ def warn_pp_macro_hides_keyword : Extens
 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 '\\'">;

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=255311&r1=255310&r2=255311&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Thu Dec 10 17:31:01 2015
@@ -2266,6 +2266,30 @@ void Preprocessor::HandleDefineDirective
   // 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.

Added: cfe/trunk/test/Lexer/objc_macros.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/objc_macros.m?rev=255311&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/objc_macros.m (added)
+++ cfe/trunk/test/Lexer/objc_macros.m Thu Dec 10 17:31:01 2015
@@ -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 at -1 {{ignoring redefinition of Objective-C qualifier macro}}
+#define __weak
+// expected-warning at -1 {{ignoring redefinition of Objective-C qualifier macro}}
+#define __unsafe_unretained
+// expected-warning at -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.




More information about the cfe-commits mailing list