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
This commit is contained in:
John McCall 2015-12-10 23:31:01 +00:00
parent ddd39efaed
commit ac200add36
3 changed files with 49 additions and 0 deletions

View File

@ -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 '\\'">;

View File

@ -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.

22
test/Lexer/objc_macros.m Normal file
View File

@ -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.