@@ -1012,7 +1012,7 @@ void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
10121012 }
10131013
10141014 // If this is a macro to be expanded, do it.
1015- if (MacroInfo *MI = II.getMacroInfo ())
1015+ if (MacroInfo *MI = II.getMacroInfo ()) {
10161016 if (!DisableMacroExpansion && !Identifier.isExpandDisabled ()) {
10171017 if (MI->isEnabled ()) {
10181018 if (!HandleMacroExpandedIdentifier (Identifier, MI))
@@ -1024,6 +1024,15 @@ void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
10241024 Identifier.setFlag (LexerToken::DisableExpand);
10251025 }
10261026 }
1027+ } else if (II.isOtherTargetMacro () && !DisableMacroExpansion) {
1028+ // If this identifier is a macro on some other target, emit a diagnostic.
1029+ // This diagnosic is only emitted when macro expansion is enabled, because
1030+ // the macro would not have been expanded for the other target either.
1031+ II.setIsOtherTargetMacro (false ); // Don't warn on second use.
1032+ getTargetInfo ().DiagnoseNonPortability (Identifier.getLocation (),
1033+ diag::port_target_macro_use);
1034+
1035+ }
10271036
10281037 // Change the kind of this identifier to the appropriate token kind, e.g.
10291038 // turning "for" into a keyword.
@@ -1467,6 +1476,10 @@ void Preprocessor::HandleDirective(LexerToken &Result) {
14671476 if (Directive[0 ] == ' d' && !strcmp (Directive, " define_target" ))
14681477 return HandleDefineDirective (Result, true );
14691478 break ;
1479+ case 19 :
1480+ if (Directive[0 ] == ' d' && !strcmp (Directive, " define_other_target" ))
1481+ return HandleDefineOtherTargetDirective (Result);
1482+ break ;
14701483 }
14711484 break ;
14721485 }
@@ -1723,9 +1736,14 @@ void Preprocessor::HandleDefineDirective(LexerToken &DefineTok,
17231736 // mode.
17241737 CurLexer->KeepCommentMode = Features.KeepMacroComments ;
17251738
1739+ // Create the new macro.
17261740 MacroInfo *MI = new MacroInfo (MacroNameTok.getLocation ());
17271741 if (isTargetSpecific) MI->setIsTargetSpecific ();
17281742
1743+ // If the identifier is an 'other target' macro, clear this bit.
1744+ MacroNameTok.getIdentifierInfo ()->setIsOtherTargetMacro (false );
1745+
1746+
17291747 LexerToken Tok;
17301748 LexUnexpandedToken (Tok);
17311749
@@ -1848,6 +1866,29 @@ void Preprocessor::HandleDefineDirective(LexerToken &DefineTok,
18481866 MacroNameTok.getIdentifierInfo ()->setMacroInfo (MI);
18491867}
18501868
1869+ // / HandleDefineOtherTargetDirective - Implements #define_other_target.
1870+ void Preprocessor::HandleDefineOtherTargetDirective (LexerToken &Tok) {
1871+ LexerToken MacroNameTok;
1872+ ReadMacroName (MacroNameTok, 1 );
1873+
1874+ // Error reading macro name? If so, diagnostic already issued.
1875+ if (MacroNameTok.getKind () == tok::eom)
1876+ return ;
1877+
1878+ // Check to see if this is the last token on the #undef line.
1879+ CheckEndOfDirective (" #define_other_target" );
1880+
1881+ // If there is already a macro defined by this name, turn it into a
1882+ // target-specific define.
1883+ if (MacroInfo *MI = MacroNameTok.getIdentifierInfo ()->getMacroInfo ()) {
1884+ MI->setIsTargetSpecific (true );
1885+ return ;
1886+ }
1887+
1888+ // Mark the identifier as being a macro on some other target.
1889+ MacroNameTok.getIdentifierInfo ()->setIsOtherTargetMacro ();
1890+ }
1891+
18511892
18521893// / HandleUndefDirective - Implements #undef.
18531894// /
@@ -1867,6 +1908,9 @@ void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
18671908 // Okay, we finally have a valid identifier to undef.
18681909 MacroInfo *MI = MacroNameTok.getIdentifierInfo ()->getMacroInfo ();
18691910
1911+ // #undef untaints an identifier if it were marked by define_other_target.
1912+ MacroNameTok.getIdentifierInfo ()->setIsOtherTargetMacro (false );
1913+
18701914 // If the macro is not defined, this is a noop undef, just return.
18711915 if (MI == 0 ) return ;
18721916
@@ -1910,7 +1954,8 @@ void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
19101954 CurLexer->MIOpt .EnterTopLevelIFNDEF (MacroNameTok.getIdentifierInfo ());
19111955 }
19121956
1913- MacroInfo *MI = MacroNameTok.getIdentifierInfo ()->getMacroInfo ();
1957+ IdentifierInfo *MII = MacroNameTok.getIdentifierInfo ();
1958+ MacroInfo *MI = MII->getMacroInfo ();
19141959
19151960 // If there is a macro, process it.
19161961 if (MI) {
@@ -1923,6 +1968,13 @@ void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
19231968 getTargetInfo ().DiagnoseNonPortability (MacroNameTok.getLocation (),
19241969 diag::port_target_macro_use);
19251970 }
1971+ } else {
1972+ // Use of a target-specific macro for some other target? If so, warn.
1973+ if (MII->isOtherTargetMacro ()) {
1974+ MII->setIsOtherTargetMacro (false ); // Don't warn on second use.
1975+ getTargetInfo ().DiagnoseNonPortability (MacroNameTok.getLocation (),
1976+ diag::port_target_macro_use);
1977+ }
19261978 }
19271979
19281980 // Should we include the stuff contained by this directive?
0 commit comments