From ac200add36b40904bae8bbe7070e60c7f60b7c35 Mon Sep 17 00:00:00 2001 From: John McCall <rjmccall@apple.com> Date: Thu, 10 Dec 2015 23:31:01 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@255311 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticLexKinds.td | 3 +++ lib/Lex/PPDirectives.cpp | 24 +++++++++++++++++++++++ test/Lexer/objc_macros.m | 22 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 test/Lexer/objc_macros.m diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 34e4759a85f..38599d6b1f5 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/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/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index fd16168203a..298543e2e95 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/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/test/Lexer/objc_macros.m b/test/Lexer/objc_macros.m new file mode 100644 index 00000000000..0223bed4e3b --- /dev/null +++ b/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. -- GitLab